[1888] | 1 | import glob |
---|
| 2 | import sys |
---|
| 3 | import subprocess |
---|
| 4 | import os |
---|
| 5 | import json |
---|
| 6 | import itertools |
---|
| 7 | import copy |
---|
| 8 | |
---|
[2214] | 9 | import netCDF4 |
---|
| 10 | from netCDF4 import Dataset |
---|
| 11 | import numpy as np |
---|
[1888] | 12 | |
---|
| 13 | mode=os.getenv("mode") |
---|
| 14 | arch=os.getenv("arch") |
---|
| 15 | svnr=os.getenv("svnR") |
---|
[1890] | 16 | ref_location=os.getenv("ref_location") |
---|
| 17 | ref_file=os.getenv("ref_file") |
---|
[1888] | 18 | |
---|
[1909] | 19 | |
---|
| 20 | |
---|
[1888] | 21 | def OSinfo(runthis): |
---|
| 22 | red = lambda text: '\033[0;31m' + text + '\033[0m' |
---|
[2139] | 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: |
---|
[1888] | 27 | print(red(runthis+" FAILED")) |
---|
[2139] | 28 | print(theErr) |
---|
[1888] | 29 | sys.exit() |
---|
| 30 | |
---|
[2139] | 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() |
---|
[1888] | 39 | |
---|
| 40 | |
---|
[2139] | 41 | |
---|
[1888] | 42 | def nonblank_lines(f): |
---|
| 43 | for l in f: |
---|
| 44 | line = l.rstrip() |
---|
| 45 | if line and not line.startswith("#"): |
---|
| 46 | yield line |
---|
| 47 | |
---|
| 48 | def main(): |
---|
[1909] | 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 | |
---|
[1910] | 56 | ref_rev = "" |
---|
[1909] | 57 | for ref in ref_list: |
---|
| 58 | if int(svnr) >= ref : |
---|
[1915] | 59 | ref_rev = str(ref) |
---|
| 60 | print("corresponding reference = ", ref) |
---|
[1909] | 61 | break |
---|
| 62 | |
---|
[1910] | 63 | if not ref_rev: |
---|
[1909] | 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) |
---|
[1890] | 69 | OSinfo("rm -f "+ref_file) |
---|
[1909] | 70 | |
---|
| 71 | |
---|
[1888] | 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: |
---|
[1890] | 86 | if os.path.exists(config+"/"+checkfile) and os.path.exists("reference/ref_"+config+"/"+checkfile): |
---|
[2214] | 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 |
---|
[2231] | 95 | np.seterr(divide='ignore', invalid='ignore') |
---|
[2214] | 96 | for var in res.variables: |
---|
[2215] | 97 | if (not (var.startswith('lon_'))) and (not (var.startswith('lat_'))) and (not (var.startswith('time_'))) and (not (var.startswith('atm__'))): |
---|
[2214] | 98 | ref_interp = ref.variables[var] |
---|
| 99 | ref_array = ref_interp[:] |
---|
| 100 | res_interp = res.variables[var] |
---|
| 101 | res_array = res_interp[:] |
---|
[2216] | 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 |
---|
[2214] | 113 | report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(validated)+"\n") |
---|
[2139] | 114 | |
---|
| 115 | elif os.path.exists(config+"/"+checkfile): # if no ref file -> set 0 |
---|
[1888] | 116 | report.write(folder_name+" "+folder_name+"@"+config_name+" "+folder_name+"@"+config_name+"@"+checkfile+" "+str(0)+"\n") |
---|
[2139] | 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 | |
---|
[1888] | 120 | |
---|
| 121 | |
---|
| 122 | if __name__== "__main__": |
---|
| 123 | main() |
---|