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 [1]:
import pandas as pd
import time

In [2]:
var = 'temp,salt'
lat = 43.7148
lon = -69.3578
#start = '2012-05-01T00:00:00Z'
start = '2014-08-24T00:00:00Z'
stop =  '2014-08-27T00:00:00Z'
level = 2

In [3]:
url='http://geoport.whoi.edu/thredds/ncss/grid/coawst_4/use/fmrc/coawst_4_use_best.ncd?\
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/coawst_4/use/fmrc/coawst_4_use_best.ncd?var=temp,salt&latitude=43.714800&longitude=-69.357800&time_start=2014-08-24T00:00:00Z&time_end=2014-08-27T00:00:00Z&vertCoord=2&accept=csv

In [4]:
# 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=10 seconds

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

In [19]:
# Plotting time series in Pandas is easy
df.ix[:,-1].plot(figsize(12,4),legend=True)
df.ix[:,-2].plot(secondary_y=True,legend=True);



In [8]:
# take a look at the first few values
df.ix[:,-3:].head(5)


Out[8]:
vertCoord[unit="meter"] temp[unit="Celsius"] salt[unit="0.001"]
date
2014-08-24 00:00:00 -0.070834 17.383890 31.860044
2014-08-24 01:00:00 0.779609 17.352831 31.862576
2014-08-24 02:00:00 1.361506 17.335934 31.866318
2014-08-24 03:00:00 1.573485 17.252645 31.871664
2014-08-24 04:00:00 1.323126 17.128550 31.878304

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


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

In [8]: