In [31]:
import netCDF4
%matplotlib inline
from scipy.stats import binned_statistic_2d
import datetime as dt
import pandas as pd
import oceans
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma
In [32]:
url='http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/brbn4/brbn4.ncml'
nc = netCDF4.Dataset(url)
ncv = nc.variables
In [33]:
ncv.keys()
Out[33]:
In [34]:
time_var = ncv['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
In [35]:
# Extract desired times. Here we select a specific time of interest
start = dt.datetime(2010,8,1,0,0,0)
istart = netCDF4.date2index(start,time_var,select='nearest')
stop = dt.datetime(2010,9,1,0,0,0)
istop = netCDF4.date2index(stop,time_var,select='nearest')
In [36]:
# Get all time records of variable [vname] at indices [iy,ix]
vname = 'wind_spd'
var = ncv[vname]
var.shape
wspd = var[istart:istop,:,:].ravel()
In [37]:
vname = 'wind_dir'
var = ncv[vname]
var.shape
wdir = var[istart:istop,:,:].ravel()
In [38]:
tim = dtime[istart:istop]
In [39]:
# Create Pandas time series object
ts = pd.Series(wspd,index=tim)
In [40]:
ts.plot()
Out[40]:
In [41]:
u,v = oceans.spdir2uv(wspd,wdir,deg=True)
In [42]:
fig=plt.figure(figsize=(12,4))
plt.plot(tim,u,tim,v)
Out[42]:
In [43]:
f1,x,y,bin1 = binned_statistic_2d(u, v, u, statistic='mean', bins=10, range=None)
f2,x,y,bin2 = binned_statistic_2d(u, v, v, statistic='mean', bins=10, range=None)
In [44]:
u
Out[44]:
In [65]:
url='http://tds.marine.rutgers.edu/thredds/dodsC/met/ncdc-nam-3hour/Uwind_nam_3hourly_MAB_and_GoM_2009.nc'
nc = netCDF4.Dataset(url)
ncv = nc.variables
print ncv.keys()
In [84]:
url='http://tds.marine.rutgers.edu/thredds/dodsC/met/ncdc-nam-3hour/Uwind_nam_3hourly_MAB_and_GoM_2009.nc'
vname = 'Uwind'
nc = netCDF4.Dataset(url)
ncv = nc.variables
time_var = ncv['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
istart = netCDF4.date2index(start,time_var,select='nearest')
istop = netCDF4.date2index(stop,time_var,select='nearest')
var = ncv[vname]
j=78
i=69
v = var[istart:istop,j,i]
tim = dtime[istart:istop]
In [83]:
tim
Out[83]:
In [73]:
istart
Out[73]:
In [85]:
def get_nam_ts(url,vname,start=None,stop=None,j=None,i=None):
nc = netCDF4.Dataset(url)
ncv = nc.variables
time_var = ncv['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
istart = netCDF4.date2index(start,time_var,select='nearest')
istop = netCDF4.date2index(stop,time_var,select='nearest')
var = ncv[vname]
v = var[istart:istop,j,i]
tim = dtime[istart:istop]
return v,tim
In [59]:
ncv.keys()
Out[59]:
In [53]:
time_var = ncv['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
In [156]:
# Extract desired times. Here we select a specific time of interest
start = dt.datetime(2009,1,1,0,0,0)
stop = dt.datetime(2009,12,1,0,0,0)
url='http://tds.marine.rutgers.edu/thredds/dodsC/met/ncdc-nam-3hour/Uwind_nam_3hourly_MAB_and_GoM_2009.nc'
vname = 'Uwind'
u,tim = get_nam_ts(url,vname=vname,start=start,stop=stop,j=78,i=69)
url='http://tds.marine.rutgers.edu/thredds/dodsC/met/ncdc-nam-3hour/Vwind_nam_3hourly_MAB_and_GoM_2009.nc'
vname = 'Vwind'
v,tim = get_nam_ts(url,vname=vname,start=start,stop=stop,j=78,i=69)
fig = plt.figure(figsize=(16,4))
plt.plot(tim,u,tim,v)
plt.grid()
In [157]:
# since we are binning both u and v the bins will be the same for u and v
ubin,x,y,bin1 = binned_statistic_2d(u, v, u, statistic='mean', bins=10, range=None)
vbin,x,y,bin2 = binned_statistic_2d(u, v, v, statistic='mean', bins=10, range=None)
In [158]:
ubin = ma.masked_invalid(ubin)
vbin = ma.masked_invalid(vbin)
In [159]:
plt.pcolormesh(x,y,vbin)
Out[159]:
In [160]:
dirbin,spdbin = oceans.uv2spdir(ubin,vbin)
spdbin = ma.masked_invalid(spdbin)
plt.pcolormesh(x,y,spdbin)
Out[160]:
In [184]:
dir, spd = oceans.uv2spdir(u,v)
In [185]:
ind = np.where((spd>=3.6) & (spd<=10.8))[0]
In [186]:
len(ind)*1.0/len(spd)
Out[186]:
In [189]:
hist, bin_edges = np.histogram(dir[ind],18)
bottom = bin_edges[:-1]
heights = np.diff(bin_edges)
plt.barh(bottom,hist,height=heights)
#plt.barh(bin_edges,hist)
Out[189]:
In [190]:
d, s = oceans.uv2spdir(0,1)
print d,s
In [181]:
d,s = oceans.uv2spdir(0,0)
print d,s
In [133]:
heights
Out[133]:
In [134]:
centers
Out[134]:
In [135]:
hist
Out[135]:
In [ ]:
7-21