IPSL Boot Camp Part 5: CDO and NCO: 1 Climate Data Operators
IPSL Boot Camp Part 5: CDO and NCO: 1 Climate Data Operators
IPSL Boot Camp Part 5: CDO and NCO: 1 Climate Data Operators
Contents
1.2 Limitations
• GRIB: all time steps need to have the same variables and within a time step each variable may occur only once
1
IPSL BootCamp: 2016/03/24. CDO and NCO 2
cdo showdate How to display actual dates even with relative time axis.
cdo splitseas How to split a file into four files (one for each season).
Here in addition to the operator and the inputfile, we have to specify an outputfile prefix. The final outputfile-
name will be constructed using the prefix and appending the shortname of the season (DJF, MAM...) and the
suffix of the file type (.nc)
IPSL BootCamp: 2016/03/24. CDO and NCO 4
cdo sellonlatbox How to select a region? Frequently we have to do selections, for example a specific region.
This time the operator needs arguments. Operator arguments are separated by commas without spaces.
cdo setmissval When analysing data from different sources and performing arithmetic operations on them, some-
times they need to have the same missing value attribute. For example
1.5 Options
cdo has a number of options such as
• a silent mode -s
• writing output with relative (-r) or absolute (-a) time axis
• writing output in a specific format (-fnc4, -fgrb, ...)
The options are placed before the operator, here the one for a multiyear monthly mean.
cdo -s ymonmean inputfile outputfile
IPSL BootCamp: 2016/03/24. CDO and NCO 5
2.1 Concatenators
nco has two operators to join files:
ncrcat joins record variables along the record dimension (similar to cdo cat)
ncecat joins variables along a new dimension, that will be the record dimension. If there is already a record dimension
it will be turned into a fixed length dimension. The corresponding variables (or hyperslabs) from each file need
to have the same dimensions and size.
With the -u option you can give a name to the new record dimension, that will be called “record” otherwise.
2.3 ncpdq
• can pack or unpack data
• can re-order dimension or
• can reverse dimensions (cdo invertlat and cdo invertlev are special cases)
-a option is for arranging dimensions in the order listed in the argument. -dim reverses the dimension dim. If there is
any record dimension in the file, the first dimesion in the dimension list will become the new record dimension.
ncrcat infileprefix*
ncrcat infile????.nc
ncrcat *.nc
3. Specify a (remote) directory using the -p option. All the input files are assumed to be in the specified directory.
This has no influence on where the output file is written.
3. The increment
Then one has to give the first file name and the others are created using the information from the arguments. Example:
ncrcat -n 6,4,10 model_x_version_4_year_1850.nc
will take the files model x version 4 year 1850.nc, model x version 4 year 1860.nc, model x version 4 year 1870.nc,
model x version 4 year 1880.nc, model x version 4 year 1890.nc and model x version 4 year 1900.nc as input.
ncrcat -A ...
will try to append the output to an existing output file.
Example: Union of two files (similar to cdo merge)
ncks -A file1 file2
Limitations:
• The record dimension (unlimited dimension) has to have the same name in both files
• Non-record dimensions with the same name in both files must have the same size
Select variables -v option (-C, -c) Record variables to operate on can be selected using -v followed by a list of
variables. The coordinate variables corresponding to the dimensions of the chosen variables are written to the
output as well. This feature can be turned off using the -C option. With -c on the other hand ALL coordinate
variables are written to the output. Hint: If you write your own NetCDF files make sure that the coordinate
variables and the dimensions have the same name.
Example:
Whatever the veriables in the infile were, the operator will process T, Rhum, U, V. The processed variables and
the coordinate variables corresponding to the dimensions of these variables will be written to the outfile.
-x option With the -x option all the variables are selected except those listed as arguments of -v.
The operator will now process whatever variable was in the infile except T, Rhum, U and V.
-d option: subsetting variables along dimensions - hyperslabs Syntax:
• If min and max are integers, they are interpreted as dimension indices.
• As a default nco uses c-style index conventions, that is starts counting with 0. This can be changed to
fortran-style conventions (start counting with 1) adding the -F option.
• Leaving min (max) empty, is interpreted as the first (last) index or value. (The comma is not omitted.)
Example: Specify the first 5 indices of dimension time
ncks -d time,0,4 infile outfile
ncks -F -d time,1,5 infile outfile
ncks -F -d time,,5 infile outfile
• Negative indices are counted from the last index (as in python), that is -1 corresponds to the last but first
index.
• If min and max are floats (numbers with a decimal point), they are interpreted as dimension values.
• min ≤ max, even if the values are stored in inversed order, unless you have wrapped coordinates (such as
longitude) and your operator is ncks and you want to specify for example a hyperslab from 320. to 40.
degree.
ncks -d lon,40.,320. infile outfile # will work
(selects everything inside the interval 40., 320.)
ncks -d lon,320.,40. infile outfile # will work
(selects everything outside the interval 40., 320.)
ncrcat -d lon,320.,40. infile outfile # won’t work
(wrapped coordinates are only supported with ncks)
Cross sections A cross section can be selected by specifying only a dimension name and a minimum index or value
without the trailing comma.
Example selecting the 6th ensemble member:
Stride Stride must be an integer following the third comma in the -d argument list. If stride is not specified the third
comma has to be omitted as well.
Example: Select every 12th time step starting from the 3rd one.
Multislabs It is possible to specify multiple hyperslabs in one call for the same dimension and for different dimensions.
2.7 Averagers
ncra - record averager Averages record variables along the record dimension.
Options can be used to select variables (-v, -x) and to define hyperslabs (-d).
ncea - ensemble averager Averages (pointwise) variables with the same name from different files. Coordinate
variables are not averaged.
ncwa - weighted averager Average variables in a single file over arbitrary dimensions, specified with the -a option.
Without the -a option, variables are averaged over all their dimesions resulting in a scalar value for each of them.
Coordinate variables are averaged as well.
weightvar is a variable in the file containing the weights. area average where the variable ORO > 0.5
The cosine of the variable lat in the infile is calculated and stored in a new variable named latw. All variables
in ifile plus the new variable latw are written to outfile.
• -v means that only the new variables are written to the output file.
IPSL BootCamp: 2016/03/24. CDO and NCO 10
2.9 ncatted
When performing arithmentic operations, the attributes for the resulting new variable are usually taken from the first
input variab in the first input file. Sometimes attributes like standard name or units will not correspond to the new
variable any more. Sometimes you need to edit attributes, or you might just want to add some.
ncatted -a att_name,var_name,mode,att_type,att_value
2.10 ncrename
rename attributes
ncrename -a oldname,newname infile outfile
rename variables
Compression NetCDF4 allows compression using a loss-less algorithm. With any nco operator you can specify the
output format, for example -4 for NetCDF4 and a compression level with the -L option followed by a number
between 0 (no compression) and 9 (maximum compression) - the compression level. The higher the number the
smaller the file and the longer the computation time.