예제 #1
0
    def main(env, rundir_path):

        conf = env.get_config()

        for starting_point in ['mean', 'random', 'excentricity_compensated']:

            config_path = 'config/starting_point_test.gronf'

            mod_conf = conf.clone()
            mod_conf.set_elements(
                'problem_config.name_template',
                'starting_point_test_%s_${event_name}' % starting_point)
            mod_conf.set_elements('analyser_configs[:].niterations', 20)
            mod_conf.set_elements(
                'optimiser_config.sampler_phases[:].niterations', 20)
            mod_conf.set_elements('optimiser_config.nbootstrap', 10)

            mod_conf.set_elements(
                'optimiser_config.sampler_phases[1].starting_point',
                starting_point)

            mod_conf.set_basepath(conf.get_basepath())
            config.write_config(mod_conf, config_path)

            grond('go', config_path)
            for rundir in glob.glob('runs/starting_point_test_*'):
                grond('plot', 'acceptance', rundir)
예제 #2
0
def test_scenario():
    playground_dir = common.get_playground_dir()
    common.get_test_data('gf_stores/crust2_ib/')
    common.get_test_data('gf_stores/crust2_ib_static/')
    gf_stores_path = common.test_data_path('gf_stores')

    with chdir(playground_dir):
        scenario_dir = 'scenario'
        if os.path.exists(scenario_dir):
            shutil.rmtree(scenario_dir)

        grond('scenario', '--targets=waveforms,insar,gnss', '--nevents=2',
              '--nstations=3', '--gf-store-superdirs=%s' % gf_stores_path,
              scenario_dir)

        with chdir(scenario_dir):
            config_path = 'config/scenario.gronf'
            quick_config_path = 'config/scenario_quick.gronf'
            event_names = grond('events', config_path).strip().split('\n')

            env = Environment([config_path] + event_names)
            conf = env.get_config()

            mod_conf = conf.clone()
            mod_conf.set_elements(
                'analyser_configs[:].niterations', 100)
            mod_conf.set_elements(
                'optimiser_config.sampler_phases[:].niterations', 100)
            mod_conf.set_elements(
                'optimiser_config.nbootstrap', 5)
            mod_conf.set_basepath(conf.get_basepath())
            config.write_config(mod_conf, quick_config_path)
            grond('diff', config_path, quick_config_path)
            grond('check', quick_config_path, *event_names)

            grond('go', quick_config_path, *event_names)
            rundir_paths = common.get_rundir_paths(config_path, event_names)
            grond('report', *rundir_paths)

            grond(
                'go', quick_config_path,
                '--parallel=2',
                '--force',
                *event_names)
            rundir_paths = common.get_rundir_paths(config_path, event_names)
            grond('report', '--parallel=2', *rundir_paths)
예제 #3
0
def run_example(project_name, config_path, quick_config_path, event_name):
    project_dir = project_name
    if os.path.exists(project_dir):
        shutil.rmtree(project_dir)

    grond('init', project_name, project_dir)
    with chdir(project_dir):
        assert os.path.isdir('config')

        common.link_test_data('events/%s/' % event_name,
                              'data/events/%s/' % event_name)

        env = Environment([config_path, event_name])
        conf = env.get_config()

        store_ids = conf.get_elements('target_groups[:].store_id')
        for store_id in store_ids:
            store_path = 'gf_stores/%s/' % store_id
            if not os.path.exists(store_path):
                common.link_test_data(store_path)

        problem_name = env.get_problem().name
        rundir_path = expand_template(conf.rundir_template,
                                      dict(problem_name=problem_name))

        grond('check', config_path, event_name,
              '--save-stations-used=used_stations.txt')

        sorted(s.station for s in model.load_stations('used_stations.txt'))

        mod_conf = conf.clone()
        mod_conf.set_elements('analyser_configs[:].niterations', 100)
        mod_conf.set_elements('optimiser_config.sampler_phases[:].niterations',
                              100)
        mod_conf.set_elements('optimiser_config.nbootstrap', 10)
        mod_conf.set_basepath(conf.get_basepath())
        config.write_config(mod_conf, quick_config_path)
        grond('go', quick_config_path, event_name)
        grond('harvest', '--force', '--export-fits=best,mean', rundir_path)
        grond('report', rundir_path)
예제 #4
0
 def write_config(self):
     write_config(self.get_config(), self.get_config_path())
예제 #5
0
def test_joint_locate():
    playground_dir = common.get_playground_dir()
    common.get_test_data('gf_stores/crust2_ib/')
    gf_stores_path = common.test_data_path('gf_stores')

    with chdir(playground_dir):
        scenario_dir = 'scenario_joint_locate'
        if os.path.exists(scenario_dir):
            shutil.rmtree(scenario_dir)

        grond('scenario', '--targets=waveforms', '--nevents=1',
              '--nstations=6', '--gf-store-superdirs=%s' % gf_stores_path,
              '--no-map', scenario_dir)

        with chdir(scenario_dir):
            config_path = 'config/scenario.gronf'
            quick_config_path_templ = 'config/scenario_quick_%i.gronf'
            event_names = grond('events', config_path).strip().split('\n')

            env = Environment([config_path] + event_names)
            conf = env.get_config()

            for irun, pick_weight in enumerate([0.5, 1.0, 2.0]):
                quick_config_path = quick_config_path_templ % irun

                mod_conf = conf.clone()
                target_groups = guts.load_all(string='''
--- !grond.PhasePickTargetGroup
normalisation_family: picks
path: pick.p
weight: 1.0
store_id: crust2_ib
distance_min: 10000.0
distance_max: 1000000.0
pick_synthetic_traveltime: '{stored:any_P}'
pick_phasename: 'any_P'
--- !grond.PhasePickTargetGroup
normalisation_family: picks
path: pick.s
weight: 1.0
store_id: crust2_ib
distance_min: 10000.0
distance_max: 1000000.0
pick_synthetic_traveltime: '{stored:any_S}'
pick_phasename: 'any_S'
''')

                mod_conf.problem_config.name_template \
                    = 'cmt_${event_name}_%i' % irun

                mod_conf.dataset_config.picks_paths = [
                    'data/scenario/picks/picks.markers'
                ]

                mod_conf.target_groups.extend(target_groups)

                mod_conf.set_elements('target_groups[-2:].weight', pick_weight)

                # mod_conf.set_elements(
                #     'analyser_configs[:].niterations', 100)
                # mod_conf.set_elements(
                #     'optimiser_config.sampler_phases[:].niterations', 100)
                # mod_conf.set_elements(
                #     'optimiser_config.nbootstrap', 5)

                mod_conf.optimiser_config.sampler_phases[-1].niterations \
                    = 10000

                mod_conf.set_elements(
                    'optimiser_config.sampler_phases[:].seed', 23)

                mod_conf.set_basepath(conf.get_basepath())
                config.write_config(mod_conf, quick_config_path)

                grond('diff', config_path, quick_config_path)
                grond('check', quick_config_path, *event_names)

                grond('go', quick_config_path, *event_names)
                rundir_paths = common.get_rundir_paths(quick_config_path,
                                                       event_names)
                grond('report', *rundir_paths)
예제 #6
0
def run_grond(rundir,
              datafolder,
              eventname,
              store_id,
              domain="time_domain",
              problem_type="CMTProblem"):

    if domain == "envelope":
        problem_type = "magnitude"
    km = 1000.
    ds = grond.Dataset(event_name=eventname)
    ds.add_waveforms(paths=['%s' % datafolder])
    ds.add_events(filename='%s/event.txt' % datafolder)
    ds.add_stations(stationxml_filenames=['%s/stations.xml' % datafolder])
    ds.add_responses(stationxml_filenames=['%s/stations.xml' % datafolder])

    ds._event_name = eventname
    print(eventname)
    ds.add_blacklist([])
    ds.empty_cache()

    quantity = 'displacement'
    group = 'all'
    tmin = '{stored:begin}+0.1'
    tmax = '{stored:begin}+2.5'
    fmin = 1
    fmax = 13.
    ffactor = 1.
    engine = gf.LocalEngine(store_superdirs=['/home/asteinbe/gf_stores'])
    gf_interpolation = 'nearest_neighbor'
    imc_Z = grond.WaveformMisfitConfig(fmin=fmin,
                                       fmax=fmax,
                                       ffactor=ffactor,
                                       tmin=tmin,
                                       tmax=tmax,
                                       norm_exponent=1,
                                       domain=domain,
                                       quantity=quantity)
    cha_Z = 'Z'

    imc_T = grond.WaveformMisfitConfig(
        fmin=fmin,
        fmax=fmax,
        ffactor=ffactor,
        tmin=tmin,
        tmax=tmax,
        norm_exponent=1,
        domain=domain,
        quantity=quantity,
    )
    cha_T = 'T'

    imc_R = grond.WaveformMisfitConfig(
        fmin=fmin,
        fmax=fmax,
        ffactor=ffactor,
        tmin=tmin,
        tmax=tmax,
        norm_exponent=1,
        domain=domain,
        quantity=quantity,
    )
    cha_R = 'R'

    event = ds.get_events()[0]
    event_origin = gf.Source(lat=event.lat, lon=event.lon)

    if event.depth is None:
        event.depth = 7 * km
    if len(event.tags) > 0:
        for tag in event.tags:
            if tag[0:6] == "stress":
                problem_type = "VLVDProblem"
                problem_type = "volume_point"
    distance_min = None
    distance_max = 30000.
    targets = []
    for st in ds.get_stations():
        for cha in cha_Z:
            target = grond.WaveformMisfitTarget(interpolation=gf_interpolation,
                                                store_id=store_id,
                                                codes=st.nsl() + (cha, ),
                                                lat=st.lat,
                                                lon=st.lon,
                                                quantity=quantity,
                                                misfit_config=imc_Z,
                                                normalisation_family="td",
                                                path="Z")
            _, bazi = event_origin.azibazi_to(target)
            if cha == 'Z':
                target.azimuth = 0.
                target.dip = -90.
            target.set_dataset(ds)
            targets.append(target)
    for st in ds.get_stations():
        for cha in cha_T:
            target = grond.WaveformMisfitTarget(codes=st.nsl() + (cha, ),
                                                lat=st.lat,
                                                lon=st.lon,
                                                interpolation=gf_interpolation,
                                                store_id=store_id,
                                                normalisation_family="td",
                                                path="T",
                                                quantity=quantity,
                                                misfit_config=imc_T)
            _, bazi = event_origin.azibazi_to(target)
            if cha == 'T':
                target.azimuth = bazi - 90.
                target.dip = 0.
            target.set_dataset(ds)
            targets.append(target)
    for st in ds.get_stations():
        for cha in cha_R:
            target = grond.WaveformMisfitTarget(codes=st.nsl() + (cha, ),
                                                lat=st.lat,
                                                lon=st.lon,
                                                interpolation=gf_interpolation,
                                                store_id=store_id,
                                                normalisation_family="td",
                                                path="R",
                                                quantity=quantity,
                                                misfit_config=imc_R)
            _, bazi = event_origin.azibazi_to(target)
            if cha == 'R':
                target.azimuth = bazi - 180.
                target.dip = 0.
            target.set_dataset(ds)
            targets.append(target)

    if problem_type == "CMTProblem":
        base_source = gf.MTSource.from_pyrocko_event(event)
        stf_type = 'HalfSinusoidSTF'
        base_source.set_origin(event_origin.lat, event_origin.lon)
        base_source.set_depth = event_origin.depth
        stf = STFType.base_stf(stf_type)
        stf.duration = event.duration or 0.0
        base_source.stf = stf
        ranges = dict(time=gf.Range(-0.1, 0.1, relative='add'),
                      north_shift=gf.Range(-1 * km, 1 * km),
                      east_shift=gf.Range(-1 * km, 1 * km),
                      depth=gf.Range(3400, 14000),
                      magnitude=gf.Range(-0.7, 3.1),
                      duration=gf.Range(0., 0.2),
                      rmnn=gf.Range(-1.4, 1.4),
                      rmee=gf.Range(-1.4, 1.4),
                      rmdd=gf.Range(-1.4, 1.4),
                      rmne=gf.Range(-1.4, 1.4),
                      rmnd=gf.Range(-1.4, 1.4),
                      rmed=gf.Range(-1.4, 1.4))

        problem = grond.problems.CMTProblem(
            name=event.name,
            base_source=base_source,
            distance_min=600.,
            mt_type='deviatoric',
            ranges=ranges,
            targets=targets,
            norm_exponent=1,
            stf_type=stf_type,
        )

    elif problem_type == "magnitude":
        base_source = gf.MTSource.from_pyrocko_event(event)
        stf_type = 'HalfSinusoidSTF'
        base_source.set_origin(event_origin.lat, event_origin.lon)
        base_source.set_depth = event_origin.depth
        stf = STFType.base_stf(stf_type)
        stf.duration = event.duration or 0.0
        base_source.stf = stf
        ranges = dict(time=gf.Range(-0.1, 0.1, relative='add'),
                      north_shift=gf.Range(-0.5 * km, 0.5 * km),
                      east_shift=gf.Range(-0.5 * km, 0.5 * km),
                      depth=gf.Range(3400, 14000),
                      magnitude=gf.Range(-0.7, 3.1),
                      duration=gf.Range(0., 0.2),
                      rmnn=gf.Range(0, 0),
                      rmee=gf.Range(0, 0),
                      rmdd=gf.Range(0, 0),
                      rmne=gf.Range(0, 0),
                      rmnd=gf.Range(0, 0),
                      rmed=gf.Range(0, 0))

        problem = grond.problems.CMTProblem(
            name=event.name,
            base_source=base_source,
            distance_min=600.,
            mt_type='deviatoric',
            ranges=ranges,
            targets=targets,
            norm_exponent=1,
            stf_type=stf_type,
        )

    elif problem_type == "volume_point":
        base_source = gf.ExplosionSource.from_pyrocko_event(event)
        stf_type = 'HalfSinusoidSTF'
        base_source.set_origin(event_origin.lat, event_origin.lon)
        base_source.set_depth = event_origin.depth
        stf = STFType.base_stf(stf_type)
        stf.duration = event.duration or 0.0
        base_source.stf = stf
        ranges = dict(time=gf.Range(-0.1, 0.1, relative='add'),
                      north_shift=gf.Range(-0.5 * km, 0.5 * km),
                      east_shift=gf.Range(-0.5 * km, 0.5 * km),
                      depth=gf.Range(3400, 14000),
                      magnitude=gf.Range(-0.7, 3.1),
                      duration=gf.Range(0., 0.2),
                      volume_change=gf.Range(0.001, 10000.1))

        problem = grond.problems.VolumePointProblem(
            name=event.name,
            base_source=base_source,
            distance_min=600.,
            ranges=ranges,
            targets=targets,
            norm_exponent=1,
        )

    elif problem_type == "VLVDProblem":
        base_source = gf.VLVDSource.from_pyrocko_event(event)
        stf_type = 'HalfSinusoidSTF'
        base_source.set_origin(event_origin.lat, event_origin.lon)
        base_source.set_depth = event_origin.depth
        stf = STFType.base_stf(stf_type)
        stf.duration = event.duration or 0.0
        base_source.stf = stf
        ranges = dict(time=gf.Range(-0.1, 0.1, relative='add'),
                      north_shift=gf.Range(-1 * km, 1 * km),
                      east_shift=gf.Range(-1 * km, 1 * km),
                      depth=gf.Range(6000, 14000),
                      volume_change=gf.Range(0.001, 10000.1),
                      duration=gf.Range(0.01, 0.2),
                      dip=gf.Range(0., 90.),
                      azimuth=gf.Range(0., 360.),
                      clvd_moment=gf.Range(0.1, 1e13))

        problem = grond.problems.VLVDProblem(
            name=event.name,
            base_source=base_source,
            distance_min=3000.,
            ranges=ranges,
            targets=targets,
            norm_exponent=1,
        )
    print(problem_type)
    problem.set_engine(engine)
    monitor = GrondMonitor.watch(rundir)
    print("analysing")
    analyser_iter = 1000
    analyser = grond.analysers.target_balancing.TargetBalancingAnalyser(
        niter=analyser_iter, use_reference_magnitude=False, cutoff=None)
    analyser.analyse(problem, ds)

    problem.dump_problem_info(rundir)

    from grond import config
    from grond import Environment

    config_path = '%s/scenario.gronf' % datafolder
    quick_config_path = '%sconfig/config.yaml' % datafolder
    util.ensuredir("%sconfig" % datafolder)
    configs_dir = os.path.dirname(os.path.abspath(__file__)) + "/configs"
    os.system("cp -r %s/config.yaml* %s" % (configs_dir, quick_config_path))
    from grond.config import read_config
    conf = read_config(quick_config_path)
    uniform_iter = 1200
    directed_iter = 20000
    mod_conf = conf.clone()
    #    mod_conf.set_elements(
    #        'path_prefix', ".")

    mod_conf.set_elements('analyser_configs[:].niterations', analyser_iter)
    mod_conf.set_elements('optimiser_config.sampler_phases[0].niterations',
                          uniform_iter)
    mod_conf.set_elements('optimiser_config.sampler_phases[0].niterations',
                          directed_iter)
    mod_conf.set_elements('optimiser_config.nbootstrap', 100)
    mod_conf.set_elements('target_groups[:].misfit_config.tmin', "%s" % tmin)
    mod_conf.set_elements('target_groups[:].misfit_config.tmax', "%s" % tmax)
    mod_conf.set_elements('target_groups[:].misfit_config.fmin', "%s" % fmin)
    mod_conf.set_elements('target_groups[:].misfit_config.fmax', "%s" % fmax)
    mod_conf.set_elements('target_groups[:].misfit_config.domain',
                          "%s" % domain)
    mod_conf.set_elements('target_groups[:].misfit_config.ffactor',
                          "%s" % ffactor)
    mod_conf.set_basepath(conf.get_basepath())

    config.write_config(mod_conf, quick_config_path)
    config.write_config(mod_conf, rundir + "/config.yaml")

    sampler_phases = [
        UniformSamplerPhase(niterations=uniform_iter),
        DirectedSamplerPhase(niterations=directed_iter)
    ]
    optimiser = grond.optimisers.highscore.HighScoreOptimiser(
        sampler_phases=list(sampler_phases),
        chain_length_factor=8,
        nbootstrap=100)
    optimiser.set_nthreads(1)

    optimiser.init_bootstraps(problem)
    tstart = time.time()
    optimiser.optimise(problem, rundir=rundir)
    grond.harvest(rundir, problem, force=True)
    #solverscipy.solve(problem, quiet=False, niter_explorative=2000, niter=10000)
    tstop = time.time()
    #os.system("grond report %s" % rundir)
    os.system("grond plot fits_waveform %s" % rundir)
    os.system("grond plot hudson %s" % rundir)
    os.system("grond plot fits_waveform_ensemble %s" % rundir)
    os.system("grond plot location_mt %s" % rundir)
    os.system("grond plot seismic_stations %s" % rundir)
    os.system("grond plot sequence %s" % rundir)
    ds.empty_cache()
    print("done with grond")
예제 #7
0
def locate(marker_file,
           gf_stores_path,
           scenario_dir,
           config_path,
           stations_path,
           event_name,
           make_report=False):
    playground_dir = common.get_playground_dir()

    with chdir(playground_dir):

        with chdir(scenario_dir):
            quick_config_path = scenario_dir + 'scenario_quick.gronf'
            event_names = grond('events', config_path).strip().split('\n')
            env = Environment([config_path] + event_names)
            conf = env.get_config()
            event_names = [event_name]

            mod_conf = conf.clone()
            #             target_groups = guts.load_all(string='''
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'pick.p'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_P}'
            # pick_phasename: 'any_P'
            # store_id: 'landau_100hz_noweak'
            # depth_max: 1
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'pick.s'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_S}'
            # pick_phasename: 'any_S'
            # store_id: 'landau_100hz_noweak'
            # depth_max: 1
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'moer.p'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_P}'
            # pick_phasename: 'any_P'
            # store_id: 'moer_1hz_no_weak'
            # depth_min: 69
            # depth_max: 71
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'moer.s'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_S}'
            # pick_phasename: 'any_S'
            # store_id: 'moer_1hz_no_weak'
            # depth_min: 69
            # depth_max: 71
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'lde.p'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_P}'
            # pick_phasename: 'any_P'
            # store_id: 'lde_100hz_no_weak'
            # depth_min: 149
            # depth_max: 151
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'lde.s'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_S}'
            # pick_phasename: 'any_S'
            # store_id: 'lde_100hz_no_weak'
            # depth_min: 149
            # depth_max: 151
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'rott.p'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_P}'
            # pick_phasename: 'any_P'
            # store_id: 'rott_100hz_no_weak'
            # depth_min: 304
            # depth_max: 306
            # --- !grond.PhasePickTargetGroup
            # normalisation_family: 'pick'
            # path: 'rott.s'
            # distance_min: 0e3
            # distance_max: 102000.0
            # pick_synthetic_traveltime: '{stored:any_S}'
            # pick_phasename: 'any_S'
            # store_id: 'rott_100hz_no_weak'
            # depth_min: 304
            # depth_max: 306
            # ''')

            mod_conf.dataset_config.picks_paths = [marker_file]
            #mod_conf.target_groups = target_groups
            mod_conf.path_prefix = scenario_dir
            mod_conf.dataset_config.stations_path = stations_path
            mod_conf.event_names = [event_name]
            # mod_conf.set_elements(
            #     'analyser_configs[:].niterations', 100)
            # mod_conf.set_elements(
            #     'optimiser_config.sampler_phases[:].niterations', 100)
            # mod_conf.set_elements(
            #     'optimiser_config.nbootstrap', 5)

            mod_conf.optimiser_config.sampler_phases[-1].niterations = 25000

            mod_conf.set_basepath(conf.get_basepath())
            config.write_config(mod_conf, quick_config_path)

            grond('go', quick_config_path, *event_names)
            rundir_paths = common.get_rundir_paths(quick_config_path,
                                                   event_names)
            best = grond('export', 'best', *rundir_paths, '--output=best.pf')
            best = grond('export', 'mean', *rundir_paths, '--output=mean.pf')
            best = model.load_events(scenario_dir + "/best.pf")[0]
            if make_report is True:
                grond('report', *rundir_paths)

            return best