Regridding from MOM6 native coordinates

At the moment, output from MOM6 returns BGC variables on the z* grid which I understand varies in time, given by e or h. Is there a neat way to regrid the BGC data onto a fixed grid and then interp it onto the same vertical grid as the physical data which is output on the z grid?

I was trying something like this but it doesn’t work!

import xarray as xr

import xgcm

from xgcm import transform

# bgc data 

ds = bgc3d_m_05_tides

# Cell interface depth to cell centre depth

grid = Grid(phy_m_05_tides, coords={'Z': {'center': 'z_l', 'left': 'z_i'}},autoparse_metadata=False)

z= grid.interp(phy_m_05_tides.e, 'Z', boundary='fill', fill_value=None)

# add the depth field to the ds

ds= ds.assign_coords(z=z)

# this is the target depth I want to eventually put the BGC data onto 

z_fixed = phy_m_05_tides.z_l

# this is where I get stuck!?

ds=ds.interp(z=zl,kwargs={"fill_value": "extrapolate"})

#check to see if it now plots as a nice cross section. (At the moment it doesn’t!)

ds.no3.isel(time=1).isel(xh=100).plot(vmin=0,vmax=2.5e-5)

Thank you

Hi @Lizzie. This is how I’d usually do this:

def interfaces_to_centres(da_zi):
    """
    Average interface quantities to get centre quantities
    """
    zl = (da_zi.zi.values[:-1] + da_zi.zi.values[1:]) / 2
    da_zi = da_zi.rename(zi="zl")
    lowers = da_zi.isel(zl=slice(1,None)).assign_coords({"zl": zl})
    uppers = da_zi.isel(zl=slice(None,-1)).assign_coords({"zl": zl})
    da_zl = (lowers + uppers) / 2
    da_zl.zl.attrs["axis"] = "Z"
    return da_zl

# Average e at interfaces onto cell centres; flip sign so depths are positive
e_zl = -1 * interfaces_to_centres(phy_m_05_tides.e)

# Get target depths
z_fixed = phy_m_05_tides.z_l.values

# Interpolate no3 onto target levels
grid = Grid(ds, periodic=False)
no3_z = grid.transform(
    ds.no3,
    'Z',
    z_fixed,
    target_data=e_zl,
    method='linear',
    mask_edges=True
)

I’ve tried to write this to use your variable names etc so that you can just drop into your code. Apologies in advance if it doesn’t work first go.

1 Like

@Lizzie, I hope you don’t mind, I changed the title slightly and added a few more tags to hopefully make this post a little more findable.