def test_DayNoise(): trN1, trN2, trNZ, trNP = test_utils.test_get_data() args = test_args.test_get_dailyspec_arguments() for tr1, tr2, trZ, trP in zip(trN1, trN2, trNZ, trNP): daynoise = DayNoise(tr1, tr2, trZ, trP, args.window, args.overlap, key='7D.M08A') daynoise.QC_daily_spectra(pd=args.pd, tol=args.tol, alpha=args.alpha, smooth=args.smooth) daynoise.average_daily_spectra(calc_rotation=True, fig_average=True, fig_coh_ph=True, save='tmp', form='png') plot = plotting(daynoise.f, daynoise.power, daynoise.power, daynoise.goodwins, daynoise.ncomp, key='7D.M08A') daynoise.save('tmp')
def main(args=None): if args is None: # Run Input Parser args = get_dailyspec_arguments() # Load Database # stdb>0.1.3 try: db, stkeys = stdb.io.load_db(fname=args.indb, keys=args.stkeys) # stdb=0.1.3 except: db = stdb.io.load_db(fname=args.indb) # Construct station key loop allkeys = db.keys() sorted(allkeys) # Extract key subset if len(args.stkeys) > 0: stkeys = [] for skey in args.stkeys: stkeys.extend([s for s in allkeys if skey in s]) else: stkeys = db.keys() sorted(stkeys) # Loop over station keys for stkey in list(stkeys): # Extract station information from dictionary sta = db[stkey] # Path where data are located datapath = Path('DATA') / stkey if not datapath.is_dir(): print() print("Path to "+str(datapath)+" doesn`t exist - continuing") continue # Path where spectra will be saved specpath = Path('SPECTRA') / stkey if not specpath.is_dir(): print() print("Path to "+str(specpath)+" doesn`t exist - creating it") specpath.mkdir(parents=True) # Path where plots will be saved if args.saveplot: plotpath = specpath / 'PLOTS' if not plotpath.is_dir(): plotpath.mkdir(parents=True) else: plotpath = False # Get catalogue search start time if args.startT is None: tstart = sta.startdate else: tstart = args.startT # Get catalogue search end time if args.endT is None: tend = sta.enddate else: tend = args.endT if tstart > sta.enddate or tend < sta.startdate: continue # Temporary print locations tlocs = sta.location if len(tlocs) == 0: tlocs = [''] for il in range(0, len(tlocs)): if len(tlocs[il]) == 0: tlocs[il] = "--" sta.location = tlocs # Update Display print() print("|===============================================|") print("|===============================================|") print("| {0:>8s} |".format( sta.station)) print("|===============================================|") print("|===============================================|") print("| Station: {0:>2s}.{1:5s} |".format( sta.network, sta.station)) print("| Channel: {0:2s}; Locations: {1:15s} |".format( sta.channel, ",".join(tlocs))) print("| Lon: {0:7.2f}; Lat: {1:6.2f} |".format( sta.longitude, sta.latitude)) print("| Start time: {0:19s} |".format( sta.startdate.strftime("%Y-%m-%d %H:%M:%S"))) print("| End time: {0:19s} |".format( sta.enddate.strftime("%Y-%m-%d %H:%M:%S"))) print("|-----------------------------------------------|") # Get all components trN1, trN2, trNZ, trNP = utils.get_data(datapath, tstart, tend) # Window size window = args.window overlap = args.overlap # minimum numer of windows minwin = args.minwin # Time axis taxis = np.arange(0., window, trNZ[0].stats.delta) # Cycle through available data for tr1, tr2, trZ, trP in zip(trN1, trN2, trNZ, trNP): year = str(trZ.stats.starttime.year).zfill(4) jday = str(trZ.stats.starttime.julday).zfill(3) print() print("*"*60) print("* Calculating noise spectra for key " + stkey+" and day "+year+"."+jday) tstamp = year+'.'+jday+'.' filename = specpath / (tstamp+'spectra.pkl') if filename.exists(): if not args.ovr: print("* -> file "+str(filename)+" exists - continuing") continue # Initialize instance of DayNoise daynoise = DayNoise(tr1, tr2, trZ, trP, window, overlap, key=stkey) # Quality control to identify outliers daynoise.QC_daily_spectra( pd=args.pd, tol=args.tol, alpha=args.alpha, smooth=args.smooth, fig_QC=args.fig_QC, save=plotpath, form=args.form, debug=args.debug) # Check if we have enough good windows nwin = np.sum(daynoise.goodwins) if nwin < minwin: print("* Too few good data segments to calculate " + "average day spectra") # continue else: print("* {0} good windows. Proceeding...".format(nwin)) # Average spectra for good windows daynoise.average_daily_spectra( calc_rotation=args.calc_rotation, fig_average=args.fig_average, fig_coh_ph=args.fig_coh_ph, save=plotpath, form=args.form) # Save to file daynoise.save(filename)
def test_daynoise_demo(): return DayNoise('demo')
def transfer_func(self): """compute daily transfer function """ targetdt = 1. / self.sps oyear = self.otime.year omonth = self.otime.month oday = self.otime.day ohour = self.otime.hour omin = self.otime.minute osec = self.otime.second label = '%d_%s_%d_%d_%d_%d' % (oyear, monthdict[omonth], oday, ohour, omin, osec) self.eventdir = self.datadir + '/' + label self.outeventdir = self.outdir + '/' + label self.label = label chan_type = None # load SAC data for chtype in self.chan_rank: fname1 = self.eventdir + '/%s_%sH1.SAC' % (self.staid, chtype) fname2 = self.eventdir + '/%s_%sH2.SAC' % (self.staid, chtype) fnamez = self.eventdir + '/%s_%sHZ.SAC' % (self.staid, chtype) fnamep = self.eventdir + '/%s_%sDH.SAC' % (self.staid, chtype) if os.path.isfile(fname1) and os.path.isfile(fname1) and \ os.path.isfile(fnamez) and os.path.isfile(fnamep): chan_type = chtype break if chan_type is None: return 0 self.chan_type = chan_type fname1 = self.eventdir + '/%s_%sH1.SAC' % (self.staid, chan_type) fname2 = self.eventdir + '/%s_%sH2.SAC' % (self.staid, chan_type) fnamez = self.eventdir + '/%s_%sHZ.SAC' % (self.staid, chan_type) fnamep = self.eventdir + '/%s_%sDH.SAC' % (self.staid, chan_type) self.sth = obspy.read(fname1) self.sth += obspy.read(fname2) self.sth += obspy.read(fnamez) self.stp = obspy.read(fnamep) # if abs(self.sth[0].stats.delta - targetdt) > 1e-3 or abs(self.sth[1].stats.delta - targetdt) > 1e-3 or \ abs(self.sth[2].stats.delta - targetdt) > 1e-3 or abs(self.stp[0].stats.delta - targetdt) > 1e-3: raise ValueError('!!! CHECK fs :' + self.staid) else: self.sth[0].stats.delta = targetdt self.sth[1].stats.delta = targetdt self.sth[2].stats.delta = targetdt self.stp[0].stats.delta = targetdt stime_event = self.sth[-1].stats.starttime etime_event = self.sth[-1].stats.endtime self.sth.trim(starttime=stime_event, endtime=etime_event, pad=True, nearest_sample=True, fill_value=0.) self.stp.trim(starttime=stime_event, endtime=etime_event, pad=True, nearest_sample=True, fill_value=0.) self.window = self.sth[-1].stats.npts / self.sps # trim data # load daily noise data daystr = '%d.%s.%d.%s' % (self.otime.year, monthdict[self.otime.month], self.otime.day, self.staid) dfname1 = self.daydir + '/ft_%s.%sH1.SAC' % (daystr, chan_type) dfname2 = self.daydir + '/ft_%s.%sH2.SAC' % (daystr, chan_type) dfnamez = self.daydir + '/ft_%s.%sHZ.SAC' % (daystr, chan_type) dfnamep = self.daydir + '/ft_%s.%sDH.SAC' % (daystr, chan_type) if not( os.path.isfile(dfname1) and os.path.isfile(dfname2) and \ os.path.isfile(dfnamez) and os.path.isfile(dfnamep)): return 0 tr1 = obspy.read(dfname1)[0] tr2 = obspy.read(dfname2)[0] trZ = obspy.read(dfnamez)[0] trP = obspy.read(dfnamep)[0] if abs(tr1.stats.delta - targetdt) > 1e-3 or abs(tr2.stats.delta - targetdt) > 1e-3 or \ abs(trZ.stats.delta - targetdt) > 1e-3 or abs(trP.stats.delta - targetdt) > 1e-3: raise ValueError('!!! CHECK fs :' + self.staid) else: tr1.stats.delta = targetdt tr2.stats.delta = targetdt trP.stats.delta = targetdt trZ.stats.delta = targetdt # trim data slidind_wlength = self.window - int( self.overlap * self.window) * tr1.stats.delta stime_noise = tr1.stats.starttime newtime = np.floor((tr1.stats.endtime - stime_noise) / slidind_wlength) * slidind_wlength tr1.trim(starttime=stime_noise, endtime=stime_noise + newtime) tr2.trim(starttime=stime_noise, endtime=stime_noise + newtime) trZ.trim(starttime=stime_noise, endtime=stime_noise + newtime) trP.trim(starttime=stime_noise, endtime=stime_noise + newtime) if np.all(trP.data == 0.) and not (np.all(tr1.data == 0.) or np.all(tr2.data == 0.)): self.daynoise = DayNoise(tr1=tr1, tr2=tr2, trZ=trZ, trP=obspy.Trace(), overlap=self.overlap, window=self.window) self.out_dtype = 'Z2-1' elif (np.all(tr1.data == 0.) or np.all(tr2.data == 0.)) and (not np.all(trP.data == 0.)): self.daynoise = DayNoise(tr1=obspy.Trace(), tr2=obspy.Trace(), trZ=trZ, trP=trP, overlap=self.overlap, window=self.window) self.out_dtype = 'ZP' elif (not (np.all(tr1.data == 0.) or np.all(tr2.data == 0.))) and ( not np.all(trP.data == 0.)): self.daynoise = DayNoise(tr1=tr1, tr2=tr2, trZ=trZ, trP=trP, overlap=self.overlap, window=self.window) self.out_dtype = 'ZP-21' else: return 0 # self.daynoise.QC_daily_spectra() # self.daynoise.average_daily_spectra() # self.tfnoise = TFNoise(self.daynoise) # self.tfnoise.transfer_func() try: self.daynoise.QC_daily_spectra() self.daynoise.average_daily_spectra() self.tfnoise = TFNoise(self.daynoise) self.tfnoise.transfer_func() except: return -1 return 1
class atacr_event_sta(object): def __init__(self, inv, datadir, outdir, noisedir, otime, overlap=0.3, chan_rank=['L', 'H', 'B'], sps=1.): network = inv.networks[0] station = network[0] channel = station[0] self.stdb_inv = stdb.StDbElement(network = network.code, station = station.code, channel = channel.code[:2],\ location = channel.location_code, latitude = station.latitude, longitude = station.longitude,\ elevation = station.elevation, polarity=1., azcorr=0., startdate = station.start_date,\ enddate = station.end_date, restricted_status = station.restricted_status) self.datadir = datadir self.outdir = outdir self.noisedir = noisedir self.otime = otime self.overlap = overlap self.staid = network.code + '.' + station.code self.chan_rank = chan_rank self.stlo = station.longitude self.stla = station.latitude self.monthdir = self.noisedir + '/%04d.%s' % ( self.otime.year, monthdict[self.otime.month]) self.daydir = self.monthdir + '/%d.%s.%d' % ( self.otime.year, monthdict[self.otime.month], self.otime.day) self.sps = sps return def transfer_func(self): """compute daily transfer function """ targetdt = 1. / self.sps oyear = self.otime.year omonth = self.otime.month oday = self.otime.day ohour = self.otime.hour omin = self.otime.minute osec = self.otime.second label = '%d_%s_%d_%d_%d_%d' % (oyear, monthdict[omonth], oday, ohour, omin, osec) self.eventdir = self.datadir + '/' + label self.outeventdir = self.outdir + '/' + label self.label = label chan_type = None # load SAC data for chtype in self.chan_rank: fname1 = self.eventdir + '/%s_%sH1.SAC' % (self.staid, chtype) fname2 = self.eventdir + '/%s_%sH2.SAC' % (self.staid, chtype) fnamez = self.eventdir + '/%s_%sHZ.SAC' % (self.staid, chtype) fnamep = self.eventdir + '/%s_%sDH.SAC' % (self.staid, chtype) if os.path.isfile(fname1) and os.path.isfile(fname1) and \ os.path.isfile(fnamez) and os.path.isfile(fnamep): chan_type = chtype break if chan_type is None: return 0 self.chan_type = chan_type fname1 = self.eventdir + '/%s_%sH1.SAC' % (self.staid, chan_type) fname2 = self.eventdir + '/%s_%sH2.SAC' % (self.staid, chan_type) fnamez = self.eventdir + '/%s_%sHZ.SAC' % (self.staid, chan_type) fnamep = self.eventdir + '/%s_%sDH.SAC' % (self.staid, chan_type) self.sth = obspy.read(fname1) self.sth += obspy.read(fname2) self.sth += obspy.read(fnamez) self.stp = obspy.read(fnamep) # if abs(self.sth[0].stats.delta - targetdt) > 1e-3 or abs(self.sth[1].stats.delta - targetdt) > 1e-3 or \ abs(self.sth[2].stats.delta - targetdt) > 1e-3 or abs(self.stp[0].stats.delta - targetdt) > 1e-3: raise ValueError('!!! CHECK fs :' + self.staid) else: self.sth[0].stats.delta = targetdt self.sth[1].stats.delta = targetdt self.sth[2].stats.delta = targetdt self.stp[0].stats.delta = targetdt stime_event = self.sth[-1].stats.starttime etime_event = self.sth[-1].stats.endtime self.sth.trim(starttime=stime_event, endtime=etime_event, pad=True, nearest_sample=True, fill_value=0.) self.stp.trim(starttime=stime_event, endtime=etime_event, pad=True, nearest_sample=True, fill_value=0.) self.window = self.sth[-1].stats.npts / self.sps # trim data # load daily noise data daystr = '%d.%s.%d.%s' % (self.otime.year, monthdict[self.otime.month], self.otime.day, self.staid) dfname1 = self.daydir + '/ft_%s.%sH1.SAC' % (daystr, chan_type) dfname2 = self.daydir + '/ft_%s.%sH2.SAC' % (daystr, chan_type) dfnamez = self.daydir + '/ft_%s.%sHZ.SAC' % (daystr, chan_type) dfnamep = self.daydir + '/ft_%s.%sDH.SAC' % (daystr, chan_type) if not( os.path.isfile(dfname1) and os.path.isfile(dfname2) and \ os.path.isfile(dfnamez) and os.path.isfile(dfnamep)): return 0 tr1 = obspy.read(dfname1)[0] tr2 = obspy.read(dfname2)[0] trZ = obspy.read(dfnamez)[0] trP = obspy.read(dfnamep)[0] if abs(tr1.stats.delta - targetdt) > 1e-3 or abs(tr2.stats.delta - targetdt) > 1e-3 or \ abs(trZ.stats.delta - targetdt) > 1e-3 or abs(trP.stats.delta - targetdt) > 1e-3: raise ValueError('!!! CHECK fs :' + self.staid) else: tr1.stats.delta = targetdt tr2.stats.delta = targetdt trP.stats.delta = targetdt trZ.stats.delta = targetdt # trim data slidind_wlength = self.window - int( self.overlap * self.window) * tr1.stats.delta stime_noise = tr1.stats.starttime newtime = np.floor((tr1.stats.endtime - stime_noise) / slidind_wlength) * slidind_wlength tr1.trim(starttime=stime_noise, endtime=stime_noise + newtime) tr2.trim(starttime=stime_noise, endtime=stime_noise + newtime) trZ.trim(starttime=stime_noise, endtime=stime_noise + newtime) trP.trim(starttime=stime_noise, endtime=stime_noise + newtime) if np.all(trP.data == 0.) and not (np.all(tr1.data == 0.) or np.all(tr2.data == 0.)): self.daynoise = DayNoise(tr1=tr1, tr2=tr2, trZ=trZ, trP=obspy.Trace(), overlap=self.overlap, window=self.window) self.out_dtype = 'Z2-1' elif (np.all(tr1.data == 0.) or np.all(tr2.data == 0.)) and (not np.all(trP.data == 0.)): self.daynoise = DayNoise(tr1=obspy.Trace(), tr2=obspy.Trace(), trZ=trZ, trP=trP, overlap=self.overlap, window=self.window) self.out_dtype = 'ZP' elif (not (np.all(tr1.data == 0.) or np.all(tr2.data == 0.))) and ( not np.all(trP.data == 0.)): self.daynoise = DayNoise(tr1=tr1, tr2=tr2, trZ=trZ, trP=trP, overlap=self.overlap, window=self.window) self.out_dtype = 'ZP-21' else: return 0 # self.daynoise.QC_daily_spectra() # self.daynoise.average_daily_spectra() # self.tfnoise = TFNoise(self.daynoise) # self.tfnoise.transfer_func() try: self.daynoise.QC_daily_spectra() self.daynoise.average_daily_spectra() self.tfnoise = TFNoise(self.daynoise) self.tfnoise.transfer_func() except: return -1 return 1 def correct(self): """compute monthly transfer function """ tmptime = self.sth[-1].stats.starttime tstamp = str(tmptime.year).zfill(4)+'.' + \ str(tmptime.julday).zfill(3)+'.' tstamp = tstamp + str(tmptime.hour).zfill(2) + \ '.'+str(tmptime.minute).zfill(2) eventstream = EventStream( sta = self.stdb_inv, sth = self.sth, stp = self.stp,\ tstamp = tstamp, lat = self.stla, lon = self.stlo, time = tmptime,\ window = self.window, sampling_rate = 1., ncomp = 4) eventstream.correct_data(self.tfnoise) # save data if not os.path.isdir(self.outeventdir): os.makedirs(self.outeventdir) outTrZ = (self.sth.select(channel='??Z')[0]).copy() outTrZ.data = eventstream.correct[self.out_dtype].copy() outfnameZ = self.outeventdir + '/%s_%sHZ.SAC' % (self.staid, self.chan_type) if os.path.isfile(outfnameZ): shutil.copyfile(src=outfnameZ, dst=outfnameZ + '_old') os.remove(outfnameZ) outTrZ.write(outfnameZ, format='SAC')
def transfer_func(self): """compute monthly transfer function """ targetdt = 1./self.sps stime = obspy.UTCDateTime('%04d%02d01' %(self.year, self.month)) monthdir = self.monthdir if not os.path.isdir(monthdir): return False chan_type = None Nday = 0 while( (stime.year == self.year) and (stime.month == self.month)): daydir = monthdir+'/%d.%s.%d' %(self.year, monthdict[self.month], stime.day) if not os.path.isdir(daydir): stime += 86400. continue fpattern= daydir+'/ft_%d.%s.%d.%s' %(self.year, monthdict[self.month], stime.day, self.staid) if chan_type is None: for chtype in self.chan_rank: fname1 = fpattern+'.%sH1.SAC' %chtype fname2 = fpattern+'.%sH2.SAC' %chtype fnamez = fpattern+'.%sHZ.SAC' %chtype fnamep = fpattern+'.%sDH.SAC' %chtype if os.path.isfile(fname1) and os.path.isfile(fname1) and \ os.path.isfile(fnamez) and os.path.isfile(fnamep): chan_type = chtype break if chan_type is None: stime += 86400. continue fname1 = fpattern+'.%sH1.SAC' %chan_type fname2 = fpattern+'.%sH2.SAC' %chan_type fnamez = fpattern+'.%sHZ.SAC' %chan_type fnamep = fpattern+'.%sDH.SAC' %chan_type if not (os.path.isfile(fname1) and os.path.isfile(fname1) and \ os.path.isfile(fnamez) and os.path.isfile(fnamep)): stime += 86400. continue tr1 = obspy.read(fname1)[0] stimetr = tr1.stats.starttime tr2 = obspy.read(fname2)[0] trZ = obspy.read(fnamez)[0] trP = obspy.read(fnamep)[0] if abs(tr1.stats.delta - targetdt) > 1e-3 or abs(tr2.stats.delta - targetdt) > 1e-3 or \ abs(trZ.stats.delta - targetdt) > 1e-3 or abs(trP.stats.delta - targetdt) > 1e-3: raise ValueError('!!! CHECK fs :'+ self.staid) else: tr1.stats.delta = targetdt tr2.stats.delta = targetdt trP.stats.delta = targetdt trZ.stats.delta = targetdt # # # if np.all(tr1.data == 0.) or np.all(tr2.data == 0.) or np.all(trZ.data == 0.) or np.all(trP.data == 0.): # # # stime += 86400. # # # continue tr1.trim(starttime = stimetr, endtime = stimetr + 8400.*10-1) tr2.trim(starttime = stimetr, endtime = stimetr + 8400.*10-1) trZ.trim(starttime = stimetr, endtime = stimetr + 8400.*10-1) trP.trim(starttime = stimetr, endtime = stimetr + 8400.*10-1) # # # print (self.staid) if np.all(trP.data == 0.) and not (np.all(tr1.data == 0.) or np.all(tr2.data == 0.)): self.stanoise += DayNoise(tr1=tr1, tr2=tr2, trZ=trZ, trP=obspy.Trace(), overlap=self.overlap, window = self.window) self.out_dtype = 'Z2-1' elif (np.all(tr1.data == 0.) or np.all(tr2.data == 0.)) and (not np.all(trP.data == 0.)): self.stanoise += DayNoise(tr1=obspy.Trace(), tr2=obspy.Trace(), trZ=trZ, trP=trP, overlap=self.overlap, window = self.window) self.out_dtype = 'ZP' elif (not (np.all(tr1.data == 0.) or np.all(tr2.data == 0.))) and (not np.all(trP.data == 0.)): self.stanoise += DayNoise(tr1=tr1, tr2=tr2, trZ=trZ, trP=trP, overlap=self.overlap, window = self.window) self.out_dtype = 'ZP-21' else: stime += 86400. continue stime += 86400. Nday += 1 if Nday <= 1: return False self.stanoise.QC_sta_spectra() self.stanoise.average_sta_spectra() self.tfnoise= TFNoise(self.stanoise) self.tfnoise.transfer_func() return True