Project

General

Profile

Using -aexpr with variables

Added by Sara Minoli almost 3 years ago

Hello,

I just registered to the forum. I hope I'm posting in the correct place.

I'm processing ncdf files with cdo. I need to convert a few files into masks, containing either 1 or NA, to then be multiplied to other files.
So, I want to replace all non-missing values with 1, while leaving all missing values as NA.

I tried it for a single file and it worked:

cdo -aexpr,'pr=(pr != -9.0E33) ? 1 : -9.0E33;' <pr_ifile> <pr_ofile>

However, if I want to use this in a loop calling different files and variables, I can't figure out how to pass a variable ${var} to -aexpr.

for var in "pr" "tas" 
do
  cdo -aexpr,'${var}=(${var} != -9.0E33) ? 1 : -9.0E33;' <$var_ifile> <$var_ofile>
done

Where is my mistake here?
Thanks!
Sara


Replies (4)

RE: Using -aexpr with variables - Added by Karin Meier-Fleischer almost 3 years ago

Hi Sara,

cdo -expr,'pr = ((pr != missval(pr))) ? 1 : missval(pr)' infile.nc outfile.nc

-Karin

RE: Using -aexpr with variables - Added by Sara Minoli almost 3 years ago

Thanks Karin!

That's better indeed. But how to use ${var} instead of pr, please?

Cheers
Sara

RE: Using -aexpr with variables - Added by Karin Meier-Fleischer almost 3 years ago

For example:

for var in "pr" "tas" 
do
    cmd="${var} = ((${var} != missval(${var}))) ? 1 : missval(${var})" 

    cdo -expr,"${cmd}" ${var}_file ${var}_outfile
done

RE: Using -aexpr with variables - Added by Sara Minoli almost 3 years ago

Fantastic! Thank you :)

    (1-4/4)