Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (105 loc) · 4.92 KB
/
Copy pathmain.py
File metadata and controls
132 lines (105 loc) · 4.92 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
import argparse
import xarray as xr
import numpy as np
from pathlib import Path
from MLP import train_mlp
from Linear import train_linear
from RandomForest import train_rf
from Evaluate import evaluate_and_save
from Dataset import load_station_ids, split_stations, compute_time_indicators, slice_datasets, VARIABLES
# ── paths ─────────────────────────────────────────────────────────────────────
STATIC_PATH = Path('/mnt/storage/sherrie-weather/Probabilistic-EARTH-W/era5-static/terrain-interpolated-ready.nc')
ERA5_PATH = Path('/mnt/storage/sherrie-weather/Probabilistic-EARTH-W/era5-hourly/2020-2023-CONUS-interpolated.zarr')
MADIS_PATH = Path('/mnt/storage/sherrie-weather/Probabilistic-EARTH-W/madis-hourly/2020-2023-CONUS.zarr')
SPLIT_PATH = Path('/mnt/home/qidong/Probabilistic-EARTH-W/WeatherDownscaling/Conf/split/quality.yaml')
OUTPUT_DIR = Path('/mnt/storage/sherrie-weather/Benchmark-Data/MOS_Corrected')
# ── train dispatch ────────────────────────────────────────────────────────────
def train_model(model_name, variable, train_idx, val_idx,
static_ds, time_indicators, era5_ds, madis_ds):
"""
Train a single model for a single variable.
Returns model_args dict ready for evaluate_and_save.
"""
if model_name == 'linear':
poly, mean_, var_, beta = train_linear(
train_idx, val_idx,
static_ds, time_indicators,
era5_ds, madis_ds, variable,
)
return {
'poly': poly,
'mean_': mean_,
'var_': var_,
'beta': beta,
}
elif model_name == 'rf':
model = train_rf(
train_idx, val_idx,
static_ds, time_indicators,
era5_ds, madis_ds, variable,
)
return {'model': model}
elif model_name == 'mlp':
model, mean_, var_, y_mean_, y_var_ = train_mlp(
train_idx, val_idx,
static_ds, time_indicators,
era5_ds, madis_ds, variable,
)
return {
'model': model,
'mean_': mean_,
'var_': var_,
'y_mean_': y_mean_,
'y_var_': y_var_,
}
else:
raise ValueError(f'Unknown model: {model_name}')
# ── main ──────────────────────────────────────────────────────────────────────
def main(args):
# --- parse arguments ---
model_name = args.model
# --- load station ids ---
station_ids = load_station_ids(SPLIT_PATH)
# --- load datasets ---
print('Loading datasets...', flush=True)
static_ds = xr.open_dataset(STATIC_PATH).sel(station_id=station_ids)
era5_ds = xr.open_dataset(ERA5_PATH, decode_times=False).sel(station_id=station_ids)
madis_ds = xr.open_dataset(MADIS_PATH, decode_times=False).sel(station_id=station_ids)
# --- station split ---
train_idx, val_idx, test_idx = split_stations(SPLIT_PATH)
# --- slice ERA5 and MADIS once ---
era5_ds, madis_ds = slice_datasets(era5_ds, madis_ds, start_year=2020, end_year=2023)
# --- time indicators ---
print('Computing time indicators...', flush=True)
lats = static_ds.latitude.values
lons = static_ds.longitude.values
time_indicators = compute_time_indicators(lats, lons, start_year=2020, end_year=2023)
# --- slice train, val, test for debugging ---
# train_idx = train_idx[:10]
# val_idx = val_idx[:10]
# test_idx = test_idx[:10]
# --- train one model per variable ---
print(f'Training [{model_name}]...', flush=True)
trained_models = {}
for variable in VARIABLES:
print(f'── [{model_name}|{variable}] training ──', flush=True)
trained_models[variable] = train_model(
model_name, variable,
train_idx, val_idx,
static_ds, time_indicators,
era5_ds, madis_ds,
)
# --- evaluate and save ---
output_path = OUTPUT_DIR / f'corrected_era5_{model_name}.nc'
evaluate_and_save(
model_name, trained_models, test_idx,
static_ds, time_indicators,
era5_ds, madis_ds,
str(output_path),
)
# ── run main ──────────────────────────────────────────────────────────────────────
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Train and evaluate a bias correction model.')
parser.add_argument('--model', type=str, required=True, choices=['linear', 'rf', 'mlp'])
args = parser.parse_args()
main(args)