Skip to content

Instantly share code, notes, and snippets.

@emiliom
Last active December 22, 2015 18:39
Show Gist options
  • Save emiliom/6514804 to your computer and use it in GitHub Desktop.
Save emiliom/6514804 to your computer and use it in GitHub Desktop.
#IOOS #MMI Example Python code to extract controlled vocabulary term information from MMI-hosted vocabularies.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Accessing IOOS MMI Vocabulary Terms"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": "Accessing and Validating IOOS MMI Vocabulary Terms"
},
{
"cell_type": "code",
"collapsed": false,
"input": "import json\nimport requests",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"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 regularized dictionaries.\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": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Test terms (as json URL end points)\nws_ioos = 'http://mmisw.org/ont/ioos/parameter/wind_speed.json'\nws_cf = 'http://mmisw.org/ont/cf/parameter/wind_speed.json'",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": "get_term_ontmeta_jsonurl(ws_ioos)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": "({u'22-rdf-syntax-ns#type': u'<http://mmisw.org/ont/ioos/parameter/Parameter>',\n u'Definition': u'\"Magnitude of wind velocity. Wind is motion of air relative to the surface of the earth.\"',\n u'Term': u'\"wind_speed\"',\n u'Units': u'\"m s-1\"',\n u'core#exactMatch': u'<http://mmisw.org/ont/cf/parameter/wind_speed>',\n u'rdf-schema#label': u'\"wind_speed\"'},\n {'definition': u'\"Magnitude of wind velocity. Wind is motion of air relative to the surface of the earth.\"',\n 'units': u'\"m s-1\"',\n 'units_type': 'example or common units',\n 'vocabulary': 'ioos/parameter'})"
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": "get_term_ontmeta_jsonurl(ws_cf)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 5,
"text": "({u'22-rdf-syntax-ns#type': u'<http://mmisw.org/ont/cf/parameter/Standard_Name>',\n u'canonical_units': u'\"m s-1\"',\n u'core#definition': u'\"Speed is the magnitude of velocity. Wind is defined as a two-dimensional (horizontal) air velocity vector, with no vertical component. (Vertical motion in the atmosphere has the standard name upward_air_velocity.) The wind speed is the magnitude of the wind velocity.\"',\n u'skos#exactMatch': u'<http://mmisw.org/ont/secoora/parameter/wind_speed>'},\n {'definition': u'\"Speed is the magnitude of velocity. Wind is defined as a two-dimensional (horizontal) air velocity vector, with no vertical component. (Vertical motion in the atmosphere has the standard name upward_air_velocity.) The wind speed is the magnitude of the wind velocity.\"',\n 'units': u'\"m s-1\"',\n 'units_type': 'canonical units',\n 'vocabulary': 'cf/parameter'})"
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": "# response for an invalid term url\nget_term_ontmeta_jsonurl('http://mmisw.org/ont/cf/parameter/uffda.json')",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 6,
"text": "(None, None)"
}
],
"prompt_number": 6
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment