How to add a constant to only one variable in netcdf
Added by Divyam Garg over 1 year ago
I have a netcdf file containing t2m (Kelvin) and tp (metres) data from ERA5 for multiple years. I wish to convert temperature to deg C and rainfall to millimetres using addc and mulc operators, also change the units with cdo. But when I try doing that, I am unable to specify the variable. For example, I gave the command -
$cdo -setattribute,t2m@units="degC" -addc,-273.15 daily_t2mtp_1975_2005.nc daily_t2mtp_degC_1975_2005.nc
This command added -273.15 to not only t2m but also to tp variable.
How can I do this?
Replies (6)
RE: How to add a constant to only one variable in netcdf - Added by Ralf Mueller over 1 year ago
hi!
There are several possiblities. I prefer the expr
operator to change both variables in a single call:
cdo -setattribute,t2m@units="degC",tp@units="mm" -expr,'t2m=t2m-273.15;tp=tp*1000' daily_t2mtp_1975_2005.nc changed.nc
setpartab is another option. In that case you can choose to perform unit-conversion semi-automatically. I'd choose this for a larger number of variables to change.
hth
ralf
RE: How to add a constant to only one variable in netcdf - Added by Robert Kvak over 1 year ago
Ralf Mueller wrote in RE: How to add a constant to only one variable in netcdf:
hi!
There are several possiblities. I prefer theexpr
operator to change both variables in a single call:
[...]setpartab is another option. In that case you can choose to perform unit-conversion semi-automatically. I'd choose this for a larger number of variables to change.
hth
ralf
Hello Ralf,
What to do if I have 10 variables and I'd like to multiply only the first variable by 2, while maintaining other 9 variables without any change, preserving all parameter names within output (?)
I've used
cdo expr,var1name=var1name*2 input output
however, only that particular var1name remained with the changed parameter name as "var1" and not "var1name" from the input. Other 9 variables from the input are gone.
Thank you
RE: How to add a constant to only one variable in netcdf - Added by Ralf Mueller over 1 year ago
Hi! Robert!
Try the aexpr
operator instead. It will keep the remaining data from the input
cheers
ralf
RE: How to add a constant to only one variable in netcdf - Added by Robert Kvak over 1 year ago
Many thanks Ralf,
it works well.
RE: How to add a constant to only one variable in netcdf - Added by Jose Coffey about 1 year ago
What should I do if I have ten variables and I want to multiply just the first variable by two, while keeping the other nine variables unchanged and ensuring that all parameter names are preserved inside the output?
However, just that specific var1name remained with the modified parameter name as "var1" and not "var1name" from the input. I used the command cdo expr,var1name=var1name*2 to generate the result. Additional nine variables from the input have been removed.
RE: How to add a constant to only one variable in netcdf - Added by Estanislao Gavilan about 1 year ago
Hi Jose,
you start defining the new variable with expr as you well did (do not forget the connotation mark ""). Then, you use the command merge to combine this new variable with the previous dataset. Finally, you delete the original var1name with delname. Something like this
cdo -z zip_6 -delname,var1name -merge input.nc -expr,"new_var1=var1name*2" input.nc output.nc