plotsst.py 1.73 KB
Newer Older
1
from mpl_toolkits.basemap import Basemap
2
from netCDF4 import Dataset, date2index
3 4
import numpy as np
import matplotlib.pyplot as plt
5 6 7
from datetime import datetime
date = datetime(2007,12,15,0) # date to plot.
# open dataset.
8
dataset = \
9 10 11
Dataset('http://www.ncdc.noaa.gov/thredds/dodsC/OISST-V2-AVHRR-AMSR_agg')
timevar = dataset.variables['time']
timeindex = date2index(date,timevar) # find time index for desired date.
12
# read sst.  Will automatically create a masked array using
13
# missing_value variable attribute. 'squeeze out' singleton dimensions.
14
sst = dataset.variables['sst'][timeindex,:].squeeze()
15
# read ice.
16
ice = dataset.variables['ice'][timeindex,:].squeeze()
17 18 19
# read lats and lons (representing centers of grid boxes).
lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]
20
lons, lats = np.meshgrid(lons,lats)
21
# create figure, axes instances.
22 23
fig = plt.figure()
ax = fig.add_axes([0.05,0.05,0.9,0.9])
24
# create Basemap instance.
25 26
# coastlines not used, so resolution set to None to skip
# continent processing (this speeds things up a bit)
27
m = Basemap(projection='kav7',lon_0=0,resolution=None)
28 29 30 31 32
# draw line around map projection limb.
# color background of map projection region.
# missing values over land will show up this color.
m.drawmapboundary(fill_color='0.3')
# plot sst, then ice with pcolor
33 34
im1 = m.pcolormesh(lons,lats,sst,shading='flat',cmap=plt.cm.jet,latlon=True)
im2 = m.pcolormesh(lons,lats,ice,shading='flat',cmap=plt.cm.gist_gray,latlon=True)
35
# draw parallels and meridians, but don't bother labelling them.
36 37
m.drawparallels(np.arange(-90.,99.,30.))
m.drawmeridians(np.arange(-180.,180.,60.))
38 39 40 41
# add colorbar
cb = m.colorbar(im1,"bottom", size="5%", pad="2%")
# add a title.
ax.set_title('SST and ICE analysis for %s'%date)
42
plt.show()