From mn/geo/geoit
Jump to: navigation, search

Plotting Meteorological Data with Python

This is a very brief introduction to using pygrib and NetCDFFile in order to plot met data in Python. Note that you should read the Python/SettingPaths section in order to be sure that you'll be able to import the appropriate modules.

Let's get started with some test data::

   mkdir test
  cd test
  cp ~sec/kleinproject/FUKU/geop_april* .

Now, start ipython in your shell, or create a python file in the test directory::

Next, let's take care of some basic imports::
import matplotlib.pyplot as plt
import pygrib
from netCDF4 import Dataset as NetCDFFile

Now, we'll write some python code to plot the netcdf variables::
nci = NetCDFFile('')
nci.variables #will print a listing of the variables (note: nci.variables is a dictionary)
z = nci.variables['z']
z.shape #what shape is the 'z' array?

for i in range(3):
 plt.imshow(z[0,i,:,:]) #we take the i'th slab
 plt.title('netcdf geopot {0}'.format(i))
Now, let's take a look at the grib data::
grbs ='geop_april_01_2011.grib')
for grb in grbs:
This will print a listing of all the gribs in the grib file. Make sure we reset the grbs back to the beginning::
ghs ='Geopotential')

for i,grb in enumerate(ghs):
 g = grb
 title = + ' at level ' + str(g.level) + ' [' + g.units + ']'
 plt.savefig( + str(i) + '.png')
That's it! In your folder you should have some test plots.