-
Notifications
You must be signed in to change notification settings - Fork 2
/
midprocessing.py
94 lines (76 loc) · 2.19 KB
/
midprocessing.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
import os
import sys
import shutil
USER = os.getenv("USER")
import xarray as xr
def update_damage_function_library(
input,
output,
recipe,
disc,
eta,
rho,
fit=True,
):
os.makedirs(output, exist_ok=True)
shutil.copy2(
f"{input}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_coefficients.nc4",
output,
)
if fit:
shutil.copy2(
f"{input}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_fit.nc4", output
)
def combine_CAMEL_coefs(
recipe,
disc,
eta,
rho,
CAMEL,
coastal,
AMEL,
input_dir,
mask="unmasked",
pulse_year=2020,
fit=True,
):
print(f"Creating {recipe} {disc} for {CAMEL}...")
os.makedirs(f"{input_dir}/{CAMEL}/{pulse_year}/{mask}", exist_ok=True)
coefs, fit = {}, {}
for sector in [coastal, AMEL]:
coefs[
sector
] = f"{input_dir}/{sector}/{pulse_year}/{mask}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_coefficients.nc4"
coefs["combined"] = xr.combine_by_coords(
[
xr.open_dataset(coefs[AMEL]),
xr.open_dataset(coefs[coastal]),
],
combine_attrs="drop",
)
coefs["combined"].attrs = {
"sector": CAMEL,
"Run type": recipe,
"Discount type": disc,
"Coastal path": coefs[coastal],
"AMEL path": coefs[AMEL],
}
coefs["combined"].to_netcdf(
f"{input_dir}/{CAMEL}/{pulse_year}/{mask}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_coefficients.nc4"
)
if fit:
for sector in [coastal, AMEL]:
fit[
sector
] = f"{input_dir}/{sector}/{pulse_year}/{mask}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_fit.nc4"
fit["combined"] = xr.open_dataset(fit[coastal]) + xr.open_dataset(fit[AMEL])
fit["combined"].attrs = {
"sector": CAMEL,
"Run type": recipe,
"Discount type": disc,
"Coastal path": fit[coastal],
"AMEL path": fit[AMEL],
}
fit["combined"].to_netcdf(
f"{input_dir}/{CAMEL}/{pulse_year}/{mask}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_fit.nc4"
)