-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.test.py
67 lines (55 loc) · 2.07 KB
/
check.test.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
import os, time
import numpy as np
import xarray as xr
from testNameList import nameList
#testIDList = ['tpv8', 'tpv104','tpv1053d','tpv1053d.6c','meng2023a','meng2023cb']
fileNameList = ['fault.dyna.r.nc','frt.txt0','frt.txt1', 'frt.txt2','frt.txt3']
refRoot = 'test.reference.results'
testRoot = 'test'
def compare_nc_files(fn1,fn2,threshold=1e-3):
isTheSame = 'SUCCESS '+fn1+' '+fn2
f1 = xr.open_dataset(fn1)
f2 = xr.open_dataset(fn2)
metadata_equal = f1.identical(f2)
data_equal = (f1==f2).all().items()
# Compare variables
for var in f1.variables:
var1 = f1[var]
var2 = f2[var]
if var1.dims != var2.dims:
isTheSame = 'FAIL var dim '+fn1+' '+fn2
if not np.allclose(var1,var2,rtol=threshold, atol=threshold):
isTheSame = 'FAIL var numbers '+fn1+' '+fn2
if not metadata_equal:# and data_equal:
isTheSame = 'FAIL metadata '+fn1+' '+fn2
try:
xr.testing.assert_allclose(f1,f2)
print('SUCCESS by xarray.testing.assert_allclose')
print('SUCCESS '+fn1 +' '+fn2)
except AssertionError as e:
print(e)
print(isTheSame)
return isTheSame
def compare_txt_files(fn1,fn2,threshold=1e-3):
isTheSame = 'SUCCESS '+fn1+' '+fn2
with open(fn1,'r') as f1, open(fn2,'r') as f2:
result1 = f1.read().split()
result2 = f2.read().split()
if len(result1) != len(result2):
isTheSame = 'FAIL '+fn1+' '+fn2
for num1,num2 in zip(result1,result2):
fnum1, fnum2 = float(num1),float(num2)
if abs(fnum1-fnum2) > threshold:
isTheSame = 'FAIL '+fn1+' '+fn2
print(isTheSame)
for testid in nameList:
print(' ')
for filename in fileNameList:
refPath = refRoot+'/'+testid+'/'+filename
testPath = testRoot+'/'+testid+'/'+filename
if os.path.exists(refPath):
if 'nc' in filename:
print(' ')
#compare_nc_files(refPath, testPath, 1e-3)
elif 'frt' in filename:
compare_txt_files(refPath, testPath, 1e-3)