Python Programming Challenge Jeffery S. Horsburgh Hydroinformatics Fall 2016 This work was funded by National Science Foundation Grants EPS 1135482 and EPS 1208732
Objectives Introduction to the Python programming language Write and execute computer code to automate repetitive tasks Discover and access data from major data sources Retrieve and use data from common hydrologic data sources
Coding Challenge
Get the Data http://waterdata.usgs.gov/nwis/uv?cb_00060=on&format=rdb&site_no=10109000&period=&begin_date=2015-10-01&end_date=2015-10-31
Coding Challenge Choose a real-time streamflow gage from the USGS that you are interested in. It could be a nearby gage or one that is near and dear to your heart. To see an interactive map of gage locations, go to: http://maps.waterdata.usgs.gov/mapper/index.html Create a Python script that does the following: Download the most recent data from the USGS website Read the file Extract the most recent streamflow value from the file Print the most recent streamflow value and the date at which it occurred to the screen
Example Solution “The most recent streamflow value for USGS Gage 10109000 was 109 cfs on 2014-08-25 3:15.”
Step 1: Create a New PyCharm Project Open PyCharm Select File New Project Name your project and select the interpreter
Step 2: Create a New Python Script Right click on your project folder Select New Python File Name your script
Step 3: Write Some Pseudo Code
Step 4: Write Some Real Code
Some Hints Develop your solution as a script in PyCharm The USGS website returns the data as a file, but you have to request it using a URL in your code. The Python module called “urllib2” (for Python 2.7) or “urllib3” for Python 3 is one option for downloading a file using a URL. The data are returned in a text file where each new line represents a new date/time and data value. Each new line is delineated with a new line character. Each element within a line is delimited by a tab character. Check out the Python “split” method for splitting a string based on a delimiter