In [ ]:
from IPython.core.display import HTML
with open('../../common/creativecommons.html', 'r') as f:
html = f.read()
with open('../../common/custom.css', 'r') as f:
styles = f.read()
HTML(styles)
name = get_notebook_name()
text = 'Check this post at'
url = 'http://nbviewer.ipython.org/urls/raw.github.com/'
url += 'ocefpaf/python4oceanographers/master/content/downloads/notebooks'
link = """<p>%s <a href="%s/%s"><em>nbviewer</em>.</a></p>""" % (text, url, name)
html += str(link)
In [ ]:
import os
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
In [ ]:
from pandas import read_hdf
from oceans.colormaps import cm
from oceans.RPSstuff import near
from oceans.datasets import woa_subset
def get_data(fname, key, depth=0):
data = read_hdf(fname, key)
lat = data.major_axis.values.astype(float)
lon = data.minor_axis.values.astype(float)
levels = data.items.values.astype(float)
nearest_idx = near(depth, levels)[0]
nearest_depth = levels[nearest_idx]
data = data[float(nearest_depth)]
data = ma.masked_equal(data.values, 9.969210e+36)
lon, lat = np.meshgrid(lon, lat)
return lon, lat, data
In [ ]:
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
def make_map(projection=ccrs.PlateCarree(), figsize=(10, 5),
resolution='110m'):
fig, ax = plt.subplots(figsize=figsize,
subplot_kw=dict(projection=projection))
ax.set_global()
ax.coastlines(resolution=resolution, color='k')
ax.add_feature(cfeature.LAND, facecolor='0.75')
# Gridlines.
gl = ax.gridlines(draw_labels=True)
gl.xlabels_top = False
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
# Work around writing longitude position twice (e.g.: 180W and 180E).
gl.xlocator = mticker.FixedLocator([-60, -120, -180, 120, 60, 0])
return fig, ax
In [ ]:
boundary = dict(llcrnrlon=0, urcrnrlon=360, llcrnrlat=-90, urcrnrlat=90)
In [ ]:
if not os.path.isfile('woa09_salinity_seasonal.h5'):
data = woa_subset(var='salinity', clim_type='seasonal',
resolution='1deg', **boundary)
data = data['Statistical Mean']['Nov'].squeeze()
data.to_hdf('woa09_salinity_seasonal.h5', 'salinity')
In [ ]:
if not os.path.isfile('woa09_temperature_seasonal.h5'):
data = woa_subset(var='temperature', clim_type='seasonal',
resolution='1deg', **boundary)
data = data['Statistical Mean']['Nov'].squeeze()
data.to_hdf('woa09_temperature_seasonal.h5', 'temperature')
In [ ]:
fmt = 'png'
kfig = dict(format=fmt, transparent=True, dpi=75)
kcbar = dict(extend='both', shrink=0.75, pad=0.02, fraction=0.1,
orientation='vertical')
proj = dict(transform=ccrs.PlateCarree())
kw = dict(colors='k', alpha=0.75, **proj)
In [ ]:
depth = 0.0
lon, lat, annual = get_data('woa09_salinity.h5', 'salinity', depth)
lon, lat, summer = get_data('woa09_salinity_seasonal.h5', 'salinity', depth)
data = annual - summer
In [ ]:
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
cs = ax.pcolormesh(lon, lat, data, cmap=cm.odv, **proj)
cs.set_clim(-1, 1)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_salinity_summer_anomaly.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
depth = 0.0
lon, lat, annual = get_data('woa09_temperature.h5', 'temperature', depth)
lon, lat, summer = get_data('woa09_temperature_seasonal.h5', 'temperature', depth)
data = annual - summer
In [ ]:
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
cs = ax.pcolormesh(lon, lat, data, cmap=cm.avhrr, **proj)
cs.set_clim(-1, 1)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_temperature_summer_anomaly.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
HTML(html)