Search code examples
netcdfcdo-climate

chaining operators in cdo


I am calculating C factor by using the following equation:

C factor =1, coverfraction= 0
C factor =-0.083 xlg(coverfraction) + 0.417, 0 < coverfraction < 78.3%, C factor= 0, coverfraction > 78.3%,

To calculate the total C factor, I apply part of the equation C factor = 0.417 − 0.083 × lg(coverfraction). But I dont know how to apply the conditions of cover fraction in the script using the cdo. In this case the log of all values is calculated including 0 and the values more than 78.3%.

The code is:

echo "Log: " $gc_ofile
cdo -O -L -log inputfile.nc ofilelog.nc
cdo -O -L "-mulc,-0.083" outputlogfile.nc ofilemulti.nc
cdo -O -L "-addc,0.417" ofilemulti.nc cfactor.nc

Solution

  • For these kinds of operations it is often easier to use the cdo command expr

    If the name of the coverfraction in the netcdf file (called coverfrac.nc) is exactly that then you can use expr like this:

    cdo expr,'cfactor=0.417−0.083*log(coverfraction)' coverfrac.nc out1.nc
    

    This will make a new variable "cfactor" as desired.

    For the other part, this is where cdo's masking comes in (I'll be posting a video on this topic soon on my youtube climate unboxed channel and will update this post when the link is available).

    So you can create a file with 0 everywhere that coverfraction is above 78.3% and 1 otherwise like this.

    cdo lec,78.3 coverfrac.nc mask.nc
    

    Now you need to multiply this by the first output file to set the result to zero where coverfrac> 78.3% and leave it unchanged otherwise.

    cdo mul out1.nc mask.nc myfunction.nc