예제 #1
0
def compute_mmax(fname_input_pattern: str, fname_config: str, label: str):
    """
    This function assignes an mmax value to each source with a catalogue
    file as selected by the provided `fname_input_pattern`.
    """

    if isinstance(fname_input_pattern, str):
        fname_list = glob(fname_input_pattern)
    else:
        fname_list = fname_input_pattern

    # Parsing config
    model = toml.load(fname_config)

    # Processing files
    for fname in sorted(fname_list):

        src_id = _get_src_id(fname)

        # Processing catalogue
        tcat = _load_catalogue(fname)

        if tcat is None or len(tcat.data['magnitude']) < 2:
            continue

        tmp = "{:.5e}".format(np.max(tcat.data['magnitude']))
        model['sources'][src_id]['mmax_{:s}'.format(label)] = float(tmp)

    # Saving results into the config file
    with open(fname_config, 'w') as fou:
        fou.write(toml.dumps(model))
        print('Updated {:s}'.format(fname_config))
예제 #2
0
def check_mfds(fname_input_pattern: str,
               fname_config: str,
               *,
               src_id: str = None):
    """
    Given a set of .xml files and a configuration file with GR params, this
    code compares the total MFD of the sources against the original one in the
    configuration file. The ID of the source if not provided is taken from the
    name of the files (i.e., last label preceded by `_`)
    """

    for fname in sorted(glob(fname_input_pattern)):

        if src_id is None:
            src_id = _get_src_id(fname)
        model = toml.load(fname_config)

        binw = 0.1
        sourceconv = SourceConverter(investigation_time=1.0,
                                     rupture_mesh_spacing=5.0,
                                     complex_fault_mesh_spacing=5.0,
                                     width_of_mfd_bin=binw)
        ssm = to_python(fname, sourceconv)

        for grp in ssm:

            for i, src in enumerate(grp):
                if i == 0:
                    nmfd = EEvenlyDiscretizedMFD.from_mfd(src.mfd, binw)
                else:
                    ged = get_evenlyDiscretizedMFD_from_truncatedGRMFD
                    tmfd = ged(src.mfd, nmfd.bin_width)
                    nmfd.stack(tmfd)

            occ = np.array(nmfd.get_annual_occurrence_rates())

            bgr = model["sources"][src_id]["bgr_weichert"]
            agr = model["sources"][src_id]["agr_weichert"]

            tmp = occ[:, 0] - binw
            mfd = 10.0**(agr - bgr * tmp[:-1]) - 10.0**(agr - bgr *
                                                        (tmp[:-1] + binw))

            _ = plt.figure(figsize=(8, 6))
            plt.plot(occ[:, 0], occ[:, 1], 'o')
            plt.plot(tmp[:-1] + binw / 2, mfd, 'x')
            print(mfd)
            plt.title(fname)
            plt.xlabel('Magnitude')
            plt.ylabel('Annual occurrence rate')
            plt.yscale('log')

            plt.show()
예제 #3
0
def check_mfds(fname_input_pattern: str, fname_config: str):

    for fname in sorted(glob(fname_input_pattern)):

        src_id = _get_src_id(fname)
        model = toml.load(fname_config)

        binw = 0.1
        sourceconv = SourceConverter(investigation_time=1.0,
                                     rupture_mesh_spacing=5.0,
                                     complex_fault_mesh_spacing=5.0,
                                     width_of_mfd_bin=binw)
        ssm = to_python(fname, sourceconv)

        for grp in ssm:
            
            for i, src in enumerate(grp):
                if i == 0:
                    nmfd = EEvenlyDiscretizedMFD.from_mfd(src.mfd, binw)
                else:
                    tmfd = get_evenlyDiscretizedMFD_from_truncatedGRMFD(src.mfd, nmfd.bin_width)
                    nmfd.stack(tmfd)

            occ = np.array(nmfd.get_annual_occurrence_rates())

            bgr = model["sources"][src_id]["bgr_weichert"]
            agr = model["sources"][src_id]["agr_weichert"]

            tmp = occ[:,0] - binw
            mfd = 10.0**(agr-bgr*tmp[:-1])-10.0**(agr-bgr*(tmp[:-1]+binw))
            
            fig = plt.figure(figsize=(8, 6))
            plt.plot(occ[:, 0], occ[:, 1], 'o')
            plt.plot(tmp[:-1]+binw/2, mfd, 'x')
            print(mfd)
            plt.title(fname) 
            plt.xlabel('Magnitude')
            plt.ylabel('Annual occurrence rate')
            plt.yscale('log')

            plt.show()
