示例#1
0
 def todict(self):
     """
     :returns: a representation of the rupture as a dict
     """
     if not code2cls:
         code2cls.update(BaseRupture.init())
     hypo = self.hypocenter.x, self.hypocenter.y, self.hypocenter.z
     mesh = surface_to_array(self.surface)  # shape (3, sy, sz)
     sy, sz = mesh.shape[1:]
     dic = {
         'serial': int(self.rup_id),
         'mag': self.mag,
         'rake': self.rake,
         'hypo': hypo,
         'trt': self.tectonic_region_type,
         'code': self.code,
         'occurrence_rate': self.occurrence_rate,
         'rupture_cls': self.__class__.__name__,
         'surface_cls': self.surface.__class__.__name__,
         'lons': mesh[0],
         'lats': mesh[1],
         'depths': mesh[2]
     }
     _fixfloat32(dic)
     return dic
示例#2
0
def get_rup_array(ebruptures):
    """
    Convert a list of EBRuptures into a numpy composite array
    """
    if not BaseRupture._code:
        BaseRupture.init()  # initialize rupture codes

    lst = []
    geoms = []
    nbytes = 0
    offset = 0
    for ebrupture in ebruptures:
        rup = ebrupture.rupture
        mesh = surface_to_array(rup.surface)
        sy, sz = mesh.shape[1:]
        # sanity checks
        assert sy < TWO16, 'Too many multisurfaces: %d' % sy
        assert sz < TWO16, 'The rupture mesh spacing is too small'
        hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z
        rate = getattr(rup, 'occurrence_rate', numpy.nan)
        points = mesh.reshape(3, -1).T   # shape (n, 3)
        n = len(points)
        tup = (ebrupture.serial, ebrupture.srcidx, ebrupture.grp_id,
               rup.code, ebrupture.n_occ, offset, offset + n, -1,
               rup.mag, rup.rake, rate, hypo, sy, sz)
        offset += n
        lst.append(tup)
        geoms.append(numpy.array([tuple(p) for p in points], point3d))
        nbytes += rupture_dt.itemsize + mesh.nbytes
    dic = dict(geom=numpy.concatenate(geoms), nbytes=nbytes)
    # TODO: PMFs for nonparametric ruptures are not converted
    return hdf5.ArrayWrapper(numpy.array(lst, rupture_dt), dic)
示例#3
0
 def get_array_nbytes(cls, ebruptures, offset):
     """
     Convert a list of EBRuptures into a numpy composite array
     """
     lst = []
     geoms = []
     nbytes = 0
     for ebrupture in ebruptures:
         rup = ebrupture.rupture
         mesh = surface_to_array(rup.surface)
         sy, sz = mesh.shape[1:]
         # sanity checks
         assert sy < TWO16, 'Too many multisurfaces: %d' % sy
         assert sz < TWO16, 'The rupture mesh spacing is too small'
         hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z
         rate = getattr(rup, 'occurrence_rate', numpy.nan)
         points = mesh.reshape(3, -1).T  # shape (n, 3)
         n = len(points)
         tup = (ebrupture.serial, ebrupture.srcidx, ebrupture.grp_id,
                rup.code, ebrupture.eidx1,
                ebrupture.eidx2, offset, offset + n,
                getattr(ebrupture, 'pmfx',
                        -1), rup.mag, rup.rake, rate, hypo, sy, sz)
         offset += n
         lst.append(tup)
         geoms.append(numpy.array([tuple(p) for p in points], point3d))
         nbytes += cls.rupture_dt.itemsize + mesh.nbytes
     geom = numpy.concatenate(geoms)
     return numpy.array(lst, cls.rupture_dt), geom, nbytes
示例#4
0
def to_csv_array(ruptures):
    """
    :param ruptures: a list of ruptures
    :returns: an array of ruptures suitable for serialization in CSV
    """
    if not code2cls:
        code2cls.update(BaseRupture.init())
    arr = numpy.zeros(len(ruptures), rupture_dt)
    for rec, rup in zip(arr, ruptures):
        mesh = surface_to_array(rup.surface)  # shape (3, s1, s2)
        rec['serial'] = rup.rup_id
        rec['mag'] = rup.mag
        rec['rake'] = rup.rake
        rec['lon'] = rup.hypocenter.x
        rec['lat'] = rup.hypocenter.y
        rec['dep'] = rup.hypocenter.z
        rec['multiplicity'] = rup.multiplicity
        rec['trt'] = rup.tectonic_region_type
        rec['kind'] = ' '.join(cls.__name__ for cls in code2cls[rup.code])
        rec['mesh'] = json.dumps(
            [[[float5(z) for z in y] for y in x] for x in mesh])
        extra = {}
        if hasattr(rup, 'probs_occur'):
            extra['probs_occur'] = rup.probs_occur
        else:
            extra['occurrence_rate'] = rup.occurrence_rate
        if hasattr(rup, 'weight'):
            extra['weight'] = rup.weight
        _fixfloat32(extra)
        rec['extra'] = json.dumps(extra)
    return arr
示例#5
0
    def _read_scenario_ruptures(self):
        oq = self.oqparam
        gsim_lt = readinput.get_gsim_lt(self.oqparam)
        G = gsim_lt.get_num_paths()
        if oq.inputs['rupture_model'].endswith('.xml'):
            ngmfs = oq.number_of_ground_motion_fields
            self.gsims = readinput.get_gsims(oq)
            self.cmaker = ContextMaker('*', self.gsims, {
                'maximum_distance': oq.maximum_distance,
                'imtls': oq.imtls
            })
            rup = readinput.get_rupture(oq)
            mesh = surface_to_array(rup.surface).transpose(1, 2, 0).flatten()
            if self.N > oq.max_sites_disagg:  # many sites, split rupture
                ebrs = [
                    EBRupture(copyobj(rup, rup_id=rup.rup_id + i),
                              0,
                              0,
                              G,
                              e0=i * G) for i in range(ngmfs)
                ]
                meshes = numpy.array([mesh] * ngmfs, object)
            else:  # keep a single rupture with a big occupation number
                ebrs = [EBRupture(rup, 0, 0, G * ngmfs, rup.rup_id)]
                meshes = numpy.array([mesh] * ngmfs, object)
            rup_array = get_rup_array(ebrs, self.srcfilter).array
            hdf5.extend(self.datastore['rupgeoms'], meshes)
        elif oq.inputs['rupture_model'].endswith('.csv'):
            aw = readinput.get_ruptures(oq.inputs['rupture_model'])
            aw.array['n_occ'] = G
            rup_array = aw.array
            hdf5.extend(self.datastore['rupgeoms'], aw.geom)

        if len(rup_array) == 0:
            raise RuntimeError(
                'There are no sites within the maximum_distance'
                ' of %s km from the rupture' %
                oq.maximum_distance(rup.tectonic_region_type, rup.mag))

        # check the number of branchsets
        branchsets = len(gsim_lt._ltnode)
        if len(rup_array) == 1 and branchsets > 1:
            raise InvalidFile(
                '%s for a scenario calculation must contain a single '
                'branchset, found %d!' % (oq.inputs['job_ini'], branchsets))

        fake = logictree.FullLogicTree.fake(gsim_lt)
        self.realizations = fake.get_realizations()
        self.datastore['full_lt'] = fake
        self.store_rlz_info({})  # store weights
        self.save_params()
        calc.RuptureImporter(self.datastore).import_rups(rup_array)
示例#6
0
def get_rup_array(ebruptures, srcfilter=nofilter):
    """
    Convert a list of EBRuptures into a numpy composite array, by filtering
    out the ruptures far away from every site
    """
    if not BaseRupture._code:
        BaseRupture.init()  # initialize rupture codes

    rups = []
    geoms = []
    nbytes = 0
    offset = 0
    for ebrupture in ebruptures:
        rup = ebrupture.rupture
        mesh = surface_to_array(rup.surface)
        sy, sz = mesh.shape[1:]  # sanity checks
        assert sy < TWO16, 'Too many multisurfaces: %d' % sy
        assert sz < TWO16, 'The rupture mesh spacing is too small'
        hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z
        points = mesh.reshape(3, -1).T  # shape (n, 3)
        rec = numpy.zeros(1, rupture_dt)[0]
        rec['serial'] = rup.rup_id
        rec['minlon'] = minlon = points[:, 0].min()
        rec['minlat'] = minlat = points[:, 1].min()
        rec['maxlon'] = maxlon = points[:, 0].max()
        rec['maxlat'] = maxlat = points[:, 1].max()
        rec['mag'] = rup.mag
        rec['hypo'] = hypo

        if srcfilter.integration_distance and len(
                srcfilter.close_sids(rec, rup.tectonic_region_type)) == 0:
            continue
        rate = getattr(rup, 'occurrence_rate', numpy.nan)
        tup = (0, ebrupture.rup_id, ebrupture.srcidx, ebrupture.grp_id,
               rup.code, ebrupture.n_occ, rup.mag, rup.rake, rate, minlon,
               minlat, maxlon, maxlat, hypo, offset, offset + len(points), sy,
               sz, 0, 0)
        #,ebrupture.source_id)
        offset += len(points)
        rups.append(tup)
        geoms.append(numpy.array([tuple(p) for p in points], point3d))
        nbytes += rupture_dt.itemsize + mesh.nbytes
    if not rups:
        return ()
    dic = dict(geom=numpy.concatenate(geoms), nbytes=nbytes)
    # NB: PMFs for nonparametric ruptures are not saved since they
    # are useless for the GMF computation
    return hdf5.ArrayWrapper(numpy.array(rups, rupture_dt), dic)
示例#7
0
def oq_rupture_to_json(rupture: Union[ParametricProbabilisticRupture,
                                      NonParametricProbabilisticRupture]):

    mesh = surface_to_array(rupture.surface)

    rec = {}
    rec["id"] = rupture.rup_id
    rec["mag"] = rupture.mag
    rec["rake"] = rupture.rake
    rec["lon"] = rupture.hypocenter.x
    rec["lat"] = rupture.hypocenter.y
    rec["dep"] = rupture.hypocenter.z
    rec["trt"] = rupture.tectonic_region_type
    # rec['multiplicity'] = rup.multiplicity
    rec["mesh"] = json.dumps([[[float5(z) for z in y] for y in x]
                              for x in mesh])

    return rec
示例#8
0
文件: io.py 项目: vup1120/hamlet
def oq_rupture_to_json(rupture: Union[ParametricProbabilisticRupture,
                                      NonParametricProbabilisticRupture]):

    mesh = surface_to_array(rupture.surface)

    rec = {}
    rec['id'] = rupture.rup_id
    rec['mag'] = rupture.mag
    rec['rake'] = rupture.rake
    rec['lon'] = rupture.hypocenter.x
    rec['lat'] = rupture.hypocenter.y
    rec['dep'] = rupture.hypocenter.z
    rec['trt'] = rupture.tectonic_region_type
    #rec['multiplicity'] = rup.multiplicity
    rec['mesh'] = json.dumps([[[float5(z) for z in y] for y in x]
                              for x in mesh])

    return rec
示例#9
0
def get_rup_array(ebruptures, srcfilter=nofilter):
    """
    Convert a list of EBRuptures into a numpy composite array, by filtering
    out the ruptures far away from every site
    """
    if not BaseRupture._code:
        BaseRupture.init()  # initialize rupture codes

    rups = []
    geoms = []
    nbytes = 0
    offset = 0
    for ebrupture in ebruptures:
        rup = ebrupture.rupture
        mesh = surface_to_array(rup.surface)
        sy, sz = mesh.shape[1:]  # sanity checks
        assert sy < TWO16, 'Too many multisurfaces: %d' % sy
        assert sz < TWO16, 'The rupture mesh spacing is too small'
        points = mesh.reshape(3, -1).T   # shape (n, 3)
        minlon = points[:, 0].min()
        minlat = points[:, 1].min()
        maxlon = points[:, 0].max()
        maxlat = points[:, 1].max()
        if srcfilter.integration_distance and len(srcfilter.close_sids(
                (minlon, minlat, maxlon, maxlat),
                rup.tectonic_region_type, rup.mag)) == 0:
            continue
        hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z
        rate = getattr(rup, 'occurrence_rate', numpy.nan)
        tup = (ebrupture.serial, ebrupture.srcidx, ebrupture.grp_id,
               rup.code, ebrupture.n_occ, rup.mag, rup.rake, rate,
               minlon, minlat, maxlon, maxlat,
               hypo, offset, offset + len(points), sy, sz)
        offset += len(points)
        rups.append(tup)
        geoms.append(numpy.array([tuple(p) for p in points], point3d))
        nbytes += rupture_dt.itemsize + mesh.nbytes
    if not rups:
        return ()
    dic = dict(geom=numpy.concatenate(geoms), nbytes=nbytes)
    # TODO: PMFs for nonparametric ruptures are not converted
    return hdf5.ArrayWrapper(numpy.array(rups, rupture_dt), dic)
