Difference between revisions of "Python/PlotMetData"
From mn/geo/geoit
(→Plotting Meteorological Data with Python) |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | =Plotting Meteorological Data with Python= | + | = 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 [[ | + | 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:: | Let's get started with some test data:: | ||
mkdir test | 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:: | Now, start ipython in your shell, or create a python file in the test directory:: | ||
ipython | ipython | ||
− | + | ||
− | Next, let's take care of some basic imports:: | + | |
− | <source lang= | + | Next, let's take care of some basic imports:: <source lang="python"> |
import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
Line 21: | Line 22: | ||
</source> | </source> | ||
− | + | <br/>Now, we'll write some python code to plot the netcdf variables:: <source lang="python"> | |
− | Now, we'll write some python code to plot the netcdf variables:: | ||
− | <source lang= | ||
nci = NetCDFFile('geopot_april_01.nc') | nci = NetCDFFile('geopot_april_01.nc') | ||
nci.variables #will print a listing of the variables (note: nci.variables is a dictionary) | nci.variables #will print a listing of the variables (note: nci.variables is a dictionary) | ||
z = nci.variables['z'] | z = nci.variables['z'] | ||
− | z.shape | + | z.shape #what shape is the 'z' array? |
for i in range(3): | for i in range(3): | ||
− | + | plt.figure() | |
− | + | plt.imshow(z[0,i,:,:]) #we take the i'th slab | |
− | + | plt.title('netcdf geopot {0}'.format(i)) | |
− | + | plt.colorbar() | |
− | + | plt.savefig('nc_geopot_{}'.format(i)) | |
− | + | ||
− | </source> | + | </source> Now, let's take a look at the grib data:: <source lang="python"> |
− | Now, let's take a look at the grib data:: | ||
− | <source lang= | ||
grbs = pygrib.open('geop_april_01_2011.grib') | grbs = pygrib.open('geop_april_01_2011.grib') | ||
for grb in grbs: | for grb in grbs: | ||
− | + | grb | |
− | </source> | + | </source> This will print a listing of all the gribs in the grib file. Make sure we reset the grbs back to the beginning:: <source lang="python"> |
− | This will print a listing of all the gribs in the grib file. Make sure we reset the grbs back to the beginning:: | ||
− | <source lang= | ||
grbs.seek(0) | grbs.seek(0) | ||
ghs = grbs.select(name='Geopotential') | ghs = grbs.select(name='Geopotential') | ||
for i,grb in enumerate(ghs): | for i,grb in enumerate(ghs): | ||
− | + | plt.figure() | |
− | + | g = grb | |
− | + | plt.imshow(g.values) | |
− | + | title = g.name + ' at level ' + str(g.level) + ' [' + g.units + ']' | |
− | + | plt.title(title) | |
− | + | plt.colorbar() | |
− | + | plt.savefig(g.name + str(i) + '.png') | |
− | </source> | + | </source> That's it! In your folder you should have some test plots. |
− | That's it! In your folder you should have some test plots. |
Latest revision as of 10:29, 11 February 2015
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::
ipythonNext, 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('geopot_april_01.nc')
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.figure()
plt.imshow(z[0,i,:,:]) #we take the i'th slab
plt.title('netcdf geopot {0}'.format(i))
plt.colorbar()
plt.savefig('nc_geopot_{}'.format(i))
grbs = pygrib.open('geop_april_01_2011.grib')
for grb in grbs:
grb
grbs.seek(0)
ghs = grbs.select(name='Geopotential')
for i,grb in enumerate(ghs):
plt.figure()
g = grb
plt.imshow(g.values)
title = g.name + ' at level ' + str(g.level) + ' [' + g.units + ']'
plt.title(title)
plt.colorbar()
plt.savefig(g.name + str(i) + '.png')