def test_dump_load(self): r = trace.FrequencyResponse() r = trace.PoleZeroResponse([0j, 0j], [1j, 2j, 1 + 3j, 1 - 3j], 1.0) r.regularize() r2 = guts.load_string(r.dump()) assert cnumeq(r.poles, r2.poles, 1e-6) assert cnumeq(r.zeros, r2.zeros, 1e-6) assert numeq(r.constant, r2.constant) r = trace.SampledResponse([0., 1., 5., 10.], [0., 1., 1., 0.]) r.regularize() r2 = guts.load_string(r.dump()) assert numeq(r.frequencies, r2.frequencies, 1e-6) assert cnumeq(r.values, r2.values, 1e-6) r = trace.IntegrationResponse(2, 5.0) r2 = guts.load_string(r.dump()) assert numeq(r.n, r2.n) assert numeq(r.gain, r2.gain, 1e-6) r = trace.DifferentiationResponse(2, 5.0) r2 = guts.load_string(r.dump()) assert numeq(r.n, r2.n) assert numeq(r.gain, r2.gain, 1e-6) r = trace.AnalogFilterResponse(a=[1.0, 2.0, 3.0], b=[2.0, 3.0]) r2 = guts.load_string(r.dump()) assert numeq(r.a, r2.a, 1e-6) assert numeq(r.b, r2.b, 1e-6)
def get_restitution(self, tr, allowed_methods): if 'polezero' in allowed_methods: try: zeros, poles, constant = self._get_polezero(tr) zeros.append(0.0j) # for displacement return trace.PoleZeroResponse( poles, zeros, 1./constant ) except FileNotFound: pass if 'sampled' in allowed_methods: try: freqs, values = self._get_sampledresponse(tr) return trace.SampledResponse( freqs, 1.0/values ) except FileNotFound: pass if 'integration' in allowed_methods: try: cha = self.get_channel(tr) if cha is None: raise eventdata.NoRestitution('No gain information available') if cha.gain == 0.0: raise eventdata.NoRestitution('Gain is zero') return trace.IntegrationResponse(1./cha.gain) except FileNotFound, e: raise eventdata.NoRestitution(e)
def read(self, filename): f = open(filename, 'rb') line = f.readline() line = str(line.decode('ascii')) station, component, century, deltayear, doy, month, day, hr, mi, sec \ = unpack_fixed( 'a5,a4,@1,i2,x1,i3,x1,i2,x1,i2,x1,i2,x1,i2,x1,f6', line[0:35], lambda s: {' ': 1900, '0': 1900, '1': 2000}[s]) # is_accelerometer = line[6] == 'A' latitude, longitude, elevation, filetype, cf_flag = \ unpack_fixed( 'f8?,x1,f9?,x1,f5?,x2,@1,a1', line[50:80], lambda s: { ' ': 'gains-and-filters', 't': 'tabulated', 'p': 'poles-and-zeros'}[s.lower()]) line = f.readline() line = str(line.decode('ascii')) comment = line.strip() tmin = calendar.timegm( (century + deltayear, 1, doy, hr, mi, int(sec))) + sec - int(sec) if filetype == 'gains-and-filters': line = f.readline() line = str(line.decode('ascii')) period, damping, sensor_sensitivity, amplifier_gain, \ digitizer_gain, gain_1hz, filter1_corner, filter1_order, \ filter2_corner, filter2_order = unpack_fixed( 'f8,f8,f8,f8,f8,f8,f8,f8,f8,f8', line) filter_defs = [ filter1_corner, filter1_order, filter2_corner, filter2_order ] line = f.readline() line = str(line.decode('ascii')) filter_defs.extend( unpack_fixed('f8,f8,f8,f8,f8,f8,f8,f8,f8,f8', line)) filters = [] for order, corner in zip(filter_defs[1::2], filter_defs[0::2]): if order != 0.0: filters.append((order, corner)) if filetype in ('gains-and-filters', 'tabulated'): data = ([], [], []) for iy in range(3): for ix in range(3): line = f.readline() line = str(line.decode('ascii')) data[ix].extend( unpack_fixed('f8,f8,f8,f8,f8,f8,f8,f8,f8,f8', line)) response_table = num.array(data, dtype=num.float) if filetype == 'poles-and-zeros': assert False, 'poles-and-zeros file type not implemented yet ' \ 'for seisan response file format' f.close() if num.all(num.abs(response_table[2]) <= num.pi): logger.warning( 'assuming tabulated phases are given in radians instead of ' 'degrees') cresp = response_table[1] * (num.cos(response_table[2]) + 1.0j * num.sin(response_table[2])) else: cresp = response_table[1] * ( num.cos(response_table[2] * d2r) + 1.0j * num.sin(response_table[2] * d2r)) self.station = station self.component = component self.tmin = tmin self.latitude = latitude self.longitude = longitude self.elevation = elevation self.filetype = filetype self.comment = comment self.period = period self.damping = damping self.sensor_sensitivity = sensor_sensitivity self.amplifier_gain = amplifier_gain self.digitizer_gain = digitizer_gain self.gain_1hz = gain_1hz self.filters = filters self.sampled_response = trace.SampledResponse(response_table[0], cresp) self._check_tabulated_response(filename=filename)