def est_SNR_1h(dstart,dend,ch1,ch2): # here you load all the functions you need to use from obspy.seg2.seg2 import readSEG2 from obspy.core import Stream import numpy as np from obspy.signal.cross_correlation import xcorr from numpy import sign dataDir = "/import/three-data/hadzii/STEINACH/STEINACH_longtime/" SNR=[] # loading the info for outfile-name stream_start = readSEG2(dataDir + str(dstart) + ".dat") t_start = stream_start[ch1].stats.seg2.ACQUISITION_TIME stream_end = readSEG2(dataDir + str(dend) + ".dat") t_end = stream_end[ch1].stats.seg2.ACQUISITION_TIME for k in range(dstart, dend, 1): st1 = merge_single(ch1,dstart,k+1) st2 = merge_single(ch2,dstart,k+1) st1.detrend('linear') st2.detrend('linear') st1.filter('lowpass',freq = 24, zerophase=True, corners=8) st1.filter('highpass', freq= 0.05, zerophase=True, corners=2) #had to be reduced from 0.1Hz st1.filter('bandstop', freqmin=8, freqmax=14, corners=4, zerophase=True) st2.filter('lowpass',freq = 24, zerophase=True, corners=8) st2.filter('highpass', freq= 0.05, zerophase=True, corners=2) #had to be reduced from 0.1Hz st2.filter('bandstop', freqmin=8, freqmax=14, corners=4, zerophase=True) # 1-bit normalization tr1 = sign(st1[0].data) tr2 = sign(st2[0].data) # cross-correlation index, value, acorr = xcorr(tr1, tr2, 25000, full_xcorr=True) # check sanity if np.max(acorr)>1: acorr = zeros(50001) corr += acorr SNR_ges=[] for isnrb3 in range(40000,49001,500): # steps of half a windowlength endwb3=isnrb3 + 1000 # 1s windows SNR_ges.append(np.max(np.abs(corr))/np.std(corr[isnrb3:endwb3])) SNR.append(np.max(SNR_ges))
def merge_single(nch,dstart,dend): '''Merges traces of one channel to larger traces. Used for cross-correlation''' # here you load all the functions you need to use from obspy.seg2.seg2 import readSEG2 from obspy.core import Stream # directories: dataDir2 = "/import/neptun-radler/STEINACH_feb/" dataDir = "/import/three-data/hadzii/STEINACH/STEINACH_longtime/" tr = [] for k in range(dstart, dend, 1): fname = '%d' %(k) fileName = fname + ".dat" st = readSEG2(dataDir + fileName) tr.append(st[nch-1]) new_stream = Stream(traces=tr) new_stream.merge(method=1, fill_value='interpolate') return new_stream
def merge_single(nch,dstart,dend): '''Merges traces of one channel to larger traces. Used for cross-correlation''' # here you load all the functions you need to use from obspy.seg2.seg2 import readSEG2 from obspy.core import Stream dataDir2 = "/import/neptun-radler/STEINACH_feb/" dataDir = "/import/three-data/hadzii/STEINACH/STEINACH_longtime/" outdir = "/home/jsalvermoser/Desktop/Processing/out_merged" tr = [] for k in range(dstart, dend, 1): fname = '%d' %(k) fileName = fname + ".dat" st = readSEG2(dataDir + fileName) #st.detrend('linear') tr.append(st[nch-1]) new_stream = Stream(traces=tr) new_stream.merge(method=1, fill_value='interpolate') start = new_stream[0].stats.starttime end = new_stream[0].stats.endtime timeframe = str(nch)+ "_" + str(start.year) +'.'+ str(start.julday) +'.'+ str(start.hour) +'.'+ str(start.minute) +'.'+ str(start.second) \ +'-'+ str(end.year) +'.'+ str(end.julday) +'.'+ str(end.hour) +'.'+ str(end.minute) +'.'+ str(end.second) new_stream.write(outdir + timeframe + ".mseed", format="MSEED") return new_stream[0]
def merge_single(nch,dstart,dend,dataDir): '''Merges traces of one channel to larger traces. Used for cross-correlation''' # here you load all the functions you need to use from obspy.seg2.seg2 import readSEG2 from obspy.core import Stream tr = [] for k in range(dstart, dend, 1): fname = '%d' %(k) fileName = fname + ".dat" st = readSEG2(dataDir + fileName) tr.append(st[nch-1]) new_stream = Stream(traces=tr) new_stream.merge(method=1, fill_value='interpolate') return new_stream
def summit_seg2_read(fname): """ Read SEG2 data recorded with the DMT summit aquisition unit. Read the SEG2 formatted seismic data aquired with the summit recorder and fill the ID fields in the stats. :parameters: ------------ :type fname: string :param fname: Path to the file containing the data to be read :rtype: :class:`~obspy.core.Stream` object :return: **st**: obspy.core.Stream object Obspy stream object containing the data """ try: from obspy.seg2.seg2 import readSEG2 except ImportError: print "obspy.segy package not installed. Exit" return # read the stream st = readSEG2(fname) # enter ID in fields of the stream for tr in st: tr.stats['network'] = tr.stats.seg2.MEASURING_POINT tr.stats['station'] = tr.stats.seg2.STATION_CODE tr.stats['channel'] = tr.stats.seg2.REGISTRATION_DIRECTION # This needs to be fixed tr.stats['sac'] = {} # longitude in degrees tr.stats['sac']['stlo'] = \ float(tr.stats.seg2.RECEIVER_LOCATION.split(' ')[0]) * \ 180 / 6371000 / 3.141592653589793 # longitude in degrees tr.stats['sac']['stla'] = \ float(tr.stats.seg2.RECEIVER_LOCATION.split(' ')[1]) * \ 180 / 6371000 / 3.141592653589793 # elevation in meters tr.stats['sac']['stel'] = \ float(tr.stats.seg2.RECEIVER_LOCATION.split(' ')[2]) return st
# PERFORM STRETCHING ch1=35 ch2=22 DEC_Dir= '/import/three-data/hadzii/STEINACH/Steinach/Steinach_ACTIVE/' FEB_Dir= '/import/three-data/hadzii/STEINACH/Steinach/STEINACH_act_feb/' DEC_fn='1002.dat' FEB_fn='1000002.dat' pos1D=6 pos2D=9 pos1F=6 pos2F=9 st_DEC = readSEG2(DEC_Dir + DEC_fn) st_DEC.detrend('linear') st_FEB = readSEG2(FEB_Dir + FEB_fn) st_FEB.detrend('linear') st_DEC.filter('lowpass',freq = 24, zerophase=True, corners=8) st_DEC.filter('highpass', freq= 0.05, zerophase=True, corners=2) #had to be reduced from 0.1Hz st_DEC.filter('bandstop', freqmin=8, freqmax=14, corners=4, zerophase=True) st_FEB.filter('lowpass',freq = 24, zerophase=True, corners=8) st_FEB.filter('highpass', freq= 0.05, zerophase=True, corners=2) #had to be reduced from 0.1Hz st_FEB.filter('bandstop', freqmin=8, freqmax=14, corners=4, zerophase=True) print st_DEC[0].stats.starttime print st_FEB[0].stats.starttime
def crossc(dstart,dend,ch1,ch2,day): # here you load all the functions you need to use from obspy.seg2.seg2 import readSEG2 from obspy.core import Stream import numpy as np from obspy.signal.cross_correlation import xcorr from numpy import sign from obspy.signal.filter import lowpass from obspy.signal.filter import highpass from obspy.signal.filter import bandstop from obspy.signal.filter import bandpass dataDir = "/import/three-data/hadzii/STEINACH/STEINACH_longtime/" outdir = "/home/jsalvermoser/Desktop/Processing/bands_SNR/" + "CH" + str(ch1) + "_CH" + str(ch2) + "/" + "JAN" + str(day) + "/" # loading the info for outfile-name stream_start = readSEG2(dataDir + str(dstart) + ".dat") t_start = stream_start[ch1].stats.seg2.ACQUISITION_TIME stream_end = readSEG2(dataDir + str(dend) + ".dat") t_end = stream_end[ch1].stats.seg2.ACQUISITION_TIME # initialization of the arrays and variables TR = [] rms = [] sq = [] ncalm = 1 nbeat = 1 corr128_calm = 0 corr128_beat = 0 nerror = 0 mu1c=0 mu2c=0 mu3c=0 mu1b=0 mu2b=0 mu3b=0 var1c=0 var2c=0 var3c=0 var1b=0 var2b=0 var3b=0 SNR_calm_b1=[] SNR_calm_b2=[] SNR_calm_b3=[] SNR_beat_b1=[] SNR_beat_b2=[] SNR_beat_b3=[] #TAPER taper_percentage=0.05 taper= np.blackman(int(len(time_vector) * taper_percentage)) taper_left, taper_right = np.array_split(taper,2) taper = np.concatenate([taper_left,np.ones(len(time_vector)-len(taper)),taper_right]) for j in range(0, dend-dstart): sq.append([]) for k in range(dstart, dend, 4): start = k end = k + 5 # only used to merge 5-1 = 4 files to one stream try: st1 = merge_single(ch1,start,end) st2 = merge_single(ch2,start,end) st1.detrend('linear') st2.detrend('linear') # calculate squares for rms r = k-dstart sq[r] = 0 for h in range(0,64000): sq[r] += (st1[0].data[h])**2 # lowpass-filter the crossc_beat correlation function st1.filter('lowpass',freq = 24, zerophase=True, corners=8) st1.filter('highpass', freq= 0.05, zerophase=True, corners=2) #had to be reduced from 0.1Hz st1.filter('bandstop', freqmin=8, freqmax=14, corners=4, zerophase=True) st2.filter('lowpass',freq = 24, zerophase=True, corners=8) st2.filter('highpass', freq= 0.05, zerophase=True, corners=2) #had to be reduced from 0.1Hz st2.filter('bandstop', freqmin=8, freqmax=14, corners=4, zerophase=True) # sometimes channels seem to fail, so I put this to prevent crashing of the program # 1-bit normalization tr1 = sign(st1[0].data) tr2 = sign(st2[0].data) # cross-correlation index, value, acorr = xcorr(tr1, tr2, 25000, full_xcorr=True) print sq[r] # check sanity if np.max(acorr)>1: acorr = zeros(50001) # sort the 128sec files into calm and beat: # the value was chosen after observing calm files if sq[r] < 1000000000000: corr128_calm += acorr ncalm += 1. else: corr128_beat += acorr nbeat += 1. print ncalm, nbeat # just to check if calm or noisy except: nerror += 1 print "%d : ERROR" %(r) if ncalm<8: corr128_calm = np.zeros(50001) # normalization else: corr128_calm = (corr128_calm/ncalm) * taper corr128_beat = (corr128_beat/nbeat) * taper # filter again and divide into 3 bands which can be investigated separately corr128_calm_band1 = highpass(corr128_calm, freq=0.1, corners=4, zerophase=True, df=500.) corr128_calm_band1 = lowpass(corr128_calm_band1, freq=2, corners=4, zerophase=True, df=500.) corr128_calm_band2 = bandpass(corr128_calm, freqmin=2, freqmax=8, df=500., corners=4, zerophase=True) corr128_calm_band3 = bandpass(corr128_calm, freqmin=8, freqmax=24, df=500., corners=4, zerophase=True) corr128_beat_band1 = highpass(corr128_beat, freq=0.1, df=500., corners=4, zerophase=True) corr128_beat_band1 = lowpass(corr128_beat_band1, freq=2, corners=4, zerophase=True, df=500.) corr128_beat_band2 = bandpass(corr128_beat, freqmin=2, freqmax=8, df=500., corners=4, zerophase=True) corr128_beat_band3 = bandpass(corr128_beat, freqmin=8, freqmax=24, df=500., corners=4, zerophase=True) # SNR (Signal-to-Noise Ratio):print 222222 # for the signal-to-noise ratio one divides the maximum of the signal by the # variance of a late window (noise). As we don't know which window has the # lowest signal fraction, we loop over some windows. We need windows of # different lengths for the different bands as different frequencies are # contained. For every band the minimum-frequency fmin is chosen (e.g. 4Hz), then # the time for one cyle is 1/fc (e.g. 0.25s) and as we take windows of 3-4 # cycles we choose a window length of 4*0.25s = 1s ## CALM + BEAT for isnrb1 in range(45000,50000,2500): # steps of half a windowlength endwb1=isnrb1 + 2500 # 5s window SNR_calm_b1.append(np.max(np.abs(corr128_calm_band1))/np.std(corr128_calm_band1[isnrb1:endwb1])) SNR_beat_b1.append(np.max(np.abs(corr128_beat_band1))/np.std(corr128_beat_band1[isnrb1:endwb1])) SNR_calm_b1 = max(SNR_calm_b1) SNR_beat_b1 = max(SNR_beat_b1) for isnrb2 in range(45000,49001,500): # steps of half a windowlength endwb2=isnrb2 + 1000 # 2s windows SNR_calm_b2.append(np.max(np.abs(corr128_calm_band2))/np.std(corr128_calm_band2[isnrb2:endwb2])) SNR_beat_b2.append(np.max(np.abs(corr128_beat_band2))/np.std(corr128_beat_band2[isnrb2:endwb2])) SNR_beat_b2 = max(SNR_beat_b2) SNR_calm_b2 = max(SNR_calm_b2) for isnrb3 in range(45000,49751,125): # steps of half a windowlength endwb3=isnrb3 + 250 # 0.5s windows SNR_calm_b3.append(np.max(np.abs(corr128_calm_band3))/np.std(corr128_calm_band3[isnrb3:endwb3])) SNR_beat_b3.append(np.max(np.abs(corr128_beat_band3))/np.std(corr128_beat_band3[isnrb3:endwb3])) SNR_beat_b3 = max(SNR_beat_b3) SNR_calm_b3 = max(SNR_calm_b3) if ncalm<8: SNR_calm_b1 = 0 SNR_calm_b2 = 0 SNR_calm_b3 = 0 print SNR_calm_b1, SNR_calm_b2, SNR_calm_b3 print SNR_beat_b1, SNR_beat_b2, SNR_beat_b3 # RMS for histogram and sifting: #for s in range(0,dend-dstart): # rms.append((sq[s]/16000)**(0.5)) # save into files: np.save(outdir + t_start + "-" + t_end + "CH" + str(ch1) + "_" +"xcorr128s_beat_0-2Hz" + "_" + "CH" + str(ch2), corr128_beat_band1) np.save(outdir + t_start + "-" + t_end + "CH" + str(ch1) + "_" +"xcorr128s_beat_2-8Hz" + "_" + "CH" + str(ch2), corr128_beat_band2) np.save(outdir + t_start + "-" + t_end + "CH" + str(ch1) + "_" +"xcorr128s_beat_8-24Hz" + "_" + "CH" + str(ch2), corr128_beat_band3) np.save(outdir + t_start + "-" + t_end + "CH" + str(ch1) + "_" +"xcorr128s_calm_0-2Hz" + "_" + "CH" + str(ch2), corr128_calm_band1) np.save(outdir + t_start + "-" + t_end + "CH" + str(ch1) + "_" +"xcorr128s_calm_2-8Hz" + "_" + "CH" + str(ch2), corr128_calm_band2) np.save(outdir + t_start + "-" + t_end + "CH" + str(ch1) + "_" +"xcorr128s_calm_8-24Hz" + "_" + "CH" + str(ch2), corr128_calm_band3) # np.save(outdir + "JAN_"+"CH" + str(ch1) + "_" +"RMS" + "_" + "CH" + str(ch2) + str(dstart) + "-" + str(dend), rms) return corr128_beat_band1,corr128_beat_band2,corr128_beat_band3, corr128_calm_band1,corr128_calm_band2,corr128_calm_band3, ncalm, nbeat, SNR_beat_b1, SNR_beat_b2, SNR_beat_b3, SNR_calm_b1, SNR_calm_b2, SNR_calm_b3
def merge(nch,dstart,dend): ''' read the file 'fileName' that is in directory 'dataDir' into a stream called 'st' The i-loop is over the channels/geophones, so the merged data of i geophones is plotted in the end. The k to m loops are over the data files, so traces from files 1 to 113 are merged together which equals one hour of measurements. For each channel i the corresponding trace is picked from all the 113 files and then written into a new stream 'new_stream'. In this stream all traces (consecutive in time) are merged then into one long trace using the 'merge-method 1'. As the new_stream only consists of one trace the index '[0]' can always be used. 't' is an array, built to merge a combined time axis for all the merged traces. The new_stream data is finally plotted against the time axis. This procedure is iterated for each channel, whereas subplots are built. ''' # here you load all the functions you need to use from obspy.seg2.seg2 import readSEG2 from obspy.core import Stream import matplotlib.pyplot as plt import numpy as np from numpy import matrix import time time_start = time.clock() dataDir = "/home/johannes/~home/Steinach/DEC_JAN/DEC_night/" outdir = "/home/johannes/~home/Steinach/outmseed/" ax = plt.subplot(111) TR = [] for a in range(0,nch): TR.append([]) for k in range(dstart, dend, 1): fname = '%d' %(k) fileName = fname + ".dat" st = readSEG2(dataDir + fileName) for i in range(0,nch): TR[i].append(st[i]) for m in range(0,nch): new_stream = Stream(traces=TR[m]) new_stream.merge(method=0, fill_value='interpolate') start = new_stream[0].stats.starttime end = new_stream[0].stats.endtime timeframe = str(m+1)+ "_" + str(start.year) +'.'+ str(start.julday) +'.'+ str(start.hour) +'.'+ str(start.minute) +'.'+ str(start.second) \ +'-'+ str(end.year) +'.'+ str(end.julday) +'.'+ str(end.hour) +'.'+ str(end.minute) +'.'+ str(end.second) new_stream.write(outdir + "CH" + str(m+1) + "/" + timeframe + ".mseed", format="MSEED") new_stream[0].normalize() dt = new_stream[0].stats.starttime.timestamp t = np.linspace(new_stream[0].stats.starttime.timestamp - dt, new_stream[0].stats.endtime.timestamp -dt, new_stream[0].stats.npts) ax.plot(t, new_stream[0].data + 1.5* m) time_elapsed = (time.clock() - time_start) print time_elapsed plt.show()
#! /usr/bin/env python # -*- coding: utf-8 -*- from obspy.seg2.seg2 import readSEG2 dataDir = "/home/jsalvermoser/Desktop/STEINACH_act_feb/" path = "/home/jsalvermoser/Desktop/STEINACH_act_feb/*.dat" import matplotlib.pyplot as plt import numpy as np import glob spec = 0 k=0 for fname in glob.glob(path): st=readSEG2(fname) spec += np.fft.rfft(st[5].data) k+=1 print fname print k spec_sum = spec/k #fileName = '1000006.dat' #st = readSEG2(dataDir + fileName) #data = st[5].data #spec1 = np.fft.rfft(data) fmax = 1000 # sampling frequency divided by 2 --> Nyquist frequency freq = np.linspace(0,fmax,len(spec)) # lowpass-filter the crossc_beat correlation function