-
Notifications
You must be signed in to change notification settings - Fork 2
/
Create_CMIP6_AnnualCycleChange_ENSMEAN.py
189 lines (162 loc) · 5.13 KB
/
Create_CMIP6_AnnualCycleChange_ENSMEAN.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
#!/usr/bin/env python
"""
#####################################################################
# Author: Daniel Argueso <daniel>
# Date: 2021-08-25T18:41:42+02:00
# Email: d.argueso@uib.es
# Last modified by: daniel
# Last modified time: 2021-08-25T18:41:44+02:00
#
# @Project@
# Version: x.0 (Beta)
# Description:
#
# Dependencies:
#
# Files:
#
#####################################################################
"""
import os, argparse
import xarray as xr
import numpy as np
from glob import glob
import subprocess as subprocess
from pathlib import Path
class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
ERROR = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
#####################################################################
#####################################################################
def parse_args():
parser = argparse.ArgumentParser(
description="PURPOSE: Check the completeness of the CMIP6 files for PGW"
)
parser.add_argument(
"-m",
"--models",
dest="models",
help="Optional input list of models",
type=str,
nargs="?",
default=None,
)
# variable(s) to process
parser.add_argument(
"-v",
"--var_names",
type=str,
help="Variable names (e.g. ta) to process. Separate "
+ 'multiple variable names with "," (e.g. tas,ta). Default is '
+ "to process all required variables hurs,tas,ps,ts,vas,uas,psl,ta,hus,ua,va,zg.",
default="hurs,tas,ps,ts,vas,uas,psl,ta,hus,ua,va,zg",
)
# input directory
parser.add_argument(
"-i",
"--input_dir",
type=str,
help="Directory with input GCM delta files on ERA5 grid",
default="./regrid_ERA5/",
)
# corrected_plevs directory
parser.add_argument(
"-cp",
"--corrected_plevs_dir",
type=str,
help="Directory where the GCM delta files with corrected plevs should be stored.",
default="./regrid_ERA5/corrected_plevs/",
)
# output directory
parser.add_argument(
"-o",
"--output_dir",
type=str,
help="Directory where the GCM ENSEMBLE delta files should be stored.",
default="./regrid_ERA5/",
)
args = parser.parse_args()
return args
args = parse_args()
models_str = args.models
if models_str is None:
with open("list_CMIP6.txt") as f:
models = f.read().splitlines()
else:
models = args.models.split(",")
variables = args.var_names.split(",")
plvs = np.asarray(
[
100000,
92500,
85000,
70000,
60000,
50000,
40000,
30000,
25000,
20000,
15000,
10000,
7000,
5000,
3000,
2000,
1000,
500,
100,
]
)
correct_plevs = True
#####################################################################
#####################################################################
def main():
Path(args.output_dir).mkdir(exist_ok=True, parents=True)
for GCM in models:
if correct_plevs:
Path(f"{args.corrected_plevs_dir}").mkdir(exist_ok=True, parents=True)
for vn, varname in enumerate(variables):
filepath = f"{args.input_dir}/{varname}_{GCM}_delta.nc"
filename = filepath.split("/")[-1]
print(filename)
fin = xr.open_dataset(filepath)
if varname in ["ta", "hus", "ua", "va", "zg"]:
fin.coords["plev"] = plvs
fin.to_netcdf(f"{args.corrected_plevs_dir}/{filename}")
else:
if "height" in fin.coords:
fin = fin.drop("height")
fin = fin[varname]
# import pdb; pdb.set_trace() # fmt: skip
fin.to_netcdf(f"{args.corrected_plevs_dir}/{filename}")
for varname in variables:
print(varname)
# import pdb; pdb.set_trace() # fmt: skip
# try:
# subprocess.check_output(
# f"cdo ensmean {args.corrected_plevs_dir}/{varname}_* {varname}_CC_signal_ssp585_2099-2070_1985-2014.nc",
# shell=True,
# )
# print(f"{bcolors.OKGREEN}Ensemble mean of GCMs{bcolors.ENDC}")
# except Exception:
# raise SystemExit(
# f"{bcolors.ERROR}ERROR: Could not make the ensemble mean{bcolors.ENDC}"
# )
filesin = sorted(glob(f"{args.corrected_plevs_dir}/{varname}_*"))
fin = xr.open_mfdataset(filesin, concat_dim="model", combine="nested")
fin_ensmean = fin.mean(dim="model").squeeze()
fin_ensmean.to_netcdf(f"{varname}_CC_signal_ssp585_2070-2099_1985-2014.nc")
###############################################################################
# __main__ scope
###############################################################################
if __name__ == "__main__":
raise SystemExit(main())
###############################################################################