Beispiel #1
0
 def test_recursive(self, read_header_mock):
     # For this we need to patch fnmatch as well, as the names here aren't
     # full path
     read_header_mock.return_value = None
     d_innera = {
         'a':
         create_group_mock({}, '/outout/outer_group0/testname', False),
         'b':
         create_group_mock({}, '/outout/outer_group0/different_name', False)
     }
     d_innerb = {
         'a':
         create_group_mock({}, '/outout/outer_group1/testname', False),
         'b':
         create_group_mock({}, '/outout/outer_group1/different_name', False)
     }
     d_outer = {
         'A': create_group_mock(d_innera, '/outout/outer_group0', True),
         'B': create_group_mock(d_innerb, '/outout/outer_group1', True)
     }
     g = create_group_mock(d_outer, 'outout', True)
     st = RFStream()
     st = rfh5.all_traces_recursive(g, st, '/outout/outer_group1/testname')
     self.assertEqual(st.count(), 1)
     st = rfh5.all_traces_recursive(g, st.clear(), '*')
     self.assertEqual(st.count(), 4)
Beispiel #2
0
def all_traces_recursive(group: h5py._hl.group.Group, stream: RFStream,
                         pattern: str) -> RFStream:
    """
    Recursively, appends all traces in a h5py group to the input stream.
    In addition this will check whether the data matches a certain pattern.

    :param group: group to search through
    :type group: class:`h5py._hl.group.Group`
    :param stream: Stream to append the traces to
    :type stream: CorrStream
    :param pattern: pattern for the path in the hdf5 file, see fnmatch for
        details.
    :type pattern: str
    :return: Stream with appended traces
    :rtype: CorrStream
    """
    for v in group.values():
        if isinstance(v, h5py._hl.group.Group):
            all_traces_recursive(v, stream, pattern)
        elif not fnmatch.fnmatch(v.name, pattern) and v.name not in pattern:
            continue
        else:
            # try:
            stream.append(RFTrace(np.array(v), header=read_hdf5_header(v)))
            # except ValueError:
            #     warnings.warn(
            #         'Header could not be converted. Attributes are: %s' % (
            #             str(v.attrs)))
    return stream
Beispiel #3
0
    def get_data(self,
                 network: str,
                 station: str,
                 phase: str,
                 evt_time: UTCDateTime,
                 tag: str = 'rf',
                 pol: str = 'v') -> RFStream:
        """
        Returns a :class:`~pyglimer.rf.create.RFStream` holding
        all the requested data.

        .. note::

            `Wildcards are allowed for all parameters`.

        :param network: network code, e.g., IU
        :type network: str
        :param station: station code, e.g., HRV
        :type station: str
        :param phase: Teleseismic phase
        :type phase: str
        :param evt_time: Origin Time of the Event
        :type evt_time: UTCDateTime, optional
        :param tag: Data tag (e.g., 'rf'). Defaults to rf.
        :type tag: str, optional
        :param pol: RF Polarisation. Defaults to v.
        :type pol: str, optional
        :return: a :class:`~pyglimer.rf.create.RFStream` holding the requested
            data.
        :rtype: RFStream
        """
        try:
            evt_time = UTCDateTime(evt_time)
            evt_time = evt_time.format_fissures()
        except TypeError:
            evt_time = '*'

        path = hierarchy.format(tag=tag,
                                network=network,
                                station=station,
                                phase=phase,
                                pol=pol,
                                evt_time=evt_time)
        # Extremely ugly way of changing the path
        if '*' not in path:
            data = np.array(self[path])
            header = read_hdf5_header(self[path])
            return RFStream(RFTrace(data, header=header))
        # Now, we need to differ between the fnmatch pattern and the actually
        # acessed path
        pattern = path.replace('/*', '*')
        path = path.split('*')[0]
        try:
            return all_traces_recursive(self[path], RFStream(), pattern)
        except KeyError:
            warnings.warn(
                f'Could not find data from {network}.{station} for phase ' +
                f'{phase}, tag {tag}, polarisation {pol}, and time ' +
                f'{evt_time}. Returning empty Stream.')
            return RFStream()
Beispiel #4
0
def cos_taper_st(st: Stream,
                 taper_len: float,
                 taper_at_masked: bool,
                 side: str = 'both') -> Stream:
    """
    Applies a cosine taper to the input Stream.

    :param tr: Input Stream
    :type tr: :class:`~obspy.core.stream.Stream`
    :param taper_len: Length of the taper per side
    :type taper_len: float
    :param taper_at_masked: applies a split to each trace and merges again
        afterwards
    :type taper_at_masked: bool
    :return: Tapered Stream
    :rtype: :class:`~obspy.core.stream.Stream`

    .. note::
        This action is performed in place. If you want to keep the
        original data use :func:`~obspy.core.stream.Stream.copy`.
    """
    if isinstance(st, Trace):
        st = Stream([st])
    elif isinstance(st, RFTrace):
        st = RFStream([st])
    for ii, _ in enumerate(st):
        try:
            st[ii] = cos_taper(st[ii], taper_len, taper_at_masked, side)
        except ValueError as e:
            warn('%s, corresponding trace not tapered.' % e)
    return st
Beispiel #5
0
 def test_get_coords_warning(self, file_mock, get_data_mock):
     get_data_mock.return_value = RFStream([self.rftr])
     d_inner = {
         'bla':
         create_group_mock(
             {}, '/rf/%s/%s/P/v' %
             (self.rftr.stats.network, self.rftr.stats.station), True)
     }
     d_middle = {
         'v':
         create_group_mock(
             d_inner, '/rf/%s/%s/P' %
             (self.rftr.stats.network, self.rftr.stats.station), True)
     }
     d_outer = {
         'P':
         create_group_mock(
             d_middle, '/rf/%s/%s' %
             (self.rftr.stats.network, self.rftr.stats.station), True)
     }
     d_oo = {
         'rf': {
             self.rftr.stats.network: {
                 self.rftr.stats.station: d_outer
             }
         }
     }
     file_mock.side_effect = d_oo.__getitem__
     with warnings.catch_warnings(record=True) as w:
         x = self.dbh.get_coords('bla', self.rftr.stats.station)
         self.assertTupleEqual(x, (None, None, None))
         self.assertEqual(len(w), 1)
Beispiel #6
0
 def test_get_coords(self, file_mock, get_data_mock):
     get_data_mock.return_value = RFStream([self.rftr])
     d_inner = {
         'bla':
         create_group_mock(
             {}, '/rf/%s/%s/P/v' %
             (self.rftr.stats.network, self.rftr.stats.station), True)
     }
     d_middle = {
         'v':
         create_group_mock(
             d_inner, '/rf/%s/%s/P' %
             (self.rftr.stats.network, self.rftr.stats.station), True)
     }
     d_outer = {
         'P':
         create_group_mock(
             d_middle, '/rf/%s/%s' %
             (self.rftr.stats.network, self.rftr.stats.station), True)
     }
     d_oo = {
         'rf': {
             self.rftr.stats.network: {
                 self.rftr.stats.station: d_outer
             }
         }
     }
     file_mock.side_effect = d_oo.__getitem__
     exp_result = (15, -55, 355)
     self.assertTupleEqual(
         exp_result,
         self.dbh.get_coords(self.rftr.stats.network,
                             self.rftr.stats.station))
Beispiel #7
0
    def test_moveout_vs_XY(self):
        stream = RFStream(read())[:1]
        for tr in stream:
            tr.stats.slowness = 10.
            tr.stats.onset = tr.stats.starttime + 20.643
            tr.stats.phase = 'P'
            tr.stats.type = 'time'
            tr.stats.station_elevation = 0
            tr.stats.station_longitude = 0
            tr.stats.station_latitude = 0
        stream.decimate(10)
        N = len(stream[0])
        t = np.linspace(0, 20 * np.pi, N)
        stream[0].data = np.sin(t) * np.exp(-0.04 * t)
        stream[0].stats.slowness = 4.0
        stream1 = stream.copy()
        stream3 = stream.copy()
        stream3[0].stats.slowness = 9.0
        stream9 = stream.copy()
        stream10 = stream.copy()

        stream1.moveout('iasp91.dat')
        stream3.moveout('iasp91.dat')

        np.testing.assert_array_almost_equal(stream9[0].data,
                                             stream10[0].data,
                                             decimal=2)
Beispiel #8
0
    def test_is_np_array(self, read_header_mock):
        read_header_mock.return_value = None
        d = {
            'a': create_group_mock({}, '/outer_group/testname', False),
            'b': create_group_mock({}, '/outer_group/different_name', False)
        }

        g = create_group_mock(d, '/outer_group', True)
        st = RFStream()
        st = rfh5.all_traces_recursive(g, st, '/outer_group/testname')
        self.assertEqual(st.count(), 1)
        st = rfh5.all_traces_recursive(g, st.clear(),
                                       '/outer_group/different_name')
        self.assertEqual(st.count(), 1)
        st = rfh5.all_traces_recursive(g, st.clear(), '*name')
        self.assertEqual(st.count(), 2)
        st = rfh5.all_traces_recursive(g, st.clear(), 'no_match')
        self.assertEqual(st.count(), 0)
Beispiel #9
0
    def test_get_data_wildcard2(self, file_mock, all_tr_recursive_mock):
        all_tr_recursive_mock.return_value = None
        net = 'AB'
        stat = '*'
        phase = '*'
        tag = 'rand'
        evt_time = '*'
        pol = '*'
        exp_path = rfh5.hierarchy.format(tag=tag,
                                         network=net,
                                         station=stat,
                                         phase=phase,
                                         pol=pol,
                                         evt_time=evt_time)
        exp_path = '/'.join(exp_path.split('/')[:-4])
        d = {exp_path: self.rftr.data, '/rand/AB/': self.rftr.data}
        file_mock.side_effect = d.__getitem__

        _ = self.dbh.get_data(net, stat, phase, evt_time, tag=tag, pol=pol)
        file_mock.assert_called_with('/rand/AB/')
        all_tr_recursive_mock.assert_called_with(d['/rand/AB/'], RFStream(),
                                                 '/rand/AB****')
Beispiel #10
0
    def test_get_data_wildcard(self, file_mock, all_tr_recursive_mock):
        all_tr_recursive_mock.return_value = None
        net = 'AB'
        stat = '*'
        tag = 'rand'
        phase = 'P'
        pol = 'v'
        evt_time = UTCDateTime(0)
        exp_path = rfh5.hierarchy.format(tag=tag,
                                         network=net,
                                         station=stat,
                                         phase=phase,
                                         pol=pol,
                                         evt_time=evt_time.format_fissures())
        d = {exp_path: self.rftr.data, '/rand/AB/': self.rftr.data}
        file_mock.side_effect = d.__getitem__

        _ = self.dbh.get_data(net, stat, phase, evt_time, tag=tag, pol=pol)
        file_mock.assert_called_with('/rand/AB/')
        all_tr_recursive_mock.assert_called_with(
            d['/rand/AB/'], RFStream(),
            '/rand/AB*/%s/%s/%s' % (phase, pol, evt_time.format_fissures()))