def test_plot_repicked(self): _picks = [pick.copy() for pick in self.event.picks] picked_channels = [] picks = [] for pick in _picks: if pick.waveform_id not in picked_channels: pick.time += 3 picks.append(pick) picked_channels.append(pick.waveform_id) fig = plot_repicked( template=self.template, picks=picks, det_stream=self.st, show=False, return_figure=True, title="test_detection") return fig
def lag_calc(detections, detect_data, template_names, templates, shift_len=0.2, min_cc=0.4, cores=1, interpolate=False, plot=False, parallel=True, debug=0): """ Main lag-calculation function for detections of specific events. Overseer function to take a list of detection objects, cut the data for them to lengths of the same length of the template + shift_len on either side. This will output a :class:`obspy.core.event.Catalog` of picked events. Pick times are based on the lag-times found at the maximum correlation, providing that correlation is above the min_cc. :type detections: list :param detections: List of :class:`eqcorrscan.core.match_filter.DETECTION` objects. :type detect_data: obspy.core.stream.Stream :param detect_data: All the data needed to cut from - can be a gappy Stream. :type template_names: list :param template_names: List of the template names, used to help identify families of events. Must be in the same order as templates. :type templates: list :param templates: List of the templates, templates should be :class:`obspy.core.stream.Stream` objects. :type shift_len: float :param shift_len: Shift length allowed for the pick in seconds, will be plus/minus this amount - default=0.2 :type min_cc: float :param min_cc: Minimum cross-correlation value to be considered a pick, default=0.4. :type cores: int :param cores: Number of cores to use in parallel processing, defaults to one. :type interpolate: bool :param interpolate: Interpolate the correlation function to achieve sub-sample precision. :type plot: bool :param plot: To generate a plot for every detection or not, defaults to False :type parallel: bool :param parallel: Turn parallel processing on or off. :type debug: int :param debug: Debug output level, 0-5 with 5 being the most output. . :returns: Catalog of events with picks. No origin information is included. These events can then be written out via :func:`obspy.core.event.Catalog.write`, or to Nordic Sfiles using :func:`eqcorrscan.utils.sfile_util.eventtosfile` and located externally. :rtype: obspy.core.event.Catalog .. note:: Picks output in catalog are generated relative to the template \ start-time. For example, if you generated your template with a \ pre_pick time of 0.2 seconds, you should expect picks generated by \ lag_calc to occur 0.2 seconds before the true phase-pick. This \ is because we do not currently store template meta-data alongside the \ templates. .. warning:: Because of the above note, origin times will be consistently \ shifted by the static pre_pick applied to the templates. .. note:: Individual channel cross-correlations are stored as a :class:`obspy.core.event.Comment` for each pick, and the summed cross-correlation value resulting from these is stored as a :class:`obspy.core.event.Comment` in the main :class:`obspy.core.event.Event` object. .. note:: The order of events is preserved (e.g. detections[n] == output[n]), providing picks have been made for that event. If no picks have been made for an event, it will not be included in the output. However, as each detection has an ID associated with it, these can be mapped to the output resource_id for each Event in the output Catalog. e.g. detections[n].id == output[m].resource_id if the output[m] is for the same event as detections[n]. """ if debug > 0: log.setLevel(logging.WARNING) ch.setLevel(logging.WARNING) if debug > 2: log.setLevel(logging.INFO) ch.setLevel(logging.INFO) if debug > 3: log.setLevel(0) ch.setLevel(0) log.addHandler(ch) if debug > 2 and plot: prep_plot = True else: prep_plot = False # First check that sample rates are equal for everything for tr in detect_data: if tr.stats.sampling_rate != detect_data[0].stats.sampling_rate: raise LagCalcError('Sampling rates are not equal') for template in templates: for tr in template: if tr.stats.sampling_rate != detect_data[0].stats.sampling_rate: raise LagCalcError('Sampling rates are not equal') # Work out the delays for each template delays = [] # List of tuples of (tempname, (sta, chan, delay)) zipped_templates = list(zip(template_names, templates)) detect_stachans = [(tr.stats.station, tr.stats.channel) for tr in detect_data] for template in zipped_templates: temp_delays = [] # Remove channels not present in continuous data _template = template[1].copy() for tr in _template: if (tr.stats.station, tr.stats.channel) not in detect_stachans: _template.remove(tr) for tr in _template: temp_delays.append( (tr.stats.station, tr.stats.channel, tr.stats.starttime - _template.sort(['starttime'])[0].stats.starttime)) delays.append((template[0], temp_delays)) del _template # Segregate detections by template, then feed to day_loop initial_cat = Catalog() for template in zipped_templates: log.info('Running lag-calc for template %s' % template[0]) template_detections = [ detection for detection in detections if detection.template_name == template[0] ] log.info('There are %i detections' % len(template_detections)) detect_streams = _prepare_data(detect_data=detect_data, detections=template_detections, zipped_templates=zipped_templates, delays=delays, shift_len=shift_len, plot=prep_plot) detect_streams = [detect_stream[1] for detect_stream in detect_streams] if len(template_detections) > 0: template_cat = _day_loop(detection_streams=detect_streams, template=template[1], min_cc=min_cc, detections=template_detections, interpolate=interpolate, cores=cores, parallel=parallel) initial_cat += template_cat if plot: for i, event in enumerate(template_cat): if len(event.picks) == 0: log.warning('Made no picks for event at time %s' % event) continue plot_stream = detect_streams[i].copy() pick_stachans = [(pick.waveform_id.station_code, pick.waveform_id.channel_code) for pick in event.picks] for tr in plot_stream: if (tr.stats.station, tr.stats.channel) \ not in pick_stachans: plot_stream.remove(tr) template_plot = template[1].copy() for tr in template_plot: if (tr.stats.station, tr.stats.channel) \ not in pick_stachans: template_plot.remove(tr) plot_repicked(template=template_plot, picks=event.picks, det_stream=plot_stream) sys.stdout.flush() # Order the catalogue to match the input output_cat = Catalog() for det in detections: event = [e for e in initial_cat if e.resource_id == det.id] if len(event) == 1: output_cat.append(event[0]) elif len(event) == 0: print('No picks made for detection:') print(det) else: raise NotImplementedError('Multiple events with same id,' ' should not happen') return output_cat
def xcorr_pick_family(family, stream, shift_len=0.2, min_cc=0.4, horizontal_chans=['E', 'N', '1', '2'], vertical_chans=['Z'], cores=1, interpolate=False, plot=False, plotdir=None): """ Compute cross-correlation picks for detections in a family. :type family: `eqcorrscan.core.match_filter.family.Family` :param family: Family to calculate correlation picks for. :type stream: `obspy.core.stream.Stream` :param stream: Data stream containing data for all (or a subset of) detections in the Family :type shift_len: float :param shift_len: Shift length allowed for the pick in seconds, will be plus/minus this amount - default=0.2 :type min_cc: float :param min_cc: Minimum cross-correlation value to be considered a pick, default=0.4. :type horizontal_chans: list :param horizontal_chans: List of channel endings for horizontal-channels, on which S-picks will be made. :type vertical_chans: list :param vertical_chans: List of channel endings for vertical-channels, on which P-picks will be made. :type cores: int :param cores: Number of cores to use in parallel processing, defaults to one. :type interpolate: bool :param interpolate: Interpolate the correlation function to achieve sub-sample precision. :type plot: bool :param plot: To generate a plot for every detection or not, defaults to False :type plotdir: str :param plotdir: Path to plotting folder, plots will be output here. :return: Dictionary of picked events keyed by detection id. """ picked_dict = {} delta = family.template.st[0].stats.delta detect_streams_dict = _prepare_data( family=family, detect_data=stream, shift_len=shift_len) detection_ids = list(detect_streams_dict.keys()) detect_streams = [detect_streams_dict[detection_id] for detection_id in detection_ids] if len(detect_streams) == 0: Logger.warning("No appropriate data found, check your family and " "detections - make sure seed ids match") return picked_dict if len(detect_streams) != len(family): Logger.warning("Not all detections have matching data. " "Proceeding anyway. HINT: Make sure SEED IDs match") # Correlation function needs a list of streams, we need to maintain order. ccc, chans = _concatenate_and_correlate( streams=detect_streams, template=family.template.st, cores=cores) for i, detection_id in enumerate(detection_ids): detection = [d for d in family.detections if d.id == detection_id][0] correlations = ccc[i] picked_chans = chans[i] detect_stream = detect_streams_dict[detection_id] checksum, cccsum, used_chans = 0.0, 0.0, 0 event = Event() for correlation, stachan in zip(correlations, picked_chans): if not stachan.used: continue tr = detect_stream.select( station=stachan.channel[0], channel=stachan.channel[1])[0] if interpolate: shift, cc_max = _xcorr_interp(correlation, dt=delta) else: cc_max = np.amax(correlation) shift = np.argmax(correlation) * delta if np.isnan(cc_max): # pragma: no cover Logger.error( 'Problematic trace, no cross correlation possible') continue picktime = tr.stats.starttime + shift checksum += cc_max used_chans += 1 if cc_max < min_cc: Logger.debug('Correlation of {0} is below threshold, not ' 'using'.format(cc_max)) continue cccsum += cc_max phase = None if stachan.channel[1][-1] in vertical_chans: phase = 'P' elif stachan.channel[1][-1] in horizontal_chans: phase = 'S' _waveform_id = WaveformStreamID(seed_string=tr.id) event.picks.append(Pick( waveform_id=_waveform_id, time=picktime, method_id=ResourceIdentifier('EQcorrscan'), phase_hint=phase, creation_info='eqcorrscan.core.lag_calc', evaluation_mode='automatic', comments=[Comment(text='cc_max={0}'.format(cc_max))])) event.resource_id = ResourceIdentifier(detection_id) event.comments.append(Comment(text="detect_val={0}".format(cccsum))) # Add template-name as comment to events event.comments.append(Comment( text="Detected using template: {0}".format(family.template.name))) if used_chans == detection.no_chans: # pragma: no cover if detection.detect_val is not None and\ checksum - detection.detect_val < -(0.3 * detection.detect_val): msg = ('lag-calc has decreased cccsum from %f to %f - ' % (detection.detect_val, checksum)) Logger.error(msg) continue else: Logger.warning( 'Cannot check if cccsum is better, used {0} channels for ' 'detection, but {1} are used here'.format( detection.no_chans, used_chans)) picked_dict.update({detection_id: event}) if plot: # pragma: no cover for i, event in enumerate(picked_dict.values()): if len(event.picks) == 0: continue plot_stream = detect_streams[i].copy() template_plot = family.template.st.copy() pick_stachans = [(pick.waveform_id.station_code, pick.waveform_id.channel_code) for pick in event.picks] for tr in plot_stream: if (tr.stats.station, tr.stats.channel) \ not in pick_stachans: plot_stream.remove(tr) for tr in template_plot: if (tr.stats.station, tr.stats.channel) \ not in pick_stachans: template_plot.remove(tr) if plotdir is not None: if not os.path.isdir(plotdir): os.makedirs(plotdir) savefile = "{plotdir}/{rid}.png".format( plotdir=plotdir, rid=event.resource_id.id) plot_repicked(template=template_plot, picks=event.picks, det_stream=plot_stream, show=False, save=True, savefile=savefile) else: plot_repicked(template=template_plot, picks=event.picks, det_stream=plot_stream, show=True) return picked_dict
def lag_calc(detections, detect_data, template_names, templates, shift_len=0.2, min_cc=0.4, cores=1, interpolate=False, plot=False): """ Main lag-calculation function for detections of specific events. Overseer function to take a list of detection objects, cut the data for them to lengths of the same length of the template + shift_len on either side. This will then write out SEISAN s-file or QuakeML for the detections with pick times based on the lag-times found at the maximum correlation, providing that correlation is above the min_cc. :type detections: list :param detections: List of DETECTION objects :type detect_data: obspy.core.stream.Stream :param detect_data: All the data needed to cut from - can be a gappy Stream :type template_names: list :param template_names: List of the template names, used to help identify \ families of events. Must be in the same order as templates. :type templates: list :param templates: List of the templates, templates are of type: \ obspy.core.stream.Stream. :type shift_len: float :param shift_len: Shift length allowed for the pick in seconds, will be plus/minus this amount - default=0.2 :type min_cc: float :param min_cc: Minimum cross-correlation value to be considered a pick, default=0.4 :type cores: int :param cores: Number of cores to use in parallel processing, defaults to \ one. :type interpolate: bool :param interpolate: Interpolate the correlation function to achieve \ sub-sample precision. :type plot: bool :param plot: To generate a plot for every detection or not, defaults to \ False. :returns: Catalog of events with picks. No origin information is \ included, these events can then be written out via \ obspy.core.event functions, or to seisan Sfiles using Sfile_util \ and located. :rtype: obspy.core.event.Catalog .. rubric: Example >>> from eqcorrscan.core import lag_calc .. note:: Picks output in catalog are generated relative to the template \ start-time. For example, if you generated your template with a \ pre_pick time of 0.2 seconds, you should expect picks generated by \ lag_calc to occur 0.2 seconds before the true phase-pick. This \ is because we do not currently store template meta-data alongside the \ templates. .. warning:: Because of the above note, origin times will be consistently \ shifted by the static pre_pick applied to the templates. """ from obspy.core.event import Catalog from eqcorrscan.utils.plotting import plot_repicked # First work out the delays for each template delays = [] # List of tuples of (tempname, (sta, chan, delay)) zipped_templates = list(zip(template_names, templates)) for template in zipped_templates: temp_delays = [] for tr in template[1]: temp_delays.append((tr.stats.station, tr.stats.channel, tr.stats.starttime - template[1]. sort(['starttime'])[0].stats.starttime)) delays.append((template[0], temp_delays)) # Segregate detections by template, then feed to day_loop initial_cat = Catalog() for template in zipped_templates: template_detections = [detection for detection in detections if detection.template_name == template[0]] detect_streams = _prepare_data(detect_data=detect_data, detections=template_detections, zipped_templates=zipped_templates, delays=delays, shift_len=shift_len) detect_streams = [detect_stream[1] for detect_stream in detect_streams] if len(template_detections) > 0: template_cat = _day_loop(detection_streams=detect_streams, template=template[1], min_cc=min_cc, interpolate=interpolate, cores=cores) initial_cat += template_cat if plot: for i, event in enumerate(template_cat): if len(event.picks) == 0: print('Made no picks for event') print(event) continue plot_repicked(template=template[1], picks=event.picks, det_stream=detect_streams[i]) return initial_cat
def lag_calc(detections, detect_data, template_names, templates, shift_len=0.2, min_cc=0.4, horizontal_chans=['E', 'N', '1', '2'], vertical_chans=['Z'], cores=1, interpolate=False, plot=False, parallel=True, debug=0): """ Main lag-calculation function for detections of specific events. Overseer function to take a list of detection objects, cut the data for them to lengths of the same length of the template + shift_len on either side. This will output a :class:`obspy.core.event.Catalog` of picked events. Pick times are based on the lag-times found at the maximum correlation, providing that correlation is above the min_cc. :type detections: list :param detections: List of :class:`eqcorrscan.core.match_filter.Detection` objects. :type detect_data: obspy.core.stream.Stream :param detect_data: All the data needed to cut from - can be a gappy Stream. :type template_names: list :param template_names: List of the template names, used to help identify families of events. Must be in the same order as templates. :type templates: list :param templates: List of the templates, templates must be a list of :class:`obspy.core.stream.Stream` objects. :type shift_len: float :param shift_len: Shift length allowed for the pick in seconds, will be plus/minus this amount - default=0.2 :type min_cc: float :param min_cc: Minimum cross-correlation value to be considered a pick, default=0.4. :type horizontal_chans: list :param horizontal_chans: List of channel endings for horizontal-channels, on which S-picks will be made. :type vertical_chans: list :param vertical_chans: List of channel endings for vertical-channels, on which P-picks will be made. :type cores: int :param cores: Number of cores to use in parallel processing, defaults to one. :type interpolate: bool :param interpolate: Interpolate the correlation function to achieve sub-sample precision. :type plot: bool :param plot: To generate a plot for every detection or not, defaults to False :type parallel: bool :param parallel: Turn parallel processing on or off. :type debug: int :param debug: Debug output level, 0-5 with 5 being the most output. :returns: Catalog of events with picks. No origin information is included. These events can then be written out via :func:`obspy.core.event.Catalog.write`, or to Nordic Sfiles using :func:`eqcorrscan.utils.sfile_util.eventtosfile` and located externally. :rtype: obspy.core.event.Catalog .. note:: Picks output in catalog are generated relative to the template start-time. For example, if you generated your template with a pre_pick time of 0.2 seconds, you should expect picks generated by lag_calc to occur 0.2 seconds before the true phase-pick. This is because we do not currently store template meta-data alongside the templates. .. warning:: Because of the above note, origin times will be consistently shifted by the static pre_pick applied to the templates. .. warning:: This routine requires only one template per channel (e.g. you should not use templates with a P and S template on a single channel). If this does occur an error will be raised. .. note:: S-picks will be made on horizontal channels, and P picks made on vertical channels - the default is that horizontal channels end in one of: 'E', 'N', '1' or '2', and that vertical channels end in 'Z'. The options vertical_chans and horizontal_chans can be changed to suit your dataset. .. note:: Individual channel cross-correlations are stored as a :class:`obspy.core.event.Comment` for each pick, and the summed cross-correlation value resulting from these is stored as a :class:`obspy.core.event.Comment` in the main :class:`obspy.core.event.Event` object. .. note:: The order of events is preserved (e.g. detections[n] == output[n]), providing picks have been made for that event. If no picks have been made for an event, it will not be included in the output. However, as each detection has an ID associated with it, these can be mapped to the output resource_id for each Event in the output Catalog. e.g. detections[n].id == output[m].resource_id if the output[m] is for the same event as detections[n]. """ if debug > 2 and plot: prep_plot = True else: prep_plot = False # First check that sample rates are equal for everything for tr in detect_data: if tr.stats.sampling_rate != detect_data[0].stats.sampling_rate: raise LagCalcError('Sampling rates are not equal') for template in templates: for tr in template: if tr.stats.sampling_rate != detect_data[0].stats.sampling_rate: raise LagCalcError('Sampling rates are not equal') # Work out the delays for each template delays = [] # List of tuples of (tempname, (sta, chan, delay)) zipped_templates = list(zip(template_names, templates)) detect_stachans = [(tr.stats.station, tr.stats.channel) for tr in detect_data] for template in zipped_templates: temp_delays = {} # Remove channels not present in continuous data _template = template[1].copy() for tr in _template: if (tr.stats.station, tr.stats.channel) not in detect_stachans: _template.remove(tr) for tr in _template: temp_delays.update({ tr.stats.station + '.' + tr.stats.channel: tr.stats.starttime - _template.sort(['starttime'])[0].stats.starttime }) delays.append((template[0], temp_delays)) del _template # Segregate detections by template, then feed to day_loop initial_cat = Catalog() for template in zipped_templates: print('Running lag-calc for template %s' % template[0]) template_detections = [ detection for detection in detections if detection.template_name == template[0] ] t_delays = [d for d in delays if d[0] == template[0]][0][1] # Check template-channels against triggered detection-channels. If the # detection was made without template-channels that would have trig- # gered earlier, then adjust the detection by that delay/earliness. delaylist = list(t_delays.items()) delaylist.sort(key=lambda tup: tup[1]) for detection in template_detections: # Find the channel with smallest delay on which the detection # triggered. Use that delay to reduce the detection-time. detection_stachans = list() for stachan in detection.chans: detection_stachans.append(stachan[0] + '.' + stachan[1]) # Find the earliest template-channel that triggered at detection earlier = 0 for delay in delaylist: delay_stachan = delay[0] if delay_stachan in detection_stachans: earlier = delay[1] break detection.detect_time = detection.detect_time - earlier if earlier > 0: print('Adjusting ' + detection.id + ' by ' + str(earlier)) debug_print('There are %i detections' % len(template_detections), 2, debug) detect_streams = _prepare_data(detect_data=detect_data, detections=template_detections, template=template, delays=t_delays, shift_len=shift_len, plot=prep_plot) detect_streams = [detect_stream[1] for detect_stream in detect_streams] if len(template_detections) > 0: template_cat = _day_loop(detection_streams=detect_streams, template=template[1], min_cc=min_cc, detections=template_detections, horizontal_chans=horizontal_chans, vertical_chans=vertical_chans, interpolate=interpolate, cores=cores, parallel=parallel, debug=debug) initial_cat += template_cat if plot: for i, event in enumerate(template_cat): if len(event.picks) == 0: continue plot_stream = detect_streams[i].copy() template_plot = template[1].copy() pick_stachans = [(pick.waveform_id.station_code, pick.waveform_id.channel_code) for pick in event.picks] for tr in plot_stream: if (tr.stats.station, tr.stats.channel) \ not in pick_stachans: plot_stream.remove(tr) for tr in template_plot: if (tr.stats.station, tr.stats.channel) \ not in pick_stachans: template_plot.remove(tr) plot_repicked(template=template_plot, picks=event.picks, det_stream=plot_stream) # Order the catalogue to match the input output_cat = Catalog() for det in detections: event = [e for e in initial_cat if str(e.resource_id) == str(det.id)] if len(event) == 1: output_cat.append(event[0]) elif len(event) == 0: print('No picks made for detection: \n%s' % det.__str__()) else: raise NotImplementedError('Multiple events with same id,' ' should not happen') return output_cat