NDBC decided in 2011 to go with NetCDF4 files using groups to store buoy data. Can we read them using standard tools?
In [2]:
import urllib
import netCDF4
from IPython.core.display import HTML
import pandas as pd
If you visit http://www.nodc.noaa.gov/BUOY/ and select the "41013" buoy, then select a one of the links from the "Buoy Data (netCDF)" table, you will find yourself on a THREDDS dataset page like the one below
In [3]:
HTML('<iframe src=http://data.nodc.noaa.gov/thredds/catalog/ndbc/cmanwx/2013/08/catalog.html?dataset=ndbc/cmanwx/2013/08/NDBC_41013_201308_D2_v00.nc width=900 height=500></iframe>')
Out[3]:
In [4]:
# try to open DAP URL
dap_url='http://data.nodc.noaa.gov/thredds/dodsC/ndbc/cmanwx/2013/08/NDBC_41013_201308_D2_v00.nc'
nc = netCDF4.Dataset(dap_url)
In [5]:
# since DAP didn't work, download the entire NetCDF file
urllib.urlretrieve('http://data.nodc.noaa.gov/thredds/fileServer/ndbc/cmanwx/2013/08/NDBC_41013_201308_D2_v00.nc',
'NDBC_41013_201308_D2_v00.nc')
Out[5]:
In [6]:
# open the local netcdf file
nc = netCDF4.Dataset('NDBC_41013_201308_D2_v00.nc')
In [7]:
# see if there are variables at the top level
nc.variables.keys()
Out[7]:
In [8]:
# convert time to Python datetime objects
time_var = nc.variables['time']
print time_var
jd = netCDF4.num2date(time_var[:],time_var.units)
In [9]:
# see if there are groups
nc.groups.keys()
Out[9]:
In [10]:
# only one group. Let's take a look:
payload1= nc.groups['payload_1']
In [11]:
# are there variables in payload_1?
payload1.variables.keys()
Out[11]:
In [12]:
# are there more groups in payload_1?
payload1.groups.keys()
Out[12]:
In [13]:
# let's pick 'anemometer_2' from 'payload_1'
payload1_anemometer2 = payload1.groups['anemometer_2']
In [14]:
# any groups in group 2?
payload1_anemometer2.groups.keys()
Out[14]:
In [15]:
# any variables in group 2?
payload1_anemometer2.variables.keys()
Out[15]:
In [16]:
# open the wind speed variable from /payload_1/anemometer_2
var = payload1_anemometer2.variables['wind_speed']
In [17]:
print var
In [18]:
# Create Pandas time series object, using the time from group 0, and data from group 12
ts = pd.Series(var,index=jd)
In [19]:
# Make a Plot
ts.plot(title=var.long_name,figsize=(12,4))
ylabel(var.units)
Out[19]:
In [19]: