Reputation: 531
I have a netcdf files with 3 variables, nammed v1
, v2
, v3
.
I would like to aggregate those variables and create a new variable v4
, as follow:
v4 = (v1*0.1)+(v2*0.2)+(v3*0.2)
I know how to aggregate 2 variables using cdo :
cdo expr,’sum=var1+var2;’ ifile ofile
But how could I handle more specific netcdf calculations like the one required?
Upvotes: 0
Views: 842
Reputation: 8107
you can also use ncap2 as an alternative
ncap2 -O -s "v4=(v1*0.1)+(v2*0.2)+(v3*0.2)" ifile.nc ofile.nc
Although I would usually use the cdo solution as suggested by Robert Wilson, the nice thing with nco is the ability to define all the metadata too:
ncatted -O -a units,v4,c,c,"units goes here" ofile.nc
ncatted -O -a long_name,v4,c,c,"long_name goes here" ofile.nc
EDIT: As Charlie correctly points out in his answer to this question, I should have highlighted the fact that ncap2 ports metadata from the source variables, so if metadata such as units already exists you need to either specify the new values in the ncap command as per his answer, or use ncatted in "modify" mode for those attributes.
Upvotes: 1
Reputation: 6352
By default, NCO propagates the metadata from the first variable on the RHS to the LHS so v4
gets the same attributes as v1
in Adrian's example. You can change or augment those attributes in the same ncap2
command using the at-sign @
to name attributes, e.g.,
ncap2 -O -s 'v4=(v1*0.1)+(v2*0.2)+(v3*0.2);v4@long_name="Fourth variable";v4@units="meters"' in.nc out.nc
Upvotes: 1
Reputation: 3417
CDO will be able to handle the formula you require:
cdo expr,’v4=(v1*0.1)+(v2*0.2)+(v3*0.2)’ ifile ofile
If you want to add the variable to the files, just do this:
cdo aexpr,’sum=var1+var2;v4=(v1*0.1)+(v2*0.2)+(v3*0.2)’ ifile ofile
Upvotes: 3