Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emiliom/7312527 to your computer and use it in GitHub Desktop.
Save emiliom/7312527 to your computer and use it in GitHub Desktop.
#IOOS #SOS #OWSLib #MMI Access the AOOS SOS GetCapabilities, issue a sample DescribeSensor request, then parse parts of the response. Includes examples of testing the validity of controlled vocabulary terms vs MMI (using the approach described on the IPython notebook http://nbviewer.ipython.org/6514804). This SOS test complements the one for Get…
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "AOOS_SOS_SensorML_owslib_AlaskaNDBCBuoy_1"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "# DescribeSensor/SensorML client access tests for IOOS SOS Milestone 1, using the AOOS 52North SOS endpoint\n\nAccess the AOOS SOS GetCapabilities, issue a sample DescribeSensor request, then parse parts of the response. Includes examples of testing the validity of controlled vocabulary terms vs MMI (using the approach [described on this IPython notebook](http://nbviewer.ipython.org/6514804)). This SOS test [complements the one for GetObservations](http://nbviewer.ipython.org/7240742)). 2013/11/4"
},
{
"cell_type": "code",
"collapsed": false,
"input": "from datetime import datetime\nimport urllib2\nimport json\nimport requests\nfrom owslib.sos import SensorObservationService\nfrom owslib.swe.sensor.sml import SensorML, EventMetadata",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 60
},
{
"cell_type": "code",
"collapsed": false,
"input": "# AOOS SOS Tests (GetCap extracted from the AOOS 52N SOS Test Client)\naoossos_gc_url = \"http://sos.aoos.org/sos/sos/kvp?service=SOS&request=GetCapabilities&AcceptVersions=1.0.0\"\naoosxml = urllib2.urlopen(aoossos_gc_url).read()\naoosgc = SensorObservationService(None, xml=aoosxml)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 61
},
{
"cell_type": "code",
"collapsed": false,
"input": "# find wmo 46083 ndbc buoy offering, from the offerings list\nndbcfind = [offering for offering in aoosgc.offerings if '46083' in offering.name.lower()]\naoosoff1 = ndbcfind[0]\nprint aoosoff1.id, aoosoff1.name, aoosoff1.description\nprint aoosoff1.procedures",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "urn_ioos_station_wmo_46083 urn:ioos:station:wmo:46083 None\n['urn:ioos:station:wmo:46083']\n"
}
],
"prompt_number": 62
},
{
"cell_type": "markdown",
"metadata": {},
"source": "### Interactions specific to DescribeSensor start here"
},
{
"cell_type": "code",
"collapsed": false,
"input": "descsen = aoosgc.get_operation_by_name('describesensor')\ndescsen.parameters",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 63,
"text": "{'outputFormat': {'values': ['text/xml; subtype=\"sensorML/1.0.1\"',\n 'text/xml; subtype=\"sensorML/1.0.1/profiles/ioos_sos/1.0\"']}}"
}
],
"prompt_number": 63
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Set parameters to be used in the DescribeSensor request\noFrmt = 'text/xml;subtype=\"sensorML/1.0.1/profiles/ioos_sos/1.0\"'\nprocedure = aoosoff1.procedures[0]",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 64
},
{
"cell_type": "code",
"collapsed": false,
"input": "response = aoosgc.describe_sensor(procedure=procedure, outputFormat=oFrmt)\nprint type(response), len(response)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "<type 'str'> 32302\n"
}
],
"prompt_number": 65
},
{
"cell_type": "code",
"collapsed": false,
"input": "aoosoff1_sml_root = SensorML(response)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 66
},
{
"cell_type": "markdown",
"metadata": {},
"source": "To see the structure of the IOOS SOS Milestone 1 SensorML response, see the reference implementation XML at http://code.google.com/p/ioostech/source/browse/trunk/templates/Milestone1.0/SML-DescribeSensor-Station.xml"
},
{
"cell_type": "code",
"collapsed": false,
"input": "print type(aoosoff1_sml_root), type(aoosoff1_sml_root.systems), len(aoosoff1_sml_root.systems)\naoosoff1_sml = aoosoff1_sml_root.systems[0]\nprint type(aoosoff1_sml)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "<class 'owslib.swe.sensor.sml.SensorML'> <type 'list'> 1\n<class 'owslib.swe.sensor.sml.SystemMetadata'>\n"
}
],
"prompt_number": 67
},
{
"cell_type": "code",
"collapsed": false,
"input": "print aoosoff1_sml.id, \" || \", aoosoff1_sml.description\nprint \"*** IDENTIFIERS:\\n\", aoosoff1_sml.identifiers\nprint \"*** CLASSIFIERS:\\n\", aoosoff1_sml.classifiers\nprint \"*** CONTACTS:\\n\", aoosoff1_sml.contacts",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "None || Fairweather Grounds 92NM Southeast of Yakutat, AK\n*** IDENTIFIERS:\n{'stationID': <owslib.swe.sensor.sml.IdentifierMetadata object at 0x333b2d0>, 'shortName': <owslib.swe.sensor.sml.IdentifierMetadata object at 0x333b250>, 'uniqueID': <owslib.swe.sensor.sml.IdentifierMetadata object at 0x333b3d0>, 'longName': <owslib.swe.sensor.sml.IdentifierMetadata object at 0x333b350>}\n*** CLASSIFIERS:\n{'operatorSector': <owslib.swe.sensor.sml.ClassifierMetadata object at 0x333b550>, 'publisher': <owslib.swe.sensor.sml.ClassifierMetadata object at 0x333b4d0>, 'parentNetwork': <owslib.swe.sensor.sml.ClassifierMetadata object at 0x333b610>, 'platformType': <owslib.swe.sensor.sml.ClassifierMetadata object at 0x333b510>, 'sponsor': <owslib.swe.sensor.sml.ClassifierMetadata object at 0x333b5d0>}\n*** CONTACTS:\n{None: <owslib.swe.sensor.sml.ContactMetadata object at 0x333b310>}\n"
}
],
"prompt_number": 68
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Explore SensorML Identifiers"
},
{
"cell_type": "code",
"collapsed": false,
"input": "aoosoff1_sml.identifiers.keys()",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 69,
"text": "['stationID', 'shortName', 'uniqueID', 'longName']"
}
],
"prompt_number": 69
},
{
"cell_type": "code",
"collapsed": false,
"input": "print \"stationID identifier information:\"\nprint \" OWSLib object type: \", type(aoosoff1_sml.identifiers['stationID'])\nprint \" name: \", aoosoff1_sml.identifiers['stationID'].name\nprint \" definition: \", aoosoff1_sml.identifiers['stationID'].definition\nprint \" value: \", aoosoff1_sml.identifiers['stationID'].value",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "stationID identifier information:\n OWSLib object type: <class 'owslib.swe.sensor.sml.IdentifierMetadata'>\n name: stationID\n definition: http://mmisw.org/ont/ioos/definition/stationID\n value: urn:ioos:station:wmo:46083\n"
}
],
"prompt_number": 70
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Explore SensorML Classifiers"
},
{
"cell_type": "code",
"collapsed": false,
"input": "aoosoff1_sml.classifiers.keys()",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 71,
"text": "['operatorSector', 'publisher', 'parentNetwork', 'platformType', 'sponsor']"
}
],
"prompt_number": 71
},
{
"cell_type": "code",
"collapsed": false,
"input": "def print_classifier_info(sml, classifier_name):\n classifier = sml.classifiers[classifier_name]\n print \"%s classifier information:\" % classifier.name\n print \" OWSLib object type: \", type(classifier)\n print \" codespace: \", classifier.codespace\n print \" name: \", classifier.name\n print \" definition: \", classifier.definition\n print \" value: \", classifier.value\n return classifier",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": "platformType = print_classifier_info(aoosoff1_sml, 'platformType')",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "platformType classifier information:\n OWSLib object type: <class 'owslib.swe.sensor.sml.ClassifierMetadata'>\n codespace: http://mmisw.org/ont/ioos/platform\n name: platformType\n definition: http://mmisw.org/ont/ioos/definition/platformType\n value: FIXED MET STATION\n"
}
],
"prompt_number": 73
},
{
"cell_type": "code",
"collapsed": false,
"input": "publisher = print_classifier_info(aoosoff1_sml, 'publisher')",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "publisher classifier information:\n OWSLib object type: <class 'owslib.swe.sensor.sml.ClassifierMetadata'>\n codespace: http://mmisw.org/ont/ioos/organization\n name: publisher\n definition: http://mmisw.org/ont/ioos/definition/publisher\n value: NDBC\n"
}
],
"prompt_number": 74
},
{
"cell_type": "code",
"collapsed": false,
"input": "operatorSector = print_classifier_info(aoosoff1_sml, 'operatorSector')",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "operatorSector classifier information:\n OWSLib object type: <class 'owslib.swe.sensor.sml.ClassifierMetadata'>\n codespace: http://mmisw.org/ont/ioos/sector\n name: operatorSector\n definition: http://mmisw.org/ont/ioos/definition/operatorSector\n value: gov_federal\n"
}
],
"prompt_number": 75
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Test the validity of classifiers against MMI vocabularies\nThe MMI term testing function is taken (and illustrated) from my notebook at http://nbviewer.ipython.org/6514804"
},
{
"cell_type": "code",
"collapsed": false,
"input": "def get_term_ontmeta_jsonurl(termjsonurl):\n \"\"\" \n Pass a complete URL for the json response corresponding to an ontology term.\n It can be from any ontology engine as long as the json is similar to MMI's.\n eg: 'http://mmisw.org/ont/cf/parameter/wind_speed.json'\n Parses the json response to return a regularized dictionary as well as the\n raw json response loaded as a dict.\n \"\"\"\n \n # This is probably not applicable to other ontology repos beyond MMI...\n param_jsonstr = requests.get(termjsonurl).content\n # Validate the term URL\n if param_jsonstr == '[]': # term URL does not exist; term is invalid\n return (None, None)\n \n #param_jsonstr = urllib2.urlopen(termjsonurl).read() \n param_json = json.loads(param_jsonstr)\n param_extract_all_dct = dict([(param_el[1].split('/')[-1][:-1], param_el[2]) \n for param_el in param_json])\n \n # Add handling of more IOOS vocabularies, as needed\n if 'cf/parameter' in termjsonurl:\n param_extract_dct = {'vocabulary': 'cf/parameter',\n 'definition': param_extract_all_dct['core#definition'],\n 'units': param_extract_all_dct['canonical_units'],\n 'units_type': 'canonical units', \n }\n elif 'ioos/parameter' in termjsonurl:\n param_extract_dct = {'vocabulary': 'ioos/parameter',\n 'definition': param_extract_all_dct['Definition'],\n 'units': param_extract_all_dct['Units'], \n 'units_type': 'example or common units', \n }\n else:\n param_extract_dct = param_extract_all_dct\n \n return (param_extract_all_dct, param_extract_dct)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 76
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Test for a term known to be valid\n# Note that get_term_ontmeta_jsonurl() currently is tuned to do special extra handling\n# only with \"parameter\" (observed properties) terms\nterm_url_json = operatorSector.codespace + '/' + operatorSector.value + '.json' \nprint term_url_json\nget_term_ontmeta_jsonurl(term_url_json)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "http://mmisw.org/ont/ioos/sector/gov_federal.json\n"
},
{
"output_type": "pyout",
"prompt_number": 77,
"text": "({u'22-rdf-syntax-ns#type': u'<http://mmisw.org/ont/ioos/sector/Organization>',\n u'description': u'\"Government-Federal\"',\n u'name': u'\"Government-Federal\"',\n u'rdf-schema#label': u'\"gov_federal\"',\n u'term': u'\"gov_federal\"'},\n {u'22-rdf-syntax-ns#type': u'<http://mmisw.org/ont/ioos/sector/Organization>',\n u'description': u'\"Government-Federal\"',\n u'name': u'\"Government-Federal\"',\n u'rdf-schema#label': u'\"gov_federal\"',\n u'term': u'\"gov_federal\"'})"
}
],
"prompt_number": 77
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Test for a term known to be invalid (doesn't exist on MMI vocabulary)\nterm_url_json = platformType.codespace + '/' + platformType.value + '.json' \nprint term_url_json\nget_term_ontmeta_jsonurl(term_url_json)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "http://mmisw.org/ont/ioos/platform/FIXED MET STATION.json\n"
},
{
"output_type": "pyout",
"prompt_number": 78,
"text": "(None, None)"
}
],
"prompt_number": 78
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Explore SensorML components (including 'parameters')"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Looks like the sensorml doesn't have any \"component\" entries \n# (sensors / observed properties / parameters) yet ...\nprint type(aoosoff1_sml.components), len(aoosoff1_sml.components)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "<type 'dict'> 0\n"
}
],
"prompt_number": 79
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment