[edit] reformatted code

Code indentation and PEP 8 violations automatically reduced with IDE tool
This commit is contained in:
Sebastian Wehling-Benatelli 2016-03-22 08:42:09 +01:00
parent fa6626d62a
commit 7225da87db

View File

@ -11,15 +11,17 @@ function conglomerate utils.
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from pylot.core.read.inputs import AutoPickParameter
from pylot.core.pick.picker import AICPicker, PragPicker from pylot.core.pick.picker import AICPicker, PragPicker
from pylot.core.pick.charfuns import CharacteristicFunction from pylot.core.pick.charfuns import CharacteristicFunction
from pylot.core.pick.charfuns import HOScf, AICcf, ARZcf, ARHcf, AR3Ccf from pylot.core.pick.charfuns import HOScf, AICcf, ARZcf, ARHcf, AR3Ccf
from pylot.core.pick.utils import checksignallength, checkZ4S, earllatepicker,\ from pylot.core.pick.utils import checksignallength, checkZ4S, earllatepicker, \
getSNR, fmpicker, checkPonsets, wadaticheck getSNR, fmpicker, checkPonsets, wadaticheck
from pylot.core.util.utils import getPatternLine from pylot.core.util.utils import getPatternLine
from pylot.core.read.data import Data from pylot.core.read.data import Data
from pylot.core.analysis.magnitude import WApp from pylot.core.analysis.magnitude import WApp
def autopickevent(data, param): def autopickevent(data, param):
stations = [] stations = []
all_onsets = {} all_onsets = {}
@ -46,14 +48,18 @@ def autopickevent(data, param):
# check S-P times (Wadati) # check S-P times (Wadati)
return wadaticheck(jk_checked_onsets, wdttolerance, iplot) return wadaticheck(jk_checked_onsets, wdttolerance, iplot)
def autopickstation(wfstream, pickparam, verbose=False): def autopickstation(wfstream, pickparam, verbose=False):
""" """
:param: wfstream :param wfstream: `~obspy.core.stream.Stream` containing waveform
:type: `~obspy.core.stream.Stream` :type wfstream: obspy.core.stream.Stream
:param: pickparam :param pickparam: container of picking parameters from input file,
:type: container of picking parameters from input file,
usually autoPyLoT.in usually autoPyLoT.in
:type pickparam: AutoPickParameter
:param verbose:
:type verbose: bool
""" """
# declaring pickparam variables (only for convenience) # declaring pickparam variables (only for convenience)
@ -137,7 +143,7 @@ def autopickstation(wfstream, pickparam, verbose=False):
Pflag = 0 Pflag = 0
Sflag = 0 Sflag = 0
Pmarker = [] Pmarker = []
Ao = None # Wood-Anderson peak-to-peak amplitude Ao = None # Wood-Anderson peak-to-peak amplitude
# split components # split components
zdat = wfstream.select(component="Z") zdat = wfstream.select(component="Z")
@ -152,9 +158,9 @@ def autopickstation(wfstream, pickparam, verbose=False):
if algoP == 'HOS' or algoP == 'ARZ' and zdat is not None: if algoP == 'HOS' or algoP == 'ARZ' and zdat is not None:
msg = '##########################################\nautopickstation:' \ msg = '##########################################\nautopickstation:' \
' Working on P onset of station {station}\nFiltering vertical ' \ ' Working on P onset of station {station}\nFiltering vertical ' \
'trace ...\n{data}'.format(station=zdat[0].stats.station, 'trace ...\n{data}'.format(station=zdat[0].stats.station,
data=str(zdat)) data=str(zdat))
if verbose: print(msg) if verbose: print(msg)
z_copy = zdat.copy() z_copy = zdat.copy()
# filter and taper data # filter and taper data
@ -169,8 +175,8 @@ def autopickstation(wfstream, pickparam, verbose=False):
Lwf = zdat[0].stats.endtime - zdat[0].stats.starttime Lwf = zdat[0].stats.endtime - zdat[0].stats.starttime
Ldiff = Lwf - Lc Ldiff = Lwf - Lc
if Ldiff < 0: if Ldiff < 0:
msg = 'autopickstation: Cutting times are too large for actual ' \ msg = 'autopickstation: Cutting times are too large for actual ' \
'waveform!\nUsing entire waveform instead!' 'waveform!\nUsing entire waveform instead!'
if verbose: print(msg) if verbose: print(msg)
pstart = 0 pstart = 0
pstop = len(zdat[0].data) * zdat[0].stats.delta pstop = len(zdat[0].data) * zdat[0].stats.delta
@ -190,8 +196,9 @@ def autopickstation(wfstream, pickparam, verbose=False):
# CharacteristicFunction # CharacteristicFunction
# class needs stream object => build it # class needs stream object => build it
assert isinstance(cf1, CharacteristicFunction), 'cf2 is not set ' \ assert isinstance(cf1, CharacteristicFunction), 'cf2 is not set ' \
'correctly: maybe the algorithm name ({algoP}) is ' \ 'correctly: maybe the algorithm name ({algoP}) is ' \
'corrupted'.format(algoP=algoP) 'corrupted'.format(
algoP=algoP)
tr_aic = tr_filt.copy() tr_aic = tr_filt.copy()
tr_aic.data = cf1.getCF() tr_aic.data = cf1.getCF()
z_copy[0].data = tr_aic.data z_copy[0].data = tr_aic.data
@ -221,10 +228,10 @@ def autopickstation(wfstream, pickparam, verbose=False):
trH1_filt = edat.copy() trH1_filt = edat.copy()
trH2_filt = ndat.copy() trH2_filt = ndat.copy()
trH1_filt.filter('bandpass', freqmin=bph1[0], trH1_filt.filter('bandpass', freqmin=bph1[0],
freqmax=bph1[1], freqmax=bph1[1],
zerophase=False) zerophase=False)
trH2_filt.filter('bandpass', freqmin=bph1[0], trH2_filt.filter('bandpass', freqmin=bph1[0],
freqmax=bph1[1], freqmax=bph1[1],
zerophase=False) zerophase=False)
trH1_filt.taper(max_percentage=0.05, type='hann') trH1_filt.taper(max_percentage=0.05, type='hann')
trH2_filt.taper(max_percentage=0.05, type='hann') trH2_filt.taper(max_percentage=0.05, type='hann')
@ -253,8 +260,7 @@ def autopickstation(wfstream, pickparam, verbose=False):
############################################################## ##############################################################
# go on with processing if AIC onset passes quality control # go on with processing if AIC onset passes quality control
if (aicpick.getSlope() >= minAICPslope and if (aicpick.getSlope() >= minAICPslope and
aicpick.getSNR() >= minAICPSNR and aicpick.getSNR() >= minAICPSNR and Pflag == 1):
Pflag == 1):
aicPflag = 1 aicPflag = 1
msg = 'AIC P-pick passes quality control: Slope: {0} counts/s, ' \ msg = 'AIC P-pick passes quality control: Slope: {0} counts/s, ' \
'SNR: {1}\nGo on with refined picking ...\n' \ 'SNR: {1}\nGo on with refined picking ...\n' \
@ -288,15 +294,16 @@ def autopickstation(wfstream, pickparam, verbose=False):
############################################################## ##############################################################
# get refined onset time from CF2 using class Picker # get refined onset time from CF2 using class Picker
assert isinstance(cf2, CharacteristicFunction), 'cf2 is not set ' \ assert isinstance(cf2, CharacteristicFunction), 'cf2 is not set ' \
'correctly: maybe the algorithm name ({algoP}) is ' \ 'correctly: maybe the algorithm name ({algoP}) is ' \
'corrupted'.format(algoP=algoP) 'corrupted'.format(
algoP=algoP)
refPpick = PragPicker(cf2, tsnrz, pickwinP, iplot, ausP, tsmoothP, refPpick = PragPicker(cf2, tsnrz, pickwinP, iplot, ausP, tsmoothP,
aicpick.getpick()) aicpick.getpick())
mpickP = refPpick.getpick() mpickP = refPpick.getpick()
############################################################# #############################################################
if mpickP is not None: if mpickP is not None:
# quality assessment # quality assessment
# get earliest and latest possible pick and symmetrized uncertainty # get earliest/latest possible pick and symmetrized uncertainty
[lpickP, epickP, Perror] = earllatepicker(z_copy, nfacP, tsnrz, [lpickP, epickP, Perror] = earllatepicker(z_copy, nfacP, tsnrz,
mpickP, iplot) mpickP, iplot)
@ -481,7 +488,7 @@ def autopickstation(wfstream, pickparam, verbose=False):
############################################################# #############################################################
if mpickS is not None: if mpickS is not None:
# quality assessment # quality assessment
# get earliest and latest possible pick and symmetrized uncertainty # get earliest/latest possible pick and symmetrized uncertainty
h_copy[0].data = trH1_filt.data h_copy[0].data = trH1_filt.data
[lpickS1, epickS1, Serror1] = earllatepicker(h_copy, nfacS, [lpickS1, epickS1, Serror1] = earllatepicker(h_copy, nfacS,
tsnrh, tsnrh,
@ -496,28 +503,30 @@ def autopickstation(wfstream, pickparam, verbose=False):
epick = [epickS1, epickS2] epick = [epickS1, epickS2]
lpick = [lpickS1, lpickS2] lpick = [lpickS1, lpickS2]
pickerr = [Serror1, Serror2] pickerr = [Serror1, Serror2]
if epickS1 == None and epickS2 is not None: if epickS1 is None and epickS2 is not None:
ipick = 1 ipick = 1
elif epickS1 is not None and epickS2 == None: elif epickS1 is not None and epickS2 is None:
ipick = 0 ipick = 0
elif epickS1 is not None and epickS2 is not None: elif epickS1 is not None and epickS2 is not None:
ipick = np.argmin([epickS1, epickS2]) ipick = np.argmin([epickS1, epickS2])
elif algoS == 'AR3': elif algoS == 'AR3':
[lpickS3, epickS3, Serror3] = earllatepicker(h_copy, nfacS, [lpickS3, epickS3, Serror3] = earllatepicker(h_copy,
nfacS,
tsnrh, tsnrh,
mpickS, iplot) mpickS,
iplot)
# get earliest pick of all three picks # get earliest pick of all three picks
epick = [epickS1, epickS2, epickS3] epick = [epickS1, epickS2, epickS3]
lpick = [lpickS1, lpickS2, lpickS3] lpick = [lpickS1, lpickS2, lpickS3]
pickerr = [Serror1, Serror2, Serror3] pickerr = [Serror1, Serror2, Serror3]
if epickS1 == None and epickS2 is not None \ if epickS1 is None and epickS2 is not None \
and epickS3 is not None: and epickS3 is not None:
ipick = np.argmin([epickS2, epickS3]) ipick = np.argmin([epickS2, epickS3])
elif epickS1 is not None and epickS2 == None \ elif epickS1 is not None and epickS2 is None \
and epickS3 is not None: and epickS3 is not None:
ipick = np.argmin([epickS2, epickS3]) ipick = np.argmin([epickS2, epickS3])
elif epickS1 is not None and epickS2 is not None \ elif epickS1 is not None and epickS2 is not None \
and epickS3 == None: and epickS3 is None:
ipick = np.argmin([epickS1, epickS2]) ipick = np.argmin([epickS1, epickS2])
elif epickS1 is not None and epickS2 is not None \ elif epickS1 is not None and epickS2 is not None \
and epickS3 is not None: and epickS3 is not None:
@ -546,7 +555,7 @@ def autopickstation(wfstream, pickparam, verbose=False):
'SNR[dB]: {2}\n' 'SNR[dB]: {2}\n'
'################################################' '################################################'
''.format(Sweight, SNRS, SNRSdB)) ''.format(Sweight, SNRS, SNRSdB))
################################################################## ################################################################
# get Wood-Anderson peak-to-peak amplitude # get Wood-Anderson peak-to-peak amplitude
# initialize Data object # initialize Data object
data = Data() data = Data()
@ -563,8 +572,8 @@ def autopickstation(wfstream, pickparam, verbose=False):
else: else:
# use larger window for getting peak-to-peak amplitude # use larger window for getting peak-to-peak amplitude
# as the S pick is quite unsure # as the S pick is quite unsure
wapp = WApp(cordat, mpickP, mpickP + sstop + \ wapp = WApp(cordat, mpickP, mpickP + sstop +
(0.5 * (mpickP + sstop)), iplot) (0.5 * (mpickP + sstop)), iplot)
Ao = wapp.getwapp() Ao = wapp.getwapp()
@ -593,7 +602,8 @@ def autopickstation(wfstream, pickparam, verbose=False):
# calculate WA-peak-to-peak amplitude # calculate WA-peak-to-peak amplitude
# using subclass WApp of superclass Magnitude # using subclass WApp of superclass Magnitude
wapp = WApp(cordat, mpickP, mpickP + sstop + (0.5 * (mpickP wapp = WApp(cordat, mpickP, mpickP + sstop + (0.5 * (mpickP
+ sstop)), iplot) + sstop)),
iplot)
Ao = wapp.getwapp() Ao = wapp.getwapp()
else: else:
@ -650,7 +660,7 @@ def autopickstation(wfstream, pickparam, verbose=False):
plt.title('%s, %s, P Weight=%d' % (tr_filt.stats.station, plt.title('%s, %s, P Weight=%d' % (tr_filt.stats.station,
tr_filt.stats.channel, tr_filt.stats.channel,
Pweight)) Pweight))
plt.yticks([]) plt.yticks([])
plt.ylim([-1.5, 1.5]) plt.ylim([-1.5, 1.5])
plt.ylabel('Normalized Counts') plt.ylabel('Normalized Counts')
@ -789,7 +799,7 @@ def autopickstation(wfstream, pickparam, verbose=False):
phase = 'P' phase = 'P'
phasepick = {'lpp': lpickP, 'epp': epickP, 'mpp': mpickP, 'spe': Perror, phasepick = {'lpp': lpickP, 'epp': epickP, 'mpp': mpickP, 'spe': Perror,
'snr': SNRP, 'snrdb': SNRPdB, 'weight': Pweight, 'fm': FM, 'snr': SNRP, 'snrdb': SNRPdB, 'weight': Pweight, 'fm': FM,
'w0': None, 'fc': None, 'Mo': None, 'Mw': None} 'w0': None, 'fc': None, 'Mo': None, 'Mw': None}
picks = {phase: phasepick} picks = {phase: phasepick}
# add P marker # add P marker
picks[phase]['marked'] = Pmarker picks[phase]['marked'] = Pmarker
@ -801,7 +811,6 @@ def autopickstation(wfstream, pickparam, verbose=False):
# add Wood-Anderson amplitude # add Wood-Anderson amplitude
picks[phase]['Ao'] = Ao picks[phase]['Ao'] = Ao
return picks return picks
@ -827,70 +836,72 @@ def iteratepicker(wf, NLLocfile, picks, badpicks, pickparameter):
newpicks = {} newpicks = {}
for i in range(0, len(badpicks)): for i in range(0, len(badpicks)):
if len(badpicks[i][0]) > 4: if len(badpicks[i][0]) > 4:
Ppattern = '%s ? ? ? P' % badpicks[i][0] Ppattern = '%s ? ? ? P' % badpicks[i][0]
elif len(badpicks[i][0]) == 4: elif len(badpicks[i][0]) == 4:
Ppattern = '%s ? ? ? P' % badpicks[i][0] Ppattern = '%s ? ? ? P' % badpicks[i][0]
elif len(badpicks[i][0]) < 4: elif len(badpicks[i][0]) < 4:
Ppattern = '%s ? ? ? P' % badpicks[i][0] Ppattern = '%s ? ? ? P' % badpicks[i][0]
nllocline = getPatternLine(NLLocfile, Ppattern) nllocline = getPatternLine(NLLocfile, Ppattern)
res = nllocline.split(None)[16] res = nllocline.split(None)[16]
# get theoretical P-onset time from residuum # get theoretical P-onset time from residuum
badpicks[i][1] = picks[badpicks[i][0]]['P']['mpp'] - float(res) badpicks[i][1] = picks[badpicks[i][0]]['P']['mpp'] - float(res)
# get corresponding waveform stream # get corresponding waveform stream
msg = '#######################################################\n' \ msg = '#######################################################\n' \
'iteratepicker: Re-picking station {0}'.format(badpicks[i][0]) 'iteratepicker: Re-picking station {0}'.format(badpicks[i][0])
print(msg) print(msg)
wf2pick = wf.select(station=badpicks[i][0]) wf2pick = wf.select(station=badpicks[i][0])
# modify some picking parameters # modify some picking parameters
pstart_old = pickparameter.getParam('pstart') pstart_old = pickparameter.getParam('pstart')
pstop_old = pickparameter.getParam('pstop') pstop_old = pickparameter.getParam('pstop')
sstop_old = pickparameter.getParam('sstop') sstop_old = pickparameter.getParam('sstop')
pickwinP_old = pickparameter.getParam('pickwinP') pickwinP_old = pickparameter.getParam('pickwinP')
Precalcwin_old = pickparameter.getParam('Precalcwin') Precalcwin_old = pickparameter.getParam('Precalcwin')
noisefactor_old = pickparameter.getParam('noisefactor') noisefactor_old = pickparameter.getParam('noisefactor')
zfac_old = pickparameter.getParam('zfac') zfac_old = pickparameter.getParam('zfac')
pickparameter.setParam(pstart=max([0, badpicks[i][1] - wf2pick[0].stats.starttime \ pickparameter.setParam(
- pickparameter.getParam('tlta')])) pstart=max([0, badpicks[i][1] - wf2pick[0].stats.starttime \
pickparameter.setParam(pstop=pickparameter.getParam('pstart') + \ - pickparameter.getParam('tlta')]))
(3 * pickparameter.getParam('tlta'))) pickparameter.setParam(pstop=pickparameter.getParam('pstart') + \
pickparameter.setParam(sstop=pickparameter.getParam('sstop') / 2) (3 * pickparameter.getParam('tlta')))
pickparameter.setParam(pickwinP=pickparameter.getParam('pickwinP') / 2) pickparameter.setParam(sstop=pickparameter.getParam('sstop') / 2)
pickparameter.setParam(Precalcwin=pickparameter.getParam('Precalcwin') / 2) pickparameter.setParam(pickwinP=pickparameter.getParam('pickwinP') / 2)
pickparameter.setParam(noisefactor=1.0) pickparameter.setParam(
pickparameter.setParam(zfac=1.0) Precalcwin=pickparameter.getParam('Precalcwin') / 2)
print("iteratepicker: The following picking parameters have been modified for iterative picking:") pickparameter.setParam(noisefactor=1.0)
print("pstart: %fs => %fs" % (pstart_old, pickparameter.getParam('pstart'))) pickparameter.setParam(zfac=1.0)
print("pstop: %fs => %fs" % (pstop_old, pickparameter.getParam('pstop'))) print(
print("sstop: %fs => %fs" % (sstop_old, pickparameter.getParam('sstop'))) "iteratepicker: The following picking parameters have been modified for iterative picking:")
print("pickwinP: %fs => %fs" % (pickwinP_old, pickparameter.getParam('pickwinP'))) print(
print("Precalcwin: %fs => %fs" % (Precalcwin_old, pickparameter.getParam('Precalcwin'))) "pstart: %fs => %fs" % (pstart_old, pickparameter.getParam('pstart')))
print("noisefactor: %f => %f" % (noisefactor_old, pickparameter.getParam('noisefactor'))) print(
print("zfac: %f => %f" % (zfac_old, pickparameter.getParam('zfac'))) "pstop: %fs => %fs" % (pstop_old, pickparameter.getParam('pstop')))
print(
"sstop: %fs => %fs" % (sstop_old, pickparameter.getParam('sstop')))
print("pickwinP: %fs => %fs" % (
pickwinP_old, pickparameter.getParam('pickwinP')))
print("Precalcwin: %fs => %fs" % (
Precalcwin_old, pickparameter.getParam('Precalcwin')))
print("noisefactor: %f => %f" % (
noisefactor_old, pickparameter.getParam('noisefactor')))
print("zfac: %f => %f" % (zfac_old, pickparameter.getParam('zfac')))
# repick station # repick station
newpicks = autopickstation(wf2pick, pickparameter) newpicks = autopickstation(wf2pick, pickparameter)
# replace old dictionary with new one # replace old dictionary with new one
picks[badpicks[i][0]] = newpicks picks[badpicks[i][0]] = newpicks
# reset temporary change of picking parameters # reset temporary change of picking parameters
print("iteratepicker: Resetting picking parameters ...") print("iteratepicker: Resetting picking parameters ...")
pickparameter.setParam(pstart=pstart_old) pickparameter.setParam(pstart=pstart_old)
pickparameter.setParam(pstop=pstop_old) pickparameter.setParam(pstop=pstop_old)
pickparameter.setParam(sstop=sstop_old) pickparameter.setParam(sstop=sstop_old)
pickparameter.setParam(pickwinP=pickwinP_old) pickparameter.setParam(pickwinP=pickwinP_old)
pickparameter.setParam(Precalcwin=Precalcwin_old) pickparameter.setParam(Precalcwin=Precalcwin_old)
pickparameter.setParam(noisefactor=noisefactor_old) pickparameter.setParam(noisefactor=noisefactor_old)
pickparameter.setParam(zfac=zfac_old) pickparameter.setParam(zfac=zfac_old)
return picks return picks