def make_distances_context(self, site_collection, rupture, dist_dict=()): """ Create distances context object for given site collection and rupture. :param site_collection: Instance of :class:`openquake.hazardlib.site.SiteCollection`. :param rupture: Instance of :class:`~openquake.hazardlib.source.rupture.Rupture` (or subclass of :class: `~openquake.hazardlib.source.rupture.BaseProbabilisticRupture`). :param dist_dict: A dictionary of already computed distances, keyed by distance name :returns: Source to site distances as instance of :class: `DistancesContext()`. Only those values that are required by GSIM are filled in this context. :raises ValueError: If any of declared required distance parameters is unknown. """ dctx = DistancesContext() for param in self.REQUIRES_DISTANCES | set(['rjb']): if param in dist_dict: # already computed distances distances = dist_dict[param] else: distances = get_distances(rupture, site_collection.mesh, param) setattr(dctx, param, distances) return dctx
def make_contexts(self, sites, rupture, filter_sites=True): """ Filter the site collection with respect to the rupture and create context objects. :param sites: Instance of :class:`openquake.hazardlib.site.SiteCollection`. :param rupture: Instance of :class:`openquake.hazardlib.source.rupture.Rupture` or subclass of :class:`openquake.hazardlib.source.rupture.BaseProbabilisticRupture` :param filter_sites: If True, possibly reduces the site collection before building the :class:`SitesContext`, depending on the value of the integration distance for the current rupture. :returns: Tuple of three items: sites context, rupture context and distances context, that is, instances of :class:`SitesContext`, :class:`RuptureContext` and :class:`DistancesContext` in a specified order. Only those values that are required by GSIM are filled in in contexts. :raises ValueError: If any of declared required parameters (that includes site, rupture and distance parameters) is unknown. """ rctx = self.make_rupture_context(rupture) # compute the rjb distance and normally filter with it dctx = DistancesContext() dctx.rjb = get_distances(rupture, sites.mesh, 'rjb') if self.maximum_distance and filter_sites: maxdist = self.maximum_distance( rupture.tectonic_region_type, rupture.mag) mask = dctx.rjb <= maxdist if mask.any(): sites, dctx.rjb = sites.filter(mask), dctx.rjb[mask] else: raise FarAwayRupture mesh = sites.mesh for param in self.REQUIRES_DISTANCES - set(['rjb']): setattr(dctx, param, get_distances(rupture, mesh, param)) sctx = self.make_sites_context(sites) return sctx, rctx, dctx
def get_ctxs(self, src_or_ruptures, sitecol, src_id=None): """ :param src_or_ruptures: a source or a list of ruptures generated by a source :param sitecol: a (filtered) SiteCollection :param src_id: the numeric ID of the source (to be assigned to the ruptures) :returns: fat RuptureContexts """ if hasattr(src_or_ruptures, 'source_id'): irups = self._gen_rups(src_or_ruptures, sitecol) else: irups = src_or_ruptures ctxs = [] fewsites = len(sitecol.complete) <= self.max_sites_disagg for rup in irups: sites = getattr(rup, 'sites', sitecol) try: r_sites, dctx = self.filter(sites, rup) except FarAwayRupture: continue ctx = self.make_rctx(rup) ctx.sites = r_sites for param in self.REQUIRES_DISTANCES - {'rrup'}: distances = get_distances(rup, r_sites, param) setattr(dctx, param, distances) reqv_obj = (self.reqv.get(self.trt) if self.reqv else None) if reqv_obj and isinstance(rup.surface, PlanarSurface): reqv = reqv_obj.get(dctx.repi, rup.mag) if 'rjb' in self.REQUIRES_DISTANCES: dctx.rjb = reqv if 'rrup' in self.REQUIRES_DISTANCES: dctx.rrup = numpy.sqrt( reqv**2 + rup.hypocenter.depth**2) for name in r_sites.array.dtype.names: setattr(ctx, name, r_sites[name]) ctx.src_id = src_id for par in self.REQUIRES_DISTANCES | {'rrup'}: setattr(ctx, par, getattr(dctx, par)) if fewsites: # get closest point on the surface closest = rup.surface.get_closest_points(sitecol.complete) ctx.clon = closest.lons[ctx.sids] ctx.clat = closest.lats[ctx.sids] ctxs.append(ctx) return ctxs
def filter(self, sites, rup): """ Filter the site collection with respect to the rupture. :param sites: Instance of :class:`openquake.hazardlib.site.SiteCollection`. :param rup: Instance of :class:`openquake.hazardlib.source.rupture.BaseRupture` :returns: (filtered sites, distance context) """ distances = get_distances(rup, sites, 'rrup') mdist = self.maximum_distance(rup.mag) + _delta_pr(rup) mask = distances <= mdist if mask.any(): sites, distances = sites.filter(mask), distances[mask] else: raise FarAwayRupture('%d: %d km' % (rup.rup_id, distances.min())) return sites, DistancesContext([('rrup', distances)])