Example #1
0
def have_store(store_id):
    engine = gf.get_engine()
    try:
        engine.get_store(store_id)
        return True
    except gf.NoSuchStore:
        return False
Example #2
0
def have_store(store_id):
    engine = gf.get_engine()
    try:
        engine.get_store(store_id)
        return True
    except gf.NoSuchStore:
        return False
Example #3
0
def have_store(store_id):
    engine = gf.get_engine()
    try:
        engine.get_store(store_id)
        return True, ''
    except gf.NoSuchStore:
        return False, 'GF store "%s" not available' % store_id
Example #4
0
    def test_scenario_combinations(self):

        generator = scenario.ScenarioGenerator(
            seed=20,
            center_lat=42.6,
            center_lon=13.3,
            radius=60*km,
            target_generators=[
                targets.WaveformGenerator(
                    store_id=ScenarioTestCase.store_id,
                    station_generator=targets.RandomStationGenerator(
                        avoid_water=False),
                    noise_generator=targets.waveform.WhiteNoiseGenerator(),
                    seismogram_quantity='velocity'),
                targets.InSARGenerator(
                    resolution=(20, 20),
                    noise_generator=targets.insar.AtmosphericNoiseGenerator(
                        amplitude=1e-5)),
                targets.GNSSCampaignGenerator(
                    station_generator=targets.RandomStationGenerator(
                        avoid_water=False,
                        channels=None))
                ],
            source_generator=scenario.DCSourceGenerator(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=10*km,
                depth_min=1*km,
                depth_max=10*km,
                magnitude_min=3.0,
                magnitude_max=4.5,
                strike=120.,
                dip=45.,
                rake=90.,
                perturbation_angle_std=15.,
                nevents=3)
        )

        engine = gf.get_engine()
        generator.init_modelling(engine)

        for src in scenario.sources.AVAILABLE_SOURCES:
            generator.source_generator = src(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=1*km,
                depth_min=1.5*km,
                depth_max=5*km,
                magnitude_min=3.0,
                magnitude_max=4.5)
            generator.source_generator.update_hierarchy(generator)

            generator.get_stations()
            generator.get_waveforms()
            generator.get_insar_scenes()
            generator.get_gnss_campaigns()
