示例#1
0
    def test_trimAllDoesNotChangeDtype(self):
        """
        If a Trace is completely trimmed, e.g. no data samples are remaining,
        the dtype should remain unchanged.

        A trace with no data samples is not really senseful but the dtype
        should not be changed anyways.
        """
        # Choose non native dtype.
        tr = Trace(np.arange(100, dtype='int16'))
        tr.trim(UTCDateTime(10000), UTCDateTime(20000))
        # Assert the result.
        self.assertEqual(len(tr.data), 0)
        self.assertEqual(tr.data.dtype, 'int16')
示例#2
0
    def test_trimAllDoesNotChangeDtype(self):
        """
        If a Trace is completely trimmed, e.g. no data samples are remaining,
        the dtype should remain unchanged.

        A trace with no data samples is not really senseful but the dtype
        should not be changed anyways.
        """
        # Choose non native dtype.
        tr = Trace(np.arange(100, dtype='int16'))
        tr.trim(UTCDateTime(10000), UTCDateTime(20000))
        # Assert the result.
        self.assertEqual(len(tr.data), 0)
        self.assertEqual(tr.data.dtype, 'int16')
示例#3
0
 def test_trim(self):
     """
     Tests the trim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1001))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 5, 0)
     trace.verify()
     # rtrim 100 samples
     trace.trim(0.5, 0.5)
     trace.verify()
     np.testing.assert_array_equal(trace.data[-5:],
                                   np.array([896, 897, 898, 899, 900]))
     np.testing.assert_array_equal(trace.data[:5],
                                   np.array([100, 101, 102, 103, 104]))
     self.assertEquals(len(trace.data), 801)
     self.assertEquals(trace.stats.npts, 801)
     self.assertEquals(trace.stats.sampling_rate, 200.0)
     self.assertEquals(trace.stats.starttime, start + 0.5)
     self.assertEquals(trace.stats.endtime, end - 0.5)
示例#4
0
 def test_trim(self):
     """
     Tests the trim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1001))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 5, 0)
     trace.verify()
     # rtrim 100 samples
     trace.trim(0.5, 0.5)
     trace.verify()
     np.testing.assert_array_equal(trace.data[-5:],
                                   np.array([896, 897, 898, 899, 900]))
     np.testing.assert_array_equal(trace.data[:5],
                                   np.array([100, 101, 102, 103, 104]))
     self.assertEquals(len(trace.data), 801)
     self.assertEquals(trace.stats.npts, 801)
     self.assertEquals(trace.stats.sampling_rate, 200.0)
     self.assertEquals(trace.stats.starttime, start + 0.5)
     self.assertEquals(trace.stats.endtime, end - 0.5)
示例#5
0
import numpy as np
from obspy.core import Trace, UTCDateTime

x = np.zeros(200)
x[100] = 1
tr = Trace(x)
tr.stats.network = "XX"
tr.stats.station = "SDFD1"
print tr
print "showing original trace"
tr.plot()
tr.stats.sampling_rate = 20
tr.stats.starttime = UTCDateTime(2011, 2, 21, 8)
print tr
tr.plot()

tr.filter("lowpass", freq=1)
print "showing filtered trace"
tr.plot()
tr.trim(tr.stats.starttime + 4.5, tr.stats.endtime - 2)
print "showing trimmed trace"
tr.plot()

tr.data = tr.data * 500
tr2 = tr.copy()
tr2.data = tr2.data + np.random.randn(len(tr2))
tr2.stats.station = "SDFD2"
print tr2
print "showing trace with gaussian noise added"
tr2.plot()
示例#6
0
        chan = tt.stats.channel
        netwk = tt.stats.network
        idchan = netwk + "." + sstat + ".." + chan
        idchan_dic = netwk + "." + sstat + "." + chan
        # print("idchan_dic == ", idchan_dic)

        if st_cont.select(id=idchan).__nonzero__():
            st1_cont = st_cont.select(id=idchan)
            st1_cont.merge()
            # print(st1_cont.__str__(extended=True))
            tc = st1_cont[0]
            # print("Trace Id.i= ", st1_cont[0])
            tc.trim(
                starttime=UTCDateTime(detection_otime),
                endtime=UTCDateTime(detection_otime) + 2 * det_dur,
                pad=True,
                nearest_sample=True,
                fill_value=0,
            )
            # print("tc.stats.starttime == ", tc.stats.starttime)
            # print("detection_otime == ", UTCDateTime(detection_otime))

            # compute maximum amplitude in the continuous waveforms
            # for magnitude estimation
            ind_tmin = int((ori) / tc.stats.delta) + 1
            ind_tmax = int((ori + 5.0) / tc.stats.delta) + 1

            if ind_tmin < 0:
                ind_tmin = 0

            # set magg to be used as maximum amplitude level in plotting
def getobs(mseed_filename,
           client,
           event,
           phases,
           frq4,
           windows,
           stas,
           stalocs,
           picks=None,
           delta_T={
               'P': 1.,
               'SH': 1.,
               'R': 10.,
               'L': 10.
           },
           taper=None,
           adjtime=None):
    # Connect to arclink server
    #st = read('../mseed/mini.seed')
    org = event.preferred_origin()
    if org is None:
        org = event.origins[0]
    st = read(mseed_filename)
    stobs = {'params': {'filter': frq4, 'windows': windows}}
    syn = {}
    torg = org.time
    trngmx = 3600.
    invout = None
    # First do any requried time adjustments
    if not adjtime is None:
        for tr in st:
            if not tr.stats.station in adjtime.keys():
                continue
            print 'Adjusting time for station %s by %g secs' % \
                (tr.stats.station,adjtime[tr.stats.station])
            tr.stats.starttime -= adjtime[tr.stats.station]

    for phase in phases:
        if not phase in stas.keys(): continue
        stobs[phase] = Stream()
        for sta in stas[phase]:
            # If this is a body wave phase find the pick - skip if none found
            if phase == 'P' or phase == 'SH':
                sta_pick = None
                # If no picks supplied then get them from events
                if picks is None:
                    for pick in event.picks:
                        if pick.phase_hint == phase[0:1] and \
                            pick.waveform_id.station_code == sta:
                            sta_pick = pick
                            break
                else:  # Get them from picks - e.g. returned by get_isctimes
                    if sta in picks.keys() and phase[0:1] in picks[sta]:
                        sta_pick = Pick()
                        sta_pick.time = picks[sta][phase[0:1]]
                if sta_pick is None:
                    print 'No %s pick found for station %s - skipping' % (
                        phase, sta)
                    continue

            # Set location code if prescribed, otherwise use '00' (preferred)
            if sta in stalocs.keys():
                loc = stalocs[sta]
            else:
                loc = '00'
            # Select the channels for this station - skip if none found
            chans = st.select(station=sta, location=loc)
            if len(chans) == 0:  # if nothing for loc='00', try also with ''
                loc = ''
                chans = st.select(station=sta, location=loc)
            if len(chans) == 0:
                print 'No channels found for %s' % sta
                continue
            try:
                inv = client.get_stations(network=chans[0].stats.network,
                                          station=sta,
                                          location=loc,
                                          starttime=torg,
                                          endtime=torg + 100.,
                                          level='response')
            except Exception as e:
                warnings.warn(str(e))
                print 'FDSNWS request failed for trace id %s - skipping' % sta
                continue
            try:
                coordinates = inv[0].get_coordinates(chans[0].id)
            except:
                print 'No coordinates found for station %s, channel %s' % \
                            (sta,chans[0].id)
                continue
            dist, azm, bazm = gps2dist_azimuth(org['latitude'],
                                               org['longitude'],
                                               coordinates['latitude'],
                                               coordinates['longitude'])
            gcarc = locations2degrees(org['latitude'], org['longitude'],
                                      coordinates['latitude'],
                                      coordinates['longitude'])
            if phase == 'R' or phase == 'P':  # Rayleigh or P wave
                try:
                    tr = st.select(station=sta, component='Z', location=loc)[0]
                except IndexError:
                    print 'No vertical for %s:%s' % (sta, loc)
                    continue
                try:
                    inv = client.get_stations(network=tr.stats.network,
                                              station=sta,
                                              channel=tr.stats.channel,
                                              location=loc,
                                              starttime=torg,
                                              endtime=torg + 100.,
                                              level='response')
                except Exception as e:
                    warnings.warn(str(e))
                    print 'FDSNWS request failed for trace id %s - skipping' % tr.id
                    continue
                tr = tr.copy()
                tr.stats.response = inv[0].get_response(tr.id, torg)
                tr.stats.coordinates = inv[0].get_coordinates(tr.id)
                tr.remove_response(pre_filt=frq4[phase], output='DISP')
                tr.stats.gcarc = gcarc
                tr.stats.azimuth = azm
                #t1 = minv[0].get_responeax(tr.stats.starttime,t+dist/rvmax)
                #t2 = min(tr.stats.endtime  ,t+dist/rvmin)
                t1 = max(torg, tr.stats.starttime)
                t2 = min(torg + trngmx, tr.stats.endtime)
                tr.trim(starttime=t1, endtime=t2)
                decim = int(0.01 + delta_T[phase] / tr.stats.delta)
                ch = inv.select(station=sta, channel=tr.stats.channel)[0][0][0]
                print tr.id,' ',tr.stats.sampling_rate,' decimated by ',decim,\
                        'sensitivity=',ch.response.instrument_sensitivity.value
                if tr.stats.starttime - torg < 0.:
                    tr.trim(starttime=torg)
                tr.decimate(factor=decim, no_filter=True)
                tr.data *= 1.e6  # Convert to microns
            elif phase == 'L' or phase == 'SH':  # Love or SH wave
                if len(chans.select(component='E')) != 0:
                    try:
                        tr1a = st.select(station=sta,
                                         component='E',
                                         location=loc)[0]
                        tr2a = st.select(station=sta,
                                         component='N',
                                         location=loc)[0]
                    except:
                        print 'Station %s does not have 2 horizontal componets -skipping' % sta
                        continue
                elif len(chans.select(component='1')) != 0:
                    try:
                        tr1a = st.select(station=sta,
                                         component='1',
                                         location=loc)[0]
                        tr2a = st.select(station=sta,
                                         component='2',
                                         location=loc)[0]
                    except:
                        print 'Station %s does not have 2 horizontal componets -skipping' % sta
                        continue
                tr1 = tr1a.copy()
                tr1.data = tr1a.data.copy()
                tr2 = tr2a.copy()
                tr2.data = tr2a.data.copy()
                ch1 = inv.select(station=sta,
                                 channel=tr1.stats.channel)[0][0][0]
                ch2 = inv.select(station=sta,
                                 channel=tr2.stats.channel)[0][0][0]
                tr1.stats.response = ch1.response
                tr1.remove_response(pre_filt=frq4[phase], output='DISP')
                tr2.stats.response = ch2.response
                tr2.remove_response(pre_filt=frq4[phase], output='DISP')
                strt = max(tr1.stats.starttime, tr2.stats.starttime)
                endt = min(tr1.stats.endtime, tr2.stats.endtime)
                tr1.trim(starttime=strt, endtime=endt)
                tr2.trim(starttime=strt, endtime=endt)
                # Rotate components first to ZNE
                vert, north, east = rotate2zne(tr1.data, ch1.azimuth, 0.,
                                               tr2.data, ch2.azimuth, 0.,
                                               np.zeros(tr1.stats.npts), 0.,
                                               0.)
                radial, transverse = rotate_ne_rt(north, east, bazm)
                tr = Trace(header=tr1.stats, data=transverse)
                tr2 = Trace(header=tr2.stats, data=radial)
                tr.stats.channel = tr.stats.channel[:-1] + 'T'
                # Change one of the invout channels to end in 'T'
                net = inv[-1]
                stn = net[0]
                chn = stn[0]
                chn.code = chn.code[:-1] + 'T'
                #
                tr.stats.gcarc = gcarc
                tr.stats.azimuth = azm
                decim = int(0.01 + delta_T[phase] / tr.stats.delta)
                print tr.id, ' ', tr.stats.sampling_rate, ' decimated by ', decim
                print '%s: sensitivity=%g, azimuth=%g, dip=%g' % (
                    ch1.code, ch1.response.instrument_sensitivity.value,
                    ch1.azimuth, ch1.dip)
                print '%s: sensitivity=%g, azimuth=%g, dip=%g' % (
                    ch2.code, ch2.response.instrument_sensitivity.value,
                    ch2.azimuth, ch2.dip)
                if tr.stats.starttime - torg < 0.:
                    tr.trim(starttime=torg)
                    tr2.trim(starttime=torg)
                tr.decimate(factor=decim, no_filter=True)
                tr2.decimate(factor=decim, no_filter=True)
                tr.radial = 1.e6 * tr2.data
                tr.stats.coordinates = coordinates
                tr.data *= 1.e6  # Convert to microns
            if phase == 'R' or phase == 'L':  # Window according to group velocity window
                gwin = windows[phase]
                tbeg, tend = (dist * .001 / gwin[1], dist * .001 / gwin[0])
                tr.trim(torg + tbeg, torg + tend)
            elif phase == 'P' or phase == 'SH':  # Window by times before and after pick
                tbef, taft = windows[phase]
                tr.trim(sta_pick.time - tbef, sta_pick.time + taft)
                idx = int(0.5 + tbef / tr.stats.delta)
                avg = tr.data[:idx].mean()
                tr.data -= avg
                if not taper is None:
                    itp = int(taper * tr.stats.npts)
                    for i in range(tr.stats.npts - itp, tr.stats.npts):
                        tr.data[i] *= 0.5 * (1. + mt.cos(
                            mt.pi * (i - (tr.stats.npts - itp)) / float(itp)))
                tr.stats.pick = sta_pick
            stobs[phase].append(tr)
            # Appen station inventory to invout
            if invout is None:
                invout = inv
            else:
                invout += inv
        # Pickle to file
    return stobs, invout