bashnetcdfncocdo-climate

clipping data - setting values below a threshold to the threshold in a netcdf file


I want to set all values below a constant c to c itself in a netcdf file: file.nc

A solution using climate data operators (CDO) would be

cdo mul -gec,$c file.nc file.nc t1.nc
cdo add -mulc,$c -ltc,$c file.nc t1.nc output.nc
rm -f t1.nc

But is there a neater/shorter way to do this?


Solution

  • There is also a more efficient way to do this with the Climate Data Operator (CDO) using the expr. Adapting the example from this post to the specific example from this question, assuming that the name of the variable is x and the constant is c, then the command would be something like this:

    cdo -expr,'x = ((x > c)) ? x : c' infile.nc outfile.nc
    

    Where ? and : are ternary conditional operators, and x ? y : z, means y if x not equal 0, else z. Additional information related to operators and expressions are available on the CDO Manual, section 2.7.1.