-
Notifications
You must be signed in to change notification settings - Fork 60
/
Streamflow_Eval.py
351 lines (258 loc) · 13.8 KB
/
Streamflow_Eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python
# coding: utf-8
#author: Ryan Johnson, PHD, Alabama Water Institute
#Date: 6-6-2022
'''
Run using the OWP_env:
https://www.geeksforgeeks.org/using-jupyter-notebook-in-virtual-environment/
https://github.com/NOAA-OWP/hydrotools/tree/main/python/nwis_client
https://noaa-owp.github.io/hydrotools/hydrotools.nwm_client.utils.html#national-water-model-file-utilities
will be benefitical for finding NWM reachs between USGS sites
'''
# Import the NWIS IV Client to load USGS site data
from hydrotools.nwis_client.iv import IVDataService
from hydrotools.nwm_client import utils
import pandas as pd
import numpy as np
import data
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import max_error
from sklearn.metrics import mean_absolute_percentage_error
import hydroeval as he
import dataretrieval.nwis as nwis
import streamstats
import geopandas as gpd
from IPython.display import display
import warnings
from progressbar import ProgressBar
import folium
import matplotlib
import mapclassify
warnings.filterwarnings("ignore")
class Reach_Eval():
def __init__(self, NWISsite, startDT, endDT, freq, cwd):
self = self
self.NWISsite = NWISsite
self.NWM_NWIS_df= utils.crosswalk(usgs_site_codes=self.NWISsite)
self.NWM_segment = self.NWM_NWIS_df.nwm_feature_id.values[0]
self.startDT = startDT
self.endDT = endDT
self.freq = freq
self.cwd = cwd
self.cms_to_cfs = 35.314666212661
#A function for accessing NWIS data and procesing to daily mean flow
def NWIS_retrieve(self):
# Retrieve data from a single site
print('Retrieving USGS site ', self.NWISsite, ' data')
service = IVDataService()
self.usgs_data = service.get(
sites=self.NWISsite,
startDT= self.startDT,
endDT=self.endDT
)
#Get Daily mean for NWM comparision
self.usgs_meanflow = pd.DataFrame(self.usgs_data.reset_index().groupby(pd.Grouper(key = 'value_time', freq = self.freq))['value'].mean())
self.usgs_meanflow = self.usgs_meanflow.reset_index()
#add key site information
#make obs data the same as temporal means
self.usgs_data = self.usgs_data.head(len(self.usgs_meanflow))
#remove obs streamflow
del self.usgs_data['value']
del self.usgs_data['value_time']
#connect mean temporal with other key info
self.usgs_meanflow = pd.concat([self.usgs_meanflow, self.usgs_data], axis=1)
self.usgs_meanflow = self.usgs_meanflow.rename(columns={'value_time':'Datetime', 'value':'USGS_flow','usgs_site_code':'USGS_ID', 'variable_name':'variable'})
self.usgs_meanflow = self.usgs_meanflow.set_index('Datetime')
#Get watershed information
#self.get_StreamStats()
#display(self.Catchment_Stats)
# A function for accessing NWM data and processing to daily mean flow
def NWM_retrieve(self):
print('Retrieving NWM reach ', self.NWM_segment, ' data')
self.nwm_predictions = data.get_nwm_data(self.NWM_segment, self.startDT, self.endDT)
self.NWM_meanflow = self.nwm_predictions.resample(self.freq).mean()*self.cms_to_cfs
self.NWM_meanflow = self.NWM_meanflow.reset_index()
self.NWM_meanflow = self.NWM_meanflow.rename(columns={'time':'Datetime', 'value':'Obs_flow','feature_id':'NWM_segment', 'streamflow':'NWM_flow', 'velocity':'NWM_velocity'})
self.NWM_meanflow = self.NWM_meanflow.set_index('Datetime')
def NWM_Eval(self):
#merge NWM and USGS
self.Evaluation = pd.concat([self.usgs_meanflow, self.NWM_meanflow], axis=1)
#remove rows with NA
self.Evaluation = self.Evaluation.dropna(axis = 0)
#create two plots, a hydrograph and a parity plot
discharge = 'Discharge ' + '('+ self.Evaluation['measurement_unit'][0]+')'
max_flow = max(max(self.Evaluation.USGS_flow), max(self.Evaluation.NWM_flow))
min_flow = min(min(self.Evaluation.USGS_flow), min(self.Evaluation.NWM_flow))
fig, ax = plt.subplots(1,2, figsize = (10,5))
ax[0].plot(self.Evaluation.index, self.Evaluation.USGS_flow, color = 'blue', label = 'USGS')
ax[0].plot(self.Evaluation.index, self.Evaluation.NWM_flow, color = 'orange', label = 'NWM')
ax[0].fill_between(self.Evaluation.index, self.Evaluation.NWM_flow, self.Evaluation.USGS_flow, where= self.Evaluation.NWM_flow >= self.Evaluation.USGS_flow, facecolor='orange', alpha=0.2, interpolate=True)
ax[0].fill_between(self.Evaluation.index, self.Evaluation.NWM_flow, self.Evaluation.USGS_flow, where= self.Evaluation.NWM_flow < self.Evaluation.USGS_flow, facecolor='blue', alpha=0.2, interpolate=True)
ax[0].set_xlabel('Datetime')
ax[0].set_ylabel(discharge)
ax[0].tick_params(axis='x', rotation = 45)
ax[0].legend()
ax[1].scatter(self.Evaluation.USGS_flow, self.Evaluation.NWM_flow, color = 'black')
ax[1].plot([min_flow, max_flow],[min_flow, max_flow], ls = '--', c='red')
ax[1].set_xlabel('Observed USGS (cfs)')
ax[1].set_ylabel('Predicted NWM (cfs)')
#calculate some performance metrics
r2 = r2_score(self.Evaluation.USGS_flow, self.Evaluation.NWM_flow)
rmse = mean_squared_error(self.Evaluation.USGS_flow, self.Evaluation.NWM_flow, squared=False)
maxerror = max_error(self.Evaluation.USGS_flow, self.Evaluation.NWM_flow)
MAPE = mean_absolute_percentage_error(self.Evaluation.USGS_flow, self.Evaluation.NWM_flow)*100
kge, r, alpha, beta = he.evaluator(he.kge, self.Evaluation.NWM_flow, self.Evaluation.USGS_flow)
print('The NWM demonstrates the following model performance')
print('R2 = ', r2)
print('RMSE = ', rmse, self.Evaluation['measurement_unit'][0])
print('Maximum error = ', maxerror, self.Evaluation['measurement_unit'][0])
print('Mean Absolute Percentage Error = ', MAPE, '%')
print('Kling-Gupta Efficiency = ', kge[0])
def get_StreamStats(self):
print('Calculating the summary statistics of the catchment')
NWISinfo = nwis.get_record(sites=self.NWISsite, service='site')
#Get site information for streamstats
lat, lon = NWISinfo['dec_lat_va'][0],NWISinfo['dec_long_va'][0]
ws = streamstats.Watershed(lat=lat, lon=lon)
NWISindex = ['NWIS_site_id', 'Drainage_area_mi2', 'Mean_Basin_Elev_ft', 'Perc_Forest', 'Perc_Develop',
'Perc_Imperv', 'Perc_Herbace', 'Perc_Slop_30', 'Mean_Ann_Precip_in', 'Ann_low_cfs', 'Ann_mean_cfs', 'Ann_hi_cfs']
#get stream statististics
self.Param="00060"
StartYr='1970'
EndYr='2021'
annual_stats = nwis.get_stats(sites=self.NWISsite,
parameterCd=self.Param,
statReportType='annual',
startDt=StartYr,
endDt=EndYr)
mean_ann_low = annual_stats[0].nsmallest(1, 'mean_va')
mean_ann_low = mean_ann_low['mean_va'].values[0]
mean_ann = np.round(np.mean(annual_stats[0]['mean_va']),0)
mean_ann_hi = annual_stats[0].nlargest(1, 'mean_va')
mean_ann_hi = mean_ann_hi['mean_va'].values[0]
try:
darea = ws.get_characteristic('DRNAREA')['value']
except (KeyError, ValueError):
darea = 'na'
try:
elev = ws.get_characteristic('ELEV')['value']
except (KeyError, ValueError):
elev = 'na'
try:
forest = ws.get_characteristic('FOREST')['value']
except (KeyError, ValueError):
forest = 'na'
try:
dev_area = ws.get_characteristic('LC11DEV')['value']
except (KeyError, ValueError):
dev_area = 'na'
try:
imp_area = ws.get_characteristic('LC11IMP')['value']
except (KeyError, ValueError):
imp_area = 'na'
try:
herb_area = ws.get_characteristic('LU92HRBN')['value']
except (KeyError, ValueError):
herb_area = 'na'
try:
perc_slope = ws.get_characteristic('SLOP30_10M')['value']
except (KeyError, ValueError):
perc_slope = 'na'
try:
precip = ws.get_characteristic('PRECIP')['value']
except (KeyError, ValueError):
precip = 'na'
#Put data into data frame and display
NWISvalues = [self.NWISsite,darea, elev,forest, dev_area, imp_area, herb_area, perc_slope, precip, mean_ann_low, mean_ann, mean_ann_hi]
Catchment_Stats = pd.DataFrame(data = NWISvalues, index = NWISindex)
self.Catchment_Stats = Catchment_Stats.T
display(self.Catchment_Stats)
#plot the watershed
title = 'Catchment for USGS station: '+self.NWISsite
poly = gpd.GeoDataFrame.from_features(ws.boundary["features"], crs="EPSG:4326")
df = poly.to_crs(epsg=3857)
self.WatershedMap = df.explore(color = 'yellow', tiles = 'Stamen Terrain')
def get_USGS_site_info(self, state):
#url for state usgs id's
url = 'https://waterdata.usgs.gov/'+state+'/nwis/current/?type=flow&group_key=huc_cd'
NWIS_sites = pd.read_html(url)
NWIS_sites = pd.DataFrame(np.array(NWIS_sites)[1]).reset_index(drop = True)
cols = ['StationNumber', 'Station name','Date/Time','Gageheight, feet', 'Dis-charge, ft3/s']
self.NWIS_sites = NWIS_sites[cols].dropna()
self.NWIS_sites = self.NWIS_sites.rename(columns ={'Station name':'station_name',
'Gageheight, feet': 'gageheight_ft',
'Dis-charge, ft3/s':'Discharge_cfs'})
self.NWIS_sites = self.NWIS_sites[self.NWIS_sites.gageheight_ft != '--']
self.NWIS_sites = self.NWIS_sites.set_index('StationNumber')
# Remove unnecessary site information
for i in self.NWIS_sites.index:
if len(str(i)) > 8:
self.NWIS_sites = self.NWIS_sites.drop(i)
#remove when confirmed it works
# NWIS_sites = NWIS_sites[2:3]
site_id = self.NWIS_sites.index
#set up Pandas DF for state streamstats
Streamstats_cols = ['NWIS_siteid', 'Drainage_area_mi2', 'Mean_Basin_Elev_ft', 'Perc_Forest', 'Perc_Develop',
'Perc_Imperv', 'Perc_Herbace', 'Perc_Slop_30', 'Mean_Ann_Precip_in']
self.State_NWIS_Stats = pd.DataFrame(columns = Streamstats_cols)
pbar = ProgressBar()
for site in pbar(site_id):
siteinfo = self.NWIS_sites['station_name'][site]
print('Calculating the summary statistics of the catchment for ', siteinfo, ', USGS: ',site)
NWISinfo = nwis.get_record(sites=site, service='site')
lat, lon = NWISinfo['dec_lat_va'][0],NWISinfo['dec_long_va'][0]
ws = streamstats.Watershed(lat=lat, lon=lon)
NWISindex = ['NWIS_site_id', 'NWIS_sitename', 'Drainage_area_mi2', 'Mean_Basin_Elev_ft', 'Perc_Forest', 'Perc_Develop',
'Perc_Imperv', 'Perc_Herbace', 'Perc_Slop_30', 'Mean_Ann_Precip_in', 'Ann_low_cfs', 'Ann_mean_cfs', 'Ann_hi_cfs']
#get stream statististics
self.Param="00060"
StartYr='1970'
EndYr='2021'
annual_stats = nwis.get_stats(sites=self.NWISsite,
parameterCd=self.Param,
statReportType='annual',
startDt=StartYr,
endDt=EndYr)
mean_ann_low = annual_stats[0].nsmallest(1, 'mean_va')
mean_ann_low = mean_ann_low['mean_va'].values[0]
mean_ann = np.round(np.mean(annual_stats[0]['mean_va']),0)
mean_ann_hi = annual_stats[0].nlargest(1, 'mean_va')
mean_ann_hi = mean_ann_hi['mean_va'].values[0]
try:
darea = ws.get_characteristic('DRNAREA')['value']
except (KeyError, ValueError):
darea = 'na'
try:
elev = ws.get_characteristic('ELEV')['value']
except (KeyError, ValueError):
elev = 'na'
try:
forest = ws.get_characteristic('FOREST')['value']
except (KeyError, ValueError):
forest = 'na'
try:
dev_area = ws.get_characteristic('LC11DEV')['value']
except (KeyError, ValueError):
dev_area = 'na'
try:
imp_area = ws.get_characteristic('LC11IMP')['value']
except (KeyError, ValueError):
imp_area = 'na'
try:
herb_area = ws.get_characteristic('LU92HRBN')['value']
except (KeyError, ValueError):
herb_area = 'na'
try:
perc_slope = ws.get_characteristic('SLOP30_10M')['value']
except (KeyError, ValueError):
perc_slope = 'na'
try:
precip = ws.get_characteristic('PRECIP')['value']
except (KeyError, ValueError):
precip = 'na'
NWISvalues = [site,siteinfo, darea, elev,forest, dev_area, imp_area, herb_area, perc_slope, precip, mean_ann_low, mean_ann, mean_ann_hi]
Catchment_Stats = pd.DataFrame(data = NWISvalues, index = NWISindex).T
self.State_NWIS_Stats = self.State_NWIS_Stats.append(Catchment_Stats)
State_NWIS_Stats.to_csv(self.cwd+'/State_NWIS_StreamStats/'+state+'StreamStats.csv')