def get_affected_box(self, src): """ Get the enlarged bounding box of a source. :param src: a source object :returns: a bounding box (min_lon, min_lat, max_lon, max_lat) """ maxdist = self(src.tectonic_region_type) bbox = get_bounding_box(src, maxdist) return (fix_lon(bbox[0]), bbox[1], fix_lon(bbox[2]), bbox[3])
def get_affected_box(self, src): """ Get the enlarged bounding box of a source. :param src: a source object :returns: a bounding box (min_lon, min_lat, max_lon, max_lat) """ mag = src.get_min_max_mag()[1] maxdist = self(src.tectonic_region_type, mag) bbox = get_bounding_box(src, maxdist) return (fix_lon(bbox[0]), bbox[1], fix_lon(bbox[2]), bbox[3])
def get_affected_box(self, src): """ Get the enlarged bounding box of a source. :param src: a source object :returns: a bounding box (min_lon, min_lat, max_lon, max_lat) """ maxdist = self(src.tectonic_region_type) try: bbox = get_bounding_box(src, maxdist) except Exception as exc: raise exc.__class__('source %s: %s' % (src.source_id, exc)) return (fix_lon(bbox[0]), bbox[1], fix_lon(bbox[2]), bbox[3])
def get_enlarged_box(self, src, maxdist=None): """ Get the enlarged bounding box of a source. :param src: a source object :param maxdist: a scalar maximum distance (or None) :returns: a bounding box (min_lon, min_lat, max_lon, max_lat) """ if maxdist is None: maxdist = self.integration_distance(src.tectonic_region_type) try: bbox = get_bounding_box(src, maxdist) except Exception as exc: raise exc.__class__('source %s: %s' % (src.source_id, exc)) return (fix_lon(bbox[0]), bbox[1], fix_lon(bbox[2]), bbox[3])
def plot_sites(calc_id=-1): """ Plot the sites and the bounding boxes of the sources, enlarged by the maximum distance """ # NB: matplotlib is imported inside since it is a costly import import matplotlib.pyplot as p from matplotlib.patches import Rectangle logging.basicConfig(level=logging.INFO) dstore = datastore.read(calc_id) sitecol = dstore['sitecol'] lons, lats = sitecol.lons, sitecol.lats sources = dstore['source_info'].value source_geom = dstore['source_geom'].value fig, ax = p.subplots() ax.grid(True) rects = [get_rectangle(src, source_geom[src['gidx1']:src['gidx2']]) for src in sources] lonset = set(lons) for ((lon, lat), width, height) in rects: lonset.add(lon) lonset.add(fix_lon(lon + width)) idl = cross_idl(min(lonset), max(lonset)) if idl: lons = lons % 360 for ((lon, lat), width, height) in rects: lonlat = (lon % 360 if idl else lon, lat) ax.add_patch(Rectangle(lonlat, width, height, fill=False)) # NB: the code below could be restored in the future # if hasattr(src.__class__, 'polygon'): # xs, ys = fix_polygon(src.polygon, idl) # p.plot(xs, ys, marker='.') p.scatter(lons, lats, marker='+') p.show()
def from_points(cls, lons, lats, depths=None, sitemodel=None, req_site_params=()): """ Build the site collection from :param lons: a sequence of longitudes :param lats: a sequence of latitudes :param depths: a sequence of depths (or None) :param sitemodel: None or an object containing site parameters as attributes :param req_site_params: a sequence of required site parameters, possibly empty """ assert len(lons) < U32LIMIT, len(lons) if depths is None: depths = numpy.zeros(len(lons)) assert len(lons) == len(lats) == len(depths), (len(lons), len(lats), len(depths)) self = object.__new__(cls) self.complete = self req = ['sids', 'lon', 'lat', 'depth'] + sorted( par for par in req_site_params if par not in ('lon', 'lat')) if 'vs30' in req and 'vs30measured' not in req: req.append('vs30measured') dtype = numpy.dtype([(p, site_param_dt[p]) for p in req]) self.array = arr = numpy.zeros(len(lons), dtype) arr['sids'] = numpy.arange(len(lons), dtype=numpy.uint32) arr['lon'] = fix_lon(numpy.array(lons)) arr['lat'] = numpy.array(lats) arr['depth'] = numpy.array(depths) if sitemodel is None: pass elif hasattr(sitemodel, 'reference_vs30_value'): # sitemodel is actually an OqParam instance self._set('vs30', sitemodel.reference_vs30_value) self._set('vs30measured', sitemodel.reference_vs30_type == 'measured') if 'z1pt0' in req_site_params: self._set('z1pt0', sitemodel.reference_depth_to_1pt0km_per_sec) if 'z2pt5' in req_site_params: self._set('z2pt5', sitemodel.reference_depth_to_2pt5km_per_sec) if 'backarc' in req_site_params: self._set('backarc', sitemodel.reference_backarc) else: for name in sitemodel.dtype.names: if name not in ('lon', 'lat'): self._set(name, sitemodel[name]) dupl = get_duplicates(self.array, 'lon', 'lat') if dupl: # raise a decent error message displaying only the first 9 # duplicates (there could be millions) n = len(dupl) dots = ' ...' if n > 9 else '' items = list(dupl.items())[:9] raise ValueError('There are %d duplicate sites %s%s' % (n, items, dots)) return self
def from_points(cls, lons, lats, depths=None, sitemodel=None, req_site_params=()): """ Build the site collection from :param lons: a sequence of longitudes :param lats: a sequence of latitudes :param depths: a sequence of depths (or None) :param sitemodel: None or an object containing site parameters as attributes :param req_site_params: a sequence of required site parameters, possibly empty """ assert len(lons) < U32LIMIT, len(lons) if depths is None: depths = numpy.zeros(len(lons)) assert len(lons) == len(lats) == len(depths), (len(lons), len(lats), len(depths)) self = object.__new__(cls) self.complete = self req = ['sids', 'lon', 'lat', 'depth'] + sorted( par for par in req_site_params if par not in ('lon', 'lat')) if 'vs30' in req and 'vs30measured' not in req: req.append('vs30measured') self.dtype = numpy.dtype([(p, site_param_dt[p]) for p in req]) self.array = arr = numpy.zeros(len(lons), self.dtype) arr['sids'] = numpy.arange(len(lons), dtype=numpy.uint32) arr['lon'] = fix_lon(numpy.array(lons)) arr['lat'] = numpy.array(lats) arr['depth'] = numpy.array(depths) if sitemodel is None: pass elif hasattr(sitemodel, 'reference_vs30_value'): # sitemodel is actually an OqParam instance self._set('vs30', sitemodel.reference_vs30_value) self._set('vs30measured', sitemodel.reference_vs30_type == 'measured') self._set('z1pt0', sitemodel.reference_depth_to_1pt0km_per_sec) self._set('z2pt5', sitemodel.reference_depth_to_2pt5km_per_sec) self._set('siteclass', sitemodel.reference_siteclass) self._set('backarc', sitemodel.reference_backarc) else: for name in sitemodel.dtype.names: if name not in ('lon', 'lat'): self._set(name, sitemodel[name]) return self
def from_points(cls, lons, lats, depths=None, sitemodel=None, req_site_params=()): """ Build the site collection from :param lons: a sequence of longitudes :param lats: a sequence of latitudes :param depths: a sequence of depths (or None) :param sitemodel: None or an object containing site parameters as attributes :param req_site_params: a sequence of required site parameters, possibly empty """ assert len(lons) < U32LIMIT, len(lons) if depths is None: depths = numpy.zeros(len(lons)) assert len(lons) == len(lats) == len(depths), (len(lons), len(lats), len(depths)) self = object.__new__(cls) self.complete = self req = ['sids', 'lon', 'lat', 'depth'] + sorted( par for par in req_site_params if par not in ('lon', 'lat')) if 'vs30' in req and 'vs30measured' not in req: req.append('vs30measured') self.dtype = numpy.dtype([(p, site_param_dt[p]) for p in req]) self.array = arr = numpy.zeros(len(lons), self.dtype) arr['sids'] = numpy.arange(len(lons), dtype=numpy.uint32) arr['lon'] = fix_lon(numpy.array(lons)) arr['lat'] = numpy.array(lats) arr['depth'] = numpy.array(depths) if sitemodel is None: pass elif hasattr(sitemodel, 'reference_vs30_value'): # sitemodel is actually an OqParam instance self._set('vs30', sitemodel.reference_vs30_value) self._set('vs30measured', sitemodel.reference_vs30_type == 'measured') self._set('z1pt0', sitemodel.reference_depth_to_1pt0km_per_sec) self._set('z2pt5', sitemodel.reference_depth_to_2pt5km_per_sec) self._set('siteclass', sitemodel.reference_siteclass) else: for name in sitemodel.dtype.names: if name not in ('lon', 'lat'): self._set(name, sitemodel[name]) return self
def from_points(cls, lons, lats, depths=None, sitemodel=None): """ Build the site collection from :param lons: a sequence of longitudes :param lats: a sequence of latitudes :param depths: a sequence of depths (or None) :param sitemodel: None or an object containing the attributes reference_vs30_value, reference_vs30_type, reference_depth_to_1pt0km_per_sec, reference_depth_to_2pt5km_per_sec, reference_backarc """ if depths is None: depths = numpy.zeros(len(lons)) assert len(lons) == len(lats) == len(depths), (len(lons), len(lats), len(depths)) self = object.__new__(cls) self.indices = None self.array = arr = numpy.zeros(len(lons), self.dtype) arr['sids'] = numpy.arange(len(lons), dtype=numpy.uint32) arr['lons'] = fix_lon(numpy.array(lons)) arr['lats'] = numpy.array(lats) arr['depths'] = numpy.array(depths) if sitemodel is None: pass elif hasattr(sitemodel, 'reference_vs30_value'): # oqparam arr['vs30'] = sitemodel.reference_vs30_value arr['vs30measured'] = sitemodel.reference_vs30_type == 'measured' arr['z1pt0'] = sitemodel.reference_depth_to_1pt0km_per_sec arr['z2pt5'] = sitemodel.reference_depth_to_2pt5km_per_sec arr['backarc'] = sitemodel.reference_backarc elif 'vs30' in sitemodel.dtype.names: # site params for name in sitemodel.dtype.names[2:]: # except lon, lat arr[name] = sitemodel[name] return self
def plot_sites(calc_id=-1): """ Plot the sites and the bounding boxes of the sources, enlarged by the maximum distance """ # NB: matplotlib is imported inside since it is a costly import import matplotlib.pyplot as p from matplotlib.patches import Rectangle logging.basicConfig(level=logging.INFO) dstore = datastore.read(calc_id) oq = dstore['oqparam'] sitecol = dstore['sitecol'] lons, lats = sitecol.lons, sitecol.lats srcfilter = SourceFilter(sitecol.complete, oq.maximum_distance) csm = readinput.get_composite_source_model(oq).pfilter( srcfilter, oq.concurrent_tasks) sources = csm.get_sources() if len(sources) > 100: logging.info('Sampling 100 sources of %d', len(sources)) sources = random.Random(42).sample(sources, 100) fig, ax = p.subplots() ax.grid(True) rects = [srcfilter.get_rectangle(src) for src in sources] lonset = set(lons) for ((lon, lat), width, height) in rects: lonset.add(lon) lonset.add(fix_lon(lon + width)) idl = cross_idl(min(lonset), max(lonset)) if idl: lons = lons % 360 for src, ((lon, lat), width, height) in zip(sources, rects): lonlat = (lon % 360 if idl else lon, lat) ax.add_patch(Rectangle(lonlat, width, height, fill=False)) if hasattr(src.__class__, 'polygon'): xs, ys = fix_polygon(src.polygon, idl) p.plot(xs, ys, marker='.') p.scatter(lons, lats, marker='+') p.show()