Module ncepgrib2 :: Class Grib2Encode

# Class Grib2Encode

source code

Class for encoding data into a GRIB2 message.

- Creating a class instance (__init__) initializes the message and adds sections 0 and 1 (the indicator and identification sections),
- method addgrid adds a grid definition (section 3) to the messsage.
- method addfield adds sections 4-7 to the message (the product definition, data representation, bitmap and data sections).
- method end adds the end section (section 8) and terminates the message.

A GRIB Edition 2 message is a machine independent format for storing one or more gridded data fields. Each GRIB2 message consists of the following sections:

- SECTION 0: Indicator Section - only one per message
- SECTION 1: Identification Section - only one per message
- SECTION 2: (Local Use Section) - optional
- SECTION 3: Grid Definition Section
- SECTION 4: Product Definition Section
- SECTION 5: Data Representation Section
- SECTION 6: Bit-map Section
- SECTION 7: Data Section
- SECTION 8: End Section

Sequences of GRIB sections 2 to 7, 3 to 7, or sections 4 to 7 may be repeated within a single GRIB message. All sections within such repeated sequences must be present and shall appear in the numerical order noted above. Unrepeated sections remain in effect until redefined.

Note: Writing section 2 (the 'local use section') is not yet supported.

Instance Methods

__init__(self, discipline, idsect) create a Grib2Enecode class instance given the GRIB2 discipline parameter and the identification section (sections 0 and 1). | source code

addfield(self, pdtnum, pdtmpl, drtnum, drtmpl, field, coordlist=None) Add a product definition section, data representation section, bitmap section and data section to the GRIB2 message (sections 4-7). | source code

addgrid(self, gdsinfo, gdtmpl, deflist=None) Add a grid definition section (section 3) to the GRIB2 message. | source code

end(self) Add an end section (section 8) to the GRIB2 message. | source code

Instance Variables
| msg A binary string containing the GRIB2 message.

Method Details

### __init__(self, discipline, idsect)
(Constructor)

source code

create a Grib2Enecode class instance given the GRIB2 discipline parameter and the identification section (sections 0 and 1).

The GRIB2 message is stored as a binary string in instance variable msg.

addgrid, addfield and end class methods must be called to complete the GRIB2 message.

Parameters:
  - `discipline` - Discipline or GRIB Master Table Number (Code Table 0.0). (0 for meteorlogical, 1 for hydrological, 2 for land surface, 3 for space, 10 for oceanographic products).
  - `idsect` - Sequence containing identification section (section 1).
    - idsect[0]=Id of orginating centre (Common Code Table C-1)
    - idsect[1]=Id of orginating sub-centre (local table)
    - idsect[2]=GRIB Master Tables Version Number (Code Table 1.0)
    - idsect[3]=GRIB Local Tables Version Number (Code Table 1.1)
    - idsect[4]=Significance of Reference Time (Code Table 1.2)
    - idsect[5]=Reference Time - Year (4 digits)
    - idsect[6]=Reference Time - Month
    - idsect[7]=Reference Time - Day
    - idsect[8]=Reference Time - Hour
    - idsect[9]=Reference Time - Minute
    - idsect[10]=Reference Time - Second
    - idsect[11]=Production status of data (Code Table 1.3)
    - idsect[12]=Type of processed data (Code Table 1.4)

### addfield(self, pdtnum, pdtmpl, drtnum, drtmpl, field, coordlist=None)

source code

Add a product definition section, data representation section, bitmap section and data section to the GRIB2 message (sections 4-7). Must be called after grid definition section is created with addgrid.

Parameters:
  - `pdtnum` - Product Definition Template Number (see Code Table 4.0)
  - `pdtmpl` - Sequence with the data values for the specified Product Definition Template (N=pdtnum). Each element of this integer array contains an entry (in the order specified) of Product Definition Template 4.N
  - `drtnum` - Data Representation Template Number (see Code Table 5.0)
  - `drtmpl` - Sequence with the data values for the specified Data Representation Template (N=drtnum). Each element of this integer array contains an entry (in the order specified) of Data Representation Template 5.N Note that some values in this template (eg. reference values, number of bits, etc...) may be changed by the data packing algorithms. Use this to specify scaling factors and order of spatial differencing, if desired.
  - `field` - numpy array of data points to pack. If field is a masked array, then a bitmap is created from the mask.
  - `coordlist` - Sequence containing floating point values intended to document the vertical discretization with model data on hybrid coordinate vertical levels. Default None.

### addgrid(self, gdsinfo, gdtmpl, deflist=None)

source code

Add a grid definition section (section 3) to the GRIB2 message.

Parameters:
  - `gdsinfo` - Sequence containing information needed for the grid definition section.
    - gdsinfo[0] = Source of grid definition (see Code Table 3.0)
    - gdsinfo[1] = Number of grid points in the defined grid.
    - gdsinfo[2] = Number of octets needed for each additional grid points defn. Used to define number of points in each row for non-reg grids (=0 for regular grid).
    - gdsinfo[3] = Interp. of list for optional points defn (Code Table 3.11)
    - gdsinfo[4] = Grid Definition Template Number (Code Table 3.1)
  - `gdtmpl` - Contains the data values for the specified Grid Definition Template ( NN=gdsinfo[4] ). Each element of this integer array contains an entry (in the order specified) of Grid Definition Template 3.NN
  - `deflist` - (Used if gdsinfo[2] != 0) Sequence containing the number of grid points contained in each row (or column) of a non-regular grid.

### end(self)

source code

Add an end section (section 8) to the GRIB2 message. A GRIB2 message is not complete without an end section. Once an end section is added, the GRIB2 message can be output to a file.

Instance Variable Details

### msg

A binary string containing the GRIB2 message. After the message has been terminated by calling the end method, this string can be written to a file.