cdo filter one variable by a second
Added by Sylvia Sullivan about 5 years ago
I would like to extract values of var x from a file only when var y is greater than a threshold. I do not need the output file to be structured; I just want to look at relationships statistically. x and y are both in the same file initially, so if I understand right, I can do the following for a threshold of zero:
cdo selvar,y infile.nc y_values.nc
cdo selvar,x infile.nc x_values.nc
cdo ifthen y_values.nc x_values.nc x_ygt0_values.nc
Is there any way to do it for a non-zero threshold? Not seeing anything in the CDO manual off-hand.
Replies (3)
RE: cdo filter one variable by a second - Added by Ralf Mueller about 5 years ago
Hi Sylvia!
I see multiple options:
- If
x
andy
are on the same grid, you can use comparison operators likegtc
on vary
, to create a 0-1-mask and multiply is with varx
to select the corresponding values fromx
. This also works with time varying data because the mask can be have a time axis:cdo -gtc,4000 -selname,y input.nc mask_y.nc cdo -div -selname,x input.nc mask_y.nc x_masked_by_y.gtc.4000.nc
The samediv
operation can be done ony
itself so that only the value remain non-missing in the corresponding output (note: division my as 0-1-mask creates missing values in the masked regions) - the ternary operator of
expr
can also be usedcdo -setctomiss,-99999 -expr,'x_masked=((y > 4000) ? x : -99999' input.nc x_selected_by_y.gt.4000.nc
I usesetctomiss
to indicate the masked values as missing values.
hth
ralf
RE: cdo filter one variable by a second - Added by Sylvia Sullivan about 5 years ago
Finally got around to testing this. I'm using solution 1 and it works like a charm. Thanks so much for your help, Ralf!
RE: cdo filter one variable by a second - Added by Ralf Mueller about 5 years ago
hi Sylvia,
thx for the final feedback! the trick with the division by a mask helped me in many cases tbh.
cheers
ralf