In [1]:
# Make it so notebook knows how to access ooitk from the notebook dir
import sys
import os
sys.path.append(os.path.dirname(os.getcwd()))

In [2]:
# Make a new session to localhost and point it to the service gateway
from ooitk.session import Session
session = Session('localhost', 5000)

Let's get the code


In [9]:
# Make a new catalog instance
from ooitk.data_catalog import DataProductCatalog
catalog = DataProductCatalog(session)

In [10]:
# Find all the data products that have 'ctd' in the name
relevant_data_products = catalog.find_data_product('ctd')

In [11]:
# Fetch a DAP dataset for the first one
dap_dataset = catalog.download(relevant_data_products[0]['_id'])

In [12]:
# Get time, time is a dimension variable so it's really simple to get
time = dap_dataset['time'][:]
# Get temp, temp is a gridded variable so it needs to baccessed like this
temp = dap_dataset['temp']['temp'][:][0]

In [13]:
# Plot it in pyplot
plt.plot(time,temp)


Out[13]:
[<matplotlib.lines.Line2D at 0x1089af150>]

In [18]:
erddap_url = 'http://erddap-test.oceanobservatories.org:8080/erddap/tabledap/e1fd3967aa55475e8338fc36a6493f5c'
nc_url = erddap_url + '.nc?&orderBy%28%22time%22%29'
from tempfile import gettempdir
import os
import requests

def scrape(url):
    tmpfile = os.path.join(gettempdir(), 'cache.nc')
    r = requests.get(url)
    chunk_size = 4096
    with open(tmpfile, 'wb') as fd:
        for chunk in r.iter_content(chunk_size):
            fd.write(chunk)
    return tmpfile

In [19]:
fname = scrape(nc_url)

In [21]:
from netCDF4 import Dataset
nc = Dataset(fname)

In [25]:
t = nc.variables['time'][:]
temp = nc.variables['sci_water_temp'][:]

In [24]:
nc.variables


Out[24]:
OrderedDict([(u'driver_timestamp', <netCDF4.Variable object at 0x109e017a0>), (u'internal_timestamp', <netCDF4.Variable object at 0x109e01830>), (u'm_present_secs_into_mission', <netCDF4.Variable object at 0x109e01b90>), (u'm_present_time', <netCDF4.Variable object at 0x109e01c20>), (u'preferred_timestamp', <netCDF4.Variable object at 0x109e01cb0>), (u'sci_m_present_secs_into_mission', <netCDF4.Variable object at 0x109e01d40>), (u'sci_m_present_time', <netCDF4.Variable object at 0x109e01dd0>), (u'sci_water_cond', <netCDF4.Variable object at 0x109e01e60>), (u'sci_water_pressure', <netCDF4.Variable object at 0x109e01ef0>), (u'sci_water_temp', <netCDF4.Variable object at 0x109e01f80>), (u'time', <netCDF4.Variable object at 0x109e2f050>)])

In [ ]: