Module pygrib :: Class index

Class index

object --+
         |
        index

index(filename, *args)

returns grib index object given GRIB filename indexed by keys given in *args. The select or __call__ method can then be used to selected grib messages based on specified values of indexed keys. Unlike open.select, containers or callables cannot be used to select multiple key values. However, using index.select is much faster than open.select.

Warning: Searching for data within multi-field grib messages does not work using an index (and is not supported by the GRIB API library). NCEP often puts u and v winds together in a single multi-field grib message. You will get incorrect results if you try to use an index to find data in these messages. Use the slower, but more robust open.select in this case.

If no key are given (i.e. *args is empty), it is assumed the filename represents a previously saved index (created using the grib_index_build tool or index.write) instead of a GRIB file.

Example usage:

>>> import pygrib
>>> grbindx=pygrib.index('sampledata/gfs.grb','shortName','typeOfLevel','level')
>>> grbindx.keys
['shortName', 'level']
>>> selected_grbs=grbindx.select(shortName='gh',typeOfLevel='isobaricInhPa',level=500)
>>> for grb in selected_grbs:
>>>     grb
1:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 500 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
>>> # __call__ method does same thing as select
>>> selected_grbs=grbindx(shortName='u',typeOfLevel='isobaricInhPa',level=250)
>>> for grb in selected_grbs:
>>>     grb
1:u-component of wind:m s**-1 (instant):regular_ll:isobaricInhPa:level 250 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
>>> grbindx.write('gfs.grb.idx') # save index to a file
>>> grbindx.close()
>>> grbindx = pygrib.index('gfs.grb.idx') # re-open index (no keys specified)
>>> grbindx.keys # not set when opening a saved index file.
None
>>> for grb in selected_grbs:
>>>     grb
1:u-component of wind:m s**-1 (instant):regular_ll:isobaricInhPa:level 250 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
Instance Methods
 
__call__(...)
same as select
 
__init__(filename, *args)
x.__init__(...) initializes x; see help(type(x)) for signature
a new object with type S, a subtype of T
__new__(T, S, ...)
 
close()
deallocate C structures associated with class instance
 
select(**kwargs)
return a list of gribmessage instances from grib index object corresponding to specific values of indexed keys (given by kwargs).
 
write(filename)
save grib index to file

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables
  keys
list of strings containing keys used in the index.
  types
if keys are typed, this list contains the type declarations (l, s or d).
Properties
  name

Inherited from object: __class__

Method Details

__init__(filename, *args)
(Constructor)

 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__

__new__(T, S, ...)

 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

select(**kwargs)

 

return a list of gribmessage instances from grib index object corresponding to specific values of indexed keys (given by kwargs). Unlike open.select, containers or callables cannot be used to select multiple key values. However, using index.select is much faster than open.select.

Example usage:

>>> import pygrib
>>> grbindx=pygrib.index('sampledata/gfs.grb','shortName','typeOfLevel','level')
>>> selected_grbs=grbindx.select(shortName='gh',typeOfLevel='isobaricInhPa',level=500)
>>> for grb in selected_grbs:
>>>     grb
1:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 500 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
>>> # __call__ method does same thing as select
>>> selected_grbs=grbindx(shortName='u',typeOfLevel='isobaricInhPa',level=250)
>>> for grb in selected_grbs:
>>>     grb
1:u-component of wind:m s**-1 (instant):regular_ll:isobaricInhPa:level 250 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
>>> grbindx.close()

Instance Variable Details

keys

list of strings containing keys used in the index. Set to None when opening a previously saved grib index file.

types

if keys are typed, this list contains the type declarations (l, s or d). Type declarations are specified by appending to the key name (i.e. level:l will search for values of level that are longs). Set to None when opening a previously saved grib index file.