Hello everyone. I need to re-map some data (temperature and salinity) from pressure-latitude space into density-latitude space. In density-latitude space, I do some calculations. Then, I need to re-map the results back into pressure-latitude space, so I can do some final calculations in pressure-latitude space.
I can do the first step, which is re-mapping temperature and salinity from pressure-latitude space into density-latitude space. Below is my working code
from xgcm import Grid
# Input Data must be 3D with depth, x-coord (lat), time.
def recast_property_from_depth_to_gamma_space(max_x_samples,max_z_samples,max_t_samples, density_bins, gridded_density, gridded_temperature, gridded_salinity):
if gridded_density.shape[0] != max_z_samples:
raise ValueError('Gridded fields must have max_z_samples rows')
elif gridded_density.shape[1] != max_x_samples:
raise ValueError('Gridded fields must have max_x_samples cols')
elif gridded_density.shape[2] != max_t_samples:
raise ValueError('Gridded fields must have max_t_samples in 3rd dim')
elif gridded_density.ndim != 3:
raise ValueError('Gridded fields must have 3 dimensions (pressure, latitude, time)')
# xgcm has to work on an xarray.Dataset, so first step is to convert and combine the xarray dataArrays into data sets
ds = gridded_salinity.to_dataset().rename({'salt':'SA'})
# add tempearture to the data set as a variable.
ds = ds.assign({'CT': gridded_temperature})
# xgcm also needs to know how density varies alongside temperature and salinity, so add that to the data set as a data variable.
ds = ds.assign({'density': gridded_density})
# Now, do the conversion
# First create an xgcm grid object
grid = Grid(ds, coords={'Z': {'center':'pressure'}}, periodic=False)
# and transform into density coordinates
gridded_salinity_gamma_space = grid.transform(ds.SA, 'Z', density_bins, target_data=ds.density)
gridded_temperature_gamma_space = grid.transform(ds.CT, 'Z', density_bins, target_data=ds.density)
# transpose that the the order of density, latitude, time is re-instated
gridded_salinity_gamma_space = gridded_salinity_gamma_space.transpose('density','latitude','time')
gridded_temperature_gamma_space = gridded_temperature_gamma_space.transpose('density','latitude','time')
return gridded_temperature_gamma_space, gridded_salinity_gamma_space
# define the target values in density, linearly spaced
[gridded_temperature_gamma_space, gridded_salinity_gamma_space] = recast_property_from_depth_to_gamma_space(max_x_samples, max_z_samples, max_t_samples, density_bins, gridded_gamma, gridded_temperature, gridded_salinity);
gridded_salinity_gamma_space
But, I cannot figure out how to re-map that back into pressure-latitude space.
There may well be a more sensible way to do this calculation. Basically, I need to know how temperature and salinity map into the spaces of pressure-latitude, which is fixed over time, and density-latitude, which is not fixed over time.
Below is my failing code and a screenshot out of what the error is
gridded_field = dT_dgamma
gridded_field # this is in density space, I want to know where dT_dgamma sits in depth/pressure space
ds = gridded_field.to_dataset() # make sure it is a dataSet, not a data Array
ds = ds.assign({'pressure': p_levels_to_interpolate_to}) # tell it what pressure is (pressure does not vary with time or space)
grid = Grid(ds, coords={'Z': {'center':'density'}}, periodic=False)
grid.transform(ds.CT, 'Z', p_levels_to_interpolate_to, target_data=ds.pressure)