forked from sophia-khan/GEE_Workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet_StreamStats.py
162 lines (122 loc) · 4.84 KB
/
Get_StreamStats.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import dataretrieval.nwis as nwis
##https://streamstats-python.readthedocs.io/en/latest/gallery_vignettes/plot_get_characteristics.html
import streamstats
import pandas as pd
import numpy as np
from progressbar import ProgressBar
def get_USGS_site_info(site_ids):
#set up Pandas DF for state streamstats
Streamstats_cols = ['NWIS_siteid','Lat', 'Long', 'Drainage_area_mi2', 'Mean_Basin_Elev_ft', 'Perc_Forest', 'Perc_Develop',
'Perc_Imperv', 'Perc_Herbace', 'Perc_Slop_30', 'Mean_Ann_Precip_in']
NWIS_Stats = pd.DataFrame(columns = Streamstats_cols)
print('Calculating NWIS streamflow id characteristics for ', len(site_ids), 'sites')
pbar = ProgressBar()
for site in pbar(site_ids):
print('NWIS site: ', site)
#try:
NWISinfo = nwis.get_record(sites=site, service='site')
lat, lon = NWISinfo['dec_lat_va'][0],NWISinfo['dec_long_va'][0]
#This sources the prestored data
try:
ws = streamstats.Watershed(lat=lat, lon=lon)
except:
print('502 error, StreamStats down, using backup files')
ws = pd.read_csv('./Data/StreamStats_All.csv')
ws['NWIS_site_id'] = ws['NWIS_site_id'].astype(str)
ws = ws[ws.NWIS_site_id == site]
NWISindex = ['NWIS_site_id', 'Lat', 'Long', 'Drainage_area_mi2', 'Mean_Basin_Elev_ft', 'Perc_Forest', 'Perc_Develop',
'Perc_Imperv', 'Perc_Herbace', 'Perc_Slop_30', 'Mean_Ann_Precip_in']
print('Retrieving Drainage Area')
try:
darea = ws.get_characteristic('DRNAREA')['value']
except KeyError:
darea = np.nan
except ValueError:
darea = np.nan
except AttributeError:
darea = ws['Drainage_area_mi2'].values[0]
print('Retrieving Mean Catchment Elevation')
try:
elev = ws.get_characteristic('ELEV')['value']
except KeyError:
elev = np.nan
except ValueError:
elev = np.nan
except AttributeError:
elev = ws['Mean_Basin_Elev_ft'].values[0]
print('Retrieving Catchment Land Cover Information')
try:
forest = ws.get_characteristic('FOREST')['value']
except KeyError:
forest = np.nan
except ValueError:
forest = np.nan
except AttributeError:
forest = ws['Perc_Forest'].values[0]
try:
dev_area = ws.get_characteristic('LC11DEV')['value']
except KeyError:
dev_area = np.nan
except ValueError:
dev_area = np.nan
except AttributeError:
dev_area = ws['Perc_Develop'].values[0]
try:
imp_area = ws.get_characteristic('LC11IMP')['value']
except KeyError:
imp_area = np.nan
except ValueError:
imp_area = np.nan
except AttributeError:
imp_area = ws['Perc_Imperv'].values[0]
try:
herb_area = ws.get_characteristic('LU92HRBN')['value']
except KeyError:
herb_area = np.nan
except ValueError:
herb_area = np.nan
except AttributeError:
herb_area = ws['Perc_Herbace'].values[0]
print('Retrieving Catchment Topographic Complexity')
try:
perc_slope = ws.get_characteristic('SLOP30_10M')['value']
except KeyError:
perc_slope = np.nan
except ValueError:
perc_slope = np.nan
except AttributeError:
perc_slope = ws['Perc_Slop_30'].values[0]
print('Retrieving Catchment Average Precip')
try:
precip = ws.get_characteristic('PRECIP')['value']
except KeyError:
precip = np.nan
except ValueError:
precip = np.nan
except AttributeError:
precip = ws['Mean_Ann_Precip_in'].values[0]
NWISvalues = [site,
lat,
lon,
darea,
elev,forest,
dev_area,
imp_area,
herb_area,
perc_slope,
precip]
print(NWISvalues)
Catchment_Stats = pd.DataFrame(data = NWISvalues, index = NWISindex).T
NWIS_Stats = NWIS_Stats.append(Catchment_Stats)
#except:
# print('Taking three minute break to prevent the blocking of IP Address')
# time.sleep(181)
colorder =['NWIS_site_id', 'Lat', 'Long', 'Drainage_area_mi2', 'Mean_Basin_Elev_ft', 'Perc_Forest',
'Perc_Develop','Perc_Imperv', 'Perc_Herbace', 'Perc_Slop_30', 'Mean_Ann_Precip_in']
NWIS_Stats = NWIS_Stats[colorder]
NWIS_Stats.reset_index(drop = True, inplace = True)
return NWIS_Stats
# In[ ]: