You are on page 1of 7

PyGribDocumentation

by
Arulalan.T
arulalant@gmail.com

Project Associate,
Centre for Atmospheric Sciences ,
Indian Institute of Technology Delhi.
03.05.2011

-1-

PygribInstallationinCDATpathprocedure
NowwearegoingtoseetheprocedurehowtoinstallPyGrib[1]insidetheCDAT[2]installedpathin
oursystem.
[1]http://code.google.com/p/pygrib/
[2]http://www2pcmdi.llnl.gov/cdat
LetusassumewehaveinstalledCDATinoursystemanditsworkingpathis/usr/local/cdat5.2/bin/.
Makesurethatthepathworkingfineaslikebelow.
$/usr/local/cdat5.2//bin/python
Executing/usr/local/cdat5.2//bin/python
Python2.5.2(r252:60911,Feb22011,15:29:23)
[GCC4.4.5]onlinux2
Typehelp,copyright,creditsorlicenseformoreinformation.
>>>
ToinstallPyGribinyournormalpythonpath,kindlyfollowthelink
http://code.google.com/p/pygrib/wiki/LinuxMacInstallation
FirstInstallthefollowingdependenciesforpygribinourubuntu.
$sudoaptgetinstalllibjasperdevlibjasperruntime.
Note:DoNotinstalllibjasperjava,jasperandopenjpegtools.
Ifyouinstalledtheseabove3packages,thenitshouldcrashourubuntuloginanditsX.
Theseareallstufftestedinubuntu10.10.
libjasperdevandlibjasperruntimeareworkingfinewithoutmakinganyproblem.
Downloadgrib_api1.9.5.tar.gzfromhttp://www.ecmwf.int/products/data/software/download/grib_api.html
Downloadpyproj1.8.8.tar.gzfromhttp://code.google.com/p/pyproj/downloads/list
Downloadpygrib1.8.0.tar.gzfromhttp://code.google.com/p/pygrib/downloads/list
Nowweneedtomovethethreedownloadedpackagestothecdatpathaslikebelow
$mvgrib_api1.9.5.tar.gz/usr/local/cdat5.2/bin/
$mvpyproj1.8.8.tar.gz/usr/local/cdat5.2/bin/
$mvpygrib1.8.0.tar.gz/usr/local/cdat5.2/bin/
Nowchangethecurrentworkingdirectoryintocdat.
$cd/usr/local/cdat5.2/bin/
$pwd
/usr/local/cdat5.2/bin/
$suroot

-2-

Installinggribapi
Followingtheseinstructionsshouldbesafeonanaveragelinuxmachine:

mkdirgrib_api_dir
tarzxvfgrib_api1.9.5.tar.gz
cdgrib_api1.9.5
exportCFLAGS=O2fPIC
./configureprefix=`pwd`/../grib_api_dir
make
makecheck
makeinstall
cd..
pwd
/usr/local/cdat5.2/bin/

Installingpyproj
Thisoneisverysimple,justlikethenormalpythoninstall:

tarzxvfpyproj1.8.8.tar.gz
cdpyproj1.8.8
/usr/local/cdat5.2/bin//pythonsetup.pyinstall
cd..
pwd
/usr/local/cdat5.2/bin/

Installingpygrib
Installingpygribinsidecdatpath.

exportGRIBAPI_DIR=`pwd`/grib_api_dir
exportJASPER_DIR=/usr
tarzxvfpygrib1.8.0.tar.gz
cdpygrib1.8.0
/usr/local/cdat5.2/bin//pythonsetup.pyinstall
/usr/local/cdat5.2/bin//pythontest.py
pwd
/usr/local/cdat5.2/bin/pygrib

CheckingpygribinCDATpath
openanewterminalasanormaluser

