def merge_sta_chn(sac_dir, sta_chn): date_code = os.path.basename(sac_dir) sta, chn = sta_chn.split('.') st_paths = glob.glob(os.path.join(sac_dir, '%s.*.%s.sac'%(sta, chn))) out_path = os.path.join(sac_dir, '%s.%s.%s.SAC'%(sta,date_code,chn)) sac.merge(st_paths, out_path) for st_path in st_paths: os.unlink(st_path)
if not os.path.exists(out_dir): os.makedirs(out_dir) out_name = '%s.%s.%s.sac'%(sta, t0, chn) out_path = os.path.join(out_dir, out_name) tr.slice(date, date+86400).write(out_path) if t0 < date: out_dir = os.path.join(sac_root, sta, date2dir(date-86400)) if not os.path.exists(out_dir): os.makedirs(out_dir) out_path = os.path.join(out_dir, out_name) tr = tr.slice(t0, date).write(out_path) if t1 > date+86400: out_dir = os.path.join(sac_root, sta, date2dir(date+86400)) if not os.path.exists(out_dir): os.makedirs(out_dir) out_path = os.path.join(out_dir, out_name) tr = tr.slice(date+86400, t1).write(out_path) # 2. merge sac files in sac dir sac_dirs = sorted(glob.glob(os.path.join(sac_root, '*/*/*/*'))) for sac_dir in sac_dirs: print('merge sac files in %s' %sac_dir) os.chdir(sac_dir) info = os.getcwd().split('/') sta = info[-4] date = info[-3] + info[-2] + info[-1] sac.merge(glob.glob('*.HHE.sac'), '%s.%s.%s.HHE.SAC' %(net, sta, date)) sac.merge(glob.glob('*.HHN.sac'), '%s.%s.%s.HHN.SAC' %(net, sta, date)) sac.merge(glob.glob('*.HHZ.sac'), '%s.%s.%s.HHZ.SAC' %(net, sta, date)) # delete sac segments todel = glob.glob('*.sac') for fname in todel: os.unlink(fname)
out_path0 = os.path.join(out_dir0, out_name0) tr0.write(out_path0) if t1 > date + 86400: tr1 = tr.slice(date + 86400, t1) out_dir1 = os.path.join(sac_root, sta, date2dir(date + 86400)) if not os.path.exists(out_dir1): os.makedirs(out_dir1) out_name1 = 'aug1.' + out_name out_path1 = os.path.join(out_dir1, out_name1) tr1.write(out_path1) # 2. merge sac files in sac dir """ This can be done seperatly """ sac_dirs = sorted(glob.glob(os.path.join(sac_root, '*/*/*/*'))) for sac_dir in sac_dirs: print('merge sac files in %s' % sac_dir) os.chdir(sac_dir) codes = os.getcwd().split('/') sta = codes[-4] date = codes[-3] + codes[-2] + codes[-1] sac.merge(glob.glob('*.%s.*' % chn_seq[0]), '%s.%s.%s.%s.SAC' % (net, sta, date, chn_seq[0])) sac.merge(glob.glob('*.%s.*' % chn_seq[1]), '%s.%s.%s.%s.SAC' % (net, sta, date, chn_seq[1])) sac.merge(glob.glob('*.%s.*' % chn_seq[2]), '%s.%s.%s.%s.SAC' % (net, sta, date, chn_seq[2])) # delete sac segments todel = glob.glob('*.sac') for fname in todel: os.unlink(fname)
if t0 < date: st0 = st.slice(t0, date) out_dir0 = os.path.join(sac_root, sta, date2dir(date - 86400)) if not os.path.exists(out_dir0): os.makedirs(out_dir0) out_name0 = 'aug0.' + out_name out_path0 = os.path.join(out_dir0, out_name0) st0.write(out_path0) if t1 > date + 86400: st1 = st.slice(date + 86400, t1) out_dir1 = os.path.join(sac_root, sta, date2dir(date + 86400)) if not os.path.exists(out_dir1): os.makedirs(out_dir1) out_name1 = 'aug1.' + out_name out_path1 = os.path.join(out_dir1, out_name1) st1.write(out_path1) # 2. merge sac files in sac dir sac_dirs = sorted(glob.glob(os.path.join(sac_root, '*/*/*/*'))) for sac_dir in sac_dirs: print('merge sac files in %s' % sac_dir) os.chdir(sac_dir) info = os.getcwd().split('/') sta = info[-4] date = info[-3] + info[-2] + info[-1] sac.merge(glob.glob('*.HHE.sac'), 'XLS.%s.%s.HHE.SAC' % (sta, date)) sac.merge(glob.glob('*.HHN.sac'), 'XLS.%s.%s.HHN.SAC' % (sta, date)) sac.merge(glob.glob('*.HHZ.sac'), 'XLS.%s.%s.HHZ.SAC' % (sta, date)) # delete sac segments todel = glob.glob('*.sac') for fname in todel: os.unlink(fname)
out_path = get_outpath(dst_root, net, sta, chn, ti) sac.cut(sac_file, begin, end, out_path) # cut tail (after tn) out_path = get_outpath(dst_root, net, sta, chn, tn) sac.cut(sac_file, tn - ts, te - ts, out_path) todel = glob.glob('*.SAC') for fname in todel: os.unlink(fname) # 2. merge sac files in dst_dir dst_dirs = glob.glob(os.path.join(dst_root, '%s/*/*/*/*' % net)) # net/sta/year/month/day for dst_dir in dst_dirs: os.chdir(dst_dir) sac_files = glob.glob('*.sac') # cutted, processed SAC remains todel = sac_files print('merge streams: {}'.format(dst_dir)) # merge for chn in ['BHE', 'BHN', 'BHZ']: tomerge = glob.glob("*.%s.*" % chn) if len(tomerge) == 0: continue net, sta, year, jday, time, chn, _ = tomerge[0].split('.') fname = "%s.%s.%s.%s.%s.SAC" % (net, sta, year, jday, chn) sac.merge(tomerge, fname) for fname in todel: if os.path.exists(fname): os.unlink(fname)
out_path = os.path.join(out_dir, 'aug.' + fname) sac.cut(fname, 0, date0 - t0, out_path) if t1 > date1: out_dir = os.path.join(sac_root, sta, date2dir(t1)) if not os.path.exists(out_dir): os.makedirs(out_dir) out_path = os.path.join(out_dir, 'aug.' + fname) sac.cut(fname, date1 - t0, t1 - t0, out_path) if date1 < t0 or date0 > t1: os.unlink(fname) else: sac.cut(fname, date0 - t0, date1 - t0, fname) # 3. merge in sac dir sac_dirs = sorted(glob.glob(os.path.join(sac_root, 'KMI/201[8-9]/*/*'))) for sac_dir in sac_dirs: print('merge sac files in %s' % sac_dir) os.chdir(sac_dir) codes = sac_dir.split('/') net, sta = codes[-5:-3] date = UTCDateTime(''.join(codes[-3:])) year = str(date.year) jday = str(date.julday).zfill(3) sac.merge(glob.glob('*.BHE.D.SAC'), '%s.%s.%s.%s.BHE.SAC' % (net, sta, year, jday)) sac.merge(glob.glob('*.BHN.D.SAC'), '%s.%s.%s.%s.BHN.SAC' % (net, sta, year, jday)) sac.merge(glob.glob('*.BHZ.D.SAC'), '%s.%s.%s.%s.BHZ.SAC' % (net, sta, year, jday)) # delete sac segments todel = glob.glob('*.D.SAC') for fname in todel: os.unlink(fname)
# 3. merge sac segments def get_sta_codes(sac_dir): sta_codes = [] os.chdir(sac_dir) fnames = glob.glob('*.sac') for fname in fnames: codes = fname.split('.') net, sta = codes[0:2] chn = codes[-2] sta_code = '%s.%s.%s' % (net, sta, chn) if not sta_code in sta_codes: sta_codes.append(sta_code) return sta_codes sac_dirs = sorted(glob.glob(os.path.join(sac_root, '*'))) for sac_dir in sac_dirs: if not os.path.isdir(sac_dir): continue print('merge sac segments: %s' % sac_dir) date = os.path.basename(sac_dir) sta_codes = get_sta_codes(sac_dir) for sta_code in sta_codes: net, sta, chn = sta_code.split('.') sac_paths = glob.glob( os.path.join(sac_dir, '%s.%s.*.%s.sac' % (net, sta, chn))) out_path = os.path.join(sac_dir, '%s.%s.%s.%s.SAC' % (net, sta, date, chn)) sac.merge(sac_paths, out_path) for sac_path in sac_paths: if os.path.exists(sac_path): os.unlink(sac_path)