Exemplo n.º 1
0
 def get_decimal_time(self):
     '''
     Returns the time of the catalogue as a decimal
     '''
     return decimal_time(self.data['year'], self.data['month'],
                         self.data['day'], self.data['hour'],
                         self.data['minute'], self.data['second'])
Exemplo n.º 2
0
    def completeness(self, catalogue, config):
        '''Gets the completeness table

        :param catalogue:
            Earthquake catalogue as instance of
            :class: hmtk.seismicity.catalogue.Catalogue

        :param dict config:
            Configuration parameters of the algorithm, containing the
            following information -
                'magnitude_bin' Size of magnitude bin (non-negative float)
                'time_bin' Size (in dec. years) of the time window
                           (non-negative float)
                'increment_lock' Boolean to indicate whether to ensure
                           completeness magnitudes always decrease with more
                           recent bins

        :returns:
            2-column table indicating year of completeness and corresponding
            magnitude numpy.ndarray
        '''
        # If mag_bin is an array then bins are input, otherwise a single
        # parameter is input
        dyear = decimal_time(catalogue.data['year'], catalogue.data['month'],
                             catalogue.data['day'], catalogue.data['hour'],
                             catalogue.data['minute'],
                             catalogue.data['second'])
        mag = catalogue.data['magnitude']

        # Get magnitude bins
        self.magnitude_bin = self._get_magnitudes_from_spacing(
            catalogue.data['magnitude'], config['magnitude_bin'])

        # Get time bins
        _s_year, time_bin = self._get_time_limits_from_config(config, dyear)

        # Count magnitudes
        self.sigma, _counter, n_mags, n_times, self.time_values = (
            self._count_magnitudes(mag, dyear, time_bin))

        # Get completeness magnitudes
        comp_time, _gradient_2, self.model_line = (
            self.get_completeness_points(self.time_values, self.sigma, n_mags,
                                         n_times))

        # If the increment lock is selected then ensure completeness time
        # does not decrease
        if config['increment_lock']:
            for iloc in range(0, len(comp_time)):
                cond = ((iloc > 0 and (comp_time[iloc] < comp_time[iloc - 1]))
                        or np.isnan(comp_time[iloc]))
                if cond:
                    comp_time[iloc] = comp_time[iloc - 1]

        self.completeness_table = np.column_stack(
            [np.floor(self.end_year - comp_time), self.magnitude_bin[:-1]])
        return self.completeness_table
Exemplo n.º 3
0
 def get_decimal_time(self):
     '''
     Returns the time of the catalogue as a decimal
     '''
     return decimal_time(self.data['year'],
                         self.data['month'],
                         self.data['day'],
                         self.data['hour'],
                         self.data['minute'],
                         self.data['second'])
Exemplo n.º 4
0
 def test_decimal_time(self):
     '''Tests the function utils.decimal_time'''
     self.year = np.array([1990, 1995, 2000])
     self.month = np.array([1, 6, 12])
     self.day = np.array([1, 30, 31])
     self.hour = np.array([0, 12, 23])
     self.minute = np.array([0, 30, 59])
     self.second = np.array([0.0, 30.0, 59.0])
     self.assertTrue(np.allclose(
         utils.decimal_time(self.year, self.month, self.day, self.hour, 
         self.minute, self.second),
         np.array([1990., 1995.49457858, 2000.99999997])))
Exemplo n.º 5
0
 def test_decimal_time1(self):
     '''Tests the function utils.decimal_time'''
     self.year = np.array([1990])
     self.month = np.array([1])
     self.day = np.array([1])
     self.hour = np.array([0])
     self.minute = np.array([0])
     self.second = np.array([0.0, 30.0, 59.0])
     self.assertTrue(np.allclose(
         utils.decimal_time(self.year, self.month, self.day, self.hour,
         self.minute, self.second),
         np.array([1990.])))
Exemplo n.º 6
0
 def test_decimal_time(self):
     '''Tests the function utils.decimal_time'''
     self.year = np.array([1990, 1995, 2000])
     self.month = np.array([1, 6, 12])
     self.day = np.array([1, 30, 31])
     self.hour = np.array([0, 12, 23])
     self.minute = np.array([0, 30, 59])
     self.second = np.array([0.0, 30.0, 59.0])
     self.assertTrue(np.allclose(
         utils.decimal_time(self.year, self.month, self.day, self.hour,
         self.minute, self.second),
         np.array([1990., 1995.49457858, 2000.99999997])))
Exemplo n.º 7
0
 def test_decimal_time1(self):
     '''Tests the function utils.decimal_time'''
     self.year = np.array([1990])
     self.month = np.array([1])
     self.day = np.array([1])
     self.hour = np.array([0])
     self.minute = np.array([0])
     self.second = np.array([0.0, 30.0, 59.0])
     self.assertTrue(np.allclose(
         utils.decimal_time(self.year, self.month, self.day, self.hour,
         self.minute, self.second),
         np.array([1990.])))
Exemplo n.º 8
0
def compl_plot(catalogue,completeness_table,zone_id,show=False):
    '''
    Creates a completeness plot for the catalogue/subcatalogue
    '''
    import matplotlib.pyplot as plt
    import matplotlib.lines as mlines
    from hmtk.seismicity.utils import decimal_time

    dyear = decimal_time(
        catalogue.data['year'],
        catalogue.data['month'],
        catalogue.data['day'],
        catalogue.data['hour'],
        catalogue.data['minute'],
        catalogue.data['second'])
    mag = catalogue.data['magnitude']
    # add completeness line
    yc = [i[0] for i in completeness_table]
    mc = [i[1] for i in completeness_table]
    yc.sort()
    mc.sort(reverse=True)
    comp = list()
    year = numpy.linspace(yc[0],dyear[-1],10000)
    for y in year:
        tmp = 0
        for i in range(len(yc)):
            if y > yc[i]:
                tmp = mc[i]
        comp.append(tmp)

    #uncomment to plot from completeness year
    #ll = (yc[0],mc[-1])
    ll = (1900,mc[-1])
    x1 = [y for y in dyear if y >= ll[0]]
    less = len(mag)-len(x1)
    x2 = year
    y1 = mag[less:]
    y2 = comp
    event = plt.plot(x1,y1,'ob',label='Events')
    compl = plt.plot(x2,y2)
    plt.setp(compl,color = 'r',linewidth=2.0)
    #plt.title(str(zone_id))
    plt.xlim(min(x1),max(x1))
    plt.xlabel('Year')
    plt.ylabel('Mw')
    rline = mlines.Line2D([],[],color='red',label='Completeness')
    plt.legend(handles=[rline],loc='lower left')
    if show:
        plt.show()
    else:
        plt.savefig('completeness_plot.eps')
    plt.clf()
Exemplo n.º 9
0
def _get_decimal_from_datetime(time):
    '''
    As the decimal time function requires inputs in the form of numpy 
    arrays need to convert each value in the datetime object  to a single 
    numpy array
    '''

    # Get decimal seconds from seconds + microseconds
    temp_seconds = np.float(time.second) + (np.float(time.microsecond) / 1.0E6)
    return decimal_time(np.array([time.year], dtype=int),
                        np.array([time.month], dtype=int),
                        np.array([time.day], dtype=int),
                        np.array([time.hour], dtype=int),
                        np.array([time.minute], dtype=int),
                        np.array([temp_seconds], dtype=int))
Exemplo n.º 10
0
def _get_decimal_from_datetime(time):
    '''
    As the decimal time function requires inputs in the form of numpy
    arrays need to convert each value in the datetime object  to a single
    numpy array
    '''

    # Get decimal seconds from seconds + microseconds
    temp_seconds = np.float(time.second) + (np.float(time.microsecond) / 1.0E6)
    return decimal_time(np.array([time.year], dtype=int),
                        np.array([time.month], dtype=int),
                        np.array([time.day], dtype=int),
                        np.array([time.hour], dtype=int),
                        np.array([time.minute], dtype=int),
                        np.array([temp_seconds], dtype=int))
Exemplo n.º 11
0
    def test_get_decimal_time(self):
        # Tests the decimal time function. The function itself is tested in
        # tests.seismicity.utils so only minimal testing is undertaken here to
        # ensure coverage
        time_dict = {'year': np.array([1990, 2000]),
                     'month': np.array([3, 9]),
                     'day': np.ones(2, dtype=int),
                     'hour': np.ones(2, dtype=int),
                     'minute': np.ones(2, dtype=int),
                     'second': np.ones(2, dtype=float)}
        expected_dec_time = decimal_time(time_dict['year'],
                                         time_dict['month'],
                                         time_dict['day'],
                                         time_dict['hour'],
                                         time_dict['minute'],
                                         time_dict['second'])

        cat = Catalogue()
        for key in ['year', 'month', 'day', 'hour', 'minute', 'second']:
            cat.data[key] = np.copy(time_dict[key])
        np.testing.assert_array_almost_equal(expected_dec_time,
                                             cat.get_decimal_time())
Exemplo n.º 12
0
    def test_get_decimal_time(self):
        # Tests the decimal time function. The function itself is tested in
        # tests.seismicity.utils so only minimal testing is undertaken here to
        # ensure coverage
        time_dict = {
            'year': np.array([1990, 2000]),
            'month': np.array([3, 9]),
            'day': np.ones(2, dtype=int),
            'hour': np.ones(2, dtype=int),
            'minute': np.ones(2, dtype=int),
            'second': np.ones(2, dtype=float)
        }
        expected_dec_time = decimal_time(time_dict['year'], time_dict['month'],
                                         time_dict['day'], time_dict['hour'],
                                         time_dict['minute'],
                                         time_dict['second'])

        cat = Catalogue()
        for key in ['year', 'month', 'day', 'hour', 'minute', 'second']:
            cat.data[key] = np.copy(time_dict[key])
        np.testing.assert_array_almost_equal(expected_dec_time,
                                             cat.get_decimal_time())
Exemplo n.º 13
0
    def completeness(self, catalogue, config):
        '''Gets the completeness table

        :param catalogue:
            Earthquake catalogue as instance of
            :class: hmtk.seismicity.catalogue.Catalogue

        :param dict config:
            Configuration parameters of the algorithm, containing the
            following information -
                'magnitude_bin' Size of magnitude bin (non-negative float)
                'time_bin' Size (in dec. years) of the time window
                           (non-negative float)
                'increment_lock' Boolean to indicate whether to ensure
                           completeness magnitudes always decrease with more
                           recent bins

        :returns:
            2-column table indicating year of completeness and corresponding
            magnitude numpy.ndarray
        '''
        # If mag_bin is an array then bins are input, otherwise a single
        # parameter is input
        dyear = decimal_time(
            catalogue.data['year'],
            catalogue.data['month'],
            catalogue.data['day'],
            catalogue.data['hour'],
            catalogue.data['minute'],
            catalogue.data['second'])
        mag = catalogue.data['magnitude']

        # Get magnitude bins
        self.magnitude_bin = self._get_magnitudes_from_spacing(
            catalogue.data['magnitude'],
            config['magnitude_bin'])

        # Get time bins
        _s_year, time_bin = self._get_time_limits_from_config(config, dyear)

        # Count magnitudes
        self.sigma, _counter, n_mags, n_times, self.time_values = (
            self._count_magnitudes(mag, dyear, time_bin))

        # Get completeness magnitudes
        comp_time, _gradient_2, self.model_line = (
            self.get_completeness_points(self.time_values, self.sigma, n_mags,
                                         n_times))

        # If the increment lock is selected then ensure completeness time
        # does not decrease
        if config['increment_lock']:
            for iloc in range(0, len(comp_time)):
                cond = (
                    (iloc > 0 and (comp_time[iloc] < comp_time[iloc - 1])) or
                    np.isnan(comp_time[iloc]))
                if cond:
                    comp_time[iloc] = comp_time[iloc - 1]

        self.completeness_table = np.column_stack([
            np.floor(self.end_year - comp_time),
            self.magnitude_bin[:-1]])
        return self.completeness_table
Exemplo n.º 14
0
 def test_decimal_time2(self):
     '''Tests the function utils.decimal_time'''
     self.year = np.array([1990])
     self.assertTrue(np.allclose(
         utils.decimal_time(self.year, [], [], [], [], []),
         np.array([1990.])))
Exemplo n.º 15
0
 def test_decimal_time2(self):
     '''Tests the function utils.decimal_time'''
     self.year = np.array([1990])
     self.assertTrue(
         np.allclose(utils.decimal_time(self.year, [], [], [], [], []),
                     np.array([1990.])))