subtracting variables within two different netcdf files

David Halley picture David Halley · Jan 4, 2017 · Viewed 7.2k times · Source

I have two netcdf files: downwelling radiation named rsds.nc and confined radiation named rsns.nc. rsds.nc contains a variable named rsds and rsns.nc contains a variable named rsns. Now I would like to have the upwelling radiation rsus.nc by subtracting the variables within rsds.nc and rsns.nc, respectively.

I tried the following methods:

ncdiff rsds.nc rsns.nc rsus.nc
ncbo op_typ=diff rsds.nc rsns.nc rsus.nc

All of them produced a rsus.nc but the variable rsus, within this file is missing. Any idea of why this is so?

Answer

N1B4 picture N1B4 · Jan 5, 2017

As an alternative to @RichSignell's answer, you can combine variables into a single file and use ncap2 to perform the subtraction without renaming variables.

ncks -A rsns.nc rsds.nc 
ncap2 -s 'rsus=(rsds-rsns)' rsds.nc rsus.nc