Skip to content

Commit 0652299

Browse files
committed
Test for elevation after iteration
1 parent 50b0bfa commit 0652299

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

utils/Wetlands.py

+54-20
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ def __init__(self, path_results):
88
super(Wetlands, self).__init__(path_results)
99
self.path_res = path_results
1010
self.mat_dtw = self._make_dtw()
11-
self.df_subs, self.mat_wetlands = self._ccap_wetlands()
12-
self.mask_wet = np.isnan(self.mat_wetlands.reshape(-1))
11+
self.df_wets, self.mat_wets = self._ccap_wetlands()
12+
self.mask_wet = np.isnan(self.mat_wets.reshape(-1))
1313
# highest values (closest to 0) = most hours with lowest DTW
1414
self.mat_dtw_sum = (self.mat_dtw * -1).sum(axis=1)
15+
self.summer = pd.date_range('2012-06-01-00', '2012-09-01-00', freq='h')
1516

1617
def _ccap_wetlands(self):
1718
""" Get CCAP wetlands df and full matrix grid """
@@ -22,7 +23,9 @@ def _ccap_wetlands(self):
2223
mat_landcover = res_base.fill_grid(ser_landcover, fill_value=-1)
2324
mat_wetlands = np.where(((mat_landcover < 13) | (mat_landcover > 18)),
2425
np.nan, mat_landcover)
25-
return (df_subs, mat_wetlands)
26+
27+
df_wet = df_subs[((df_subs.Majority >= 13) & (df_subs.Majority < 19))].iloc[:, :3]
28+
return (df_wet, mat_wetlands)
2629

2730
def _make_dtw(self, slr=0.0):
2831
""" dtw, all locations all times """
@@ -43,13 +46,13 @@ def indicator_all(self, cutoff=-2500, show=False):
4346
mat_indicator = np.where(self.mat_dtw_sum <= cutoff, np.nan, self.mat_dtw_sum)
4447
if show:
4548
print ('Indicated cells: {}'.format(np.count_nonzero(~np.isnan(mat_indicator))))
46-
print ('Wetland cells: {}'.format(np.count_nonzero(~np.isnan(self.mat_wetlands))))
49+
print ('Wetland cells: {}'.format(np.count_nonzero(~np.isnan(self.mat_wets))))
4750
return mat_indicator
4851

4952
mat_ind = mat_indicator[~self.mask_wet & ~np.isnan(mat_indicator)]
5053

5154
df_ind = pd.DataFrame(mat_indicator.reshape(-1)).dropna()
52-
df_wet = pd.DataFrame(self.mat_wetlands.reshape(-1)).dropna()
55+
df_wet = pd.DataFrame(self.mat_wets.reshape(-1)).dropna()
5356

5457
## get count of how many correct
5558
count_correct = (len(mat_ind))
@@ -63,30 +66,32 @@ def indicator_all(self, cutoff=-2500, show=False):
6366
# print ('Percent corretly identified: {} %\n'.format(round(performance, 3)))
6467
return (performance, count_correct, count_incorrect)
6568

66-
def make_indicator(self, dtw_inc=0.01, hrs_per=50, seasonal=False):
69+
def make_indicator(self, dtw_inc=0.1, hrs_per=50, seasonal=False):
6770
"""
6871
Make an indicator by iterating over depth to water and hours at that dtw
6972
dtw_inc = dtw increment, use 0.01 for increased precision (expensive)
7073
hrs_per = percent of total hours to begin minimum threshold
7174
seasonal = search just summer?
7275
"""
73-
start = time.time()
76+
start = time.time()
7477
### select wetland from all dtw information
7578
mat_wet_dtw = self.mat_dtw[~self.mask_wet]
7679
mat_nonwet_dtw = self.mat_dtw[self.mask_wet]
7780
mat_dry_dtw = mat_nonwet_dtw[~np.isnan(mat_nonwet_dtw)].reshape(
7881
-1, mat_nonwet_dtw.shape[1])
79-
names = ['dtw_hrs_wet_dry.npy', 'dtw_hrs_wet_dry.df']
82+
83+
84+
85+
names = ['dtw_hrs_wet_dry1.npy', 'dtw_hrs_wet_dry1.df']
8086
## truncate for just summer
8187
if seasonal:
82-
summer = pd.date_range('2012-06-01-00', '2012-09-01-00', freq='h')
8388
df_wet_dtw1 = pd.DataFrame(mat_wet_dtw.T, index=self.ts_yr_hr)
84-
df_wet_dtw = pd.DataFrame(mat_wet_dtw.T, index=self.ts_yr_hr).loc[summer, :]
89+
df_wet_dtw = pd.DataFrame(mat_wet_dtw.T, index=self.ts_yr_hr).loc[self.summer, :]
8590

86-
df_dry_dtw = pd.DataFrame(mat_dry_dtw.T, index=self.ts_yr_hr).loc[summer, :]
91+
df_dry_dtw = pd.DataFrame(mat_dry_dtw.T, index=self.ts_yr_hr).loc[self.summer, :]
8792
mat_wet_dtw = df_wet_dtw.values.T
8893
mat_dry_dtw = df_dry_dtw.values.T
89-
names = ['dtw_hrs_wet_dry_summer.npy', 'dtw_hrs_wet_dry_summer.df']
94+
names = ['dtw_hrs_wet_dry_summer1.npy', 'dtw_hrs_wet_dry_summer1.df']
9095

