Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents
minLevel3

grib_compare examples

    1. The default behaviour for grib_compare without any option is to perform a bit by bit comparison of the two messages. If the messages are found to be bitwise different then grib_compare switches to a "key based" mode to find out which coded keys are different. To see how grib_compare works we first set the shortName=2d (2 metre dew point temperature) in the file regular_latlon_surface.grib1

      Code Block
       
      > grib_set -s shortName=2d regular_latlon_surface.grib1 2d.grib1
      

      Then we can compare the two fields with grib_compare.

      Code Block
       
      > grib_compare regular_latlon_surface.grib1 2d.grib1
      
      -- GRIB #1 -- shortName=2t paramId=167 stepRange=0 levelType=sfc level=0 packingType=grid_simple gridType=regular_ll --
      long [indicatorOfParameter]: [167] != [168]
      

      In the output we see that the only "coded" key with different values in the two messages is indicatorOfParameter which is the relevant key for the parameter information. The comparison can be forced to be successful listing the keys with different values in the -b option.

      Code Block
       
      > grib_compare -b indicatorOfParameter regular_latlon_surface.grib1 2d.grib1
      

...

    1. We can change the order of the messages in a file using grib_copy with the -B option:

      Code Block
       
      > grib_copy -B typeOfLevel tigge_pf_ecmwf.grib2 out.grib2
      

      If we now compare the two files:

      Code Block
       
      > grib_compare -f tigge_pf_ecmwf.grib2 out.grib2
      
      -- GRIB #1 -- shortName=10u paramId=165 stepRange=96 levelType=sfc level=10 packingType=grid_simple gridType=regular_ll --
      long [discipline]: [0] != [2]
      long [totalLength]: [1555] != [990]
      long [parameterCategory]: [2] != [0]
      long [parameterNumber]: [2] != [22]
      long [scaledValueOfFirstFixedSurface]: [10] != [0]
      long [typeOfSecondFixedSurface]: [255] != [106]
      scaleFactorOfSecondFixedSurface is set to missing in 1st field is not missing in 2nd field
      scaledValueOfSecondFixedSurface is set to missing in 1st field is not missing in 2nd field
      long [numberOfValues]: [684] != [239]
      double [referenceValue]: [-1.57229328155517578125e+01] != [4.15843811035156250000e+01]
      	absolute diff. = 57.3073, relative diff. = 1.3781
      	tolerance=3.8147e-06
      long [binaryScaleFactor]: [-10] != [-15]
      long [bitsPerValue]: [16] != [24]
      long [section6Length]: [6] != [92]
      long [bitMapIndicator]: [255] != [0]
      long [section7Length]: [1373] != [722]
      Different size for "codedValues"  [684]  [239]
      ...    very long output 
      

      the comparison is failing because of the different order of the messages. We can use the -r option to compare the files assuming that the messages are not in the same order:

      Code Block
       
      > grib_compare -r tigge_pf_ecmwf.grib2 out.grib2
      

      and we have a successful comparison because for each message in the first file an identical message is found in the second file. This option should be used carefully as it is very time expensive.

 

grib_copy examples

    1. To copy only the pressure levels from a file

      Code Block
      >  grib_copy -w levtype=pl ../data/tigge_pf_ecmwf.grib2 out.grib
      

...

    1. To copy selected fields and apply sorting (sorted by level in ascending order)

      Code Block
      >  grib_copy -w typeOfLevel=heightAboveGround -B'level:i asc' tigge_af_ecmwf.grib2 out.grib
      

      Note: we need to specify the ':i' to get a numerical sort. By default values are sorted as strings so a level of 100 would come before 20!

 

grib_dump examples

    1. To dump in a WMO documentation style with hexadecimal octet values (-H).

      Code Block
      > grib_dump -OH ../data/reduced_gaussian_model_level.grib1
      

...

    1. To obtain all the key names (computed keys included) available in a grib file.

      Code Block
       
      >  grib_dump -D ../data/regular_latlon_surface.grib1
      

 

grib_filter examples

    1. The grib_filter processes sequentially all grib messages contained in the input files and applies the rules to each one of them. Input messages can be written to the output by using the "write" statement. The write statement can be parameterised so that output is sent to multiple files depending on key values used in the output file name. If we write a rules_file containing the only statement:

      Code Block
      write "../data/split/[centre]_[date]_[dataType]_[levelType].grib[editionNumber]";
      

      Applying this rules_file to the "../data/tigge_pf_ecmwf.grib2" grib file we obtain several files in the ../data/split directory containing fields split according to their key values

      Code Block
      > grib_filter rules_file ../data/tigge_pf_ecmwf.grib2
      > ls ../data/split
      ecmf_20060619_pf_sfc.grib2
      ecmf_20060630_pf_sfc.grib2
      ecmf_20070122_pf_pl.grib2
      ecmf_20070122_pf_pt.grib2
      ecmf_20070122_pf_pv.grib2
      ecmf_20070122_pf_sfc.grib2
      

...

    1. The switch statement is an enhanced version of the if statement. Its syntax is the following:

      Code Block
      switch (key1) {
          case val1:
              # block of rules;
          case val2:
              # block of rules;
          default:
              # block of rules
      }
      

      Each value of each key given as argument to the switch statement is matched against the values specified in the case statements. If there is a match, then the block or rules corresponding to the matching case statement is executed. Otherwise, the default case is executed. The default case is mandatory if the case statements do not cover all the possibilities. The "~" operator can be used to match "anything". Following is an example showing the use of the switch statement:

      Code Block
      processing paramId=[paramId] [shortName] [stepType]
      switch (shortName) {
          case tp :
              set stepType=accum;
          case 10u :
              set typeOfLevel=surface;
          default:
      }
      

 

grib_get examples

    1. grib_get fails if a key is not found.

      Code Block
       
      > grib_get -p gribname ../data/tigge_pf_ecmwf.grib2
      

...

    1. To get the step of the first GRIB message in a file:

      Code Block
       
      > grib_get -w count=1 -p step ../data/tigge_pf_ecmwf.grib2
      

 

grib_get_data examples

    1. To get a latitude, longitude, value list, skipping the missing values(=9999)

      Code Block
      > grib_get_data ../data/reduced_gaussian_model_level.grib2
      

...

    1. If you want to print the value of other keys with the data value list

      Code Block
      > grib_get_data -p centre,level,step ../data/reduced_gaussian_model_level.grib2
      

 

grib_index_build examples

    1. By default grib_index_build will index on the MARS keys.

      Code Block
       
      >  grib_index_build ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2
      

...

    1. To specify a custom list of keys to index on, use the -k option.

      Code Block
       
      >  grib_index_build -k paramId,dataDate ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2
      

 

grib_ls examples

    1. Without options a default list of keys is printed. The default list is different depending on the type of grib message.

      Code Block
       
      >  grib_ls ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2 
      

...

    1. To get a list ordered by the 'level' key (ascending order).

      Code Block
       
      >  grib_ls -B 'level:i asc' tigge_af_ecmwf.grib2
      

      Note: we need to specify the ':i' to get a numerical sort. By default values are sorted as strings so a level of 100 would come before 20!

 

grib_set examples

    1. To set productDefinitionTemplateNumber=2 only for the fields with productDefinitionTemplateNumber=11

      Code Block
      > grib_set -s productDefinitionTemplateNumber=2 -w productDefinitionTemplateNumber=11 ../data/tigge_pf_ecmwf.grib2 out.grib2
      

...

    1. It's possible to ask ecCodes to calculate the number of bits per value needed to pack a given field with a fixed number of decimal digits of precision. For example if we want to pack a temperature expressed in Kelvin with 1 digits of precision after the decimal point we can set changeDecimalPrecision=1

      Code Block
       
      > grib_set -s changeDecimalPrecision=1 ../data/regular_latlon_surface.grib2 ../data/out.grib2
      

 

grib_to_netcdf examples

    1. Produce a NetCDF file from grib edition 1

      Code Block
      > grib_to_netcdf -o output.nc input.grib1
      

...