Example #1
0
def merger(noise_trace, st_events_poisson, samp_rate, delta):
    """ Merges the noise with the events """
    # Creates the stream with the noise and events
    newnoise_t_formerging = noise_trace.slice(0, (st_events_poisson[-1].stats.endtime)+100)
    seis = Stream()
    seis += newnoise_t_formerging
    seis += st_events_poisson
    figs = plt.figure(figsize=(14,5))
    seis.plot(fig = figs)
    figs.suptitle("Noise and events", fontsize = 12)
    ylab=figs.text(0.05, 0.5, 'Amplitude of signal', va='center', 
                rotation='vertical', fontsize=12)
    figs.text(0.5, 0, 'Timestamp', ha='center', fontsize=12)
    seis.merge()
    seis += st_events_poisson
    seis.merge(method = 1, interpolation_samples=-1)
    figs = plt.figure(figsize=(14,5))
    seis.plot(fig = figs)
    plt.title("Synthetic seismogram", fontsize = 12)
    ylab=figs.text(0.05, 0.5, 'Amplitude of signal', va='center', 
                rotation='vertical', fontsize=12)
    figs.text(0.5, 0, 'Timestamp', ha='center', fontsize=12)

    syn_seis = seis[0]
    syn_seis.stats.sampling_rate = samp_rate
    syn_seis.stats.delta = delta
    
    return syn_seis
Example #2
0
def _read_filepattern(fpattern, starttime, endtime, trim):
    """Read a stream from files whose names match a given pattern.
    """
    flist = glob.glob(fpattern)
    starttimes = []
    endtimes = []
    # first only read the header information
    for fname in flist:
        st = read(fname,headonly=True)
        starttimes.append(st[0].stats.starttime.datetime)
        endtimes.append(st[-1].stats.endtime.datetime)
    # now read the stream from the files that contain the period
    st = Stream()
    for ind,fname in enumerate(flist):
        if (starttimes[ind] < endtime) and (endtimes[ind] > starttime):
            if trim:
                st += read(fname,starttime=UTCDateTime(starttime),endtime=UTCDateTime(endtime))
            else:
                st += read(fname)
    try:
        st.merge()
    except:
        print "Error merging traces for requested period!"
        st = Stream()
    return st
Example #3
0
def _do_select_internal(dev_id: str,
                        start_time: datetime.datetime,
                        end_time: datetime.datetime,
                        do_merge: bool = True) -> Stream:
    """
    Retrieve stream from database

    Constraint:

    """
    # start_time, end_time = start_time.utcnow(), end_time.utcnow()
    st = start_time.strftime('%Y-%m-%dT%H:%M:%S')
    et = end_time.strftime('%Y-%m-%dT%H:%M:%S')
    print(st, et)
    # Build query
    qry = (f"SELECT time_record_start, contents from skt_sensor "
           f"WHERE sensor_id='{dev_id}' AND "
           f"time_bucket='{start_time.date()}' AND "
           f"time_record_start >= '{st}' AND "
           f"time_record_start <= '{et}'")

    result_set = session.execute(qry)

    # Create empty stream
    s = Stream()

    # For each stream in result_set
    for r in result_set:
        target = zlib.decompress(r.contents)

        bio = BytesIO(target)

        s += obspy.read(bio)

    # No result in result_set
    if len(s) == 0:
        return None

    # Fix network code
    for t in s:
        t.stats.network = 'SK'

    if do_merge:
        s.merge(fill_value='latest')

    return s
Example #4
0
def _read_filepattern(fpattern, starttime, endtime, trim):
    """Read a stream from files whose names match a given pattern.
    """
    flist = glob.glob(fpattern)
    starttimes = []
    endtimes = []
    # first only read the header information
    for fname in flist:
        st = read(fname,headonly=True)
        starttimes.append(st[0].stats.starttime.datetime)
        endtimes.append(st[-1].stats.endtime.datetime)
    # now read the stream from the files that contain the period
    st = Stream()
    for ind,fname in enumerate(flist):
        if (starttimes[ind] < endtime) and (endtimes[ind] > starttime):
            if trim:
                st += read(fname,starttime=UTCDateTime(starttime),endtime=UTCDateTime(endtime))
            else:
                st += read(fname)
    st.merge()
    return st
Example #5
0
def extractDataFromArchive(t1,
                           t2,
                           fileNames,
                           fileTimes,
                           wantedStaChas=[['*', '*']]):
    # Return nothing if there is no data
    if len(fileTimes) == 0:
        return EmptyStream()
    # Catch the case where the asked time range is completely outside the archive data availability
    if t1 > fileTimes[-1, 1] or t2 < fileTimes[0, 0]:
        return EmptyStream()
    # Figure out what set of files are wanted
    collectArgs = np.where((fileTimes[:, 0] <= t2)
                           & (fileTimes[:, 1] >= t1))[0]
    stream = EmptyStream()
    flagged = False
    # Read in all of the information
    for aFile in fileNames[collectArgs]:
        # Flag to user if the archive structure has changed
        if not os.path.exists(aFile):
            flagged = True
            continue
        aStream = read(aFile)
        for aSta, aCha in wantedStaChas:
            stream += aStream.select(station=aSta, channel=aCha)
    if flagged:
        print('Archive structure as changed, reload the current archive')
    # Merge traces which are adjacent
    try:
        stream.merge(method=1)
    except:
        stream = RemoveOddRateTraces(stream)
        stream.merge(method=1)
    # If any trace has masked values, split
    if True in [isinstance(tr.data, np.ma.masked_array) for tr in stream]:
        stream = stream.split()
    # Trim to wanted times
    stream.trim(UTCDateTime(t1), UTCDateTime(t2))
    return stream
