Created
April 30, 2014 17:18
-
-
Save dharhas/e93ac930e32e9c80c738 to your computer and use it in GitHub Desktop.
simple example of using ulmo and pandas to download nwis daily streamflow data and calculate historic mean discharge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from ulmo.usgs import nwis | |
# download and cache site data (this will take a long time the first time) | |
# currently downloads all available parameters | |
nwis.hdf5.update_site_data('06043500') | |
# read daily mean discharge data from cache (statistics code 00003) | |
data = nwis.hdf5.get_site_data('06043500', parameter_code='00060:00003')['00060:00003'] | |
# convert data to a pandas dataframe | |
df = pd.DataFrame(data['values']).drop(['last_checked','last_modified','qualifiers'], axis=1).set_index('datetime') | |
df.value = df.value.apply(np.float) | |
df.index = pd.to_datetime(df.index).to_period('D') | |
# mark bad data as NaN | |
df[df.values == -999999] = np.nan | |
# group the data by month, day & calculate means | |
daily_groups = df.groupby((lambda d: d.month, lambda d: d.day)) | |
means = daily_groups.mean() | |
print 'historic daily mean on March 23rd is %s' % means.ix[3,23].value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment