def cut_event(event_id):
    # get event info
    [event_loc, pick_dict] = pha_list[event_id]
    if len(event_loc)==5: 
        ot, lat, lon, dep, mag = event_loc
    if len(event_loc)==4: 
        ot, lat, lon, mag = event_loc
        dep = -1
    data_dict = get_data_dict(ot, data_dir)
    event_name = dtime2str(ot)
    event_dir = os.path.join(out_root, event_name)
    if not os.path.exists(event_dir): os.makedirs(event_dir)

    # cut event
    print('cutting {}'.format(event_name))
    for net_sta, [tp, ts] in pick_dict.items():
        time_range = [tp-event_win[0], tp+event_win[1]]
        t0 = event_win[0]
        t1 = ts - tp + event_win[0] if ts else None
        if net_sta not in data_dict: continue
        data_paths = data_dict[net_sta]
        chn_codes = [data_path.split('.')[3][0:3] for data_path in data_paths]
        out_paths = [os.path.join(event_dir,'%s.%s.sac'%(net_sta,chn)) for chn in chn_codes]
        # cut event
        for i in range(3):
            st = read(data_paths[i], starttime=time_range[0], endtime=time_range[1])
            st.write(out_paths[i])
            sac.ch_event(out_paths[i], lon, lat, dep, mag, [t0,t1])
Ejemplo n.º 2
0
 def __getitem__(self, index):
     train_paths_i, valid_paths_i = [], []
     # get one sta-date
     sta_date, samples = self.sta_date_items[index]
     net_sta, date = sta_date.split('_')
     net, sta = net_sta.split('.')
     date = UTCDateTime(date)
     dtype = [('tp', 'O'), ('ts', 'O')]
     picks = np.array(
         [tuple(sample[-3:-1]) for sample in samples if sample[-1]],
         dtype=dtype)
     # read & prep one day's data
     print('reading %s %s' % (net_sta, date.date))
     data_dict = get_data_dict(date, data_dir)
     if net_sta not in data_dict: return train_paths_i, valid_paths_i
     st_paths = data_dict[net_sta]
     try:
         stream = read(st_paths[0])
         stream += read(st_paths[1])
         stream += read(st_paths[2])
     except:
         return train_paths_i, valid_paths_i
     if to_filter: stream = preprocess(stream, samp_rate, freq_band)
     if len(stream) != 3: return train_paths_i, valid_paths_i
     for [samp_class, event_name, tp, ts, _] in samples:
         out_dir = os.path.join(out_root, samp_class, 'negative',
                                event_name)
         samp_name = 'neg_%s_%s_%s' % (net, sta, event_name[:-3])
         n_aug = num_aug if samp_class == 'train' else 1
         for aug_idx in range(n_aug):
             # rand time shift
             start_time = tp + np.random.rand(1)[0] * rand_dt
             end_time = start_time + win_len
             # check if tp-ts exists in selected win
             is_tp = (picks['tp'] > max(ts, start_time)) * (picks['tp'] <
                                                            end_time)
             is_ts = (picks['ts'] > max(ts, start_time)) * (picks['ts'] <
                                                            end_time)
             if sum(is_tp * is_ts) > 0: continue
             # slice & prep
             st = obspy_slice(stream, start_time, end_time)
             if 0 in st.max() or len(st) != 3: continue
             st = st.detrend('demean').normalize(
                 global_max=global_max_norm)  # note: no detrend here
             # write & record out_paths
             if samp_class == 'train': train_paths_i.append([])
             if samp_class == 'valid': valid_paths_i.append([])
             for tr in st:
                 out_path = os.path.join(
                     out_dir,
                     '%s.%s.%s' % (aug_idx, samp_name, tr.stats.channel))
                 tr.stats.sac.t1 = ts - start_time
                 tr.write(out_path, format='sac')
                 if samp_class == 'train':
                     train_paths_i[-1].append(out_path)
                 if samp_class == 'valid':
                     valid_paths_i[-1].append(out_path)
     return train_paths_i, valid_paths_i
Ejemplo n.º 3
0
 def __getitem__(self, index):
     train_paths_i, valid_paths_i = [], []
     # get one sta-date
     sta_date, samples = self.sta_date_items[index]
     net_sta, date = sta_date.split('_')
     net, sta = net_sta.split('.')
     date = UTCDateTime(date)
     # read & prep one day's data
     print('reading %s %s' % (net_sta, date.date))
     data_dict = get_data_dict(date, data_dir)
     if net_sta not in data_dict: return train_paths_i, valid_paths_i
     st_paths = data_dict[net_sta]
     try:
         stream = read(st_paths[0])
         stream += read(st_paths[1])
         stream += read(st_paths[2])
     except:
         return train_paths_i, valid_paths_i
     if to_filter: stream = preprocess(stream, samp_rate, freq_band)
     if len(stream) != 3: return train_paths_i, valid_paths_i
     for [samp_class, event_name, tp, ts] in samples:
         out_dir = os.path.join(out_root, samp_class, 'positive',
                                event_name)
         samp_name = 'pos_%s_%s_%s' % (net, sta, event_name[:-3])
         n_aug = num_aug if samp_class == 'train' else 1
         for aug_idx in range(n_aug):
             # rand time shift & prep
             start_time = tp - np.random.rand(1)[0] * rand_dt
             end_time = start_time + win_len
             st = obspy_slice(stream, start_time, end_time)
             if 0 in st.max() or len(st) != 3: continue
             st = st.detrend('demean').normalize(
                 global_max=global_max_norm)  # note: no detrend here
             # write & record out_paths
             if samp_class == 'train': train_paths_i.append([])
             if samp_class == 'valid': valid_paths_i.append([])
             for tr in st:
                 if aug_idx > 0 and max_noise > 0:
                     tr = add_noise(tr, tp, ts)
                 out_path = os.path.join(
                     out_dir,
                     '%s.%s.%s' % (aug_idx, samp_name, tr.stats.channel))
                 tr.stats.sac.t0, tr.stats.sac.t1 = tp - start_time, ts - start_time
                 tr.write(out_path, format='sac')
                 if samp_class == 'train':
                     train_paths_i[-1].append(out_path)
                 if samp_class == 'valid':
                     valid_paths_i[-1].append(out_path)
     return train_paths_i, valid_paths_i
Ejemplo n.º 4
0
 def __getitem__(self, index):
     data_paths_i = []
     # get one sta-date
     sta_date, samples = self.sta_date_items[index]
     net_sta, date = sta_date.split('_')
     net, sta = net_sta.split('.')
     date = UTCDateTime(date)
     # read & prep one day's data
     print('reading %s %s' % (net_sta, date.date))
     data_dict = get_data_dict(date, data_dir)
     if net_sta not in data_dict: return data_paths_i
     st_paths = data_dict[net_sta]
     try:
         stream = read(st_paths[0])
         stream += read(st_paths[1])
         stream += read(st_paths[2])
     except:
         return data_paths_i
     stream = preprocess(stream, samp_rate, freq_band)
     if len(stream) != 3: return data_paths_i
     for [event_dir, tp, ts] in samples:
         # time shift & prep
         start_time = tp - win_len[0]
         end_time = tp + win_len[1]
         if data_format == 'sac':
             st = sac.obspy_slice(stream, start_time, end_time)
         else:
             st = stream.slice(start_time, end_time)
         if 0 in st.max() or len(st) != 3: continue
         st = st.detrend('demean')  # note: no detrend here
         # write & record out_paths
         data_paths_i.append([])
         for tr in st:
             out_path = os.path.join(event_dir,
                                     '%s.%s' % (net_sta, tr.stats.channel))
             tr.write(out_path, format='sac')
             tr = read(out_path)[0]
             tr.stats.sac.t0, tr.stats.sac.t1 = tp - start_time, ts - start_time
             tr.write(out_path, format='sac')
             data_paths_i[-1].append(out_path)
     return data_paths_i
Ejemplo n.º 5
0
def cut_event(event_id):
    # get event info
    [event_loc, pick_dict] = pha_list[event_id]
    ot, lat, lon, dep, mag = event_loc
    data_dict = get_data_dict(ot, data_dir)
    event_name = dtime2str(ot)
    event_dir = os.path.join(out_root, event_name)
    if not os.path.exists(event_dir): os.makedirs(event_dir)
    # cut event
    print('cutting {}'.format(event_name))
    for net_sta, [tp, ts] in pick_dict.items():
      if net_sta not in data_dict: continue
      for data_path in data_dict[net_sta]:
        b = tp - read(data_path)[0].stats.starttime - event_win[0]
        chn_code = data_path.split('.')[-2]
        out_path = os.path.join(event_dir,'%s.%s'%(net_sta,chn_code))
        # cut event
        sac.cut(data_path, b, b+event_win[1], out_path)
        # write header
        tn = {}
        tn['t0'] = event_win[0]
        if ts: tn['t1'] = ts - tp + event_win[0]
        sac.ch_event(out_path, lat, lon, dep, mag, tn)