Example #6
0
def getData(tstart, tend, opt):

    """
    Download data from files in a folder, from IRIS, or a Earthworm waveserver
    
    A note on SAC/miniSEED files: as this makes no assumptions about the naming scheme of
    your data files, please ensure that your headers contain the correct SCNL information!

    tstart: UTCDateTime of beginning of period of interest
    tend: UTCDateTime of end of period of interest
    opt: Options object describing station/run parameters
    
    Returns ObsPy stream objects, one for cutting and the other for triggering
    """    
    
    nets = opt.network.split(',')
    stas = opt.station.split(',')
    locs = opt.location.split(',')
    chas = opt.channel.split(',')
    
    st = Stream()
    
    if opt.server == 'file':
    
        # Generate list of files
        if opt.server == 'file':
            flist = list(itertools.chain.from_iterable(glob.iglob(os.path.join(
                root,opt.filepattern)) for root, dirs, files in os.walk(opt.searchdir)))
                
        # Determine which subset of files to load based on start and end times and
        # station name; we'll fully deal with stations below
        flist_sub = []
        for f in flist:
            # Load header only
            stmp = obspy.read(f, headonly=True)
            # Check if station is contained in the stas list
            if stmp[0].stats.station in stas:
                # Check if contains either start or end time
                ststart = stmp[0].stats.starttime
                stend = stmp[-1].stats.endtime
                if (ststart<=tstart and tstart<=stend) or (ststart<=tend and
                    tend<=stend) or (tstart<=stend and ststart<=tend):
                    flist_sub.append(f)
        
        # Fully load data from file
        stmp = Stream()
        for f in flist_sub:
            tmp = obspy.read(f, starttime=tstart, endtime=tend+opt.maxdt)
            if len(tmp) > 0:
                stmp = stmp.extend(tmp)
    
        # Filter and merge
        stmp = stmp.filter('bandpass', freqmin=opt.fmin, freqmax=opt.fmax, corners=2,
            zerophase=True)
        stmp = stmp.taper(0.05,type='hann',max_length=opt.mintrig)
        for m in range(len(stmp)):
            if stmp[m].stats.sampling_rate != opt.samprate:
                stmp[m] = stmp[m].resample(opt.samprate)
        stmp = stmp.merge(method=1, fill_value=0)
        
        # Only grab stations/channels that we want and in order
        netlist = []
        stalist = []
        chalist = []
        loclist = []
        for s in stmp:
            stalist.append(s.stats.station)
            chalist.append(s.stats.channel)
            netlist.append(s.stats.network)
            loclist.append(s.stats.location)
            
        # Find match of SCNL in header or fill empty
        for n in range(len(stas)):
            for m in range(len(stalist)):
                if (stas[n] in stalist[m] and chas[n] in chalist[m] and nets[n] in
                    netlist[m] and locs[n] in loclist[m]):
                    st = st.append(stmp[m])
            if len(st) == n:
                print("Couldn't find "+stas[n]+'.'+chas[n]+'.'+nets[n]+'.'+locs[n])
                trtmp = Trace()
                trtmp.stats.sampling_rate = opt.samprate
                trtmp.stats.station = stas[n]
                st = st.append(trtmp.copy())
    
    else:   
     
        if '.' not in opt.server:
            client = Client(opt.server)
        else:
            client = EWClient(opt.server, opt.port)
        
        for n in range(len(stas)):
            try:
                stmp = client.get_waveforms(nets[n], stas[n], locs[n], chas[n],
                        tstart, tend+opt.maxdt)
                for m in range(len(stmp)):
                    stmp[m].data = np.where(stmp[m].data == -2**31, 0, stmp[m].data) # replace -2**31 (Winston NaN token) w 0
                stmp = stmp.filter('bandpass', freqmin=opt.fmin, freqmax=opt.fmax,
                    corners=2, zerophase=True)
                stmp = stmp.taper(0.05,type='hann',max_length=opt.mintrig)
                for m in range(len(stmp)):
                    if stmp[m].stats.sampling_rate != opt.samprate:
                        stmp[m] = stmp[m].resample(opt.samprate)
                stmp = stmp.merge(method=1, fill_value=0)
            except (obspy.clients.fdsn.header.FDSNException):
                try: # try again
                    stmp = client.get_waveforms(nets[n], stas[n], locs[n], chas[n],
                            tstart, tend+opt.maxdt)
                    for m in range(len(stmp)):
                        stmp[m].data = np.where(stmp[m].data == -2**31, 0, stmp[m].data) # replace -2**31 (Winston NaN token) w 0
                    stmp = stmp.filter('bandpass', freqmin=opt.fmin, freqmax=opt.fmax,
                        corners=2, zerophase=True)
                    stmp = stmp.taper(0.05,type='hann',max_length=opt.mintrig)
                    for m in range(len(stmp)):
                        if stmp[m].stats.sampling_rate != opt.samprate:
                            stmp[m] = stmp[m].resample(opt.samprate)
                    stmp = stmp.merge(method=1, fill_value=0)
                except (obspy.clients.fdsn.header.FDSNException):
                    print('No data found for {0}.{1}'.format(stas[n],nets[n]))
                    trtmp = Trace()
                    trtmp.stats.sampling_rate = opt.samprate
                    trtmp.stats.station = stas[n]
                    stmp = Stream().extend([trtmp.copy()])
                                            
            # Last check for length; catches problem with empty waveserver
            if len(stmp) != 1:
                print('No data found for {0}.{1}'.format(stas[n],nets[n]))
                trtmp = Trace()
                trtmp.stats.sampling_rate = opt.samprate
                trtmp.stats.station = stas[n]
                stmp = Stream().extend([trtmp.copy()])
                
            st.extend(stmp.copy()) 
    
    # Edit 'start' time if using offset option
    if opt.maxdt:
        dts = np.fromstring(opt.offset, sep=',')
        for n, tr in enumerate(st):
            tr.stats.starttime = tr.stats.starttime-dts[n]
    
    st = st.trim(starttime=tstart, endtime=tend, pad=True, fill_value=0)
    stC = st.copy()
    
    return st, stC
Example #7
0
def grab_file_data(filepath, scnl, tstart, tend, fill_value=0):
    import obspy
    from obspy import Stream, Trace
    import glob, os, itertools

    stas = []
    chas = []
    nets = []
    locs = []
    for sta in scnl:
        stas.append(sta.split('.')[0])
        chas.append(sta.split('.')[1])
        nets.append(sta.split('.')[2])
        if len(sta) == 4:
            locs.append(sta.split('.')[3])
        else:
            locs.append('')

    st = Stream()

    #if opt.server == 'file':
    if True:

        # Generate list of files
        #if opt.server == 'file':
        flist = list(
            itertools.chain.from_iterable(
                glob.iglob(os.path.join(root, "*"))
                for root, dirs, files in os.walk(filepath)))
        # "*" takes the place of wildcard lists, see REDPy documentation

        # Determine which subset of files to load based on start and end times and
        # station name; we'll fully deal with stations below
        flist_sub = []
        for f in flist:
            # Load header only
            stmp = obspy.read(f, headonly=True)
            # Check if station is contained in the stas list
            if stmp[0].stats.station in stas:
                # Check if contains either start or end time
                ststart = stmp[0].stats.starttime
                stend = stmp[0].stats.endtime
                if (ststart <= tstart and tstart <= stend) or (
                        ststart <= tend
                        and tend <= stend) or (tstart <= stend
                                               and ststart <= tend):
                    flist_sub.append(f)

        # Fully load data from file
        stmp = Stream()
        for f in flist_sub:
            tmp = obspy.read(f, starttime=tstart, endtime=tend)
            if len(tmp) > 0:
                stmp = stmp.extend(tmp)

        # Filter and merge
        #stmp = stmp.filter('bandpass', freqmin=opt.fmin, freqmax=opt.fmax, corners=2,
        #    zerophase=True)
        #stmp = stmp.taper(0.05,type='hann',max_length=opt.mintrig)
        #for m in range(len(stmp)):
        #    if stmp[m].stats.sampling_rate != opt.samprate:
        #        stmp[m] = stmp[m].resample(opt.samprate)
        stmp = stmp.merge(method=1, fill_value=fill_value)

        # Only grab stations/channels that we want and in order
        netlist = []
        stalist = []
        chalist = []
        loclist = []
        for s in stmp:
            stalist.append(s.stats.station)
            chalist.append(s.stats.channel)
            netlist.append(s.stats.network)
            loclist.append(s.stats.location)

        # Find match of SCNL in header or fill empty
        for n in range(len(stas)):
            for m in range(len(stalist)):
                if (stas[n] in stalist[m] and chas[n] in chalist[m]
                        and nets[n] in netlist[m] and locs[n] in loclist[m]):
                    st = st.append(stmp[m])
            if len(st) == n:
                print("Couldn't find " + stas[n] + '.' + chas[n] + '.' +
                      nets[n] + '.' + locs[n])
                trtmp = Trace()
                trtmp.stats.station = stas[n]
                st = st.append(trtmp.copy())

        if len(st) > 1:
            if fill_value == 0 or fill_value == None:
                st.detrend('demean')
                st.taper(max_percentage=0.01)
            st.merge(fill_value=fill_value)
        st.trim(tstart, tend, pad=0)
        st.detrend('demean')

    print(st)
    return st
Example #8
0
def get_stream(datasource,
               scnl,
               tstart,
               tend,
               fill_value=0,
               filepattern='*',
               filter=None,
               samprate=100,
               verbose=False):
    """
    Generalized (and more robust) way to retrieve waveform data through ObsPy
    Download data from files in a folder, from IRIS, or a Earthworm waveserver
    
    A note on SAC/miniSEED files: as this makes no assumptions about the naming scheme of
    your data files, please ensure that your headers contain the correct SCNL information!
    tstart: UTCDateTime of beginning of period of interest
    tend: UTCDateTime of end of period of interest
    
    filepattern='*'
     You can specify a pattern for your files to reduce the files within the directory
     searched. For example, filepattern=2019.06.*.mseed if your files are miniSEED files
     named by date and you only want those from June 2019. Simple wildcarding is supported
     (i.e., * and ?, [] for ranges of values or lists) but not full regular expressions.
     
    samprate=100
     Resamples all waveforms to the same sample rate.
    
    Returns ObsPy stream objects
    
    Based on code by Alicia Hotovec-Ellis and Aaron Wech.
    
    Example:
    >>> get_stream(['vdap.org', 16024], ['HSR.EHZ.CC.--'], '2004-09-28T00:00:00', '2004-09-28T01:00:00')
    >>> get_stream(['file', '/Users/vdapseismo/data/'], ['HSR.EHZ.CC.--'], '2004-09-28T00:00:00', '2004-09-28T01:00:00')
    >>> get_stream(['IRIS'], ['HSR.EHZ.CC.--'], '2004-09-28T00:00:00', '2004-09-28T01:00:00')
    """

    from obspy import UTCDateTime
    import obspy
    from obspy.clients.fdsn import Client
    from obspy.clients.earthworm import Client as EWClient
    from obspy.core.trace import Trace
    from obspy.core.stream import Stream
    from obspy.signal.trigger import coincidence_trigger
    import numpy as np
    from scipy import stats
    from scipy.fftpack import fft
    import glob, os, itertools

    #print(datasource)
    #print(scnl)
    #print(tstart)
    #print(tend)

    tstart = UTCDateTime(tstart)
    tend = UTCDateTime(tend)

    nets = []
    stas = []
    locs = []
    chas = []
    for s in scnl:
        #print(s)
        nets.append(s.split('.')[2])
        stas.append(s.split('.')[0])
        locs.append(s.split('.')[3])
        chas.append(s.split('.')[1])

    st = Stream()

    if '/' in datasource:
        # Retrieve data from file structure

        flist = list(
            itertools.chain.from_iterable(
                glob.iglob(os.path.join(root, filepattern))
                for root, dirs, files in os.walk(datasource)))

        # Determine which subset of files to load based on start and end times and
        # station name; we'll fully deal with stations below
        flist_sub = []
        for f in flist:
            # Load header only
            stmp = obspy.read(f, headonly=True)
            # Check if station is contained in the stas list
            if stmp[0].stats.station in stas:
                # Check if contains either start or end time
                ststart = stmp[0].stats.starttime
                stend = stmp[0].stats.endtime
                if (ststart <= tstart and tstart <= stend) or (
                        ststart <= tend
                        and tend <= stend) or (tstart <= stend
                                               and ststart <= tend):
                    flist_sub.append(f)

        # Fully load data from file
        stmp = Stream()
        for f in flist_sub:
            tmp = obspy.read(f, starttime=tstart, endtime=tend)
            if len(tmp) > 0:
                stmp = stmp.extend(tmp)

        # merge
        stmp = stmp.taper(max_percentage=0.01)
        for m in range(len(stmp)):
            if stmp[m].stats.sampling_rate != samprate:
                stmp[m] = stmp[m].resample(samprate)
        stmp = stmp.merge(method=1, fill_value=fill_value)

        # Only grab stations/channels that we want and in order
        netlist = []
        stalist = []
        chalist = []
        loclist = []
        for s in stmp:
            stalist.append(s.stats.station)
            chalist.append(s.stats.channel)
            netlist.append(s.stats.network)
            loclist.append(s.stats.location)

        # Find match of SCNL in header or fill empty
        for n in range(len(stas)):
            for m in range(len(stalist)):
                if (stas[n] in stalist[m] and chas[n] in chalist[m]
                        and nets[n] in netlist[m] and locs[n] in loclist[m]):
                    st = st.append(stmp[m])
            if len(st) == n:
                print('No data found for {}.{}.{}.{}'.format(
                    stas[n], chas[n], nets[n], locs[n]))
                trtmp = Trace()
                trtmp.stats.sampling_rate = samprate
                trtmp.stats.station = stas[n]
                st = st.append(trtmp.copy())

    else:
        # retrieve data from server

        if '.' not in datasource:
            client = Client(datasource)
        else:
            datasource = datasource.split(':')
            client = EWClient(datasource[0], int(datasource[1]))

        for n in range(len(stas)):
            try:
                stmp = client.get_waveforms(nets[n], stas[n], locs[n], chas[n],
                                            tstart, tend)
                for m in range(len(stmp)):
                    #stmp[m].data = np.ma.masked_where(stmp[m].data == -2**31, stmp[m].data) # masks out all values of -2**31 (Winston NaN Token)
                    #stmp[m] = stmp[m].split().merge(method=0, fill_value='interpolate')[0] # splits trace at masked values; then re-merges using linear interpolation
                    stmp[m].data = np.where(stmp[m].data == -2**31, 0,
                                            stmp[m].data)
                    if stmp[m].stats.sampling_rate != samprate:
                        stmp[m] = stmp[m].resample(samprate)
                stmp = stmp.taper(max_percentage=0.01)
                stmp = stmp.merge(method=1, fill_value=fill_value)
            except (obspy.clients.fdsn.header.FDSNException):
                try:  # try again
                    stmp = client.get_waveforms(nets[n], stas[n], locs[n],
                                                chas[n], tstart, tend)
                    for m in range(len(stmp)):
                        #stmp[m].data = np.ma.masked_where(stmp[m].data == -2**31, stmp[m].data) # masks out all values of -2**31 (Winston NaN Token)
                        #stmp[m] = stmp[m].split().merge(method=0, fill_value='interpolate')[0] # splits trace at masked values; then re-merges using linear interpolation
                        stmp[m].data = np.where(stmp[m].data == -2**31, 0,
                                                stmp[m].data)
                        if stmp[m].stats.sampling_rate != samprate:
                            stmp[m] = stmp[m].resample(samprate)
                    stmp = stmp.taper(max_percentage=0.01)
                    stmp = stmp.merge(method=1, fill_value=fill_value)
                except (obspy.clients.fdsn.header.FDSNException):
                    print('No data found for {0}.{1}'.format(stas[n], nets[n]))
                    trtmp = Trace()
                    trtmp.stats.sampling_rate = samprate
                    trtmp.stats.station = stas[n]
                    stmp = Stream().extend([trtmp.copy()])

            # Last check for length; catches problem with empty waveserver
            if len(stmp) != 1:
                print('No data found for {}.{}.{}.{}'.format(
                    stas[n], chas[n], nets[n], locs[n]))
                trtmp = Trace()
                trtmp.stats.sampling_rate = samprate
                trtmp.stats.station = stas[n]
                stmp = Stream().extend([trtmp.copy()])

            st.extend(stmp.copy())

    st = st.trim(starttime=tstart,
                 endtime=tend,
                 pad=True,
                 fill_value=fill_value)

    return st
