def test_catalogue_selection(self): # Tests the tools for selecting events within the catalogue self.catalogue.data['longitude'] = np.arange(1., 6., 1.) self.catalogue.data['latitude'] = np.arange(6., 11., 1.) self.catalogue.data['depth'] = np.ones(5, dtype=bool) # No events selected flag_none = np.zeros(5, dtype=bool) selector0 = CatalogueSelector(self.catalogue) test_cat1 = selector0.select_catalogue(flag_none) self.assertEqual(len(test_cat1.data['longitude']), 0) self.assertEqual(len(test_cat1.data['latitude']), 0) self.assertEqual(len(test_cat1.data['depth']), 0) # All events selected flag_all = np.ones(5, dtype=bool) test_cat1 = selector0.select_catalogue(flag_all) self.assertTrue( np.allclose(test_cat1.data['longitude'], self.catalogue.data['longitude'])) self.assertTrue( np.allclose(test_cat1.data['latitude'], self.catalogue.data['latitude'])) self.assertTrue( np.allclose(test_cat1.data['depth'], self.catalogue.data['depth'])) # Some events selected flag_1 = np.array([True, False, True, False, True]) test_cat1 = selector0.select_catalogue(flag_1) self.assertTrue( np.allclose(test_cat1.data['longitude'], np.array([1., 3., 5.]))) self.assertTrue( np.allclose(test_cat1.data['latitude'], np.array([6., 8., 10]))) self.assertTrue( np.allclose(test_cat1.data['depth'], np.array([1., 1., 1.])))
def test_catalogue_selection(self): # Tests the tools for selecting events within the catalogue self.catalogue.data['longitude'] = np.arange(1.,6., 1.) self.catalogue.data['latitude'] = np.arange(6., 11., 1.) self.catalogue.data['depth'] = np.ones(5, dtype=bool) # No events selected flag_none = np.zeros(5, dtype=bool) selector0 = CatalogueSelector(self.catalogue) test_cat1 = selector0.select_catalogue(flag_none) self.assertEqual(len(test_cat1.data['longitude']), 0) self.assertEqual(len(test_cat1.data['latitude']), 0) self.assertEqual(len(test_cat1.data['depth']), 0) # All events selected flag_all = np.ones(5, dtype=bool) test_cat1 = selector0.select_catalogue(flag_all) self.assertTrue(np.allclose(test_cat1.data['longitude'], self.catalogue.data['longitude'])) self.assertTrue(np.allclose(test_cat1.data['latitude'], self.catalogue.data['latitude'])) self.assertTrue(np.allclose(test_cat1.data['depth'], self.catalogue.data['depth'])) # Some events selected flag_1 = np.array([True, False, True, False, True]) test_cat1 = selector0.select_catalogue(flag_1) self.assertTrue(np.allclose(test_cat1.data['longitude'], np.array([1., 3., 5.]))) self.assertTrue(np.allclose(test_cat1.data['latitude'], np.array([6., 8., 10]))) self.assertTrue(np.allclose(test_cat1.data['depth'], np.array([1., 1., 1.])))
def test_select_within_depth_range(self): ''' Tests the function to select within the depth range ''' # Setup function self.catalogue = Catalogue() self.catalogue.data['depth'] = np.array([5., 15., 25., 35., 45.]) selector0 = CatalogueSelector(self.catalogue) # Test case 1: No limits specified - all catalogue valid test_cat_1 = selector0.within_depth_range() np.testing.assert_array_almost_equal(test_cat_1.data['depth'], self.catalogue.data['depth']) # Test case 2: Lower depth limit specfied only test_cat_1 = selector0.within_depth_range(lower_depth=30.) np.testing.assert_array_almost_equal(test_cat_1.data['depth'], np.array([5., 15., 25.])) # Test case 3: Upper depth limit specified only test_cat_1 = selector0.within_depth_range(upper_depth=20.) np.testing.assert_array_almost_equal(test_cat_1.data['depth'], np.array([25., 35., 45.])) # Test case 4: Both depth limits specified test_cat_1 = selector0.within_depth_range(upper_depth=20., lower_depth=40.) np.testing.assert_array_almost_equal(test_cat_1.data['depth'], np.array([25., 35.]))
def test_point_in_circular_distance(self): """ Tests point in circular distance """ self.catalogue.data["longitude"] = np.arange(4.0, 7.5, 0.5) self.catalogue.data["latitude"] = np.arange(4.0, 7.5, 0.5) self.catalogue.data["depth"] = np.ones(7, dtype=float) test_point = Point(5.5, 5.5) test_mesh = self.catalogue.hypocentres_as_mesh() selector0 = CatalogueSelector(self.catalogue) # Within 10 km test_cat_10 = selector0.circular_distance_from_point(test_point, 10.0, distance_type="epicentral") np.testing.assert_array_equal(test_cat_10.data["longitude"], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data["latitude"], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data["depth"], np.array([1.0])) # Within 100 km test_cat_100 = selector0.circular_distance_from_point(test_point, 100.0, distance_type="epicentral") np.testing.assert_array_equal(test_cat_100.data["longitude"], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_equal(test_cat_100.data["latitude"], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_equal(test_cat_100.data["depth"], np.array([1.0, 1.0, 1.0])) # Within 1000 km test_cat_1000 = selector0.circular_distance_from_point(test_point, 1000.0, distance_type="epicentral") np.testing.assert_array_equal(test_cat_1000.data["longitude"], self.catalogue.data["longitude"]) np.testing.assert_array_equal(test_cat_1000.data["latitude"], self.catalogue.data["latitude"]) np.testing.assert_array_equal(test_cat_1000.data["depth"], self.catalogue.data["depth"])
def test_select_within_polygon(self): ''' Tests the selection of points within polygon ''' # Setup polygon nodes = np.array([[5.0, 6.0], [6.0, 6.0], [6.0, 5.0], [5.0, 5.0]]) polygon0 = Polygon([Point(nodes[iloc, 0], nodes[iloc, 1]) for iloc in range(0, 4)]) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple case with nodes inside, outside and on the border of polygon selector0 = CatalogueSelector(self.catalogue) test_cat1 = selector0.within_polygon(polygon0) self.assertTrue(np.allclose(test_cat1.data['longitude'], np.array([5.0, 5.5, 6.0]))) self.assertTrue(np.allclose(test_cat1.data['latitude'], np.array([5.0, 5.5, 6.0]))) self.assertTrue(np.allclose(test_cat1.data['depth'], np.array([1.0, 1.0, 1.0]))) # CASE 2: As case 1 with one of the inside nodes outside of the depths self.catalogue.data['depth'] = \ np.array([1.0, 1.0, 1.0, 50.0, 1.0, 1.0, 1.0], dtype=float) selector0 = CatalogueSelector(self.catalogue) test_cat1 = selector0.within_polygon(polygon0, upper_depth=0.0, lower_depth=10.0) self.assertTrue(np.allclose(test_cat1.data['longitude'], np.array([5.0, 6.0]))) self.assertTrue(np.allclose(test_cat1.data['latitude'], np.array([5.0, 6.0]))) self.assertTrue(np.allclose(test_cat1.data['depth'], np.array([1.0])))
def test_within_rupture_distance(self): """ Tests the function to select within Joyner-Boore distance """ self.catalogue.data["longitude"] = np.arange(4.0, 7.5, 0.5) self.catalogue.data["latitude"] = np.arange(4.0, 7.5, 0.5) self.catalogue.data["depth"] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Construct Fault trace0 = np.array([[5.5, 6.0], [5.5, 5.0]]) fault_trace = Line([Point(trace0[i, 0], trace0[i, 1]) for i in range(0, 2)]) # Simple fault with vertical dip fault0 = SimpleFaultSurface.from_fault_data(fault_trace, 0.0, 20.0, 90.0, 1.0) # Within 100 km test_cat_100 = selector0.within_rupture_distance(fault0, 100.0) np.testing.assert_array_almost_equal(test_cat_100.data["longitude"], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data["latitude"], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data["depth"], np.array([1.0, 1.0, 1.0])) # Simple fault with 30 degree dip fault0 = SimpleFaultSurface.from_fault_data(fault_trace, 0.0, 20.0, 30.0, 1.0) # Within 100 km test_cat_100 = selector0.within_rupture_distance(fault0, 100.0) np.testing.assert_array_almost_equal(test_cat_100.data["longitude"], np.array([4.5, 5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data["latitude"], np.array([4.5, 5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data["depth"], np.array([1.0, 1.0, 1.0, 1.0]))
def test_cartesian_square_on_point(self): """ Tests the cartesian square centres on point """ self.catalogue.data["longitude"] = np.arange(4.0, 7.5, 0.5) self.catalogue.data["latitude"] = np.arange(4.0, 7.5, 0.5) self.catalogue.data["depth"] = np.ones(7, dtype=float) test_point = Point(5.5, 5.5) test_mesh = self.catalogue.hypocentres_as_mesh() selector0 = CatalogueSelector(self.catalogue) # Within 10 km test_cat_10 = selector0.cartesian_square_centred_on_point(test_point, 10.0, distance_type="epicentral") np.testing.assert_array_equal(test_cat_10.data["longitude"], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data["latitude"], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data["depth"], np.array([1.0])) # Within 100 km test_cat_100 = selector0.cartesian_square_centred_on_point(test_point, 100.0, distance_type="epicentral") np.testing.assert_array_almost_equal(test_cat_100.data["longitude"], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data["latitude"], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data["depth"], np.array([1.0, 1.0, 1.0])) # Within 1000 km test_cat_1000 = selector0.cartesian_square_centred_on_point(test_point, 1000.0, distance_type="epicentral") np.testing.assert_array_almost_equal(test_cat_1000.data["longitude"], self.catalogue.data["longitude"]) np.testing.assert_array_almost_equal(test_cat_1000.data["latitude"], self.catalogue.data["latitude"]) np.testing.assert_array_almost_equal(test_cat_1000.data["depth"], self.catalogue.data["depth"])
def test_select_within_distance(self): ''' Tests the selection of earthquakes within distance of fault ''' # Create fault self.fault_source = mtkComplexFaultSource('101', 'A complex fault') # Test case when input as list of nhlib.geo.line.Line self.fault_source.create_geometry(self.trace_line, mesh_spacing=2.0) self.assertIsInstance(self.fault_source.geometry, ComplexFaultSurface) # Create simple catalogue self.catalogue.data['longitude'] = np.arange(0., 4.1, 0.1) self.catalogue.data['latitude'] = np.arange(0., 4.1, 0.1) self.catalogue.data['depth'] = np.ones(41, dtype=float) self.catalogue.data['eventID'] = np.arange(0, 41, 1) selector0 = CatalogueSelector(self.catalogue) # Test when considering Joyner-Boore distance self.fault_source.select_catalogue(selector0, 50.) np.testing.assert_array_equal( self.fault_source.catalogue.data['eventID'], np.arange(2, 14, 1)) # Test when considering rupture distance self.fault_source.select_catalogue(selector0, 50., 'rupture') np.testing.assert_array_equal( self.fault_source.catalogue.data['eventID'], np.arange(2, 12, 1)) # The usual test to ensure error is raised when no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.fault_source.select_catalogue(selector0, 40.0) self.assertEqual(ver.exception.message, 'No events found in catalogue!')
def test_select_within_magnitude_range(self): ''' Tests the function to select within the magnitude range ''' # Setup function self.catalogue = Catalogue() self.catalogue.data['magnitude'] = np.array([4., 5., 6., 7., 8.]) selector0 = CatalogueSelector(self.catalogue) # Test case 1: No limits specified - all catalogue valid test_cat_1 = selector0.within_magnitude_range() np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], self.catalogue.data['magnitude']) # Test case 2: Lower depth limit specfied only test_cat_1 = selector0.within_magnitude_range(lower_mag=5.5) np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], np.array([6., 7., 8.])) # Test case 3: Upper depth limit specified only test_cat_1 = selector0.within_magnitude_range(upper_mag=5.5) np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], np.array([4., 5.])) # Test case 4: Both depth limits specified test_cat_1 = selector0.within_magnitude_range(upper_mag=7.5, lower_mag=5.5) np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], np.array([6., 7.]))
def test_select_within_time(self): """ Tests the function to select within a time period """ self.catalogue.data["year"] = np.arange(1900, 2010, 20) self.catalogue.data["month"] = np.arange(1, 12, 2) self.catalogue.data["day"] = np.ones(6, dtype=int) self.catalogue.data["hour"] = np.ones(6, dtype=int) self.catalogue.data["minute"] = np.zeros(6, dtype=int) self.catalogue.data["second"] = np.ones(6, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Start time and End time not defined test_cat_1 = selector0.within_time_period() self._compare_time_data_dictionaries(test_cat_1.data, self.catalogue.data) # Start time defined - end time not defined begin_time = datetime.datetime(1975, 1, 1, 0, 0, 0, 0) expected_data = { "year": np.array([1980, 2000]), "month": np.array([9, 11]), "day": np.array([1, 1]), "hour": np.array([1, 1]), "minute": np.array([0, 0]), "second": np.array([1.0, 1.0]), } test_cat_1 = selector0.within_time_period(start_time=begin_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data) # Test 3 - Start time not defined, end-time defined finish_time = datetime.datetime(1965, 1, 1, 0, 0, 0, 0) expected_data = { "year": np.array([1900, 1920, 1940, 1960]), "month": np.array([1, 3, 5, 7]), "day": np.array([1, 1, 1, 1]), "hour": np.array([1, 1, 1, 1]), "minute": np.array([0, 0, 0, 0]), "second": np.array([1.0, 1.0, 1.0, 1.0]), } test_cat_1 = selector0.within_time_period(end_time=finish_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data) # Test 4 - both start time and end-time defined begin_time = datetime.datetime(1935, 1, 1, 0, 0, 0, 0) finish_time = datetime.datetime(1995, 1, 1, 0, 0, 0, 0) expected_data = { "year": np.array([1940, 1960, 1980]), "month": np.array([5, 7, 9]), "day": np.array([1, 1, 1]), "hour": np.array([1, 1, 1]), "minute": np.array([0, 0, 0]), "second": np.array([1.0, 1.0, 1.0]), } test_cat_1 = selector0.within_time_period(begin_time, finish_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data)
def test_select_within_fault_distance(self): ''' Tests the selection of events within a distance from the fault ''' # Set up catalouge self.catalogue = Catalogue() self.catalogue.data['longitude'] = np.arange(0., 5.5, 0.5) self.catalogue.data['latitude'] = np.arange(0., 5.5, 0.5) self.catalogue.data['depth'] = np.zeros(11, dtype=float) self.catalogue.data['eventID'] = np.arange(0, 11, 1) self.fault_source = mtkSimpleFaultSource('101', 'A simple fault') trace_as_line = line.Line( [point.Point(2.0, 3.0), point.Point(3.0, 2.0)]) self.fault_source.create_geometry(trace_as_line, 30., 0., 30.) selector0 = CatalogueSelector(self.catalogue) # Test 1 - simple case Joyner-Boore distance self.fault_source.select_catalogue(selector0, 40.) np.testing.assert_array_almost_equal( np.array([2., 2.5]), self.fault_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([2., 2.5]), self.fault_source.catalogue.data['latitude']) # Test 2 - simple case Rupture distance self.fault_source.catalogue = None self.fault_source.select_catalogue(selector0, 40., 'rupture') np.testing.assert_array_almost_equal( np.array([2.5]), self.fault_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([2.5]), self.fault_source.catalogue.data['latitude']) # Test 3 - for vertical fault ensure that Joyner-Boore distance # behaviour is the same as for rupture distance fault1 = mtkSimpleFaultSource('102', 'A vertical fault') fault1.create_geometry(trace_as_line, 90., 0., 30.) self.fault_source.create_geometry(trace_as_line, 90., 0., 30.) # Joyner-Boore self.fault_source.select_catalogue(selector0, 40.) # Rupture fault1.select_catalogue(selector0, 40., 'rupture') np.testing.assert_array_almost_equal( self.fault_source.catalogue.data['longitude'], fault1.catalogue.data['longitude']) np.testing.assert_array_almost_equal( self.fault_source.catalogue.data['latitude'], fault1.catalogue.data['latitude']) # The usual test to ensure error is raised when no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.fault_source.select_catalogue(selector0, 40.0) self.assertEqual(ver.exception.message, 'No events found in catalogue!')
def test_select_within_time(self): ''' Tests the function to select within a time period ''' self.catalogue.data['year'] = np.arange(1900, 2010, 20) self.catalogue.data['month'] = np.arange(1, 12, 2) self.catalogue.data['day'] = np.ones(6, dtype=int) self.catalogue.data['hour'] = np.ones(6, dtype=int) self.catalogue.data['minute'] = np.zeros(6, dtype=int) self.catalogue.data['second'] = np.ones(6, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Start time and End time not defined test_cat_1 = selector0.within_time_period() self._compare_time_data_dictionaries(test_cat_1.data, self.catalogue.data) # Start time defined - end time not defined begin_time = datetime.datetime(1975, 1, 1, 0, 0, 0, 0) expected_data = {'year': np.array([1980, 2000]), 'month': np.array([9, 11]), 'day': np.array([1, 1]), 'hour': np.array([1, 1]), 'minute': np.array([0, 0]), 'second': np.array([1., 1.])} test_cat_1 = selector0.within_time_period(start_time=begin_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data) # Test 3 - Start time not defined, end-time defined finish_time = datetime.datetime(1965, 1, 1, 0, 0, 0, 0) expected_data = {'year': np.array([1900, 1920, 1940, 1960]), 'month': np.array([1, 3, 5, 7]), 'day': np.array([1, 1, 1, 1]), 'hour': np.array([1, 1, 1, 1]), 'minute': np.array([0, 0, 0, 0]), 'second': np.array([1., 1., 1., 1.])} test_cat_1 = selector0.within_time_period(end_time=finish_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data) # Test 4 - both start time and end-time defined begin_time = datetime.datetime(1935, 1, 1, 0, 0, 0, 0) finish_time = datetime.datetime(1995, 1, 1, 0, 0, 0, 0) expected_data = {'year': np.array([1940, 1960, 1980]), 'month': np.array([5, 7, 9]), 'day': np.array([1, 1, 1]), 'hour': np.array([1, 1, 1]), 'minute': np.array([0, 0, 0]), 'second': np.array([1., 1., 1.])} test_cat_1 = selector0.within_time_period(begin_time, finish_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data)
def test_select_catalogue(self): ''' Tests the select_catalogue function - essentially a wrapper to the two selection functions ''' self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) # Bad case - no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100.) self.assertEqual(ver.exception.message, 'No events found in catalogue!') # Create a catalogue self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # To ensure that square function is called - compare against direct instance # First implementation - compare select within distance self.point_source.select_catalogue_within_distance(selector0, 100., 'epicentral') expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 100., 'circle') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Second implementation - compare select within cell expected_catalogue = None self.point_source.select_catalogue_within_cell(selector0, 150.) expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 150., 'square') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Finally ensure error is raised when input is neither 'circle' nor 'square' with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100., 'bad input') self.assertEqual(ver.exception.message, 'Unrecognised selection type for point source!')
def test_select_events_in_source(self): ''' Basic test of method to select events from catalogue in polygon ''' self.area_source = mtkAreaSource('101', 'A Source') simple_polygon = polygon.Polygon([ point.Point(2.0, 3.0), point.Point(3.0, 3.0), point.Point(3.0, 2.0), point.Point(2.0, 2.0) ]) self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(1.0, 4.5, 0.5) self.catalogue.data['latitude'] = np.arange(1.0, 4.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple Case - No buffer selector0 = CatalogueSelector(self.catalogue) self.area_source.create_geometry(simple_polygon, 0., 30.) self.area_source.select_catalogue(selector0, 0.) np.testing.assert_array_almost_equal( np.array([2., 2.5, 3.]), self.area_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([2., 2.5, 3.]), self.area_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.area_source.catalogue.data['depth']) # Simple case - dilated by 200 km (selects all) self.area_source.select_catalogue(selector0, 200.) np.testing.assert_array_almost_equal( np.array([1., 1.5, 2., 2.5, 3., 3.5, 4.0]), self.area_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([1., 1.5, 2., 2.5, 3., 3.5, 4.0]), self.area_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.ones(7, dtype=float), self.area_source.catalogue.data['depth']) # Bad case - no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.area_source.select_catalogue(selector0, 0.0) self.assertEqual(ver.exception.message, 'No events found in catalogue!')
def test_select_events_within_cell(self): ''' Tests the selection of events within a cell centred on the point ''' self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Simple case - 200 km by 200 km cell centred on point self.point_source.select_catalogue_within_cell(selector0, 100.) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.point_source.catalogue.data['depth'])
def test_select_catalogue_rrup(self): """ Tests catalogue selection with Joyner-Boore distance """ self.fault = mtkActiveFault( '001', 'A Fault', self.simple_fault, [(5., 0.5), (7., 0.5)], 0., None, msr_sigma=[(-1.5, 0.15), (0., 0.7), (1.5, 0.15)]) cat1 = Catalogue() cat1.data = {"eventID": ["001", "002", "003", "004"], "longitude": np.array([30.1, 30.1, 30.5, 31.5]), "latitude": np.array([30.0, 30.25, 30.4, 30.5]), "depth": np.array([5.0, 250.0, 10.0, 10.0])} selector = CatalogueSelector(cat1) # Select within 50 km of the fault self.fault.select_catalogue(selector, 50.0, distance_metric="rupture") np.testing.assert_array_almost_equal( self.fault.catalogue.data["longitude"], np.array([30.1, 30.5])) np.testing.assert_array_almost_equal( self.fault.catalogue.data["latitude"], np.array([30.0, 30.4])) np.testing.assert_array_almost_equal( self.fault.catalogue.data["depth"], np.array([5.0, 10.0]))
def test_create_cluster_set(self): """ """ # Setup function self.catalogue = Catalogue() self.catalogue.data["EventID"] = np.array([1, 2, 3, 4, 5, 6]) self.catalogue.data["magnitude"] = np.array([7.0, 5.0, 5.0, 5.0, 4.0, 4.0]) selector0 = CatalogueSelector(self.catalogue) vcl = np.array([0, 1, 1, 1, 2, 2]) cluster_set = selector0.create_cluster_set(vcl) np.testing.assert_array_equal(cluster_set[0].data["EventID"], np.array([1])) np.testing.assert_array_almost_equal(cluster_set[0].data["magnitude"], np.array([7.0])) np.testing.assert_array_equal(cluster_set[1].data["EventID"], np.array([2, 3, 4])) np.testing.assert_array_almost_equal(cluster_set[1].data["magnitude"], np.array([5.0, 5.0, 5.0])) np.testing.assert_array_equal(cluster_set[2].data["EventID"], np.array([5, 6])) np.testing.assert_array_almost_equal(cluster_set[2].data["magnitude"], np.array([4.0, 4.0]))
def test_select_within_polygon(self): ''' Tests the selection of points within polygon ''' # Setup polygon nodes = np.array([[5.0, 6.0], [6.0, 6.0], [6.0, 5.0], [5.0, 5.0]]) polygon0 = Polygon( [Point(nodes[iloc, 0], nodes[iloc, 1]) for iloc in range(0, 4)]) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple case with nodes inside, outside and on the border of polygon selector0 = CatalogueSelector(self.catalogue) test_cat1 = selector0.within_polygon(polygon0) self.assertTrue( np.allclose(test_cat1.data['longitude'], np.array([5.0, 5.5, 6.0]))) self.assertTrue( np.allclose(test_cat1.data['latitude'], np.array([5.0, 5.5, 6.0]))) self.assertTrue( np.allclose(test_cat1.data['depth'], np.array([1.0, 1.0, 1.0]))) # CASE 2: As case 1 with one of the inside nodes outside of the depths self.catalogue.data['depth'] = \ np.array([1.0, 1.0, 1.0, 50.0, 1.0, 1.0, 1.0], dtype=float) selector0 = CatalogueSelector(self.catalogue) test_cat1 = selector0.within_polygon(polygon0, upper_depth=0.0, lower_depth=10.0) self.assertTrue( np.allclose(test_cat1.data['longitude'], np.array([5.0, 6.0]))) self.assertTrue( np.allclose(test_cat1.data['latitude'], np.array([5.0, 6.0]))) self.assertTrue(np.allclose(test_cat1.data['depth'], np.array([1.0])))
def test_select_within_time(self): ''' Tests the function to select within a time period ''' self.catalogue.data['year'] = np.arange(1900, 2010, 20) self.catalogue.data['month'] = np.arange(1, 12, 2) self.catalogue.data['day'] = np.ones(6, dtype=int) self.catalogue.data['hour'] = np.ones(6, dtype=int) self.catalogue.data['minute'] = np.zeros(6, dtype=int) self.catalogue.data['second'] = np.ones(6, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Start time and End time not defined test_cat_1 = selector0.within_time_period() self._compare_time_data_dictionaries(test_cat_1.data, self.catalogue.data) # Start time defined - end time not defined begin_time = datetime.datetime(1975, 1, 1, 0, 0, 0, 0) expected_data = { 'year': np.array([1980, 2000]), 'month': np.array([9, 11]), 'day': np.array([1, 1]), 'hour': np.array([1, 1]), 'minute': np.array([0, 0]), 'second': np.array([1., 1.]) } test_cat_1 = selector0.within_time_period(start_time=begin_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data) # Test 3 - Start time not defined, end-time defined finish_time = datetime.datetime(1965, 1, 1, 0, 0, 0, 0) expected_data = { 'year': np.array([1900, 1920, 1940, 1960]), 'month': np.array([1, 3, 5, 7]), 'day': np.array([1, 1, 1, 1]), 'hour': np.array([1, 1, 1, 1]), 'minute': np.array([0, 0, 0, 0]), 'second': np.array([1., 1., 1., 1.]) } test_cat_1 = selector0.within_time_period(end_time=finish_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data) # Test 4 - both start time and end-time defined begin_time = datetime.datetime(1935, 1, 1, 0, 0, 0, 0) finish_time = datetime.datetime(1995, 1, 1, 0, 0, 0, 0) expected_data = { 'year': np.array([1940, 1960, 1980]), 'month': np.array([5, 7, 9]), 'day': np.array([1, 1, 1]), 'hour': np.array([1, 1, 1]), 'minute': np.array([0, 0, 0]), 'second': np.array([1., 1., 1.]) } test_cat_1 = selector0.within_time_period(begin_time, finish_time) self._compare_time_data_dictionaries(expected_data, test_cat_1.data)
def test_point_in_circular_distance(self): ''' Tests point in circular distance ''' self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) test_point = Point(5.5, 5.5) test_mesh = self.catalogue.hypocentres_as_mesh() selector0 = CatalogueSelector(self.catalogue) # Within 10 km test_cat_10 = selector0.circular_distance_from_point( test_point, 10., distance_type='epicentral') np.testing.assert_array_equal(test_cat_10.data['longitude'], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data['latitude'], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data['depth'], np.array([1.0])) # Within 100 km test_cat_100 = selector0.circular_distance_from_point( test_point, 100., distance_type='epicentral') np.testing.assert_array_equal(test_cat_100.data['longitude'], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_equal(test_cat_100.data['latitude'], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_equal(test_cat_100.data['depth'], np.array([1.0, 1.0, 1.0])) # Within 1000 km test_cat_1000 = selector0.circular_distance_from_point( test_point, 1000., distance_type='epicentral') np.testing.assert_array_equal(test_cat_1000.data['longitude'], self.catalogue.data['longitude']) np.testing.assert_array_equal(test_cat_1000.data['latitude'], self.catalogue.data['latitude']) np.testing.assert_array_equal(test_cat_1000.data['depth'], self.catalogue.data['depth'])
def test_cartesian_square_on_point(self): ''' Tests the cartesian square centres on point ''' self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) test_point = Point(5.5, 5.5) test_mesh = self.catalogue.hypocentres_as_mesh() selector0 = CatalogueSelector(self.catalogue) # Within 10 km test_cat_10 = selector0.cartesian_square_centred_on_point( test_point, 10., distance_type='epicentral') np.testing.assert_array_equal(test_cat_10.data['longitude'], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data['latitude'], np.array([5.5])) np.testing.assert_array_equal(test_cat_10.data['depth'], np.array([1.0])) # Within 100 km test_cat_100 = selector0.cartesian_square_centred_on_point( test_point, 100., distance_type='epicentral') np.testing.assert_array_almost_equal(test_cat_100.data['longitude'], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data['latitude'], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data['depth'], np.array([1.0, 1.0, 1.0])) # Within 1000 km test_cat_1000 = selector0.cartesian_square_centred_on_point( test_point, 1000., distance_type='epicentral') np.testing.assert_array_almost_equal(test_cat_1000.data['longitude'], self.catalogue.data['longitude']) np.testing.assert_array_almost_equal(test_cat_1000.data['latitude'], self.catalogue.data['latitude']) np.testing.assert_array_almost_equal(test_cat_1000.data['depth'], self.catalogue.data['depth'])
def test_select_events_in_circular_distance(self): ''' Basic test of method to select events within a distance of the point ''' self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple Case - 100 km epicentral distance selector0 = CatalogueSelector(self.catalogue) self.point_source.create_geometry(simple_point, 0., 30.) self.point_source.select_catalogue_within_distance(selector0, 100., 'epicentral') np.testing.assert_array_almost_equal( np.array([0, 1, 2]), self.point_source.catalogue.data['eventID']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.point_source.catalogue.data['depth']) # Simple case - 100 km hypocentral distance (hypocentre at 70 km) self.point_source.select_catalogue_within_distance(selector0, 100., 'hypocentral', 70.) np.testing.assert_array_almost_equal( np.array([1]), self.point_source.catalogue.data['eventID']) np.testing.assert_array_almost_equal( np.array([4.5]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4.5]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1.]), self.point_source.catalogue.data['depth'])
def test_within_rupture_distance(self): ''' Tests the function to select within Joyner-Boore distance ''' self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Construct Fault trace0 = np.array([[5.5, 6.0], [5.5, 5.0]]) fault_trace = Line( [Point(trace0[i, 0], trace0[i, 1]) for i in range(0, 2)]) # Simple fault with vertical dip fault0 = SimpleFaultSurface.from_fault_data(fault_trace, 0., 20., 90., 1.) # Within 100 km test_cat_100 = selector0.within_rupture_distance(fault0, 100.) np.testing.assert_array_almost_equal(test_cat_100.data['longitude'], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data['latitude'], np.array([5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data['depth'], np.array([1.0, 1.0, 1.0])) # Simple fault with 30 degree dip fault0 = SimpleFaultSurface.from_fault_data(fault_trace, 0., 20., 30., 1.) # Within 100 km test_cat_100 = selector0.within_rupture_distance(fault0, 100.) np.testing.assert_array_almost_equal(test_cat_100.data['longitude'], np.array([4.5, 5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data['latitude'], np.array([4.5, 5.0, 5.5, 6.0])) np.testing.assert_array_almost_equal(test_cat_100.data['depth'], np.array([1.0, 1.0, 1.0, 1.0]))
def test_create_cluster_set(self): """ """ # Setup function self.catalogue = Catalogue() self.catalogue.data["EventID"] = np.array([1, 2, 3, 4, 5, 6]) self.catalogue.data["magnitude"] = np.array( [7.0, 5.0, 5.0, 5.0, 4.0, 4.0]) selector0 = CatalogueSelector(self.catalogue) vcl = np.array([0, 1, 1, 1, 2, 2]) cluster_set = selector0.create_cluster_set(vcl) np.testing.assert_array_equal(cluster_set[0].data["EventID"], np.array([1])) np.testing.assert_array_almost_equal(cluster_set[0].data["magnitude"], np.array([7.0])) np.testing.assert_array_equal(cluster_set[1].data["EventID"], np.array([2, 3, 4])) np.testing.assert_array_almost_equal(cluster_set[1].data["magnitude"], np.array([5.0, 5.0, 5.0])) np.testing.assert_array_equal(cluster_set[2].data["EventID"], np.array([5, 6])) np.testing.assert_array_almost_equal(cluster_set[2].data["magnitude"], np.array([4.0, 4.0]))
parser = CsvCatalogueParser(input_file) catalogue = parser.read_file() print 'Input complete: %s events in catalogue' % catalogue.get_number_events() print 'Catalogue Covers the Period: %s to %s' % (catalogue.start_year, catalogue.end_year) # <codecell> area_model.sources[0].catalogue = 'data_input/hmtk_bsb2013.csv' #area_model.sources[0].catalogue = catalogue # <codecell> from hmtk.seismicity.selector import CatalogueSelector # Create an instance of the selector class selector = CatalogueSelector(catalogue, create_copy=True) # <codecell> for source in area_model.sources: # Selects the earthquakes within each polygon source.select_catalogue(selector) print 'Source Number %s - %s contains %8.0f events' % ( source.id, source.name, source.catalogue.get_number_events()) # <codecell> # Example - show earthquakes in the Gulf of Corinth zone map_config_goc = { 'min_lon': 21.0, 'max_lon': 25.0,
'min_lon': np.floor(105), 'max_lon': np.ceil(155), 'min_lat': np.floor(-45), 'max_lat': np.ceil(-9), 'resolution': 'c' } # Creating a basemap - input a cconfiguration and (if desired) a title #basemap1 = HMTKBaseMap(map_config, 'Earthquake Catalogue') # Adding the seismic sources #basemap1.add_source_model(source_model, area_border='r-', border_width=1.5, alpha=0.5) # In[33]: # Select catalogue from within sourcezone selector1 = CatalogueSelector(catalogue_depth_clean, create_copy=True) for source in source_model.sources: source.select_catalogue(selector1) llon, ulon, llat, ulat = source.catalogue.get_bounding_box() print llon, ulon, llat, ulat # Map the Source #src_basemap = HMTKBaseMap(map_config, "Source: {:s}".format(source.name)) #print "Source ID: %s Source Name: %s Number of Events: %g" % (source.id, source.name, # source.catalogue.get_number_events()) # Add on the catalogue #src_basemap.add_catalogue(source.catalogue, overlay=False) # In[34]: # Map configuration