In [22]:
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

In [2]:
url='http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/brbn4/brbn4.ncml'
nc = netCDF4.Dataset(url)
ncv = nc.variables

In [3]:
ncv.keys()


Out[3]:
[u'latitude',
 u'longitude',
 u'time',
 u'wind_dir',
 u'wind_spd',
 u'gust',
 u'wave_height',
 u'dominant_wpd',
 u'average_wpd',
 u'mean_wave_dir',
 u'air_pressure',
 u'air_temperature',
 u'sea_surface_temperature',
 u'dewpt_temperature',
 u'visibility',
 u'water_level']

In [4]:
time_var = ncv['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)

In [7]:
# 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 [8]:
# 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 [9]:
vname = 'wind_dir'
var = ncv[vname]
var.shape
wdir = var[istart:istop,:,:].ravel()

In [10]:
tim = dtime[istart:istop]

In [11]:
# Create Pandas time series object
ts = pd.Series(wspd,index=tim)

In [12]:
ts.plot()


Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd7bebb16d0>

In [17]:
u,v = oceans.spdir2uv(wspd,wdir,deg=True)

In [21]:
fig=plt.figure(figsize=(12,4))
plt.plot(tim,u,tim,v)


Out[21]:
[<matplotlib.lines.Line2D at 0x7fd7bb2f1810>,
 <matplotlib.lines.Line2D at 0x7fd7bb27a910>]

In [27]:
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 [30]:
u


Out[30]:
masked_array(data = [-- 2.4492935982947064e-16 -- ..., -- -- --],
             mask = [ True False  True ...,  True  True  True],
       fill_value = 99.0)

In [ ]:
url='http://tds.marine.rutgers.edu/thredds/dodsC/met/ncdc-nam-3hour/swrad_nam_3hourly_MAB_and_GoM_2009.nc'