Example #9
0
# get list of all days and of unique days
days_all = np.array([
    datetime.strptime(a[-8:], '%Y.%j').strftime('%Y.%m.%d') for a in dayfiles
])
days = np.unique(days_all)

for dy in days:
    file_list = dayfiles[np.where(
        days_all == dy)]  # files for this day, all stations

    st = Stream()
    for fl in file_list:
        if fl not in bad_files and os.stat(fl).st_size != 0:
            st += read(fl)
    if len(st) > 0:
        # decimate or resample to 1Hz
        if np.all([tr.stats.sampling_rate == 100 for tr in st]):
            st.decimate(100, no_filter=True)
        else:
            st.resample(1.0, no_filter=True)

        st.merge(method=1, fill_value=0)  # make sure 1 trace per sta/comp

        for tr in st:  # rename channels to L* to match [edited] dataless
            tr.stats.channel = 'L' + tr.stats.channel[1:]

        # write miniseed
        ofile = os.path.join(mseed_dir_out,'EN-%s.%s.%s.mseed' % \
                (dy.split('.')[0],dy.split('.')[1],dy.split('.')[2]))
        st.write(ofile, format='MSEED')
Example #10
0
def ProcessStream(stream):
    n_day = 0
    for trace in stream:
        if trace.stats.channel != channel:
            continue
        net = trace.stats.network
        sta = trace.stats.station
        station_path = sacPath + sta + '-' + net
        if not os.path.exists(station_path):
            os.makedirs(station_path)
        starttime = trace.stats.starttime
        endtime = trace.stats.endtime
        day_gap = datetime.date(
            endtime.year, endtime.month, endtime.day) - datetime.date(
                starttime.year, starttime.month, starttime.day)
        time_day = [starttime]
        for ii in range(day_gap.days):
            splittime = datetime.date(time_day[ii].year, time_day[ii].month,
                                      time_day[ii].day) + datetime.timedelta(1)
            splittime = str(splittime.year) + '-' + str(
                splittime.month) + '-' + str(splittime.day) + 'T00:00:00'
            splittime = UTCDateTime(splittime)
            time_day.append(splittime)
        time_day.append(endtime)

        for ii in range(len(time_day) - 1):
            starttime = time_day[ii]
            endtime = time_day[ii + 1]
            st_day = trace.slice(starttime, endtime, nearest_sample=False)
            if st_day.stats.npts < 10:
                continue
            day_name = sacPath + sta + '-' + net + '/' + sta + '-' + net + '-' + str(
                starttime.year) + '-' + str(
                    starttime.julday).zfill(3) + '-' + channel + '.SAC'
            if os.path.exists(day_name):
                st_tmp = read(day_name, format='SAC')
                if int(st_tmp[0].stats.sampling_rate) != int(resamplingRate):
                    resampling_rate = int(
                        round(st_tmp[0].stats.sampling_rate / resamplingRate))
                    st_tmp[0].decimate(resampling_rate)
                day_name_tmp = day_name + '.tmp'
                st_day.write(day_name_tmp, format='SAC')
                st_day = read(day_name_tmp, format='SAC')
                os.remove(day_name_tmp)
                if int(st_day[0].stats.sampling_rate) != int(resamplingRate):
                    resampling_rate = int(
                        round(st_day[0].stats.sampling_rate / resamplingRate))
                    if resampling_rate == 100:
                        st_day[0].decimate(10, no_filter=True)
                        st_day[0].decimate(10, no_filter=True)
                    else:
                        st_day[0].decimate(resampling_rate, no_filter=True)
                if np.abs(st_day[0].stats.sampling_rate -
                          resamplingRate) > 0.01:
                    st_day[0].resample(resamplingRate)
                else:
                    st_day[0].stats.sampling_rate = resamplingRate
                st_tmp[0].data = st_tmp[0].data.astype(np.float32)
                st_day[0].data = st_day[0].data.astype(np.float32)
                merge_data = Stream()
                merge_data.append(st_tmp[0])
                merge_data.append(st_day[0])
                merge_data.sort(['starttime'])
                merge_data.merge(method=1, fill_value='latest')
                os.remove(day_name)
                merge_data.write(day_name, format='SAC')
            else:
                if int(st_day.stats.sampling_rate) != int(resamplingRate):
                    resampling_rate = int(
                        round(st_day.stats.sampling_rate / resamplingRate))
                    if resampling_rate == 100:
                        st_day.decimate(10, no_filter=True)
                        st_day.decimate(10, no_filter=True)
                    else:
                        st_day.decimate(resampling_rate, no_filter=True)
                if np.abs(st_day.stats.sampling_rate - resamplingRate) > 0.01:
                    st_day.resample(resamplingRate)
                else:
                    st_day.stats.sampling_rate = resamplingRate
                st_day.write(day_name, format='SAC')
                n_day = n_day + 1

            print(sta + ' now julday is: ' + str(starttime.date), n_day)
