-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_completeness_CMIP6_PGW.py
150 lines (127 loc) · 4.42 KB
/
check_completeness_CMIP6_PGW.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
#!/usr/bin/env python
"""
@File : check_completeness_simulation.py
@Time : 2023/02/20 18:11:40
@Author : Daniel Argüeso
@Version : 1.0
@Contact : d.argueso@uib.es
@License : (C)Copyright 2023, Daniel Argüeso
@Project : EPICC
@Desc : None
"""
import os
import argparse
from glob import glob
import pandas as pd
import datetime as dt
import xarray as xr
from tqdm.auto import tqdm
import logging
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 ta,hur,ua,va,zg,hurs,tas,ps,psl,ts,uas,vas",
default="ta,hur,ua,va,zg,hurs,tas,ps,psl,ts,uas,vas",
)
args = parser.parse_args()
return args
args = parse_args()
models_str = args.models
variables = args.var_names.split(",")
if models_str is None:
with open("list_CMIP6.txt") as f:
models_hist = f.read().splitlines()
with open("list_CMIP6.txt") as f:
models_proj = f.read().splitlines()
models = list(set(models_hist + models_proj))
else:
models = args.models.split(",")
logfile = "check_CMIP6_PGW.log"
if os.path.exists(logfile):
os.remove(logfile)
logging.basicConfig(
filename=logfile,
filemode="a",
format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
datefmt="%H:%M:%S",
level=logging.INFO,
)
logging.info("Checking completeness of CMIP6 files for PGW")
logger = logging.getLogger("checkCMI6PGW")
scenarios = {"historical": [1950, 2014], "ssp585": [2015, 2099]}
class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
def main():
for GCM in models:
complete = True
first = True
for scen in scenarios.keys():
print(f"{bcolors.HEADER}Checking {GCM} {scen}{bcolors.ENDC}")
for var in (pbar := tqdm(variables)):
pbar.set_description(f"Processing {var}")
path_in = f"./{scen}/{var}/{GCM}/"
finlesin_var = sorted(glob(f"{path_in}/{var}_*"))
# print(finlesin_var)
if finlesin_var:
finvar = xr.open_mfdataset(
finlesin_var, use_cftime=True, combine="by_coords"
)
# Checking that the period is covered
if (finvar.time[0].dt.year > scenarios[scen][0]) or (
finvar.time[-1].dt.year < scenarios[scen][1]
):
complete = False
if first:
logger.info(
f"{bcolors.FAIL}{scen} {GCM} is not complete{bcolors.ENDC}"
)
first = False
logger.info(f"{GCM} {scen} {var} is not complete")
finvar.close()
else:
complete = False
if first:
logger.info(
f"{bcolors.FAIL}{scen} {GCM} is not complete{bcolors.ENDC}"
)
first = False
logger.info(f"{GCM} {scen} {var} is not complete")
if complete:
print(f"{bcolors.OKGREEN} {GCM} is complete{bcolors.ENDC}")
logger.info(f"{bcolors.OKGREEN} {GCM} is complete{bcolors.ENDC}")
else:
print(
f"{bcolors.FAIL}{GCM} is not complete{bcolors.ENDC}"
f"{bcolors.BOLD} Check log file {logfile} for details{bcolors.ENDC}"
)
###############################################################################
# __main__ scope
###############################################################################
if __name__ == "__main__":
raise SystemExit(main())
###############################################################################