Skip to content

Commit b359bde

Browse files
committed
Plot Wetland Changes
1 parent 81f7f11 commit b359bde

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

utils/Wetlands.py

+51-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, path_results, dtw_inc=0.01, hrs_per=50, z_thresh=3.75):
1616
# highest values (closest to 0) = most hours with lowest DTW
1717
self.mat_dtw_sum = (self.mat_dtw * -1).sum(axis=1)
1818
self.summer = pd.date_range('2012-06-01-00', '2012-09-01-00', freq='h')
19-
19+
2020
def _ccap_wetlands(self):
2121
""" Get CCAP wetlands df and full matrix grid """
2222
df_subs = pd.read_csv(op.join(self.path_data, 'SWMM_subs.csv'))
@@ -151,7 +151,7 @@ def apply_indicator(self, seasonal=False):
151151
### best for summer is ~ 61% wetlands and 34.4% of drylands
152152
BB.print_all(df_new)
153153

154-
def find_cells(self, dtw_thresh=0.01, hrs_thresh=4442):
154+
def find_cells(self, dtw_thresh=0.05, hrs_thresh=4442):
155155
""" Find locations that meet the threshold conditions """
156156
df_dtw = pd.DataFrame(self.mat_dtw, columns=self.ts_yr_hr)
157157

@@ -167,12 +167,53 @@ def find_cells(self, dtw_thresh=0.01, hrs_thresh=4442):
167167
left_index=True, right_index=True, how='outer')
168168
df_passed = df_merged[df_merged['wet_test']]
169169

170-
df_low = df_passed[df_passed.Esurf <= z_thresh]
171-
df_passed_wets = df_low[df_low.index.isin(self.df_wets.Zone)]
172-
df_passed_drys = df_low[~df_low.index.isin(self.df_wets.Zone)]
170+
df_low = df_passed[df_passed.Esurf <= self.z_thresh]
171+
# df_passed_wets = df_low[df_low.index.isin(self.df_wets.Zone)]
172+
# df_passed_drys = df_low[~df_low.index.isin(self.df_wets.Zone)]
173+
df_low['dry'] = df_low.index.map(lambda x: -1 if x in self.df_wets.loc[:, 'Zone'].values else 0)
174+
175+
print ('Wets: {}'.format(df_low[df_low['dry']<0].shape[0]))
176+
print ('Drys: {}'.format(df_low[df_low['dry']==0].shape[0]))
177+
# return df_passed_wets, df_passed_drys
178+
return df_low
179+
180+
181+
def plot_wets_drys(self):
182+
df_lows = self.find_cells(dtw_thresh=0.05, hrs_thresh=4442)
183+
184+
## change value for cells not considered (visualization purposes)
185+
df_subs = pd.DataFrame(index=self.df_swmm.index)
186+
df_subs['dry'] = df_subs.index.map(lambda x: df_lows.loc[x, 'dry']
187+
if x in df_lows.index.values else 1)
173188

174-
print ('Wets: {}'.format(df_passed_wets.shape[0]))
175-
print ('Drys: {}'.format(df_passed_drys.shape[0]))
189+
## converts dfs to matrixes
190+
mat_lows = res_base.fill_grid(df_subs.dry, fill_value=-2)
191+
192+
fig, axes = plt.subplots(ncols=3, sharey=True)
193+
axe = axes.ravel()
194+
# cm = plt.get_cmap('coolwarm')
195+
cm = mpl.colors.ListedColormap(['black', 'blue', 'green', 'gray'])
196+
# axe = axes.ravel()
197+
for i, ax in enumerate(axes.ravel()):
198+
im = axe[i].imshow(mat_lows.reshape(self.nrows, self.ncols), cmap=cm)
199+
200+
axe[i].set(title='SLR: {}'.format(0.0),
201+
xlabel='# Wetlands: {}'.format(
202+
df_lows[((df_lows['dry']==0) | (df_lows['dry']==1))].shape[0]),
203+
)
204+
205+
cbar_ax = fig.add_axes([0.945, 0.18, 0.025, 0.62])
206+
207+
cbar = fig.colorbar(im, cbar_ax, spacing='proportional')
208+
cbar.ax.get_yaxis().set_ticks([])
209+
cbar.ax.set_xlabel('Legend', fontsize=12, labelpad=10)
210+
cbar.ax.xaxis.set_label_position('top')
211+
for j, lab in enumerate(['inactive','wetland (CCAP)','wetland(nonCCAP)','upland']):
212+
cbar.ax.text(.5, (2 * j + 1) / 8.0, lab, ha='center', va='center',
213+
bbox=dict(facecolor='white', alpha=0.75), rotation=90)
214+
215+
fig.subplots_adjust(left=0.05, right=0.915, wspace=0.25, hspace=0.25)
216+
plt.show()
176217

177218
def optimize(self, increment=1):
178219
""" Maximize the percent correctly identiified """
@@ -248,6 +289,7 @@ def indicator_all(self, cutoff=-2500, show=False):
248289
performance = (count_correct - count_incorrect) / float(np.count_nonzero(~mask_wet)) * 100
249290
# print ('Percent corretly identified: {} %\n'.format(round(performance, 3)))
250291
return (performance, count_correct, count_incorrect)
292+
251293
### probably useless
252294
def dtw_wet_all(self, transpose=False):
253295
""" Separate ALL dtw / cells / times by wetlands/nonwetland """
@@ -314,10 +356,11 @@ def dtw_wet_avg_ann(self):
314356
'WNC', 'Wetlands_Paper', 'Results_Default')
315357
res = Wetlands(PATH_res, dtw_inc=0.01, hrs_per=50, z_thresh=3.75)
316358
# res.optimize(increment=10)
317-
res.make_indicator(masked=True, seasonal=False)
359+
# res.make_indicator(masked=True, seasonal=False)
318360
# res.apply_indicator(seasonal=False)
319361

320362
## nonseasonal indicators: dtw < 0.05; hrs_thresh>4443 --------- best
321363
## seasonal indicators : dtw < 0.17; hrs_thresh > 1211
322364

323365
# res.find_cells()
366+
res.plot_wets_drys()

0 commit comments

Comments
 (0)