Example #11
0
def getData(tstart, tend, opt):
    """
    Download data from files in a folder, from IRIS, or a Earthworm waveserver
    
    A note on SAC/miniSEED files: as this makes no assumptions about the naming scheme of
    your data files, please ensure that your headers contain the correct SCNL information!

    tstart: UTCDateTime of beginning of period of interest
    tend: UTCDateTime of end of period of interest
    opt: Options object describing station/run parameters
    
    Returns ObsPy stream objects, one for cutting and the other for triggering
    """

    nets = opt.network.split(',')
    stas = opt.station.split(',')
    locs = opt.location.split(',')
    chas = opt.channel.split(',')

    st = Stream()

    if opt.server == 'SAC' or opt.server == 'miniSEED':

        # Generate list of files
        if opt.server == 'SAC':
            flist = glob.glob(opt.sacdir + '*.sac') + glob.glob(opt.sacdir +
                                                                '*.SAC')
        elif opt.server == 'miniSEED':
            flist = glob.glob(opt.mseeddir +
                              '*.mseed') + glob.glob(opt.mseeddir + '*.MSEED')

        # Load data from file
        stmp = Stream()
        for f in flist:
            tmp = obspy.read(f, starttime=tstart, endtime=tend)
            if len(tmp) > 0:
                stmp = stmp.extend(tmp)

        # Filter and merge
        stmp = stmp.filter('bandpass',
                           freqmin=opt.fmin,
                           freqmax=opt.fmax,
                           corners=2,
                           zerophase=True)
        stmp = stmp.taper(0.05, type='hann', max_length=opt.mintrig)
        for m in range(len(stmp)):
            if stmp[m].stats.sampling_rate != opt.samprate:
                stmp[m] = stmp[m].resample(opt.samprate)
        stmp = stmp.merge(method=1, fill_value=0)

        # Only grab stations/channels that we want and in order
        netlist = []
        stalist = []
        chalist = []
        loclist = []
        for s in stmp:
            stalist.append(s.stats.station)
            chalist.append(s.stats.channel)
            netlist.append(s.stats.network)
            loclist.append(s.stats.location)

        # Find match of SCNL in header or fill empty
        for n in range(len(stas)):
            for m in range(len(stalist)):
                if (stas[n] in stalist[m] and chas[n] in chalist[m]
                        and nets[n] in netlist[m] and locs[n] in loclist[m]):
                    st = st.append(stmp[m])
            if len(st) == n:
                print("Couldn't find " + stas[n] + '.' + chas[n] + '.' +
                      nets[n] + '.' + locs[n])
                trtmp = Trace()
                trtmp.stats.sampling_rate = opt.samprate
                trtmp.stats.station = stas[n]
                st = st.append(trtmp.copy())

    else:

        if '.' not in opt.server:
            client = Client(opt.server)
        else:
            client = EWClient(opt.server, opt.port)

        for n in range(len(stas)):
            try:
                stmp = client.get_waveforms(nets[n], stas[n], locs[n], chas[n],
                                            tstart, tend)
                stmp = stmp.filter('bandpass',
                                   freqmin=opt.fmin,
                                   freqmax=opt.fmax,
                                   corners=2,
                                   zerophase=True)
                stmp = stmp.taper(0.05, type='hann', max_length=opt.mintrig)
                for m in range(len(stmp)):
                    if stmp[m].stats.sampling_rate != opt.samprate:
                        stmp[m] = stmp[m].resample(opt.samprate)
                stmp = stmp.merge(method=1, fill_value=0)
            except (obspy.fdsn.header.FDSNException):
                try:  # try again
                    stmp = client.get_waveforms(nets[n], stas[n], locs[n],
                                                chas[n], tstart, tend)
                    stmp = stmp.filter('bandpass',
                                       freqmin=opt.fmin,
                                       freqmax=opt.fmax,
                                       corners=2,
                                       zerophase=True)
                    stmp = stmp.taper(0.05,
                                      type='hann',
                                      max_length=opt.mintrig)
                    for m in range(len(stmp)):
                        if stmp[m].stats.sampling_rate != opt.samprate:
                            stmp[m] = stmp[m].resample(opt.samprate)
                    stmp = stmp.merge(method=1, fill_value=0)
                except (obspy.fdsn.header.FDSNException):
                    print('No data found for {0}.{1}'.format(stas[n], nets[n]))
                    trtmp = Trace()
                    trtmp.stats.sampling_rate = opt.samprate
                    trtmp.stats.station = stas[n]
                    stmp = Stream().extend([trtmp.copy()])
            st.extend(stmp.copy())

    st = st.trim(starttime=tstart, endtime=tend, pad=True, fill_value=0)
    stC = st.copy()

    return st, stC