예제 #4
0
def main(fname_input_pattern, fname_config, folder_out_figs):

    config = toml.load(fname_config)
    tmp = [1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990]
    years = np.array(tmp)
    mags = np.array([4.0, 4.5, 5, 5.5, 6, 6.5, 7])
    binw = 0.2
    ref_mag = 5.0

    # Mags in descending order
    years[::-1].sort()
    idxs = np.arange(len(mags))
    idxs[::-1].sort()
    bmin = 0.85
    bmax = 1.15
    bmin = 0.90
    bmax = 1.10

    for fname in glob.glob(fname_input_pattern):
        src_id = _get_src_id(fname)
        var = config['sources'][src_id]
        res = completeness_analysis(fname,
                                    idxs,
                                    years,
                                    mags,
                                    binw,
                                    ref_mag, [bmin, bmax],
                                    src_id,
                                    folder_out_figs,
                                    rewrite=False)
        var['completeness_table'] = list(res[3])
        var['agr_weichert'] = float('{:.4f}'.format(res[0]))
        var['bgr_weichert'] = float('{:.4f}'.format(res[1]))

    with open(fname_config, 'w') as fou:
        fou.write(toml.dumps(config))
        print('Updated {:s}'.format(fname_config))
예제 #5
0
def subcatalogues_analysis(fname_input_pattern,
                           fname_config,
                           outdir,
                           *,
                           skip=[],
                           yealim='',
                           **kwargs):
    """
    Analyze the catalogue
    """

    create_folder(outdir)

    # Parsing config
    model = toml.load(fname_config)

    # Processing files
    for fname in sorted(glob(fname_input_pattern)):

        # Get source ID
        src_id = _get_src_id(fname)
        if src_id in skip:
            continue

        # Create figure
        out = create_mtd(fname,
                         src_id,
                         None,
                         False,
                         False,
                         0.25,
                         10,
                         pmint=1900)

        if out is None:
            continue

        if len(yealim) > 0:
            tmp = yealim.split(',')
            tmp = numpy.array(tmp)
            tmp = tmp.astype(numpy.float)
            plt.xlim(tmp)

        if 'xlim' in kwargs:
            plt.xlim(kwargs['xlim'])

        if 'ylim' in kwargs:
            plt.ylim(kwargs['ylim'])

        print('src_id: {:s} '.format(src_id), end='')
        if ('sources' in model and src_id in model['sources']
                and 'completeness_table' in model['sources'][src_id]):
            print(' source specific completeness')
            ctab = numpy.array(model['sources'][src_id]['completeness_table'])
            ctab = ctab.astype(numpy.float)
        else:
            print(' default completeness')
            ctab = numpy.array(model['default']['completeness_table'])
            ctab = ctab.astype(numpy.float)

        print(ctab)
        _plot_ctab(ctab)

        ext = 'png'
        figure_fname = os.path.join(outdir,
                                    'fig_mtd_{:s}.{:s}'.format(src_id, ext))
        plt.savefig(figure_fname, format=ext)
        plt.close()
def subcatalogues_analysis(fname_input_pattern,
                           fname_config,
                           outdir,
                           skip=[],
                           **kwargs):
    """
    Analyze the catalogue
    """

    create_folder(outdir)

    # Parsing config
    model = toml.load(fname_config)

    # Processing files
    for fname in sorted(glob(fname_input_pattern)):

        # Get source ID
        src_id = _get_src_id(fname)
        if src_id in skip:
            continue

        # Create figure
        out = create_mtd(fname,
                         src_id,
                         None,
                         False,
                         False,
                         0.5,
                         10,
                         pmint=1900)

        if out is None:
            continue

        if 'xlim' in kwargs:
            plt.xlim(kwargs['xlim'])

        if 'ylim' in kwargs:
            plt.ylim(kwargs['ylim'])

        print('src_id:', src_id)
        if ('sources' in model
                and 'completeness_table' in model['sources'][src_id]):
            ctab = numpy.array(model['sources'][src_id]['completeness_table'])
        else:
            ctab = numpy.array(model['default']['completeness_table'])

        n = len(ctab)
        for i in range(0, n - 1):
            plt.plot([ctab[i, 0], ctab[i, 0]], [ctab[i, 1], ctab[i + 1, 1]],
                     '-r')
            plt.plot([ctab[i, 0], ctab[i + 1, 0]],
                     [ctab[i + 1, 1], ctab[i + 1, 1]], '-r')

        ylim = plt.gca().get_ylim()
        xlim = plt.gca().get_xlim()

        plt.plot([ctab[n - 1, 0], ctab[n - 1, 0]], [ylim[1], ctab[n - 1, 1]],
                 '-r')
        plt.plot([ctab[0, 0], xlim[1]], [ctab[0, 1], ctab[0, 1]], '-r')

        ext = 'png'
        figure_fname = os.path.join(outdir,
                                    'fig_mtd_{:s}.{:s}'.format(src_id, ext))
        plt.savefig(figure_fname, format=ext)
        plt.close()
예제 #7
0
def create_nrml_sources(fname_input_pattern: str, fname_config: str, 
                        folder_out: str, fname_subzone_shp: str="", 
                        fname_subzone_config: str=""):

    create_folder(folder_out)
    
    # If true we take some of the information from subzones
    subzones = (len(fname_subzone_shp) > 0 and len(fname_subzone_config) > 0)
    if subzones:
        polygons_gdf = gpd.read_file(fname_subzone_shp)
        model_subz = toml.load(fname_subzone_config) 

    # This is used to instantiate the MSR
    module = importlib.import_module('openquake.hazardlib.scalerel')

    # Parsing config
    model = toml.load(fname_config)

    rms = model['rupture_mesh_spacing']
    mmin = model['mmin']
    bwid = model['bin_width']
    tom = PoissonTOM(1.0)

    # Processing files
    for fname in glob(fname_input_pattern):

        src_id = os.path.basename(fname).split('.')[0]
        rc_id = _get_src_id(fname)
        
        df = pd.read_csv(fname)
        
        # Create a geodataframe with the points in a given zone
        if subzones:

            # Create a geodataframe with points
            geom = [PointShapely(xy) for xy in zip(df.lon, df.lat)]
            gdf = gpd.GeoDataFrame(df, crs='epsg:4326', geometry=geom)

            # Select subzones within a zone
            tdf = polygons_gdf[polygons_gdf["parent"] == src_id]

            # Should contain the points within
            df = gpd.sjoin(gdf, tdf, op='within')

        # This is the information on the source in the config file
        srcd = model['sources'][src_id]

        # Looping over the points
        srcs = []
        for idx, pnt in df.iterrows():

            if subzones:
                srcd_sz = model_subz['sources'][pnt.id]

            pfx = model.get("source_prefix", "")
            pfx += "_" if len(pfx) else pfx
            sid = '{:s}{:s}_{:d}'.format(pfx, src_id, idx)
            name = ""

            trt = srcd['tectonic_region_type']
            msr_str = model['msr'][trt]

            my_class = getattr(module, msr_str)
            msr = my_class()

            # Get mmax and set the MFD
            mmx = srcd['mmax']
            mfd = TruncatedGRMFD(mmin, mmx, bwid, pnt.agr, pnt.bgr)

            key = 'rupture_aspect_ratio'
            rar = get_param(srcd, model['default'], key)

            key = 'upper_seismogenic_depth'
            usd = get_param(srcd, model['default'], key)

            key = 'lower_seismogenic_depth'
            lsd = get_param(srcd, model['default'], key)

            key = 'nodal_plane_distribution'
            tmp = get_param(srcd, model['default'], key)
            npd = _get_nodal_plane_distribution(tmp)

            key = 'hypocenter_distribution'
            tmp = get_param(srcd, model['default'], key)
            hyd = _get_hypocenter_distribution(tmp)

            if subzones:
                tmp = get_param(srcd_sz, model['default'], key)
                npd = _get_nodal_plane_distribution(tmp)

            loc = Point(pnt.lon, pnt.lat)
            src = PointSource(sid, name, trt, mfd, rms, msr, rar, tom,
                              usd, lsd, loc, npd, hyd)
            srcs.append(src)

        # Write output file
        fname_out = os.path.join(folder_out, 'src_{:s}.xml'.format(src_id))
        write_source_model(fname_out, srcs, 'Zone {:s}'.format(src_id))
예제 #8
0
def compute_a_value(fname_input_pattern: str,
                    bval: float,
                    fname_config: str,
                    folder_out: str,
                    use: str = '',
                    folder_out_figs: str = None,
                    plt_show=False):
    """
    This function assignes an a-value to each source with a file selected by
    the provided `fname_input_pattern`.
    """

    if len(use) > 0:
        use = get_list(use)

    # Processing input parameters
    bval = float(bval)
    if folder_out is not None:
        create_folder(folder_out)
    if folder_out_figs is not None:
        create_folder(folder_out_figs)

    if isinstance(fname_input_pattern, str):
        fname_list = glob(fname_input_pattern)
    else:
        fname_list = fname_input_pattern

    # Parsing config
    model = toml.load(fname_config)
    binw = model['bin_width']

    # Processing files
    for fname in sorted(fname_list):

        # Get source ID
        src_id = _get_src_id(fname)
        if len(use) > 0 and src_id not in use:
            continue
        print(fname)

        mmax, ctab = get_mmax_ctab(model, src_id)

        # Processing catalogue
        tcat = _load_catalogue(fname)

        if tcat is None or len(tcat.data['magnitude']) < 2:
            continue

        # Completeness analysis
        tcat = _add_defaults(tcat)
        tcat.data["dtime"] = tcat.get_decimal_time()
        try:
            cent_mag, t_per, n_obs = get_completeness_counts(tcat, ctab, binw)
            if cent_mag is None:
                print('   Completeness analysis failed')
                continue
        except ValueError:
            print('   Completeness analysis failed')
            continue

        df = pd.DataFrame()
        df['mag'] = cent_mag
        df['deltaT'] = t_per
        df['nobs'] = n_obs
        fout = os.path.join(folder_out, 'occ_count_zone_{:s}'.format(src_id))
        df.to_csv(fout, index=False)

        # Computing GR a
        if 'sources' not in model:
            model['sources'] = {}
        if src_id not in model['sources']:
            model['sources'][src_id] = {}

        exrs = get_exrs(df, bval)
        aval = get_agr(df.mag[0] - binw / 2, bval, exrs[0])

        tmp = "{:.5e}".format(aval)
        model['sources'][src_id]['agr_counting'] = float(tmp)

        tmp = "{:.5e}".format(bval)
        model['sources'][src_id]['bgr_counting'] = float(tmp)

        gwci = get_weichert_confidence_intervals
        lcl, ucl, ex_rates, ex_rates_scaled = gwci(cent_mag, n_obs, t_per,
                                                   bval)

        _ = plt.figure()
        ax = plt.gca()
        plt.plot(cent_mag, n_obs / t_per, 'o', markerfacecolor='none')
        plt.plot(cent_mag - binw / 2,
                 ex_rates_scaled,
                 's',
                 markerfacecolor='none',
                 color='red')

        plt.plot(cent_mag - binw / 2, lcl, '--', color='black')
        plt.plot(cent_mag - binw / 2, ucl, '--', color='black')

        xmag = numpy.arange(cent_mag[0] - binw / 2, mmax - 0.01 * binw,
                            binw / 2)
        exra = (10.0**(aval - bval * xmag) - 10.0**(aval - bval * mmax))
        plt.plot(xmag, exra, '--', lw=3, color='green')

        plt.yscale('log')
        plt.xlabel('Magnitude')
        plt.ylabel('Annual rate of exceedance')
        plt.text(0.75,
                 0.95,
                 'Fixed b_GR = {:.2f}'.format(bval),
                 transform=ax.transAxes)
        plt.grid(which='major', color='grey')
        plt.grid(which='minor', linestyle='--', color='lightgrey')
        plt.title(src_id)

        if plt_show:
            plt.show()

        # Saving figures
        if folder_out_figs is not None:
            ext = 'png'
            fmt = 'fig_mfd_{:s}.{:s}'
            figure_fname = os.path.join(folder_out_figs,
                                        fmt.format(src_id, ext))

            plt.savefig(figure_fname, format=ext)
            plt.close()

    # Saving results into the config file
    with open(fname_config, 'w') as fou:
        fou.write(toml.dumps(model))
        print('Updated {:s}'.format(fname_config))
예제 #9
0
def weichert_analysis(fname_input_pattern,
                      fname_config,
                      folder_out=None,
                      folder_out_figs=None,
                      skip=[],
                      binw=None,
                      plt_show=False):
    """
    Computes GR parameters for a set of catalogues stored in a .csv file

    :param fname_input_pattern:
        It can be either a string (definining a pattern) or a list of
        .csv files. The file names must have the source ID at the end. The
        delimiter of the source ID on the left is `_`
    :param fname_config:
        The name of the .toml configuration file
    :param folder_out:
        The folder where to store the files with the counting of occurrences
    :param folder_out_figs:
        The folder where to store the figures
    :param skip:
        A list with the IDs of the sources to skip
    """

    if folder_out is not None:
        create_folder(folder_out)
    if folder_out_figs is not None:
        create_folder(folder_out_figs)

    # Parsing config
    if fname_config is not None:
        model = toml.load(fname_config)

    if binw is None and fname_config is not None:
        binw = model['bin_width']
    else:
        binw = 0.1

    if isinstance(fname_input_pattern, str):
        fname_list = [f for f in glob(fname_input_pattern)]
    else:
        fname_list = fname_input_pattern

    # Processing files
    for fname in sorted(fname_list):

        print(fname)

        # Get source ID
        src_id = _get_src_id(fname)
        if src_id in skip:
            print("   skipping")
            continue

        if 'sources' in model:
            if (src_id in model['sources']
                    and 'mmax' in model['sources'][src_id]):
                mmax = model['sources'][src_id]['mmax']
            else:
                mmax = model['default']['mmax']
            if (src_id in model['sources']
                    and 'completeness_table' in model['sources'][src_id]):
                key_tmp = 'completeness_table'
                ctab = numpy.array(model['sources'][src_id][key_tmp])
                print('Using source specific completeness')
            else:
                ctab = numpy.array(model['default']['completeness_table'])
        else:
            mmax = model['default']['mmax']
            ctab = numpy.array(model['default']['completeness_table'])

        # Processing catalogue
        tcat = _load_catalogue(fname)

        if tcat is None or len(tcat.data['magnitude']) < 2:
            print('    Source {:s} has less than 2 eqks'.format(src_id))
            continue

        tcat.data["dtime"] = tcat.get_decimal_time()
        cent_mag, t_per, n_obs = get_completeness_counts(tcat, ctab, binw)

        if folder_out is not None:
            df = pd.DataFrame()
            df['mag'] = cent_mag
            df['deltaT'] = t_per
            df['nobs'] = n_obs
            fmt = 'occ_count_zone_{:s}'
            fout = os.path.join(folder_out, fmt.format(src_id))
            df.to_csv(fout, index=False)

        # Computing GR a and b
        tcat = _add_defaults(tcat)
        weichert_config = {
            'magnitude_interval': binw,
            'reference_magnitude': 0.0
        }
        weichert = Weichert()
        bval_wei, sigmab, aval_wei, sigmaa = weichert.calculate(
            tcat, weichert_config, ctab)

        # Computing confidence intervals
        gwci = get_weichert_confidence_intervals
        lcl, ucl, ex_rates, ex_rates_scaled = gwci(cent_mag, n_obs, t_per,
                                                   bval_wei)

        if 'sources' not in model:
            model['sources'] = {}
        if src_id not in model['sources']:
            model['sources'][src_id] = {}

        tmp = "{:.5e}".format(aval_wei)
        model['sources'][src_id]['agr_weichert'] = float(tmp)
        tmp = "{:.3f}".format(bval_wei)
        model['sources'][src_id]['bgr_weichert'] = float(tmp)

        _ = plt.figure()
        ax = plt.gca()
        plt.plot(cent_mag, n_obs / t_per, 'o', markerfacecolor='none')
        plt.plot(cent_mag - binw / 2,
                 ex_rates_scaled,
                 's',
                 markerfacecolor='none',
                 color='red')

        plt.plot(cent_mag - binw / 2, lcl, '--', color='darkgrey')
        plt.plot(cent_mag - binw / 2, ucl, '--', color='darkgrey')

        xmag = numpy.arange(cent_mag[0] - binw / 2, mmax - 0.01 * binw,
                            binw / 2)
        exra = (10.0**(aval_wei - bval_wei * xmag) -
                10.0**(aval_wei - bval_wei * mmax))
        plt.plot(xmag, exra, '--', lw=3, color='green')

        plt.yscale('log')
        plt.xlabel('Magnitude')
        plt.ylabel('Annual rate of exceedance')
        plt.text(0.75,
                 0.95,
                 'b_GR = {:.2f}'.format(bval_wei),
                 transform=ax.transAxes)
        plt.grid(which='major', color='grey')
        plt.grid(which='minor', linestyle='--', color='lightgrey')
        plt.title(src_id)

        if plt_show:
            plt.show()

        # Saving figures
        if folder_out_figs is not None:
            ext = 'png'
            fmt = 'fig_mfd_{:s}.{:s}'
            figure_fname = os.path.join(folder_out_figs,
                                        fmt.format(src_id, ext))

            plt.savefig(figure_fname, format=ext)
            plt.close()

    # Saving results into the config file
    if fname_config is not None:
        with open(fname_config, 'w') as f:
            f.write(toml.dumps(model))
            print('Updated {:s}'.format(fname_config))
예제 #10
0
def process_gcmt_datafames(fname_folder: str, folder_out: str):
    """
    :param fnames:
        A list containing the names of the files to be processed or a pattern
    :param folder_out:
        The name of the output folder
    """

    create_folder(folder_out)

    if isinstance(fname_folder, str):
        fnames = [f for f in glob(fname_folder)]
    else:
        fnames = fname_folder

    for fname in fnames:

        df = pd.read_csv(fname)
        if len(df.dip1) < 1:
            continue

        # See https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/gridspec_nested.html
        f = plt.figure(figsize=(15, 15))
        gs0 = gridspec.GridSpec(2, 2, figure=f)
        src_id = _get_src_id(fname)

        ext = "png"
        fmt = "zone_{:s}.{:s}"
        figure_name = os.path.join(folder_out, fmt.format(src_id, ext))

        fmclassification = {}
        eventfm = {}
        dip_1 = {}
        dip_2 = {}
        strike_1 = {}
        strike_2 = {}
        for idx, row in df.iterrows():

            plungeb = row.loc['plunge_b']
            plungep = row['plunge_p']
            plunget = row['plunge_t']
            mclass = mecclass(plunget, plungeb, plungep)
            eventfm[idx] = mclass
            if mclass in fmclassification:
                fmclassification[mclass] += 1
                dip_1[mclass].append(row['dip1'])
                dip_2[mclass].append(row['dip2'])
                strike_1[mclass].append(row['strike1'])
                strike_2[mclass].append(row['strike2'])
            else:
                fmclassification[mclass] = 1
                dip_1[mclass] = [row['dip1']]
                dip_2[mclass] = [row['dip2']]
                strike_1[mclass] = [row['strike1']]
                strike_2[mclass] = [row['strike2']]

        title = "Source: {:s}".format(src_id)
        _ = plot_histogram(gs0[0, 0], fmclassification, title)
        plot_xx(gs0[0, 1], dip_1, dip_2, strike_1, strike_2)

        stk1 = get_simpler(strike_1)
        stk2 = get_simpler(strike_2)
        dip1 = get_simpler(dip_1)
        dip2 = get_simpler(dip_2)
        plot_yy(gs0[1, 0], dip1, dip2, stk1, stk2)

        plot_density_simple(gs0[1, 1], dip1, dip2, stk1, stk2)

        plt.savefig(figure_name, format=ext)
        plt.close()

    return fmclassification