-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuniqResponses.py
More file actions
222 lines (200 loc) · 8.12 KB
/
uniqResponses.py
File metadata and controls
222 lines (200 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#! /usr/bin/python
'''
find unique responses in fdsn stationxml file
'''
import checkNRL as checkNRL
import sisxmlparser3_0 as sisxmlparser
import datetime
import os
import re
import sys
VERBOSE=False
def setVerbose(b):
VERBOSE = b
def areSameStageType(stageA, stageB):
for t in ['PolesZeros', 'Coefficients', 'FIR', 'Polynomial']:
booleanA = hasattr(stageA, t)
booleanB = hasattr(stageB, t)
if booleanA != booleanB:
return (False, "Not same stage type: %s, %s %s"%(t, booleanA, booleanB))
return True, "ok"
def samePolesZeros(pzA, pzB):
zerosA = getattr(pzA, 'Zero', [])
polesA = getattr(pzA, 'Pole', [])
zerosB = getattr(pzB, 'Zero', [])
polesB = getattr(pzB, 'Pole', [])
result = checkNRL.checkMultiple( [
("PzTransferFunctionType", pzA.PzTransferFunctionType, pzB.PzTransferFunctionType),
("NormalizationFactor", pzA.NormalizationFactor, pzB.NormalizationFactor, 0.001),
("NormalizationFrequency", pzA.NormalizationFrequency, pzB.NormalizationFrequency, 0.001),
("zero len", len(zerosA), len(zerosB)),
("pole len", len(polesA), len(polesB))
])
if not result[0]:
return result
checklist = []
for zi in range(len(zerosA)):
checklist.append(("%d zero real"%(zi,), float(pzA.Zero[zi].Real.ValueOf), float(pzB.Zero[zi].Real.ValueOf), 0.001))
checklist.append(("%d zero imag"%(zi,), float(pzA.Zero[zi].Imaginary.ValueOf), float(pzB.Zero[zi].Imaginary.ValueOf), 0.001))
for pi in range(len(polesA)):
checklist.append(("%d pole real"%(pi,), float(pzA.Pole[pi].Real.ValueOf), float(pzB.Pole[pi].Real.ValueOf), 0.001))
checklist.append(("%d pole imag"%(pi,), float(pzA.Pole[pi].Imaginary.ValueOf), float(pzB.Pole[pi].Imaginary.ValueOf), 0.001))
result = checkNRL.checkMultiple(checklist)
return result
def sameCoefficients(coefA, coefB):
numerA = getattr(coefA, 'Numerator', [])
denomA = getattr(coefA, 'Denominator', [])
numerB = getattr(coefB, 'Numerator', [])
denomB = getattr(coefB, 'Denominator', [])
result = checkNRL.checkMultiple( [
("CfTransferFunctionType", coefA.CfTransferFunctionType, coefB.CfTransferFunctionType),
("numerator len", len(numerA), len(numerB)),
("denominator len", len(denomA), len(denomB))
])
if not result[0]:
return result
checklist = []
for zi in range(len(numerA)):
checklist.append(("%d numerator"%(zi,), float(coefA.Numerator[zi].ValueOf), float(coefB.Numerator[zi].ValueOf), 0.001))
for pi in range(len(denomA)):
checklist.append(("%d denominator"%(pi,), float(coefA.Denominator[pi].ValueOf), float(coefB.Denominator[pi].ValueOf), 0.001))
result = checkNRL.checkMultiple(checklist)
return result
def sameFIR(firA, firB):
result = checkNRL.checkMultiple( [
('Symmetry', firA.Symmetry, firB.Symmetry),
('len NumeratorCoefficient', len(firA.NumeratorCoefficient), len(firB.NumeratorCoefficient))
] )
if not result[0]:
return result
checklist = []
for i in range(len(firA.NumeratorCoefficient)):
checklist.append(("%d NumeratorCoefficient"%(i,), float(firA.NumeratorCoefficient[i].ValueOf), float(firB.NumeratorCoefficient[i].ValueOf), 0.001))
result = checkNRL.checkMultiple(checklist)
return result
def sameDecimation(stageA, stageB):
booleanA = hasattr(stageA, 'Decimation')
booleanB = hasattr(stageB, 'Decimation')
if booleanA != booleanB:
return (False, "Not same stage %s: %s %s"%('Decimation', booleanA, booleanB))
if booleanA:
result = checkNRL.checkMultiple( [
('InputSampleRate', stageA.Decimation.InputSampleRate.ValueOf, stageB.Decimation.InputSampleRate.ValueOf, 0.001),
('Factor', stageA.Decimation.Factor, stageB.Decimation.Factor)
] )
return result
else:
return True, "ok"
def sameGain(stageA, stageB):
booleanA = hasattr(stageA, 'StageGain')
booleanB = hasattr(stageB, 'StageGain')
if booleanA != booleanB:
return (False, "Not same stage %s: %s %s"%('StageGain', booleanA, booleanB))
if booleanA:
result = checkNRL.checkMultiple( [
('Value', stageA.StageGain.Value, stageB.StageGain.Value, 0.001),
('Frequency', stageA.StageGain.Frequency, stageB.StageGain.Frequency, 0.001)
] )
return result
else:
return True, "ok"
def areSameStage(stageA, stageB):
result = areSameStageType(stageA, stageB)
if not result[0]:
return result
if hasattr(stageA, 'PolesZeros'):
result = samePolesZeros(stageA.PolesZeros, stageB.PolesZeros)
elif hasattr(stageA, 'Coefficients'):
result = sameCoefficients(stageA.Coefficients, stageB.Coefficients)
elif hasattr(stageA, 'FIR'):
result = sameFIR(stageA.FIR, stageB.FIR)
elif hasattr(stageA, 'Polynomial'):
result = False, "don't know how to do polynomial yet"
if not result[0]:
return result
result = sameDecimation(stageA, stageB)
if not result[0]:
return result
return sameGain(stageA, stageB)
def areSameStageByIndex(respA, respB, stageIndex):
stageA = getattr(respA, 'Stage', [])
stageB = getattr(respB, 'Stage', [])
result = areSameStage(respA.Stage[stageIndex+1], respB.Stage[stageIndex+1])
if not result[0]:
result = False, "Stage %d %s"%(stageIndex,result[1])
if VERBOSE: print(result[0])
return result
return True, "ok"
def areSameResponse(respA, respB):
stageA = getattr(respA, 'Stage', [])
stageB = getattr(respB, 'Stage', [])
if VERBOSE: print("areSameResponse: ")
result = checkNRL.checkIntEqual("num stages", len(stageA), len(stageB))
if not result[0]:
if VERBOSE: print("not same num stages %s %s %d %d"%(result[0], result[1], len(stageA), len(stageB)))
return result
for i in range(0, len(stageA)):
result = areSameStage(respA.Stage[i], respB.Stage[i])
if not result[0]:
result = False, "Stage %d %s"%(i+1,result[1])
if VERBOSE: print(result[0])
return result
return True, "ok"
def uniqueResponses(staxml):
uniqResponse = []
for n in staxml.Network:
for s in n.Station:
for c in s.Channel:
chanCode = checkNRL.getChanCodeId(n, s, c)
foundMatch = None
if VERBOSE: print("chanCode %s "%(chanCode, ))
# print "chanCode %s numStage = %d"%(chanCode, len(c.Response.Stage),)
for uResp in uniqResponse:
# try:
result = areSameResponse(c.Response, uResp[1])
# except:
# e = sys.exc_info()[0]
# print "Error comparing %s, %s"%(uResp[0], e)
# result = False, " %s"%(e,)
# return
if VERBOSE: print("areSame %s: %s"%(result[0], result[1],))
if result[0]:
foundMatch = uResp
break
if foundMatch is None:
uniqResponse.append( ( chanCode, c.Response, [ chanCode] ) )
if VERBOSE: print("no match %d"%(len(uniqResponse),))
else:
foundMatch[2].append(chanCode)
if VERBOSE: print("found match %s %s"%(chanCode, foundMatch[0]))
return uniqResponse
def usage():
print("python uniqueResponses <staxml>")
def main():
args = sys.argv[1:]
if len(args) == 0:
usage()
return
if not os.path.isfile(args[0]):
print("Can't find file %s"%(args[0],))
return
staxml = sisxmlparser.parse(args[0])
print("Find unique responses")
uniq = uniqueResponses(staxml)
print("NRL check unique responses")
nrledUniq = checkNRL.checkRespListInNRL('nrl', uniq, 'logger_samp_rate.sort')
numChans = 0
print("found %d uniq responses "%(len(uniq), ))
for x in nrledUniq:
for xc in x[2]:
sys.stdout.write("%s "%(xc,))
sys.stdout.write(" NRL: ")
sys.stdout.write(" Sensor: ")
for s in x[3]:
sys.stdout.write("%s "%(s,))
sys.stdout.write(" Logger: ")
for l in x[4]:
sys.stdout.write("%s "%(l,))
print("")
if __name__ == '__main__':
main()