This example demonstrates connecting to a NIWA Sensor Observations Service from a Python Script.
Use the stations WFS feed to identify candidate hydro stations in the area of interest and then obtain some timeseries data.
OWSLib is a Python package for client programming with Open Geospatial Consortium (OGC) web service (hence OWS) interface standards, and their related content models.
http://geopython.github.io/OWSLib/
easy_install OWSLib
see http://nbviewer.ipython.org/github/peterataylor/OWSLib-SOS-example/blob/master/owslib-sos-notebook.ipynb for more examples
# NIWA WMS and WFS Server endpoints are at
niwa_ows = "http://gs.niwa.co.nz/geoserver/stations/ows"
niwa_hydro_sos = "http://hydro-sos.niwa.co.nz/?datasource=0"
%matplotlib inline
from owslib.wfs import WebFeatureService
from owslib.sos import SensorObservationService
from owslib.swe.observation.sos200 import SosCapabilitiesReader
x = SosCapabilitiesReader(url=niwa_hydro_sos)
print x.capabilities_url(niwa_hydro_sos)
# stations_wfs = WebFeatureService(niwa_ows)
sos = SensorObservationService(niwa_hydro_sos,version='2.0.0')
# print sos['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2584']
http://hydro-sos.niwa.co.nz/?datasource=0&service=SOS&request=GetCapabilities&acceptversions=2.0.0
id = sos.identification
print "Identification:"
print "\tTitle:\t", id.title
print "\tVersion:\t", id.version
print "\tAbstract:\t", id.abstract
print "\tKeywords:"
for key in id.keywords: print "\t\t", key
print "\tFees:\t", id.fees
print "\tAccess Constraints:\t", id.accessconstraints
# list operations
print "Operations:"
for op in sos.operations: print "\t", op.name
# , op.formatOptions, op.methods
print "Service Provider:"
p = sos.provider
print "\tName:\t", p.name
print "\tURL:\t", p.url
print "Contact:"
print "\tContact:\t", p.contact.name, p.contact.position
Identification: Title: KISTERS KiWIS SOS2 Version: 2.0.0 Abstract: None Keywords: Fees: None Access Constraints: maxNumberOfReturnedValues=250000 Operations: DescribeSensor GetCapabilities GetDataAvailability GetFeatureOfInterest GetObservation Service Provider: Name: NIWA URL: http://www.niwa.co.nz Contact: Contact: Jochen Schmidt Chief Scientist - Environmental Information
Note that KiWIS is currently reporting an incorrect URL for these ops
# from owslib.ows import OperationsMetadata
getcap = sos.get_operation_by_name('GetCapabilities')
getcap.methods
[{'constraints': [Constraint: Content-Type - ['application/x-kvp']], 'type': 'Get', 'url': 'http://hydro-sos.niwa.co.nz?datasource=0'}, {'constraints': [Constraint: Content-Type - ['application/soap+xml', 'application/xml']], 'type': 'Post', 'url': 'http://hydro-sos.niwa.co.nz?datasource=0'}]
descsen = sos.get_operation_by_name('DescribeSensor')
descsen.methods
[{'constraints': [Constraint: Content-Type - ['application/x-kvp']], 'type': 'Get', 'url': 'http://hydro-sos.niwa.co.nz?datasource=0'}, {'constraints': [Constraint: Content-Type - ['application/soap+xml', 'application/xml']], 'type': 'Post', 'url': 'http://hydro-sos.niwa.co.nz?datasource=0'}]
go = sos.get_operation_by_name('GetObservation')
go.methods
[{'constraints': [Constraint: Content-Type - ['application/x-kvp']], 'type': 'Get', 'url': 'http://hydro-sos.niwa.co.nz?datasource=0'}, {'constraints': [Constraint: Content-Type - ['application/soap+xml', 'application/xml']], 'type': 'Post', 'url': 'http://hydro-sos.niwa.co.nz?datasource=0'}]
# Listing available stations...
stations = list(sos.contents)
for s in stations[0:10]: print sos.contents[s]
Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2584, name: Ratings QC 2584 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 1162, name: Ratings QC 1162 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 4572, name: River level QC 4572 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 4278, name: River level QC 4278 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 864, name: River level QC 864 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2893, name: Ratings QC 2893 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 4272, name: River level QC 4272 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 4386, name: River level QC 4386 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2518, name: Ratings QC 2518 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 862, name: Ratings QC 862
def printstation(s) :
print "id\t", s.id
print "name\t", s.name
print "description\t", s.description
print "BBOX\t", s.bbox
print "BBOX SRS\t", s.bbox_srs
print "procedures\t", s.procedures
# print s.procedure_description_formats
print "Observed Properties\t", s.observed_properties
print "Features of Interest\t", s.features_of_interest
# print s.response_formats
# print s.observation_models
# print "SRS\t", s.srs
# print "begin position\t", s.being_position
print
for id in list(sos.contents)[1:10]: printstation(sos.contents[id])
id http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 1162 name Ratings QC 1162 description Offering for timeseries type Ratings QC 1162 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 1162'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 4572 name River level QC 4572 description Offering for timeseries type River level QC 4572 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 4572'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 4278 name River level QC 4278 description Offering for timeseries type River level QC 4278 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 4278'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 864 name River level QC 864 description Offering for timeseries type River level QC 864 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 864'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2893 name Ratings QC 2893 description Offering for timeseries type Ratings QC 2893 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2893'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 4272 name River level QC 4272 description Offering for timeseries type River level QC 4272 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 4272'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 4386 name River level QC 4386 description Offering for timeseries type River level QC 4386 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 4386'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2518 name Ratings QC 2518 description Offering for timeseries type Ratings QC 2518 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2518'] Observed Properties [] Features of Interest [] id http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 862 name Ratings QC 862 description Offering for timeseries type Ratings QC 862 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 862'] Observed Properties [] Features of Interest []
In our cheap and cheerful system we have a one to one relationship between the station and the offering
for o in sos.offerings[1:10]:
print o
Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 3696, name: River level QC 3696 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 4384, name: Ratings QC 4384 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Daily staff gauge readings QC 9687, name: Daily staff gauge readings QC 9687 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 4386, name: River level QC 4386 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2152, name: Ratings QC 2152 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 2154, name: River level QC 2154 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Rainfall QC 8190, name: Rainfall QC 8190 Offering id: http://hydro-sos.niwa.co.nz/tstypes/River level QC 2757, name: River level QC 2757 Offering id: http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2755, name: Ratings QC 2755
def printoffering(s) :
print "id\t", s.id
print "name\t", s.name
print "description\t", s.description
print "BBOX\t", s.bbox
print "BBOX SRS\t", s.bbox_srs
print "procedures\t", s.procedures
# print s.procedure_description_formats
print "Observed Properties\t", s.observed_properties
print "Features of Interest\t", s.features_of_interest
print "Response formats\t", s.response_formats
# print s.observation_models
# print "SRS\t", s.srs
# print "begin position\t", s.being_position
print
for o in sos.offerings[1:10]: printoffering(o)
id http://hydro-sos.niwa.co.nz/tstypes/River level QC 3696 name River level QC 3696 description Offering for timeseries type River level QC 3696 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 3696'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 4384 name Ratings QC 4384 description Offering for timeseries type Ratings QC 4384 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 4384'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/Daily staff gauge readings QC 9687 name Daily staff gauge readings QC 9687 description Offering for timeseries type Daily staff gauge readings QC 9687 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Daily staff gauge readings QC 9687'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 4386 name River level QC 4386 description Offering for timeseries type River level QC 4386 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 4386'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2152 name Ratings QC 2152 description Offering for timeseries type Ratings QC 2152 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2152'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 2154 name River level QC 2154 description Offering for timeseries type River level QC 2154 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 2154'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/Rainfall QC 8190 name Rainfall QC 8190 description Offering for timeseries type Rainfall QC 8190 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Rainfall QC 8190'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/River level QC 2757 name River level QC 2757 description Offering for timeseries type River level QC 2757 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/River level QC 2757'] Observed Properties [] Features of Interest [] Response formats [] id http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2755 name Ratings QC 2755 description Offering for timeseries type Ratings QC 2755 BBOX (-176.6631, -46.6519, 178.2036, -34.9196) BBOX SRS urn:ogc:def:crs:EPSG::4326 procedures ['http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2755'] Observed Properties [] Features of Interest [] Response formats []
from owslib.fes import FilterCapabilities
from owslib.crs import Crs
from datetime import datetime
This is what a valid request might look like
http://hydro-sos.niwa.co.nz/?datasource=0
&version=2.0
&service=SOS
&request=getobservation
&featureOfInterest=http%3A%2F%2Fhydro-sos.niwa.co.nz%2Fstations%2F819
&observedProperty=http%3A%2F%2Fhydro-sos.niwa.co.nz%2Fparameters%2FStage%2FFlow+rating+QC+2152
&temporalFilter=om:phenomenonTime,2014-01-29T12:00:00.000+13:00/2014-07-29T00:00:00.000+00:00
op = 'http://hydro-sos.niwa.co.nz/parameters/Stage/Flow rating QC 2152'
off = 'http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2152'
proc = 'http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2152'
print sos.get_operation_by_name('GetObservation').methods[0]['url']
# for KiWIS methods are 0 and 1 for Get and Post as the methods is an array not a map.
response = sos.get_observation(offerings=[off], observedProperties=[op], method=0)
http://hydro-sos.niwa.co.nz?datasource=0
response
'<sos:GetObservationResponse xsi:schemaLocation="http://www.opengis.net/sos/2.0 http://schemas.opengis.net/sos/2.0/sos.xsd http://www.opengis.net/waterml/2.0 http://schemas.opengis.net/waterml/2.0/waterml2.xsd" xmlns:wml2="http://www.opengis.net/waterml/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:om="http://www.opengis.net/om/2.0" xmlns:sos="http://www.opengis.net/sos/2.0" xmlns:sa="http://www.opengis.net/sampling/2.0" xmlns:sams="http://www.opengis.net/samplingSpatial/2.0" xmlns:xlink="http://www.w3.org/1999/xlink"><extension xmlns="http://www.opengis.net/swes/2.0"><wml2:SOSProfileExtension><wml2:metadata><wml2:DocumentMetadata gml:id="Ki.DocMD.1"><wml2:generationDate>2014-12-04T20:34:54.621+00:00</wml2:generationDate><wml2:generationSystem>KISTERS KiWIS</wml2:generationSystem></wml2:DocumentMetadata></wml2:metadata></wml2:SOSProfileExtension></extension><sos:observationData><om:OM_Observation gml:id="Ki.OM_Obs.1"><om:phenomenonTime><gml:TimePeriod gml:id="Ki.ObsTime.1"><gml:beginPosition>2014-07-29T00:00:00.000+00:00</gml:beginPosition><gml:endPosition>2014-07-29T00:00:00.000+00:00</gml:endPosition></gml:TimePeriod></om:phenomenonTime><om:resultTime><gml:TimeInstant gml:id="Ki.resTime.1"><gml:timePosition>2014-07-29T00:00:00.000+00:00</gml:timePosition></gml:TimeInstant></om:resultTime><om:procedure xlink:href="http://hydro-sos.niwa.co.nz/tstypes/Ratings QC 2152" xlink:title="Ratings QC 2152"/><om:observedProperty xlink:href="http://hydro-sos.niwa.co.nz/parameters/Stage/Flow rating QC 2152" xlink:title="Stage/Flow rating QC 2152"/><om:featureOfInterest xlink:href="http://hydro-sos.niwa.co.nz/stations/819" xlink:title="Makarau at Coles"/><om:result><wml2:MeasurementTimeseries gml:id="Ki.Ts.2152"><wml2:defaultPointMetadata><wml2:DefaultTVPMeasurementMetadata><wml2:uom code="L/s"/></wml2:DefaultTVPMeasurementMetadata></wml2:defaultPointMetadata><wml2:point><wml2:MeasurementTVP><wml2:time>2014-07-29T00:00:00.000+00:00</wml2:time><wml2:value>832</wml2:value><wml2:metadata><wml2:TVPMeasurementMetadata><wml2:qualifier xlink:href="http://hydro-sos.niwa.co.nz/qualifiers/0" xlink:title="0"/></wml2:TVPMeasurementMetadata></wml2:metadata></wml2:MeasurementTVP></wml2:point></wml2:MeasurementTimeseries></om:result></om:OM_Observation></sos:observationData></sos:GetObservationResponse>'
Two months of data takes a couple of seconds to fetch
# pick a start & stop time
# om:phenomenonTime,2014-01-29T12:00:00.000+13:00/2014-07-29T00:00:00.000+00:00
start = '2014-06-01T00:00:00.000+13:00'
stop = '2014-08-01T00:00:00.000+13:00'
response = sos.get_observation(offerings=[off], observedProperties=[op], method=0,
eventTime='om:phenomenonTime,%s/%s' % (start,stop))
response[0:1400]
'<sos:GetObservationResponse xsi:schemaLocation="http://www.opengis.net/sos/2.0 http://schemas.opengis.net/sos/2.0/sos.xsd http://www.opengis.net/waterml/2.0 http://schemas.opengis.net/waterml/2.0/waterml2.xsd" xmlns:wml2="http://www.opengis.net/waterml/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:om="http://www.opengis.net/om/2.0" xmlns:sos="http://www.opengis.net/sos/2.0" xmlns:sa="http://www.opengis.net/sampling/2.0" xmlns:sams="http://www.opengis.net/samplingSpatial/2.0" xmlns:xlink="http://www.w3.org/1999/xlink"><extension xmlns="http://www.opengis.net/swes/2.0"><wml2:SOSProfileExtension><wml2:metadata><wml2:DocumentMetadata gml:id="Ki.DocMD.1"><wml2:generationDate>2014-12-04T20:34:55.671+00:00</wml2:generationDate><wml2:generationSystem>KISTERS KiWIS</wml2:generationSystem></wml2:DocumentMetadata></wml2:metadata></wml2:SOSProfileExtension></extension><sos:observationData><om:OM_Observation gml:id="Ki.OM_Obs.1"><om:phenomenonTime><gml:TimePeriod gml:id="Ki.ObsTime.1"><gml:beginPosition>2014-05-31T11:00:00.000+00:00</gml:beginPosition><gml:endPosition>2014-07-29T00:00:00.000+00:00</gml:endPosition></gml:TimePeriod></om:phenomenonTime><om:resultTime><gml:TimeInstant gml:id="Ki.resTime.1"><gml:timePosition>2014-07-29T00:00:00.000+00:00</gml:timePosition></gml:TimeInstant></om:resultTime><om:procedure xlink:href="h'
Currently the OWSLib doesn't have a built in parser for O&M Results. So we have to process the XML ourselves.
This GML has fairly messy namespace definitions so we want to build the namespace map from the header element. ElementTree doesn't do this that well so we will use lxml
from lxml import etree
root = etree.fromstring(response)
nsmap = root.nsmap
print nsmap
{'om': 'http://www.opengis.net/om/2.0', 'gml': 'http://www.opengis.net/gml/3.2', 'wml2': 'http://www.opengis.net/waterml/2.0', 'xlink': 'http://www.w3.org/1999/xlink', 'sos': 'http://www.opengis.net/sos/2.0', 'sams': 'http://www.opengis.net/samplingSpatial/2.0', 'sa': 'http://www.opengis.net/sampling/2.0', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
om_obs = root.findall('sos:observationData/om:OM_Observation', root.nsmap)
print "Result %s properties." % (len(om_obs))
# print om_obs
o = om_obs[0]
pt = o.find("om:phenomenonTime/gml:TimePeriod", root.nsmap)
print "PhenomenonTime: %s to %s" % (pt.find("gml:beginPosition", root.nsmap).text,
pt.find("gml:endPosition", root.nsmap).text)
print "Feature of Interest: ", o.find("om:featureOfInterest", root.nsmap).attrib
print "ObservedProperty: ", o.find("om:observedProperty", root.nsmap).attrib
ObservedProperty = o.find("om:observedProperty", root.nsmap).attrib['{http://www.w3.org/1999/xlink}title']
Result 1 properties. PhenomenonTime: 2014-05-31T11:00:00.000+00:00 to 2014-07-29T00:00:00.000+00:00 Feature of Interest: {'{http://www.w3.org/1999/xlink}href': 'http://hydro-sos.niwa.co.nz/stations/819', '{http://www.w3.org/1999/xlink}title': 'Makarau at Coles'} ObservedProperty: {'{http://www.w3.org/1999/xlink}href': 'http://hydro-sos.niwa.co.nz/parameters/Stage/Flow rating QC 2152', '{http://www.w3.org/1999/xlink}title': 'Stage/Flow rating QC 2152'}
om_points = om_obs[0].findall('om:result/wml2:MeasurementTimeseries/wml2:point', root.nsmap)
print "Result %s observations." % (len(om_points))
dates = [ p.find('wml2:MeasurementTVP/wml2:time', root.nsmap).text for p in om_points ]
values = [ float(p.find('wml2:MeasurementTVP/wml2:value', root.nsmap).text) for p in om_points ]
ts = pd.Series( values, pd.to_datetime(dates))
Result 5617 observations.
ts.plot(figsize=(20,6))
<matplotlib.axes._subplots.AxesSubplot at 0x104fa9950>
measure = 'Rainfall QC 6520'
op = 'http://hydro-sos.niwa.co.nz/parameters/%s' % measure
off = 'http://hydro-sos.niwa.co.nz/tstypes/%s' % measure
start = '1995-08-15T00:00:00.000+00:00'
stop = '1996-02-15T00:00:00.000+00:00'
response = sos.get_observation(offerings=[off], observedProperties=[op], method=0,
eventTime='om:phenomenonTime,%s/%s' % (start,stop))
root = etree.fromstring(response)
om_obs = root.findall('sos:observationData/om:OM_Observation', root.nsmap)
print "Result %s properties." % (len(om_obs))
# print om_obs
o = om_obs[0]
ObservedProperty = o.find("om:observedProperty", root.nsmap).attrib['{http://www.w3.org/1999/xlink}title']
FeatureOfInterest = o.find("om:featureOfInterest", root.nsmap).attrib['{http://www.w3.org/1999/xlink}title']
print FeatureOfInterest, ObservedProperty
Result 1 properties. Waiho at Almer Hut Rainfall QC 6520
om_points = om_obs[0].findall('om:result/wml2:MeasurementTimeseries/wml2:point', root.nsmap)
print "Result %s observations." % (len(om_points))
import pandas as pd
dates = [ p.find('wml2:MeasurementTVP/wml2:time', root.nsmap).text for p in om_points ]
values = [ float(p.find('wml2:MeasurementTVP/wml2:value', root.nsmap).text) for p in om_points ]
ts = pd.Series( values, pd.to_datetime(dates))
Result 5864 observations.
daily = ts.resample('D', how='sum')
daily.head()
daily.plot(style='go', figsize=(20,6), label='Rainfall', title=FeatureOfInterest + ' - ' + ObservedProperty)
<matplotlib.axes._subplots.AxesSubplot at 0x125c8b150>
measure = 'Rainfall QC 6518'
op = 'http://hydro-sos.niwa.co.nz/parameters/%s' % measure
off = 'http://hydro-sos.niwa.co.nz/tstypes/%s' % measure
start = '1983-12-12T00:00:00.000+00:00'
stop = '2014-12-04T00:00:00.000+00:00'
response = sos.get_observation(offerings=[off], observedProperties=[op], method=0,
eventTime='om:phenomenonTime,%s/%s' % (start,stop))
root = etree.fromstring(response)
om_obs = root.findall('sos:observationData/om:OM_Observation', root.nsmap)
print "Result %s properties." % (len(om_obs))
# print om_obs
o = om_obs[0]
ObservedProperty = o.find("om:observedProperty", root.nsmap).attrib['{http://www.w3.org/1999/xlink}title']
FeatureOfInterest = o.find("om:featureOfInterest", root.nsmap).attrib['{http://www.w3.org/1999/xlink}title']
print FeatureOfInterest, ObservedProperty
Result 1 properties. Waiho at Douglas Hut Rainfall QC 6518
om_points = om_obs[0].findall('om:result/wml2:MeasurementTimeseries/wml2:point', root.nsmap)
print "Result %s observations." % (len(om_points))
import pandas as pd
dates = [ p.find('wml2:MeasurementTVP/wml2:time', root.nsmap).text for p in om_points ]
values = [ float(p.find('wml2:MeasurementTVP/wml2:value', root.nsmap).text) for p in om_points ]
ts = pd.Series( values, pd.to_datetime(dates))
Result 305721 observations.
ts.plot(style='g', figsize=(20,6), label='Rainfall', title=FeatureOfInterest + ' - ' + ObservedProperty)
<matplotlib.axes._subplots.AxesSubplot at 0x10e524ad0>
# resample
monthlys = ts.resample('A', how='sum')
monthlys.head()
1983-12-31 715.347222 1984-12-31 16595.852187 1985-12-31 12817.512548 1986-12-31 9099.869011 1987-12-31 3587.238073 Freq: A-DEC, dtype: float64
monthlys.plot(figsize=(20, 8), kind='bar', title="Total Annual Rainfall")
<matplotlib.axes._subplots.AxesSubplot at 0x125c1bb50>