$/usr/local/cdat5.2//bin/pythonExecuting/usr/local/cdat5.2//bin/python
Python2.5.2(r252:60911,Feb22011,15:29:23)
[GCC4.4.5]onlinux2
Typehelp,copyright,creditsorlicenseformoreinformation.
>>>importpygrib
>>>
Thatsit.Wehavesuccessfullyinstalledpygribinourcdatpathandimportedit.

-3-

PygribUsage

fromthepythoninterpreterprompt,importthepackage:
>>>importpygrib

openaGRIBfile,createagribmessageiterator:
>>>grbs=pygrib.open('sampledata/flux.grb')

pygribopeninstancesbehavelikeregularpythonfileobjects,

withseek,tell,read,readlineandclosemethods,exceptthatoffsetsaremeasuredingribmessages
insteadofbytes:
>>>grbs.seek(2)
>>>grbs.tell()
2
>>>grb=grbs.read(1)[0]#readreturnsalistwiththenextN(N=1inthiscase)messages.
>>>grb#printingagribmessageobjectdisplayssummaryinfo
3:Maximumtemperature:K(instant):regular_gg:heightAboveGround:level2m:fcsttime108
120:from200402291200
>>>grbs.tell()
3

printaninventoryofthefile:
>>>grbs.seek(0)
>>>forgrbingrbs:
>>>grb
1:Precipitationrate:kgm**2s**1(avg):regular_gg:surface:level0:fcsttime108120:from
200402291200
2:Surfacepressure:Pa(instant):regular_gg:surface:level0:fcsttime120:from200402291200
3:Maximumtemperature:K(instant):regular_gg:heightAboveGround:level2m:fcsttime108
120:from200402291200
4:Minimumtemperature:K(instant):regular_gg:heightAboveGround:level2m:fcsttime108
120:from200402291200

findthefirstgribmessagewithamatchingname:
>>>grb=grbs.select(name='Maximumtemperature')[0]

extractthedatavaluesusingthe'values'key(grb.keys()willreturnalistoftheavailablekeys):
#Thedataisreturnedasanumpyarray,orifmissingvaluesorabitmap
#arepresent,anumpymaskedarray.Reducedlat/lonorgaussiangrid
#dataisautomaticallyexpandedtoaregulargrid.Detailsoftheinternal
#representationofthegribdata(suchasthescanningmode)arehandled
#automatically.
>>>data=grb.values#sameasgrb['values']
>>>data.shape,data.min(),data.max()
(94,192)223.7319.9

getthelatitudesandlongitudesofthegrid:
>>>lats,lons=grb.latlons()
>>>lats.shape,lats.min(),lats.max(),lons.shape,lons.min(),lons.max()
(94,192)88.541950137388.54195013730.0358.125

-4-

getthesecondgribmessage:
>>>grb=grbs.message(2)#sameasgrbs.seek(1);grb=grbs.readline()
>>>grb
2:Surfacepressure:Pa(instant):regular_gg:surface:level0:fcsttime120:from200402291200

modifythevaluesassociatedwithexistingkeys(eitherviaattributeordictionaryaccess):
>>>grb['forecastTime']=240
>>>grb.dataDate=20100101

getthebinarystringassociatedwiththecodedmessage:
>>>msg=grb.tostring()
>>>grbs.close()#closethegribfile.

writethemodifiedmessagetoanewGRIBfile:
>>>grbout=open('test.grb','wb')
>>>grbout.write(msg)
>>>grbout.close()
>>>pygrib.open('test.grb').readline()
1:Surfacepressure:Pa(instant):regular_gg:surface:level0:fcsttime240:from201001011200

FewMoreexamples

Toopen
>>>grbs=pygrib.open('gdas1.t00z.grbanl')

Togetthefilename
>>>grbs.name
'gdas1.t00z.grbanl'

Togethowmanymessages/linesithas
>>>grbs.messages
361

Togetmessageofparticularlineno
>>>grbs[5]

5:GeopotentialHeight:gpm(instant):regular_ll:isobaricInhPa:level900:fcsttime0:from201005250000

>>>grbs.message(250)
250:Vcomponentofwind:ms**1(instant):regular_ll:pressureFromGroundLayer:levelNone:fcsttime0:from201005250000

Note:grbmessagenumbersstartat1

Togetthenextmessagefromcurrentposition
>>>grbs.next()
251:Vcomponentofwind:ms**1(instant):regular_ll:pressureFromGroundLayer:levelNone:fcsttime0:from201005250000

Tomovethefilepointertothefirstposition
>>>grbs.seek(0)
>>>grbs.next()
1:GeopotentialHeight:gpm(instant):regular_ll:isobaricInhPa:level1000:fcsttime0:from201005250000

Togetthefirstmessage
>>>g=grbs[1]
>>>g.messagenumber
1

-5-

Togetthekeysofthemessage
>>>g.keys()

['parametersVersion','definitionFilesVersion','two','three','truncateDegrees','offset','count','countTotal','unitsFactor','unitsBias','eps','globalDomain','libraryVersion',
'kindOfProduct','GRIBEditionNumber','offsetSection0','section0Length','totalLength','editionNumber','WMO','productionStatusOfProcessedData','section1Length','table2Version',
'centre','generatingProcessIdentifier','gridDefinition','indicatorOfParameter','parameterName','parameterUnits','indicatorOfTypeOfLevel','pressureUnits','typeOfLevel','level',
'yearOfCentury','month','day','hour','minute','second','unitOfTimeRange','P1','P2','timeRangeIndicator','numberIncludedInAverage','numberMissingFromAveragesOrAccumulations',
'centuryOfReferenceTimeOfData','subCentre','paramIdECMF','paramId','cfName','units','name','decimalScaleFactor','setLocalDefinition','dataDate','year','dataTime','julianDay',
'stepUnits','stepType','stepRange','startStep','endStep','marsParam','validityDate','validityTime','wrongPadding','deleteLocalDefinition','localUsePresent','shortNameECMF',
'shortName','ifsParam','gridDescriptionSectionPresent','bitmapPresent','section2Length','radius','shapeOfTheEarth','numberOfVerticalCoordinateValues','neitherPresent',
'pvlLocation','dataRepresentationType','gridDefinitionTemplateNumber','Ni','Nj','latitudeOfFirstGridPoint','latitudeOfFirstGridPointInDegrees','longitudeOfFirstGridPoint',
'longitudeOfFirstGridPointInDegrees','resolutionAndComponentFlags','ijDirectionIncrementGiven','earthIsOblate','resolutionAndComponentFlags3','resolutionAndComponentFlags4',
'uvRelativeToGrid','resolutionAndComponentFlags6','resolutionAndComponentFlags7','resolutionAndComponentFlags8','latitudeOfLastGridPoint','latitudeOfLastGridPointInDegrees',
'longitudeOfLastGridPoint','longitudeOfLastGridPointInDegrees','iDirectionIncrement','jDirectionIncrement','scanningMode','iScansNegatively','jScansPositively',
'jPointsAreConsecutive','alternativeRowScanning','iScansPositively','scanningMode4','scanningMode5','scanningMode6','scanningMode7','scanningMode8',
'jDirectionIncrementInDegrees','iDirectionIncrementInDegrees','numberOfDataPoints','numberOfValues','latLonValues','latitudes','longitudes','distinctLatitudes','distinctLongitudes',
'PVPresent','PLPresent','lengthOfHeaders','missingValue','tableReference','section4Length','halfByte','dataFlag','binaryScaleFactor','referenceValue','bitsPerValue',
'referenceValueError','sphericalHarmonics','complexPacking','integerPointValues','additionalFlagPresent','packingType','bitMapIndicator','values','numberOfCodedValues',
'packingError','unpackedError','maximum','minimum','average','numberOfMissing','standardDeviation','skewness','kurtosis','isConstant','dataLength','changeDecimalPrecision',
'decimalPrecision','bitsPerValueAndRepack','scaleValuesBy','offsetValuesBy','gridType','getNumberOfValues','section5Length']

