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

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

Fix for null elements comparison in test suite

File size: 5.4 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                        np.seterr(divide='ignore', invalid='ignore')
96                        for var in res.variables:
97                            if (not (var.startswith('lon_'))) and (not (var.startswith('lat_'))) and (not (var.startswith('time_'))) and (not (var.startswith('atm__'))):
98                                ref_interp = ref.variables[var]
99                                ref_array = ref_interp[:]
100                                res_interp = res.variables[var]
101                                res_array = res_interp[:]
102                                if (res_array.shape == ref_array.shape):
103                                    diff = np.zeros_like( ref_array )
104                                    np.divide(ref_array-res_array,ref_array,diff,where=(ref_array[:]>10**-15))
105                                    if ( np.max(np.abs(diff)) >  1*10**-9 ):
106                                        validated = -1
107                                    diff = np.zeros_like( ref_array )
108                                    np.divide(ref_array-res_array,res_array,diff,where=(ref_array[:]>10**-15))
109                                    if ( np.max(np.abs(diff)) >  1*10**-9 ):
110                                        validated = -1
111                                else:
112                                        validated = -1
113                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(validated)+"\n")
114
115                    elif os.path.exists(config+"/"+checkfile): # if no ref file -> set 0
116                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(0)+"\n")
117                    elif os.path.exists("reference/ref_"+config+"/"+checkfile): # if no output file -> set -2
118                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(-2)+"\n")
119
120                   
121
122if __name__== "__main__":
123  main()
Note: See TracBrowser for help on using the repository browser.