Decrease indentation by returning from error condition

This commit is contained in:
Darius Arnold 2018-07-06 16:17:07 +02:00
parent 71d8626fa3
commit 1ff50c000e

View File

@ -420,8 +420,6 @@ class AutopickStation(object):
def autopickstation(self): def autopickstation(self):
try: try:
self.pick_p_phase() self.pick_p_phase()
#TODO handle exceptions correctly (goal is to be compatible with old code first)
# requires an overlook of what should be returned in case picking fails at various stages
except MissingTraceException as mte: except MissingTraceException as mte:
print(mte) print(mte)
except PickingFailedException as pfe: except PickingFailedException as pfe:
@ -741,8 +739,11 @@ class AutopickStation(object):
fig, linecolor = get_fig_from_figdict(self.fig_dict, 'aicFig') fig, linecolor = get_fig_from_figdict(self.fig_dict, 'aicFig')
aicpick = AICPicker(aiccf, self.p_params.tsnrz, self.p_params.pickwinP, self.iplot, aicpick = AICPicker(aiccf, self.p_params.tsnrz, self.p_params.pickwinP, self.iplot,
Tsmooth=self.p_params.aictsmooth, fig=fig, linecolor=linecolor) Tsmooth=self.p_params.aictsmooth, fig=fig, linecolor=linecolor)
# save aicpick for plotting later
self.p_results.aicpick = aicpick
# add pstart and pstop to aic plot # add pstart and pstop to aic plot
if fig: if fig:
# TODO remove plotting from picking, make own plot function
for ax in fig.axes: for ax in fig.axes:
ax.vlines(self.p_params.pstart, ax.get_ylim()[0], ax.get_ylim()[1], color='c', linestyles='dashed', label='P start') ax.vlines(self.p_params.pstart, ax.get_ylim()[0], ax.get_ylim()[1], color='c', linestyles='dashed', label='P start')
ax.vlines(self.p_params.pstop, ax.get_ylim()[0], ax.get_ylim()[1], color='c', linestyles='dashed', label='P stop') ax.vlines(self.p_params.pstop, ax.get_ylim()[0], ax.get_ylim()[1], color='c', linestyles='dashed', label='P stop')
@ -752,72 +753,75 @@ class AutopickStation(object):
# go on with processing if AIC onset passes quality control # go on with processing if AIC onset passes quality control
slope = aicpick.getSlope() slope = aicpick.getSlope()
if not slope: slope = 0 if not slope: slope = 0
if slope >= self.p_params.minAICPslope and aicpick.getSNR() >= self.p_params.minAICPSNR and Pflag == 1: # todo why did picking fail was saved in the pick dictionary, should this be reimplemented?
self.p_results.aicPflag = 1 if Pflag != 1:
msg = 'AIC P-pick passes quality control: Slope: {0} counts/s, ' \ raise PickingFailedException('AIC P onset quality control failed')
'SNR: {1}\nGo on with refined picking ...\n' \ if slope <= self.p_params.minAICPslope:
'autopickstation: re-filtering vertical trace ' \ error_msg = 'AIC P onset slope to small: got {}, min {}'.format(slope, self.p_params.minAICPslope)
'...'.format(aicpick.getSlope(), aicpick.getSNR()) raise PickingFailedException(error_msg)
self.vprint(msg) if aicpick.getSNR() < self.p_params.minAICPSNR:
# refilter waveform with larger bandpass error_msg = 'AIC P onset SNR to small: got {}, min {}'.format(aicpick.getSNR(), self.p_params.minAICPSNR)
tr_filt, z_copy = self.prepare_wfstream(self.zstream, freqmin=self.p_params.bpz2[0], freqmax=self.p_params.bpz2[1]) raise PickingFailedException(error_msg)
# save filtered trace in instance for later plotting
self.tr_filt_z_bpz2 = tr_filt
cuttimes2 = [round(max([aicpick.getpick() - self.p_params.Precalcwin, 0])),
round(min([len(self.ztrace.data) * self.ztrace.stats.delta,
aicpick.getpick() + self.p_params.Precalcwin]))]
if self.p_params.algoP == 'HOS':
cf2 = HOScf(z_copy, cuttimes2, self.p_params.tlta, self.p_params.hosorder)
elif self.p_params.algoP == 'ARZ':
cf2 = ARZcf(z_copy, cuttimes2, self.p_params.tpred2z, self.p_params.Parorder, self.p_params.tdet2z, self.p_params.addnoise)
else:
cf2 = None
# save cf2 for plotting
self.cf2 = cf2
# get refined onset time from CF2
assert isinstance(cf2, CharacteristicFunction), 'cf2 is not set ' \
'correctly: maybe the algorithm name ({algoP}) is ' \
'corrupted'.format(algoP=self.p_params.algoP)
fig, linecolor = get_fig_from_figdict(self.fig_dict, 'refPpick')
refPpick = PragPicker(cf2, self.p_params.tsnrz, self.p_params.pickwinP, self.iplot, self.p_params.ausP,
self.p_params.tsmoothP, aicpick.getpick(), fig, linecolor)
# save PragPicker result for plotting
self.p_results.refPpick = refPpick
self.p_results.mpickP = refPpick.getpick()
if self.p_results.mpickP is not None:
# quality assessment, get earliest/latest pick and symmetrized uncertainty
fig, linecolor = get_fig_from_figdict(self.fig_dict, 'el_Ppick')
self.p_results.epickP, self.p_results.lpickP, self.p_results.Perror = earllatepicker(z_copy, self.p_params.nfacP, self.p_params.tsnrz, self.p_results.mpickP,
self.iplot, fig=fig, linecolor=linecolor)
self.p_results.SNRP, self.p_results.SNRPdB, self.p_results.Pnoiselevel = getSNR(z_copy, self.p_params.tsnrz, self.p_results.mpickP)
# weight P-onset using symmetric error self.p_results.aicPflag = 1
self.p_results.Pweight = get_quality_class(self.p_results.Perror, self.p_params.timeerrorsP) msg = 'AIC P-pick passes quality control: Slope: {0} counts/s, ' \
if self.p_results.Pweight <= self.first_motion_params.minfmweight and self.p_results.SNRP >= self.first_motion_params.minFMSNR: 'SNR: {1}\nGo on with refined picking ...\n' \
fig, linecolor = get_fig_from_figdict(self.fig_dict, 'fm_picker') 'autopickstation: re-filtering vertical trace ' \
self.p_results.FM = fmpicker(self.zstream, z_copy, self.first_motion_params.fmpickwin, self.p_results.mpickP, self.iplot, '...'.format(aicpick.getSlope(), aicpick.getSNR())
fig, linecolor) self.vprint(msg)
else: # refilter waveform with larger bandpass
self.p_results.FM = 'N' tr_filt, z_copy = self.prepare_wfstream(self.zstream, freqmin=self.p_params.bpz2[0], freqmax=self.p_params.bpz2[1])
msg = "autopickstation: P-weight: {0}, " \ # save filtered trace in instance for later plotting
"SNR: {1}, SNR[dB]: {2}, Polarity: {3}".format(self.p_results.Pweight, self.p_results.SNRP, self.p_results.SNRPdB, self.p_results.FM) self.tr_filt_z_bpz2 = tr_filt
print(msg) starttime2 = round(max(aicpick.getpick() - self.p_params.Precalcwin, 0))
msg = 'autopickstation: Refined P-Pick: {} s | P-Error: {} s'.format(self.p_results.mpickP, self.p_results.Perror) endtime2 = round(min(len(self.ztrace.data) * self.ztrace.stats.delta, aicpick.getpick() + self.p_params.Precalcwin))
print(msg) cuttimes2 = [starttime2, endtime2]
self.s_results.Sflag = 1 if self.p_params.algoP == 'HOS':
cf2 = HOScf(z_copy, cuttimes2, self.p_params.tlta, self.p_params.hosorder)
self.p_results.aicpick = aicpick elif self.p_params.algoP == 'ARZ':
else: cf2 = ARZcf(z_copy, cuttimes2, self.p_params.tpred2z, self.p_params.Parorder, self.p_params.tdet2z, self.p_params.addnoise)
msg = 'Bad initial (AIC) P-pick, skipping this onset!\n' \
'AIC-SNR={0}, AIC-Slope={1}counts/s\n' \
'(min. AIC-SNR={2}, ' \
'min. AIC-Slope={3}counts/s)'.format(aicpick.getSNR(), aicpick.getSlope(),
self.p_params.minAICPSNR, self.p_params.minAICPslope)
self.vprint(msg)
self.s_results.Sflag = 0
else: else:
#todo why did picking fail should be saved in the pick dictionary cf2 = None
raise PickingFailedException('AIC P onset did not pass quality control') # save cf2 for plotting
self.cf2 = cf2
# get refined onset time from CF2
assert isinstance(cf2, CharacteristicFunction), 'cf2 is not set correctly: maybe the algorithm name () is ' \
'corrupted'.format(self.p_params.algoP)
fig, linecolor = get_fig_from_figdict(self.fig_dict, 'refPpick')
refPpick = PragPicker(cf2, self.p_params.tsnrz, self.p_params.pickwinP, self.iplot, self.p_params.ausP,
self.p_params.tsmoothP, aicpick.getpick(), fig, linecolor)
# save PragPicker result for plotting
self.p_results.refPpick = refPpick
self.p_results.mpickP = refPpick.getpick()
if self.p_results.mpickP is None:
msg = 'Bad initial (AIC) P-pick, skipping this onset!\n AIC-SNR={}, AIC-Slope={}counts/s\n' \
'(min. AIC-SNR={}, min. AIC-Slope={}counts/s)'
msg.format(aicpick.getSNR(), aicpick.getSlope(), self.p_params.minAICPSNR, self.p_params.minAICPslope)
self.vprint(msg)
self.s_results.Sflag = 0
raise PickingFailedException(msg)
# quality assessment, get earliest/latest pick and symmetrized uncertainty
fig, linecolor = get_fig_from_figdict(self.fig_dict, 'el_Ppick')
elpicker_results = earllatepicker(z_copy, self.p_params.nfacP, self.p_params.tsnrz, self.p_results.mpickP,
self.iplot, fig=fig, linecolor=linecolor)
self.p_results.epickP, self.p_results.lpickP, self.p_results.Perror = elpicker_results
snr_results = getSNR(z_copy, self.p_params.tsnrz, self.p_results.mpickP)
self.p_results.SNRP, self.p_results.SNRPdB, self.p_results.Pnoiselevel = snr_results
# weight P-onset using symmetric error
self.p_results.Pweight = get_quality_class(self.p_results.Perror, self.p_params.timeerrorsP)
if self.p_results.Pweight <= self.first_motion_params.minfmweight and self.p_results.SNRP >= self.first_motion_params.minFMSNR:
fig, linecolor = get_fig_from_figdict(self.fig_dict, 'fm_picker')
self.p_results.FM = fmpicker(self.zstream, z_copy, self.first_motion_params.fmpickwin,
self.p_results.mpickP, self.iplot, fig, linecolor)
msg = "autopickstation: P-weight: {}, SNR: {}, SNR[dB]: {}, Polarity: {}"
msg.format(self.p_results.Pweight, self.p_results.SNRP, self.p_results.SNRPdB, self.p_results.FM)
print(msg)
msg = 'autopickstation: Refined P-Pick: {} s | P-Error: {} s'
msg.format(self.p_results.mpickP, self.p_results.Perror)
print(msg)
self.s_results.Sflag = 1
def pick_s_phase(self): def pick_s_phase(self):