Fromtheabovekeyswecanchooseanyoneandpassittothegvarasshownlikebelow

Togetthedataofthemessage
>>>g['values']
array([[289.1,289.1,289.1,...,289.1,289.1,289.1],
[300.8,300.8,300.8,...,301.,300.9,300.9],
[293.7,293.6,293.6,...,293.7,293.7,293.7],
...,
[231.2,231.3,231.4,...,231.,231.1,231.1],
[232.2,232.3,232.3,...,232.1,232.1,232.2],
[233.,233.,233.,...,233.,233.,233.]])

TogetthemissingValueofthemessage
>>>g['missingValue']
9999

Togetthelevelofthemessage
>>>g['level']
1000

Togetthelatitudesasnumpyarray
>>>g['distinctLatitudes']
array([90.,89.5,89.,88.5,88.,87.5,87.,86.5,86.,
,,,
85.5,86.,86.5,87.,87.5,88.,88.5,89.,89.5,90.])

Togetthelongitudesasnumpyarray
>>>g['distinctLongitudes']
array([0.,0.5,1.,1.5,2.,2.5,3.,3.5,
,,,
356.,356.5,357.,357.5,358.,358.5,359.,359.5])

Togetbothlatitudesandlongitudes
>>>g.latlons()
(array([[90.,90.,90.,...,90.,90.,90.],
...,
[90.,90.,90.,...,90.,90.,90.]]),array([[0.,0.5,1.,...,358.5,359.,359.5],
...,
[0.,0.5,1.,...,358.5,359.,359.5]]))

-6-

Usingforloop
>>>foriingrbs:

printi
1:GeopotentialHeight:gpm(instant):regular_ll:isobaricInhPa:level1000:fcsttime0:from201005250000
2:GeopotentialHeight:gpm(instant):regular_ll:isobaricInhPa:level975:fcsttime0:from201005250000
3:GeopotentialHeight:gpm(instant):regular_ll:isobaricInhPa:level950:fcsttime0:from201005250000

360:Vcomponentofwind:ms**1(instant):regular_ll:maxWind:level0:fcsttime0:from201005250000
361:Vcomponentofwind:ms**1(instant):regular_ll:sigma:level9950:fcsttime0:from201005250000

Togetdataandotherpropertiesbelongstoonegribfile,variableName,typeOfLevel,andlevel
byfastestwayusingthefileindex
>>>file_name='gdas1.t00z.grbanl'
>>>fileidx=pygrib.index(file_name,'name','typeOfLevel','level')
>>>g=fileidx.select(name="GeopotentialHeight",typeOfLevel="isobaricInhPa",level=1000)
>>>g
[1:GeopotentialHeight:gpm(instant):regular_ll:isobaricInhPa:level1000:fcsttime0:from201005250000]

>>>g[0]['values']
array([[289.1,289.1,289.1,...,289.1,289.1,289.1],

[300.8,300.8,300.8,...,301.,300.9,300.9],
...,
[233.,233.,233.,...,233.,233.,233.]])
Notes:
1. Herethis'fileidx'belongstothefile_name='gdas1.t00z.grbanl'
2. fileindexismorefasterthanpygrib.open()method
3. Infileindexwecansettheargumentsmaximumonlythefollowingthreeoptions...
'name','typeOfLevel','level'

Links:

HomePage:http://code.google.com/p/pygrib/

Documentation:http://pygrib.googlecode.com/svn/trunk/docs/index.html

InstallationinSystemPythonPath:http://code.google.com/p/pygrib/wiki/LinuxMacInstallation

InstallationinCDATPath:http://tuxcoder.wordpress.com/2011/05/03/howtoinstallpygribinside
ourcdatpath/

-7-

You might also like