def _test(self, mlons, mlats, mdepths, slons, slats, sdepths, expected_mpoint_indices): mlons, mlats, mdepths = [numpy.array(arr, float) for arr in (mlons, mlats, mdepths)] slons, slats, sdepths = [numpy.array(arr, float) for arr in (slons, slats, sdepths)] actual_indices, dists = geodetic.min_idx_dst(mlons, mlats, mdepths, slons, slats, sdepths) numpy.testing.assert_equal(actual_indices, expected_mpoint_indices) expected_closest_mlons = mlons.flat[expected_mpoint_indices] expected_closest_mlats = mlats.flat[expected_mpoint_indices] expected_closest_mdepths = mdepths.flat[expected_mpoint_indices] expected_distances = geodetic.distance( expected_closest_mlons, expected_closest_mlats, expected_closest_mdepths, slons, slats, sdepths ) self.assertTrue((dists == expected_distances).all()) # testing min_geodetic_distance with the same lons and lats min_geo_distance = geodetic.min_geodetic_distance(mlons, mlats, slons, slats) min_distance = geodetic.min_idx_dst(mlons, mlats, mdepths * 0, slons, slats, sdepths * 0)[1] numpy.testing.assert_almost_equal(min_geo_distance, min_distance)
def _test(self, mlons, mlats, mdepths, slons, slats, sdepths, expected_mpoint_indices): mlons, mlats, mdepths = [ numpy.array(arr, float) for arr in (mlons, mlats, mdepths) ] slons, slats, sdepths = [ numpy.array(arr, float) for arr in (slons, slats, sdepths) ] actual_indices, dists = geodetic.min_idx_dst(mlons, mlats, mdepths, slons, slats, sdepths) numpy.testing.assert_equal(actual_indices, expected_mpoint_indices) expected_closest_mlons = mlons.flat[expected_mpoint_indices] expected_closest_mlats = mlats.flat[expected_mpoint_indices] expected_closest_mdepths = mdepths.flat[expected_mpoint_indices] expected_distances = geodetic.distance(expected_closest_mlons, expected_closest_mlats, expected_closest_mdepths, slons, slats, sdepths) self.assertTrue((dists == expected_distances).all()) # testing min_geodetic_distance with the same lons and lats min_geo_distance = geodetic.min_geodetic_distance( mlons, mlats, slons, slats) min_distance = geodetic.min_idx_dst(mlons, mlats, mdepths * 0, slons, slats, sdepths * 0)[1] numpy.testing.assert_almost_equal(min_geo_distance, min_distance)
def prefilter_background_model(hdf5, branch_key, sites, integration_distance, msr=WC1994(), aspect=1.5): """ Identify those points within the integration distance :param sites: Sites under consideration :param float integration_distance: Maximum distance from rupture to site for consideration :param msr: Magnitude scaling relation :param float aspect: Aspect ratio :returns: List of site IDs within the integration distance """ bg_locations = hdf5["Grid/Locations"][:].astype("float64") n_locations = bg_locations.shape[0] distances = min_idx_dst(sites.lons, sites.lats, numpy.zeros_like(sites.lons), bg_locations[:, 0], bg_locations[:, 1], numpy.zeros(n_locations))[1] # Add buffer equal to half of length of median area from Mmax mmax_areas = msr.get_median_area( hdf5["/".join(["Grid", branch_key, "MMax"])][:], 0.0) mmax_lengths = numpy.sqrt(mmax_areas / aspect) ok = distances <= (0.5 * mmax_lengths + integration_distance) # get list of indices from array of booleans return numpy.where(ok)[0].tolist()
def _min_idx_dst(self, mesh): if self.depths is None: depths1 = numpy.zeros_like(self.lons) else: depths1 = self.depths if mesh.depths is None: depths2 = numpy.zeros_like(mesh.lons) else: depths2 = mesh.depths return geodetic.min_idx_dst(self.lons, self.lats, depths1, mesh.lons, mesh.lats, depths2)
def _min_idx_dst(self, mesh): if self.depths is None: depths1 = numpy.zeros_like(self.lons) else: depths1 = self.depths if mesh.depths is None: depths2 = numpy.zeros_like(mesh.lons) else: depths2 = mesh.depths return geodetic.min_idx_dst( self.lons, self.lats, depths1, mesh.lons, mesh.lats, depths2)
def get_closest(self, lon, lat): """ Get the closest object to the given longitude and latitude and its distance. :param lon: longitude in degrees :param lat: latitude in degrees :param max_distance: distance in km (or None) """ if rtree: x, y = self.proj(lon, lat) idx = list(self.index.nearest((x, y, x, y), 1))[0] min_dist = geodetic_distance(lon, lat, self.lons[idx], self.lats[idx]) else: zeros = numpy.zeros_like(self.lons) idx, min_dist = min_idx_dst(self.lons, self.lats, zeros, lon, lat) return self.objects[idx], min_dist
def get_closest(self, lon, lat, max_distance=None): """ Get the closest object to the given longitude and latitude and its distance. If the `max_distance` is given and all objects are farther than the maximum distance, returns (None, None). :param lon: longitude in degrees :param lat: latitude in degrees :param max_distance: distance in km (or None) """ if rtree: x, y = self.proj(lon, lat) idx = list(self.index.nearest((x, y, x, y), 1))[0] min_dist = geodetic_distance( lon, lat, self.lons[idx], self.lats[idx]) else: zeros = numpy.zeros_like(self.lons) idx, min_dist = min_idx_dst(self.lons, self.lats, zeros, lon, lat) if max_distance is not None and min_dist > max_distance: return None, None return self.objects[idx], min_dist
def get_closest(self, lon, lat, max_distance=None): """ Get the closest object to the given longitude and latitude and its distance. If the `max_distance` is given and all objects are farther than the maximum distance, returns (None, None). :param lon: longitude in degrees :param lat: latitude in degrees :param max_distance: distance in km (or None) """ if rtree: x, y = self.proj(lon, lat) idx = list(self.index.nearest((x, y, x, y), 1))[0] min_dist = geodetic_distance(lon, lat, self.lons[idx], self.lats[idx]) else: zeros = numpy.zeros_like(self.lons) idx, min_dist = min_idx_dst(self.lons, self.lats, zeros, lon, lat) if max_distance is not None and min_dist > max_distance: return None, None return self.objects[idx], min_dist
def get_background_sids(self, src_filter): """ We can apply the filtering of the background sites as a pre-processing step - this is done here rather than in the sampling of the ruptures themselves """ branch_key = self.idx_set["grid_key"] idist = src_filter.integration_distance(DEFAULT_TRT) lons, lats = src_filter.sitecol.lons, src_filter.sitecol.lats with h5py.File(self.source_file, 'r') as hdf5: bg_locations = hdf5["Grid/Locations"].value n_locations = bg_locations.shape[0] distances = min_idx_dst(lons, lats, numpy.zeros_like(lons), bg_locations[:, 0], bg_locations[:, 1], numpy.zeros(n_locations))[1] # Add buffer equal to half of length of median area from Mmax mmax_areas = self.msr.get_median_area( hdf5["/".join(["Grid", branch_key, "MMax"])].value, 0.0) # for instance hdf5['Grid/FM0_0_MEANFS_MEANMSR/MMax'] mmax_lengths = numpy.sqrt(mmax_areas / self.aspect) ok = distances <= (0.5 * mmax_lengths + idist) # get list of indices from array of booleans return numpy.where(ok)[0].tolist()