0

I have a netcdf file called data.nc and a masking file called mask.nc. The data.nc file has a 3D variable A with dimensions over (time,long,lat), and 4D variable B with dimensions over (time,depth,long,lat).

The mask.nc file has two masking variables mask_3d (time,long,lat) and mask_4d (time,depth,long,lat) with 0 and 1 values.

So far, I am masking each variable separately using:

cdo -div -selname,A data.nc -selname,mask_3d mask.nc out.nc

and

cdo -div -selname,B data.nc -selname,mask_4d mask.nc out2.nc

My question is:

How can I mask both variables A and B in data.nc using only one command ?

Amr Talaat
  • 81
  • 2
  • 10
  • not sure exactly what your end point is, you mean you want both masked variables in the same output file instead of two separate ones? – ClimateUnboxed Nov 03 '22 at 14:42

1 Answers1

2

I'm not sure this really qualifies as an answer, perhaps it should be more of a comment, but I think this is not possible with cdo. If I understand correctly, you essentially want the output to be in the same file (cdo only allows one output file, so by definition your question implies this).

The reason is that you would need to cat the two output files together in order to get what you want, but because cdo cat allows you to cat a variable number of files together, and even use wildcards (e.g. cdo cat *.nc out.nc) then it doesn't know how many input files to expect and thus cdo does not let you pipe such commands in combination with other commands as it can't interpret them safely.

Thus you would need to keep this as three lines (at least for a cdo based solution, I think, but stand to be corrected):

cdo -div -selname,A data.nc -selname,mask_3d mask.nc out1.nc
cdo -div -selname,B data.nc -selname,mask_4d mask.nc out2.nc
cdo cat out?.nc out.nc 

sorry about that... That said, once commands start to get long, I think that keeping them separate as here aids legibility of the code.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • Thanks Adrian for your comment, your point is clear for me. However, I was wondering if we can use only one line command to produce only one output file with the two variables being masked. For example: `cdo -div -selname,A,B data.nc -selname,mask_3d,mask_4d mask.nc out.nc` – Amr Talaat Nov 03 '22 at 15:25
  • 1
    I don't think that is possible, to my best knowledge as a caveat... – ClimateUnboxed Nov 03 '22 at 15:33
  • Thanks Adrian for clarification. – Amr Talaat Nov 03 '22 at 15:36
  • 2
    Agreed @AdrianTompkins. Highly unlikely this can be done in one line. CDO is essentially designed to work with multiple files only when they have compatible dimensions, but there is a conflict in this case. I don’t see any way to handle two files with 3d and 4d variables in one command – Robert Wilson Nov 03 '22 at 15:57