Test support for different types of CSW queries in USGS ScienceBase. In ScienceBase, each item has a CSW which contains all child items. Thus I clicked on the json link on the top level Hurricane Sandy link, and located the CSW endpoint within.
In [7]:
from pylab import *
from owslib.csw import CatalogueServiceWeb
from owslib import fes
import datetime as dt
from numpy import random
In [8]:
# top level USGS Hurricane Sandy community CSW link
#https://www.sciencebase.gov/catalog/item/519bee13e4b0e4e151f0232c/csw?service=CSW&version=2.0.2&request=GetCapabilities
endpoint='https://www.sciencebase.gov/catalog/item/519bee13e4b0e4e151f0232c/csw'
# san francisco bay
#endpoint='https://beta.sciencebase.gov/catalog/item/5064c1cee4b0050306265490/csw'
In [24]:
#box=[-180, -90, 180, 90]
box=[-76.4751, 38.3890, -71.7432, 42.9397] # new york harbor region
#box=[-72.0, 41.0, -69.0, 43.0] # gulf of maine
box =[-123.0, 37.0, -120., 38.] # san fran bay
box = [-75., 38., -73., 41.] # barnegat bay
# 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)
# 2011
#jd_start = dt.datetime(2013,4,20)
#jd_stop = dt.datetime(2013,4,24)
# ... or relative to now
jd_start = dt.datetime.utcnow()- dt.timedelta(days=3)
jd_stop = dt.datetime.utcnow() + 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
In [49]:
# Bounding Box [lon_min, lat_min, lon_max, lat_max]
box=[-75., 39., -71., 41.5] # new york harbor region
#box=[-72.0, 41.0, -69.0, 43.0] # gulf of maine
#box=[-160.0, 18.0, -154., 23.0] #hawaii
box=[-75, 38, -73, 40] #barnegat
In [50]:
bbox = fes.BBox(box)
In [51]:
std_name_list=['water_surface_height_above_reference_datum',
'sea_surface_height_above_geoid','sea_surface_elevation',
'sea_surface_height_above_reference_ellipsoid','sea_surface_height_above_sea_level',
'sea_surface_height','water level']
In [52]:
std_name_list=['GDP','output']
In [53]:
csw = CatalogueServiceWeb(endpoint,timeout=60)
csw.version
Out[53]:
In [54]:
for oper in csw.operations:
if oper.name == 'GetRecords':
print oper.constraints
In [55]:
csw.getrecords2(maxrecords=1000,esn='full')
print len(csw.records.keys())
for rec,item in csw.records.iteritems():
print item.title
In [56]:
val='Barnegat'
text_filt = fes.PropertyIsLike(propertyname='AnyText',literal=('*%s*' % val))
filter_list = [text_filt ]
In [57]:
# try request using multiple filters "and" syntax: [[filter1,filter2]]
csw.getrecords2(constraints=filter_list,maxrecords=1000,esn='full')
print len(csw.records.keys())
In [58]:
for rec,item in csw.records.iteritems():
print item.title
In [59]:
val='Barnegat'
text_filt = fes.PropertyIsLike(propertyname='AnyText',literal=('*%s*' % val))
filter_list = [fes.And([ bbox, text_filt]) ]
# try request using multiple filters "and" syntax: [[filter1,filter2]]
csw.getrecords2(constraints=filter_list,maxrecords=1000,esn='full')
print len(csw.records.keys())
In [60]:
val='Barnegat'
filter_list = [ bbox]
# try request using multiple filters "and" syntax: [[filter1,filter2]]
csw.getrecords2(constraints=filter_list,maxrecords=1000,esn='full')
print len(csw.records.keys())
In [61]:
for rec,item in csw.records.iteritems():
print item.title
In [62]:
choice=random.choice(list(csw.records.keys()))
print choice
csw.records[choice].references
In [63]:
foo=csw.records[choice]
In [20]:
print(foo.xml)
In [20]: