Пример #1
0
def get_mags_rates(source_model_fname, time_span):
    """
    :param str source_model_fname:
        The name of the xml shapefile
    :param float time_span:
        In years
    """
    #
    # read the source_model
    src_model, info = read(source_model_fname, False)
    #
    # process sources
    rate = 0.
    mag = None
    for src in src_model:
        if isinstance(src, NonParametricSeismicSource):
            for dat in src.data:
                rupture = dat[0]
                pmf = dat[1].data
                rate += pmf[1][0]
                if mag is None:
                    mag = rupture.mag
                else:
                    assert abs(mag-rupture.mag) < 1e-2
    return mag, rate
Пример #2
0
def xml_vs_mfd(source_id, source_model_fname, model_id,
               oqmbt_project_fname):
    """
    :param str source_id:
        The ID of the source to be analysed
    :param str source_model_fname:
        The name of the xml shapefile
    :param str model_id:
        The model ID
    """
    #
    # read the source_model
    src_model, info = read(source_model_fname)
    #
    # compute total mfd sources
    tmfd = get_total_mfd(src_model)
    #
    # read project
    oqtkp = OQtProject.load_from_file(oqmbt_project_fname)
    model_id = oqtkp.active_model_id
    model = oqtkp.models[model_id]
    #
    # get source mfd
    src = model.sources[source_id]
    mfd = src.mfd
    if isinstance(src.mfd, TruncatedGRMFD):
        mfd = get_evenlyDiscretizedMFD_from_truncatedGRMFD(mfd)
    #
    # compute total mfd sources
    plt.figure(figsize=(10, 8))
    plot_mfd_cumulative(tmfd)
    plot_mfd_cumulative(mfd, title=source_model_fname)
Пример #3
0
def get_mags_rates(source_model_fname: str, time_span: float):
    """
    This computes the total rate for non-paramteric source modelling the
    occurrence of a single magnitude value.

    :param str source_model_fname:
        The name of the xml shapefile
    :param float time_span:
        The time in years to which the probability of occurrence refers to
    :returns:
        A tuple with two floats. The magnitude modelled and the corresponding
        total annual rate of occurrence.
    """

    # Read the source_model
    src_model, _ = read(source_model_fname, False)

    # Process sources
    rate = 0.
    mag = None
    for src in src_model:
        if isinstance(src, NonParametricSeismicSource):
            for dat in src.data:
                rupture = dat[0]
                pmf = dat[1].data
                rate += pmf[1][0]
                if mag is None:
                    mag = rupture.mag
                else:
                    assert abs(mag - rupture.mag) < 1e-2
    return mag, rate / time_span
Пример #4
0
def mfd_from_xml(source_model_fname):
    """
    :param str source_model_fname:
        The name of the xml shapefile
    """
    #
    # read the source_model
    src_model, info = read(source_model_fname)
    #
    # compute total mfd sources
    return get_total_mfd(src_model)
Пример #5
0
def read_faults(faults_xml_filename=None):
    """
    Reads the information on faults included in an .xml file

    :parameter faults_xml_filename:
        The name of the .xml file with the faults
    """
    #
    # loading project
    project_pickle_filename = os.environ.get('OQMBT_PROJECT')
    oqtkp = OQtProject.load_from_file(project_pickle_filename)
    model_id = oqtkp.active_model_id
    model = oqtkp.models[model_id]
    if faults_xml_filename is None:
        fname = getattr(model, 'faults_xml_filename')
        faults_xml_filename = os.path.join(oqtkp.directory, fname)
    #
    # read .xml file content
    sources, _ = read(faults_xml_filename)
    #
    # save the information
    for f in sources:
        #
        # fixing the id of the fault source
        sid = str(f.source_id)
        if not re.search('^fs_', sid):
            sid = 'fs_{:s}'.format(sid)
        if isinstance(f, SimpleFaultSource):
            src = OQtSource(sid, 'SimpleFaultSource')
            src.trace = f.fault_trace
            src.msr = f.magnitude_scaling_relationship
            src.mfd = f.mfd
            src.rupture_aspect_ratio = f.rupture_aspect_ratio
            src.trt = f.tectonic_region_type
            src.dip = f.dip
            src.upper_seismogenic_depth = f.upper_seismogenic_depth
            src.lower_seismogenic_depth = f.lower_seismogenic_depth
            src.name = f.name
            src.rake = f.rake
            model.sources[sid] = src
        else:
            raise ValueError('Unsupported fault type')
    #
    # save the project
    oqtkp.models[model_id] = model
    oqtkp.save()
Пример #6
0
    def testcase01(self):
        """ Test case 1"""

        # Bin width
        bwdt = 0.1
        time = 500000

        folder = tempfile.mkdtemp()
        fname_ssm = os.path.join(BASE_DATA_PATH, 'ssm01.xml')
        fname_ini = os.path.join(BASE_DATA_PATH, 'job.ini')
        tmps = 'if [ -d "${:s}" ]; then rm -Rf $WORKING_DIR; fi'
        command = tmps.format(folder)
        subprocess.run(command, shell=True)
        command = 'oq engine --run {:s} --exports csv -p export_dir={}'.format(
            fname_ini, folder)
        subprocess.run(command, shell=True)
        fname_ses = get_fname(folder, 'ruptures_*.csv')

        # Read catalogue
        cat = csv.get_catalogue_from_ses(fname_ses, time)

        # Find minimum and maximum magnitude
        mmin = numpy.floor(min(cat.data['magnitude']) / bwdt) * bwdt
        mmax = numpy.ceil(
            max(cat.data['magnitude']) / bwdt) * bwdt + bwdt * 0.05

        # Compute MFD from the ses catalogue
        cm_ses = numpy.arange(mmin, mmax, bwdt)
        nobs_ses, _ = numpy.histogram(cat.data['magnitude'], cm_ses)

        # Create total MFD
        ssm, _ = model.read(fname_ssm)
        cnt = 0
        occ_tot = 0
        for src in ssm:
            if cnt == 0:
                mfd = EEvenlyDiscretizedMFD.from_mfd(src.mfd, bwdt)
            else:
                mfd.stack(src.mfd)
            tmp = numpy.array(src.mfd.get_annual_occurrence_rates())
            occ_tot += sum(tmp[:, 1])
            cnt += 1

        mfdocc = numpy.array(mfd.get_annual_occurrence_rates())

        expected = occ_tot
        computed = cat.get_number_events() / time

        ratio = abs(computed / expected)
        msg = 'Total rates do not match. Ratio {:f}'.format(ratio)
        self.assertTrue(abs(ratio - 1.) < 1e-2, msg)

        if False:
            import matplotlib.pyplot as plt
            plt.plot(mfdocc[:, 0], mfdocc[:, 1], 'x-', label='ses')
            plt.plot(cm_ses[:-1] + bwdt / 2, nobs_ses / time, 'o-')
            plt.yscale('log')
            plt.grid()
            plt.legend()
            plt.show()

        shutil.rmtree(folder)
Пример #7
0
 def test_read_source_model(self):
     """ read simple source model """
     fname = os.path.join(BASE_DATA_PATH, 'data', 'model',
                          'source_model.xml')
     srcs, _ = model.read(fname)
     self.assertEqual(len(srcs), 1)