Search code examples
pythonnetcdfnco

Adding bounds variable to netcdf file


I would like to add latitude bounds to an existing netCDF file. See http://www.cgd.ucar.edu/cms/eaton/netcdf/CF-20010629.htm#bnds for definition of bounds.

Is there a command that can add bounds automatically? I am open to any solution using nco or netCDF4 or cdms toolkit. However, I would prefer it to be a post processing solution.


Solution

  • Add bounds to an existing coordinate like this:

    ncap2 -O -s 'crd@bounds="crd_bnds";defdim("bnds",2);crd_bnds[$crd,$bnds]=0.0;*crd_dff=0.5*(crd(1)-crd(0));crd_bnds(:,0)=crd-crd_dff;crd_bnds(:,1)=crd+crd_dff;' in.nc out.nc
    

    This yields

    zender@aerosol:~$ ncks --cdl -v crd ~/foo2.nc
    netcdf foo2 {
      dimensions:
        bnds = 2 ;
        crd = 10 ;
    
      variables:
        int crd(crd) ;
          crd:bounds = "crd_bnds" ;
    
        double crd_bnds(crd,bnds) ;
    
      data:
        crd = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ;
    
        crd_bnds = -0.5, 0.5, 0.5, 1.5, 1.5, 2.5, 2.5, 3.5, 3.5, 4.5, 4.5, 5.5, 5.5, 6.5, 6.5, 7.5, 7.5, 8.5, 8.5, 9.5 ;
    
    } // group /
    

    Here the coordinate is named "crd", and the bounds are named "crd_bnds". The formula for the bounds values can be simple when the coordinate changes by a fixed value (as above), or more involved to account for variable spacing (exercise for reader). We will add an ncap2 function to do this automatically in the near future.

    cz