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.h5'):
data = woa_subset(var='salinity', clim_type='annual',
resolution='1deg', **boundary)
data = data['OA Climatology']['annual'].squeeze()
data.to_hdf('woa09_salinity.h5', 'salinity')
In [ ]:
if not os.path.isfile('woa09_temperature.h5'):
data = woa_subset(var='temperature', clim_type='annual',
resolution='1deg', **boundary)
data = data['OA Climatology']['annual']
data.to_hdf('woa09_temperature.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
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
lon, lat, data = get_data('woa09_salinity.h5', 'salinity', depth)
levels = np.arange(27.7, 37.3, 0.3)
cs = ax.pcolormesh(lon, lat, data, cmap=cm.odv, **proj)
cs.set_clim(27.5, 37.5)
cc = ax.contour(lon, lat, data, levels=levels, **kw)
ax.clabel(cc, inline=True, fmt='%2.1f', fontsize=10)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_salinity_0_annual.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
depth = 1000.0
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
lon, lat, data = get_data('woa09_salinity.h5', 'salinity', depth)
levels = np.arange(34.3, 35.75, 0.2)
cs = ax.pcolormesh(lon, lat, data, cmap=cm.odv, **proj)
cs.set_clim(34.3, 35.65)
cc = ax.contour(lon, lat, data, levels=levels, **kw)
ax.clabel(cc, inline=True, fmt='%2.1f', fontsize=10)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_salinity_1000_annual.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
depth = 4500.0
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
lon, lat, data = get_data('woa09_salinity.h5', 'salinity', depth)
levels = np.arange(34.4, 35, 0.2)
cs = ax.pcolormesh(lon, lat, data, cmap=cm.odv, **proj)
cs.set_clim(34.4, 35)
cc = ax.contour(lon, lat, data, levels=levels, **kw)
ax.clabel(cc, inline=True, fmt='%2.1f', fontsize=10)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_salinity_4500_annual.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
depth = 0.0
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
lon, lat, data = get_data('woa09_temperature.h5', 'temperature', depth)
levels = np.arange(-1, 30, 1.5)
cs = ax.pcolormesh(lon, lat, data, cmap=cm.avhrr, **proj)
cs.set_clim(-1, 29)
cc = ax.contour(lon, lat, data, levels=levels, **kw)
ax.clabel(cc, inline=True, fmt='%2.1f', fontsize=10)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_temperature_0_annual.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
depth = 1000.0
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
lon, lat, data = get_data('woa09_temperature.h5', 'temperature', depth)
levels = np.arange(-2, 15, 0.5)
cs = ax.pcolormesh(lon, lat, data, cmap=cm.avhrr, **proj)
cs.set_clim(-2, 15)
cc = ax.contour(lon, lat, data, levels=levels, **kw)
ax.clabel(cc, inline=True, fmt='%2.1f', fontsize=10)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_temperature_1000_annual.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
depth = 4500.0
fig, ax = make_map(projection=ccrs.PlateCarree(central_longitude=-150))
lon, lat, data = get_data('woa09_temperature.h5', 'temperature', depth)
levels = np.arange(-0.6, 10, 0.2)
cs = ax.pcolormesh(lon, lat, data, cmap=cm.avhrr, **proj)
cs.set_clim(-0.6, 10)
cc = ax.contour(lon, lat, data, levels=levels, **kw)
ax.clabel(cc, inline=True, fmt='%2.1f', fontsize=10)
_ = fig.colorbar(cs, **kcbar)
In [ ]:
fname = 'woa09_temperature_4500_annual.%s' % fmt
fig.savefig(fname, **kfig)
_ = os.system('convert -trim %s %s' % (fname, fname))
In [ ]:
HTML(html)