|
1 |
| -# add pathway to folders 1 level higher (i.e., to mesmer and configs) |
2 | 1 | import sys
|
| 2 | +import warnings |
3 | 3 |
|
| 4 | +# add pathway to configs folder |
4 | 5 | sys.path.append("../")
|
5 | 6 |
|
6 |
| -import os.path |
7 |
| -import warnings |
8 |
| - |
9 | 7 | # load in configurations used in this script
|
10 | 8 | import configs.config_across_scen_T_cmip6ng_test as cfg
|
11 | 9 |
|
|
19 | 17 | create_emus_lt,
|
20 | 18 | create_emus_lv,
|
21 | 19 | )
|
22 |
| -from mesmer.io import ( |
23 |
| - load_cmipng, |
24 |
| - load_phi_gc, |
25 |
| - load_regs_ls_wgt_lon_lat, |
26 |
| - save_mesmer_bundle, |
27 |
| -) |
| 20 | +from mesmer.io import load_cmipng, load_phi_gc, load_regs_ls_wgt_lon_lat |
28 | 21 | from mesmer.utils import convert_dict_to_arr, extract_land, separate_hist_future
|
29 | 22 |
|
30 |
| -# where to save the bundle |
31 |
| -bundle_out_file = os.path.join("tests", "test-data", "test-mesmer-bundle.pkl") |
32 |
| -os.makedirs(os.path.dirname(bundle_out_file), exist_ok=True) |
33 |
| - |
34 | 23 | # specify the target variable
|
35 | 24 | targ = cfg.targs[0]
|
36 | 25 | print(targ)
|
37 | 26 |
|
38 | 27 | # load in the ESM runs
|
39 | 28 | esms = cfg.esms
|
| 29 | +print(esms) |
40 | 30 |
|
41 | 31 | # load in tas with global coverage
|
42 | 32 | tas_g_dict = {} # tas with global coverage
|
43 | 33 | GSAT_dict = {} # global mean tas
|
44 |
| -GHFDS_dict = {} # global mean hfds (needed as predictor) |
45 | 34 | tas_g = {}
|
46 | 35 | GSAT = {}
|
47 |
| -GHFDS = {} |
48 | 36 | time = {}
|
49 | 37 |
|
50 | 38 | for esm in esms:
|
51 | 39 | print(esm)
|
52 | 40 | tas_g_dict[esm] = {}
|
53 | 41 | GSAT_dict[esm] = {}
|
54 |
| - GHFDS_dict[esm] = {} |
55 | 42 | time[esm] = {}
|
56 | 43 |
|
57 | 44 | for scen in cfg.scenarios:
|
|
62 | 49 |
|
63 | 50 | if tas_g_tmp is None:
|
64 | 51 | warnings.warn(f"Scenario {scen} does not exist for tas for ESM {esm}")
|
65 |
| - else: # if scen exists: save fields + load hfds fields for it too |
| 52 | + else: # if scen exists: save fields |
66 | 53 | tas_g_dict[esm][scen], GSAT_dict[esm][scen], lon, lat, time[esm][scen] = (
|
67 | 54 | tas_g_tmp,
|
68 | 55 | GSAT_tmp,
|
69 | 56 | lon_tmp,
|
70 | 57 | lat_tmp,
|
71 | 58 | time_tmp,
|
72 | 59 | )
|
73 |
| - _, GHFDS_dict[esm][scen], _, _, _ = load_cmipng("hfds", esm, scen, cfg) |
74 | 60 |
|
75 | 61 | tas_g[esm] = convert_dict_to_arr(tas_g_dict[esm])
|
76 | 62 | GSAT[esm] = convert_dict_to_arr(GSAT_dict[esm])
|
77 |
| - GHFDS[esm] = convert_dict_to_arr(GHFDS_dict[esm]) |
78 | 63 |
|
79 | 64 | # load in the constant files
|
80 | 65 | reg_dict, ls, wgt_g, lon, lat = load_regs_ls_wgt_lon_lat(cfg.reg_type, lon, lat)
|
|
89 | 74 | print(esm)
|
90 | 75 |
|
91 | 76 | print(esm, "Start with global trend module")
|
| 77 | + |
92 | 78 | params_gt_T = train_gt(GSAT[esm], targ, esm, time[esm], cfg, save_params=True)
|
93 |
| - params_gt_hfds = train_gt(GHFDS[esm], "hfds", esm, time[esm], cfg, save_params=True) |
94 | 79 |
|
95 | 80 | preds_gt = {"time": time[esm]}
|
96 |
| - emus_gt_T = create_emus_gt( |
97 |
| - params_gt_T, preds_gt, cfg, concat_h_f=True, save_emus=True |
98 |
| - ) |
99 | 81 | gt_T_s = create_emus_gt(
|
100 | 82 | params_gt_T, preds_gt, cfg, concat_h_f=False, save_emus=False
|
101 | 83 | )
|
| 84 | + emus_gt_T = create_emus_gt( |
| 85 | + params_gt_T, preds_gt, cfg, concat_h_f=True, save_emus=True |
| 86 | + ) |
102 | 87 |
|
103 | 88 | print(
|
104 | 89 | esm,
|
105 | 90 | "Start preparing predictors for global variability, local trends, and local variability",
|
106 | 91 | )
|
107 |
| - gt_T2_s = {} |
108 |
| - for scen in gt_T_s.keys(): |
109 |
| - gt_T2_s[scen] = gt_T_s[scen] ** 2 |
110 | 92 |
|
111 |
| - gt_hfds_s = create_emus_gt( |
112 |
| - params_gt_hfds, preds_gt, cfg, concat_h_f=False, save_emus=False |
113 |
| - ) |
114 |
| - |
115 |
| - gv_novolc_T = {} |
116 |
| - for scen in emus_gt_T.keys(): |
117 |
| - gv_novolc_T[scen] = GSAT[esm][scen] - emus_gt_T[scen] |
118 |
| - gv_novolc_T_s, time_s = separate_hist_future(gv_novolc_T, time[esm], cfg) |
| 93 | + GSAT_s, time_s = separate_hist_future(GSAT[esm], time[esm], cfg) |
| 94 | + gv_novolc_T_s = {} |
| 95 | + for scen in gt_T_s.keys(): |
| 96 | + gv_novolc_T_s[scen] = GSAT_s[scen] - gt_T_s[scen] |
119 | 97 |
|
120 | 98 | tas_s, time_s = separate_hist_future(tas[esm], time[esm], cfg)
|
121 | 99 |
|
122 | 100 | print(esm, "Start with global variability module")
|
| 101 | + |
123 | 102 | params_gv_T = train_gv(gv_novolc_T_s, targ, esm, cfg, save_params=True)
|
124 | 103 |
|
125 | 104 | time_v = {}
|
| 105 | + scen = list(emus_gt_T.keys())[0] |
126 | 106 | time_v["all"] = time[esm][scen]
|
127 |
| - # remember: scen comes from emus_gt_T.keys() here |
128 |
| - # (= necessary to derive compatible emus_gt & emus_gv) |
129 | 107 | preds_gv = {"time": time_v}
|
130 | 108 | emus_gv_T = create_emus_gv(params_gv_T, preds_gv, cfg, save_emus=True)
|
131 | 109 |
|
| 110 | + # create full global emulations |
132 | 111 | print(esm, "Merge the global trend and the global variability.")
|
133 |
| - emus_g_T = create_emus_g( |
| 112 | + emus_g = create_emus_g( |
134 | 113 | emus_gt_T, emus_gv_T, params_gt_T, params_gv_T, cfg, save_emus=True
|
135 | 114 | )
|
136 | 115 |
|
137 | 116 | print(esm, "Start with local trends module")
|
| 117 | + |
138 | 118 | preds = {
|
139 | 119 | "gttas": gt_T_s,
|
140 |
| - "gttas2": gt_T2_s, |
141 |
| - "gthfds": gt_hfds_s, |
142 | 120 | "gvtas": gv_novolc_T_s,
|
143 |
| - } # predictors_list |
144 |
| - targs = {"tas": tas_s} # targets list |
| 121 | + } |
| 122 | + targs = {"tas": tas_s} |
145 | 123 | params_lt, params_lv = train_lt(preds, targs, esm, cfg, save_params=True)
|
146 | 124 |
|
147 |
| - preds_lt = {"gttas": gt_T_s, "gttas2": gt_T2_s, "gthfds": gt_hfds_s} |
| 125 | + preds_lt = {"gttas": gt_T_s} |
148 | 126 | lt_s = create_emus_lt(params_lt, preds_lt, cfg, concat_h_f=False, save_emus=True)
|
149 | 127 | emus_lt = create_emus_lt(params_lt, preds_lt, cfg, concat_h_f=True, save_emus=True)
|
150 | 128 |
|
|
162 | 140 | # load in the auxiliary files
|
163 | 141 | aux = {}
|
164 | 142 | aux["phi_gc"] = load_phi_gc(
|
165 |
| - lon, lat, ls, cfg, L_start=1750, L_end=2000, L_interval=250 |
166 |
| - ) # better results with default values L, but like this much faster + less space needed |
| 143 | + lon, lat, ls, cfg, L_start=1500, L_end=2000, L_interval=250 |
| 144 | + ) |
167 | 145 |
|
168 | 146 | # train lv AR1_sci on residual variability
|
169 | 147 | targs_res_lv = {"tas": res_lv_s}
|
|
178 | 156 | # create full emulations
|
179 | 157 | print(esm, "Merge the local trends and the local variability.")
|
180 | 158 | emus_l = create_emus_l(emus_lt, emus_lv, params_lt, params_lv, cfg, save_emus=True)
|
181 |
| - |
182 |
| - save_mesmer_bundle( |
183 |
| - bundle_out_file, |
184 |
| - params_lt, |
185 |
| - params_lv, |
186 |
| - params_gv_T, |
187 |
| - seeds=cfg.seed, |
188 |
| - land_fractions=ls["grid_l_m"], |
189 |
| - lat=lat["c"], |
190 |
| - lon=lon["c"], |
191 |
| - time=time_s, |
192 |
| - ) |
0 commit comments