Accessing ncSOS with OWSLib


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

In [9]:
# Caricoos
# buoy data (single current meter)
url='http://dm2.caricoos.org/thredds/sos/buoys/Historical/PR1/PR1.sbe37.historical.1m.nc'
cari = SensorObservationService(url)
contents = cari.contents

In [10]:
cari.contents


Out[10]:
{'network-all': <owslib.swe.observation.sos100.SosObservationOffering at 0x3a79dd0>,
 'urn_ioos_station_ncsos_Grid0': <owslib.swe.observation.sos100.SosObservationOffering at 0x3a79f10>}

In [11]:
off = cari.offerings[1]
off.name


Out[11]:
'urn:ioos:station:ncsos:Grid0'

In [12]:
off.response_formats


Out[12]:
['text/xml;schema="om/1.0.0"']

In [13]:
off.observed_properties


Out[13]:
['urn:ioos:sensor:ncsos:Grid0:conductivity',
 'urn:ioos:sensor:ncsos:Grid0:conductivity_qc',
 'urn:ioos:sensor:ncsos:Grid0:temperature',
 'urn:ioos:sensor:ncsos:Grid0:temperature_qc',
 'urn:ioos:sensor:ncsos:Grid0:salinity',
 'urn:ioos:sensor:ncsos:Grid0:salinity_qc',
 'urn:ioos:sensor:ncsos:Grid0:sigma_t',
 'urn:ioos:sensor:ncsos:Grid0:sigma_t_qc']

In [7]:
# the get observation request below works.  How can we recreate this using OWSLib?
# http://geoport-dev.whoi.edu/thredds/sos/usgs/data2/notebook/1211-A1H.cdf?service=SOS&version=1.0.0&request=GetObservation&responseFormat=text%2Fxml%3Bsubtype%3D%22om%2F1.0.0%22&offering=1211-A1H&observedProperty=u_1205&procedure=urn:ioos:station:gov.usgs:1211-A1H

In [15]:
#pdb.set_trace()
response = cari.get_observation(offerings=['Grid0'],
                                 responseFormat='text/xml;subtype="om/1.0.0"',
                                 observedProperties=['temperature'],
                                 procedure='urn:ioos:sensor:ncsos:Grid0')


---------------------------------------------------------------------------
ExceptionReport                           Traceback (most recent call last)
<ipython-input-15-d0a2db1acdea> in <module>()
      3                                  responseFormat='text/xml;subtype="om/1.0.0"',
      4                                  observedProperties=['temperature'],
----> 5                                  procedure='urn:ioos:sensor:ncsos:Grid0')

/home/local/python27_epd/lib/python2.7/site-packages/owslib/swe/observation/sos100.pyc in get_observation(self, responseFormat, offerings, observedProperties, eventTime, method, **kwargs)
    168             tr = etree.fromstring(response)
    169             if tr.tag == nspath_eval("ows:ExceptionReport", namespaces):
--> 170                 raise ows.ExceptionReport(tr)
    171             else:
    172                 return response

ExceptionReport: 'No longitude point specified'

In [9]:
print response[0:4000]


<?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>1211-A1H       start time = 28 Dec 1976  16:29:59  cycles = 250
               stop  time =  8 Jan 1977  01:29:59  # days = 10
Expt. = 'USGS'  sampling interval = 60.00 minutes
Lat =  40.901112
Lon = -69.176666  File created:  22:50 JUN 29,'83
depth = 69m  Mag.var = -16.000000
#  Variable     Units        Codes Depth   Inst.   Minimum   Maximum
-- ------------ ------------ ----- ------- ------ --------- ---------
 1 TIME         SECONDS      T     
 2 EAST  COMP   CMS/SEC      R       69.00 V-0069   -44.012    49.297
 3 NORTH COMP   CMS/SEC      R       69.00 V-0069   -50.654    72.174
 4 VDIR.1       DEG          R       69.00 V-0069     1.868   359.745
 5 VSPD.1       CMS/SEC      R       69.00 V-0069     1.416    79.172
 6 INT_ROTOR    CMS/SEC      R       69.00 V-0069     5.847    79.927
---------------------------------------------------------------------
 Comments:                                                      REDUCE
 GREAT SOUTH CHANNEL; ARGO MERCHANT SITE.  BEFORE TIPPED.
 VARIABLES LISTED IN ORDER SHOWN ABOVE</gml:description>
  <gml:name>Current meter archive data - Butman et al, Oct.  1992</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>40.9011116027832 -69.17666625976562</gml:lowerCorner>
      <gml:upperCorner>40.9011116027832 -69.17666625976562</gml:upperCorner>
    </gml:Envelope>
  </gml:boundedBy>
  <om:member>
    <om:Observation>
      <gml:description />
      <gml:name />
      <gml:boundedBy>
        <gml:Envelope srsName="http://www.opengis.net/def/crs/EPSG/0/4326">
          <gml:lowerCorner>40.9011116027832 -69.17666625976562</gml:lowerCorner>
          <gml:upperCorner>40.9011116027832 -69.17666625976562</gml:upperCorner>
        </gml:Envelope>
      </gml:boundedBy>
      <om:samplingTime>
        <gml:TimePeriod gml:id="DATA_TIME">
          <gml:beginPosition>1976-12-28T21:29:59Z</gml:beginPosition>
          <gml:endPosition>1977-01-08T06:29:59Z</gml:endPosition>
        </gml:TimePeriod>
      </om:samplingTime>
      <om:procedure xlink:href="urn:ioos:station:gov.usgs:1211-A1H" />
      <om:observedProperty xlink:href="u_1205" />
      <om:featureOfInterest xlink:href="urn:ioos:station:gov.usgs:1211-A1H" />
      <om:result>
        <om:DataArray>
          <swe:elementCount>
            <swe:Count>
              <swe:value>1</swe:value>
            </swe:Count>
          </swe:elementCount>
          <swe:elementType name="SimpleDataArray">
            <swe:DataRecord>
              <swe:field name="time">
                <swe:Time definition="http://www.opengis.net/def/property/OGC/0/SamplingTime">
                  <swe:uom xlink:href="http://www.opengis.net/def/uom/ISO-8601/0/Gregorian" />
                </swe:Time>
              </swe:field>
              <swe:field name="u_1205">
                <swe:Quantity definition="http://mmisw.org/ont/cf/parameter/u_1205">
                  <swe:uom code="cm/s" />
                </swe:Quantity>
              </swe:field>
            </swe:DataRecord>
          </swe:elementType>
          <swe:encoding>
            <swe:TextBlock blockSeparator=" " decimalSeparator="." tokenSeparator="," />
          </swe:encoding>
          <swe:values>1976-12-28T21:29:59Z,-7.53701 1976-12-28T22:29:59Z,-2.06346 1976-12-28T23:29:59Z,5.02748 1976-12-29T00:29:59Z,9.32351 1976-12-29T01:29:59Z,16.191 1976-12-29T02:29:59Z,18.452 1976-12-29T03:29:59Z,19.452

In [10]:
# usgs woods hole ADCP data
# url='http://geoport-dev.whoi.edu/thredds/sos/usgs/data2/notebook/9111aqd-a.nc'
# adcp = SensorObservationService(url)

In [18]:
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 [19]:
data, time = parse_om_xml(response)

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

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


Out[17]:
<matplotlib.axes.AxesSubplot at 0x43b0710>

In [ ]:
''' this doesn't work
# Try adding a time range to the getobs request:

start = '1976-12-302T00:00:00Z'
stop = '1977-01-07T00:00:00Z'

response = usgs.get_observation(offerings=['1211-A1H'],
                                 responseFormat='text/xml;subtype="om/1.0.0"',
                                 observedProperties=['u_1205'],
                                 procedure='urn:ioos:station:gov.usgs:1211-A1H',
                                 eventTime='%s/%s' % (start,stop)
''';

In [ ]: