示例#1
0
def make_gmfs(ses_ruptures, sitecol, imts, gsims, trunc_level, correl_model,
              monitor):
    """
    :param ses_ruptures: a list of SESRuptures
    :param sitecol: a SiteCollection instance
    :param imts: an ordered list of intensity measure type strings
    :param gsims: an order list of GSIM instance
    :param trunc_level: truncation level
    :param correl_model: correlation model instance
    :param monitor: a monitor instance
    :returns: a list of arrays, one for each rupture
    """
    gmfs = []
    ctx_mon = monitor('make contexts')
    gmf_mon = monitor('compute poes')
    for rupture, group in itertools.groupby(ses_ruptures,
                                            operator.attrgetter('rupture')):
        sesruptures = list(group)
        indices = sesruptures[0].indices
        r_sites = (sitecol if indices is None else site.FilteredSiteCollection(
            indices, sitecol.complete))
        with ctx_mon:
            computer = calc.gmf.GmfComputer(rupture, r_sites, imts, gsims,
                                            trunc_level, correl_model)
        with gmf_mon:
            for sr in sesruptures:
                gmf = computer.compute([sr.seed])[0]
                # TODO: change idx to rup_idx, also in hazardlib
                gmf['idx'] = sr.ordinal
                gmfs.append(gmf)
    return gmfs
示例#2
0
 def init(self):
     """
     Initialize the computers. Should be called on the workers
     """
     self.N = len(self.sitecol.complete)
     self.I = I = len(self.imts)
     self.R = sum(len(rlzs) for rlzs in self.rlzs_by_gsim.values())
     self.gmv_dt = numpy.dtype([('sid', U32), ('eid', U64),
                                ('gmv', (F32, (I, )))])
     self.gmv_eid_dt = numpy.dtype([('gmv', (F32, (I, ))), ('eid', U64)])
     self.sids = self.sitecol.sids
     self.computers = []
     gsims = sorted(self.rlzs_by_gsim)
     for ebr in self.ebruptures:
         sites = site.FilteredSiteCollection(ebr.sids,
                                             self.sitecol.complete)
         computer = calc.gmf.GmfComputer(ebr, sites, self.imts, gsims,
                                         self.truncation_level,
                                         self.correlation_model)
         self.computers.append(computer)
     # dictionary rlzi -> array(imts, events, nbytes)
     self.gmdata = AccumDict(accum=numpy.zeros(len(self.imts) + 2, F32))
     self.eids = numpy.concatenate(
         [ebr.events['eid'] for ebr in self.ebruptures])
     # dictionary eid -> index
     self.eid2idx = dict(zip(self.eids, range(len(self.eids))))
示例#3
0
def make_gmfs(eb_ruptures, sitecol, imts, gsims,
              trunc_level, correl_model, monitor=Monitor()):
    """
    :param eb_ruptures: a list of EBRuptures
    :param sitecol: a SiteCollection instance
    :param imts: an ordered list of intensity measure type strings
    :param gsims: an order list of GSIM instance
    :param trunc_level: truncation level
    :param correl_model: correlation model instance
    :param monitor: a monitor instance
    :returns: a dictionary serial -> GmfaSidsEtags
    """
    dic = {}  # serial -> GmfaSidsEtags
    ctx_mon = monitor('make contexts')
    gmf_mon = monitor('compute poes')
    sites = sitecol.complete
    for ebr in eb_ruptures:
        with ctx_mon:
            r_sites = site.FilteredSiteCollection(ebr.indices, sites)
            computer = calc.gmf.GmfComputer(
                ebr.rupture, r_sites, imts, gsims, trunc_level, correl_model)
        with gmf_mon:
            gmfa = computer.calcgmfs(ebr.multiplicity, ebr.rupture.seed)
            dic[ebr.serial] = GmfaSidsEtags(gmfa, r_sites.indices, ebr.etags)
    return dic
示例#4
0
def get_gmfs_from_txt(oqparam, fname):
    """
    :param oqparam:
        an :class:`openquake.commonlib.oqvalidation.OqParam` instance
    :param fname:
        the full path of the CSV file
    :returns:
        a composite array of shape (N, R) read from a CSV file with format
        `etag indices [gmv1 ... gmvN] * num_imts`
    """
    with open(fname) as csvfile:
        firstline = next(csvfile)
        try:
            coords = valid.coordinates(firstline)
        except:
            raise InvalidFile(
                'The first line of %s is expected to contain comma separated'
                'ordered coordinates, got %s instead' % (fname, firstline))
        lons, lats, depths = zip(*coords)
        sitecol = site.SiteCollection.from_points(lons, lats, depths, oqparam)
        if not oqparam.imtls:
            oqparam.set_risk_imtls(get_risk_models(oqparam))
        imts = list(oqparam.imtls)
        imt_dt = numpy.dtype([(imt, F32) for imt in imts])
        num_gmfs = oqparam.number_of_ground_motion_fields
        gmf_by_imt = numpy.zeros((num_gmfs, len(sitecol)), imt_dt)
        etags = []

        for lineno, line in enumerate(csvfile, 2):
            row = line.split(',')
            try:
                indices = list(map(valid.positiveint, row[1].split()))
            except:
                raise InvalidFile(
                    'The second column in %s is expected to contain integer '
                    'indices, got %s' % (fname, row[1]))
            r_sites = (sitecol if not indices else site.FilteredSiteCollection(
                indices, sitecol))
            for i in range(len(imts)):
                try:
                    array = numpy.array(valid.positivefloats(row[i + 2]))
                    # NB: i + 2 because the first 2 fields are etag and indices
                except:
                    raise InvalidFile(
                        'The column #%d in %s is expected to contain positive '
                        'floats, got %s instead' % (i + 3, fname, row[i + 2]))
                gmf_by_imt[imts[i]][lineno - 2][r_sites.sids] = array
            etags.append(row[0])
    if lineno < num_gmfs + 1:
        raise InvalidFile('%s contains %d rows, expected %d' %
                          (fname, lineno, num_gmfs + 1))
    if etags != sorted(etags):
        raise InvalidFile('The etags in %s are not ordered: %s' %
                          (fname, etags))
    return sitecol, numpy.array([encode(e) for e in etags]), gmf_by_imt.T
示例#5
0
def get_gmfs_from_csv(oqparam, sitecol, fname):
    """
    :param oqparam:
        an :class:`openquake.commonlib.oqvalidation.OqParam` instance
    :param sitecol:
        a SiteCollection instance with sites consistent with the CSV file
    :param fname:
        the full path of the CSV file
    :returns:
        a composite array of shape (N, R) read from a CSV file with format
        `tag indices [gmv1 ... gmvN] * num_imts`
    """
    imts = list(oqparam.imtls)
    imt_dt = numpy.dtype([(imt, float) for imt in imts])
    num_gmfs = oqparam.number_of_ground_motion_fields
    gmf_by_imt = numpy.zeros((num_gmfs, len(sitecol)), imt_dt)
    tags = []
    with open(fname) as csvfile:
        for lineno, line in enumerate(csvfile, 1):
            row = line.split(',')
            try:
                indices = list(map(valid.positiveint, row[1].split()))
            except:
                raise InvalidFile(
                    'The second column in %s is expected to contain integer '
                    'indices, got %s instead' % (fname, row[1]))
            r_sites = (sitecol if not indices else site.FilteredSiteCollection(
                indices, sitecol))
            for i in range(len(imts)):
                try:
                    array = numpy.array(valid.positivefloats(row[i + 2]))
                    # NB: i + 2 because the first 2 fields are tag and indices
                except:
                    raise InvalidFile(
                        'The column #%d in %s is expected to contain positive '
                        'floats, got %s instead' % (i + 3, fname, row[i + 2]))
                gmf_by_imt[imts[i]][lineno - 1] = r_sites.expand(array, 0)
            tags.append(row[0])
    if lineno < num_gmfs:
        raise InvalidFile('%s contains %d rows, expected %d' %
                          (fname, lineno, num_gmfs))
    if tags != sorted(tags):
        raise InvalidFile('The tags in %s are not ordered: %s' % (fname, tags))
    return sitecol, numpy.array(tags, '|S100'), gmf_by_imt.T
示例#6
0
def create(GmfColl,
           eb_ruptures,
           sitecol,
           imts,
           rlzs_assoc,
           trunc_level,
           correl_model,
           min_iml,
           monitor=Monitor()):
    """
    :param GmfColl: a GmfCollector class to be instantiated
    :param eb_ruptures: a list of EBRuptures with the same trt_model_id
    :param sitecol: a SiteCollection instance
    :param imts: list of IMT strings
    :param rlzs_assoc: a RlzsAssoc instance
    :param trunc_level: truncation level
    :param correl_model: correlation model instance
    :param min_iml: a dictionary of minimum intensity measure levels
    :param monitor: a monitor instance
    :returns: a GmfCollector instance
    """
    trt_id = eb_ruptures[0].trt_id
    gsims = rlzs_assoc.gsims_by_trt_id[trt_id]
    rlzs_by_gsim = rlzs_assoc.get_rlzs_by_gsim(trt_id)
    rlzs = rlzs_assoc.realizations
    ctx_mon = monitor('make contexts')
    gmf_mon = monitor('compute poes')
    sites = sitecol.complete
    samples = rlzs_assoc.samples[trt_id]
    gmfcoll = GmfColl(imts, rlzs)
    for ebr in eb_ruptures:
        rup = ebr.rupture
        with ctx_mon:
            r_sites = site.FilteredSiteCollection(ebr.indices, sites)
            computer = calc.gmf.GmfComputer(rup, r_sites, imts, gsims,
                                            trunc_level, correl_model, samples)
        with gmf_mon:
            data = computer.calcgmfs(rup.seed, ebr.events, rlzs_by_gsim,
                                     min_iml)
            for eid, imti, rlz, gmf_sids in data:
                gmfcoll.save(eid, imti, rlz, *gmf_sids)
    return gmfcoll