NCO tools consist of several powerful commands to read/modify/create netcdf files. The full documentation can be found [here]. Some simple basic command lines are shown here as an example.

To rename variable var1 as “newvar” or dimension x as “lon” :

ncrename -O -v var1,newvar
ncrename -O -d x,lon

To crop a netcdf file, i.e. to reduce the domain size (with option -F indices start from 1):

ncks -F -d time,1,10
ncks -F -d x,92,111

Similarly, if you have a well written netcdf file (with appropriate attributes) and a lon-lat grid, you may be able to use (don’t forget the dots in the numbers):

ncks -d lat,-30.0,30.0

To only keep variables var1 and var2 in a netcdf file (option -O is to overwritte):

ncks -O -v var1,var2

To remove variables var1 and var2 from a netcdf file:

ncks -O -x -v var1,var2

To merge two files of same dimension (name and size), e.g. containing the variable var(x,y) and containing the variables nav_lon(x,y), nav_lat(x,y) and Bathymetry(x,y), you can do as follows:

ncks -A

The variable var(x,y) will then be included into

To make dimension time the record dimension (UNLIMITED):

ncks -O --mk_rec_dmn time

To concatenate files with the same variables and consecutive time steps (e.g. there is one file per month [whatever the output frequency within this file] and you want a file containing the JJAS months):


To calculate time-averages, e.g. to calculate a monthly mean from a file containing daily outputs:

ncra -F -d time,1,31

This can also be used to calculate mean Jan/Feb/March/… over a multi-year file:

ncra -F -d time,1,1872,12
ncra -F -d time,2,1872,12
ncra -F -d time,3,1872,12

To average over an ensemble of files of same structure, e.g. to calculate the monthly-mean diurnal cycle from hourly outputs in daily files:

nces wrfhrly_y1999m01d??.nc

NB: nces was previously known as ncea.

To remove a degenerated dimension (e.g. z):

ncwa -F -a z,1

NB: The aforementioned arythmetic operators interpret the _FillValue attribute

Here are a few examples showing how to modify netcdf attributes (see NCO user guide for further information). To delete attribute “standard_name” for variable “var1”:

ncatted -a standard_name,var1,d,,

To modify existing attribute “long_name” of character type for variable var1:

ncatted -a long_name,var1,m,c,'temperature'

To create non-existing attribute “units” of character type for variable var1:

ncatted -a units,var1,c,c,'K'

Finally, a very powerful command is ncap2. Again, there is a large number of possibilities, see NCO user guide for further information. A few examples are given here: To calculate new variable called KE from existing uu and vv variables:

ncap2 -F -s "KE=0.5*(uu*uu+vv*vv)"

To create a land mask variable (sftlf) based on SST (tos) values :

ncap2 -F -s "sftlf=tos(1,:,:)*0.0 ; \\
sftlf = sftlf.delete_miss() ; \\
sftlf(:,:) = 100.0 ; \\
where( tos(1,:,:) > 260.0 || tos(1,:,:) < 310.0 ) sftlf=0.0" \\

To use a loop to fill existing variable X from index 1 to index 482:

ncap2 -F -s \\
"idx=1 ; while(idx<482){X(idx) = 20.0+0.75*idx; idx++;}" \\

To create variables x and y corresponding to dimensions x and y knowing that xmin=-10000.0 and ymax=20000.0 with a grid spacing of 500.0 along both x and y:

ncap2 -O -s 'x=array(-10000.0,500.0,$x)' -s 'y=array(20000.0-($y.size-1)*500.0,500.0,$y)'

To print the minimum or maximum of a variable called ‘radlw’:

ncap2 -O -C -v -s "tmp=radlw.min();print(tmp)" | cut -f 3- -d ' '
ncap2 -O -C -v -s "tmp=radlw.max();print(tmp)" | cut -f 3- -d ' '