Skip to content

Instantly share code, notes, and snippets.

@rsignell-usgs
Created March 6, 2017 20:18
Show Gist options
  • Save rsignell-usgs/26a75c7fe8eae86d83318cc08bbc99d0 to your computer and use it in GitHub Desktop.
Save rsignell-usgs/26a75c7fe8eae86d83318cc08bbc99d0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# IOOS Archive CSW search"
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "from owslib.csw import CatalogueServiceWeb\nfrom owslib import fes\nimport numpy as np\nfrom pprint import pprint\nfrom datetime import datetime, timedelta",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "endpoint = 'https://data.nodc.noaa.gov/geoportal/csw'",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "csw = CatalogueServiceWeb(endpoint,timeout=60)\nprint(csw.version)",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "2.0.2\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "csw.get_operation_by_name('GetRecords').constraints",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "[Constraint: SupportedCommonQueryables - ['Subject', 'Title', 'Abstract', 'AnyText', 'Format', 'Identifier', 'Modified', 'Type', 'BoundingBox'],\n Constraint: SupportedISOQueryables - ['apiso:Subject', 'apiso:Title', 'apiso:Abstract', 'apiso:AnyText', 'apiso:Format', 'apiso:Identifier', 'apiso:Modified', 'apiso:Type', 'apiso:BoundingBox', 'apiso:CRS.Authority', 'apiso:CRS.ID', 'apiso:CRS.Version', 'apiso:RevisionDate', 'apiso:AlternateTitle', 'apiso:CreationDate', 'apiso:PublicationDate', 'apiso:OrganizationName', 'apiso:HasSecurityConstraints', 'apiso:Language', 'apiso:ResourceIdentifier', 'apiso:ParentIdentifier', 'apiso:KeywordType', 'apiso:TopicCategory', 'apiso:ResourceLanguage', 'apiso:GeographicDescriptionCode', 'apiso:Denominator', 'apiso:DistanceValue', 'apiso:DistanceUOM', 'apiso:TempExtent_begin', 'apiso:TempExtent_end', 'apiso:ServiceType', 'apiso:ServiceTypeVersion', 'apiso:Operation', 'apiso:OperatesOn', 'apiso:OperatesOnIdentifier', 'apiso:OperatesOnName', 'apiso:CouplingType'],\n Constraint: AdditionalQueryables - ['apiso:Degree', 'apiso:AccessConstraints', 'apiso:OtherConstraints', 'apiso:Classification', 'apiso:ConditionApplyingToAccessAndUse', 'apiso:Lineage', 'apiso:ResponsiblePartyRole', 'apiso:SpecificationTitle', 'apiso:SpecificationDate', 'apiso:SpecificationDateType']]"
},
"metadata": {},
"execution_count": 4
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "def fes_date_filter(start, stop, constraint='overlaps'):\n \"\"\"\n Take datetime-like objects and returns a fes filter for date range\n (begin and end inclusive).\n NOTE: Truncates the minutes!!!\n Examples\n --------\n >>> from datetime import datetime, timedelta\n >>> stop = datetime(2010, 1, 1, 12, 30, 59).replace(tzinfo=pytz.utc)\n >>> start = stop - timedelta(days=7)\n >>> begin, end = fes_date_filter(start, stop, constraint='overlaps')\n >>> begin.literal, end.literal\n ('2010-01-01 12:00', '2009-12-25 12:00')\n >>> begin.propertyoperator, end.propertyoperator\n ('ogc:PropertyIsLessThanOrEqualTo', 'ogc:PropertyIsGreaterThanOrEqualTo')\n >>> begin, end = fes_date_filter(start, stop, constraint='within')\n >>> begin.literal, end.literal\n ('2009-12-25 12:00', '2010-01-01 12:00')\n >>> begin.propertyoperator, end.propertyoperator\n ('ogc:PropertyIsGreaterThanOrEqualTo', 'ogc:PropertyIsLessThanOrEqualTo')\n \"\"\"\n start = start.strftime('%Y-%m-%d %H:00')\n stop = stop.strftime('%Y-%m-%d %H:00')\n if constraint == 'overlaps':\n propertyname = 'apiso:TempExtent_begin'\n begin = fes.PropertyIsLessThanOrEqualTo(propertyname=propertyname,\n literal=stop)\n propertyname = 'apiso:TempExtent_end'\n end = fes.PropertyIsGreaterThanOrEqualTo(propertyname=propertyname,\n literal=start)\n elif constraint == 'within':\n propertyname = 'apiso:TempExtent_begin'\n begin = fes.PropertyIsGreaterThanOrEqualTo(propertyname=propertyname,\n literal=start)\n propertyname = 'apiso:TempExtent_end'\n end = fes.PropertyIsLessThanOrEqualTo(propertyname=propertyname,\n literal=stop)\n else:\n raise NameError('Unrecognized constraint {}'.format(constraint))\n return begin, end",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "#filter 1: search only IOOS data\n# IOOS data may be found by using this free text search (from Yuanjie Li <yuanjie.li@noaa.gov>)\nval = 'Integrated Ocean Observing System Data Assembly Centers Data Stewardship Program'\nfilter1 = fes.PropertyIsLike(propertyname='apiso:AnyText',literal=('*%s*' % val),\n escapeChar='\\\\',wildCard='*',singleChar='?')\n\n#filter 2: search only Glider data\nval = 'Glider'\nfilter2 = fes.PropertyIsLike(propertyname='apiso:AnyText',literal=('*%s*' % val),\n escapeChar='\\\\',wildCard='*',singleChar='?')\n\n# filter 3: Search only specific region \nbbox = [-87.40, 34.25, -63.70, 66.70] # [lon_min, lat_min, lon_max, lat_max]\nbbox_filter = fes.BBox(bbox,crs='urn:ogc:def:crs:OGC:1.3:CRS84')\n\n# filter 4: search only in last year\nnow = datetime.utcnow()\nstart, stop = now - timedelta(days=(365)), now + timedelta(days=(0))\nbegin, end = fes_date_filter(start, stop)\n\n# Add all the filters to list:\nfilter_list = [fes.And([filter1, filter2, bbox_filter, begin, end])]\n\n# Query CSW\ncsw.getrecords2(constraints=filter_list,maxrecords=15,esn='full')\n\nprint(len(csw.records.keys()))\nfor rec in list(csw.records.keys()):\n print(csw.records[rec].title)",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "9\nPhysical trajectory profile data from glider sp066 deployed by Woods Hole Oceanographic Institution in the East Coast - US/Canada from 2015-12-17 to 2016-04-06 (NCEI Accession 0153789)\nPhysical trajectory profile data from glider blue deployed by University of Massachusetts; University of Massachusetts - Dartmouth in the Mid-Atlantic Bight from 2016-05-18 to 2016-06-06 (NCEI Accession 0153544)\nPhysical trajectory profile data from glider ru30 deployed by Rutgers, the State University of New Jersey in the Mid-Atlantic Bight from 2016-09-01 to 2016-09-26 (NCEI Accession 0156681)\nPhysical trajectory profile data from glider whoi_406 deployed by Woods Hole Oceanographic Institution in the Mid-Atlantic Bight from 2016-09-02 to 2016-09-20 (NCEI Accession 0156640)\nPhysical trajectory profile data from glider ru28 deployed by Rutgers, the State University of New Jersey in the Mid-Atlantic Bight from 2016-09-01 to 2016-09-22 (NCEI Accession 0156659)\nPhysical trajectory profile data from glider ru32 deployed by Rutgers, the State University of New Jersey in the Mid-Atlantic Bight from 2016-08-10 to 2016-08-19 (NCEI Accession 0156390)\nPhysical trajectory profile data from glider ud_134 deployed by University of Delaware in the Mid-Atlantic Bight from 2016-08-30 to 2016-09-07 (NCEI Accession 0156682)\nPhysical trajectory profile data from glider ru28 deployed by Rutgers, the State University of New Jersey in the Mid-Atlantic Bight from 2016-07-14 to 2016-07-20 (NCEI Accession 0156658)\nPhysical trajectory profile data from glider blue deployed by University of Massachusetts; University of Massachusetts - Dartmouth in the Mid-Atlantic Bight from 2016-08-18 to 2016-08-22 (NCEI Accession 0156657)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-IOOS3-py",
"display_name": "Python [conda env:IOOS3]",
"language": "python"
},
"language_info": {
"file_extension": ".py",
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"pygments_lexer": "ipython3",
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment