Ejemplo n.º 1
0
 def test_xcorr_pick_family_export_cc(self):
     cc_dir = 'cc_exported'
     xcorr_pick_family(family=self.party[0],
                       stream=self.data,
                       shift_len=0.2,
                       plot=False,
                       interpolate=False,
                       export_cc=True,
                       cc_dir=cc_dir)
     cc_files = glob.glob(os.path.join(cc_dir, '*.npy'))
     assert len(cc_files) == len(self.party[0])
     for fcc in cc_files:
         np.load(fcc)
Ejemplo n.º 2
0
 def test_family_picking_missing_data(self):
     """ Check that this all works okay when one channel has some gaps """
     gappy_data = self.data.copy()
     sr = gappy_data[0].stats.sampling_rate
     mask = np.zeros(len(gappy_data[0].data), dtype=bool)
     gap_start = self.party[0][5].detect_time - 5
     gap_length = 3600
     gap_end = gap_start + gap_length
     gap_start = int(sr * (gap_start - UTCDateTime(0)))
     gap_end = int(sr * (gap_end - UTCDateTime(0)))
     mask[gap_start:gap_end] = np.ones(int(gap_length * sr), dtype=bool)
     gappy_data[0].data = np.ma.masked_array(data=gappy_data[0].data,
                                             mask=mask)
     catalog_dict = xcorr_pick_family(family=self.party[0],
                                      stream=gappy_data,
                                      shift_len=0.2,
                                      plot=False)
     gap = gappy_data.split().get_gaps()
     for event in catalog_dict.values():
         if len(event.picks) != len(self.data):
             self.assertEqual(len(event.picks), len(self.data) - 1)
             # Check that the event happened to be in the gap
             self.assertTrue(gap[0][4] <= event.picks[0].time <= gap[0][5])
             # Check that there isn't a pick on the channel missing data
             self.assertNotIn('.'.join(gap[0][0:4]), [
                 pick.waveform_id.get_seed_string() for pick in event.picks
             ])
         for pick in event.picks:
             self.assertTrue("cc_max=" in pick.comments[0].text)
             self.assertAlmostEqual(
                 float(pick.comments[0].text.split("=")[-1]), 1.0, 1)
Ejemplo n.º 3
0
 def test_family_picking_with_interpolation(self):
     catalog_dict = xcorr_pick_family(family=self.party[0],
                                      stream=self.data,
                                      shift_len=0.2,
                                      plot=False,
                                      interpolate=True)
     for event in catalog_dict.values():
         for pick in event.picks:
             self.assertTrue("cc_max=" in pick.comments[0].text)
             self.assertAlmostEqual(
                 float(pick.comments[0].text.split("=")[-1]), 1.0, 1)
Ejemplo n.º 4
0
 def test_family_picking(self):
     catalog_dict = xcorr_pick_family(family=self.party[0],
                                      stream=self.data,
                                      shift_len=0.2,
                                      plot=False,
                                      export_cc=False)
     for event in catalog_dict.values():
         self.assertEqual(len(event.picks), len(self.data))
         for pick in event.picks:
             self.assertTrue("cc_max=" in pick.comments[0].text)
             self.assertAlmostEqual(
                 float(pick.comments[0].text.split("=")[-1]), 1.0, 1)
Ejemplo n.º 5
0
    def lag_calc(self,
                 stream,
                 pre_processed,
                 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,
                 parallel=True,
                 process_cores=None,
                 ignore_bad_data=False,
                 relative_magnitudes=False,
                 **kwargs):
        """
        Compute picks based on cross-correlation alignment.

        Works in place on events in the Family

        :type stream: obspy.core.stream.Stream
        :param stream:
            All the data needed to cut from - can be a gappy Stream.
        :type pre_processed: bool
        :param pre_processed:
            Whether the stream has been pre-processed or not to match the
            templates. See note below.
        :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:
            The path to save plots to. If `plotdir=None` (default) then the
            figure will be shown on screen.
        :type parallel: bool
        :param parallel: Turn parallel processing on or off.
        :type process_cores: int
        :param process_cores:
            Number of processes to use for pre-processing (if different to
            `cores`).
        :type ignore_bad_data: bool
        :param ignore_bad_data:
            If False (default), errors will be raised if data are excessively
            gappy or are mostly zeros. If True then no error will be raised,
            but an empty trace will be returned (and not used in detection).
        :type relative_magnitudes: bool
        :param relative_magnitudes:
            Whether to calculate relative magnitudes or not. See
            :func:`eqcorrscan.utils.mag_calc.relative_magnitude` for more
            information. Keyword arguments `noise_window`, `signal_window` and
            `min_snr` can be passed as additional keyword arguments to pass
            through to `eqcorrscan.utils.mag_calc.relative_magnitude`.

        :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::
            Note on pre-processing: You can provide a pre-processed stream,
            which may be beneficial for detections over large time periods
            (the stream can have gaps, which reduces memory usage).  However,
            in this case the processing steps are not checked, so you must
            ensure that the template in the Family has the same sampling
            rate and filtering as the stream.
            If pre-processing has not be done then the data will be processed
            according to the parameters in the template.

        .. Note::
            Picks are corrected for the template pre-pick time.
        """
        from eqcorrscan.core.lag_calc import xcorr_pick_family

        processed_stream = self._process_streams(
            stream=stream,
            pre_processed=pre_processed,
            process_cores=process_cores,
            parallel=parallel,
            ignore_bad_data=ignore_bad_data)
        picked_dict = xcorr_pick_family(family=self,
                                        stream=processed_stream,
                                        shift_len=shift_len,
                                        min_cc=min_cc,
                                        horizontal_chans=horizontal_chans,
                                        vertical_chans=vertical_chans,
                                        cores=cores,
                                        interpolate=interpolate,
                                        plot=plot,
                                        plotdir=plotdir)
        for detection_id, event in picked_dict.items():
            for pick in event.picks:
                pick.time += self.template.prepick
            d = [d for d in self.detections if d.id == detection_id][0]
            d.event.picks = event.picks
        if relative_magnitudes:
            self.relative_magnitudes(stream=processed_stream,
                                     pre_processed=True,
                                     min_cc=min_cc,
                                     **kwargs)
        return self.catalog
Ejemplo n.º 6
0
    def lag_calc(self,
                 stream,
                 pre_processed,
                 shift_len=0.2,
                 min_cc=0.4,
                 min_cc_from_mean_cc_factor=None,
                 horizontal_chans=['E', 'N', '1', '2'],
                 vertical_chans=['Z'],
                 cores=1,
                 interpolate=False,
                 plot=False,
                 plotdir=None,
                 parallel=True,
                 process_cores=None,
                 ignore_length=False,
                 ignore_bad_data=False,
                 export_cc=False,
                 cc_dir=None,
                 **kwargs):
        """
        Compute picks based on cross-correlation alignment.

        Works in place on events in the Family

        :type stream: obspy.core.stream.Stream
        :param stream:
            All the data needed to cut from - can be a gappy Stream.
        :type pre_processed: bool
        :param pre_processed:
            Whether the stream has been pre-processed or not to match the
            templates. See note below.
        :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 min_cc_from_mean_cc_factor: float
        :param min_cc_from_mean_cc_factor:
            If set to a value other than None, then the minimum cross-
            correlation value for a trace is set individually for each
            detection based on:
            min(detect_val / n_chans * min_cc_from_mean_cc_factor, min_cc).
        :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:
            The path to save plots to. If `plotdir=None` (default) then the
            figure will be shown on screen.
        :type parallel: bool
        :param parallel: Turn parallel processing on or off.
        :type process_cores: int
        :param process_cores:
            Number of processes to use for pre-processing (if different to
            `cores`).
        :type ignore_length: bool
        :param ignore_length:
            If using daylong=True, then dayproc will try check that the data
            are there for at least 80% of the day, if you don't want this check
            (which will raise an error if too much data are missing) then set
            ignore_length=True.  This is not recommended!
        :type ignore_bad_data: bool
        :param ignore_bad_data:
            If False (default), errors will be raised if data are excessively
            gappy or are mostly zeros. If True then no error will be raised,
            but an empty trace will be returned (and not used in detection).
        :type export_cc: bool
        :param export_cc:
            To generate a binary file in NumPy for every detection or not,
            defaults to False
        :type cc_dir: str
        :param cc_dir:
            Path to saving folder, NumPy files will be output here.

        :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::
            Note on pre-processing: You can provide a pre-processed stream,
            which may be beneficial for detections over large time periods
            (the stream can have gaps, which reduces memory usage).  However,
            in this case the processing steps are not checked, so you must
            ensure that the template in the Family has the same sampling
            rate and filtering as the stream.
            If pre-processing has not be done then the data will be processed
            according to the parameters in the template.

        .. Note::
            Picks are corrected for the template pre-pick time.
        """
        from eqcorrscan.core.lag_calc import xcorr_pick_family

        processed_stream = self._process_streams(
            stream=stream,
            pre_processed=pre_processed,
            process_cores=process_cores,
            parallel=parallel,
            ignore_bad_data=ignore_bad_data,
            ignore_length=ignore_length)
        picked_dict = xcorr_pick_family(
            family=self,
            stream=processed_stream,
            shift_len=shift_len,
            min_cc=min_cc,
            horizontal_chans=horizontal_chans,
            min_cc_from_mean_cc_factor=min_cc_from_mean_cc_factor,
            vertical_chans=vertical_chans,
            cores=cores,
            interpolate=interpolate,
            plot=plot,
            plotdir=plotdir,
            export_cc=export_cc,
            cc_dir=cc_dir)
        catalog_out = Catalog([ev for ev in picked_dict.values()])
        for detection_id, event in picked_dict.items():
            for pick in event.picks:
                pick.time += self.template.prepick
            d = [d for d in self.detections if d.id == detection_id][0]
            d.event.picks = event.picks
        # TODO: reinstate this is relative magnitudes becomes viable.
        # if relative_magnitudes:
        #     self.relative_magnitudes(
        #         stream=processed_stream, pre_processed=True, min_cc=min_cc,
        #         **kwargs)
        #     return self.catalog
        return catalog_out