-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPastas_ModelGraphs_1950-2020.py
439 lines (346 loc) · 16.2 KB
/
Pastas_ModelGraphs_1950-2020.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# ##############################################################################
"""Developing Pastas models for different wells in Bangkok, Thailand.
A well nest at a given location may have 1-4 wells
Simulating groundwater levels
Calibration period: typically from 1978-2020 (based on data
availability)
Inputs: Basin-wide Pumping
Outputs: Pastas models (.pas files), graphs
Article Title: Hybrid data-driven, physics-based modeling of ground-
water and subsidence with application to Bangkok, Thailand
Jenny Soonthornrangsan 2023
TU Delft
"""
# ##############################################################################
###############################################################################
# import statements
###############################################################################
# Importing packages and libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pastas as ps
import os
import sys
import warnings
# Bangkok Subsidence Model Package
import bkk_sub_gw
# Importing script for pre-processing Thai GW data
import main_functions as mfs
# Ignoring Pastas warnings
warnings.simplefilter(action="ignore", category=FutureWarning)
# Changing current directory to locaiton of python script
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# %%###########################################################################
# Plotting settings
###############################################################################
plt.rc("font", size=10) # controls default text size
plt.rc("axes", titlesize=10) # fontsize of the title
plt.rc("axes", labelsize=7) # fontsize of the x and y labels
plt.rc("xtick", labelsize=7) # fontsize of the x tick labels
plt.rc("ytick", labelsize=7) # fontsize of the y tick labels
plt.rc("legend", fontsize=6) # fontsize of the legend
# %%###########################################################################
# Pastas settings
###############################################################################
# If saving model, save_model = 1
save_model = 0
# If importing previous saved models, import_model = 1
import_model = 1
# If saving graphs, save_graph = 1
save_graph = 1
# Publication graphs
paper_graph = 1
# Additional figures
add_graph = 0
# Folder to save/import graph and model
modelpath = os.path.abspath("models")
figpath = os.path.abspath("figures")
pumppath = os.path.join(os.path.abspath("inputs"), "BasinPumping.xlsx")
pumpsheet = "EstTotalPump_54-60_Int50"
# If running with pumping
# If basin wide pumping set pump_basin_flag to 1
pump_basin_flag = 1
# Pumping response function
pump_rfunc = ps.Gamma()
# Calibration period
calitime_min = "1978"
calitime_max = "2005"
# Solver
# Options: ps.LmfitSolve, ps.LeastSquares
# solver = ps.LeastSquares()
# Noise model
noise_TF = True
# Option to run all well nests, set flag to 1
list_wellnest_flag = 1
# If running only one well nest, set flag to 0, WellNest Name
Wellnest_name = ["LCBKK013"]
# Getting a list of all the wells
# Total path
tot_path = os.path.abspath("inputs")
# All well nest list
if list_wellnest_flag == 1:
files = os.listdir(tot_path)
files = [i.replace(".xlsx", "") for i in files
if i.startswith("LC") and "_" not in i]
else:
files = Wellnest_name
###############################################################################
# Creating/Importing and Plotting Pastas Model
###############################################################################
# For each well nest
for Wellnest_name in files:
# Only open figures for each well nest
plt.close("all")
# Saving results for each well nest
models = []
time_mins = []
time_maxs = []
well_names = []
# Reading in groundwater data
full_path = os.path.join(tot_path, Wellnest_name + ".xlsx")
data = pd.read_excel(full_path, skiprows=3)
# For all wells in well nest
for wells in data.columns[-(len(data.columns)-2):]:
# Name of well as a string
well_name = wells
#######################################################################
# Creating Pastas Model
#######################################################################
# If not importing, then creating new models
if import_model == 0:
# Preprocessing head data
# Inputing in raw data for well nest and well name
# Output: head data relative to 0 for the entire well nest
# head data relative to 0 m for specific well
all_head_data, gw_well_head = mfs.GW_Data_Process(data,
well_name)
print("\nGW obs imported for " + Wellnest_name + " and " +
well_name + "\n")
# CORRECTING GW HEAD DATA TO LAND SURFACE (COASTAL DEM 2.1)
# Reading in land surface elevation for each well nest
landsurf_path = os.path.join(tot_path,
"LandSurfElev_GWWellLocs.xlsx")
landsurf_data = pd.read_excel(landsurf_path,
sheet_name="2.1",
usecols="C:F",
index_col=0)
# Correcting head relative to 0 m to be head relative to a
# global datum.
# Corrected by adding land surface elevation values to
# head values relative to 0 m
gw_well_head.Head += (landsurf_data.RASTERVALU.loc[Wellnest_name])
# Saving years and annual average heads
gw_well_head["year"] = gw_well_head.index.year # Saving year
gw_year = gw_well_head.groupby(gw_well_head["year"]).mean()
gw_year["Date"] = pd.to_datetime(gw_year.index, format="%Y")
gw_year.index = gw_year.Date # Setting index as date
gw_year["year"] = gw_year.index.year # Saving year
# Setting time min and time max to first and last obs year
time_min = str(gw_year[gw_year.Head.notna()].year[0])
time_max = str(gw_year[gw_year.Head.notna()].year[-1])
# Gets rid of data not within min and max time
gw_year = gw_year[(gw_year["year"] >= int(time_min)) &
(gw_year["year"] <= int(time_max))]
# If GW well does not have data within time period, skips this well
if gw_year.Head.isnull().all():
print("\nSkipping ", well_name,
"because no head values in time period\n\n\n")
continue
# Initializing model
model = ps.Model(gw_well_head.Head)
print("\nModel initialized for GW\n")
# Initial steady state heads estimate for d constant
# Steady state heads already realtive to same global datum
# as land surface elevation
SS_path = os.path.join(tot_path,
"SS_Head_GWWellLocs.xlsx")
SS_data = pd.read_excel(SS_path,
sheet_name="SS_Py",
index_col=0)
# Getting steady state heads according to aquifer
if "BK" in well_name:
initial_d = SS_data.loc[Wellnest_name, "BK"]
elif "PD" in well_name:
initial_d = SS_data.loc[Wellnest_name, "PD"]
elif "NL" in well_name:
initial_d = SS_data.loc[Wellnest_name, "NL"]
elif "NB" in well_name:
initial_d = SS_data.loc[Wellnest_name, "NB"]
# Setting d parameter to SS heads and to vary +/- initial
# estimates
model.set_parameter(name="constant_d",
initial=initial_d,
pmin=initial_d-10,
pmax=initial_d+10,
vary=True)
###################################################################
# Adding pumping data
###################################################################
if pump_basin_flag == 1:
# Daily interpolated and estimated pumping rates for the basin
# from simulated (Chula report)
EstTotPump = pd.read_excel(pumppath, sheet_name=pumpsheet,
index_col=0, parse_dates=["Date"])
# Creating stress model
EstTotPump_ = ps.StressModel(EstTotPump.Pump, rfunc=pump_rfunc,
name="well", settings="well",
up=False)
# Adding stress model
model.add_stressmodel(EstTotPump_)
print("\nPumping obs added basin-wide\n")
###################################################################
# Solving/Saving Pastas Model
###################################################################
# Try running model
try:
print("Running....")
# If noise model True
if noise_TF:
# First run is not with noise model
# Gets first parameter estimates
# Warm up is 30 years
model.solve(tmin=calitime_min, tmax=calitime_max,
report=False, noise=False,
solver=ps.LeastSquares(), warmup=365*30)
# Second run with noise model using initial
# parameters as the calibrated parameters from
# first run
model.solve(tmin=calitime_min, tmax=calitime_max,
initial=False, report=False,
noise=noise_TF, solver=ps.LeastSquares(),
warmup=365*30)
# Noise model False
else:
# First run is not with noise model
# Gets first parameter estimates
# Warm up is 30 years
model.solve(tmin=calitime_min, tmax=calitime_max,
report=False, noise=False,
solver=ps.LeastSquares(), warmup=365*30)
# Second run with noise model using initial
# parameters as the calibrated parameters from
# first run
model.solve(tmin=calitime_min, tmax=calitime_max,
initial=False, report=False,
noise=noise_TF, solver=ps.LeastSquares(),
warmup=365*30)
# If time series out of bounds
except ValueError:
print("Time series out of bounds.\nCannot run model")
sys.exit()
# If saving model
if save_model == 1:
model.to_file(modelpath + "/" + Wellnest_name + "_" +
well_name + "_GW_" + calitime_min + "_" + calitime_max +
"_model.pas")
#######################################################################
# Importing Pastas Model
#######################################################################
# If importing Pastas model
else:
# Model files
modelfiles = os.listdir(modelpath)
# If file exists:
try:
# Load existing model
wellmodel = [s for s in modelfiles
if np.logical_and(Wellnest_name in s,
well_name in s)][0]
model = ps.io.load(modelpath + "/" + wellmodel)
# Gets time min and max from file name
time_min = wellmodel[wellmodel.find("_1")+1:wellmodel.find("_1")+5]
time_max = wellmodel[wellmodel.find("_2")+1:wellmodel.find("_2")+5]
# Saving optimal parameters before deleting stress
optiparam = model.parameters["optimal"]
stdparam = model.parameters["stderr"]
# Deleting stress
model.del_stressmodel("well")
# Adding new pumping stress time series
# If the same pumping stress time series, then
# optimal parameters are the same
EstTotPump = pd.read_excel(pumppath, sheet_name=pumpsheet,
index_col=0, parse_dates=["Date"])
EstTotPump_ = ps.StressModel(EstTotPump.Pump, rfunc=pump_rfunc,
name="well", settings="well",
up=False)
# Adding new stress model
model.add_stressmodel(EstTotPump_)
# Setting the same optimal parameters
model.parameters["optimal"] = optiparam
model.parameters["stderr"] = stdparam
# If does not exist
except FileNotFoundError:
print("No model for " + Wellnest_name + "_" + well_name)
continue
# Saving Pastas model, Well_name
models.append(model)
well_names.append(well_name)
###########################################################################
# Pastas Plotting and Graphing
###########################################################################
# set plotting time min and time max
if "BK" in well_name:
ptime_min = "1986"
else:
ptime_min = "1978"
ptime_max = "2020"
# Saving time_mins and time_maxs
time_mins.append(ptime_min)
time_maxs.append(ptime_max)
# If plotting additional graphs
if add_graph == 1:
# Plotting for calibrated time period
ax = model.plot(tmin=ptime_min, tmax=ptime_max,
figsize=(10, 6))
# Setting yaxis limits
# Different for wells in different aquifers
if "BK" in well_name:
plt.ylim(-25, -5)
elif "PD" in well_name:
# Well nests with big variation in head
if Wellnest_name == "LCBKK036" or \
Wellnest_name == "LCSPK007":
plt.ylim(-55, -10)
# Rest of wells
else:
plt.ylim(-30, -5)
elif "NL" in well_name:
plt.ylim(-70, -10)
elif "NB" in well_name:
plt.ylim(-60, -5)
# If saving graphs
if save_graph == 1:
# First figure from plot
# Fig name
fig_name1 = Wellnest_name + "_" + well_name + "_GW_" + \
time_min + "_" + time_max + "_1.png"
# Fig path
full_figpath = os.path.join(figpath, fig_name1)
# Saving fig
plt.savefig(full_figpath, dpi=150, bbox_inches="tight",
format="png")
# Second figure
model.plots.results(tmin=ptime_min, tmax=ptime_max,
figsize=(10, 6))
# Fig name
fig_name2 = Wellnest_name + "_" + well_name + "_GW_" + \
time_min + "_" + time_max + "_2.png"
# Fig path
full_figpath = os.path.join(figpath, fig_name2)
# Saving fig
plt.savefig(full_figpath, dpi=150, bbox_inches="tight",
format="png")
# If not saving graphs
else:
model.plots.results(tmin=time_min, tmax=time_max,
figsize=(10, 6))
# If replicating publication figures
if paper_graph == 1:
# Calibrating
# If calibration and validating
califlag = [calitime_min, calitime_max]
bkk_sub_gw.bkk_plotting.Pastas_results(models, Wellnest_name,
well_names, time_mins,
time_maxs, figpath, save_graph,
califlag=califlag)