Ejemplo n.º 1
0
 def __call__(self, ltmodel, apply_unc, fname, fileno, monitor):
     fname_hits = collections.Counter()  # fname -> number of calls
     mags = set()
     src_groups = []
     [sm] = nrml.read_source_models([fname], self.converter, monitor)
     newsm = self.makesm(fname, sm, apply_unc)
     fname_hits[fname] += 1
     for sg in newsm:
         # sample a source for each group
         if os.environ.get('OQ_SAMPLE_SOURCES'):
             sg.sources = random_filtered_sources(
                 sg.sources, self.srcfilter, sg.id)
         sg.info = numpy.zeros(len(sg), source_info_dt)
         for i, src in enumerate(sg):
             if hasattr(src, 'data'):  # nonparametric
                 srcmags = ['%.3f' % item[0].mag for item in src.data]
             else:
                 srcmags = ['%.3f' % item[0] for item in
                            src.get_annual_occurrence_rates()]
             mags.update(srcmags)
             dic = {k: v for k, v in vars(src).items()
                    if k != 'id' and k != 'src_group_id'}
             src.checksum = zlib.adler32(
                 pickle.dumps(dic, pickle.HIGHEST_PROTOCOL))
             sg.info[i] = (ltmodel.ordinal, 0, src.source_id,
                           src.code, src.num_ruptures, 0, 0, 0,
                           src.checksum, src.wkt())
         src_groups.append(sg)
     return dict(fname_hits=fname_hits, changes=newsm.changes,
                 src_groups=src_groups, mags=mags,
                 ordinal=ltmodel.ordinal, fileno=fileno)
Ejemplo n.º 2
0
def read_source_model(fname, converter, monitor):
    """
    :param fname: path to a source model XML file
    :param converter: SourceConverter
    :param monitor: a Monitor instance
    :returns: a SourceModel instance
    """
    [sm] = nrml.read_source_models([fname], converter)
    return {fname: sm}
Ejemplo n.º 3
0
def read_source_model(fname, converter, srcfilter, monitor):
    """
    :param fname: path to a source model XML file
    :param converter: SourceConverter
    :param srcfilter: None unless OQ_SAMPLE_SOURCES is set
    :param monitor: a Monitor instance
    :returns: a SourceModel instance
    """
    [sm] = nrml.read_source_models([fname], converter)
    if srcfilter:  # if OQ_SAMPLE_SOURCES is set sample the close sources
        for i, sg in enumerate(sm.src_groups):
            sg.sources = random_filtered_sources(sg.sources, srcfilter, i)
    return {fname: sm}
Ejemplo n.º 4
0
def read_input(hparams, **extra):
    """
    :param hparams: a dictionary of hazard parameters
    :returns: an Input namedtuple (groups, sitecol, gsim_lt, cmakerdict)

    The dictionary must contain the keys

    - "maximum_distance"
    - "imtls"
    - "source_model_file" or "rupture_model_file"
    - "sites" or "site_model_file"
    - "gsim" or "gsim_logic_tree_file"

    Moreover:

    - if "source_model_file" is given, then "investigation_time" is mandatory
    - if "rupture_model_file" is given, the "number_of_ground_motion_fields"
      and "ses_seed" are mandatory
    - if there is an area source, then "area_source_discretization" is needed
    - if  "site_model_file" is missing, then global site parameters are needed

    The optional keys include

    - "rupture_mesh_spacing" (default 5.)
    - "complex_fault_mesh_spacing" (default rupture_mesh_spacing)
    - "width_of_mfd_bin" (default 1.)
    - "minimum_magnitude"
    - "discard_trts" (default "")
    - "number_of_logic_tree_samples" (default 0)
    - "ses_per_logic_tree_path" (default 1)
    """
    if isinstance(hparams, str):
        hparams = read_hparams(hparams)
    if extra:
        hparams = hparams.copy()
        hparams.update(extra)
    assert 'imts' in hparams or 'imtls' in hparams
    assert isinstance(hparams['maximum_distance'], IntegrationDistance)
    smfname = hparams.get('source_model_file')
    if smfname:  # nonscenario
        itime = hparams['investigation_time']
    else:
        itime = 50.  # ignored in scenario
    rmfname = hparams.get('rupture_model_file')
    if rmfname:
        ngmfs = hparams["number_of_ground_motion_fields"]
        ses_seed = hparams["ses_seed"]
    converter = sourceconverter.SourceConverter(
        itime,
        hparams.get('rupture_mesh_spacing', 5.),
        hparams.get('complex_fault_mesh_spacing'),
        hparams.get('width_of_mfd_bin', 1.0),
        hparams.get('area_source_discretization'),
        hparams.get('minimum_magnitude', {'default': 0}),
        hparams.get('source_id'),
        discard_trts=hparams.get('discard_trts', ''))
    if smfname:
        [sm] = nrml.read_source_models([smfname], converter)
        groups = sm.src_groups
    elif rmfname:
        ebrs = _get_ebruptures(rmfname, converter, ses_seed)
        groups = _rupture_groups(ebrs)
    else:
        raise KeyError('Missing source_model_file or rupture_file')
    trts = set(grp.trt for grp in groups)
    if 'gsim' in hparams:
        gslt = gsim_lt.GsimLogicTree.from_(hparams['gsim'])
    else:
        gslt = gsim_lt.GsimLogicTree(hparams['gsim_logic_tree_file'], trts)

    # fix source attributes
    idx = 0
    num_rlzs = gslt.get_num_paths()
    for grp_id, sg in enumerate(groups):
        assert len(sg)  # sanity check
        for src in sg:
            src.id = idx
            src.grp_id = grp_id
            src.trt_smr = grp_id
            src.samples = num_rlzs
            idx += 1

    cmakerdict = {}  # trt => cmaker
    start = 0
    n = hparams.get('number_of_logic_tree_samples', 0)
    s = hparams.get('random_seed', 42)
    for trt, rlzs_by_gsim in gslt.get_rlzs_by_gsim_trt(n, s).items():
        cmakerdict[trt] = contexts.ContextMaker(trt, rlzs_by_gsim, hparams)
        cmakerdict[trt].start = start
        start += len(rlzs_by_gsim)
    if rmfname:
        # for instance for 2 TRTs with 5x2 GSIMs and ngmfs=10, then the
        # number of occupation is 100 for each rupture, for a total
        # of 200 events, see scenario/case_13
        nrlzs = gslt.get_num_paths()
        for grp in groups:
            for ebr in grp:
                ebr.n_occ = ngmfs * nrlzs

    sitecol = _get_sitecol(hparams, gslt.req_site_params)
    return Input(groups, sitecol, gslt, cmakerdict)