示例#10
0
    def _read_scenario_ruptures(self):
        oq = self.oqparam
        if oq.inputs['rupture_model'].endswith(('.xml', '.toml', '.txt')):
            self.gsims = readinput.get_gsims(oq)
            self.cmaker = ContextMaker(
                '*', self.gsims,
                {'maximum_distance': oq.maximum_distance,
                 'filter_distance': oq.filter_distance})
            n_occ = numpy.array([oq.number_of_ground_motion_fields])
            rup = readinput.get_rupture(oq)
            ebr = EBRupture(rup, 0, 0, n_occ)
            ebr.e0 = 0
            rup_array = get_rup_array([ebr], self.srcfilter).array
            mesh = surface_to_array(rup.surface).transpose(1, 2, 0).flatten()
            hdf5.extend(self.datastore['rupgeoms'],
                        numpy.array([mesh], object))
        elif oq.inputs['rupture_model'].endswith('.csv'):
            aw = readinput.get_ruptures(oq.inputs['rupture_model'])
            rup_array = aw.array
            hdf5.extend(self.datastore['rupgeoms'], aw.geom)

        if len(rup_array) == 0:
            raise RuntimeError(
                'There are no sites within the maximum_distance'
                ' of %s km from the rupture' % oq.maximum_distance(
                    rup.tectonic_region_type, rup.mag))

        gsim_lt = readinput.get_gsim_lt(self.oqparam)
        # check the number of branchsets
        branchsets = len(gsim_lt._ltnode)
        if len(rup_array) == 1 and branchsets > 1:
            raise InvalidFile(
                '%s for a scenario calculation must contain a single '
                'branchset, found %d!' % (oq.inputs['job_ini'], branchsets))

        fake = logictree.FullLogicTree.fake(gsim_lt)
        self.realizations = fake.get_realizations()
        self.datastore['full_lt'] = fake
        self.store_rlz_info({})  # store weights
        self.save_params()
        calc.RuptureImporter(self.datastore).import_rups(rup_array)
示例#11
0
def get_source_ids(ebruptures, srcfilter):
    """
    Save source_id given by source model and srcidx found in ebruptures
    :param ebruptures: list of EBRuptures objects
    """
    if not BaseRupture._code:
        BaseRupture.init()  # initialize rupture codes

    srcs = []

    for ebrupture in ebruptures:
        rup = ebrupture.rupture
        mesh = surface_to_array(rup.surface)
        sy, sz = mesh.shape[1:]  # sanity checks
        assert sy < TWO16, 'Too many multisurfaces: %d' % sy
        assert sz < TWO16, 'The rupture mesh spacing is too small'
        hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z
        points = mesh.reshape(3, -1).T  # shape (n, 3)
        rec = numpy.zeros(1, rupture_dt)[0]
        rec['minlon'] = points[:, 0].min()
        rec['minlat'] = points[:, 1].min()
        rec['maxlon'] = points[:, 0].max()
        rec['maxlat'] = points[:, 1].max()
        rec['mag'] = rup.mag
        rec['hypo'] = hypo

        if srcfilter.integration_distance and len(
                srcfilter.close_sids(rec, rup.tectonic_region_type)) == 0:
            continue
        tup = (ebrupture.source_id, ebrupture.srcidx, ebrupture.trt)

        #if tup not in srcs:
        srcs.append(tup)

    if not srcs:
        return ()

    dic = {}

    return hdf5.ArrayWrapper(numpy.array(srcs, source_ids_dt), dic)
示例#12
0
 def get_array_nbytes(cls, ebruptures):
     """
     Convert a list of EBRuptures into a numpy composite array
     """
     lst = []
     geom = []
     nbytes = 0
     for ebrupture in ebruptures:
         rup = ebrupture.rupture
         mesh = surface_to_array(rup.surface)
         sy, sz = mesh.shape[1:]
         # sanity checks
         assert sy < TWO16, 'Too many multisurfaces: %d' % sy
         assert sz < TWO16, 'The rupture mesh spacing is too small'
         hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z
         rate = getattr(rup, 'occurrence_rate', numpy.nan)
         tup = (ebrupture.serial, ebrupture.grp_id, rup.code,
                ebrupture.eidx1, ebrupture.eidx2,
                getattr(ebrupture, 'pmfx', -1), rup.seed, rup.mag, rup.rake,
                rate, hypo, sy, sz)
         lst.append(tup)
         geom.append(mesh.reshape(3, -1))
         nbytes += cls.rupture_dt.itemsize + mesh.nbytes
     return numpy.array(lst, cls.rupture_dt), geom, nbytes