Project

General

Profile

cdo filter one variable by a second

Added by Sylvia Sullivan about 4 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 4 years ago

Hi Sylvia!

I see multiple options:

  1. If x and y are on the same grid, you can use comparison operators like gtc on var y, to create a 0-1-mask and multiply is with var x to select the corresponding values from x. 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 same div operation can be done on y 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)
  2. the ternary operator of expr can also be used
    cdo -setctomiss,-99999 -expr,'x_masked=((y > 4000) ? x : -99999' input.nc x_selected_by_y.gt.4000.nc
    I use setctomiss to indicate the masked values as missing values.

hth
ralf

RE: cdo filter one variable by a second - Added by Sylvia Sullivan about 4 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 4 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

    (1-3/3)