<?xml version="1.0" encoding="ascii"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>pygrib</title> <link rel="stylesheet" href="epydoc.css" type="text/css" /> <script type="text/javascript" src="epydoc.js"></script> </head> <body bgcolor="white" text="black" link="blue" vlink="#204080" alink="#204080"> <!-- ==================== NAVIGATION BAR ==================== --> <table class="navbar" border="0" width="100%" cellpadding="0" bgcolor="#a0c0ff" cellspacing="0"> <tr valign="middle"> <!-- Home link --> <th bgcolor="#70b0f0" class="navbar-select" > Home </th> <!-- Tree link --> <th> <a href="module-tree.html">Trees</a> </th> <!-- Index link --> <th> <a href="identifier-index.html">Indices</a> </th> <!-- Help link --> <th> <a href="help.html">Help</a> </th> <th class="navbar" width="100%"></th> </tr> </table> <table width="100%" cellpadding="0" cellspacing="0"> <tr valign="top"> <td width="100%"> <span class="breadcrumbs"> Module pygrib </span> </td> <td> <table cellpadding="0" cellspacing="0"> <!-- hide/show private --> </table> </td> </tr> </table> <!-- ==================== MODULE DESCRIPTION ==================== --> <h1 class="epydoc">Module pygrib</h1><p class="nomargin-top"></p> <h1 class="heading">Introduction</h1> <p>Python module for reading and writing GRIB (editions 1 and 2) files. GRIB is the World Meterological Organization <a href="http://www.wmo.ch/pages/prog/www/WMOCodes/GRIB.html" target="_top">standard</a> for distributing gridded data. The module is a python interface to the <a href="https://software.ecmwf.int/wiki/display/GRIB/Home" target="_top">GRIB API</a> C library from the European Centre for Medium-Range Weather Forecasts (<a href="http://www.ecmwf.int" target="_top">ECMWF</a>).</p> <h1 class="heading">Required</h1> <ul> <li> <a href="http://python.org" target="_top">Python</a> 2.4 or higher. </li> <li> <a href="http://sourceforge.net/project/showfiles.php?group_id=1369" target="_top">numpy</a> N-dimensional array object for python. Version 1.2.1 or higher (version 1.5.1 required for Python 3). </li> <li> <a href="http://code.google.com/p/pyproj/" target="_top">pyproj</a> Python interface to <a href="http://trac.osgeo.org/proj" target="_top">PROJ.4</a> library for cartographic transformations <b>or</b> <a href="http://matplotlib.sf.net" target="_top">matplotlib</a> and the <a href="http://matplotlib.sf.net/basemap/doc/html" target="_top">basemap</a> toolkit. Pyproj 1.8.9 is required for Python 3. </li> <li> <a href="https://software.ecmwf.int/wiki/display/GRIB/Home" target="_top">GRIB API</a> C library for encoding and decoding GRIB messages (edition 1 and edition 2). Version 1.8.0 or higher required. To be fully functional, the GRIB API library requires <a href="http://www.ece.uvic.ca/~mdadams/jasper" target="_top">Jasper</a> or <a href="http://www.openjpeg.org" target="_top">OpenJPEG</a> for JPEG200 encoding, and <a href="http://www.libpng.org/pub/png/libpng.html" target="_top">PNG</a> for PNG encoding. </li> </ul> <p>These dependencies are available via the package management system of most Linux distributions, and on MacOS X using <a href="http://macports.org/" target="_top">macports</a>. If you build grib_api yourself as a static library on a 64-bit system you may need to set <code>CFLAGS</code> to <code>'-O2 -fPIC'</code> before running the <code>configure</code> script. To use pygrib on Windows, you must use the <a href="http://cygwin.com" target="_top">Cygwin</a> environment, since the grib_api library requires a posix environment. Cygwin installation instructions are available <a href="http://code.google.com/p/pygrib/wiki/CygwinWindowsInstall" target="_top">here</a>.</p> <h1 class="heading">Installation</h1> <ul> <li> <a href="http://code.google.com/p/pygrib/downloads/list" target="_top">Download</a> the source code. </li> <li> pygrib installation options can either be set with environment variables, or specified in a text file (setup.cfg). To use environment variables, set <code>$GRIBAPI_DIR</code>, <code>$JASPER_DIR</code>, <code>$OPENJPEG_DIR</code>, <code>$PNG_DIR</code> and <code>$ZLIB_DIR</code> so that the include files and libraries for GRIB API, Jasper, OpenJPEG, PNG and zlib will be found. For example, the include files for jasper should be found in <code>$JASPER_DIR/include</code> or <code>$JASPER_DIR/include/jasper</code>, and the jasper library should be found in <code>$JASPER_DIR/lib</code> or <code>$JASPER_DIR/lib64</code>. If any of those environment variables are not set, then it is assumed that GRIB API was not built with support for that library. If the libraries and include files are installed in separate locations, the environment variables <code>$GRIBAPI_INCDIR</code> and <code>$GRIBAPI_LIBDIR</code> can be used to define the locations separately (same goes for <code>JASPER</code>, <code>OPENJPEG</code>, <code>PNG</code> and <code>ZLIB</code>). To use setup.cfg, copy setup.cfg.template to setup.cfg, open setup.cfg in a text editor and follow the instructions in the comments for editing. </li> <li> Run 'python setup.py build' and then 'python setup.py install', as root if necessary. Note that if you are using environment variables to specify the build options, you cannot build and install in one step with 'sudo python setup.py install', since sudo does not pass environment variables. Instead, run 'python setup.py build' first as a regular user, then run 'sudo python setup.py install' if the install directory requires admin or root privileges. </li> <li> Run 'python test.py' to test your installation. </li> <li> Look at examples in <code>test</code> directory (most require <a href="http://matplotlib.sf.net" target="_top">matplotlib</a> and <a href="http://matplotlib.sourceforge.net/basemap/doc/html/" target="_top">basemap</a>). </li> <li> If you're on MacOS X, see <a href="http://pygrib.googlecode.com/svn/trunk/README.macosx" target="_top">README.macosx</a> for special instruction on how to install pygrib and all it's dependencies using <a href="http://macports.org" target="_top">macports</a>. </li> </ul> <h1 class="heading">Example usage</h1> <ul> <li> from the python interpreter prompt, import the package: <pre class="literalblock"> >>> import pygrib </pre> </li> <li> open a GRIB file, create a grib message iterator: <pre class="literalblock"> >>> grbs = pygrib.open('sampledata/flux.grb') </pre> </li> <li> pygrib open instances behave like regular python file objects, with <code>seek</code>, <code>tell</code>, <code>read</code>, <code>readline</code> and <code>close</code> methods, except that offsets are measured in grib messages instead of bytes: <pre class="literalblock"> >>> grbs.seek(2) >>> grbs.tell() 2 >>> grb = grbs.read(1)[0] # read returns a list with the next N (N=1 in this case) messages. >>> grb # printing a grib message object displays summary info 3:Maximum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200 >>> grbs.tell() 3 </pre> </li> <li> print an inventory of the file: <pre class="literalblock"> >>> grbs.seek(0) >>> for grb in grbs: >>> grb 1:Precipitation rate:kg m**-2 s**-1 (avg):regular_gg:surface:level 0:fcst time 108-120 hrs (avg):from 200402291200 2:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 120 hrs:from 200402291200 3:Maximum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200 4:Minimum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200 </pre> </li> <li> find the first grib message with a matching name: <pre class="literalblock"> >>> grb = grbs.select(name='Maximum temperature')[0] </pre> </li> <li> extract the data values using the 'values' key (grb.keys() will return a list of the available keys): <pre class="literalblock"> # The data is returned as a numpy array, or if missing values or a bitmap # are present, a numpy masked array. Reduced lat/lon or gaussian grid # data is automatically expanded to a regular grid. Details of the internal # representation of the grib data (such as the scanning mode) are handled # automatically. >>> maxt = grb.values # same as grb['values'] >>> maxt.shape, maxt.min(), maxt.max() (94, 192) 223.7 319.9 </pre> </li> <li> get the latitudes and longitudes of the grid: <pre class="literalblock"> >>> lats, lons = grb.latlons() >>> lats.shape, lats.min(), lats.max(), lons.shape, lons.min(), lons.max() (94, 192) -88.5419501373 88.5419501373 0.0 358.125 </pre> </li> <li> get the second grib message: <pre class="literalblock"> >>> grb = grbs.message(2) # same as grbs.seek(1); grb=grbs.readline() >>> grb 2:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 120 hrs:from 200402291200 </pre> </li> <li> extract data and get lat/lon values for a subset over North America: <pre class="literalblock"> >>> data, lats, lons = grb.data(lat1=20,lat2=70,lon1=220,lon2=320) >>> data.shape, lats.min(), lats.max(), lons.min(), lons.max() (26, 53) 21.904439458 69.5216630593 221.25 318.75 </pre> </li> <li> modify the values associated with existing keys (either via attribute or dictionary access): <pre class="literalblock"> >>> grb['forecastTime'] = 240 >>> grb.dataDate = 20100101 </pre> </li> <li> get the binary string associated with the coded message: <pre class="literalblock"> >>> msg = grb.tostring() >>> grbs.close() # close the grib file. </pre> </li> <li> write the modified message to a new GRIB file: <pre class="literalblock"> >>> grbout = open('test.grb','wb') >>> grbout.write(msg) >>> grbout.close() >>> pygrib.open('test.grb').readline() 1:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 240 hrs:from 201001011200 </pre> </li> </ul> <h1 class="heading">Documentation</h1> <ul> <li> see below for the full python API documentation. </li> </ul> <h1 class="heading">Changelog</h1> <ul> <li> see <a href="http://pygrib.googlecode.com/svn/trunk/Changelog" target="_top">Changelog</a> file. </li> </ul> <hr /> <div class="fields"> <p><strong>Author:</strong> Jeffrey Whitaker. </p> <p><strong>Contact:</strong> <a href="mailto:jeffrey.s.whitaker@noaa.gov" target="_top">Jeff Whitaker</a> </p> <p><strong>Version:</strong> 1.9.7 </p> <p><strong>Copyright:</strong> copyright 2010 by Jeffrey Whitaker. </p> <p><strong>License:</strong> Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. </p> </div><!-- ==================== CLASSES ==================== --> <a name="section-Classes"></a> <table class="summary" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td align="left" colspan="2" class="table-header"> <span class="table-header">Classes</span></td> </tr> <tr> <td class="summary" colspan="2"> <a href="pygrib.gribmessage-class.html" class="summary-name">gribmessage</a><br /> Grib message object. </td> </tr> <tr> <td class="summary" colspan="2"> <a href="pygrib.index-class.html" class="summary-name">index</a><br /> index(filename, *args) </td> </tr> <tr> <td class="summary" colspan="2"> <a href="pygrib.open-class.html" class="summary-name">open</a><br /> open(filename) </td> </tr> </table> <!-- ==================== FUNCTIONS ==================== --> <a name="section-Functions"></a> <table class="summary" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td align="left" colspan="2" class="table-header"> <span class="table-header">Functions</span></td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a name="datetime_to_julian"></a><span class="summary-sig-name">datetime_to_julian</span>(<span class="summary-sig-arg">date</span>)</span><br /> convert python datetime instance to Julian day number.</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a name="fromstring"></a><span class="summary-sig-name">fromstring</span>(<span class="summary-sig-arg">string</span>)</span><br /> Create a gribmessage instance from a python bytes object representing a binary grib message (the reverse of <a href="pygrib.gribmessage-class.html#tostring" class="link">gribmessage.tostring</a>).</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="pygrib-module.html#gaulats" class="summary-sig-name">gaulats</a>(<span class="summary-sig-arg">nlats</span>)</span><br /> Returns nlats gaussian latitudes, in degrees, oriented from north to south.</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="pygrib-module.html#julian_to_datetime" class="summary-sig-name">julian_to_datetime</a>(<span class="summary-sig-arg">julday</span>)</span><br /> convert Julian day number to python datetime instance.</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a name="multi_support_off"></a><span class="summary-sig-name">multi_support_off</span>(<span class="summary-sig-arg">...</span>)</span><br /> turn off support for multi-field grib messages</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a name="multi_support_on"></a><span class="summary-sig-name">multi_support_on</span>(<span class="summary-sig-arg">...</span>)</span><br /> turn on support for multi-field grib messages (default)</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="pygrib-module.html#reload" class="summary-sig-name">reload</a>(<span class="summary-sig-arg">grb</span>)</span><br /> Recreate gribmessage object, updating all the keys to be consistent with each other.</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="pygrib-module.html#setdates" class="summary-sig-name">setdates</a>(<span class="summary-sig-arg">grb</span>)</span><br /> set fcstimeunits, analDate and validDate attributes using julianDay, forecastTime and indicatorOfUnitOfTimeRange.</td> <td align="right" valign="top"> </td> </tr> </table> </td> </tr> </table> <!-- ==================== VARIABLES ==================== --> <a name="section-Variables"></a> <table class="summary" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td align="left" colspan="2" class="table-header"> <span class="table-header">Variables</span></td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="__package__"></a><span class="summary-name">__package__</span> = <code title="None">None</code> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="__version__"></a><span class="summary-name">__version__</span> = <code title="'1.9.7'"><code class="variable-quote">'</code><code class="variable-string">1.9.7</code><code class="variable-quote">'</code></code> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="default_encoding"></a><span class="summary-name">default_encoding</span> = <code title="'ascii'"><code class="variable-quote">'</code><code class="variable-string">ascii</code><code class="variable-quote">'</code></code> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="grib_api_version"></a><span class="summary-name">grib_api_version</span> = <code title="'1.11.0'"><code class="variable-quote">'</code><code class="variable-string">1.11.0</code><code class="variable-quote">'</code></code> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="missingvalue_float"></a><span class="summary-name">missingvalue_float</span> = <code title="-1e+100">-1e+100</code> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="missingvalue_int"></a><span class="summary-name">missingvalue_int</span> = <code title="4294967295">4294967295</code> </td> </tr> </table> <!-- ==================== FUNCTION DETAILS ==================== --> <a name="section-FunctionDetails"></a> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td align="left" colspan="2" class="table-header"> <span class="table-header">Function Details</span></td> </tr> </table> <a name="gaulats"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">gaulats</span>(<span class="sig-arg">nlats</span>)</span> </h3> </td><td align="right" valign="top" > </td> </tr></table> <p>Returns nlats gaussian latitudes, in degrees, oriented from north to south. nlats must be even.</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="julian_to_datetime"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">julian_to_datetime</span>(<span class="sig-arg">julday</span>)</span> </h3> </td><td align="right" valign="top" > </td> </tr></table> <p>convert Julian day number to python datetime instance.</p> <p>Used to create validDate and analDate attributes from julianDay and forecastTime keys.</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="reload"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">reload</span>(<span class="sig-arg">grb</span>)</span> </h3> </td><td align="right" valign="top" > </td> </tr></table> <p>Recreate gribmessage object, updating all the keys to be consistent with each other. For example, if the forecastTime key is changed, recreating the gribmessage object with this function will cause the analDate and verifDate keys to be updated accordingly.</p> <p>Equivalent to fromstring(grb.tostring())</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="setdates"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">setdates</span>(<span class="sig-arg">grb</span>)</span> </h3> </td><td align="right" valign="top" > </td> </tr></table> <p>set fcstimeunits, analDate and validDate attributes using julianDay, forecastTime and indicatorOfUnitOfTimeRange. Called automatically when gribmessage instance created, but can be called manually to update keys if one of them is modified after instance creation.</p> <dl class="fields"> </dl> </td></tr></table> </div> <br /> <!-- ==================== NAVIGATION BAR ==================== --> <table class="navbar" border="0" width="100%" cellpadding="0" bgcolor="#a0c0ff" cellspacing="0"> <tr valign="middle"> <!-- Home link --> <th bgcolor="#70b0f0" class="navbar-select" > Home </th> <!-- Tree link --> <th> <a href="module-tree.html">Trees</a> </th> <!-- Index link --> <th> <a href="identifier-index.html">Indices</a> </th> <!-- Help link --> <th> <a href="help.html">Help</a> </th> <th class="navbar" width="100%"></th> </tr> </table> <table border="0" cellpadding="0" cellspacing="0" width="100%%"> <tr> <td align="left" class="footer"> Generated by Epydoc 3.0.1 on Mon Oct 21 13:23:56 2013 </td> <td align="right" class="footer"> <a target="mainFrame" href="http://epydoc.sourceforge.net" >http://epydoc.sourceforge.net</a> </td> </tr> </table> <script type="text/javascript"> <!-- // Private objects are initially displayed (because if // javascript is turned off then we want them to be // visible); but by default, we want to hide them. So hide // them unless we have a cookie that says to show them. checkCookie(); // --> </script> </body> </html>