Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Confirmed: This was the most recent edition of this training course

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
Expand
titleSolution...

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:

Code Block
   % grib_copy -w shortName=t file1.grib1 t.grib1

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

Code Block
   % 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:

Code Block
  % grib_copy file.grib1 “[shortName]_an_pl.grib1”

Using grib_ls of the two files confirms that the contents are correct.   For example:

Code Block
% 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

...

Expand
titleSolution...

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

Code Block
% grib_copy -B “level:li asc” file.grib1 “[shortName]_ordered.grib1”

Note the use of “level:l” 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:

Code Block
 % 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

Expand
titleSolution...

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

Code Block
% grib_set -s dataDate=20190204,dataTime=12 file.grib1 file_fixed.grib1

 


Using grib_ls confirms that this has been done correctly:

 


Code Block
% grib_ls -p centre,shortName,typeOfLevel,level,dataDate,dataTime file_fixed.grib1
   file file_fixed.grib1
   centre       shortName    typeOfLevel  level        dataDate     dataTime     
   ecmf         z            isobaricInhPa  1000         2019020120190204     012        
   ecmf         t            isobaricInhPa  1000         2019020120190204     012        
   ecmf         z            isobaricInhPa  850          2019020120190204     012        
   ecmf         t            isobaricInhPa  850          2019020120190204     012        
   ecmf         z            isobaricInhPa  700          2019020120190204     012        
   ecmf         t            isobaricInhPa  700          2019020120190204     012        
   ecmf         z            isobaricInhPa  500          2019020120190204     012        
   ecmf         t            isobaricInhPa  500          2019020120190204     012        
   ecmf         z            isobaricInhPa  400          2019020120190204     012        
   ecmf         t            isobaricInhPa  400          2019020120190204     012        
   ecmf         z            isobaricInhPa  300          2019020120190204     012        
   ecmf         t            isobaricInhPa  300          2019020120190204     012        
   12 of 12 grib messages in file_fixed.grib1
 
   12 of 12 total grib messages in 1 files

...

Expand
titleSolution...

If grib_set is used with the -S option, only the changed GRIB message is copied to the output file:

Code Block
 % 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:

Code Block
% 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.

...

Expand
titleSolution...

To set the reference date to 1 January 2019, use the -R option

Code Block
% 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:

Code Block
% 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 ?
Expand
titleSolution...

Converting file3.grib1 to netCDF gives the following:

Code Block
% 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.

...