-
Notifications
You must be signed in to change notification settings - Fork 37
/
basepmpdriver.py
89 lines (83 loc) · 3.39 KB
/
basepmpdriver.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
import basepmp
import os
import sys
import pcmdi_metrics
import glob
import shutil
from pcmdi_metrics.pcmdi import PMPDriver, create_mean_climate_parser
class PMPDriverTest(basepmp.PMPTest):
def setUp(self):
self.path_parameter_files = os.path.join(
os.path.dirname(__file__), "pcmdi")
self.traceback = eval(os.environ.get("TRACEBACK", "False"))
self.update = eval(os.environ.get("UPDATE_TESTS", "False"))
def runPMP(self, parameterFile):
if self.traceback:
tb = "-t"
else:
tb = ""
print()
print()
print()
print()
print("---------------------------------------------------")
print("RUNNING:", parameterFile)
print("---------------------------------------------------")
print()
print()
print()
print()
parser = create_mean_climate_parser()
parser.add_args_and_values(['-p', parameterFile])
parameter = parser.get_parameter(
cmd_default_vars=False, argparse_vals_only=False)
driver = PMPDriver(parameter)
driver.run_diags()
parameters, files = self.assertFilesOut(parameterFile)
for fnm in files:
nm = os.path.basename(fnm)
# Ok now we are trying to find the same file
good_files = glob.glob(
os.path.dirname(__file__) +
"/pcmdi/%s/*.json" %
parameters.case_id)
print("GOOD FILES:", good_files)
if len(good_files) == 0:
raise Exception(" ".join("could not find good files",
__file__, os.path.dirname(__file__),
"/pcmdi/%s/*.json" % parameters.case_id))
allCorrect = True
for gnm in good_files:
if os.path.basename(gnm) == nm:
print("comparing:", fnm, gnm)
if self.update:
shutil.copy(fnm, gnm)
else:
correct = self.assertSimilarJsons(
fnm, gnm, rtol=5.E-3, atol=0., raiseOnError=False)
if not correct and os.path.exists(gnm+".mac"):
correct = self.assertSimilarJsons(
fnm, gnm+".mac", rtol=5.E-3, atol=0, raiseOnError=False)
allCorrect = allCorrect and correct
if not allCorrect:
raise Exception(
"Error Encountered on some of the output files, check log")
def assertFilesOut(self, parameterFile):
# Ok at that point we we can start testing things
pth, fnm = os.path.split(parameterFile)
if pth != "":
sys.path.append(pth)
if fnm.lower()[-3:] == ".py":
fnm = fnm[:-3]
parameters = __import__(fnm, globals(), locals(), [], 0)
# Ok now let's figure out where the results have been dumped
pthout = pcmdi_metrics.io.base.Base(
os.path.join(
parameters.metrics_output_path),
"*.json")
pthout.case_id = parameters.case_id
files = glob.glob(pthout())
if len(files) == 0:
raise Exception(
"could not find output files after running mean_climate_driver on parameter file: %s" % parameterFile)
return parameters, files