Skip to content

Tile Plotting Tutorial

barronh edited this page Apr 20, 2016 · 6 revisions

How to make a typical tileplot

Below are two methods for making tile plots. The first is in the python environment and the second is from the command line interface. These tutorials show work with CMAQ, but can easily be modified for CAMx or GEOS-Chem (see [modifications](Tutorial Modifications))

Method 1 - Python with PseudoNetCDF

This option uses PseudoNetCDF functions to make tile plotting easy. You can then add anything else you want using standard matplotlib and basemap functions.

# point to a real CMAQ (or CAMx or GEOS-Chem file)
inpath = '/path/to/inputfile'

# import numeric array library
import numpy as np

# import plotting library to save figures
from matplotlib import pyplot as plt

# import NetCDF Processing loading functions
from PseudoNetCDF.pncparse import pncparse

# import coordinate processing functions
from PseudoNetCDF.coordutil import getmap, getxbnds, getybnds

# Read in all inputs with the following options
# returns all files and an archive of arguments (including defaults)
# implicit option '-f', 'netcdf' opens the file as netcdf (other -f options "bpch", "uamiv")
# option '--from-conv', 'ioapi' uses the ioapi meta-data to derive latitudes, longitudes, and time variables
# option '--slice', 'LAY,0' takes the first layer
# option '--reduce', 'TSTEP,mean' takes the average over time (use any function in the numpy library)
infiles, args = pncparse(args = [inpath, '--from-conv', 'ioapi', '--variables', 'O3', '--slice', 'LAY,0', '--reduce', 'TSTEP,mean'])
infile = infiles[0]

# Make a map and add features
map = getmap(infile)
map.drawcoastlines()
map.drawstates()
map.drawcounties()

# uncomment the next line to add a custom shape file
#map.readshapefile(shapefilepath, 'name1')

xbnds, xunit = getxbnds(infile)
ybnds, yunit = getybnds(infile)
X, Y = np.meshgrid(xbnds, ybnds)
O3 = infile.variables['O3'][0, 0] # remove the singlet dimensions for time and layer
patches = map.pcolor(X, Y, O3)

# uncomment out the next lines to add observations as overlaid dots
# with the same color scale as the model
# you must define obsx, obsy, and obsvalues
# map.scatter(obsx, obsy, c = obsvalues, marker = 'o', size = 40, norm = patches.norm)
plt.colorbar()
plt.savefig('O3_lay1_tavg_pypnc.png')

Method 2 - Python with pncmap.py

This method basically does the same thing as above, but from the command line with several additional options. To add observations, simply add a 1-D files after the 2-D tile plot data. See pncmap.py --help for more options

!pncmap.py -v O3 --from-conv=ioapi -s LAY,0 -r TSTEP,mean --norm="Normalize()" /path/to/inputfile O3_lay1_tavg_pnc
Clone this wiki locally