Skip to content

Instantly share code, notes, and snippets.

@postfalk
Last active July 25, 2017 19:02
Show Gist options
  • Save postfalk/2830343c4cfea112fed10282d2fc6962 to your computer and use it in GitHub Desktop.
Save postfalk/2830343c4cfea112fed10282d2fc6962 to your computer and use it in GitHub Desktop.
Rivers Python Examples

Python examples for the Unimpaired Flows API

All Python example are written for Python 2.7.

  1. Install git from https://git-scm.com/ or copy code from below in your own project.
git clone https://gist.github.com/2830343c4cfea112fed10282d2fc6962.git pythonExamples
cd pythonExamples
  1. Install Python dependencies.
pip install -r requirements
  1. Run:
python basic_example.py

Run with jupyter

  1. Start notebook server, a web browser window will open.
jupyter notebook
  1. Open basic_example.ipynb

1. basic_example.py, basic_example.ipynb

Sends a simple request to /api/data/flat/ to demonstrate how to get data for a single stream segment.

2. pagination_example.ipynb

Demonstrates how to use pagination in code.

Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# Basic data load example for one COMID"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import requests\n",
"from pandas.io.json import json_normalize"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Please install requests and pandas packages in your Python environment. From the command line:\n",
"\n",
"```sh\n",
"pip install requests pandas\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"FLOW_URL = 'https://rivers.codefornature.org/api/data/flat/'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"STATUS_CODES = {\n",
" 200: 'Success',\n",
" 400: 'Malformed query',\n",
" 401: 'Not authorized',\n",
" 404: 'Not found',\n",
" 504: 'Timed out. Request smaller batch of data.'\n",
"}\n",
"\n",
"def get_data(comid):\n",
" \"\"\"Function retrieving data for a single stream segment\n",
"\n",
" Args:\n",
" comid(int): Comid for stream segment\n",
"\n",
" Returns:\n",
" dict containing data.\n",
" \"\"\"\n",
" payload = {'where': json.dumps({'stream_segment': comid})}\n",
" res = requests.get(FLOW_URL, params=payload)\n",
" print 'Requested URL:', res.url\n",
" print 'Status:', res.status_code\n",
" if res.status_code == 200:\n",
" return json.loads(res.text)['results']\n",
" try: \n",
" print STATUS_CODES[res.status_code]\n",
" return\n",
" except KeyError:\n",
" print 'Something is wrong.'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requested URL: https://rivers.codefornature.org/api/data/flat/?where=%7B%22stream_segment%22%3A+4438300%7D\n",
"Status: 200\n",
" gauge measurement month stream_segment value variable year\n",
"0 None mean 7 4438300 1.062441 estimated 1960\n",
"1 None mean 6 4438300 2.673222 estimated 1960\n",
"2 None mean 5 4438300 5.586807 estimated 1960\n",
"3 None mean 4 4438300 4.000396 estimated 1960\n",
"4 None mean 3 4438300 9.148357 estimated 1960\n",
"5 None mean 2 4438300 6.006383 estimated 1960\n",
"6 None mean 1 4438300 2.809859 estimated 1960\n",
"7 None mean 12 4438300 2.988378 estimated 1985\n",
"8 None mean 10 4438300 1.405909 estimated 1985\n",
"9 None mean 11 4438300 2.739167 estimated 1985\n",
"10 None mean 9 4438300 0.382786 estimated 1960\n",
"11 None mean 8 4438300 0.415151 estimated 1960\n",
"12 None mean 1 4438300 2.477455 estimated 1962\n",
"13 None mean 5 4438300 3.588895 estimated 1996\n",
"14 None mean 3 4438300 5.238526 estimated 1962\n",
"15 None mean 2 4438300 6.038062 estimated 1962\n",
"16 None mean 5 4438300 2.015027 estimated 1962\n",
"17 None mean 4 4438300 3.161171 estimated 1962\n",
"18 None mean 7 4438300 0.490164 estimated 1962\n",
"19 None mean 6 4438300 1.322266 estimated 1962\n",
"20 None mean 9 4438300 0.331893 estimated 1962\n",
"21 None mean 8 4438300 0.327349 estimated 1962\n",
"22 None mean 8 4438300 0.370964 estimated 1996\n",
"23 None mean 9 4438300 0.493025 estimated 1996\n",
"24 None mean 12 4438300 19.670910 estimated 1996\n",
"25 None mean 10 4438300 0.865048 estimated 1996\n",
"26 None mean 11 4438300 3.978505 estimated 1996\n",
"27 None mean 10 4438300 0.265816 estimated 1994\n",
"28 None mean 11 4438300 3.297965 estimated 1994\n",
"29 None mean 12 4438300 5.847822 estimated 1994\n",
".. ... ... ... ... ... ... ...\n",
"762 None mean 10 4438300 0.306715 estimated 1992\n",
"763 None mean 11 4438300 1.193587 estimated 1992\n",
"764 None mean 9 4438300 0.314401 estimated 1964\n",
"765 None mean 8 4438300 0.236130 estimated 1964\n",
"766 None mean 3 4438300 4.594631 estimated 1964\n",
"767 None mean 2 4438300 3.827774 estimated 1964\n",
"768 None mean 1 4438300 11.604913 estimated 1964\n",
"769 None mean 7 4438300 0.487042 estimated 1964\n",
"770 None mean 6 4438300 1.126887 estimated 1964\n",
"771 None mean 5 4438300 1.887602 estimated 1964\n",
"772 None mean 4 4438300 2.796485 estimated 1964\n",
"773 None mean 1 4438300 10.113504 estimated 1971\n",
"774 None mean 9 4438300 0.485910 estimated 2015\n",
"775 None mean 8 4438300 0.270100 estimated 2015\n",
"776 None mean 8 4438300 0.332512 estimated 1998\n",
"777 None mean 9 4438300 0.460599 estimated 1998\n",
"778 None mean 3 4438300 4.233689 estimated 2015\n",
"779 None mean 2 4438300 6.193927 estimated 2015\n",
"780 None mean 1 4438300 3.033989 estimated 2015\n",
"781 None mean 5 4438300 2.960416 estimated 1998\n",
"782 None mean 7 4438300 0.449120 estimated 2015\n",
"783 None mean 6 4438300 1.207406 estimated 2015\n",
"784 None mean 5 4438300 2.061431 estimated 2015\n",
"785 None mean 4 4438300 3.307589 estimated 2015\n",
"786 None mean 10 4438300 0.230487 estimated 1987\n",
"787 None mean 11 4438300 0.742353 estimated 1987\n",
"788 None mean 12 4438300 7.949836 estimated 1987\n",
"789 None mean 2 4438300 6.013943 estimated 1971\n",
"790 None mean 9 4438300 0.441267 estimated 1982\n",
"791 None mean 9 4438300 0.381862 estimated 1972\n",
"\n",
"[792 rows x 7 columns]\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" res = get_data(4438300)\n",
" # load into Pandas dataframe\n",
" if res:\n",
" df = json_normalize(res)\n",
" print df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
"""Example creating Pandas data frame from API return.
Dependencies (install with pip): requests, pandas"""
import os
import json
import requests
from pandas.io.json import json_normalize
# API endpoint
FLOW_URL = 'https://rivers.codefornature.org/api/data/flat/'
# You could assign your token also directly,
# please make sure NOT to commit a valid token to a GitHub repository
STATUS_CODES = {
200: 'Success',
400: 'Malformed query',
401: 'Not authorized',
404: 'Not found',
504: 'Timed out. Request smaller batch of data.'}
def get_data(comid):
"""Function retrieving data for a single stream segment
Args:
comid(int): Comid for stream segment
Returns:
dict containing data.
"""
payload = {'where': json.dumps({'stream_segment': comid})}
res = requests.get(FLOW_URL, params=payload)
print 'Requested URL:', res.url
print 'Status:', res.status_code
if res.status_code == 200:
return json.loads(res.text)['results']
try:
print STATUS_CODES[res.status_code]
return
except KeyError:
print 'Something is wrong.'
if __name__ == '__main__':
res = get_data(4438300)
if res:
df = json_normalize(res)
print df
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# requirements for running Python 2.7 examples
# install with
# pip install -r requirements.txt
jupyter==1.0.0
requests==2.13.0
pandas==0.19.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment