bashnetcdfnco

Scale a specific field in a netcdf file keeping metadata


I would like to scale a specific field in a netcdf file by a constant.

Using CDO I know how to scale all fields by a constant $c:

cdo mulc,$c in.nc out.nc

but to apply this to a specific field I would have to cut out the variable, apply the scaling, and then delete the entry from the original file and merge the files, a solution which is cumbersome, slow and not very elegant:

cdo merge -mulc,$c -selvar,$var in.nc -delvar,$var in.nc out.nc

I tried to do it in nco

ncap -s  "ACSWTTEN=10*ACSWTTEN" in.nc out.nc

which works but it strips all the metadata from the field, as nco seems to create a new variable in this way. Again, I can manually redefine all the metadata, but this does not seem an elegant approach.

So, is there a way of scaling a single specific field in a netcdf file that contains many fields, but without destroying the metadata?


Solution

  • Yes, in fact your NCO command above is exactly right except you need to use ncap2 not ncap, its predecessor:

    ncap2 -s  "ACSWTTEN=10*ACSWTTEN" in.nc out.nc
    

    We will deprecate ncap soon because the situation is confusing :)