|
27 | 27 | import pandas as pd |
28 | 28 |
|
29 | 29 |
|
30 | | - |
31 | 30 | # TODO: If ts_ metrics are used, must propagate through into the thresholds file |
32 | 31 |
|
33 | 32 | # ============================# |
34 | 33 | # LOAD INPUT ARGUMENTS |
35 | | -network = ''; station = ''; location = ''; channels = ''; start = '';end = ''; outfile = '' |
| 34 | +network = "" |
| 35 | +station = "" |
| 36 | +location = "" |
| 37 | +channels = "" |
| 38 | +start = "" |
| 39 | +end = "" |
| 40 | +outfile = "" |
36 | 41 |
|
37 | 42 | args = reportUtils.getArgs() |
38 | | -start= args.start |
| 43 | +start = args.start |
39 | 44 | end = args.end |
40 | 45 | # month = args.month |
41 | 46 |
|
42 | 47 | preferenceFile = args.preference_file |
43 | 48 |
|
44 | 49 | if not preferenceFile: |
45 | 50 | # If no preference file included, run everything |
46 | | - thresholdGroups = ['Completeness','Amplitudes','Timing','State of Health','Metadata'] |
47 | | - groupsDict = {'Completeness':['avgGaps','gapsRatioGt12','noData'], |
48 | | - 'Amplitudes' : ['flat','lowRms','hiAmp','lowAmp','badResp', |
49 | | - 'avgSpikes','pegged','dead','noise1','noise2', |
50 | | - 'medianUnique','rmsRatio','xTalk', |
51 | | - 'gainRatio','nonCoher','polarity', |
52 | | - 'dcOffsets','nSpikes','rmsRatio'], |
53 | | - 'Timing' : ['poorTQual','suspectTime','noTime'], |
54 | | - 'State of Health' : ['ampSat','filtChg','clip', |
55 | | - 'spikes','glitch','padding','tSync'], |
56 | | - 'Metadata' : ['zDip','horDip','zeroZ','lowScale','nonMSUnits']} |
57 | | - |
| 51 | + thresholdGroups = [ |
| 52 | + "Completeness", |
| 53 | + "Amplitudes", |
| 54 | + "Timing", |
| 55 | + "State of Health", |
| 56 | + "Metadata", |
| 57 | + ] |
| 58 | + groupsDict = { |
| 59 | + "Completeness": ["avgGaps", "gapsRatioGt12", "noData"], |
| 60 | + "Amplitudes": [ |
| 61 | + "flat", |
| 62 | + "lowRms", |
| 63 | + "hiAmp", |
| 64 | + "lowAmp", |
| 65 | + "badResp", |
| 66 | + "avgSpikes", |
| 67 | + "pegged", |
| 68 | + "dead", |
| 69 | + "noise1", |
| 70 | + "noise2", |
| 71 | + "medianUnique", |
| 72 | + "rmsRatio", |
| 73 | + "xTalk", |
| 74 | + "gainRatio", |
| 75 | + "nonCoher", |
| 76 | + "polarity", |
| 77 | + "dcOffsets", |
| 78 | + "nSpikes", |
| 79 | + "rmsRatio", |
| 80 | + ], |
| 81 | + "Timing": ["poorTQual", "suspectTime", "noTime"], |
| 82 | + "State of Health": [ |
| 83 | + "ampSat", |
| 84 | + "filtChg", |
| 85 | + "clip", |
| 86 | + "spikes", |
| 87 | + "glitch", |
| 88 | + "padding", |
| 89 | + "tSync", |
| 90 | + ], |
| 91 | + "Metadata": ["zDip", "horDip", "zeroZ", "lowScale", "nonMSUnits"], |
| 92 | + } |
| 93 | + |
58 | 94 | else: |
59 | 95 | try: |
60 | 96 | with open(preferenceFile) as f: |
61 | 97 | exec(compile(f.read(), preferenceFile, "exec")) |
62 | 98 | except OSError: |
63 | | - print('Cannot open', preferenceFile) |
| 99 | + print("Cannot open", preferenceFile) |
64 | 100 | quit() |
65 | | - |
| 101 | + |
66 | 102 | # Commandline arguments override preference file values, if provided |
67 | 103 | if args.network: |
68 | 104 | network = args.network |
|
71 | 107 | if args.locations: |
72 | 108 | location = args.locations |
73 | 109 | if args.channels: |
74 | | - channels= args.channels |
| 110 | + channels = args.channels |
75 | 111 | if args.outfile: |
76 | 112 | outfile = args.outfile |
77 | 113 | if args.metricsource: |
|
88 | 124 | print("WARNING: No threshold file provided. Exiting.") |
89 | 125 | quit() |
90 | 126 |
|
| 127 | +# Allow for backward compatibility in preference file |
| 128 | +if metricSource == "IRIS": |
| 129 | + metricSource = "EarthScope" |
| 130 | +if metadataSource == "IRIS": |
| 131 | + metadataSource = "EarthScope" |
91 | 132 |
|
92 | 133 | directory = os.path.dirname(outfile) |
93 | 134 |
|
|
102 | 143 |
|
103 | 144 | if os.path.isfile(outfile): |
104 | 145 | resp1 = input("This file already exists - overwrite?[y/n]: ") |
105 | | - if (resp1.upper() == 'Y') or (resp1.upper() == 'YES'): |
106 | | - print('Removing existing file') |
| 146 | + if (resp1.upper() == "Y") or (resp1.upper() == "YES"): |
| 147 | + print("Removing existing file") |
107 | 148 | os.remove(outfile) |
108 | | - |
109 | | - elif (resp1.upper() == 'N') or (resp1.upper()== 'NO'): |
110 | | - resp2= input('Should I append to the existing file?[y/n]: ') |
111 | | - if (not resp2.upper() == 'Y') and (not resp2.upper() == 'YES'): |
| 149 | + |
| 150 | + elif (resp1.upper() == "N") or (resp1.upper() == "NO"): |
| 151 | + resp2 = input("Should I append to the existing file?[y/n]: ") |
| 152 | + if (not resp2.upper() == "Y") and (not resp2.upper() == "YES"): |
112 | 153 | quit("Exiting") |
113 | 154 | else: |
114 | | - print('Input not recognized, cancelling') |
| 155 | + print("Input not recognized, cancelling") |
115 | 156 | quit() |
116 | | - |
| 157 | + |
117 | 158 | # Load up list of metrics and metadata, for reference later on |
118 | 159 | if os.path.isfile(metrics_file): |
119 | | - with open(metrics_file,'r') as f: |
| 160 | + with open(metrics_file, "r") as f: |
120 | 161 | metricsList = f.read().splitlines() |
121 | 162 | else: |
122 | 163 | # This should not happen unless running outside of QuARG since QuARG.py has a check before running findIssues.py |
123 | | - print("WARNING: Could not find list of MUSTANG metrics in file %s - does it exist?" % metrics_file) |
124 | | - print(" You can create this list by entering the Thresholds Editor - it will automatically generate there") |
| 164 | + print( |
| 165 | + "WARNING: Could not find list of MUSTANG metrics in file %s - does it exist?" |
| 166 | + % metrics_file |
| 167 | + ) |
| 168 | + print( |
| 169 | + " You can create this list by entering the Thresholds Editor - it will automatically generate there" |
| 170 | + ) |
125 | 171 | quit() |
126 | | - |
| 172 | + |
127 | 173 | if os.path.isfile(metadata_file): |
128 | | - with open(metadata_file,'r') as f: |
| 174 | + with open(metadata_file, "r") as f: |
129 | 175 | metadataList = f.read().splitlines() |
130 | 176 | else: |
131 | 177 | # This should not happen unless running outside of QuARG since QuARG.py has a check before running findIssues.py |
132 | | - print("WARNING: Could not find list of IRIS metadata fields in file %s - does it exist?" % metadata_file) |
133 | | - print(" You can create this list by entering the Thresholds Editor - it will automatically generate there") |
| 178 | + print( |
| 179 | + "WARNING: Could not find list of EarthScope metadata fields in file %s - does it exist?" |
| 180 | + % metadata_file |
| 181 | + ) |
| 182 | + print( |
| 183 | + " You can create this list by entering the Thresholds Editor - it will automatically generate there" |
| 184 | + ) |
134 | 185 | quit() |
135 | | - |
| 186 | + |
136 | 187 |
|
137 | 188 | # ============================# |
138 | 189 | # GO THROUGH THRESHOLDS |
139 | 190 |
|
140 | 191 | # Add the header to the file |
141 | | -with open(outfile, 'w') as f: |
| 192 | +with open(outfile, "w") as f: |
142 | 193 | f.write("# Threshold|Target|Start|End|Ndays|Status|Value|Notes\n") |
143 | | -f.close() |
| 194 | +f.close() |
144 | 195 |
|
145 | 196 | # Get metadata dataframe at the beginning to use wherever necessary, since it is always the same |
146 | | -metadataDF = reportUtils.getMetadata(network, station, location, channels, start, end, metadataSource) |
| 197 | +metadataDF = reportUtils.getMetadata( |
| 198 | + network, station, location, channels, start, end, metadataSource |
| 199 | +) |
147 | 200 |
|
148 | 201 |
|
149 | 202 | failedMetricsAll = list() |
150 | 203 | failedThresholdsAll = list() |
151 | | -# thresholdFile = './groupsTEST.txt' |
152 | 204 | for thresholdGroup in thresholdGroups: |
153 | 205 | print() |
154 | 206 | print("Running %s Thresholds" % thresholdGroup) |
|
157 | 209 | except: |
158 | 210 | print(" Could not find any thresholds for %s" % thresholdGroup) |
159 | 211 | continue |
160 | | - |
| 212 | + |
161 | 213 | thresholdsList.sort() |
162 | | - |
163 | | - allMetrics, failedThresholds = thresholds.get_threshold_metrics(thresholdsList, thresholdFile) |
| 214 | + |
| 215 | + allMetrics, failedThresholds = thresholds.get_threshold_metrics( |
| 216 | + thresholdsList, thresholdFile |
| 217 | + ) |
164 | 218 | metadatas = [e for e in metadataList if e in allMetrics] |
165 | 219 | metrics = [e for e in metricsList if e in allMetrics] |
166 | 220 |
|
167 | | -# hasMetadata = False; |
168 | 221 | hasMetrics = False |
169 | | -# if len(metadatas) > 0: |
170 | | -# print("This thresholds Group contains some metadata fields") |
171 | | -# hasMetadata = True |
172 | 222 | if len(metrics) > 0: |
173 | 223 | hasMetrics = True |
174 | 224 |
|
175 | | - |
176 | 225 | if hasMetrics: |
177 | | - metricDF, failedMetrics = reportUtils.mergeMetricDF(network, station, location, channels, start, end, metrics, metricSource) |
| 226 | + metricDF, failedMetrics = reportUtils.mergeMetricDF( |
| 227 | + network, station, location, channels, start, end, metrics, metricSource |
| 228 | + ) |
178 | 229 | else: |
179 | | - metricDF = pd.DataFrame(columns=['value','target','start','end','network','station','location','channel']) |
| 230 | + metricDF = pd.DataFrame( |
| 231 | + columns=[ |
| 232 | + "value", |
| 233 | + "target", |
| 234 | + "start", |
| 235 | + "end", |
| 236 | + "network", |
| 237 | + "station", |
| 238 | + "location", |
| 239 | + "channel", |
| 240 | + ] |
| 241 | + ) |
180 | 242 | failedMetrics = list() |
181 | | - |
| 243 | + |
182 | 244 | for failedThreshold in failedThresholds: |
183 | 245 | if not failedThreshold in failedThresholdsAll: |
184 | 246 | failedThresholdsAll.append(failedThreshold) |
185 | | - |
| 247 | + |
186 | 248 | for failedMetric in failedMetrics: |
187 | 249 | if not failedMetric in failedMetricsAll: |
188 | 250 | failedMetricsAll.append(failedMetric) |
189 | | - |
190 | | -# if hasMetrics == True and not metricDF.empty: |
| 251 | + |
191 | 252 | for threshold in thresholdsList: |
192 | 253 | if not threshold in failedThresholds: |
193 | | - thresholds.do_threshold(threshold, thresholdFile, metricDF, metadataDF, outfile, instruments, start, end, hasMetrics, chanTypes) |
| 254 | + thresholds.do_threshold( |
| 255 | + threshold, |
| 256 | + thresholdFile, |
| 257 | + metricDF, |
| 258 | + metadataDF, |
| 259 | + outfile, |
| 260 | + instruments, |
| 261 | + start, |
| 262 | + end, |
| 263 | + hasMetrics, |
| 264 | + chanTypes, |
| 265 | + ) |
194 | 266 |
|
195 | | -with open('failedMetrics.txt','w') as f: |
| 267 | +with open("failedMetrics.txt", "w") as f: |
196 | 268 | for failedThreshold in failedThresholdsAll: |
197 | | - f.write('threshold: %s\n' % failedThreshold) |
| 269 | + f.write("threshold: %s\n" % failedThreshold) |
198 | 270 | for failedMetric in failedMetricsAll: |
199 | | - f.write('metric: %s\n' % failedMetric) |
| 271 | + f.write("metric: %s\n" % failedMetric) |
200 | 272 |
|
201 | 273 | print("INFO: Completed generating issue file") |
202 | | - |
|
0 commit comments