Module pygrib :: Class open

Class open

object --+
         |
        open

open(filename)

returns GRIB file iterator object given GRIB filename. When iterated, returns instances of the gribmessage class. Behaves much like a python file object, with seek, tell, read and close methods, except that offsets are measured in grib messages instead of bytes. Additional methods include rewind (like seek(0)), message (like seek(N-1); followed by readline()), and select (filters messages based on specified conditions). The __call__ method forwards to select, and instances can be sliced with __getitem__ (returning lists of gribmessage instances). The position of the iterator is not altered by slicing with __getitem__.

Instance Methods
 
__call__(...)
same as select
 
__enter__(...)
 
__exit__(...)
 
__getitem__(x, y)
x[y]
 
__init__(filename)
x.__init__(...) initializes x; see help(type(x)) for signature
 
__iter__(x)
iter(x)
a new object with type S, a subtype of T
__new__(T, S, ...)
 
__next__(...)
 
close()
close GRIB file, deallocate C structures associated with class instance
 
message(N)
retrieve N'th message in iterator.
the next value, or raise StopIteration
next(x)
 
read(N=None)
read N messages from current position, returning grib messages instances in a list.
 
readline()
read one entire grib message from the file.
 
rewind(...)
rewind iterator (same as seek(0))
 
seek(N, from_what=0)
advance iterator N grib messages from beginning of file (if from_what=0), from current position (if from_what=1) or from the end of file (if from_what=2).
 
select(**kwargs)
return a list of gribmessage instances from iterator filtered by kwargs.
 
tell(...)
returns position of iterator (grib message number, 0 means iterator is positioned at beginning of file).

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

Instance Variables
  messagenumber
The grib message number that the iterator currently points to (the value returned by tell).
  messages
The total number of grib messages in the file.
  name
The GRIB file which the instance represents.
Properties
  closed
  has_multi_field_msgs

Inherited from object: __class__

Method Details

__init__(filename)
(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__

message(N)

 

retrieve N'th message in iterator. same as seek(N-1) followed by readline().

read(N=None)

 

read N messages from current position, returning grib messages instances in a list. If N=None, all the messages to the end of the file are read. pygrib.open(f).read() is equivalent to list(pygrib.open(f)), both return a list containing gribmessage instances for all the grib messages in the file f.

readline()

 

read one entire grib message from the file. Returns a gribmessage instance, or None if an EOF is encountered.

select(**kwargs)

 

return a list of gribmessage instances from iterator filtered by kwargs. If keyword is a container object, each grib message in the iterator is searched for membership in the container. If keyword is a callable (has a _call__ method), each grib message in the iterator is tested using the callable (which should return a boolean). If keyword is not a container object or a callable, each grib message in the iterator is tested for equality.

Example usage:

>>> import pygrib
>>> grbs = pygrib.open('sampledata/gfs.grb')
>>> selected_grbs=grbs.select(shortName='gh',typeOfLevel='isobaricInhPa',level=10)
>>> for grb in selected_grbs: grb
26:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 10 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
>>> # the __call__ method does the same thing
>>> selected_grbs=grbs(shortName='gh',typeOfLevel='isobaricInhPa',level=10)
>>> for grb in selected_grbs: grb
26:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 10 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
>>> # to select multiple specific key values, use containers (e.g. sequences)
>>> selected_grbs=grbs(shortName=['u','v'],typeOfLevel='isobaricInhPa',level=[10,50])
>>> for grb in selected_grbs: grb
193:u-component of wind:m s**-1 (instant):regular_ll:isobaricInhPa:level 50 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
194:v-component of wind:m s**-1 (instant):regular_ll:isobaricInhPa:level 50 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
199:u-component of wind:m s**-1 (instant):regular_ll:isobaricInhPa:level 10 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
200:v-component of wind:m s**-1 (instant):regular_ll:isobaricInhPa:level 10 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
>>> # to select key values based on a conditional expression, use a function
>>> selected_grbs=grbs(shortName='gh',level=lambda l: l < 500 and l >= 300)
>>> for grb in selected_grbs: grb
14:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 45000 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
15:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 40000 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
16:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 35000 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst
17:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 30000 Pa:fcst time 72 hrs:from 200412091200:lo res cntl fcst