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

Last change on this file since 2283 was 2283, checked in by jderouillat, 2 years ago

Fix the test about memory consumption (exclude source path from checks)

File size: 7.1 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")
15enable_mem_track=os.getenv("enable_mem_track")
16svnr=os.getenv("svnR")
17ref_location=os.getenv("ref_location")
18ref_file=os.getenv("ref_file")
19
20
21
22def OSinfo(runthis):
23    red = lambda text: '\033[0;31m' + text + '\033[0m'
24    osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
25    theInfo, theErr = osstdout.communicate()
26    #print(theInfo)
27    if theErr:
28        print(red(runthis+" FAILED"))
29        print(theErr)
30        sys.exit()
31
32#def OSinfo(runthis):
33#    red = lambda text: '\033[0;31m' + text + '\033[0m'
34#    osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
35#    theInfo = osstdout.communicate()[0].strip()
36#    if osstdout.returncode!=0:
37#        print(red(runthis+" FAILED"))
38#        print(theInfo)
39#        sys.exit()
40
41
42
43def nonblank_lines(f):
44    for l in f:
45        line = l.rstrip()
46        if line and not line.startswith("#"):
47            yield line
48
49def main():
50    ref_list = glob.glob(ref_location+"/*")
51    for i in range(len(ref_list)):
52        tmp = ref_list[i].split("/")
53        rev = tmp[len(tmp)-1]
54        ref_list[i] = int(rev)
55    ref_list.sort(reverse=True) #ref_list in descending order
56   
57    ref_rev = ""
58    for ref in ref_list:
59        if int(svnr) >= ref :
60            ref_rev = str(ref)
61            print("corresponding reference = ", ref)
62            break
63       
64    if not ref_rev:
65        print("no available reference found ... exit")
66        return
67   
68    OSinfo("cp "+ref_location+"/"+ref_rev+"/"+ref_file+" ./")
69    OSinfo("tar -zxvf "+ref_location+"/"+ref_rev+"/"+ref_file)
70    OSinfo("rm -f "+ref_file)
71   
72   
73    test_folder_list = glob.glob('test_*')
74
75    for test_folder in test_folder_list:
76        report_filename=""
77        # check if test concerns xios features (NetCDF), or memory consumption (mem files)
78        files_list=""
79        flist = open(test_folder+"/checkfile.def", 'r')
80        files_list = flist.read()
81        flist.close()
82        if ( enable_mem_track==None ) and ( not('.mem' in files_list) ) :
83            report_filename = "report_"+svnr+"_"+arch+"_"+mode+".txt"
84        elif ( enable_mem_track=='--memtrack full' ) and ( '.mem' in files_list ) :
85            #report_filename = "report_"+svnr+"_"+arch+"_"+mode+"_memtrack.txt"
86            # -> use same report that for feature test
87            report_filename = "report_"+svnr+"_"+arch+"_"+mode+".txt"
88        else :
89            continue
90
91        config_list = glob.glob(test_folder+"/CONFIG_*")
92       
93       
94        with open(test_folder+"/checkfile.def", "r") as fh:
95            checkfiles = list(nonblank_lines(fh))
96
97        with open(report_filename, "a") as report:
98            for config in config_list:
99                folder_name = list(config.split("/"))[0]
100                config_name = list(config.split("/"))[1]
101                for checkfile in checkfiles:
102                    if os.path.exists(config+"/"+checkfile) and os.path.exists("reference/ref_"+config+"/"+checkfile):
103                        if ( enable_mem_track==None ) and ( not('.mem' in files_list) ): # NetCDF
104                            #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")
105                            #if os.stat("diff_"+checkfile+".txt").st_size==0: # if no diff -> set 0
106                            #    report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(1)+"\n")
107                            #else: # if cdo diffn returns diff -> set -1
108                            #    report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(-1)+"\n")
109                            ref = Dataset( "reference/ref_"+config+"/"+checkfile )
110                            res = Dataset( config+"/"+checkfile )
111                            validated = 1
112                            np.seterr(divide='ignore', invalid='ignore')
113                            for var in res.variables:
114                                if (not (var.startswith('lon_'))) and (not (var.startswith('lat_'))) and (not (var.startswith('time_'))) and (not (var.startswith('atm__'))):
115                                    ref_interp = ref.variables[var]
116                                    ref_array = ref_interp[:]
117                                    res_interp = res.variables[var]
118                                    res_array = res_interp[:]
119                                    if (res_array.shape == ref_array.shape):
120                                        diff = np.zeros_like( ref_array )
121                                        np.divide(ref_array-res_array,ref_array,diff,where=(ref_array[:]>10**-15))
122                                        if ( np.max(np.abs(diff)) >  1*10**-9 ):
123                                            validated = -1
124                                        diff = np.zeros_like( ref_array )
125                                        np.divide(ref_array-res_array,res_array,diff,where=(ref_array[:]>10**-15))
126                                        if ( np.max(np.abs(diff)) >  1*10**-9 ):
127                                            validated = -1
128                                    else:
129                                        validated = -1
130                            report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(validated)+"\n")
131                       
132                        elif ( enable_mem_track=='--memtrack full' ) and ( '.mem' in files_list ) : # mem file
133                            validated = 1
134                            OSinfo("diff "+config+"/"+checkfile+" "+"reference/ref_"+config+"/"+checkfile+" | grep bytes 2>&1  > diff_"+checkfile+".txt")
135                            if os.stat("diff_"+checkfile+".txt").st_size==0: # if no diff -> set 0
136                                validated = 1
137                            else: # if diff returns diff -> set -1
138                                validated = -1
139                            report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(validated)+"\n")
140
141                    elif os.path.exists(config+"/"+checkfile): # if no ref file -> set 0
142                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(0)+"\n")
143                    elif os.path.exists("reference/ref_"+config+"/"+checkfile): # if no output file -> set -2
144                        report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(-2)+"\n")
145
146                   
147
148if __name__== "__main__":
149  main()
Note: See TracBrowser for help on using the repository browser.