Network Data Access - USGS NWIS Service-based Data Access

Karl Benedict
Director, Earth Data Analysis Center
Associate Professor, University Libraries
University of New Mexico
kbene@unm.edu

An Analysis

This analysis demonstrates searching for datasets that meet a set of specified conditions, accessing via advertised services, processing and plotting the data from the service.

Service Documentation: http://waterservices.usgs.gov/rest/IV-Service.html

Enable the needed python libraries


In [2]:
import urllib
import zipfile
import StringIO
import string
import pandas
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import HTML
import json

Set some initial variables


In [3]:
county_name = ""
start_date = "20140101"
end_date = "20150101"
diag = False

Options


In [21]:
## Retrieve the bounding box of the specified county - if no county is specified, the bounding boxes for all NM counties will be requested
countyBBOXlink = "http://gstore.unm.edu/apps/epscor/search/nm_counties.json?limit=100&query=" + county_name  ## define the request URL
print countyBBOXlink ## print the request URL for verification
print
bboxFile = urllib.urlopen(countyBBOXlink) ## request the bounding box information from the server
bboxData = json.load(bboxFile)
# print bboxData

# Get data for BBOX defined by specified county(ies)
myCounties = []
for countyBBOX in bboxData["results"]:
    minx,miny,maxx,maxy = countyBBOX[u'box']
    myDownloadLink = "http://waterservices.usgs.gov/nwis/iv/?bBox=%f,%f,%f,%f&format=json&period=P7D&parameterCd=00060" % (minx,miny,maxx,maxy) # retrieve data for the specified BBOX for the last 7 days as JSON
    print myDownloadLink
    myCounty = {u'name':countyBBOX[u'text'],u'minx':minx,u'miny':miny,u'maxx':maxx,u'maxy':maxy,u'downloadLink':myDownloadLink}
    myCounties.append(myCounty)


http://gstore.unm.edu/apps/epscor/search/nm_counties.json?limit=100&query=

http://waterservices.usgs.gov/nwis/iv/?bBox=-107.196175,34.869022,-106.149574,35.219643&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-109.047470,33.200928,-107.711266,34.581020&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-105.353912,32.518768,-103.509666,34.088490&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-109.046219,34.577576,-107.066689,35.348335&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-105.377747,36.217609,-104.007179,36.996243&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-103.740021,34.302570,-103.042656,34.954407&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-104.893143,33.995411,-103.945618,34.779518&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-107.299683,31.783148,-106.340469,33.052795&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-104.852219,31.999939,-103.722298,32.965614&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-109.047516,31.863693,-107.608276,33.209648&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-105.312973,34.346718,-104.124809,35.217098&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-104.439285,35.389469,-103.364792,36.218761&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-109.050179,31.332170,-108.208084,32.777843&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-103.815002,32.000206,-103.052620,33.570629&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-106.373932,33.132030,-104.884827,34.347271&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-106.418861,35.754272,-106.171654,35.973446&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-108.230026,31.783499,-107.296341,32.605537&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-109.046799,34.958965,-107.306587,36.002884&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-105.720543,35.754265,-104.327225,36.262341&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-106.377632,32.000275,-104.847557,33.390816&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-104.129730,34.604656,-103.041344,35.740074&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-107.626930,35.930576,-105.530098,37.000153&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-103.949631,33.570068,-103.043282,34.605068&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-107.626511,35.197712,-106.245178,36.219799&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-109.046188,36.000317,-107.420914,37.000008&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-105.722206,35.041561,-103.637016,35.871017&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-106.249222,35.040012,-105.714470,36.002357&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-108.000618,32.605160,-106.340408,33.480118&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-107.726555,33.475189,-105.923950,34.580662&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-106.058365,36.013012,-105.200111,36.996017&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-106.471077,34.259670,-105.290115,35.042179&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-104.009125,35.739273,-103.001961,37.000294&format=json&period=P7D&parameterCd=00060
http://waterservices.usgs.gov/nwis/iv/?bBox=-107.204681,34.436993,-106.410973,34.958065&format=json&period=P7D&parameterCd=00060

In [70]:
#countySubset = [myCounties[0]]
#print countySubset

valueList = []

for county in myCounties:
    print "processing: %s" % county["downloadLink"]
    try:
        datafile = urllib.urlopen(county["downloadLink"])
        data = json.load(datafile)
        values = data["value"]["timeSeries"][0]["values"]
        for item in values:
            for valueItem in item["value"]:
                #print json.dumps(item["value"], sort_keys=True, indent=4)
                myValue = {"dateTime":valueItem["dateTime"].replace("T"," ").replace(".000-06:00",""),"value":valueItem["value"], "county":county["name"]}
                #print myValue
                valueList.append(myValue)
        #print valueList
    except:
        print "\tfailed for this one ..."
     
    #print json.dumps(values, sort_keys=True, indent=4)


processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-107.196175,34.869022,-106.149574,35.219643&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-109.047470,33.200928,-107.711266,34.581020&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-105.353912,32.518768,-103.509666,34.088490&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-109.046219,34.577576,-107.066689,35.348335&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-105.377747,36.217609,-104.007179,36.996243&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-103.740021,34.302570,-103.042656,34.954407&format=json&period=P7D&parameterCd=00060
	failed for this one ...
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-104.893143,33.995411,-103.945618,34.779518&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-107.299683,31.783148,-106.340469,33.052795&format=json&period=P7D&parameterCd=00060
	failed for this one ...
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-104.852219,31.999939,-103.722298,32.965614&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-109.047516,31.863693,-107.608276,33.209648&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-105.312973,34.346718,-104.124809,35.217098&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-104.439285,35.389469,-103.364792,36.218761&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-109.050179,31.332170,-108.208084,32.777843&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-103.815002,32.000206,-103.052620,33.570629&format=json&period=P7D&parameterCd=00060
	failed for this one ...
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-106.373932,33.132030,-104.884827,34.347271&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-106.418861,35.754272,-106.171654,35.973446&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-108.230026,31.783499,-107.296341,32.605537&format=json&period=P7D&parameterCd=00060
	failed for this one ...
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-109.046799,34.958965,-107.306587,36.002884&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-105.720543,35.754265,-104.327225,36.262341&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-106.377632,32.000275,-104.847557,33.390816&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-104.129730,34.604656,-103.041344,35.740074&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-107.626930,35.930576,-105.530098,37.000153&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-103.949631,33.570068,-103.043282,34.605068&format=json&period=P7D&parameterCd=00060
	failed for this one ...
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-107.626511,35.197712,-106.245178,36.219799&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-109.046188,36.000317,-107.420914,37.000008&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-105.722206,35.041561,-103.637016,35.871017&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-106.249222,35.040012,-105.714470,36.002357&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-108.000618,32.605160,-106.340408,33.480118&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-107.726555,33.475189,-105.923950,34.580662&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-106.058365,36.013012,-105.200111,36.996017&format=json&period=P7D&parameterCd=00060
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-106.471077,34.259670,-105.290115,35.042179&format=json&period=P7D&parameterCd=00060
	failed for this one ...
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-104.009125,35.739273,-103.001961,37.000294&format=json&period=P7D&parameterCd=00060
	failed for this one ...
processing: http://waterservices.usgs.gov/nwis/iv/?bBox=-107.204681,34.436993,-106.410973,34.958065&format=json&period=P7D&parameterCd=00060

In [71]:
df = pandas.DataFrame(valueList)

In [90]:
df['dateTime'] = pandas.to_datetime(df["dateTime"])
df['value'] = df['value'].astype(float).fillna(-1)

print df.shape
print df.dtypes
print "column names"
print "------------"
for colName in df.columns:
    print colName
print
print df.head()


(15056, 3)
county              object
dateTime    datetime64[ns]
value              float64
dtype: object
column names
------------
county
dateTime
value

       county            dateTime  value
0  Bernalillo 2014-04-17 23:05:00   0.73
1  Bernalillo 2014-04-17 23:10:00   0.73
2  Bernalillo 2014-04-17 23:15:00   0.73
3  Bernalillo 2014-04-17 23:20:00   0.73
4  Bernalillo 2014-04-17 23:25:00   0.73

In [92]:
fig,ax = plt.subplots(figsize=(10,8))
ax.width = 1
ax.height = .5
plt.xkcd()
#plt.ylim(-25,30)
ax.plot_date(df['dateTime'], df['value'], '.', label="Discharge (cf/sec)", color="0.2")
fig.autofmt_xdate()
plt.legend(loc=2, bbox_to_anchor=(1.0,1))
plt.title("15-minute Discharge - cubic feet per second")
plt.ylabel("Discharge")
plt.xlabel("Date")


Out[92]:
<matplotlib.text.Text at 0xa3ca790>

In [ ]: