Test Data.gov bbox, start, stop filters


In [1]:
from owslib.csw import CatalogueServiceWeb
from owslib.fes import SortBy, SortProperty
from owslib import fes
import datetime as dt

In [2]:
#csw = CatalogueServiceWeb('http://www.ngdc.noaa.gov/geoportal/csw',timeout=60) # NGDC Geoportal
csw = CatalogueServiceWeb('http://catalog.data.gov/csw-all',timeout=60)

In [3]:
csw.get_operation_by_name('GetRecords').constraints


Out[3]:
[Constraint: SupportedISOQueryables - ['apiso:DistanceValue', 'apiso:Abstract', 'apiso:RevisionDate', 'apiso:Subject', 'apiso:KeywordType', 'apiso:Title', 'apiso:CRS', 'apiso:PublicationDate', 'apiso:Type', 'apiso:AlternateTitle', 'apiso:BoundingBox', 'apiso:AnyText', 'apiso:ParentIdentifier', 'apiso:Modified', 'apiso:Operation', 'apiso:Format', 'apiso:TempExtent_end', 'apiso:DistanceUOM', 'apiso:OrganisationName', 'apiso:ServiceType', 'apiso:TempExtent_begin', 'apiso:ResourceLanguage', 'apiso:ServiceTypeVersion', 'apiso:OperatesOn', 'apiso:Denominator', 'apiso:HasSecurityConstraints', 'apiso:OperatesOnIdentifier', 'apiso:GeographicDescriptionCode', 'apiso:Language', 'apiso:Identifier', 'apiso:OperatesOnName', 'apiso:TopicCategory', 'apiso:CreationDate', 'apiso:CouplingType'],
 Constraint: AdditionalQueryables - ['apiso:Lineage', 'apiso:Classification', 'apiso:Creator', 'apiso:Relation', 'apiso:OtherConstraints', 'apiso:SpecificationTitle', 'apiso:ResponsiblePartyRole', 'apiso:SpecificationDateType', 'apiso:Degree', 'apiso:Contributor', 'apiso:ConditionApplyingToAccessAndUse', 'apiso:SpecificationDate', 'apiso:AccessConstraints', 'apiso:Publisher'],
 Constraint: SupportedDublinCoreQueryables - ['dc:contributor', 'dc:source', 'dc:language', 'dc:title', 'dc:subject', 'dc:creator', 'dc:type', 'ows:BoundingBox', 'dct:modified', 'dct:abstract', 'dc:relation', 'dc:date', 'dc:identifier', 'dc:publisher', 'dc:format', 'csw:AnyText', 'dc:rights']]

In [4]:
# adjust to match MaxRecordDefault of CSW, if would be cleaner if we pick this up Capabilities XML
# this issue will allow for this: https://github.com/geopython/OWSLib/issues/211
pagesize = 10
sort_property = 'dc:title'  # a supported queryable of the CSW
sort_order = 'ASC'  # should be 'ASC' or 'DESC'

In [5]:
sortby = SortBy([SortProperty(sort_property, sort_order)])
foo=sortby.properties

In [6]:
# hopefully something like this will be implemented in fes soon
def dateRange(start_date='1900-01-01',stop_date='2100-01-01',constraint='overlaps'):
    if constraint == 'overlaps':
        start = fes.PropertyIsLessThanOrEqualTo(propertyname='apiso:TempExtent_begin', literal=stop_date)
        stop = fes.PropertyIsGreaterThanOrEqualTo(propertyname='apiso:TempExtent_end', literal=start_date)
    elif constraint == 'within':
        start = fes.PropertyIsGreaterThanOrEqualTo(propertyname='apiso:TempExtent_begin', literal=start_date)
        stop = fes.PropertyIsLessThanOrEqualTo(propertyname='apiso:TempExtent_end', literal=stop_date)
    return start,stop

In [7]:
val = 'salinity'
box=[-72.0, 41.0, -69.0, 43.0]   # gulf of maine

In [8]:
# specific specific times (UTC) ...

# hurricane sandy
jd_start = dt.datetime(2012,10,26)
jd_stop = dt.datetime(2012,11,2)

# 2014 feb 10-15 storm
jd_start = dt.datetime(2014,2,10)
jd_stop = dt.datetime(2014,2,15)

# 2014 recent
jd_start = dt.datetime(2014,3,8)
jd_stop = dt.datetime(2014,3,11)

# 2014 recent
jd_start = dt.datetime(1988,1,1)
jd_stop = dt.datetime(1988,3,1)

# 2011 
#jd_start = dt.datetime(2013,4,20)
#jd_stop = dt.datetime(2013,4,24)

# ... or relative to now
#jd_now = dt.datetime.utcnow()
#jd_start = jd_now - dt.timedelta(days=3)
#jd_stop = jd_now + dt.timedelta(days=3)

start_date = jd_start.strftime('%Y-%m-%d %H:00')
stop_date  = jd_stop.strftime('%Y-%m-%d %H:00')

jd_start = dt.datetime.strptime(start_date,'%Y-%m-%d %H:%M')
jd_stop = dt.datetime.strptime(stop_date,'%Y-%m-%d %H:%M')

print start_date,'to',stop_date


1988-01-01 00:00 to 1988-03-01 00:00

In [9]:
start,stop = dateRange(start_date,stop_date)
filter1 = fes.PropertyIsLike(propertyname='apiso:AnyText',literal=('*%s*' % val),
                        escapeChar='\\',wildCard='*',singleChar='?')
bbox = fes.BBox(box,crs='urn:ogc:def:crs:OGC:1.3:CRS84')

In [10]:
filter_list = [fes.And([ bbox, filter1]) ]
csw.getrecords2(constraints=filter_list)
csw.results['matches']


Out[10]:
2257

In [11]:
filter_list = [fes.And([ bbox, filter1, start,stop]) ]
csw.getrecords2(constraints=filter_list)
csw.results['matches']


Out[11]:
151

In [12]:
startposition = 0
maxrecords = 20
while True:
    
    print 'getting records %d to %d' % (startposition, startposition+pagesize)
    csw.getrecords2(constraints=filter_list,
                    startposition=startposition, maxrecords=pagesize, sortby=sortby)
#    print csw.request
    for rec,item in csw.records.iteritems():
        print item.title
    if csw.results['nextrecord'] == 0:
        break
    startposition += pagesize
    if startposition >= maxrecords:
        break


getting records 0 to 10
AIR PRESSURE and Other Data from UNKNOWN From World-Wide Distribution from 19880101 to 19880131 (NODC Accession 8800056)
AIR PRESSURE and Other Data from UNKNOWN From World-Wide Distribution from 19880201 to 19880227 (NODC Accession 8800051)
Annual update of delayed mode profile data from DFO-MPO Canada (NODC Accession 0098794)
CARBON DIOXIDE - PARTIAL PRESSURE (pCO2) - SEA and Other Data from DISCOVERER and Other Platforms from 19870519 to 19940107 (NODC Accession 9900015)
Carbon dioxide, temperature, salinity, and other variables collected via surface underway survey from Volunteer Observing Ship Hakuho Maru in the North Pacific Ocean, South Pacific Ocean and others from 1968-11-16 to 1988-03-23 (NODC Accession 0080981)
Carbon dioxide, temperature, salinity collected via surface underway in the Indian and North/South Pacific Ocean as part of the EPOCS project from 23 May 1986 to 20 April 1989 (NODC Accession 0000071)
Chemical, physical, and salinity data from bottle and CTD casts in a world-wide distribution from 14 May 1957 to 18 December 1999 (NODC Accession 0000266)
Chemical, temperature, and other data from bottle casts in a world-wide distribution from 04 October 1961 to 24 August 1990 (NODC Accession 0000231)
Climate Forecast System Reanalysis (CFSR), for 1979 to 2011
Climate Forecast System Reforecast, for 1981 to 2011
getting records 10 to 20
Climate Forecast System Reforecast, for 1981 to 2011
Current meter components and other data from FIXED PLATFORMS from 20 September 1987 to 09 October 1991 (NODC Accession 9100216)
DEPTH - OBSERVATION and Other Data from 19020101 to 19951231 (NODC Accession 9700202)
DEPTH - OBSERVATION and Other Data from CUXHAVEN and Other Platforms from 19200118 to 19891219 (NODC Accession 9800177)
DEPTH - OBSERVATION and Other Data from KNORR and Other Platforms From South Atlantic Ocean and Others from 19720724 to 19890113 (NODC Accession 9500080)
DEPTH - OBSERVATION and Other Data from METEOR and Other Platforms From North Atlantic Ocean and Others from 19670111 to 19910422 (NODC Accession 9500038)
DEPTH - OBSERVATION and Other Data from MORSKOY GEOFIZIK and Other Platforms from 19690305 to 19981124 (NODC Accession 9900007)
DEPTH - OBSERVATION and Other Data from MULTIPLE SHIPS from 19560816 to 19910610 (NODC Accession 9700233)
DEPTH - OBSERVATION and Other Data from MUSSON and Other Platforms From North Atlantic Ocean from 19780705 to 19891204 (NODC Accession 9900127)
DEPTH - OBSERVATION and Other Data from POLARSTERN and Other Platforms from 19240804 to 19991124 (NODC Accession 9900244)

In [12]: