Project

General

Profile

Piping expr into single command for python interface

Added by Jan Streffing about 2 years ago

I'm reading unstructured mesh files via cdo into python, remapping them to regular grid on the fly:

data_ref = cdo.yearmean(input='-fldmean -setctomiss,0 -remap,r'+remap_resolution+','+meshpath+'/weights_unstr_2_r'+remap_resolution+'.nc -setgrid,'+meshpath+'/'+mesh_file+' '+str(path),returnArray=variable)

Filled in with variables:

-fldmean -setctomiss,0 -remap,r360x181,weights_unstr_2_r360x180.nc -setgrid,CORE2_finaltopo_mean.nc sst.fesom.1850.nc

I have a file woa 2009 that contains ocean an basin field where each basin has its own id (data.nc). I know how to work that file such that only the mask from the basin I want is left and everything else is 0. When working in a console I could create a mask file from here:

cdo vardes data.nc 
  -1  basin         basin code [ids]

cdo -chname,basin,${variable} -divc,${basin_id} -expr,'basin=(basin == ${basin_id}) ? basin : 0' data.nc ${basin_name}.nc

In in shell I would multiply the remapped field with the correct mask file in a second separate cdo command to get the final field.
The crux for me, is that for the python interface I would like to do this all on one command chain, so that I can dump the result straight into a numpy masked array, without ever having to write anything to disk. Is this possible? I can't quite wrap my head around what the chain would look like.

Something like this?

data1 = cdo.yearmean(input="-fldmean -divc,2 -expr,'"+variable+"=("+variable+" == "+mask_id+") ? "+variable+" : 0' data.nc -setctomiss,0 -remap,r"+remap_resolution+","+meshpath+"/weights_unstr_2_r"+remap_resolution+".nc -setgrid,"+meshpath+"/"+mesh_file+" "+str(path),returnArray=variable)
sst.fesom.1850.nc (5.83 MB) sst.fesom.1850.nc example data
weights_unstr_2_r360x180.nc (50.7 MB) weights_unstr_2_r360x180.nc remapping weights
data.nc (2.04 MB) data.nc woa 2009 ocean basin field (https://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NODC/.WOA09/.Masks/.basin/index.html)
CORE2_finaltopo_mean.nc (45.9 MB) CORE2_finaltopo_mean.nc grid description