9196
print ('Finding optimum criteria; will take a bit')
9297
dtw_tests = np.arange(0, 1, dtw_inc)
@@ -151,6 +156,32 @@ def apply_indicator(self, seasonal=False):
151156
### best for summer is ~ 61% wetlands and 34.4% of drylands
152157
BB.print_all(df_new)
153158

159+
def find_cells(self, dtw_thresh=0.05, hrs_thresh=4442, z_thresh=1.0):
160+
""" Find locations that meet the threshold conditions """
161+
df_dtw = pd.DataFrame(self.mat_dtw, columns=self.ts_yr_hr)
162+
163+
## number of hrs below dtw threshold
164+
df_dtw['dtw_counts'] = ((df_dtw<=dtw_thresh).sum(axis=1))
165+
166+
## put true if passed hrs threshold
167+
df_dtw['wet_test'] = df_dtw.dtw_counts.apply(lambda x: True if x >= hrs_thresh else False)
168+
169+
## attach elevation data ; zones col is one indexed in df_subs
170+
df_dtw.index += 10001
171+
df_merged = df_dtw.merge(self.df_swmm.loc[:, ['Esurf', 'Majority']],
172+
left_index=True, right_index=True, how='outer')
173+
df_passed = df_merged[df_merged['wet_test']]
174+
175+
df_low = df_passed[df_passed.Esurf <= z_thresh]
176+
df_passed_wets = df_low[df_low.index.isin(self.df_wets.Zone)]
177+
df_passed_drys = df_low[~df_low.index.isin(self.df_wets.Zone)]
178+
179+
print ('Wets: {}'.format(df_passed_wets.shape[0]))
180+
print ('Drys: {}'.format(df_passed_drys.shape[0]))
181+
182+
183+
184+
154185
def optimize(self, increment=1):
155186
""" Maximize the percent correctly identiified """
156187
optimal = []
@@ -188,7 +219,7 @@ def plot_indicators(self, cut=-2500):
188219
axe[0].hist(self.mat_dtw_sum[~mask], bins=bins)
189220
axe[1].imshow(self.mat_dtw_sum.reshape(74, -1), cmap=plt.cm.jet)
190221
axe[2].imshow(mat_highest.reshape(74, -1), cmap=plt.cm.jet)
191-
axe[3].imshow(self.mat_wetlands.reshape(74, -1), cmap=plt.cm.jet)
222+
axe[3].imshow(self.mat_wets.reshape(74, -1), cmap=plt.cm.jet)
192223

193224
titles = ['Hist of summed negative dtws', 'Total annual DTW',
194225
'Locs of cells above dtw cutoff: {}'.format(cut), 'Locs of wetlands cells']
@@ -204,11 +235,10 @@ def dtw_wet_all(self, transpose=False):
204235
""" Separate ALL dtw / cells / times by wetlands/nonwetland """
205236
## convert to dtw for subsetting
206237
df_dtw = pd.DataFrame(self.mat_dtw)
207-
df_wet = self.df_subs[((self.df_subs.Majority >= 13) & (self.df_subs.Majority < 19))].iloc[:, :3]
208238

209239
df_dtw.index = range(10000, 13774)
210-
df_wets = df_dtw[df_dtw.index.isin(df_wet.Zone)]
211-
df_drys = df_dtw[~df_dtw.index.isin(df_wet.Zone)].dropna()
240+
df_wets = df_dtw[df_dtw.index.isin(self.df_wets.Zone)]
241+
df_drys = df_dtw[~df_dtw.index.isin(self.df_wets.Zone)].dropna()
212242
# print (df_wets.shape)
213243
# print (df_drys.shape)
214244
if transpose:
@@ -252,14 +282,13 @@ def comp_histograms(self, kind='avg', plot=True):
252282
### probably useless
253283
def dtw_wet_avg_ann(self):
254284
""" Subset dtw df (all cells, annual average to just wetland cells """
255-
df_wet = self.df_subs[((self.df_subs.Majority >= 13) & (self.df_subs.Majority < 19))].iloc[:, :3]
256285

257286
## avg annual dtw
258287
df_dtw = dtw(self.path_res).df_year
259288
df_dtw.columns = [str(col) for col in df_dtw.columns]
260289

261-
df_wets = df_dtw[df_dtw.index.isin(df_wet.Zone)]
262-
df_drys = df_dtw[~df_dtw.index.isin(df_wet.Zone)].dropna()
290+
df_wets = df_dtw[df_dtw.index.isin(self.df_wets.Zone)]
291+
df_drys = df_dtw[~df_dtw.index.isin(self.df_wets.Zone)].dropna()
263292
return df_wets, df_drys
264293

265294

@@ -268,4 +297,9 @@ def dtw_wet_avg_ann(self):
268297
res = Wetlands(PATH_res)
269298
# res.optimize(increment=10)
270299
# res.make_indicator(dtw_inc=0.01, hrs_per=50, seasonal=True)
271-
res.apply_indicator(seasonal=True)
300+
# res.apply_indicator(seasonal=False)
301+
302+
## nonseasonal indicators: dtw < 0.05; hrs_thresh>4443 --------- best
303+
## seasonal indicators : dtw < 0.17; hrs_thresh > 1211
304+
305+
res.find_cells()

0 commit comments

Comments
 (0)