Page tree
Skip to end of metadata
Go to start of metadata

Question 1 - using grib_copy

The file file1.grib contains parameters T and Z on five 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
 Solution...

To copy only those messages for parameter T from the GRIB file tz_an_pl.grib1 use grib_copy with the -w option to specify a shortName=t:

 
% grib_copy -w shortName=t tz_an_pl.grib1 t_an_pl.grib1

Similarly to copy only those messages for parameter Z, use:

	% grib_copy -w shortName=z tz_an_pl.grib1 z_an_pl.grib1

Or, more simply, and because the file contains only parameters T and Z, one can use:

	% grib_copy tz_an_pl.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
  	file1.grib1
  	centre       shortName    typeOfLevel  level        dataDate     
  	ecmf         t            isobaricInhPa  1000         20180223    
  	ecmf         t            isobaricInhPa  850          20180223    
  	ecmf         t            isobaricInhPa  700          20180223    
  	ecmf         t            isobaricInhPa  500          20180223    
  	ecmf         t            isobaricInhPa  400          20180223    
  	ecmf         t            isobaricInhPa  300          20180223
  	6 of 6 grib messages in file1.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
 Solution...

To reorder the messages in order of ascending pressure level, use, e.g.:


% grib_copy -B “level:i asc” tz_an_pl.grib1 “[shortName]_ordered.grib1”

Note the use of “level:i” here to specify that the level should be treated as an integer !  Otherwise the level value is treated as a string and you don't get the result you expect.

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          20180223    
	ecmf         t            isobaricInhPa  400          20180223    
	ecmf         t            isobaricInhPa  500          20180223    
	ecmf         t            isobaricInhPa  700          20180223    
	ecmf         t            isobaricInhPa  850          20180223    
	ecmf         t            isobaricInhPa  1000         20180223
	6 of 6 grib messages in t_ordered.grib1
	
	6 of 6 total grib messages in 1 files 

 

Question 2 - using grib_set

Use grib_set to change the date and time to 12UTC on 26 February 2018 for all messages in file1.grib

 Solution...

To change the date and time for the GRIB messages in file.grib1 use:


% grib_set -s dataDate=20180226,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 20180226 1200 ecmf t isobaricInhPa 1000 20180226 1200 ecmf z isobaricInhPa 850 20180226 1200 ecmf t isobaricInhPa 850 20180226 1200 ecmf z isobaricInhPa 700 20180226 1200 ecmf t isobaricInhPa 700 20180226 1200 ecmf z isobaricInhPa 500 20180226 1200 ecmf t isobaricInhPa 500 20180226 1200 ecmf z isobaricInhPa 400 20180226 1200 ecmf t isobaricInhPa 400 20180226 1200 ecmf z isobaricInhPa 300 20180226 1200 ecmf t isobaricInhPa 300 20180226 1200 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
 Solution...

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=20180226,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 20180223 0 ecmf t isobaricInhPa 1000 20180223 0 ecmf z isobaricInhPa 850 20180223 0 ecmf t isobaricInhPa 850 20180223 0 ecmf z isobaricInhPa 700 20180223 0 ecmf t isobaricInhPa 700 20180223 0 ecmf z isobaricInhPa 500 20180223 0 ecmf t isobaricInhPa 500 20180226 1200 ecmf z isobaricInhPa 400 20180223 0 ecmf t isobaricInhPa 400 20180223 0 ecmf z isobaricInhPa 300 20180223 0 ecmf t isobaricInhPa 300 20180223 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
 Solution...

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=20180226,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 20180226 1200 1 of 1 grib messages in t500_only.grib1 1 of 1 total grib messages in 1 file
 

 

Question 3 - using grib_to_netcdf

Use grib_to_netcdf to convert the GRIB messages in file2.grib to NetCDF.

  • Try with both the default data type (NC_SHORT) and NC_FLOAT.
  • Check the data values in each case with ncdump.
 Solution...

To convert the GRIB messages in file.grib1 to netCDF with data type NC_SHORT use:


% grib_to_netcdf -o out1.nc file2.grib1
grib_to_netcdf: Version 2.6.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 'out1.nc'
grib_to_netcdf: NetCDF library version: 4.4.1 of Aug  3 2016 11:10:49 $
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 out1.nc ... data: t2m =
       14339, 5131,
       -709, -3269,
       32767, 17772,
       2818, -4499,
       10921, -6495,
       -8534, -24034,
       -11309, -20019,
       -27543, -32766 ;
     } 

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 out2.nc file2.grib1
  grib_to_netcdf: Version 2.6.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 Aug  3 2016 11:10:49 $
grib_to_netcdf: Creating large (64 bit) file format.
grib_to_netcdf: Defining variable 't2m'.
grib_to_netcdf: Done.
% ncdump -v out2.nc ... data: t2m =
        275.9413, 274.326,
        273.3016, 272.8525,
        279.1742, 276.5436,
        273.9203, 272.6367,
        275.3418, 272.2865,
        271.9288, 269.2097,
        271.442, 269.9141,
        268.5941, 267.6779 ;
   	}

In this case, the data values do not need further processing.

  • Repeat but set the Reference date to 22 February 2018 and compare with previous results

 Solution...

To set the reference date to 22 February 2018, use the -R option:


	% grib_to_netcdf -R 20180222 -o out2b.nc file2.grib1	
grib_to_netcdf: Version 2.6.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 Aug  3 2016 11:10:49 $
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.  The difference now is in the time variable. Without setting the reference time we have:


	% ncdump -v time out2b.nc
	netcdf out2b {
    ...
    	    int time(time) ;
    	            time:units = "hours since 2018-02-18 00:00:0.0" ;
    	            time:long_name = "time" ;
    	            time:calendar = "gregorian" ;
    ...
	data:
	 time = 126, 132, 138, 144 ;
	}

Without setting the reference time we have:


% ncdump -v time out2.nc  
	netcdf out2 {
    ...
    	    int time(time) ;
    	            time:units = "hours since 1900-01-01 00:00:0.0" ;
    	            time:long_name = "time" ;
    	            time:calendar = "gregorian" ;    ... 
	data:
 	time = 1035654, 1035660, 1035666, 1035672 ;
	}

The time units are different and the time values are smaller in out2b.nc because the date of the GRIB data is closer to the new reference date of 22 February 2018.

Question 4 - using grib_to_netcdf

Use grib_to_netcdf to convert the GRIB messages in file3.grib to NetCDF.

  • What happens … and why ?

 Solution...

Converting file3.grib to netCDF gives the following:

	% grib_to_netcdf: Version 2.6.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 'out3.nc'
grib_to_netcdf: NetCDF library version: 4.4.1 of Aug  3 2016 11:10:49 $
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.


  • No labels