Accessing the Unidata THREDDS NetCDF Subset Service

Using the grid as point service, extracting a time series closest to a specified lon,lat location.

At first we use the NetCDF Subset Web Form below to construct the query, but then reuse the URL generated by the form directly in the Python code below. In this way different time periods, depths or variables may be extracted without returning to the form, and analyzed and visualized in Python without saving and loading CSV files.




In [15]:
import pandas as pd
import time

In [16]:
var = 'temp,salt'
lat = 39.93
lon = -74.12
#start = '2012-05-01T00:00:00Z'
start = '2012-03-01T00:30:00Z'
stop =  '2012-05-10T00:00:00Z'
level = 7

In [16]:


In [17]:
url='http://geoport.whoi.edu/thredds/ncss/grid/bbleh/spring2013?\
var=%s&latitude=%f&longitude=%f&time_start=%s&time_end=%s&\
vertCoord=%d&accept=csv' % (var,lat,lon,start,stop,level)
print(url)


http://geoport.whoi.edu/thredds/ncss/grid/bbleh/spring2013?var=temp,salt&latitude=39.930000&longitude=-74.120000&time_start=2012-03-01T00:30:00Z&time_end=2012-05-10T00:00:00Z&vertCoord=7&accept=csv

In [18]:
# Extract CSV data directly into Pandas DataFrame (and time how long it takes to extract the data)
time0=time.time()
df = pd.read_csv(url,index_col='date',parse_dates=True)  # skip the units row 
print('Elapsed time=%d seconds' % (time.time()-time0))


Elapsed time=125 seconds

In [19]:
# Make a new DataFrame with just the last two variables (temp & salt)
df2=df.ix[:,-2:]

In [20]:
# Plotting time series in Pandas is easy
df2.plot(figsize=(12,4));



In [21]:
# take a look at the first few values
df2.head(5)


Out[21]:
temp[unit="Celsius"] salt[unit=""]
date
2012-03-01 00:30:00 6.408102 16.761736
2012-03-01 01:00:00 6.365255 16.760641
2012-03-01 01:30:00 6.325319 16.757613
2012-03-01 02:00:00 6.277970 16.759291
2012-03-01 02:30:00 6.226942 16.763459

In [22]:
# calculate the maximum
df2.max()


Out[22]:
temp[unit="Celsius"]    22.212173
salt[unit=""]           29.180200
dtype: float64

In [22]: