Project

General

Profile

Is the setcode option able to change more than one variable (parameter)?

Added by Anonymous almost 13 years ago

I want to convert from netCDF to GRIB2.
My NetCDF file has 2 variables, temp and rh.

If I specify the setcode option, it changes both variables to the specified
setcode value.

The CDO documentation says:
"setcode... Set code number...
Sets the code number for all variables to the same given value."
but I hope there is a workaround

1) Is there a way I can keep the variables distinct?

2) If the answer to #1 is no, is there a workaround that I can do?

Details are below:

Starting with the example NetCDF file in
"The NetCDF Users Guide" (version 4.1.2, March 2011) on page 18:

$ ncdump -c example_1.nc
netcdf example_1 {
dimensions:
lat = 5 ;
lon = 10 ;
level = 4 ;
time = UNLIMITED ; // (1 currently)
variables:
float temp(time, level, lat, lon) ;
temp:long_name = "temperature" ;
temp:units = "celsius" ;
float rh(time, lat, lon) ;
rh:long_name = "relative humidity" ;
rh:valid_range = 0., 1. ;
int lat(lat) ;
lat:units = "degrees_north" ;
int lon(lon) ;
lon:units = "degrees_east" ;
int level(level) ;
level:units = "millibars" ;
short time(time) ;
time:units = "hours since 1996-1-1" ;

// global attributes:
:source = "Fictional Model Output" ;
data:

lat = 20, 30, 40, 50, 60 ;
lon = -160, -140, -118, -96, -84, -52, -45, -35, -25, -15 ;
level = 1000, 850, 700, 500 ;
time = 12 ;
}
$ cdo showname example_1.nc
temp rh
cdo showname: Processed 2 variables. ( 0.00s )

$ cdo -f grb2 -setcode,187 -settabnum,129 example_1.nc 187-129.grb2
cdo setcode: Started child process "settabnum,129 example_1.nc (pipe1.1)".
cdo(2) settabnum: Processed 250 values from 2 variables over 1 timestep. ( 0.01s )
cdo setcode: Processed 250 values from 2 variables over 1 timestep. ( 0.01s )

$ cdo sinfo 187-129.grb2 File format: GRIB2
-1 : Institut Source Param Time Typ Grid Size Num Levels Num
1 : ECMWF unknown 187.129 var P0 50 1 4 1
2 : ECMWF unknown 187.129 var P16 50 1 1 2
Horizontal grids :
1 : lonlat > size : dim = 50 nlon = 10 nlat = 5
lon : first = 200 last = 345 inc = 16.1111111 degrees_east
lat : first = 20 last = 60 inc = 10 degrees_north
Vertical grids :
1 : pressure Pa : 100000 85000 70000 50000
2 : surface : 0
Time axis : 1 step
RefTime = 1996-01-01 00:00:00 Units = hours Calendar = PROLEPTIC
YYYY-MM-DD hh:mm:ss YYYY-MM-DD hh:mm:ss YYYY-MM-DD hh:mm:ss YYYY-MM-DD hh:mm:ss
1996-01-01 12:00:00
cdo sinfo: Processed 2 variables over 1 timestep. ( 0.08s )

$ cdo -V
Climate Data Operators version 1.5.0 (http://code.zmaw.de/projects/cdo)
Compiler: gcc -std=gnu99 -g -O2
version: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
with: PTHREADS
Compiled: by YYYYY on YYYYYYYYYY (x86_64-unknown-linux-gnu) May 16 2011 13:42:31
CDI library version : 1.5.0 of May 16 2011 13:42:26
CGRIBEX library version : 1.5.0 of Feb 15 2011 10:23:39
GRIB_API library version : 1.9.8
netCDF library version : 4.1.2 of May 3 2011 18:56:46 $
SERVICE library version : 1.3.0 of May 16 2011 13:42:14
EXTRA library version : 1.3.0 of May 16 2011 13:42:11
IEG library version : 1.3.0 of May 16 2011 13:42:14
FILE library version : 1.7.1 of May 16 2011 13:42:11


Replies (3)

RE: Is the setcode option able to change more than one variable (parameter)? - Added by Anonymous almost 13 years ago

I think I answered my own question.

First I need to split the file, then merge the split files together.

E.g., to convert from NetCDF-classic to GRIB2,

$ cdo splitname example_1.nc split_
cdo splitname: Processed 250 values from 2 variables over 1 timestep. ( 0.00s )

This creates 2 files, split_temp.nc and split_rh.nc

Convert to grib and map the variable to something "grib-like". Note that the code and table number are random for this example. For a real life scenario, find the appropriate values in an official table.

$ cdo -f grb2 -setcode,187 -settabnum,129 split_temp.nc split_temp.grb2
$ cdo -f grb2 -setcode,188 -settabnum,130 split_rh.nc split_rh.grb2

Now merge them
$ cdo merge split_temp.grb2 split_rh.grb2 example_1.grb2

RE: Is the setcode option able to change more than one variable (parameter)? - Added by Uwe Schulzweida almost 13 years ago

Since CDO version 1.5.0 we introduce a new concept to deal with code and table numbers. We combine these numbers and call it parameter identifier. A GRIB1 parameter identifier has the two components code number and table number:

GRIB1 parameter identifier: < code number>.<table number>  e.g. temperature 130.128
A parameter in GRIB2 has three components:
GRIB2 parameter identifier: <parameter number>.<parameter category>.<discipline>  e.g temperature 0.0.0
A GRIB2 parameter identifier behaves like a GRIB1 parameter if the component <discipline> is missing.

The function setparam replaces the obsolete functions setcode and settabnum. Use:

cdo setparam,188.130 ifile ofile
instead of
cdo setcode,188 -settabnum,130 ifile ofile

netCDF variables normally don't have a parameter identifier. In this case they get consecutive negative numbers starting with -1. You can change a list of parameter identifiers with the function chparam:

cdo -f grb2 chparam,-1,187.129,-2,188.130  ifile  ofile

RE: Is the setcode option able to change more than one variable (parameter)? - Added by Anonymous almost 13 years ago

Adding on to Uwe's comment, when converting from NetCDF to GRIB2, the default category.discipline is 0.255 (category and discipline are GRIB2 concepts). Let's say that you already have the "code" attribute for a variable defined to be a positive integer in your NetCDF file.
Your "ncdump -c" might show something like this:
variables:
double s4(lat, lon);
s4:units = "example" ;
s4:missing_value = NaN ;
s4:_FillValue = NaN ;
s4:code = 101 ;

Therefore, in order for the chparam to work correctly, use the fully-qualified descriptor, e.g.,

cdo -v -f grb2 chparam,101.0.255,101.129.254,102.0.255,102.129.254,103.0.255,103.129.254,104.0.255,104.129.254,105.0.255,105.129.254,106.0.255,106.129.254 file1 file2

Here, I changed my codes to all belong in category 129, discipline 254 of the GRIB2 file.

    (1-3/3)