This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
180 lines (157 loc) · 6.63 KB
/
main.py
File metadata and controls
180 lines (157 loc) · 6.63 KB
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
import multiprocessing
import hoopla
from hoopla import data, models
from hoopla.calibration.calibration import make_calibration
from hoopla.config import DATA_PATH
from hoopla.initialization import list_catchments
from hoopla.simulation import make_simulation
from hoopla.forecast import make_forecast
def run_hoopla(combinations: dict):
config = combinations['config']
catchment_names = list_catchments(config.general.time_step)
# Load models
print('Loading models ...')
catchment_name = catchment_names[0]
hydro_model = models.load_hydro_model(combinations['hydro_model_name'])
pet_model = models.load_pet_model(combinations['pet_model_name'])
sar_model = models.load_sar_model(combinations['sar_model_name'])
da_model = models.load_da_model(combinations['da_model_name'])
# Load observations
print('Loading data ...')
observations = data.load_observations(
path=f'{DATA_PATH}/{config.general.time_step}/Hydromet_obs/Hydromet_obs_{catchment_name}.mat',
file_format='mat',
config=config,
pet_model=pet_model,
sar_model=sar_model
)
calibration_file_results = f'./results/calibration-C={catchment_name}-H={hydro_model.name()}-E={pet_model.name()}-S={sar_model.name()}.json'
simulation_file_results = f'./results/simulation-C={catchment_name}-H={hydro_model.name()}-E={pet_model.name()}-S={sar_model.name()}.json'
forecast_file_results = f'./results/forecast-C={catchment_name}-H={hydro_model.name()}-E={pet_model.name()}-S={sar_model.name()}.json'
# Calibration
# -----------
if config.operations.calibration:
model_parameters = data.load_model_parameters(
filepath=f'{DATA_PATH}/{config.general.time_step}/Model_parameters/model_param_boundaries.mat',
model_name=hydro_model.name(),
file_format='mat',
)
if config.general.compute_snowmelt:
sar_model_parameters = data.load_sar_model_parameters(
filepath=f'{DATA_PATH}/{config.general.time_step}/Model_parameters/snow_model_param_boundaries.mat',
model_name=sar_model.name(),
file_format='mat',
calibrate_snow=config.calibration.calibrate_snow
)
# Add the SAR model's parameters at the end of the parameters to calibrate
model_parameters += sar_model_parameters
# Crop observed data according to specified dates and warm up
print('Removing unused data ...')
observations_for_calibration, _, observations_for_warm_up = data.crop_data(
config=config,
observations=observations.copy(),
hydro_model=hydro_model,
pet_model=pet_model,
sar_model=sar_model,
ini_type='ini_calibration'
)
print('Starting calibration ...')
make_calibration(
observations=observations_for_calibration,
observations_for_warm_up=observations_for_warm_up,
config=config,
hydro_model=hydro_model,
pet_model=pet_model,
sar_model=sar_model,
model_parameters=model_parameters,
filepath_results=calibration_file_results
)
# Simulation
# ----------
if config.operations.simulation:
calibrated_params = data.load_calibrated_model_parameters(
filepath=calibration_file_results
)
# Crop data for the simulation
print('Removing unused data ...')
observations_for_simulation, _, observations_for_warm_up = data.crop_data(
config=config,
observations=observations.copy(),
hydro_model=hydro_model,
pet_model=pet_model,
sar_model=sar_model,
ini_type='ini_simulation'
)
print('Starting simulation ...')
make_simulation(
observations=observations_for_simulation,
observations_for_warm_up=observations_for_warm_up,
config=config,
hydro_model=hydro_model,
pet_model=pet_model,
sar_model=sar_model,
da_model=da_model,
parameters=calibrated_params,
filepath_results=simulation_file_results
)
# Forecast
if config.operations.forecast:
# Load meteo forecast data
if config.forecast.meteo_ens:
# Meteorological ensemble prediction data
forecast_data = data.load_ens_met_data(
filepath=f'{DATA_PATH}/{config.general.time_step}/Ens_met_fcast/Met_fcast_{catchment_name}.mat',
file_format='mat',
config=config,
sar_model=sar_model
)
else:
forecast_data = data.load_forecast_data(
filepath=f'{DATA_PATH}/{config.general.time_step}/Det_met_fcast/Met_fcast_{catchment_name}.mat',
file_format='mat',
config=config,
sar_model=sar_model
)
# Load calibrated model parameters
calibrated_params = data.load_calibrated_model_parameters(
filepath=calibration_file_results
)
# Crop data for the simulation
print('Removing unused data ...')
observations, observations_for_forecast, observations_for_warm_up = data.crop_data(
config=config,
observations=observations.copy(),
hydro_model=hydro_model,
pet_model=pet_model,
sar_model=sar_model,
ini_type='ini_forecast',
forecast_data=forecast_data
)
print('Starting forecast')
make_forecast(
observations=observations,
observations_for_warm_up=observations_for_warm_up,
observations_for_forecast=observations_for_forecast,
config=config,
hydro_model=hydro_model,
pet_model=pet_model,
sar_model=sar_model,
da_model=da_model,
parameters=calibrated_params,
filepath_results=forecast_file_results
)
if __name__ == '__main__':
config = hoopla.load_config('./config.toml')
# Listing models
print('Available models\n----------------')
print('hydro_models', models.list_hydro_models())
print('evapotranspiration_models', models.list_pet_models())
print('snow_models', models.list_sar_models())
print('da_models', models.list_da_models())
models_combination = hoopla.util.make_combinations(config)
if config.general.parallelism:
pool = multiprocessing.Pool()
pool.map(run_hoopla, models_combination)
else:
for combination in models_combination:
run_hoopla(combination)