# Regridding using xESMF with 3D Mask

**URL:** <https://discourse.pangeo.io/t/regridding-using-xesmf-with-3d-mask/1579>\
**Category:** Science\
**Created:** [June 18, 2021, 4:29pm UTC](https://discourse.pangeo.io/t/regridding-using-xesmf-with-3d-mask/1579 "2021-06-18T16:29:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophrenkl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/christophrenkl/32/1052_2.png) [@christophrenkl](https://discourse.pangeo.io/u/christophrenkl)\
**Post date:** [June 18, 2021, 4:29pm UTC](https://discourse.pangeo.io/t/regridding-using-xesmf-with-3d-mask/1579/1 "2021-06-18T16:29:31Z")

</div>

Hello everyone!

Hopefully this is the right place to ask this question:

I am currently trying to create initial conditions for a high-resolution, regional ocean model (based on ROMS) from the [GLORYS12v1](https://resources.marine.copernicus.eu/?option=com_csw&view=details&product_id=GLOBAL_REANALYSIS_PHY_001_030) reanalysis dataset. Since the model grids and vertical coordinate systems are different I am interpolating the three-dimensional (longitude, latitude, depth) GLORYS data horizontally onto the model grid using the xESMF module which generally works like a charm.

To account for the differences in the land/sea masks between my model and the reanalysis, I am using the extrapolation feature described [here](https://pangeo-xesmf.readthedocs.io/en/latest/notebooks/Masking.html#Extrapolation). This requires a `mask` variable which can only be 2D because of the underlying ESMF design.

However, the land/sea mask in GLORYS12v1 changes with depth, so that I have to apply the regridding for each vertical level separately. Is there any preferred way to do that?

Suppose that `ds` a `xarray.Dataset()` with dimensions `depth`, `lon`, and `lat` containing multiple variables including a 3D `mask`. Off the top of my hat, I can think of two options (both untested):

1. Iterate over `depth` dimension, build the `xesmf.Regridder()` for each level and manually create an output dataset `ds_int` with the interpolated data. I’m afraid that this could be quite slow.
2. Write a dummy function that takes 2D fields as input arguments and applies the regridding for an individual model level. I would then apply this function to all depth levels using the `xarray.apply_ufunc()` method.

Do you have any other suggestions, tips, or tricks how to best approach this (in the best case computationally efficiently)?

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![rabernat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rabernat/32/22_2.png) [@rabernat](https://discourse.pangeo.io/u/rabernat)\
**Post date:** [June 23, 2021, 2:19pm UTC](https://discourse.pangeo.io/t/regridding-using-xesmf-with-3d-mask/1579/2 "2021-06-23T14:19:57Z")

</div>

Thanks for your question @christophrenkl!

Since no one has replied here, I’m guessing your issue is a little too technical for the forum. You’re probably best off opening an xesmf issue:

> **[pangeo-data/xESMF](https://github.com/pangeo-data/xESMF/issues)**
>
> Universal Regridder for Geospatial Data. Contribute to pangeo-data/xESMF development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![christophrenkl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/christophrenkl/32/1052_2.png) [@christophrenkl](https://discourse.pangeo.io/u/christophrenkl)\
**Post date:** [June 23, 2021, 6:25pm UTC](https://discourse.pangeo.io/t/regridding-using-xesmf-with-3d-mask/1579/3 "2021-06-23T18:25:36Z")

</div>

Thank you for your suggestion, @rabernat! I just opened an [issue on GitHub](https://github.com/pangeo-data/xESMF/issues/98).
