Project

General

Profile

Conditional sampling in a single CDO command

Added by Kevin Helfer about 5 years ago

Hello everyone!

Is it possible to get a conditional selection of one variable depending on another variable in that same file while also using other selection criteria? More specifically: I am trying to select the u wind of all cloudy points (i.e. qc>0) within a specific lat/lon box and a specific time period (with both u and qc in one file).

I've already found cdo ifthen (which conveniently selects everything != 0 so that's very fitting for this example) but it seems to rely on two separate input files for the variable to be selected and the condition. I.e. something like: cdo ifthen FileWithPreselectedU FileWithQcMask Output. But what I want is more like: cdo ifthen (select[lon/lat/time/u] InputFile) (select[lon/lat/time/qc] ThatSameInputFile) Output.

Of course, I could first create these two files and then use ifthen, but that seems overly time- and disk-space-consuming to me. Is there another way?

Thanks,
Kevin


Replies (5)

RE: Conditional sampling in a single CDO command - Added by Ralf Mueller about 5 years ago

hi Kevin!

Sounds like a job for the expr operator:

  • pdf docu chap. 2.7.1
  • cdo -h expr

hth
ralf

RE: Conditional sampling in a single CDO command - Added by Kevin Helfer about 5 years ago

Hi Ralf,

not sure how that would work in a one-liner. Could you elaborate?

Kevin

RE: Conditional sampling in a single CDO command - Added by Ralf Mueller about 5 years ago

cdo -f nc infov -expr,'mask=(topo > 10) ? 7 : -7' -topo

-topo is the internal topography (varname topo), with the above call you create a new variable called mask depending on the value of the topography.

RE: Conditional sampling in a single CDO command - Added by Kevin Helfer about 5 years ago

Thanks, Ralf, that's what I needed. For other readers: the solution to my initial problem looks like this (using 999 as a fill value):

cdo -expr,'usample=(tot_qc_dia > 0) ? u : 999' -sellonlatbox,... -setgrid,... -select,startdate=...,enddate=... $inFile $outFile

RE: Conditional sampling in a single CDO command - Added by Ralf Mueller about 5 years ago

you could add a

setctomiss,999
- then CDO would handle the fillvalue as a real mask when computing things like fldmean or during interpolation

    (1-5/5)