Example #5
0
    def test_scenario_map(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = self.generator
        engine = gf.get_engine()

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('plot', generator)

        s = collection.get_scenario('plot')
        s.get_map()
Example #6
0
    def test_scenario_map(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = self.generator
        engine = gf.get_engine()

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('plot', generator)

        s = collection.get_scenario('plot')
        s.get_map()
Example #7
0
    def test_scenario_insar(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = self.generator
        engine = gf.get_engine()
        generator.init_modelling(engine)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('insar', generator)

        s = collection.get_scenario('insar')
        s.ensure_insar_scenes()
Example #8
0
    def test_scenario_insar(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = self.generator
        engine = gf.get_engine()
        generator.init_modelling(engine)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('insar', generator)

        s = collection.get_scenario('insar')
        s.ensure_insar_scenes()
Example #9
0
    def test_scenario_gnss(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = self.generator
        engine = gf.get_engine()
        generator.init_modelling(engine)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('gnss', generator)

        s = collection.get_scenario('gnss')
        assert len(s.get_gnss_campaigns()) == 1
Example #10
0
    def test_scenario_gnss(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = self.generator
        engine = gf.get_engine()
        generator.init_modelling(engine)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('gnss', generator)

        s = collection.get_scenario('gnss')
        assert len(s.get_gnss_campaigns()) == 1
Example #11
0
    def test_scenario_map(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = scenario.ScenarioGenerator(
            seed=20,
            center_lat=42.6,
            center_lon=13.3,
            radius=60*km,
            target_generators=[
                targets.WaveformGenerator(
                    store_id=ScenarioTestCase.store_id,
                    station_generator=targets.RandomStationGenerator(
                        avoid_water=False),
                    noise_generator=targets.waveform.WhiteNoiseGenerator(),
                    seismogram_quantity='velocity'),
                targets.InSARGenerator(
                    resolution=(20, 20),
                    noise_generator=targets.insar.AtmosphericNoiseGenerator(
                        amplitude=1e-5)),
                targets.GNSSCampaignGenerator(
                    station_generator=targets.RandomStationGenerator(
                        avoid_water=False,
                        channels=None))
                ],
            source_generator=scenario.DCSourceGenerator(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=10*km,
                depth_min=1*km,
                depth_max=10*km,
                magnitude_min=3.0,
                magnitude_max=4.5,
                strike=120.,
                dip=45.,
                rake=90.,
                perturbation_angle_std=15.,
                nevents=3)
        )

        engine = gf.get_engine()

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('plot', generator)

        s = collection.get_scenario('plot')
        s.get_map()
Example #12
0
    def test_scenario_combinations(self):
        import copy
        generator = copy.deepcopy(self.generator)
        engine = gf.get_engine()
        generator.init_modelling(engine)

        for src in scenario.sources.AVAILABLE_SOURCES:
            generator.source_generator = src(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=1*km,
                depth_min=1.5*km,
                depth_max=5*km,
                magnitude_min=3.0,
                magnitude_max=4.5)
            generator.source_generator.update_hierarchy(generator)

            generator.get_stations()
            generator.get_waveforms()
            generator.get_insar_scenes()
            generator.get_gnss_campaigns()
Example #13
0
    def test_scenario_combinations(self):
        import copy
        generator = copy.deepcopy(self.generator)
        engine = gf.get_engine()
        generator.init_modelling(engine)

        for src in scenario.sources.AVAILABLE_SOURCES:
            generator.source_generator = src(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=1*km,
                depth_min=1.5*km,
                depth_max=5*km,
                magnitude_min=3.0,
                magnitude_max=4.5)
            generator.source_generator.update_hierarchy(generator)

            generator.get_stations()
            generator.get_waveforms()
            generator.get_insar_scenes()
            generator.get_gnss_campaigns()
Example #14
0
    def test_scenario_gnss(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = scenario.ScenarioGenerator(
            seed=20,
            center_lat=42.6,
            center_lon=13.3,
            radius=60*km,
            target_generators=[
                targets.GNSSCampaignGenerator(
                    station_generator=targets.RandomStationGenerator(
                        avoid_water=False,
                        channels=None))
                ],
            source_generator=scenario.DCSourceGenerator(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=10*km,
                depth_min=1*km,
                depth_max=10*km,
                magnitude_min=3.0,
                magnitude_max=4.5,
                strike=120.,
                dip=45.,
                rake=90.,
                perturbation_angle_std=15.,
                nevents=3)
        )

        engine = gf.get_engine()
        generator.init_modelling(engine)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('gnss', generator)

        s = collection.get_scenario('gnss')
        assert len(s.get_gnss_campaigns()) == 1
Example #15
0
    def test_scenario_insar(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        generator = scenario.ScenarioGenerator(
            seed=20,
            center_lat=42.6,
            center_lon=13.3,
            radius=60*km,
            target_generators=[
                targets.InSARGenerator(
                    resolution=(20, 20),
                    noise_generator=targets.insar.AtmosphericNoiseGenerator(
                        amplitude=1e-5))
                ],
            source_generator=scenario.DCSourceGenerator(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=10*km,
                depth_min=1*km,
                depth_max=10*km,
                magnitude_min=3.0,
                magnitude_max=4.5,
                strike=120.,
                dip=45.,
                rake=90.,
                perturbation_angle_std=15.,
                nevents=3)
        )

        engine = gf.get_engine()
        generator.init_modelling(engine)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('insar', generator)

        s = collection.get_scenario('insar')
        s.ensure_data()
Example #16
0
    def test_scenario_waveforms(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        vmin = 2500.
        generator = self.generator

        def twin(source):
            tmin = source.time
            tmax = source.time + 100*km / vmin
            return tmin, tmax

        engine = gf.get_engine()
        generator.init_modelling(engine)

        ref_sources = generator.get_sources()
        ref_trs_list = []
        for source in ref_sources:
            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            ref_trs_list.append(trs)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('one', generator)
        with self.assertRaises(scenario.ScenarioError):
            collection.add_scenario('one', generator)

        assert len(collection.list_scenarios()) == 1
        assert collection.list_scenarios()[0].scenario_id == 'one'

        s = collection.get_scenario('one')

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)

        collection2 = scenario.ScenarioCollection(tempdir, engine)

        assert len(collection2.list_scenarios()) == 1
        assert collection2.list_scenarios()[0].scenario_id == 'one'

        s = collection2.get_scenario('one')

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)

        tmin, tmax = s.get_time_range()
        s.ensure_waveforms(tmin, tmax)
        p = s.get_waveform_pile()

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            tmin, tmax = twin(source)
            trs = p.all(tmin=tmin, tmax=tmax, include_last=False)
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)
Example #17
0
    def test_regional(self):
        engine = gf.get_engine()
        store_id = 'crust2_mf'
        try:
            engine.get_store(store_id)
        except gf.NoSuchStore:
            logger.warn('GF Store %s not available - skipping test' % store_id)
            return

        nsources = 10
        nstations = 10

        print 'cache source channels par wallclock seismograms_per_second'
        nprocs_max = multiprocessing.cpu_count()

        for sourcetype, channels in [
                ['point', 'Z'],
                ['point', 'NEZ'],
                ['rect', 'Z'],
                ['rect', 'NEZ']]:

            for nprocs in [1, 2, 4, 8, 16, 32]:
                if nprocs > nprocs_max:
                    continue

                sources = []
                for isource in xrange(nsources):
                    m = pmt.MomentTensor.random_dc()
                    strike, dip, rake = map(float, m.both_strike_dip_rake()[0])

                    if sourcetype == 'point':
                        source = gf.DCSource(
                            north_shift=rand(-20.*km, 20*km),
                            east_shift=rand(-20.*km, 20*km),
                            depth=rand(10*km, 20*km),
                            strike=strike,
                            dip=dip,
                            rake=rake,
                            magnitude=rand(4.0, 5.0))

                    elif sourcetype == 'rect':
                        source = gf.RectangularSource(
                            north_shift=rand(-20.*km, 20*km),
                            east_shift=rand(-20.*km, 20*km),
                            depth=rand(10*km, 20*km),
                            length=10*km,
                            width=5*km,
                            nucleation_x=0.,
                            nucleation_y=-1.,
                            strike=strike,
                            dip=dip,
                            rake=rake,
                            magnitude=rand(4.0, 5.0))
                    else:
                        assert False

                    sources.append(source)

                targets = []
                for istation in xrange(nstations):
                    dist = rand(40.*km, 900*km)
                    azi = rand(-180., 180.)

                    north_shift = dist * math.cos(azi*d2r)
                    east_shift = dist * math.sin(azi*d2r)

                    for cha in channels:
                        target = gf.Target(
                            codes=('', 'S%04i' % istation, '', cha),
                            north_shift=north_shift,
                            east_shift=east_shift,
                            quantity='displacement',
                            interpolation='multilinear',
                            optimization='enable',
                            store_id=store_id)

                        targets.append(target)

                ntraces = len(targets) * len(sources)
                for temperature in ['cold', 'hot']:
                    t0 = time.time()
                    resp = engine.process(sources, targets, nprocs=nprocs)
                    # print resp.stats

                    t1 = time.time()
                    duration = t1 - t0
                    sps = ntraces / duration
                    if temperature == 'hot':
                        if nprocs == 1:
                            sps_ref = sps
                        print '%-5s %-6s %-8s %3i %9.3f %12.1f %12.1f' % (
                            temperature, sourcetype, channels, nprocs, t1-t0,
                            sps, sps/sps_ref)

                    del resp
Example #18
0
    def test_against_kiwi(self):
        engine = gf.get_engine()
        store_id = 'chile_70km_crust'
        try:
            store = engine.get_store(store_id)
        except gf.NoSuchStore:
            logger.warn('GF Store %s not available - skipping test' % store_id)
            return

        base_source = gf.RectangularSource(
            depth=15*km,
            strike=0.,
            dip=90.,
            rake=0.,
            magnitude=4.5,
            nucleation_x=-1.,
            length=10*km,
            width=0*km,
            stf=gf.BoxcarSTF(duration=1.0))

        base_event = base_source.pyrocko_event()

        channels = 'NEZ'
        nstations = 10
        stations = []
        targets = []
        for istation in xrange(nstations):
            dist = rand(40.*km, 900*km)
            azi = rand(-180., 180.)
            north_shift = dist * math.cos(azi*d2r)
            east_shift = dist * math.sin(azi*d2r)
            lat, lon = od.ne_to_latlon(0., 0., north_shift, east_shift)
            sta = 'S%02i' % istation
            station = model.Station(
                '', sta, '',
                lat=lat,
                lon=lon)

            station.set_channels_by_name('N', 'E', 'Z')
            stations.append(station)

            for cha in channels:
                target = gf.Target(
                    codes=station.nsl() + (cha,),
                    lat=lat,
                    lon=lon,
                    quantity='displacement',
                    interpolation='multilinear',
                    optimization='enable',
                    store_id=store_id)

                targets.append(target)

        from tunguska import glue

        nsources = 10

        # nprocs_max = multiprocessing.cpu_count()
        nprocs = 1

        try:
            seis = glue.start_seismosizer(
                gfdb_path=op.join(store.store_dir, 'db'),
                event=base_event,
                stations=stations,
                hosts=['localhost']*nprocs,
                balance_method='123321',
                effective_dt=0.5,
                verbose=False)

            ksource = to_kiwi_source(base_source)

            seis.set_source(ksource)

            recs = seis.get_receivers_snapshot(('syn',), (), 'plain')
            trs = []
            for rec in recs:
                for tr in rec.get_traces():
                    tr.set_codes(channel=transchan[tr.channel])
                    trs.append(tr)

            trs2 = engine.process(base_source, targets).pyrocko_traces()

            trace.snuffle(trs + trs2)

            seis.set_synthetic_reference()

            for sourcetype in ['point', 'rect']:
                sources = []
                for isource in xrange(nsources):
                    m = pmt.MomentTensor.random_dc()
                    strike, dip, rake = map(float, m.both_strike_dip_rake()[0])

                    if sourcetype == 'point':
                        source = gf.RectangularSource(
                            north_shift=rand(-20.*km, 20*km),
                            east_shift=rand(-20.*km, 20*km),
                            depth=rand(10*km, 20*km),
                            nucleation_x=0.0,
                            nucleation_y=0.0,
                            strike=strike,
                            dip=dip,
                            rake=rake,
                            magnitude=rand(4.0, 5.0),
                            stf=gf.BoxcarSTF(duration=1.0))

                    elif sourcetype == 'rect':
                        source = gf.RectangularSource(
                            north_shift=rand(-20.*km, 20*km),
                            east_shift=rand(-20.*km, 20*km),
                            depth=rand(10*km, 20*km),
                            length=10*km,
                            width=5*km,
                            nucleation_x=-1.,
                            nucleation_y=0,
                            strike=strike,
                            dip=dip,
                            rake=rake,
                            magnitude=rand(4.0, 5.0),
                            stf=gf.BoxcarSTF(duration=1.0))
                    else:
                        assert False

                    sources.append(source)

                for temperature in ['cold', 'hot']:
                    t0 = time.time()
                    resp = engine.process(sources, targets, nprocs=nprocs)
                    t1 = time.time()
                    if temperature == 'hot':
                        dur_pyrocko = t1 - t0

                    del resp

                ksources = map(to_kiwi_source, sources)

                for temperature in ['cold', 'hot']:
                    t0 = time.time()
                    seis.make_misfits_for_sources(
                        ksources, show_progress=False)
                    t1 = time.time()
                    if temperature == 'hot':
                        dur_kiwi = t1 - t0

                print 'pyrocko %-5s %5.2fs  %5.1fx' % (
                    sourcetype, dur_pyrocko, 1.0)
                print 'kiwi    %-5s %5.2fs  %5.1fx' % (
                    sourcetype, dur_kiwi, dur_pyrocko/dur_kiwi)

        finally:
            seis.close()
            del seis
Example #19
0
    def test_scenario_waveforms(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        vmin = 2500.
        generator = self.generator

        def twin(source):
            tmin = source.time
            tmax = source.time + 100*km / vmin
            return tmin, tmax

        engine = gf.get_engine()
        generator.init_modelling(engine)

        ref_sources = generator.get_sources()
        ref_trs_list = []
        for source in ref_sources:
            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            ref_trs_list.append(trs)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('one', generator)
        with self.assertRaises(scenario.ScenarioError):
            collection.add_scenario('one', generator)

        assert len(collection.list_scenarios()) == 1
        assert collection.list_scenarios()[0].scenario_id == 'one'

        s = collection.get_scenario('one')

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)

        collection2 = scenario.ScenarioCollection(tempdir, engine)

        assert len(collection2.list_scenarios()) == 1
        assert collection2.list_scenarios()[0].scenario_id == 'one'

        s = collection2.get_scenario('one')

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)

        tmin, tmax = s.get_time_range()
        s.ensure_waveforms(tmin, tmax)
        p = s.get_waveform_pile()

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            tmin, tmax = twin(source)
            trs = p.all(tmin=tmin, tmax=tmax, include_last=False)
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)
Example #20
0
    def off_test_synthetic(self):

        from pyrocko import gf

        km = 1000.
        nstations = 10
        edepth = 5*km
        store_id = 'crust2_d0'

        swin = 2.
        lwin = 9.*swin
        ks = 1.0
        kl = 1.0
        kd = 3.0

        engine = gf.get_engine()
        snorths = (num.random.random(nstations)-1.0) * 50*km
        seasts = (num.random.random(nstations)-1.0) * 50*km
        targets = []
        for istation, (snorths, seasts) in enumerate(zip(snorths, seasts)):
            targets.append(
                gf.Target(
                    quantity='displacement',
                    codes=('', 's%03i' % istation, '', 'Z'),
                    north_shift=float(snorths),
                    east_shift=float(seasts),
                    store_id=store_id,
                    interpolation='multilinear'))

        source = gf.DCSource(
            north_shift=50*km,
            east_shift=50*km,
            depth=edepth)

        store = engine.get_store(store_id)

        response = engine.process(source, targets)
        trs = []

        station_traces = defaultdict(list)
        station_targets = defaultdict(list)
        for source, target, tr in response.iter_results():
            tp = store.t('any_P', source, target)
            t = tp - 5 * tr.deltat + num.arange(11) * tr.deltat
            if False:
                gauss = trace.Trace(
                    tmin=t[0],
                    deltat=tr.deltat,
                    ydata=num.exp(-((t-tp)**2)/((2*tr.deltat)**2)))

                tr.ydata[:] = 0.0
                tr.add(gauss)

            trs.append(tr)
            station_traces[target.codes[:3]].append(tr)
            station_targets[target.codes[:3]].append(target)

        station_stalta_traces = {}
        for nsl, traces in station_traces.iteritems():
            etr = None
            for tr in traces:
                sqr_tr = tr.copy(data=False)
                sqr_tr.ydata = tr.ydata**2
                if etr is None:
                    etr = sqr_tr
                else:
                    etr += sqr_tr

            autopick.recursive_stalta(swin, lwin, ks, kl, kd, etr)
            etr.set_codes(channel='C')

            station_stalta_traces[nsl] = etr

        trace.snuffle(trs + station_stalta_traces.values())
        deltat = trs[0].deltat

        nnorth = 50
        neast = 50

        size = 400*km

        north = num.linspace(-size/2., size/2., nnorth)
        north2 = num.repeat(north, neast)
        east = num.linspace(-size/2., size/2., neast)
        east2 = num.tile(east, nnorth)
        depth = 5*km

        def tcal(target, i):
            try:
                return store.t(
                    'any_P',
                    gf.Location(
                        north_shift=north2[i],
                        east_shift=east2[i],
                        depth=depth),
                    target)

            except gf.OutOfBounds:
                return 0.0

        nsls = sorted(station_stalta_traces.keys())

        tts = num.fromiter((tcal(station_targets[nsl][0], i)
                           for i in xrange(nnorth*neast)
                           for nsl in nsls), dtype=num.float)

        arrays = [
            station_stalta_traces[nsl].ydata.astype(num.float) for nsl in nsls]
        offsets = num.array(
            [int(round(station_stalta_traces[nsl].tmin / deltat))
             for nsl in nsls], dtype=num.int32)
        shifts = -num.array(
            [int(round(tt / deltat))
             for tt in tts], dtype=num.int32).reshape(nnorth*neast, nstations)
        weights = num.ones((nnorth*neast, nstations))

        print shifts[25*neast + 25] * deltat

        print offsets.dtype, shifts.dtype, weights.dtype

        print 'stack start'
        mat, ioff = parstack(arrays, offsets, shifts, weights, 1)
        print 'stack stop'

        mat = num.reshape(mat, (nnorth, neast))

        from matplotlib import pyplot as plt

        fig = plt.figure()

        axes = fig.add_subplot(1, 1, 1, aspect=1.0)

        axes.contourf(east/km, north/km, mat)

        axes.plot(
            g(targets, 'east_shift')/km,
            g(targets, 'north_shift')/km, '^')
        axes.plot(source.east_shift/km, source.north_shift/km, 'o')
        plt.show()
Example #21
0
                        markers_debug.append(marker)

                except wmeasure.AmplitudeMeasurementFailed:
                    nerrors += 1
                    amps.append(None)

            data.append([magnitude, duration, depth, distance] + amps)

    if debug:
        trace.snuffle(traces_debug, markers=markers_debug)

    return num.array(data, dtype=num.float)


if __name__ == '__main__':
    engine = gf.get_engine()
    store_id = 'crust2_m5_hardtop_8Hz_fine'
    magnitude_min, magnitude_max = 3., 7.
    moment_tensor = None  # random DC
    # moment_tensor = pmt.MomentTensor.from_values([strike, dip, rake])
    stress_drop_min, stress_drop_max = 1.0e6, 10.0e6
    rupture_velocity_min, rupture_velocity_max = 2500.*0.9, 3600.*0.9
    depth_min, depth_max = 1.*km, 30.*km
    distance_min, distance_max = 100*km, 100*km

    measure_ML = wmeasure.AmplitudeMeasure(
        timing_tmin=gf.Timing('vel:8'),
        timing_tmax=gf.Timing('vel:2'),
        fmin=None,
        fmax=None,
        response=wmeasure.response_wa,
Example #22
0
    def test_scenario_waveforms(self):
        tempdir = mkdtemp(prefix='pyrocko-scenario')
        self.tempdirs.append(tempdir)

        vmin = 2500.

        generator = scenario.ScenarioGenerator(
            seed=20,
            center_lat=42.6,
            center_lon=13.3,
            radius=60*km,
            target_generators=[
                targets.WaveformGenerator(
                    store_id=ScenarioTestCase.store_id,
                    station_generator=targets.RandomStationGenerator(
                        nstations=5,
                        avoid_water=False),
                    noise_generator=targets.waveform.WhiteNoiseGenerator(),
                    seismogram_quantity='velocity'),
                ],
            source_generator=scenario.DCSourceGenerator(
                time_min=util.str_to_time('2017-01-01 00:00:00'),
                time_max=util.str_to_time('2017-01-01 02:00:00'),
                radius=10*km,
                depth_min=1*km,
                depth_max=10*km,
                magnitude_min=3.0,
                magnitude_max=4.5,
                strike=120.,
                dip=45.,
                rake=90.,
                perturbation_angle_std=15.,
                nevents=3)
        )

        def twin(source):
            tmin = source.time
            tmax = source.time + 100*km / vmin
            return tmin, tmax

        engine = gf.get_engine()
        generator.init_modelling(engine)

        ref_sources = generator.get_sources()
        ref_trs_list = []
        for source in ref_sources:
            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            ref_trs_list.append(trs)

        collection = scenario.ScenarioCollection(tempdir, engine)
        collection.add_scenario('one', generator)
        with self.assertRaises(scenario.ScenarioError):
            collection.add_scenario('one', generator)

        assert len(collection.list_scenarios()) == 1
        assert collection.list_scenarios()[0].scenario_id == 'one'

        s = collection.get_scenario('one')

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)

        collection2 = scenario.ScenarioCollection(tempdir, engine)

        assert len(collection2.list_scenarios()) == 1
        assert collection2.list_scenarios()[0].scenario_id == 'one'

        s = collection2.get_scenario('one')

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            trs = generator.get_waveforms(*twin(source))
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)

        tmin, tmax = s.get_time_range()
        s.ensure_data(tmin, tmax)
        p = s.get_waveform_pile()

        for ref_trs, source in zip(
                ref_trs_list,
                s.get_generator().get_sources()):

            tmin, tmax = twin(source)
            trs = p.all(tmin=tmin, tmax=tmax, include_last=False)
            trs.sort(key=lambda tr: tr.nslc_id)
            self.assert_traces_almost_equal(trs, ref_trs)
def get_engine():
    return gf.get_engine(store_superdirs=['gf_stores'])
Example #24
0
    def _off_test_synthetic(self):

        from pyrocko import gf

        km = 1000.
        nstations = 10
        edepth = 5 * km
        store_id = 'crust2_d0'

        swin = 2.
        lwin = 9. * swin
        ks = 1.0
        kl = 1.0
        kd = 3.0

        engine = gf.get_engine()
        snorths = (num.random.random(nstations) - 1.0) * 50 * km
        seasts = (num.random.random(nstations) - 1.0) * 50 * km
        targets = []
        for istation, (snorths, seasts) in enumerate(zip(snorths, seasts)):
            targets.append(
                gf.Target(quantity='displacement',
                          codes=('', 's%03i' % istation, '', 'Z'),
                          north_shift=float(snorths),
                          east_shift=float(seasts),
                          store_id=store_id,
                          interpolation='multilinear'))

        source = gf.DCSource(north_shift=50 * km,
                             east_shift=50 * km,
                             depth=edepth)

        store = engine.get_store(store_id)

        response = engine.process(source, targets)
        trs = []

        station_traces = defaultdict(list)
        station_targets = defaultdict(list)
        for source, target, tr in response.iter_results():
            tp = store.t('any_P', source, target)
            t = tp - 5 * tr.deltat + num.arange(11) * tr.deltat
            if False:
                gauss = trace.Trace(tmin=t[0],
                                    deltat=tr.deltat,
                                    ydata=num.exp(-((t - tp)**2) /
                                                  ((2 * tr.deltat)**2)))

                tr.ydata[:] = 0.0
                tr.add(gauss)

            trs.append(tr)
            station_traces[target.codes[:3]].append(tr)
            station_targets[target.codes[:3]].append(target)

        station_stalta_traces = {}
        for nsl, traces in station_traces.items():
            etr = None
            for tr in traces:
                sqr_tr = tr.copy(data=False)
                sqr_tr.ydata = tr.ydata**2
                if etr is None:
                    etr = sqr_tr
                else:
                    etr += sqr_tr

            autopick.recursive_stalta(swin, lwin, ks, kl, kd, etr)
            etr.set_codes(channel='C')

            station_stalta_traces[nsl] = etr

        trace.snuffle(trs + list(station_stalta_traces.values()))
        deltat = trs[0].deltat

        nnorth = 50
        neast = 50

        size = 200 * km

        north = num.linspace(-size, size, nnorth)
        north2 = num.repeat(north, neast)
        east = num.linspace(-size, size, neast)
        east2 = num.tile(east, nnorth)
        depth = 5 * km

        def tcal(target, i):
            try:
                return store.t(
                    'any_P',
                    gf.Location(north_shift=north2[i],
                                east_shift=east2[i],
                                depth=depth), target)

            except gf.OutOfBounds:
                return 0.0

        nsls = sorted(station_stalta_traces.keys())

        tts = num.fromiter((tcal(station_targets[nsl][0], i)
                            for i in range(nnorth * neast) for nsl in nsls),
                           dtype=num.float)

        arrays = [
            station_stalta_traces[nsl].ydata.astype(num.float) for nsl in nsls
        ]
        offsets = num.array([
            int(round(station_stalta_traces[nsl].tmin / deltat))
            for nsl in nsls
        ],
                            dtype=num.int32)
        shifts = -num.array([int(round(tt / deltat)) for tt in tts],
                            dtype=num.int32).reshape(nnorth * neast, nstations)
        weights = num.ones((nnorth * neast, nstations))

        print(shifts[25 * neast + 25] * deltat)

        print(offsets.dtype, shifts.dtype, weights.dtype)

        print('stack start')
        mat, ioff = parstack(arrays, offsets, shifts, weights, 1)
        print('stack stop')

        mat = num.reshape(mat, (nnorth, neast))

        from matplotlib import pyplot as plt

        fig = plt.figure()

        axes = fig.add_subplot(1, 1, 1, aspect=1.0)

        axes.contourf(east / km, north / km, mat)

        axes.plot(
            g(targets, 'east_shift') / km,
            g(targets, 'north_shift') / km, '^')
        axes.plot(source.east_shift / km, source.north_shift / km, 'o')
        plt.show()
Example #25
0
def get_engine():
    return gf.get_engine(store_superdirs=['gf_stores'])