In [7]:
%%bash
ncdump data/model-gfs.nc
In [8]:
from netCDF4 import Dataset, num2date
In [96]:
data = Dataset('data/model-gfs.nc', 'r')
ImportError on netCDF4? From the command line:
with anaconda:
with canopy:
In [23]:
# get the time variable, and convert to datetimes
time_var = data.variables['time']
print time_var
In [13]:
num2date?
In [18]:
print time_var.units
print time_var[:]
In [25]:
time = num2date(time_var[:], time_var.units ).squeeze()
print time.shape
In [10]:
import matplotlib.pyplot as plt
%matplotlib inline
In [30]:
fig, ax = plt.subplots(1,1, figsize=(12,6))
T_iso = data.variables['Temperature_isobaric']
ax.plot(time, T_iso[:].squeeze(), 'r-')
Out[30]:
In [31]:
from datetime import datetime
print datetime.now()
In [75]:
def set_defaults(ax, label_font):
ax.set_xlabel('Forecast time (UTC)', fontdict=label_font)
from matplotlib.dates import DateFormatter, DayLocator, HourLocator
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(range(6,24,6)))
ax.xaxis.set_major_formatter(DateFormatter('%b %d'))
ax.xaxis.set_minor_formatter(DateFormatter('%HZ'))
# format code documentation is at (among other places): http://strftime.org
In [52]:
fig, ax = plt.subplots(1, 1, figsize=(12, 6))
ax.plot(time, data.variables['Temperature_isobaric'][:].squeeze(), 'r-', linewidth=2)
set_defaults(ax)
In [53]:
vals = [1,2,3,4]
names = ['one', 'two','three','four']
pairs = zip(names,vals)
In [54]:
print zip(*pairs)
In [64]:
a,b,c,d,e = 'ericb'
print c
In [65]:
import math
def polar_to_cartesian(r,th):
x = r*math.cos(th)
y = r*math.sin(th)
return x, y
X, Y = polar_to_cartesian(2, math.pi/3)
print(X,Y)
In [69]:
format_template = "{0} == {1}, which is the {0}th number"
for nv in zip(names, vals):
print(format_template.format(*nv))
In [77]:
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
label_font = dict(size=16, weight='bold')
for ax, varname in zip(axes, ['Temperature_isobaric', 'Relative_humidity_isobaric']):
ax.plot(time, data.variables[varname][:].squeeze(), 'r-', linewidth=2)
set_defaults(ax, label_font)
ax.set_ylabel(varname, fontdict=label_font)
In [78]:
num_map = dict(zip(vals, names))
print num_map
In [80]:
print num_map[1]
In [83]:
states = dict(Colorado={'abbreviation': 'CO', 'capitol': 'Denver', 'notes': 'Home!', 'flat':False},
Oklahoma={'abbreviation': 'OK', 'capitol': 'Oklahoma City', 'flat': True},
Kansas={'abbreviation': 'KS', 'capitol': 'Topeka', 'flat': True})
In [84]:
states['Colorado']['capitol']
Out[84]:
In [92]:
def K2F(K):
return 1.8 * (K-273.15) + 32
print K2F(300)
print K2F
In [100]:
variable_styles = {'Temperature_isobaric':{'line':dict(color='r', marker='s', linestyle='-'),
'label':u'Temperature (°F)', 'converter':K2F},
'Relative_humidity_isobaric': {'line':dict(color='g', marker='o'),
'label':'RH (%)', 'converter':None}}
In [101]:
print variable_styles['Temperature_isobaric']['line']
print variable_styles['Relative_humidity_isobaric']['label']
In [102]:
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
label_font = dict(size=16, weight='bold')
vars_to_plot = ['Temperature_isobaric', 'Relative_humidity_isobaric']
for ax, varname in zip(axes, vars_to_plot):
data_vals = data.variables[varname][:].squeeze()
# get the converter from the dictionary
converter = variable_styles[varname]['converter']
if converter is not None:
# do something to the data if there is a converter
data_vals = converter(data_vals)
linespec = variable_styles[varname]['line']
ax.plot(time, data_vals, **linespec)
set_defaults(ax, label_font)
ax.set_ylabel(variable_styles[varname]['label'], fontdict=label_font)
What are good resources for further learning? Learning Python Python Essential Reference