def read_pair(id): """ Function to read a Pair/EigM file. Returns Pair object """ data = sw.load(id) #Check if we are reading an EigenM object or a Pair if type(data) is 'splitwavepy.measure.eigenM.EigenM': quality = data.quality pair = data.data return pair, quality else: Exception('Unexpected Object type')
def test_pickle_CrossM_io(self): # generate an eigenM object a = sw.CrossM(delta=0.1) # save to file filename = 'temp.crsm' a.save(filename) # load b = sw.load(filename) # check they are the same assert a == b # cleanup try: os.remove(filename) except OSError: print("Error: cleanup failed for some reason")
def splitting(station,files,switch='off',phase='SKS'): """ Measures SKS splitting for all streams listed in a ttext file at the provided path. These streams must be saved as SAC files.abs This function is the primary part of this module/package/script/thing, all the pther functions support this one. inpath - string contaiing the path of the textfile which contains the sac file to be read switch - optional kwarg to specify if you want to manually window the data or use a set of windows (Walpoles windows are availbale for use by default) """ outfile = output_init(station,switch,phase) with open(files,'r') as reader: # NEW_read_stream.txt is a textfile containing filenames of streams which have been read and saved by Split_Read for this station. s for line in reader.readlines(): line.strip('\n') st_id = '{}*'.format(str(line[0:-1])) st = read_sac(st_id) # Intialise some global variables which I need to pass things between fucntions (this is probably not be best way to do things but it works!) global pair_glob global quality global tt_glob if st != False and len(st) is 3: # i.e. if the stream is sufficiently populated and has been read. tt_UTC, t0, traveltime = model_traveltimes(st[0],phase) # Returns SKS arrival as a UTCDateTime object, event origin time and SKS arrival relative to the origin time tt_glob = traveltime #global variable, allows for function interect to call for a repeat meausrent # print(t0) # print('SKS_UTC ={}'.format(SKS_UTC)) quality = [] # variable to hold Callback key entries for estimated quality of splitting measurements date,time = int(str(t0.year)+str(t0.julday).zfill(3)),int(str(t0.hour).zfill(2)+str(t0.minute).zfill(2)+str(t0.second).zfill(2)) #creates time and date stamps if switch is 'on': eig_file ='{}/{}/{}/{}_{:07d}_{:06d}.eigm'.format('/Users/ja17375/Shear_Wave_Splitting/Python/Eigm_Files',phase,station,station,date,time) elif switch is 'off': eig_file = '{}/{}/{}/{}_{}_{:07d}_{:06d}'.format('/Users/ja17375/Shear_Wave_Splitting/Python/Eigm_Files',phase,station,station,'JW_Windows',date,time) if os.path.isfile(eig_file): split = sw.load(eig_file) #loads eigm file write_splitting(outfile,station,phase,eigm=split,st=st,date=date,time=time) else: pair = st_prep(st = st, f_min = 0.01,f_max = 0.5) # print(st[0].stats.starttime) pair_glob = pair if switch == 'on': # If manual windowing is on if phase == 'SKKS' and st[0].stats.sac.gcarc < 105.0: split = None else: print('Test Passed. Phase ={}, GCARC = {}'.format(phase,st[0].stats.sac.gcarc)) split, wbeg, wend,fig = split_measure(pair,traveltime) split.quality = quality plt.savefig('{}{}_{}_{:07d}_{:06d}'.format('/Users/ja17375/Shear_Wave_Splitting/Python/Figures/Eigm_Surface/',station,phase,date,time)) plt.close() write_splitting(outfile,station,phase,eigm=split,st=st,date=date,time=time) split.save(eig_file) elif switch == 'off': #Manual windowing is off. For now this will just mean Jacks windows will be used. Eventually add automation or support for entering windows. (wl_fast,wl_dfast,wl_tlag,wl_dtlag,wl_wbeg,wl_wend) = split_match(date,time,station) pair.set_window(wl_wbeg,wl_wend) # Sets window to that of Jacks wbeg,wend = pair.wbeg(), pair.wend() split = sw.EigenM(pair,lags=(4,) ) # Measures splitting fig = plt.figure(figsize=(12,6)) eigen_plot(split,fig) plt.savefig('{}/{}_{}_{}_{:07d}_{:06d}'.format('/Users/ja17375/Shear_Wave_Splitting/Python/Figures/Eigm_Surface',station,phase,'JW_Windows',date,time)) plt.close() split.quality = 'w' write_splitting(outfile,station,phase,eigm=split,st=st,date=date,time=time) #Call write_splitting where there are measuremtns to output # save_sac(st,quality[0],date,time,wbeg,wend,switch) split.save(eig_file) # Saves splitting measurements else: write_splitting(outfile,phase,station) plt.close('all') outfile.close()
def plotall(filestem): plt.figure(figsize=(15,10)) gs = gridspec.GridSpec(7, 6) ax0 = plt.subplot(gs[0, 0:5]) plt.ion() plt.show() # plot trace at top (this will put sks windows in automatically) sks = sw.load(filestem + '_sks.trnm') sks.srcpoldata()._ptr(ax0) # add the skks window skks = sw.load(filestem + '_skks.trnm') ax0.axvline(skks.data.wbeg(), linewidth=1, color='k') ax0.axvline(skks.data.wend(), linewidth=1, color='k') # plot FFT power spectrum from radial component ax06 = plt.subplot(gs[0,5]) power0 = np.abs(np.fft.fft(sks.srcpoldata().data()[0]))**2 power1 = np.abs(np.fft.fft(sks.srcpoldata().data()[1]))**2 freq = np.fft.fftfreq(sks.srcpoldata().data().shape[1], sks.srcpoldata().delta) ax06.fill_between(freq, power0, label='rad', alpha=0.5) ax06.fill_between(freq, power1, label='trans', alpha=0.5) ax06.set_xlim(0, 0.6) ax06.set_xlabel('Frequency (Hz)') ax06.set_ylabel('Power') ax06.legend() # now start plotting particle motions and error surf ax10 = plt.subplot(gs[1,0]) sks.data._ppm(ax10) ax20 = plt.subplot(gs[2,0]) sks.data_corr()._ppm(ax20) ax11 = plt.subplot(gs[1:3, 1:3]) sks._psurf(ax11, vals=sks.energy1/sks.energy2, conf95=True, marker=True, info=True) # ax10.axis('off') # ax20.axis('off') # ax11.axis('off') ax13 = plt.subplot(gs[1,3]) skks.data._ppm(ax13) ax23 = plt.subplot(gs[2,3]) skks.data_corr()._ppm(ax23) ax14 = plt.subplot(gs[1:3, 4:6]) skks._psurf(ax14, vals=skks.energy1/skks.energy2, conf95=True, marker=True, info=True) # now load eigenm files sks = sw.load(filestem + '_sks.eigm') skks = sw.load(filestem + '_skks.eigm') # now start plotting particle motions and error surf ax30 = plt.subplot(gs[3,0]) sks.data._ppm(ax30) ax40 = plt.subplot(gs[4,0]) sks.data_corr()._ppm(ax40) ax31 = plt.subplot(gs[3:5, 1:3]) sks._psurf(ax31, vals=sks.lam1/sks.lam2, conf95=True, marker=True, info=True) ax33 = plt.subplot(gs[3,3]) skks.data._ppm(ax33) ax43 = plt.subplot(gs[4,3]) skks.data_corr()._ppm(ax43) ax34 = plt.subplot(gs[3:5, 4:6]) skks._psurf(ax34, vals=skks.lam1/skks.lam2, conf95=True, marker=True, info=True) # now load crossm files sks = sw.load(filestem + '_sks.xcrm') skks = sw.load(filestem + '_skks.xcrm') # now start plotting particle motions and error surf ax50 = plt.subplot(gs[5,0]) sks.data._ppm(ax50) ax60 = plt.subplot(gs[6,0]) sks.data_corr()._ppm(ax60) ax51 = plt.subplot(gs[5:7, 1:3]) sks._psurf(ax51, vals=sks.fisher(), conf95=True, marker=True, info=True) ax53 = plt.subplot(gs[5,3]) skks.data._ppm(ax53) ax63 = plt.subplot(gs[6,3]) skks.data_corr()._ppm(ax63) ax54 = plt.subplot(gs[5:7, 4:6]) skks._psurf(ax54, vals=skks.fisher(), conf95=True, marker=True, info=True) # plt.tick_params( # axis='both', # changes apply to the x-axis # which='both', # both major and minor ticks are affected # bottom='off', # ticks along the bottom edge are off # top='off', # ticks along the top edge are off # labelbottom='off') plt.tight_layout() plt.draw() qual = input('Data Quality ([g]ood/[o]k/[p]oor) \n >') plt.draw() disc = input('Are SKS/SKKS discrepant (can add comments)? ([y]es/[n]o/[u]ndeterminable) \n ?') plt.close() return qual,disc