source: XIOS/dev/dev_ym/XIOS_COUPLING/xios_test_suite/TEST_SUITE/step2.py @ 2214

Last change on this file since 2214 was 2214, checked in by jderouillat, 3 years ago

Introduce a tolerance threshold in XIOS_COUPLING test suite (moving from cdo diff to a Python comparison, especially to compare results of different compilers)

File size: 4.8 KB
Line 
1import glob
2import sys
3import subprocess
4import os
5import json
6import itertools
7import copy
8
9import netCDF4
10from netCDF4 import Dataset
11import numpy as np
12
13mode=os.getenv("mode")
14arch=os.getenv("arch")
15svnr=os.getenv("svnR")
16ref_location=os.getenv("ref_location")
17ref_file=os.getenv("ref_file")
18
19
20
21def OSinfo(runthis):
22    red = lambda text: '\033[0;31m' + text + '\033[0m'
23    osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
24    theInfo, theErr = osstdout.communicate()
25    #print(theInfo)
26    if theErr:
27        print(red(runthis+" FAILED"))
28        print(theErr)
29        sys.exit()
30
31#def OSinfo(runthis):
32#    red = lambda text: '\033[0;31m' + text + '\033[0m'
33#    osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
34#    theInfo = osstdout.communicate()[0].strip()
35#    if osstdout.returncode!=0:
36#        print(red(runthis+" FAILED"))
37#        print(theInfo)
38#        sys.exit()
39
40
41
42def nonblank_lines(f):
43    for l in f:
44        line = l.rstrip()
45        if line and not line.startswith("#"):
46            yield line
47
48def main():
49    ref_list = glob.glob(ref_location+"/*")
50    for i in range(len(ref_list)):
51        tmp = ref_list[i].split("/")
52        rev = tmp[len(tmp)-1]
53        ref_list[i] = int(rev)
54    ref_list.sort(reverse=True) #ref_list in descending order
55   
56    ref_rev = ""
57    for ref in ref_list:
58        if int(svnr) >= ref :
59            ref_rev = str(ref)
60            print("corresponding reference = ", ref)
61            break
62       
63    if not ref_rev:
64        print("no available reference found ... exit")
65        return
66   
67    OSinfo("cp "+ref_location+"/"+ref_rev+"/"+ref_file+" ./")
68    OSinfo("tar -zxvf "+ref_location+"/"+ref_rev+"/"+ref_file)
69    OSinfo("rm -f "+ref_file)
70   
71   
72    test_folder_list = glob.glob('test_*')
73
74    for test_folder in test_folder_list:
75        config_list = glob.glob(test_folder+"/CONFIG_*")
76       
77       
78        with open(test_folder+"/checkfile.def", "r") as fh:
79            checkfiles = list(nonblank_lines(fh))
80
81        with open("report_"+svnr+"_"+arch+"_"+mode+".txt", "a") as report:
82            for config in config_list:
83                folder_name = list(config.split("/"))[0]
84                config_name = list(config.split("/"))[1]
85                for checkfile in checkfiles:
86                    if os.path.exists(config+"/"+checkfile) and os.path.exists("reference/ref_"+config+"/"+checkfile):
87                        #OSinfo("cdo -W diffn "+config+"/"+checkfile+" "+"reference/ref_"+config+"/"+checkfile+"  2>&1 |grep -v 'Found more than one time variable'|grep -v 'cdo diffn: Processed'|grep -v 'cdo    diffn: Processed'|grep -v 'Time variable >time_counter< not found!' > diff_"+checkfile+".txt")
88                        #if os.stat("diff_"+checkfile+".txt").st_size==0: # if no diff -> set 0
89                        #    report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(1)+"\n")
90                        #else: # if cdo diffn returns diff -> set -1
91                        #    report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(-1)+"\n")
92                        ref = Dataset( "reference/ref_"+config+"/"+checkfile )
93                        res = Dataset( config+"/"+checkfile )
94                        validated = 1
95                        for var in res.variables:
96                            if (not (var.startswith('lon_'))) and (not (var.startswith('lat_'))) and (not (var.startswith('time_'))):
97                                ref_interp = ref.variables[var]
98                                ref_array = ref_interp[:]
99                                res_interp = res.variables[var]
100                                res_array = res_interp[:]
101                                diff = (ref_array-res_array)/ref_array
102                                if ( np.max(np.abs(diff)) >  2*10**-3 ):
103                                    print( var,  ", max relative error : ", np.max(diff) )
104                                    validated = -1
105                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(validated)+"\n")
106
107                    elif os.path.exists(config+"/"+checkfile): # if no ref file -> set 0
108                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(0)+"\n")
109                    elif os.path.exists("reference/ref_"+config+"/"+checkfile): # if no output file -> set -2
110                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(-2)+"\n")
111
112                   
113
114if __name__== "__main__":
115  main()
Note: See TracBrowser for help on using the repository browser.