|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# |
| 4 | +#(c) 2017 Adewale Amosu |
| 5 | +# |
| 6 | +# |
| 7 | +############################### |
| 8 | + |
| 9 | +import xlrd |
| 10 | +import sys |
| 11 | +import numpy as np |
| 12 | +import os |
| 13 | +import mmap |
| 14 | +import pprint |
| 15 | +import sys |
| 16 | +import matplotlib.pyplot as plt |
| 17 | +from matplotlib import colors |
| 18 | +from matplotlib.ticker import MultipleLocator |
| 19 | +import re |
| 20 | + |
| 21 | +############################## |
| 22 | +print " " |
| 23 | +print " ------------------------------------------------------------------------------------------------" |
| 24 | +print " PyLogFinder.py: Program to check if specific logs are in multiple LAS files using any Mnemonic " |
| 25 | +print " (c) 2017 Authors: Adewale Amosu, Hamdi Mahmood" |
| 26 | +print " Texas A&M University" |
| 27 | +print " ------------------------------------------------------------------------------------------------" |
| 28 | +print " " |
| 29 | +print " Number of arguments: ", len(sys.argv) |
| 30 | +print " " |
| 31 | +print " The arguments are: " , str(sys.argv) |
| 32 | +print " " |
| 33 | + |
| 34 | + |
| 35 | +if len(sys.argv) <=1: |
| 36 | + print " ------------------------------------------------------------------------------------------------" |
| 37 | + print " " |
| 38 | + print " Usage: python PyLogFinder.py Foldername_containing_LAS_files, List_of_Mnemonics_separated_by_commas" |
| 39 | + print " " |
| 40 | + print " e.g: python PyLogFinder.py TEST_DATA,DT,DTS,GR,LLD,PE" |
| 41 | + print " " |
| 42 | + print " See 'Database.xls' for full list of Mnemonics or to add new Mnemonics" |
| 43 | + print " " |
| 44 | + print " ------------------------------------------------------------------------------------------------" |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | +strval2 = sys.argv[1].split(',') |
| 48 | +strval=strval2[1:] |
| 49 | +#print strval |
| 50 | + |
| 51 | +if len(strval2) <2: |
| 52 | + print " ------------------------------------------------------------------------------------------------" |
| 53 | + print " " |
| 54 | + print " Usage: python PyLogFinder.py Foldername_containing_LAS_files, List_of_Mnemonics_separated_by_commas" |
| 55 | + print " " |
| 56 | + print " e.g: python PyLogFinder.py TEST_DATA,DT,DTS,GR,LLD,PE" |
| 57 | + print " " |
| 58 | + print " See 'Database.xls' for full list of Mnemonics or to add new Mnemonics" |
| 59 | + print " " |
| 60 | + print " ------------------------------------------------------------------------------------------------" |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + |
| 64 | +############################### |
| 65 | +## folder,store files in array |
| 66 | +# |
| 67 | +foldername=strval2[0] |
| 68 | +print " FolderName:", foldername |
| 69 | +print " " |
| 70 | + |
| 71 | +dirlist=os.listdir(foldername) |
| 72 | +files=[] |
| 73 | +for filename in dirlist: |
| 74 | + if ".LAS" in filename: |
| 75 | + files.append('./'+foldername+'/'+filename) |
| 76 | +for filename in dirlist: |
| 77 | + if ".las" in filename: |
| 78 | + files.append('./'+foldername+'/'+filename) |
| 79 | +## print files |
| 80 | +############################## |
| 81 | + |
| 82 | + |
| 83 | +############################## |
| 84 | +#Extract from Database |
| 85 | +# |
| 86 | +data = xlrd.open_workbook('Database.xls') |
| 87 | +sheetname = data.sheet_names() |
| 88 | +logall = data.sheet_by_index(0) |
| 89 | + |
| 90 | +valMN = [c.value for c in logall.col(0)] |
| 91 | +valTY = [c.value for c in logall.col(2)] |
| 92 | +valUN = [c.value for c in logall.col(3)] |
| 93 | +valDE = [c.value for c in logall.col(4)] |
| 94 | +#Print Data |
| 95 | +#for k in range(0, len(valMN)): |
| 96 | +# print valMN[k]," : ", valTY[k]," : ", valDE[k] |
| 97 | +############################# |
| 98 | + |
| 99 | + |
| 100 | +############################## |
| 101 | +# index2checkm |
| 102 | +inputind = [] |
| 103 | +checkm =[] |
| 104 | +for k in range(0, len(strval)): |
| 105 | + inputind.append(valMN.index(strval[k])) |
| 106 | + ind1 = valMN.index(strval[k]) |
| 107 | + checkm.append((valTY[ind1])) |
| 108 | + print " ", strval[k], inputind[k], checkm[k] |
| 109 | +############################## |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +############################# |
| 114 | +# Extract Mnemonics, grep in file, results store in 2D array |
| 115 | +#results = [[0 for _ in range(0, len(files))] for _ in range(0, len(checkm))] |
| 116 | +#pprint.pprint(results) |
| 117 | +results=np.zeros(shape=(len(files), len(checkm))) |
| 118 | +#pprint.pprint(results) |
| 119 | + |
| 120 | +for j in range(0, len(files)): |
| 121 | + print " ------------------------------------------------------" |
| 122 | + for k in range(0, len(checkm)): |
| 123 | + print " " |
| 124 | + indices = [i for i, x in enumerate(valTY) if x == checkm[k]] |
| 125 | + grepL = np.array(valMN)[indices] |
| 126 | + #print checkm[k], indices |
| 127 | + for mnem in grepL: |
| 128 | + #if mnem+'.' in open(files[j]).read(): |
| 129 | + f = open(files[j]) |
| 130 | + s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) |
| 131 | + #if s.find(mnem) != -1: |
| 132 | + if re.search(r'\b' + mnem + r'\b', s): |
| 133 | + results[j][k]=1 |
| 134 | + print " ",j,k, files[j], mnem, results[j][k] |
| 135 | + break |
| 136 | + |
| 137 | +#pprint.pprint(results) |
| 138 | + |
| 139 | +############################# |
| 140 | +# print result to screen and files |
| 141 | +f = open(foldername+"_Results.txt",'w') |
| 142 | +print " " |
| 143 | +sys.stdout.write(" ") |
| 144 | +sys.stdout.write("Files ") |
| 145 | +f.write("Files ") |
| 146 | +for k in range(0, len(strval)): |
| 147 | + sys.stdout.write(strval[k]+" ") |
| 148 | + f.write(strval[k]+" ") |
| 149 | +print " " |
| 150 | +f.write('\n') |
| 151 | +for j in range(0, len(files)): |
| 152 | + #print(files[j], " ", end='') |
| 153 | + sys.stdout.write(" ") |
| 154 | + sys.stdout.write(files[j]) |
| 155 | + sys.stdout.write(" ") |
| 156 | + f.write(str(files[j])) |
| 157 | + f.write(" ") |
| 158 | + for k in range(0, len(checkm)): |
| 159 | + #print results[j, k] |
| 160 | + sys.stdout.write(" ") |
| 161 | + sys.stdout.write("%d " % (results[j,k])) |
| 162 | + sys.stdout.write(" ") |
| 163 | + f.write(str(results[j,k])) |
| 164 | + f.write(" ") |
| 165 | + print " " |
| 166 | + f.write('\n') |
| 167 | + |
| 168 | +f.close() |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +################################## |
| 173 | +#Plots |
| 174 | +row_labels=files |
| 175 | +col_labels=strval |
| 176 | +cmap = colors.ListedColormap(['white', 'springgreen']) |
| 177 | +#cmap = colors.ListedColormap(['white', 'red']) |
| 178 | +fig, ax = plt.subplots(figsize=(25, 15)) |
| 179 | +ax.imshow(results, cmap=cmap, interpolation='nearest', aspect='auto') |
| 180 | +ax.grid(which='minor', linestyle='-', linewidth=2,alpha=1) |
| 181 | +ax.xaxis.set_ticks_position("top") |
| 182 | + |
| 183 | +plt.rc('font', size=23) |
| 184 | +plt.xticks(range(0, len(strval)), col_labels) |
| 185 | +plt.yticks(range(0, len(files)), row_labels) |
| 186 | +locs = np.arange(len(row_labels)) |
| 187 | +for axis in [ax.yaxis]: |
| 188 | + axis.set_ticks(locs + 0.5, minor=True) |
| 189 | + axis.set(ticks=locs) |
| 190 | +locs = np.arange(len(col_labels)) |
| 191 | +for axis in [ax.xaxis]: |
| 192 | + axis.set_ticks(locs + 0.5, minor=True) |
| 193 | + axis.set(ticks=locs) |
| 194 | +ax.grid(True, which='minor') |
| 195 | +plt.gca().set_position([0.25, 0.2, 0.65, 0.7]) |
| 196 | +# |
| 197 | +#save and display |
| 198 | +plt.savefig(foldername+"_Results.png") |
| 199 | +plt.show() |
0 commit comments