Apply for help about vertical interpolation in MPAS-ocean #5335
Replies: 2 comments 24 replies
-
@shipf16, thanks for your question! I also received your email. We don't have a great tool for doing vertical interpolation like you are requesting. What we do have is a python function in a conda package called #!/usr/bin/env python
from mpas_analysis.shared.interpolation import interp_1d
import xarray as xr
import numpy as np
sim = \
'20221129.CMPASO-JRA1p4_TL319_EC30to60E2r2.fix-time-series-stats.chrysalis'
# you could also iterate over years and months
dates = ['0001-01-01']
# a list of 3D variables you want to interpolate
variables = ['timeMonthly_avg_activeTracers_temperature',
'timeMonthly_avg_activeTracers_salinity',
'timeMonthly_avg_zMid']
# we often use a restart file as an MPAS mesh file, needed here for masking
mesh_filename = f'{sim}.mpaso.rst.0001-02-01_00000.nc'
# you will have to figure out how to read in the EN4 vertical grid as an
# xarray dataset and extract the (positive up) z coordinate
# note that the coordinate and the dimension need to be named differently
# (i.e. not following CF-conventions) because of a quirk in the interp_1d function,
# so z and nz in this example.
z = xr.DataArray(data=np.linspace(0, -5000, 42), dims=['nz'])
# now do the interpolation
# first, make a cell mask
ds_mesh = xr.open_dataset(mesh_filename)
# the index of the bottom cell, converted from Fortran to Python indexing
max_level_cell = ds_mesh.maxLevelCell - 1
n_vert_levels = ds_mesh.sizes['nVertLevels']
vert_index = xr.DataArray.from_dict({'dims': ('nVertLevels',),
'data': np.arange(n_vert_levels)})
# make a mask for where cells are above the bathymetry
cell_mask = vert_index <= max_level_cell
# then, interpolate the requested variable for each date
for date in dates:
in_monthly_filename = \
f'{sim}.mpaso.hist.am.timeSeriesStatsMonthly.{date}.nc'
out_monthly_filename = f'mpaso.T_S_to_EN4.{date}.nc'
ds = xr.open_dataset(in_monthly_filename)
# just keep the variables we want
ds = ds[variables]
# drop the Time dimension, since this interferes with the interpolation
ds = ds.isel(Time=0)
for variable in variables:
# mask out invalid cells
ds[variable] = ds[variable].where(cell_mask)
# add the destination vertical coordinate
ds['z'] = z
# do the interpolation
ds = interp_1d(ds, inInterpDim='nVertLevels',
inInterpCoord='timeMonthly_avg_zMid',
outInterpDim='nz', outInterpCoord='z')
# the coordinate and dimension need to have different names for interp_1d
# but can be renamed after interpolation to be the same if you like:
ds = ds.rename({'nz': 'z'})
ds.to_netcdf(out_monthly_filename) It is probably a good idea to do this vertical interpolation before you use ncremap to do horizontal interpolation. If you paste an I should also note that this performs bilinear interpolation using the |
Beta Was this translation helpful? Give feedback.
-
I see, I thought the purpose was for analysis. If the purposes is assimilating EN4 in MPAS-Ocean, you probably want to be interpolating in the other direction. You should be interpolating the EN4 data to the MPAS-Ocean depth. Since EN4 is on a regular grid, you just need to do interpolation first to the MPAS horizontal location (e.g. using ncremap from EN4 to MPAS, rather than the other way around) and then do simple 1D vertical interpolation from your z coordinate to MPAS-O's zMId. I think the way you're doing it is the wrong way around for assimilating data into MPAS-Ocean. What you want to do is much easier than what I originally thought -- it's easy to get data from a regular grid in 3D to the MPAS-Ocean points. It's much harder to go the other direction. Please let me know if what I just said is accurate. If so, do you need specific guidance in this direction? I may have time to write an example script tomrrow. |
Beta Was this translation helpful? Give feedback.
-
Dear Scientist,
Hello, everyone, I meet a technical problem with the vertical interpolation and apply for your guidance. For interpolating in-depth, I would like to compare and compute the difference in temperature and salinity between the ocean outputs from E3SM v2 and ocean reanalysis data (EN4.2.2). However, the vertical layers of MPAS are 60 layers and EN4.2.2 data has 42 layers. I am thinking whether I could convert the 60 layers of MPAS-ocean to 42 layers of EN4.2.2.
However, in the MPAS-ocean, I find that the depth coordinate varies with space and time. For example, in the layer 10, the depth varies from one grid cell to another. In this case, I am thinking about whether I could interpolate the MPAS-ocean outputs to the same depth from EN4 data. Are there any similar NCO commands like this (ncremap -P mwf -s grd_ocn -g grd_out) to perform the vertical interpolation prior to the horizontal interpolation? I want to compare the difference between MPAS-ocean outputs and EN4 data on each same depth. Could you please provide me with more guidance and help?
Thank you very much!
Beta Was this translation helpful? Give feedback.
All reactions