Example #12
0
def getData(tstart, tend, opt):

    """
    Download data from files in a folder, from IRIS, or a Earthworm waveserver
    
    A note on SAC/miniSEED files: as this makes no assumptions about the naming scheme of
    your data files, please ensure that your headers contain the correct SCNL information!

    tstart: UTCDateTime of beginning of period of interest
    tend: UTCDateTime of end of period of interest
    opt: Options object describing station/run parameters
    
    Returns ObsPy stream objects, one for cutting and the other for triggering
    """    
    
    nets = opt.network.split(',')
    stas = opt.station.split(',')
    locs = opt.location.split(',')
    chas = opt.channel.split(',')
    
    st = Stream()
    
    if opt.server == 'SAC' or opt.server == 'miniSEED':
    
        # Generate list of files
        if opt.server == 'SAC':
            flist = list(itertools.chain.from_iterable(glob.iglob(os.path.join(
                root,'*.sac')) for root, dirs, files in os.walk(opt.sacdir)))+list(
                itertools.chain.from_iterable(glob.iglob(os.path.join(
                root,'*.SAC')) for root, dirs, files in os.walk(opt.sacdir)))
        elif opt.server == 'miniSEED':
            flist = list(itertools.chain.from_iterable(glob.iglob(os.path.join(
                root,'*.mseed')) for root, dirs, files in os.walk(opt.mseeddir)))+list(
                itertools.chain.from_iterable(glob.iglob(os.path.join(
                root,'*.MSEED')) for root, dirs, files in os.walk(opt.mseeddir)))
                
        # Determine which subset of files to load based on start and end times and
        # station name; we'll fully deal with stations below
        flist_sub = []
        for f in flist:
            # Load header only
            stmp = obspy.read(f, headonly=True)
            # Check if station is contained in the stas list
            if stmp[0].stats.station in stas:
                # Check if contains either start or end time
                ststart = stmp[0].stats.starttime
                stend = stmp[0].stats.endtime
                if (ststart<=tstart and tstart<=stend) or (ststart<=tend and
                    tend<=stend) or (tstart<=stend and ststart<=tend):
                    flist_sub.append(f)
        
        # Fully load data from file
        stmp = Stream()
        for f in flist_sub:
            tmp = obspy.read(f, starttime=tstart, endtime=tend+opt.maxdt)
            if len(tmp) > 0:
                stmp = stmp.extend(tmp)
    
        # Filter and merge
        stmp = stmp.filter('bandpass', freqmin=opt.fmin, freqmax=opt.fmax, corners=2,
            zerophase=True)
        stmp = stmp.taper(0.05,type='hann',max_length=opt.mintrig)
        for m in range(len(stmp)):
            if stmp[m].stats.sampling_rate != opt.samprate:
                stmp[m] = stmp[m].resample(opt.samprate)
        stmp = stmp.merge(method=1, fill_value=0)
        
        # Only grab stations/channels that we want and in order
        netlist = []
        stalist = []
        chalist = []
        loclist = []
        for s in stmp:
            stalist.append(s.stats.station)
            chalist.append(s.stats.channel)
            netlist.append(s.stats.network)
            loclist.append(s.stats.location)
            
        # Find match of SCNL in header or fill empty
        for n in range(len(stas)):
            for m in range(len(stalist)):
                if (stas[n] in stalist[m] and chas[n] in chalist[m] and nets[n] in
                    netlist[m] and locs[n] in loclist[m]):
                    st = st.append(stmp[m])
            if len(st) == n:
                print("Couldn't find "+stas[n]+'.'+chas[n]+'.'+nets[n]+'.'+locs[n])
                trtmp = Trace()
                trtmp.stats.sampling_rate = opt.samprate
                trtmp.stats.station = stas[n]
                st = st.append(trtmp.copy())
    
    else:   
     
        if '.' not in opt.server:
            client = Client(opt.server)
        else:
            client = EWClient(opt.server, opt.port)
        
        for n in range(len(stas)):
            try:
                stmp = client.get_waveforms(nets[n], stas[n], locs[n], chas[n],
                        tstart, tend+opt.maxdt)
                stmp = stmp.filter('bandpass', freqmin=opt.fmin, freqmax=opt.fmax,
                    corners=2, zerophase=True)
                stmp = stmp.taper(0.05,type='hann',max_length=opt.mintrig)
                for m in range(len(stmp)):
                    if stmp[m].stats.sampling_rate != opt.samprate:
                        stmp[m] = stmp[m].resample(opt.samprate)
                stmp = stmp.merge(method=1, fill_value=0)
            except (obspy.clients.fdsn.header.FDSNException):
                try: # try again
                    stmp = client.get_waveforms(nets[n], stas[n], locs[n], chas[n],
                            tstart, tend+opt.maxdt)
                    stmp = stmp.filter('bandpass', freqmin=opt.fmin, freqmax=opt.fmax,
                        corners=2, zerophase=True)
                    stmp = stmp.taper(0.05,type='hann',max_length=opt.mintrig)
                    for m in range(len(stmp)):
                        if stmp[m].stats.sampling_rate != opt.samprate:
                            stmp[m] = stmp[m].resample(opt.samprate)
                    stmp = stmp.merge(method=1, fill_value=0)
                except (obspy.clients.fdsn.header.FDSNException):
                    print('No data found for {0}.{1}'.format(stas[n],nets[n]))
                    trtmp = Trace()
                    trtmp.stats.sampling_rate = opt.samprate
                    trtmp.stats.station = stas[n]
                    stmp = Stream().extend([trtmp.copy()])
                                            
            # Last check for length; catches problem with empty waveserver
            if len(stmp) != 1:
                print('No data found for {0}.{1}'.format(stas[n],nets[n]))
                trtmp = Trace()
                trtmp.stats.sampling_rate = opt.samprate
                trtmp.stats.station = stas[n]
                stmp = Stream().extend([trtmp.copy()])
                
            st.extend(stmp.copy()) 
    
    # Edit 'start' time if using offset option
    if opt.maxdt:
        dts = np.fromstring(opt.offset, sep=',')
        for n, tr in enumerate(st):
            tr.stats.starttime = tr.stats.starttime-dts[n]
    
    st = st.trim(starttime=tstart, endtime=tend, pad=True, fill_value=0)
    stC = st.copy()
    
    return st, stC
Example #13
0
    print(file)
    site_idx = sites.index(file.split('/')[-1].split('.')[0])
    channel_idx = channels.index(file.split('/')[-1].split('.')[2][-3:])
    split_files[site_idx][channel_idx].append(file)

# Load in data for each site and perform noise analysis
for m, site in enumerate(sites):
    print('Parsing data for site: ' + site)
    for n, channel in enumerate(channels):
        streams = Stream()
        for o in range(len(split_files[m][n])):
            print('Parsing file:')
            print(split_files[m][n][o])
            streams += obspy.read(split_files[m][n][o])
        print('Merging streams...')
        streams.merge()
        print('Current data is:')
        print(streams)

        # Build probabilistic power spectral density objects for each trace
        all_ppsds = []
        all_ppsd_names = []
        for stream in streams:
            print('Calculating PPSDs for stream:')
            print(stream)
            ppsds = []
            ppsd_names = []
            metadata = Parser(metadata_directory + stream.stats.station +
                              stream.stats.channel[-1:] + '.seed')
            ppsd = PPSD(stream.stats, metadata)
            ppsd.add(stream)
Example #14
0
            if (stations_ok) and (tr.stats.station not in stations):
                continue
            if (locations_ok) and (tr.stats.location not in locations):
                continue
            if (channels_ok) and (tr.stats.channel not in channels):
                continue
            st.append(tr)

    if st.count() > 0:
        # If the starttime is given then it trims the resulting traces
        if starttime:
            st.trim(starttime=starttime,
                    endtime=endtime,
                    nearest_sample=nearest_sample)

        st.merge(method=1, fill_value=0, interpolation_samples=1)
    else:
        print "Empty stream"

    n_trace = st.count()
    return st, n_trace


if BC_UI:

    class _stream_dbread_view(HasTraits):

        time_interval = Float(30.0)
        networks = List(Str, value=['PF', 'YA'])
        stations = List(Str, value=['FOR', 'UV05'])
        locations = List(Str, value=['00', '10'])
Example #15
0
File: db.py Project: ftilmann/miic
                continue
            if (stations_ok) and (tr.stats.station not in stations):
                continue
            if (locations_ok) and (tr.stats.location not in locations):
                continue
            if (channels_ok) and (tr.stats.channel not in channels):
                continue
            st.append(tr)

    if st.count() > 0:
        # If the starttime is given then it trims the resulting traces
        if starttime:
            st.trim(starttime=starttime, endtime=endtime,
                    nearest_sample=nearest_sample)

        st.merge(method=1, fill_value=0, interpolation_samples=1)
    else:
        print "Empty stream"

    n_trace = st.count()
    return st, n_trace


if BC_UI:
    class _stream_dbread_view(HasTraits):
    
        time_interval = Float(30.0)
        networks = List(Str, value=['PF', 'YA'])
        stations = List(Str, value=['FOR', 'UV05'])
        locations = List(Str, value=['00', '10'])
        channels = List(Str, value=['HHZ', 'HLZ'])