NetCDF Operators (NCO)

This page provides an example of how to use NetCDF Operators (NCO) with SILO NetCDF files.

Software and data requirements

The NetCDF Operators (NCO) toolkit is a suite of command-line programs to interact with NetCDF files. You can download pre-built binaries or compile from source.

Example 1. Display the metadata

The ncdump tool can be used to view the metadata within a NetCDF file. For example, to view the file structure and attributes of the 1981 monthly rainfall data:

  1. Download the monthly rainfall data for 1981. This can be done using a variety of methods.
  2. Run the the following command:
    ncdump -h 1981.monthly_rain.nc

    This shows the structure of the file (time, lat and lon coordinates) and ancillary information such as the data packing parameters (scale_factor, add_offset). The truncated output of the previous example is:

    netcdf \1981.monthly_rain {
    dimensions:
    	lat = 681 ;
    	lon = 841 ;
    	time = UNLIMITED ; // (12 currently)
    variables:
    	double lat(lat) ;
    		lat:long_name = "latitude" ;
    		lat:standard_name = "latitude" ;
    		lat:units = "degrees_north" ;
    		lat:axis = "Y" ;
    	double lon(lon) ;
    		lon:long_name = "longitude" ;
    		lon:standard_name = "longitude" ;
    		lon:units = "degrees_east" ;
            	lon:axis = "X" ;
    	double time(time) ;
    		time:units = "days since 1981-01-01" ;
    		time:calendar = "standard" ;
    		time:axis = "T" ;
    	short monthly_rain(time, lat, lon) ;
    		monthly_rain:_FillValue = -32767s ;
    		monthly_rain:long_name = "Monthly rainfall" ;
    		monthly_rain:units = "mm" ;
    		monthly_rain:scale_factor = 0.1 ;
    		monthly_rain:add_offset = 3250.f ;
    
    // global attributes:
    		
    }

Example 2. Compute seasonal climatologies

The NetCDF record averager (ncra) can be used to compute seasonal means.

For example, to compute the 1981-2000 summer (Dec-Jan-Feb) seasonal average:

  1. Download monthly rainfall files for 1980 to 2000. This can be done using a variety of methods.
  2. Run the record averager:
    ncra -F -d time,12,,12,3 \
        1980.monthly_rain.nc 1981.monthly_rain.nc \
        1982.monthly_rain.nc 1983.monthly_rain.nc \
        1984.monthly_rain.nc 1985.monthly_rain.nc \
        1986.monthly_rain.nc 1987.monthly_rain.nc \
        1988.monthly_rain.nc 1989.monthly_rain.nc \
        1990.monthly_rain.nc 1991.monthly_rain.nc \
        1992.monthly_rain.nc 1993.monthly_rain.nc \
        1994.monthly_rain.nc 1995.monthly_rain.nc \
        1996.monthly_rain.nc 1997.monthly_rain.nc \
        1998.monthly_rain.nc 1999.monthly_rain.nc \
        2000.monthly_rain.nc 1981_2000.monthly_rain.djf.nc

    This will write the output to file 1981_2000.monthly_rain.djf.nc.

    The arguments are as follows:

    -F invokes FORTRAN-style indexing (indices start at 1, not 0), and -d time,12,,12,3 selects data starting at time-slice 12 (December in the first file, 1981), continuing to the end (December in the last file, 2000) using a stride of 12 (looping over every year), and in each "sub-cycle" extracting 3 datasets (Dec, Jan, Feb).

Note on specifying input files

On UNIX systems the list of input files can be abbreviated using regular expressions. Using the Bash shell, the list can be expressed as:

198[123456789].monthly_rain.nc 199[0123456789].monthly_rain.nc 2000.monthly_rain.nc

NCO also has an option that enables it to generate the list of input files, providing the filename contains a numeric suffix immediately before the file-type suffix. To use this option, the SILO annual files must first be renamed from <year>.<variable>.nc to <variable>.<year>.nc, so the time index (year) is immediately before the ".nc" suffix. Using this feature, the climatology example becomes:

ncra -F -d time,12,,12,3 -n 19,4,1 monthly_rain.1981.nc \
    1981_2000.monthly_rain.djf.nc

where the -n option instructs ncra to load 20 files, each having a 4-digit numeric suffix, starting with file monthly_rain.1981.nc and incrementing the numeric index by 1 to access each successive file.

This feature is useful when computing long-term averages requiring many input files.

Last updated: 15 July 2020