- Created by Paul Dando, last modified on Nov 15, 2023
1. The file file1.grib1 contains parameters T and Z on six pressure levels.
- Use grib_copy to create two files, one containing all the pressure levels for parameter T, the other for Z. Check the content of the new files with grib_ls
To copy only those messages for parameter T from the GRIB file file1.grib1 use grib_copy with the -w option to specify a shortName=t:
% grib_copy -w shortName=t file1.grib1 t.grib1
Similarly to copy only those messages for parameter Z, use:
% grib_copy -w shortName=z file1.grib1 z.grib1
Or, more simply, and because the file contains only parameters T and Z, one can use:
% grib_copy file.grib1 “[shortName]_an_pl.grib1”
Using grib_ls of the two files confirms that the contents are correct. For example:
% grib_ls -p centre,shortName,typeOfLevel,level,dataDate t.grib1 t.grib1 centre shortName typeOfLevel level dataDate ecmf t isobaricInhPa 1000 20190201 ecmf t isobaricInhPa 850 20190201 ecmf t isobaricInhPa 700 20190201 ecmf t isobaricInhPa 500 20190201 ecmf t isobaricInhPa 400 20190201 ecmf t isobaricInhPa 300 20190201 6 of 6 grib messages in t.grib1 6 of 6 total grib messages in 1 files
- Repeat but output the messages so the levels in the new files are in increasing numerical order
To reorder the messages in order of ascending pressure level, use, e.g.:
% grib_copy -B “level:i asc” file.grib1 “[shortName]_ordered.grib1”
Note the use of “level:i” here to specify that the level should be treated as an integer !
Using grib_ls to inspect the two files confirms that the contents are correct. For example:
% grib_ls -p centre,shortName,typeOfLevel,level,dataDate t_ordered.grib1 t_ordered.grib1 centre shortName typeOfLevel level dataDate ecmf t isobaricInhPa 300 20190201 ecmf t isobaricInhPa 400 20190201 ecmf t isobaricInhPa 500 20190201 ecmf t isobaricInhPa 700 20190201 ecmf t isobaricInhPa 850 20190201 ecmf t isobaricInhPa 1000 20190201 6 of 6 grib messages in t_ordered.grib1 6 of 6 total grib messages in 1 files
2. Use grib_set to change the date and time to 12UTC on 4 February 2019 for all messages in file1.grib1
To change the date and time for the GRIB messages in file.grib1 use:
% grib_set -s dataDate=20190204,dataTime=12 file.grib1 file_fixed.grib1
Using grib_ls confirms that this has been done correctly:
% grib_ls -p centre,shortName,typeOfLevel,level,dataDate,dataTime file_fixed.grib1 file_fixed.grib1 centre shortName typeOfLevel level dataDate dataTime ecmf z isobaricInhPa 1000 20190204 12 ecmf t isobaricInhPa 1000 20190204 12 ecmf z isobaricInhPa 850 20190204 12 ecmf t isobaricInhPa 850 20190204 12 ecmf z isobaricInhPa 700 20190204 12 ecmf t isobaricInhPa 700 20190204 12 ecmf z isobaricInhPa 500 20190204 12 ecmf t isobaricInhPa 500 20190204 12 ecmf z isobaricInhPa 400 20190204 12 ecmf t isobaricInhPa 400 20190204 12 ecmf z isobaricInhPa 300 20190204 12 ecmf t isobaricInhPa 300 20190204 12 12 of 12 grib messages in file_fixed.grib1 12 of 12 total grib messages in 1 files
- Repeat but change the date and time for T at 500hPa only
To make the same change of date and time for the temperature at 500hPa only use the -w option with grib_set to make sure that the correct message is changed:
% grib_set -w shortName=t,level=500 -s dataDate=20190204,dataTime=12 file.grib1 t500_fixed.grib1
Again, grib_ls can be used to confirm the output is correct:
% grib_ls -p centre,shortName,typeOfLevel,level,dataDate,dataTime t500_fixed.grib1 t500_fixed.grib1 centre shortName typeOfLevel level dataDate dataTime ecmf z isobaricInhPa 1000 20190201 0 ecmf t isobaricInhPa 1000 20190201 0 ecmf z isobaricInhPa 850 20190201 0 ecmf t isobaricInhPa 850 20190201 0 ecmf z isobaricInhPa 700 20190201 0 ecmf t isobaricInhPa 700 20190201 0 ecmf z isobaricInhPa 500 20190201 0 ecmf t isobaricInhPa 500 20190204 1200 ecmf z isobaricInhPa 400 20190201 0 ecmf t isobaricInhPa 400 20190201 0 ecmf z isobaricInhPa 300 20190201 0 ecmf t isobaricInhPa 300 20190201 0 12 of 12 grib messages in t500_fixed.grib1 12 of 12 total grib messages in 1 files
- Repeat so that T at 500hPa only is written to the output file
If grib_set is used with the -S option, only the changed GRIB message is copied to the output file:
% grib_set -S -w shortName=t,level=500 -s dataDate=20190204,dataTime=12 file.grib1 t500_only.grib1
Again, grib_ls can be used to confirm the output is correct:
% grib_ls -p centre,shortName,typeOfLevel,level,dataDate,dataTime t500_only.grib1 t500_only.grib1 centre shortName typeOfLevel level dataDate dataTime ecmf t isobaricInhPa 500 20190204 12 1 of 1 grib messages in t500_only.grib1 1 of 1 total grib messages in 1 files
3. Use grib_to_netcdf to convert the GRIB messages in file2.grib1 to NetCDF.
- Try with both the default data type (NC_SHORT) and NC_FLOAT.
- Check the data values in each case with ncdump.
To convert the GRIB messages in file.grib1 to netCDF with data type NC_SHORT use:
% grib_to_netcdf -D NC_SHORT -o out2.nc file2.grib1 grib_to_netcdf: Version 2.10.0 grib_to_netcdf: Processing input file 'file2.grib1'. grib_to_netcdf: Found 4 GRIB fields in 1 file. grib_to_netcdf: Ignoring key(s): method, type, stream, refdate, hdate grib_to_netcdf: Creating netCDF file 'out2.nc' grib_to_netcdf: NetCDF library version: 4.4.1 of May 8 2017 15:47:29 $ grib_to_netcdf: Creating large (64 bit) file format. grib_to_netcdf: Defining variable 't2m'. grib_to_netcdf: Done.
Using ncdump to print the values for the variable t2m shows:
% ncdump -v t2m out2.nc ... data: t2m = 6307, -14374, -8825, -32766, 32767, 10489, 18439, 1565, 16883, 652, 3044, -2386, 7017, 146, 564, -1652 ; }
The data values appear as integers because of the NC_SHORT data format. These values need to be unpacked using the scale_factor and add_offset netCDF attributes.
Repeating but setting the netCDF data format to NC_FLOAT gives:
% grib_to_netcdf -D NC_FLOAT -o out2a.nc file2.grib1 grib_to_netcdf: Version 2.10.0 grib_to_netcdf: Processing input file 'file2.grib1'. grib_to_netcdf: Found 4 GRIB fields in 1 file. grib_to_netcdf: Ignoring key(s): method, type, stream, refdate, hdate grib_to_netcdf: Creating netCDF file 'out2a.nc' grib_to_netcdf: NetCDF library version: 4.4.1 of May 8 2017 15:47:29 $ grib_to_netcdf: Creating large (64 bit) file format. grib_to_netcdf: Defining variable 't2m'. grib_to_netcdf: Done. % ncdump -v out2a.nc ... data: t2m = 273.2383, 269.6726, 270.6294, 266.5017, 277.8003, 273.9594, 275.33, 272.4207, 275.0618, 272.2634, 272.6757, 271.7396, 273.3607, 272.1761, 272.2482, 271.8661 ; }
In this case, the data values do not need further processing.
- Repeat but set the Reference date to 6 February 2019 and compare with previous results
To set the reference date to 1 January 2019, use the -R option
% grib_to_netcdf -R 20190206 -o out2b.nc file2.grib1 grib_to_netcdf: Version 2.10.0 grib_to_netcdf: Processing input file 'file2.grib1'. grib_to_netcdf: Found 4 GRIB fields in 1 file. grib_to_netcdf: Ignoring key(s): method, type, stream, refdate, hdate grib_to_netcdf: Creating netCDF file 'out2b.nc' grib_to_netcdf: NetCDF library version: 4.4.1 of May 8 2017 15:47:29 $ grib_to_netcdf: Creating large (64 bit) file format. grib_to_netcdf: Defining variable 't2m'. grib_to_netcdf: Done.
Compare the time variable settings from out2.nc and out2b.nc:
% ncdump -v time out2.nc ... variables: ... int time(time) ; time:units = "hours since 1900-01-01 00:00:00.0" ; time:long_name = "time" ; time:calendar = "gregorian" ; data: time = 1043886, 1043892, 1043898, 1043904 ; ... % ncdump -v time out2b.nc ... variables: ... int time(time) ; time:units = "hours since 2019-02-06 00:00:00.0" ; time:long_name = "time" ; time:calendar = "gregorian" ; data: time = -114, -108, -102, -96 ; ...
The time units are different and the time values are negative in out2b.nc because the date of the GRIB data is 1 February 2019 (so before the new reference date).
4. Use grib_to_netcdf to convert the GRIB messages in file3.grib1 to NetCDF.
- What happens … and why ?
Converting file3.grib1 to netCDF gives the following:
% grib_to_netcdf -o out4.nc file3.grib1 grib_to_netcdf: Version 2.10.0 grib_to_netcdf: Processing input file 'file3.grib1'. grib_to_netcdf: Found 1 GRIB field in 1 file. grib_to_netcdf: Ignoring key(s): method, type, stream, refdate, hdate grib_to_netcdf: Creating netCDF file 'out4.nc' grib_to_netcdf: NetCDF library version: 4.4.1 of May 8 2017 15:47:29 $ grib_to_netcdf: Creating large (64 bit) file format. ECCODES ERROR : First GRIB is not on a regular lat/lon grid or on a regular Gaussian grid. Exiting.
The conversion fails because the GRIB data is represented on a reduced Gaussian grid (gridType=reduced_gg). Conversion to NetCDF is possible only for GRIB data with gridType=regular_ll or gridType=regular_gg.