1 | import glob |
---|
2 | import sys |
---|
3 | import subprocess |
---|
4 | import os |
---|
5 | import json |
---|
6 | import itertools |
---|
7 | import copy |
---|
8 | |
---|
9 | import netCDF4 |
---|
10 | from netCDF4 import Dataset |
---|
11 | import numpy as np |
---|
12 | |
---|
13 | mode=os.getenv("mode") |
---|
14 | arch=os.getenv("arch") |
---|
15 | enable_mem_track=os.getenv("enable_mem_track") |
---|
16 | svnr=os.getenv("svnR") |
---|
17 | ref_location=os.getenv("ref_location") |
---|
18 | ref_file=os.getenv("ref_file") |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | def 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 | |
---|
43 | def nonblank_lines(f): |
---|
44 | for l in f: |
---|
45 | line = l.rstrip() |
---|
46 | if line and not line.startswith("#"): |
---|
47 | yield line |
---|
48 | |
---|
49 | |
---|
50 | def extract_bytes( filename ): |
---|
51 | print( filename ) |
---|
52 | fmem = open(filename, 'r') |
---|
53 | bytes_tot = 0 |
---|
54 | for line in fmem: |
---|
55 | if not line.startswith("***"): |
---|
56 | continue |
---|
57 | |
---|
58 | bytes_line = line.replace("\n", "") |
---|
59 | print( bytes_line.split() ) |
---|
60 | bytes_str = bytes_line.split()[2] |
---|
61 | if (bytes_str.isdigit() ): |
---|
62 | bytes_tot += int( bytes_str ) |
---|
63 | fmem.close() |
---|
64 | |
---|
65 | return bytes_tot |
---|
66 | |
---|
67 | |
---|
68 | def main(): |
---|
69 | ref_list = glob.glob(ref_location+"/*") |
---|
70 | for i in range(len(ref_list)): |
---|
71 | tmp = ref_list[i].split("/") |
---|
72 | rev = tmp[len(tmp)-1] |
---|
73 | ref_list[i] = int(rev) |
---|
74 | ref_list.sort(reverse=True) #ref_list in descending order |
---|
75 | |
---|
76 | ref_rev = "" |
---|
77 | for ref in ref_list: |
---|
78 | if int(svnr) >= ref : |
---|
79 | ref_rev = str(ref) |
---|
80 | print("corresponding reference = ", ref) |
---|
81 | break |
---|
82 | |
---|
83 | if not ref_rev: |
---|
84 | print("no available reference found ... exit") |
---|
85 | return |
---|
86 | |
---|
87 | OSinfo("cp "+ref_location+"/"+ref_rev+"/"+ref_file+" ./") |
---|
88 | OSinfo("tar -zxvf "+ref_location+"/"+ref_rev+"/"+ref_file) |
---|
89 | OSinfo("rm -f "+ref_file) |
---|
90 | |
---|
91 | |
---|
92 | test_folder_list = glob.glob('test_*') |
---|
93 | |
---|
94 | for test_folder in test_folder_list: |
---|
95 | report_filename="" |
---|
96 | # check if test concerns xios features (NetCDF), or memory consumption (mem files) |
---|
97 | files_list="" |
---|
98 | flist = open(test_folder+"/checkfile.def", 'r') |
---|
99 | files_list = flist.read() |
---|
100 | flist.close() |
---|
101 | if ( enable_mem_track==None ) and ( not('.mem' in files_list) ) : |
---|
102 | report_filename = "report_"+svnr+"_"+arch+"_"+mode+".txt" |
---|
103 | elif ( enable_mem_track=='--memtrack full' ) and ( '.mem' in files_list ) : |
---|
104 | #report_filename = "report_"+svnr+"_"+arch+"_"+mode+"_memtrack.txt" |
---|
105 | # -> use same report that for feature test |
---|
106 | report_filename = "report_"+svnr+"_"+arch+"_"+mode+".txt" |
---|
107 | else : |
---|
108 | continue |
---|
109 | |
---|
110 | config_list = glob.glob(test_folder+"/CONFIG_*") |
---|
111 | |
---|
112 | |
---|
113 | with open(test_folder+"/checkfile.def", "r") as fh: |
---|
114 | checkfiles = list(nonblank_lines(fh)) |
---|
115 | |
---|
116 | with open(report_filename, "a") as report: |
---|
117 | for config in config_list: |
---|
118 | folder_name = list(config.split("/"))[0] |
---|
119 | config_name = list(config.split("/"))[1] |
---|
120 | for checkfile in checkfiles: |
---|
121 | if os.path.exists(config+"/"+checkfile) and os.path.exists("reference/ref_"+config+"/"+checkfile): |
---|
122 | if ( enable_mem_track==None ) and ( not('.mem' in files_list) ): # NetCDF |
---|
123 | #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") |
---|
124 | #if os.stat("diff_"+checkfile+".txt").st_size==0: # if no diff -> set 0 |
---|
125 | # report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(1)+"\n") |
---|
126 | #else: # if cdo diffn returns diff -> set -1 |
---|
127 | # report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(-1)+"\n") |
---|
128 | ref = Dataset( "reference/ref_"+config+"/"+checkfile ) |
---|
129 | res = Dataset( config+"/"+checkfile ) |
---|
130 | validated = 1 |
---|
131 | np.seterr(divide='ignore', invalid='ignore') |
---|
132 | for var in res.variables: |
---|
133 | print("checkfile = ", checkfile, ", var = ", var) |
---|
134 | if (not (var.startswith('lon_'))) and (not (var.startswith('lat_'))) and (not (var.startswith('time_'))) and (not (var.startswith('atm__')) and (var!="lat") and (var!="lon") ) and \ |
---|
135 | (var!="axis") and (not (var.startswith('axis_')))and (not (var.startswith('domain_'))) and (not (var.startswith('default_pool_id__default_gatherer_id_0__atm__'))): |
---|
136 | ref_interp = ref.variables[var] |
---|
137 | ref_array = ref_interp[:] |
---|
138 | res_interp = res.variables[var] |
---|
139 | res_array = res_interp[:] |
---|
140 | if (res_array.shape == ref_array.shape): |
---|
141 | diff = np.zeros_like( ref_array ) |
---|
142 | np.divide(ref_array-res_array,ref_array,diff,where=(ref_array[:]>10**-15)) |
---|
143 | if ( np.max(np.abs(diff)) > 1*10**-9 ): |
---|
144 | validated = -1 |
---|
145 | diff = np.zeros_like( ref_array ) |
---|
146 | np.divide(ref_array-res_array,res_array,diff,where=(ref_array[:]>10**-15)) |
---|
147 | if ( np.max(np.abs(diff)) > 1*10**-9 ): |
---|
148 | validated = -1 |
---|
149 | else: |
---|
150 | validated = -1 |
---|
151 | report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(validated)+"\n") |
---|
152 | |
---|
153 | elif ( enable_mem_track=='--memtrack full' ) and ( '.mem' in files_list ) : # mem file |
---|
154 | validated = 1 |
---|
155 | ref_memory = extract_bytes( "reference/ref_"+config+"/"+checkfile ) |
---|
156 | res_memory = extract_bytes( config+"/"+checkfile ) |
---|
157 | if ( ref_memory == res_memory ) : |
---|
158 | validated = 1 |
---|
159 | else: |
---|
160 | validated = -1 |
---|
161 | report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(validated)+"\n") |
---|
162 | |
---|
163 | elif os.path.exists(config+"/"+checkfile): # if no ref file -> set 0 |
---|
164 | report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(0)+"\n") |
---|
165 | elif os.path.exists("reference/ref_"+config+"/"+checkfile): # if no output file -> set -2 |
---|
166 | report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(-2)+"\n") |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | if __name__== "__main__": |
---|
171 | main() |
---|