Accessing ncSOS with OWSLib


In [17]:
from owslib.sos import SensorObservationService
import pdb
from owslib.etree import etree
import pandas as pd
import datetime as dt

In [18]:
# ncSOS endpoint with time series data
#url='http://geoport-dev.whoi.edu/thredds/sos/usgs/data2/notebook/1211-A1H.cdf'
url='http://www.neracoos.org:8180/thredds/sos/UMO/DSG/SOS/A01/Accelerometer/HistoricRealtime/Agg.ncml'
sos = SensorObservationService(url)
contents = sos.contents

In [19]:
sos.contents


Out[19]:
{'network-all': <owslib.swe.observation.sos100.SosObservationOffering at 0x7fccbf1d9650>,
 'urn_ioos_station_edu.maine_A01': <owslib.swe.observation.sos100.SosObservationOffering at 0x7fccbf1d9390>}

In [20]:
off = sos.offerings[1]
off.name


Out[20]:
'urn:ioos:station:edu.maine:A01'

In [21]:
off.response_formats


Out[21]:
['text/xml;schema="om/1.0.0"',
 'text/xml;schema="om/1.0.0/profiles/ioos_sos/1.0"']

In [22]:
off.observed_properties


Out[22]:
['urn:ioos:sensor:edu.maine:A01:significant_wave_height',
 'urn:ioos:sensor:edu.maine:A01:significant_wave_height_qc',
 'urn:ioos:sensor:edu.maine:A01:dominant_wave_period',
 'urn:ioos:sensor:edu.maine:A01:dominant_wave_period_qc']

In [23]:
#pdb.set_trace()
response = sos.get_observation(offerings=['A01'],
                                 responseFormat='text/xml;schema="om/1.0.0"',
                                 observedProperties=['significant_wave_height'])

In [24]:
print response[0:1400]


<?xml version="1.0" encoding="UTF-8"?>
<om:ObservationCollection xmlns:om="http://www.opengis.net/om/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gml="http://www.opengis.net/gml" xmlns:swe="http://www.opengis.net/swe/1.0" xsi:schemaLocation="http://www.opengis.net/om/1.0 http://schemas.opengis.net/om/1.0.0/observation.xsd">
  <gml:description>Ocean observation data from the Northeastern Regional Association of Coastal &amp;amp; Ocean Observing Systems (NERACOOS). The NERACOOS region includes the northeast United States and Canadian Maritime provinces, as part of the United States Integrated Ocean Observing System (IOOS).  These data are served by Unidata's Thematic Realtime Environmental Distributed Data Services (THREDDS) Data Server (TDS) in a variety of interoperable data services and output formats.</gml:description>
  <gml:name>NERACOOS Gulf of Maine Ocean Array: Realtime Buoy Observations: A01 Massachusetts Bay: A01 ACCELEROMETER Massachusetts Bay</gml:name>
  <gml:boundedBy>
    <gml:Envelope srsName="http://www.opengis.net/def/crs/EPSG/0/4326">
      <!-- overwrite these with your actual offering ROI -->
      <gml:lowerCorner>42.51825332641602 -70.5660629272461</gml:lowerCorner>
      <gml:upperCorner>42.51825332641602 -70.5660629272461</gml:upperCorner>
    </gml:Envelope>
  </gml:boundedBy>
  <

In [25]:
def parse_om_xml(response):
    # extract data and time from OM-XML response
    root = etree.fromstring(response)
    # root.findall(".//{%(om)s}Observation" % root.nsmap )
    values = root.find(".//{%(swe)s}values" % root.nsmap )
    date_value = array( [ (dt.datetime.strptime(d,"%Y-%m-%dT%H:%M:%SZ"),float(v))
            for d,v in [l.split(',') for l in values.text.split()]] )
    time = date_value[:,0]
    data = date_value[:,1]
    return data,time

In [26]:
data, time = parse_om_xml(response)

In [27]:
ts = pd.Series(data,index=time)

In [28]:
ts.plot(figsize(12,4));



In [29]:
# Try adding a time range to the getobs request:

start = '2010-03-01T00:00:00Z'
stop = '2010-04-15T00:00:00Z'

response = sos.get_observation(offerings=['A01'],
                                 responseFormat='text/xml;schema="om/1.0.0"',
                                 observedProperties=['significant_wave_height'],
                                 eventTime='%s/%s' % (start,stop))

In [30]:
data, time = parse_om_xml(response)

In [31]:
ts = pd.Series(data,index=time)

In [32]:
ts.plot(figsize(12,4));