Skip Navigation Links www.nws.noaa.gov 
NOAA logo - Click to go to the NOAA home page National Weather Service   NWS logo - Click to go to the NWS home page
Climate Prediction Center
 
 

 
About Us
   Our Mission
   Who We Are

Contact Us
   CPC Information
   CPC Web Team

 
HOME > Monitoring_and_Data > Oceanic and Atmospheric Data > Reanalysis: Atmospheric Data > g2grb
 

g2grb (GrADS 2 grib2): writing grib2 with GrADS and wgrib2

Writing out gridded data

Writing grib2 files is not fun if you have to start writing code in Fortran. g2grb is program that allows you to write grib2 files directly from GrADS. If you can plot a map with GrADS you can make a grib file (with some exceptions). Suppose you have a GFS 12 hour forecast that doesn't have the 500-1000 mb thickness. With only 4 lines in GrADS, we write a grib2 file of the thickness.
     ga-> set lat 20 50
     ga-> set lon -90 -40
     ga-> define thick=HGTprs(lev=500)-HGTprs(lev=1000)
     ga-> g2grb thick output.grb d=2010051300:THICK:500-1000 mb:12 hour fcst
Forgot to include the the wind speed at 850 mb, no problem.
     ga-> define spd=mag(UGRDprs(lev=850),VGRDprs(lev=850))
     ga-> g2grb spd +output.grb d=2010051300:WIND:850 mb:12 hour fcst

??/??/2010 First public release

Instructions: Setting the GrADS Environment

To use g2grb.gs, the GrADS environement has to be set up correctly. The time and vertical level have to be fixed (not covering a range of values). The latitude and longitude have to be varying. Basically when you display an field, you should see a map. An aninimation or a hovmuller diagram is a no go. The script g2grb can only handle regular lat-lon grids, so both Gaussian and Mercator grids are poorly supported. (g2grb will make a grib2 file but it will be a lat-lon approximation of the Gaussian/Mercator grids.) Grids such as Lambert conformal and polar stereographic are fine because GrADS will internally use a regular lat-lon grid.

Note, when opening up a global grid, GrADS usually repeats the date line. You should change the longitude range so that doesn't occur. Some programs may not be able to handle a file with the extreme left and right latitudes being repeated.


                        Example of a setup

     ga-> set time 00Z01Jan2001
     ga-> set lev 500
     ga-> set x 1 144
     ga-> set y 1 73

Instructions: writing a grib2 file

To write a grib message (record), you run the command g2grb (short for grads2grib). To write write two grib messages to a file, you run g2grb twice. The only difference is that for the second (and following writes), you have to prefix the file name with a plus sign '+'. The plus sign is way to tell g2grb that you want to append to an existing file.

   Usage: g2grb EXPRESSION {+}FILE {t=TEMPLATE} METADATA

   EXPRESSION is a GrADS expression that will make a map.  
     ga-> display EXPRESSION            should produce a map.

   {+} the plus sign is optional
       with the plus sign, the new grib2 message is appended to FILE
       without the plus sign, any old FILE is deleted before writing to FILE.
       You must use the plus sign to write many records to a file.

   FILE is the grib2 output file

   {t=TEMPLATE} optional, for advanced users
       TEMPLATE is a grib2 file
       without t=TEMPLATE option, g2grb uses the default template
         which has all the metadata set to undefined
       By specifying a template file, you can specify default values of the metadata


   METADATA is the metadata for the grib2 message.
       modeled on the wgrib2 inventory

  METADATA = {d=YYYYMMDDHH{MM{SS}}:}VAR:LEVEL:FTIME{:optional fields}

  d=YYYYMMDDHH{MM{SS}}
	The date code sets the reference time which is usually the
                 analysis or start of the forecast.
	The date code is optional and if missing, the GrADS time is used.
	If seconds (SS) or minutes (MM) are missing, zero is used.

  VAR = upper-case standard NCEP GRIB2 names, ex UGRD, TMP, HGT
	
  LEVEL = wgrib2 level, many levels are supported, ex. 500 mb, surface

  FTIME = wgrib2 ftime, many time codes are supported, ex. anl, 4 hour fcst

  optional fields:
                  scaling=DEC_SCALE,BIN_SCALE
                  packing=s/c1/c2/c3/j   (type of compression)
                  VAR=integer       VAR = string supported by -set VAR int
                  to see supported values of VAR, type
                     wgrib2 junk -set help 1

  The number of optional fields is limited to 5 but can be changed.

Undefined Values

GrADS uses a special number to signify an undefined value. With GrADS 2.0+, g2grb queries GrADS for the special value. For older versions of GrADS, g2grb has to be modified for the value as defined by the control file. Change the line following "control file specific value". Or you can change data/control file to use 9.999e20 which is the wgrib/wgrib2 undefined value.

Installation

  1. install wgrib2 v0.1.8.4+ on your path or change the location in the script g2grb.gs to point to a new wgrib2
  2. save g2grb.gs (<-- right click to download) in your to grads script directory, $GASCRP
  3. save template.grb2 (<-- right click to download) to your grads data directory, $GASCRP
  4. or use the FTP directory

Compression

The default from g2grb is packed integer (like grib1). The files can be made smaller by setting the compression in the metadata.

              Compression available in metadata

    :packing=s               simple, packed integer
    :packing=c1              complex1 compression
    :packing=c2              complex2 compression
    :packing=c3              complex3 compression
    :packing=j               jpeg2000 compression

Grib also allows setting the precision of the data. By default, the data are stored as scaled 12 bit integers when written out in grib2 format. The only option that is currently available is :scaline=DEC:BIN where DEC and BIN are integers representing the decimal and binary scaling.

Customization

g2grb.gs has much of the metadata set to undefined (255) such as the center and subcenter. If you want the center set to a specific value, you have to change the center in the file template.grb2. You can either use "-set center NN" or "-set_byte".

   $ wgrib2 template_grb2 -set center 100 -grib template.grb2.new
   $ mv template.grb2.new template.grb2

Limitations

Grib2 files have metadata which g2grb leaves as undefined. Products that are distributed should have the appropriate metadata set. The metadata can be set with wgrib2 as well by other programs.

g2grb assumes that GrADS internal grid is a regular lat-lon grid. A grad that has a varying x/y will not be handled right. g2grb wil make a regular lat-lon grid based on the latitude and longitude of the starting and ending grid points.

All data are written out using GrADS internal grid. Writing a Lambert-conformal or polar stereographic grid is possible but is untested and undocumented.

How g2grb.gs works

     1. writes EXPRESSION (grid) as a binary file (BINDATA)
     2. writes metadata as a one-line/record text file (METADATA)
     3. makes an empty grib2 file using the GrADS internal grid
         (uses wgrib2 to make a new template file with the new grid.)
     4. uses wgrib2 to read BINDATA and METADATA and output a grib2 file.

Station data to grib2

GrADS can display station data as either point data or as a gridded field. The latter can be done using a Cressman analysis (oacres) or by settng the grid value based on the station data in the grid cell (oabin). Internally oacres and oabin can create a gridded field which can be written out using g2grb.gs. An example of converting some temperature observations to a grib2 file using g2grb.gs is given here. The data for this example is here.

Multiple Grids

GrADS can handle multiple grids and convert between the grids using the "lterp" command. G2grb.gs is not so smart and assumes that the "Expression" to write is properly discribed by the GrADS "q dim" command. You may have to use "set dfile I" to change the dimension environment to match the dimension of "Expression".

Other uses

Writing the grib2 file can be done on the GrADS script level by calling the needed utilities. The same technique can be adapted by other programs. For exaple, a model would write binary grid data and a simple text metadata. A script based on g2grb could later convert the grid and metadata into grib2.

Comments: Wesley.Ebisuzaki@noaa.gov


NOAA/ National Weather Service
National Centers for Environmental Prediction
Climate Prediction Center
5830 University Research Court
College Park, Maryland 20740
Climate Prediction Center Web Team
Page last modified: Dec 16, 2015, Dec 19, 2017, Sep 17, 2020
Disclaimer Privacy Policy