Project

General

Profile

Applying expr when there is a minus symbol in variable name

Added by Robert Wilson almost 5 years ago

I am attempting to apply expr to a file where the variable name I want to use is "SPM-OC5_mean".

I have tried various variations of "cdo expr,'new=SPM-OC5_mean' infile outfile". However, none of them seem to work.

Is there a way to handle variable names which contain arithemtic operators?

Regards

Robert


Replies (7)

RE: Applying expr when there is a minus symbol in variable name - Added by Karin Meier-Fleischer almost 5 years ago

Hi Robert,

yes, I didn't manage that either. Do you want to rename the variable only than use the chname operator

cdo -chname,SPM-OC5_mean,new infile outfile

If you want to do some computations you can rename the variable before you do it.

For exmple:

cdo -chname,SPM-OC5_mean,dummy infile tmp.nc
cdo -expr,'new = ((dummy > 0.0)) ? 1.0 : 0.0' tmp.nc outfile.nc
rm -rf tmp.nc

-Karin

RE: Applying expr when there is a minus symbol in variable name - Added by Robert Wilson almost 5 years ago

Hi Karin

Thanks. That is one option. My aim is to have a function that wraps around cdo and expr and can work more generally, but I think I can see a way of getting that to work using the chname method you suggest.

Robert

RE: Applying expr when there is a minus symbol in variable name - Added by Ralf Mueller almost 5 years ago

Is there a way to handle variable names which contain arithmetic operators?

the short answer is: no. I don't see any options to do so. IMO this is a bad idea anyway, better use an underline and keep the 'SPM-OC5_mean" as longname or a custom attribute value.

you could implement the replacement with underline and back-replacement afterwards as part of your routing I guess.

cheers
ralf

RE: Applying expr when there is a minus symbol in variable name - Added by Robert Wilson almost 5 years ago

Thanks Ralf

Yes, I agree. It's a bad naming convention, but one used by some of the common netcdf products I use.

I'll just make my wrapper on exp super strict, forcing users to use chname before calling it when variables have arithmetic symbols in them.

Cheers

Robert

RE: Applying expr when there is a minus symbol in variable name - Added by Ralf Mueller almost 5 years ago

in which language do your write your code?

RE: Applying expr when there is a minus symbol in variable name - Added by Robert Wilson almost 5 years ago

Hi Ralph

It is an R package that acts as a wrapper for cdo. https://github.com/r4ecology/rcdo

Cheers

Robert

RE: Applying expr when there is a minus symbol in variable name - Added by Ralf Mueller almost 5 years ago

alright - I won't be of much help for R, though. Never used it much, except for some huge graph representation. I have libs for Ruby and Python, but R didn't seem to offer much meta-programming techniques. So I dropped the plan to come up with a R-interface myself. Cool, that your wrote one!

cheers
ralf

    (1-7/7)