forked from jbbarre/ISM_SimulationChecker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompliance_checker.py
More file actions
executable file
·1444 lines (1297 loc) · 51.5 KB
/
compliance_checker.py
File metadata and controls
executable file
·1444 lines (1297 loc) · 51.5 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#
# ISMIP7 Compliance Checker — check summary
#
# 1. Naming (_check_naming)
# - Variable name matches the expected ISMIP7 name from the data request.
# - Region field in filename matches the region inferred from the grid (AIS/GrIS).
# - ISM member id (field 4) matches format mNNN (e.g. m001).
# - ESM name (field 5) is a recognised CMIP6/CMIP7 model name.
# - Forcing member id (field 6) matches format fNNN (e.g. f001).
# - Set counter (field 8) matches format [C|E|P]NNN (e.g. C001, E041, P132).
# - Year range (field 9) matches format YYYY-YYYY; start <= end; both match the actual time axis.
#
# 2. Numerical (_check_numerical)
# - Variable units match the data request.
# - All values lie within the allowed min/max range for the relevant region.
# - Array is not entirely fill/missing values.
#
# 3. Spatial (_check_spatial) [xyt variables only]
# - Lower-left and upper-right grid corners lie within the expected AIS or GrIS extents.
# - Grid resolution is one of the allowed values (1, 2, 4, 8, 16, 32 km).
# - x and y resolution are equal (square cells).
#
# 4. Time (_check_time)
# - Time dimension is present, is an unlimited (record) dimension, and its values are monotonically increasing.
# - Time step matches the expected annual cadence (within tolerance).
# - Experiment end date matches experiments_ismip7.csv; duration >= 1 year for historical, exact for others.
#
# 5. Attributes (_check_attributes)
# - Global attributes present: group, model, contact_name, contact_email, crs
# (epsg:3413 for GrIS, epsg:3031 for AIS).
# - Coordinate attributes: time has units, calendar, bounds (FL vars); x/y have units (xyt).
# - Variable standard_name matches data request (if specified).
# - _FillValue must be present and equal the default netCDF4 fill value for the variable's dtype.
# If missing_value is also present, it must equal _FillValue.
# - Main variable and time coordinate are single-precision float (float32 / f4).
# - scale_factor and add_offset are not allowed on the main variable.
import datetime
import os
import re
import subprocess
import argparse
import numpy as np
import pandas as pd
import xarray as xr
import netCDF4
from tqdm import tqdm
DEFAULT_SOURCE_PATH = "./Models/GrIS/ISMIP7/SYNTH1/CORE"
DEFAULT_VARIABLE_LIST = "ismip7_scalars"
VARIABLE_LIST_CHOICES = ("ismip7_scalars", "ismip7_xyt", "ismip7")
VARIABLE_REQUEST_XLSX = os.path.join("conventions", "ISMIP7_variable_request.xlsx")
EXPERIMENTS_ISMIP7_CSV_FILENAME = "experiments_ismip7.csv"
AIS_GRID_EXTENT = [-3040000, -3040000, 3040000, 3040000]
GrIS_GRID_EXTENT = [-720000, -3450000, 960000, -570000]
AIS_POSSIBLE_RESOLUTION = [2, 4, 8, 16, 32]
GrIS_POSSIBLE_RESOLUTION = [1, 2, 4, 8, 16, 32]
TIME_STEP_MIN_DAYS = 365
TIME_STEP_MAX_DAYS = 366
# ISMIP7 CORE file naming convention:
# {var}_{region}_{group}_{model}_{modelid}_{ESM}_{forcingid}_{experiment}_{configid}_{startyear}-{endyear}.nc
ISMIP7_FILENAME_PARTS = 10
ISMIP7_FILENAME_VAR_IDX = 0
ISMIP7_FILENAME_REGION_IDX = 1
ISMIP7_FILENAME_ISM_MEMBER_IDX = 4
ISMIP7_FILENAME_ESM_IDX = 5
ISMIP7_FILENAME_FORCING_MEMBER_IDX = 6
ISMIP7_FILENAME_EXPERIMENT_IDX = 7
ISMIP7_FILENAME_SET_COUNTER_IDX = 8
ISMIP7_FILENAME_YEAR_RANGE_IDX = 9
# Known CMIP6 and CMIP7 model names valid as ESM forcing identifiers (field 5).
VALID_ESM_NAMES: set[str] = {
# CMIP6
"ACCESS-CM2", "ACCESS-ESM1-5",
"AWI-CM-1-1-MR", "AWI-ESM-1-1-LR",
"BCC-CSM2-MR", "BCC-ESM1",
"CAMS-CSM1-0",
"CAS-ESM2-0",
"CESM2", "CESM2-FV2", "CESM2-WACCM", "CESM2-WACCM-FV2",
"CIESM",
"CMCC-CM2-HR4", "CMCC-CM2-SR5", "CMCC-ESM2",
"CNRM-CM6-1", "CNRM-CM6-1-HR", "CNRM-ESM2-1",
"CanESM5", "CanESM5-1", "CanESM5-CanOE",
"E3SM-1-0", "E3SM-1-1", "E3SM-1-1-ECA", "E3SM-2-0",
"EC-Earth3", "EC-Earth3-AerChem", "EC-Earth3-CC", "EC-Earth3-Veg", "EC-Earth3-Veg-LR",
"FGOALS-f3-L", "FGOALS-g3",
"FIO-ESM-2-0",
"GFDL-CM4", "GFDL-ESM4",
"GISS-E2-1-G", "GISS-E2-1-G-CC", "GISS-E2-1-H", "GISS-E2-2-G", "GISS-E2-2-H",
"HadGEM3-GC31-LL", "HadGEM3-GC31-MM",
"IITM-ESM",
"INM-CM4-8", "INM-CM5-0",
"IPSL-CM5A2-INCA", "IPSL-CM6A-LR", "IPSL-CM6A-LR-INCA",
"KACE-1-0-G",
"KIOST-ESM",
"MCM-UA-1-0",
"MIROC-ES2H", "MIROC-ES2L", "MIROC6",
"MPI-ESM-1-2-HAM", "MPI-ESM1-2-HR", "MPI-ESM1-2-LR",
"MRI-ESM2-0",
"NESM3",
"NorCPM1", "NorESM2-LM", "NorESM2-MM",
"SAM0-UNICON",
"TaiESM1",
"UKESM1-0-LL", "UKESM1-1-LL",
# CMIP7 (extend as models are registered)
"ACCESS-ESM2-0",
"CanESM6",
"CESM3",
"CNRM-CM7", "CNRM-ESM2-2",
"EC-Earth4",
"GFDL-ESM5",
"GISS-E3",
"HadGEM4-GC51-LL",
"IPSL-CM7A-LR",
"MIROC7",
"MPI-ESM2-0",
"MRI-ESM3-0",
"NorESM3-LM", "NorESM3-MM",
"UKESM2-0-LL",
}
def main() -> None:
args = _parse_args()
source_path = args.source_path
variable_list = args.variable_list
run_checker(
source_path=source_path,
variable_list=variable_list,
workdir=os.getcwd(),
)
def run_checker(
source_path: str,
variable_list: str = DEFAULT_VARIABLE_LIST,
workdir: str | None = None,
commit_num: str | None = None,
):
workdir = os.path.abspath(workdir or os.getcwd())
commit_num = _get_commit_number() if commit_num is None else commit_num
experiments_ismip7 = _load_experiments_csv(
os.path.join(workdir, EXPERIMENTS_ISMIP7_CSV_FILENAME)
)
ismip_meta, ismip_var, mandatory_variables = _load_criteria(workdir, variable_list)
summary = _run_compliance_checker(
source_path=source_path,
commit_num=commit_num,
ismip_meta=ismip_meta,
ismip_var=ismip_var,
mandatory_variables=mandatory_variables,
experiments=experiments_ismip7,
criteria_file=VARIABLE_REQUEST_XLSX,
)
log_path = os.path.join(source_path, "compliance_checker_log.txt")
log_text = ""
if os.path.exists(log_path):
with open(log_path, "r") as log_file:
log_text = log_file.read()
summary["log_path"] = log_path
summary["log_text"] = log_text
return summary
def _get_commit_number() -> str:
try:
bash_command = "git log --pretty=format:'%h' -n 1"
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
commit_num, _error = process.communicate()
return commit_num.decode("UTF-8")
except Exception:
print("Could not retrieve git commit number. Is there a .git directory here?")
return "No commit number identified."
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Check simulation NetCDF datasets for ISMIP compliance."
)
parser.add_argument(
"--source-path",
default=DEFAULT_SOURCE_PATH,
help="Path to the directory containing the CORE NetCDF files.",
)
parser.add_argument(
"--variable-list",
choices=VARIABLE_LIST_CHOICES,
default=DEFAULT_VARIABLE_LIST,
help="Variable list to apply: ismip7_xyt, ismip7_scalars, or ismip7 (both).",
)
return parser.parse_args()
def _load_criteria(workdir: str, variable_list: str):
excel_path = os.path.join(workdir, VARIABLE_REQUEST_XLSX)
try:
df = pd.read_excel(excel_path, sheet_name="ISM")
except IOError:
print(
"ERROR: Unable to open the variable request file. Is the path correct? "
+ excel_path
)
raise
df = df.dropna(subset=["Variable Name"])
if variable_list == "ismip7_xyt":
df = df[df["Dim"] == "x,y,t"]
elif variable_list == "ismip7_scalars":
df = df[df["Dim"] == "t"]
ismip_meta = []
for _, row in df.iterrows():
entry = {
"variable": row["Variable Name"],
"dim": str(row["Dim"]),
"units": str(row["units"]) if pd.notna(row["units"]) else "",
"mandatory": 1 if str(row["Mandatory (yes/no)"]).lower() == "yes" else 0,
"standard_name": str(row["standard_name"]) if pd.notna(row["standard_name"]) else None,
"type": str(row["Type"]) if pd.notna(row["Type"]) else "",
}
for col in df.columns:
lc = str(col).lower()
if lc.startswith("min_") or lc.startswith("max_"):
try:
val = row[col]
entry[lc] = None if pd.isna(val) else float(val)
except Exception:
entry[lc] = None
ismip_meta.append(entry)
ismip_var = [d["variable"] for d in ismip_meta]
ismip_mandatory_var = [d["variable"] for d in ismip_meta if d["mandatory"] == 1]
return ismip_meta, ismip_var, ismip_mandatory_var
def _load_experiments_csv(file_path: str):
experiments = []
frame = pd.read_csv(file_path, delimiter=";")
for _, row in frame.iterrows():
experiments.append(
{
"experiment": row["experiment"],
"startinf": datetime.datetime.strptime(row["startinf"], "%Y-%m-%d"),
"startsup": datetime.datetime.strptime(row["startsup"], "%Y-%m-%d"),
"endinf": datetime.datetime.strptime(row["endinf"], "%Y-%m-%d"),
"endsup": datetime.datetime.strptime(row["endsup"], "%Y-%m-%d"),
"duration": int(row["duration"]),
}
)
return experiments
def _run_compliance_checker(
source_path: str,
commit_num: str,
ismip_meta,
ismip_var,
mandatory_variables,
experiments,
criteria_file,
):
if not os.path.isdir(source_path):
print(f"ERROR: Directory not found: '{source_path}'. Please check your --source-path argument.")
return _empty_summary()
try:
with open(os.path.join(source_path, "compliance_checker_log.txt"), "w") as f:
print("-> Checking " + source_path)
print()
today = datetime.date.today()
_write_log_header(f, commit_num, source_path, today, criteria_file)
experiment_groups = _group_files_by_experiment(source_path)
if not experiment_groups:
msg = f"No .nc files found in directory '{source_path}'. Please check your --source-path argument."
print(f"ERROR: {msg}")
f.write(f"ERROR: {msg}\n")
return _empty_summary()
summary = _process_experiments(
log_file=f,
source_path=source_path,
experiment_groups=experiment_groups,
mandatory_variables=mandatory_variables,
experiments=experiments,
ismip_var=ismip_var,
ismip_meta=ismip_meta,
)
_insert_synthesis(
source_path=source_path,
exp_counter=summary["exp_counter"],
file_counter=summary["file_counter"],
total_errors=summary["total_errors"],
total_file_errors=summary["total_file_errors"],
total_naming_errors=summary["total_naming_errors"],
total_num_errors=summary["total_num_errors"],
total_spatial_errors=summary["total_spatial_errors"],
total_time_errors=summary["total_time_errors"],
total_attr_errors=summary["total_attr_errors"],
report_naming_issues=summary["report_naming_issues"],
)
return summary
except TypeError as err:
print(
"Something went wrong with your dataset. Please, check your file(s) carefully. Error:",
err,
)
return _empty_summary()
def _empty_summary() -> dict:
return {
"exp_counter": 0,
"file_counter": 0,
"total_errors": 0,
"total_naming_errors": 0,
"total_num_errors": 0,
"total_spatial_errors": 0,
"total_time_errors": 0,
"total_attr_errors": 0,
"total_file_errors": 0,
"report_naming_issues": [],
}
def _group_files_by_experiment(source_path: str) -> dict:
groups = {}
for f in sorted(os.listdir(source_path)):
if not f.endswith(".nc"):
continue
parts = f.split("_")
exp_name = parts[ISMIP7_FILENAME_EXPERIMENT_IDX] if len(parts) == ISMIP7_FILENAME_PARTS else "_unknown"
if exp_name not in groups:
groups[exp_name] = []
groups[exp_name].append(f)
return groups
def _process_experiments(
log_file,
source_path: str,
experiment_groups: dict,
mandatory_variables,
experiments,
ismip_var,
ismip_meta,
):
total_naming_errors = 0
total_num_errors = 0
total_spatial_errors = 0
total_time_errors = 0
total_attr_errors = 0
total_file_errors = 0
report_naming_issues = []
file_counter = 0
exp_counter = 0
for experiment_name, exp_files in experiment_groups.items():
exp_counter += 1
exp_summary = _process_single_experiment(
log_file=log_file,
source_path=source_path,
experiment_name=experiment_name,
exp_files=exp_files,
mandatory_variables=mandatory_variables,
experiments=experiments,
ismip_var=ismip_var,
ismip_meta=ismip_meta,
report_naming_issues=report_naming_issues,
)
file_counter += exp_summary["file_counter"]
total_naming_errors += exp_summary["exp_naming_errors"]
total_num_errors += exp_summary["exp_num_errors"]
total_spatial_errors += exp_summary["exp_spatial_errors"]
total_time_errors += exp_summary["exp_time_errors"]
total_attr_errors += exp_summary["exp_attr_errors"]
total_file_errors += exp_summary["exp_file_errors"]
_print_experiment_summary(
experiment_name=exp_summary["experiment_name"],
exp_errors=exp_summary["exp_errors"],
)
total_errors = (
total_naming_errors
+ total_num_errors
+ total_spatial_errors
+ total_time_errors
+ total_attr_errors
+ total_file_errors
)
_print_total_summary(source_path=source_path, total_errors=total_errors)
return {
"exp_counter": exp_counter,
"file_counter": file_counter,
"total_errors": total_errors,
"total_naming_errors": total_naming_errors,
"total_num_errors": total_num_errors,
"total_spatial_errors": total_spatial_errors,
"total_time_errors": total_time_errors,
"total_attr_errors": total_attr_errors,
"total_file_errors": total_file_errors,
"report_naming_issues": report_naming_issues,
}
def _process_single_experiment(
log_file,
source_path: str,
experiment_name: str,
exp_files: list,
mandatory_variables,
experiments,
ismip_var,
ismip_meta,
report_naming_issues,
):
exp_naming_errors = 0
exp_num_errors = 0
exp_spatial_errors = 0
exp_time_errors = 0
exp_attr_errors = 0
exp_file_errors = 0
temp_mandatory_var = list(mandatory_variables)
for i in exp_files:
variable = i.split("_")[ISMIP7_FILENAME_VAR_IDX]
if variable in temp_mandatory_var:
temp_mandatory_var.remove(variable)
file_counter = 0
if experiment_name in [dic["experiment"] for dic in experiments]:
log_file.write("\n ")
log_file.write("**********************************************************\n")
log_file.write(" ** Experiment: " + experiment_name + " \n ")
log_file.write("**********************************************************\n")
log_file.write("\n ")
if not temp_mandatory_var:
log_file.write(
"Mandatory variables Test: "
+ experiment_name
+ " : all mandatory variables exist. \n"
)
else:
log_file.write(
"ERROR: In experiment "
+ experiment_name
+ ", these mandatory variable(s) is (are) missing: "
+ str(temp_mandatory_var)
+ "\n"
)
exp_file_errors += len(temp_mandatory_var)
for file in tqdm(exp_files):
file_counter += 1
file_summary = _process_single_file(
log_file=log_file,
source_path=source_path,
file=file,
experiment_name=experiment_name,
ismip_var=ismip_var,
ismip_meta=ismip_meta,
experiments=experiments,
report_naming_issues=report_naming_issues,
)
exp_naming_errors += file_summary["var_naming_errors"]
exp_num_errors += file_summary["var_num_errors"]
exp_spatial_errors += file_summary["var_spatial_errors"]
exp_time_errors += file_summary["var_time_errors"]
exp_attr_errors += file_summary["var_attr_errors"]
else:
log_file.write("\n ")
log_file.write("**********************************************************\n")
log_file.write(" ** Experiment: " + experiment_name + " \n ")
log_file.write("**********************************************************\n")
log_file.write("\n ")
log_file.write(
"ERROR: The compliance check is ignored for experiment "
+ experiment_name
+ " as it is not in "
+ str([exp["experiment"] for exp in experiments])
+ ". \n"
)
exp_naming_errors += 1
report_naming_issues.append(
"Compliance check ignored : experiment "
+ experiment_name
+ " not in the experiments list."
)
exp_errors = (
exp_time_errors
+ exp_spatial_errors
+ exp_num_errors
+ exp_naming_errors
+ exp_attr_errors
+ exp_file_errors
)
return {
"file_counter": file_counter,
"experiment_name": experiment_name,
"exp_errors": exp_errors,
"exp_naming_errors": exp_naming_errors,
"exp_num_errors": exp_num_errors,
"exp_spatial_errors": exp_spatial_errors,
"exp_time_errors": exp_time_errors,
"exp_attr_errors": exp_attr_errors,
"exp_file_errors": exp_file_errors,
}
def _process_single_file(
log_file,
source_path: str,
file: str,
experiment_name: str,
ismip_var,
ismip_meta,
experiments,
report_naming_issues,
):
var_naming_errors = 0
var_num_errors = 0
var_spatial_errors = 0
var_time_errors = 0
var_attr_errors = 0
file_name = os.path.basename(file)
file_name_split = file_name.split("_")
considered_variable = file_name_split[ISMIP7_FILENAME_VAR_IDX]
region = file_name_split[ISMIP7_FILENAME_REGION_IDX]
try:
ds = xr.open_dataset(os.path.join(source_path, file),
decode_times=False)
except (ValueError, TypeError) as e:
log_file.write(" - ERROR: Cannot open " + file_name + ": " + str(e) + "\n")
var_naming_errors += 1
return {
"var_naming_errors": var_naming_errors,
"var_num_errors": var_num_errors,
"var_spatial_errors": var_spatial_errors,
"var_time_errors": var_time_errors,
"var_attr_errors": var_attr_errors,
}
file_variables = list(ds.data_vars)
if len(file_name_split) != ISMIP7_FILENAME_PARTS:
log_file.write(
" - ERROR: the file name "
+ file_name
+ " does not follow the naming convention (expected "
+ str(ISMIP7_FILENAME_PARTS)
+ " underscore-separated fields).\n"
)
report_naming_issues.append(
"Compliance check ignored: file "
+ file_name
+ " does not follow the naming convention."
)
var_naming_errors += 1
return {
"var_naming_errors": var_naming_errors,
"var_num_errors": var_num_errors,
"var_spatial_errors": var_spatial_errors,
"var_time_errors": var_time_errors,
"var_attr_errors": var_attr_errors,
}
experiment_varname = file_name_split[ISMIP7_FILENAME_EXPERIMENT_IDX]
if experiment_varname != experiment_name:
log_file.write(
" - ERROR: in the file name "
+ file_name
+ ", the experiment name ("
+ experiment_varname
+ ") does not match the expected experiment: "
+ experiment_name
+ ".\n"
)
report_naming_issues.append(
"Compliance check ignored: in the file name "
+ file_name
+ ", the experiment name ("
+ experiment_varname
+ ") does not match the expected experiment: "
+ experiment_name
+ ".\n"
)
var_naming_errors += 1
return {
"var_naming_errors": var_naming_errors,
"var_num_errors": var_num_errors,
"var_spatial_errors": var_spatial_errors,
"var_time_errors": var_time_errors,
"var_attr_errors": var_attr_errors,
}
if considered_variable in ismip_var:
var_naming_errors, var_num_errors, var_spatial_errors, var_time_errors, var_attr_errors = (
_run_variable_checks(
log_file=log_file,
ds=ds,
file_name=file_name,
considered_variable=considered_variable,
experiment_name=experiment_name,
file_variables=file_variables,
region=region,
ismip_var=ismip_var,
ismip_meta=ismip_meta,
experiments=experiments,
report_naming_issues=report_naming_issues,
)
)
var_errors = var_naming_errors + var_num_errors + var_spatial_errors + var_time_errors + var_attr_errors
log_file.write("\n")
log_file.write("----------------------------------------------------------\n")
log_file.write(
experiment_name + " - " + considered_variable + " - File:" + file_name + "\n"
)
if var_errors > 0:
log_file.write(str(var_errors) + " error(s). Please review before sharing.\n")
else:
log_file.write("No errors. Good job !\n")
log_file.write("No warnings.\n")
log_file.write("----------------------------------------------------------\n")
return {
"var_naming_errors": var_naming_errors,
"var_num_errors": var_num_errors,
"var_spatial_errors": var_spatial_errors,
"var_time_errors": var_time_errors,
"var_attr_errors": var_attr_errors,
}
def _check_naming(
log_file,
ds,
file_name: str,
region: str,
dim: set,
isscalar: bool,
report_naming_issues: list,
) -> int:
errors = 0
log_file.write("NAMING Tests \n")
if not isscalar and not {"x", "y"}.issubset(dim):
log_file.write(
" - ERROR: Compliance check ignored: x or y in the mandatory dimensions (x,y,t) is missing.\n"
)
log_file.write(
" Only " + str(list(dim)) + " has been detected.\n"
)
report_naming_issues.append(
"Compliance check ignored: x or y in the mandatory dimensions (x,y,t) is missing in "
+ file_name
)
return errors + 1
if region not in ["AIS", "GrIS"]:
log_file.write(
" - ERROR: Region "
+ region
+ " not recognized. It should be AIS or GrIS. The compliance check has been interrupted for this variable.\n"
)
report_naming_issues.append(
"Compliance check ignored: region (AIS/GrIS) not identified in the file "
+ file_name
+ " due to wrong naming."
)
errors += 1
parts = file_name.split("_")
if len(parts) == ISMIP7_FILENAME_PARTS:
ism_member = parts[ISMIP7_FILENAME_ISM_MEMBER_IDX]
if not re.fullmatch(r"m\d{3}", ism_member):
log_file.write(
f" - ERROR: ISM member id '{ism_member}' (field {ISMIP7_FILENAME_ISM_MEMBER_IDX}) does not match expected format mNNN (e.g. m001).\n"
)
errors += 1
esm_name = parts[ISMIP7_FILENAME_ESM_IDX]
if esm_name not in VALID_ESM_NAMES:
log_file.write(
f" - ERROR: ESM name '{esm_name}' (field {ISMIP7_FILENAME_ESM_IDX}) is not a recognised CMIP6/CMIP7 model name.\n"
)
errors += 1
forcing_member = parts[ISMIP7_FILENAME_FORCING_MEMBER_IDX]
if not re.fullmatch(r"f\d{3}", forcing_member):
log_file.write(
f" - ERROR: forcing member id '{forcing_member}' (field {ISMIP7_FILENAME_FORCING_MEMBER_IDX}) does not match expected format fNNN (e.g. f001).\n"
)
errors += 1
set_counter = parts[ISMIP7_FILENAME_SET_COUNTER_IDX]
if not re.fullmatch(r"[CEP]\d{3}", set_counter):
log_file.write(
f" - ERROR: set counter '{set_counter}' (field {ISMIP7_FILENAME_SET_COUNTER_IDX}) does not match expected format [C|E|P]NNN (e.g. C001, E041, P132).\n"
)
errors += 1
year_range_field = parts[ISMIP7_FILENAME_YEAR_RANGE_IDX].removesuffix(".nc")
year_range_match = re.fullmatch(r"(\d{4})-(\d{4})", year_range_field)
if not year_range_match:
log_file.write(
f" - ERROR: year range '{year_range_field}' (field {ISMIP7_FILENAME_YEAR_RANGE_IDX}) does not match expected format YYYY-YYYY (e.g. 2015-2300).\n"
)
errors += 1
else:
fn_start_year = int(year_range_match.group(1))
fn_end_year = int(year_range_match.group(2))
if fn_start_year > fn_end_year:
log_file.write(
f" - ERROR: year range '{year_range_field}': start year {fn_start_year} is after end year {fn_end_year}.\n"
)
errors += 1
elif "time" in ds.coords:
_decoded_time = xr.decode_cf(ds, decode_times=xr.coders.CFDatetimeCoder(use_cftime=True))["time"]
actual_start = min(_decoded_time).item().year
actual_end = max(_decoded_time).item().year
if fn_start_year != actual_start:
log_file.write(
f" - ERROR: filename start year {fn_start_year} does not match first time step year {actual_start}.\n"
)
errors += 1
if fn_end_year != actual_end:
log_file.write(
f" - ERROR: filename end year {fn_end_year} does not match last time step year {actual_end}.\n"
)
errors += 1
if fn_start_year == actual_start and fn_end_year == actual_end:
log_file.write(
f" - Filename year range {fn_start_year}-{fn_end_year} matches time axis: OK\n"
)
return errors
def _check_numerical(
log_file,
ds,
ivar: str,
ismip_meta: list,
var_index: int,
region: str,
isscalar: bool,
) -> int:
errors = 0
log_file.write("NUMERICAL Tests \n")
if ds[ivar].attrs["units"] == ismip_meta[var_index]["units"]:
log_file.write(" - The unit is correct: " + ds[ivar].attrs["units"] + "\n")
else:
log_file.write(
" - ERROR: The unit of the variable is "
+ ds[ivar].attrs["units"]
+ " and should be "
+ ismip_meta[var_index]["units"]
+ " \n"
)
errors += 1
if not isscalar:
if False in ds[ivar].isnull():
if (
ds[ivar].min(skipna=True).item()
>= ismip_meta[var_index]["min_value_" + region.lower()]
):
log_file.write(" - The minimum value successfully verified.\n")
else:
log_file.write(
" - ERROR: The minimum value ("
+ str(ds[ivar].min(skipna=True).values.item(0))
+ ") is out of range. Min value accepted: "
+ str(ismip_meta[var_index]["min_value_" + region.lower()])
+ "\n"
)
errors += 1
if (
ds[ivar].max(skipna=True).item()
<= ismip_meta[var_index]["max_value_" + region.lower()]
):
log_file.write(" - The maximum value successfully verified.\n")
else:
log_file.write(
" - ERROR: The maximum value ("
+ str(ds[ivar].max(skipna=True).values.item(0))
+ ") is out of range. Max value accepted: "
+ str(ismip_meta[var_index]["max_value_" + region.lower()])
+ "\n"
)
errors += 1
else:
log_file.write(" - ERROR: The array only contains missing values.\n")
errors += 1
return errors
def _check_spatial(
log_file,
ds,
grid_extent: list,
possible_resolution: list,
) -> int:
errors = 0
log_file.write("SPATIAL Tests \n")
coords = ds.coords.to_dataset()
Xbottomleft = int(min(coords["x"]).values.item())
Ybottomleft = int(min(coords["y"]).values.item())
Xtopright = int(max(coords["x"]).values.item())
Ytopright = int(max(coords["y"]).values.item())
if Xbottomleft == grid_extent[0] and Ybottomleft == grid_extent[1]:
log_file.write(" - Grid: Lowest left corner is well defined.\n")
else:
log_file.write(
" - ERROR: Lowest left corner of the grid ["
+ str(Xbottomleft) + "," + str(Ybottomleft)
+ "] is not correctly defined. ["
+ str(grid_extent[0]) + "," + str(grid_extent[1])
+ "] Expected\n"
)
errors += 1
if Xtopright == grid_extent[2] and Ytopright == grid_extent[3]:
log_file.write(" - Grid: Upper right corner is well defined.\n")
else:
log_file.write(
" - ERROR: Upper right corner of the grid ["
+ str(Xtopright) + "," + str(Ytopright)
+ "] is not correctly defined. ["
+ str(grid_extent[2]) + "," + str(grid_extent[3])
+ "] Expected\n"
)
errors += 1
Xresolution = round((coords["x"][1].values - coords["x"][0].values) / 1000, 0)
Yresolution = round((coords["y"][1].values - coords["y"][0].values) / 1000, 0)
if Xresolution in set(possible_resolution) and Yresolution in set(possible_resolution):
log_file.write(
" - The grid resolution ("
+ str(int(Xresolution))
+ " km) was successfully verified.\n"
)
else:
log_file.write(
" - ERROR: resolution x="
+ str(Xresolution)
+ " km, y="
+ str(Yresolution)
+ " km is not an authorized grid resolution. Allowed: "
+ str(possible_resolution)
+ " km\n"
)
errors += 1
return errors
def _check_time(
log_file,
ds,
dim: set,
experiments: list,
experiment_name: str,
) -> int:
errors = 0
log_file.write("TIME Tests \n")
if not ({"t"}.issubset(dim) or {"time"}.issubset(dim)):
log_file.write(
" - ERROR: The time dimension is missing. Time Tests have been ignored.\n"
)
return errors + 1
time_dim = "time" if "time" in ds.dims else "t"
unlimited_dims = ds.encoding.get("unlimited_dims", set())
if time_dim in unlimited_dims:
log_file.write(" - Time is a record (unlimited) dimension: OK\n")
else:
log_file.write(
f" - ERROR: dimension '{time_dim}' is not a record (unlimited) dimension.\n"
)
errors += 1
try:
ds = xr.decode_cf(ds, decode_times=xr.coders.CFDatetimeCoder(use_cftime=True))
except Exception as err:
log_file.write(
" - ERROR: The time coordinate could not be decoded. Time checks cannot proceed.\n"
)
errors += 1
# we can't proceed because the next steps will crash
return errors
start_exp = min(ds["time"]).values.astype("datetime64[D]")
end_exp = max(ds["time"]).values.astype("datetime64[D]")
duration_years = end_exp.item().year - start_exp.item().year + 1
index_exp = [dic["experiment"] for dic in experiments].index(experiment_name)
if not (
np.issubdtype(start_exp.dtype, np.datetime64)
& np.issubdtype(end_exp.dtype, np.datetime64)
):
log_file.write(
" - ERROR: the time format of the Netcdf file is not recognized. Time Tests have been ignored.\n"
)
return errors + 1
if not _strictly_increasing(ds.coords["time"]):
log_file.write(
" - ERROR: the time series is not monotonically increasing. Time segments may have been concatenated in the wrong order.\n"
)
return errors + 1
if len(ds["time"].values) > 1:
if isinstance(ds["time"].values[1] - ds["time"].values[0], datetime.timedelta):
time_step = (ds["time"].values[1] - ds["time"].values[0]).days
elif isinstance(ds["time"].values[1] - ds["time"].values[0], np.timedelta64):
time_step = np.timedelta64(
ds["time"].values[1] - ds["time"].values[0], "D"
) / np.timedelta64(1, "D")
else:
time_step = ds["time"].values[1] - ds["time"].values[0]
if TIME_STEP_MIN_DAYS <= time_step <= TIME_STEP_MAX_DAYS:
log_file.write(" - Time step: " + str(time_step) + " days\n")
else:
log_file.write(
" - ERROR: the time step ("
+ str(time_step)
+ ") should be comprised between ["
+ str(TIME_STEP_MIN_DAYS)
+ " and "
+ str(TIME_STEP_MAX_DAYS)
+ "].\n"
)
errors += 1
else:
log_file.write(" - Only one time step present; time step check skipped.\n")
exp = experiments[index_exp]
dateformat_end_exp = datetime.datetime(
end_exp.item().year, end_exp.item().month, end_exp.item().day
)
if exp["duration"] == -1:
# Undetermined length: only require >= 1 year, start and end date in range
if duration_years >= 1:
log_file.write(
" - Experiment lasts " + str(duration_years) + " years (>= 1 year required): OK\n"
)
else:
log_file.write(
" - ERROR: the experiment lasts "
+ str(duration_years)
+ " years but must be at least 1 year long.\n"
)
errors += 1
dateformat_start_exp = datetime.datetime(
start_exp.item().year, start_exp.item().month, start_exp.item().day
)
if exp["startinf"] <= dateformat_start_exp <= exp["startsup"]:
log_file.write(
" - Experiment starts correctly on "
+ start_exp.item().strftime("%Y-%m-%d")