Skip to content

Instantly share code, notes, and snippets.

@dharhas
Created April 30, 2014 17:18
Show Gist options
  • Save dharhas/e93ac930e32e9c80c738 to your computer and use it in GitHub Desktop.
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
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