def testProjection(self): s2 = math.sqrt(2.) ndata = num.array([s2,s2], dtype=num.float) edata = num.array([s2,0.], dtype=num.float) ddata = num.array([1.,-1.], dtype=num.float) dt = 1.0 n = trace.Trace(deltat=dt, ydata=ndata, tmin=100, channel='N') e = trace.Trace(deltat=dt, ydata=edata, tmin=100, channel='E') d = trace.Trace(deltat=dt, ydata=ddata, tmin=100, channel='D') azi = 45. cazi = math.cos(azi*d2r) sazi = math.sin(azi*d2r) rot45 = num.array([[cazi, sazi, 0],[-sazi,cazi, 0], [0,0,-1]], dtype=num.float) C = lambda x: model.Channel(x) rotated = trace.project([n,e,d], rot45, [C('N'),C('E'),C('D')], [C('R'),C('T'),C('U')]) for tr in rotated: if tr.channel == 'R': r = tr if tr.channel == 'T': t = tr if tr.channel == 'U': u = tr assert( num.all(r.get_ydata() - num.array([ 2., 1. ]) < 1.0e-6 ) ) assert( num.all(t.get_ydata() - num.array([ 0., -1 ]) < 1.0e-6 ) ) assert( num.all(u.get_ydata() - num.array([ -1., 1. ]) < 1.0e-6 ) ) deps = trace.project_dependencies(rot45, [C('N'),C('E'),C('D')], [C('R'),C('T'),C('U')]) assert( set(['N','E']) == set(deps['R']) and set(['N', 'E']) == set(deps['T']) and set(['D']) == set(deps['U']) ) # should work though no horizontals given projected = trace.project([d], rot45, [C('N'),C('E'),C('D')], [C('R'),C('T'),C('U')]) if tr.channel == 'U': u = tr assert( num.all(u.get_ydata() - num.array([ -1., 1. ]) < 1.0e-6 ) )
def _get_waveform(self, obj, quantity='displacement', tmin=None, tmax=None, tpad=0., tfade=0., freqlimits=None, deltat=None, cache=None, backazimuth=None, source=None, target=None, debug=False): assert not debug or (debug and cache is None) if cache is True: cache = self._cache _, _, _, channel = self.get_nslc(obj) station = self.get_station(self.get_nsl(obj)) nslc = station.nsl() + (channel, ) if self.is_blacklisted(nslc): raise NotFound('Waveform is blacklisted:', nslc) if not self.is_whitelisted(nslc): raise NotFound('Waveform is not on whitelist:', nslc) assert tmin is not None assert tmax is not None tmin = float(tmin) tmax = float(tmax) nslc = tuple(nslc) cache_k = nslc + (tmin, tmax, tuple(freqlimits), tfade, deltat, tpad, quantity) if cache is not None and (nslc + cache_k) in cache: obj = cache[nslc + cache_k] if isinstance(obj, Exception): raise obj elif obj is None: raise NotFound('Waveform not found!', nslc) else: return obj syn_test = self.synthetic_test toffset_noise_extract = 0.0 if syn_test: if not syn_test.respect_data_availability: if syn_test.real_noise_scale != 0.0: raise DatasetError( 'respect_data_availability=False and ' 'addition of real noise cannot be combined.') tr = syn_test.get_waveform(nslc, tmin, tmax, tfade=tfade, freqlimits=freqlimits) if cache is not None: cache[tr.nslc_id + cache_k] = tr if debug: return [], [], [] else: return tr if syn_test.real_noise_scale != 0.0: toffset_noise_extract = syn_test.real_noise_toffset abs_delays = [] for ocha in 'ENZRT': sc = self.station_corrections.get(station.nsl() + (channel, ), None) if sc: abs_delays.append(abs(sc.delay)) if abs_delays: abs_delay_max = max(abs_delays) else: abs_delay_max = 0.0 projections = self._get_projections(station, backazimuth, source, target, tmin, tmax) try: trs_projected = [] trs_restituted = [] trs_raw = [] exceptions = [] for matrix, in_channels, out_channels in projections: deps = trace.project_dependencies(matrix, in_channels, out_channels) try: trs_restituted_group = [] trs_raw_group = [] if channel in deps: for cha in deps[channel]: trs_restituted_this, trs_raw_this = \ self.get_waveform_restituted( station.nsl() + (cha,), quantity=quantity, tmin=tmin, tmax=tmax, tpad=tpad+abs_delay_max, toffset_noise_extract=toffset_noise_extract, # noqa tfade=tfade, freqlimits=freqlimits, deltat=deltat, want_incomplete=debug, extend_incomplete=self.extend_incomplete) trs_restituted_group.extend(trs_restituted_this) trs_raw_group.extend(trs_raw_this) trs_projected.extend( trace.project(trs_restituted_group, matrix, in_channels, out_channels)) trs_restituted.extend(trs_restituted_group) trs_raw.extend(trs_raw_group) except NotFound as e: exceptions.append((in_channels, out_channels, e)) if not trs_projected: err = [] for (in_channels, out_channels, e) in exceptions: sin = ', '.join(c.name for c in in_channels) sout = ', '.join(c.name for c in out_channels) err.append('(%s) -> (%s): %s' % (sin, sout, e)) raise NotFound('\n'.join(err)) for tr in trs_projected: sc = self.station_corrections.get(tr.nslc_id, None) if sc: if self.apply_correction_factors: tr.ydata /= sc.factor if self.apply_correction_delays: tr.shift(-sc.delay) if tmin is not None and tmax is not None: tr.chop(tmin, tmax) if syn_test: trs_projected_synthetic = [] for tr in trs_projected: if tr.channel == channel: tr_syn = syn_test.get_waveform(tr.nslc_id, tmin, tmax, tfade=tfade, freqlimits=freqlimits) if tr_syn: if syn_test.real_noise_scale != 0.0: tr_syn = tr_syn.copy() tr_noise = tr.copy() tr_noise.set_ydata(tr_noise.get_ydata() * syn_test.real_noise_scale) tr_syn.add(tr_noise) trs_projected_synthetic.append(tr_syn) trs_projected = trs_projected_synthetic if cache is not None: for tr in trs_projected: cache[tr.nslc_id + cache_k] = tr tr_return = None for tr in trs_projected: if tr.channel == channel: tr_return = tr if debug: return trs_projected, trs_restituted, trs_raw, tr_return elif tr_return: return tr_return else: raise NotFound('waveform not available', station.nsl() + (channel, )) except NotFound: if cache is not None: cache[nslc + cache_k] = None raise
def get_waveform( self, obj, quantity='displacement', tmin=None, tmax=None, tpad=0., tfade=0., freqlimits=None, deltat=None, cache=None, backazimuth=None, source=None, target=None, debug=False): assert not debug or (debug and cache is None) if cache is True: cache = self._cache _, _, _, channel = self.get_nslc(obj) station = self.get_station(self.get_nsl(obj)) nslc = station.nsl() + (channel,) if self.is_blacklisted(nslc): raise NotFound( 'waveform is blacklisted', nslc) if not self.is_whitelisted(nslc): raise NotFound( 'waveform is not on whitelist', nslc) if tmin is not None: tmin = float(tmin) if tmax is not None: tmax = float(tmax) if cache is not None and (nslc, tmin, tmax) in cache: obj = cache[nslc, tmin, tmax] if isinstance(obj, Exception): raise obj else: return obj abs_delays = [] for ocha in 'ENZRT': sc = self.station_corrections.get(station.nsl() + (channel,), None) if sc: abs_delays.append(abs(sc.delay)) if abs_delays: abs_delay_max = max(abs_delays) else: abs_delay_max = 0.0 projections = self._get_projections( station, backazimuth, source, target, tmin, tmax) try: trs_projected = [] trs_restituted = [] trs_raw = [] for matrix, in_channels, out_channels in projections: deps = trace.project_dependencies( matrix, in_channels, out_channels) trs_restituted_group = [] trs_raw_group = [] if channel in deps: for cha in deps[channel]: trs_restituted_this, trs_raw_this = \ self.get_waveform_restituted( station.nsl() + (cha,), quantity=quantity, tmin=tmin, tmax=tmax, tpad=tpad+abs_delay_max, toffset_noise_extract=0.0, tfade=tfade, freqlimits=freqlimits, deltat=deltat, want_incomplete=debug, extend_incomplete=self.extend_incomplete) trs_restituted_group.extend(trs_restituted_this) trs_raw_group.extend(trs_raw_this) trs_projected.extend( trace.project( trs_restituted_group, matrix, in_channels, out_channels)) trs_restituted.extend(trs_restituted_group) trs_raw.extend(trs_raw_group) for tr in trs_projected: sc = self.station_corrections.get(tr.nslc_id, None) if sc: if self.apply_correction_factors: tr.ydata /= sc.factor if self.apply_correction_delays: tr.shift(-sc.delay) if tmin is not None and tmax is not None: tr.chop(tmin, tmax) if cache is not None: for tr in trs_projected: cache[tr.nslc_id, tmin, tmax] = tr if debug: return trs_projected, trs_restituted, trs_raw for tr in trs_projected: if tr.channel == channel: return tr raise NotFound( 'waveform not available', station.nsl() + (channel,)) except NotFound as e: if cache is not None: cache[nslc, tmin, tmax] = e raise