Ejemplo n.º 1
0
def test_get_synthetics():
    # Try to load 3 stations, out of which 2 are in range for P
    db = instaseis.open_db('syngine://prem_a_20s')
    cat = obspy.read_events('./stfinv/data/virginia.xml')
    st = read('./stfinv/data/dis.II.BFO.00.BHZ')
    st += read('./stfinv/data/dis.GE.DAG..BHZ')
    st += read('./stfinv/data/dis.G.CRZF.00.BHZ')
    st_data, st_syn = st.get_synthetics(db=db,
                                        origin=cat[0].origins[0],
                                        out_dir='/tmp')

    npt.assert_equal(len(st_data), 2)
    npt.assert_equal(len(st_syn), 12)

    for istat in range(0, 2):
        channels = ['MPP', 'MRP', 'MRR', 'MRT', 'MTP', 'MTT']
        for channel in channels:
            st_test = st_syn.select(station=st_data[istat].stats.station,
                                    network=st_data[istat].stats.network,
                                    location=st_data[istat].stats.location,
                                    channel=channel)
            npt.assert_equal(len(st_test), 1)

        for tr in st_syn[istat * 6:(istat + 1) * 6]:
            npt.assert_string_equal(str(tr.stats.station),
                                    str(st_data[istat].stats.station))
            npt.assert_string_equal(str(tr.stats.location),
                                    str(st_data[istat].stats.location))
            npt.assert_string_equal(str(tr.stats.network),
                                    str(st_data[istat].stats.network))

            npt.assert_equal(tr.stats.npts, st_data[istat].stats.npts)
            npt.assert_allclose(tr.stats.delta, st_data[istat].stats.delta)
            npt.assert_allclose(float(tr.stats.starttime),
                                float(st_data[istat].stats.starttime))
Ejemplo n.º 2
0
def test_seismogram_extraction(all_remote_dbs):
    """
    Test the seismogram extraction from local and remote databases.
    """
    # Remote and local database.
    r_db = all_remote_dbs
    l_db = instaseis.open_db(r_db._client.filepath)
    # Mock responses to get the tornado testing to work.
    _add_callback(r_db._client)

    source = instaseis.Source(
        latitude=4., longitude=3.0, depth_in_m=0, m_rr=4.71e+17, m_tt=3.81e+17,
        m_pp=-4.74e+17, m_rt=3.99e+17, m_rp=-8.05e+17, m_tp=-1.23e+17)

    receiver = instaseis.Receiver(latitude=10., longitude=20., depth_in_m=None)

    components = r_db.available_components

    kwargs = {"source": source, "receiver": receiver,
              "components": components}
    _compare_streams(r_db, l_db, kwargs)

    # Test velocity and acceleration.
    kwargs = {"source": source, "receiver": receiver,
              "components": components, "kind": "velocity"}
    _compare_streams(r_db, l_db, kwargs)
    kwargs = {"source": source, "receiver": receiver,
              "components": components, "kind": "acceleration"}
    _compare_streams(r_db, l_db, kwargs)

    # Test remove source shift.
    kwargs = {"source": source, "receiver": receiver,
              "components": components,
              "remove_source_shift": False}
    _compare_streams(r_db, l_db, kwargs)

    # Test resampling.
    kwargs = {"source": source, "receiver": receiver,
              "components": components,
              "dt": 1.0, "kernelwidth": 6}
    _compare_streams(r_db, l_db, kwargs)

    # Test force source.
    if "displ_only" in r_db._client.filepath:
        source = instaseis.ForceSource(
            latitude=89.91, longitude=0.0, depth_in_m=12000,
            f_r=1.23E10,
            f_t=2.55E10,
            f_p=1.73E10)
        kwargs = {"source": source, "receiver": receiver}
        _compare_streams(r_db, l_db, kwargs)

    # Fix receiver depth, network, and station codes.
    receiver = instaseis.Receiver(latitude=10., longitude=20.,
                                  depth_in_m=0.0, station="ALTM",
                                  network="BW")

    kwargs = {"source": source, "receiver": receiver,
              "components": components}
    _compare_streams(r_db, l_db, kwargs)
Ejemplo n.º 3
0
 def __init__(self, path, kernelwidth=12):
     try:
         db = instaseis.open_db(path)
     except:
         Exception
     self.db = db
     self.kernelwidth = 12
Ejemplo n.º 4
0
def test_get_synthetics():
    # Try to load 3 stations, out of which 2 are in range for P
    db = instaseis.open_db('syngine://prem_a_20s')
    cat = obspy.read_events('./stfinv/data/virginia.xml')
    st = read('./stfinv/data/dis.II.BFO.00.BHZ')
    st += read('./stfinv/data/dis.GE.DAG..BHZ')
    st += read('./stfinv/data/dis.G.CRZF.00.BHZ')
    st_data, st_syn = st.get_synthetics(db=db, origin=cat[0].origins[0],
                                        out_dir='/tmp')

    npt.assert_equal(len(st_data), 2)
    npt.assert_equal(len(st_syn), 12)

    for istat in range(0, 2):
        channels = ['MPP', 'MRP', 'MRR', 'MRT', 'MTP', 'MTT']
        for channel in channels:
            st_test = st_syn.select(station=st_data[istat].stats.station,
                                    network=st_data[istat].stats.network,
                                    location=st_data[istat].stats.location,
                                    channel=channel)
            npt.assert_equal(len(st_test), 1)

        for tr in st_syn[istat * 6:(istat + 1) * 6]:
            npt.assert_string_equal(str(tr.stats.station),
                                    str(st_data[istat].stats.station))
            npt.assert_string_equal(str(tr.stats.location),
                                    str(st_data[istat].stats.location))
            npt.assert_string_equal(str(tr.stats.network),
                                    str(st_data[istat].stats.network))

            npt.assert_equal(tr.stats.npts, st_data[istat].stats.npts)
            npt.assert_allclose(tr.stats.delta, st_data[istat].stats.delta)
            npt.assert_allclose(float(tr.stats.starttime),
                                float(st_data[istat].stats.starttime))
Ejemplo n.º 5
0
def get_ns(wf1,source_conf,insta):
    
    # Nr of time steps in traces
    if insta:
        # get path to instaseis db
        #ToDo: ugly.
        dbpath = json.load(open(os.path.join(source_conf['project_path'],
            'config.json')))['wavefield_path']
        # open 
        db = instaseis.open_db(dbpath)
        # get a test seismogram to determine...
        stest = db.get_seismograms(source=instaseis.ForceSource(latitude=0.0,
            longitude=0.0),receiver=instaseis.Receiver(latitude=10.,
            longitude=0.0),dt=1./source_conf['sampling_rate'])[0]
        
        nt = stest.stats.npts
        Fs = stest.stats.sampling_rate
    else:
        with WaveField(wf1) as wf1:
            nt = int(wf1.stats['nt'])
            Fs = round(wf1.stats['Fs'],8)
    
    # Necessary length of zero padding for carrying out frequency domain correlations/convolutions
    n = next_fast_len(2*nt-1)     
    
    # Number of time steps for synthetic correlation
    n_lag = int(source_conf['max_lag'] * Fs)
    if nt - 2*n_lag <= 0:
        click.secho('Resetting maximum lag to %g seconds: Synthetics are too\
 short for a maximum lag of %g seconds.' %(nt//2/Fs,n_lag/Fs))
        n_lag = nt // 2
        
    n_corr = 2*n_lag + 1
    
    return nt,n,n_corr,Fs
Ejemplo n.º 6
0
def all_remote_dbs(request):
    client = create_async_client(request.param, None)

    _add_callback(client)

    db = instaseis.open_db("http://localhost:%i" % client.port)
    db._client = client
    return db
Ejemplo n.º 7
0
def all_remote_dbs(request):
    client = create_async_client(request.param, None)

    _add_callback(client)

    db = instaseis.open_db("http://localhost:%i" % client.port)
    db._client = client
    return db
Ejemplo n.º 8
0
def compare_dbs(seed, databases):
    if seed:
        random.seed(seed)
    reference = instaseis.open_db(databases[0])
    others = [instaseis.open_db(_i) for _i in databases[1:]]

    max_depth = reference.info.max_radius - reference.info.min_radius

    while True:
        receiver = instaseis.Receiver(
            latitude=random.random() * 180.0 - 90.0,
            longitude=random.random() * 360.0 - 180.0,
            network="AB",
            station="CED",
        )
        source = instaseis.Source(
            latitude=random.random() * 180.0 - 90.0,
            longitude=random.random() * 360.0 - 180.0,
            depth_in_m=random.random() * max_depth,
            m_rr=4.710000e24 / 1e7,
            m_tt=3.810000e22 / 1e7,
            m_pp=-4.740000e24 / 1e7,
            m_rt=3.990000e23 / 1e7,
            m_rp=-8.050000e23 / 1e7,
            m_tp=-1.230000e24 / 1e7,
            origin_time=obspy.UTCDateTime(2011, 1, 2, 3, 4, 5),
        )

        print("======")

        ref = reference.get_seismograms(source=source,
                                        receiver=receiver,
                                        components="ZNERT")

        oth = [
            _i.get_seismograms(source=source,
                               receiver=receiver,
                               components="ZNERT") for _i in others
        ]

        for _i, _j in zip(oth, others):
            print(_j.info.directory, ":", ref == _i)
            assert ref == _i, str(source) + "\n" + str(receiver)
Ejemplo n.º 9
0
    def prepare(self):

        # initialize parameters
        self.Fs = self.config['wavefield_sampling_rate']
        self.npts = int(self.config['wavefield_duration'] * self.Fs)
        self.ntraces = self.sourcegrid.shape[-1]
        self.data_quantity = self.config['synt_data']

        if self.config['wavefield_domain'] == 'fourier':
            self.fdomain = True
            if self.npts % 2 == 1:
                self.npad = 2 * self.npts - 2
            else:
                self.npad = 2 * self.npts
        elif self.config['wavefield_domain'] == 'time':
            self.fdomain = False
            self.npad = next_fast_len(2 * self.npts - 1)
        else:
            raise ValueError('Unknown domain {}.'.format(
                self.config['wavefield_domain']))
        self.freq = np.fft.rfftfreq(self.npad, d=1.0 / self.Fs)

        # Apply a filter
        if self.config['wavefield_filter'] is not None:
            freq_nyq = self.Fs / 2.0  # Nyquist

            if freq_nyq < self.config['wavefield_filter'][1]:
                warn("Selected upper freq > Nyquist, \
reset to 95\% of Nyquist freq.")
            freq_minres = 1. / self.config['wavefield_duration']
            # lowest resolved
            freq_max = min(0.999 * freq_nyq,
                           self.config['wavefield_filter'][1])
            freq_min = max(freq_minres, self.config['wavefield_filter'][0])

            f0 = freq_min / freq_nyq
            f1 = freq_max / freq_nyq
            self.filter = butter(4, [f0, f1], 'bandpass')
        else:
            self.filter = None

        # if using instaseis: Find and open database
        if self.config['wavefield_type'] == 'instaseis':
            path_to_db = self.config['wavefield_path']
            self.db = instaseis.open_db(path_to_db)
            if self.db.info['length'] < self.npts / self.Fs:
                warn("Resetting wavefield duration to axisem database length.")
                fsrc = instaseis.ForceSource(latitude=0.0,
                                             longitude=0.0,
                                             f_r=1.0)
                rec = instaseis.Receiver(latitude=0.0, longitude=0.0)
                test = self.db.get_seismograms(source=fsrc,
                                               receiver=rec,
                                               dt=1. / self.Fs)
                self.npts = test[0].stats.npts
Ejemplo n.º 10
0
def play(path_LF, path_HF, nevent=40):
    global events
    events = []
    fnam = os.path.join(DATA_PATH, 'VBB_3days_VEL.mseed')
    if not os.path.exists(fnam):
        fnam = os.path.join(DATA_PATH, 'VBB_3days_VEL_SYNT.mseed')

    st = read(fnam)

    db_HF = instaseis.open_db(path_HF)
    db_LF = instaseis.open_db(path_LF)
    for i in range(0, nevent):
        M, dist = create_event(M_min=2.0, M_max=4.0)
        if autoreject(M, dist):
            events.append([dist, M, 0])
            string = '%3d/%3d: Below threshold: Event in %d deg with M%3.1f'
        elif autoaccept(M, dist):
            events.append([dist, M, 1])
            string = '%3d/%3d: Above threshold: Event in %d deg with M%3.1f'
        else:
            st_LF, st_HF, t0 = select_and_add(st, db_HF, db_LF, M, dist)
            fig, ax = plot_spec(st_HF=st_HF, st_LF=st_LF, winlen_sec_LF=50)
            picks = pickify(fig, ax, M=M, dist=dist, t0=t0)
            if events[-1][-1] == 1:
                string = '%3d/%3d: Found event in %d deg with M%3.1f'
            else:
                string = '%3d/%3d: Missed event in %d deg with M%3.1f'
        print(string % (i, nevent, dist, M))
    with open('result.txt', 'a') as f:
        for event in events:
            plt.plot(event[0], event[1], 'o', c='C%d' % event[2])
            f.write('%5.1f,  %4.2f, %d \n' % (event[0], event[1], event[2]))

    plt.plot(-1, -1, 'o', c='C1', label='detected')
    plt.plot(-1, -1, 'o', c='C0', label='missed')
    plt.legend()
    plt.title('Your detection results after %d events' % nevent)
    plt.xlabel('distance [degree]')
    plt.ylabel('magnitude $M_W$')
    plt.ylim(2, 4)
    plt.xlim(0, 180)
    plt.show()
Ejemplo n.º 11
0
    def setup(self):
        self.db = open_db(self.path, read_on_demand=False,
                          buffer_size_in_mb=250)
        self.counter = 0
        self.current_depth_counter = 0
        # Depth increases in 1 km steps up to a depth of 25 km.
        self.depths = range(0, 25001, 1000)
        self.depth_count = len(self.depths)

        # Fix Receiver
        self.rec = Receiver(latitude=45.0, longitude=45.0)
Ejemplo n.º 12
0
    def setup(self):
        self.db = open_db(self.path,
                          read_on_demand=False,
                          buffer_size_in_mb=250)
        self.counter = 0
        self.current_depth_counter = 0
        # Depth increases in 1 km steps up to a depth of 25 km.
        self.depths = range(0, 25001, 1000)
        self.depth_count = len(self.depths)

        # Fix Receiver
        self.rec = Receiver(latitude=45.0, longitude=45.0)
Ejemplo n.º 13
0
def test_get_grf6():

    db = instaseis.open_db('syngine://prem_a_20s')

    reclat = 10.0
    reclon = 10.0

    cat = obspy.read_events('./stfinv/data/virginia.xml')
    # Define Moment tensor
    tensor = cat[0].focal_mechanisms[0].moment_tensor.tensor
    # tensor = obspy.core.event.Tensor(m_rr=1e20, m_pp=-2e20, m_tt=0.5e20,
    #                                  m_rt=5e19, m_rp=-7e19, m_tp=-1e20)

    # Get a reference trace with normal instaseis
    rec = instaseis.Receiver(latitude=reclat,
                             longitude=reclon,
                             network='XX',
                             station='YY',
                             location='00')

    # src = instaseis.Source(latitude=evlat, longitude=evlon,
    #                        m_rr=tensor.m_rr,
    #                        m_tt=tensor.m_tt,
    #                        m_pp=tensor.m_pp,
    #                        m_tp=tensor.m_tp,
    #                        m_rt=tensor.m_rt,
    #                        m_rp=tensor.m_rp,
    #                        depth_in_m=evdepth)
    src = instaseis.Source.parse(cat[0])

    st_ref = db.get_seismograms(src, rec, dt=0.1, components='Z')

    # Get a synthetic
    st_grf6 = Stream()
    st_grf6 += get_grf6(db,
                        origin=cat[0].origins[0],
                        rec_lat=reclat,
                        rec_lon=reclon,
                        dt=0.1,
                        stats=st_ref[0].stats,
                        depth_in_m=cat[0].origins[0].depth)

    st_synth = st_grf6.calc_synthetic_from_grf6(st_ref, tensor=tensor, stf=[1])

    # st_synth.plot(outfile='synth.png')
    # st_ref.plot(outfile='ref.png')

    npt.assert_allclose(st_ref[0].data,
                        st_synth[0].data,
                        rtol=5e-1,
                        atol=1e-7,
                        err_msg='Synthetic not the same')
Ejemplo n.º 14
0
def main():
    # === Read input file ===
    param = toml.load(toml_path, _dict=dict)

    # === Read the mSEED file ===
    get_parameters = Get_Parameters()
    mSEED_path = get_parameters.Get_Path(param['directory'],
                                         param['mSEED_file'])
    stream = obspy.read(mSEED_path)
    PRIOR = get_parameters.PRIOR(stream, param['PRIOR'])

    # === GET PRIOR ===
    PRIOR = get_parameters.Get_ranges(PRIOR)  # WHEN RUN IS NOT YET DONE

    # === Cut the BW windows (P&S) ===
    BW_obs = Cut_windows(PRIOR['VELOC_taup'],
                         P_HP=PRIOR['P_HP'],
                         P_LP=PRIOR['P_LP'],
                         S_HP=PRIOR['S_HP'],
                         S_LP=PRIOR['S_LP'],
                         Pre_P=PRIOR['Pre_P'],
                         Pre_S=PRIOR['Pre_S'],
                         Post_P=PRIOR['Post_P'],
                         Post_S=PRIOR['Post_S'],
                         global_P_shift=PRIOR['Global_P_shift'],
                         global_S_shift=PRIOR['Global_S_shift'],
                         zero_phase=PRIOR['Zero_Phase'],
                         Order=PRIOR['Order'],
                         Taper=PRIOR['Taper_obs'],
                         Taper_len=PRIOR['Taper_len'],
                         Zero_len=PRIOR['Zero_len'])
    if PRIOR['P_pick'] == None or PRIOR['S_pick'] == None:
        BW_obs.Get_bw_windows(stream,
                              PRIOR['epi_s'],
                              PRIOR['depth_s'],
                              PRIOR['origin_time'],
                              MANUAL=False)
    else:
        BW_obs.Get_bw_windows(stream,
                              PRIOR['P_pick'],
                              PRIOR['S_pick'],
                              PRIOR['origin_time'],
                              MANUAL=True)

    ## === 2. Get Green's Functions ===
    db = instaseis.open_db(PRIOR['VELOC'])
    depth = 58691.9

    a = 1
Ejemplo n.º 15
0
def compare_dbs(seed, databases):
    if seed:
        random.seed(seed)
    reference = instaseis.open_db(databases[0])
    others = [instaseis.open_db(_i) for _i in databases[1:]]

    max_depth = (reference.info.max_radius - reference.info.min_radius)

    while True:
        receiver = instaseis.Receiver(
            latitude=random.random() * 180.0 - 90.0,
            longitude=random.random() * 360.0 - 180.0,
            network="AB", station="CED")
        source = instaseis.Source(
            latitude=random.random() * 180.0 - 90.0,
            longitude=random.random() * 360.0 - 180.0,
            depth_in_m=random.random() * max_depth,
            m_rr=4.710000e+24 / 1E7,
            m_tt=3.810000e+22 / 1E7,
            m_pp=-4.740000e+24 / 1E7,
            m_rt=3.990000e+23 / 1E7,
            m_rp=-8.050000e+23 / 1E7,
            m_tp=-1.230000e+24 / 1E7,
            origin_time=obspy.UTCDateTime(2011, 1, 2, 3, 4, 5))

        print('======')

        ref = reference.get_seismograms(source=source, receiver=receiver,
                                        components="ZNERT")

        oth = [_i.get_seismograms(source=source, receiver=receiver,
                                  components="ZNERT") for _i in others]

        for _i, _j in zip(oth, others):
            print(_j.info.directory, ":", ref == _i)
            assert ref == _i, str(source) + "\n" + str(receiver)
Ejemplo n.º 16
0
    def _calc(self):
        db = instaseis.open_db(self.db_name)

        nazi = 8

        dt = min([self.dt, db.info.dt])

        lats = np.linspace(start=-90., stop=90., num=self.ndist)
        lons = np.linspace(start=-180, stop=180., num=nazi, endpoint=False)

        src = instaseis.Source(latitude=90.0,
                               longitude=0.0,
                               depth_in_m=self.depth_in_m,
                               m_rr=-1.670000e+28 / 1e7,
                               m_tt=3.820000e+27 / 1e7,
                               m_pp=1.280000e+28 / 1e7,
                               m_rt=-7.840000e+27 / 1e7,
                               m_rp=-3.570000e+28 / 1e7,
                               m_tp=1.550000e+27 / 1e7)

        npts = _get_npts(db, dt)

        stack_R = np.zeros(shape=(self.ndist, nazi, npts))
        stack_T = np.zeros(shape=(self.ndist, nazi, npts))
        stack_Z = np.zeros(shape=(self.ndist, nazi, npts))

        for ilat in tqdm(range(0, int(self.ndist))):
            lat = lats[ilat]

            for ilon in range(0, nazi):
                lon = lons[ilon]
                rec = instaseis.Receiver(latitude=lat,
                                         longitude=lon,
                                         network="AB",
                                         station="%d" % lon)

                st = db.get_seismograms(src,
                                        rec,
                                        components='RTZ',
                                        kind='velocity',
                                        dt=dt,
                                        remove_source_shift=True)

                stack_R[ilat, ilon, :] = st.select(channel='*R')[0].data
                stack_T[ilat, ilon, :] = st.select(channel='*T')[0].data
                stack_Z[ilat, ilon, :] = st.select(channel='*Z')[0].data

        return stack_R, stack_T, stack_Z, st[0].stats, db.info
Ejemplo n.º 17
0
def test_get_grf6():

    db = instaseis.open_db('syngine://prem_a_20s')

    reclat = 10.0
    reclon = 10.0

    cat = obspy.read_events('./stfinv/data/virginia.xml')
    # Define Moment tensor
    tensor = cat[0].focal_mechanisms[0].moment_tensor.tensor
    # tensor = obspy.core.event.Tensor(m_rr=1e20, m_pp=-2e20, m_tt=0.5e20,
    #                                  m_rt=5e19, m_rp=-7e19, m_tp=-1e20)

    # Get a reference trace with normal instaseis
    rec = instaseis.Receiver(latitude=reclat, longitude=reclon,
                             network='XX', station='YY', location='00')

    # src = instaseis.Source(latitude=evlat, longitude=evlon,
    #                        m_rr=tensor.m_rr,
    #                        m_tt=tensor.m_tt,
    #                        m_pp=tensor.m_pp,
    #                        m_tp=tensor.m_tp,
    #                        m_rt=tensor.m_rt,
    #                        m_rp=tensor.m_rp,
    #                        depth_in_m=evdepth)
    src = instaseis.Source.parse(cat[0])

    st_ref = db.get_seismograms(src, rec, dt=0.1, components='Z')

    # Get a synthetic
    st_grf6 = Stream()
    st_grf6 += get_grf6(db, origin=cat[0].origins[0],
                        rec_lat=reclat, rec_lon=reclon,
                        dt=0.1, stats=st_ref[0].stats,
                        depth_in_m=cat[0].origins[0].depth)

    st_synth = st_grf6.calc_synthetic_from_grf6(st_ref,
                                                tensor=tensor,
                                                stf=[1])

    # st_synth.plot(outfile='synth.png')
    # st_ref.plot(outfile='ref.png')

    npt.assert_allclose(st_ref[0].data, st_synth[0].data,
                        rtol=5e-1,
                        atol=1e-7,
                        err_msg='Synthetic not the same')
Ejemplo n.º 18
0
def create_insta_from_invcat(network, event, database):
    """
    This function creates synthetic data using the given network and
    event information, with the database of instaseis

    :param network: Desired Network, for which the data is generated
    :type  network: obspy.core.inventory.Network

    :param event: Event, for wich the data is generated. The event must have
    stored the moment tensor (e.g. given by glogalcmt.org)
    :type  event: obspy.core.event.Event

    :param database: Link to the database, e.g. the path on your harddrive
    :type  database: str
    """

    db = instaseis.open_db(database)

    tofe = event.origins[0].time
    lat = event.origins[0].latitude
    lon = event.origins[0].longitude
    depth = event.origins[0].depth

    source = instaseis.Source(latitude=lat,
                              longitude=lon,
                              depth_in_m=depth,
                              m_rr=event.MomentTensor.m_rr,
                              m_tt=event.MomentTensor.m_tt,
                              m_pp=event.MomentTensor.m_pp,
                              m_rt=event.MomentTensor.m_rt,
                              m_rp=event.MomentTensor.m_rp,
                              m_tp=event.MomentTensor.m_tp,
                              origin_time=tofe)

    stream = Stream()
    tmp = []
    for station in network:
        rec = instaseis.Receiver(latitude=str(station.latitude),
                                 longitude=str(station.longitude),
                                 network=str(network.code),
                                 station=str(station.code))
        tmp.append(db.get_seismograms(source=source, receiver=rec))

    for x in tmp:
        stream += x

    return stream
Ejemplo n.º 19
0
    def on_select_folder_button_released(self):
        pwd = os.getcwd()
        self.folder = str(QtGui.QFileDialog.getExistingDirectory(self, "Choose Directory", pwd))
        if not self.folder:
            return
        self.instaseis_db = open_db(self.folder)

        # Adjust depth slider to the DB.
        max_rad = self.instaseis_db.info.max_radius / 1e3
        min_rad = self.instaseis_db.info.min_radius / 1e3
        self.ui.depth_slider.setMinimum(min_rad - max_rad)
        self.ui.depth_slider.setMaximum(0)

        self._setup_finite_source()

        self.plot_map()
        self.update()
        self.set_info()
def test_info(syngine_client):
    """
    Make sure the /info route is similar enough.
    """
    # Syngine and local database.
    s_db = syngine_client
    l_db = instaseis.open_db(db_path)

    s_info = copy.deepcopy(s_db.info)
    l_info = copy.deepcopy(l_db.info)

    np.testing.assert_allclose(s_info.slip, l_info.slip)
    np.testing.assert_allclose(s_info.sliprate, l_info.sliprate)

    for key in ["directory", "slip", "sliprate"]:
        del s_info[key]
        del l_info[key]

    assert s_info.__dict__ == l_info.__dict__
Ejemplo n.º 21
0
def test_info(syngine_client):
    """
    Make sure the /info route is similar enough.
    """
    # Syngine and local database.
    s_db = syngine_client
    l_db = instaseis.open_db(db_path)

    s_info = copy.deepcopy(s_db.info)
    l_info = copy.deepcopy(l_db.info)

    np.testing.assert_allclose(s_info.slip, l_info.slip)
    np.testing.assert_allclose(s_info.sliprate, l_info.sliprate)

    for key in ["directory", "slip", "sliprate"]:
        del s_info[key]
        del l_info[key]

    assert s_info.__dict__ == l_info.__dict__
Ejemplo n.º 22
0
def open_files(data_path, event_file, db_path):
    # Read all data
    st = read(data_path)

    # Fill stream with station coordinates (in SAC header)
    st.get_station_coordinates()

    # Read event file
    try:
        cat = obspy.read_events(event_file)
        if len(cat) > 1:
            msg = 'File %s contains more than one event. Dont know, which one\
                   to chose. Please provide QuakeML file with just one event.'
            raise TypeError(msg)

        event = cat[0]
    except:
        event = get_event_from_obspydmt(event_file)

    origin = event.origins[0]

    db = instaseis.open_db(db_path)

    # Initialize with MT from event file
    try:
        tensor = event.focal_mechanisms[0].moment_tensor.tensor
    except IndexError:
        print('No moment tensor present, using explosion. Hilarity may ensue')
        tensor = obspy.core.event.Tensor(m_rr=1e20, m_tt=1e20, m_pp=1e20,
                                         m_rp=0.0, m_rt=0.0, m_tp=0.0)

    # Init with Gaussian STF with a length T:
    # log10 T propto 0.5*Magnitude
    # Scaling is such that the 5.7 Virginia event takes 5 seconds
    if len(event.magnitudes) > 0:
        duration = 10 ** (0.5 * (event.magnitudes[0].mag / 5.7)) * 5.0 / 2
    else:
        duration = 2.5

    print('Assuming duration of %8.1f sec' % duration)
    stf = signal.gaussian(duration * 2, duration / 4 / db.info.dt)

    return db, st, origin, tensor, stf
Ejemplo n.º 23
0
    def on_select_folder_button_released(self):
        pwd = os.getcwd()
        self.folder = str(
            QtGui.QFileDialog.getExistingDirectory(self, "Choose Directory",
                                                   pwd))
        if not self.folder:
            return
        self.instaseis_db = open_db(self.folder)

        # Adjust depth slider to the DB.
        max_rad = self.instaseis_db.info.max_radius / 1e3
        min_rad = self.instaseis_db.info.min_radius / 1e3
        self.ui.depth_slider.setMinimum(min_rad - max_rad)
        self.ui.depth_slider.setMaximum(0)

        self._setup_finite_source()

        self.plot_map()
        self.update()
        self.set_info()
Ejemplo n.º 24
0
    def on_open_instaseis_button_released(self):
        cwd = os.getcwd()
        self.folder = str(
            QtGui.QFileDialog.getExistingDirectory(
                self, "choose instaseis database folder", cwd))
        if not self.folder:
            return

        self.instaseis_db = instaseis.open_db(self.folder)
        self.source = instaseis.Source(latitude=self.ui.evla.value(),
                                       longitude=self.ui.evlo.value(),
                                       depth_in_m=self.ui.evdp.value() * 1000.,
                                       m_rr=self.ui.m_rr.value(),
                                       m_tt=self.ui.m_tt.value(),
                                       m_pp=self.ui.m_pp.value(),
                                       m_rt=self.ui.m_rt.value(),
                                       m_rp=self.ui.m_rp.value(),
                                       m_tp=self.ui.m_pp.value())
        self.receiver = instaseis.Receiver(latitude=self.ui.stla.value(),
                                           longitude=self.ui.stlo.value())
        self.stream = self.instaseis_db.get_seismograms(
            source=self.source,
            receiver=self.receiver,
            components=str(self.ui.component.currentText()),
            kind=str(self.ui.motion_type.currentText()),
            remove_source_shift=True)
        self.stream[0].stats.sac = {}
        self.stream[0].stats.sac['o'] = 0.0
        self.stream[0].stats.sac['gcarc'] = geodetics.locations2degrees(
            float(self.ui.evla.value()), float(self.ui.evlo.value()),
            float(self.ui.stla.value()), float(self.ui.stlo.value()))
        self.stream_copy = self.stream.copy()

        time = self.get_time_axis
        self.ui.window_start.setMaximum(time[-1])
        self.ui.window_end.setMaximum(time[-1])
        self.ui.window_start.setValue(time[0])
        self.ui.window_end.setValue(time[-1])
        self.instaseis = True
        self.plot_map()
        self.update()
Ejemplo n.º 25
0
    def on_select_remote_connection_button_released(self):
        text, ok = QtGui.QInputDialog.getText(
            self, "Remote Instaseis Connection",
            "Enter URL to remote Instaseis Server:")
        if not ok:
            return
        text = str(text)

        self.instaseis_db = open_db(text)

        # Adjust depth slider to the DB.
        max_rad = self.instaseis_db.info.max_radius / 1E3
        min_rad = self.instaseis_db.info.min_radius / 1E3
        self.ui.depth_slider.setMinimum(min_rad - max_rad)
        self.ui.depth_slider.setMaximum(0)

        self._setup_finite_source()

        self.plot_map()
        self.update()
        self.set_info()
Ejemplo n.º 26
0
    def on_select_remote_connection_button_released(self):
        text, ok = QtGui.QInputDialog.getText(
            self, "Remote Instaseis Connection",
            "Enter URL to remote Instaseis Server:")
        if not ok:
            return
        text = str(text)

        self.instaseis_db = open_db(text)

        # Adjust depth slider to the DB.
        max_rad = self.instaseis_db.info.max_radius / 1E3
        min_rad = self.instaseis_db.info.min_radius / 1E3
        self.ui.depth_slider.setMinimum(min_rad - max_rad)
        self.ui.depth_slider.setMaximum(0)

        self._setup_finite_source()

        self.plot_map()
        self.update()
        self.set_info()
Ejemplo n.º 27
0
def test_info(all_remote_dbs):
    """
    Make sure the info is identical no matter if it comes from a local or
    from a remote database.
    """
    # Remote and local database.
    r_db = all_remote_dbs
    l_db = instaseis.open_db(r_db._client.filepath)
    # Mock responses to get the tornado testing to work.
    _add_callback(r_db._client)

    r_info = copy.deepcopy(r_db.info)
    l_info = copy.deepcopy(l_db.info)

    np.testing.assert_allclose(r_info.slip, l_info.slip)
    np.testing.assert_allclose(r_info.sliprate, l_info.sliprate)

    for key in ["directory", "slip", "sliprate"]:
        del r_info[key]
        del l_info[key]

    assert r_info.__dict__ == l_info.__dict__
Ejemplo n.º 28
0
def test_info(all_remote_dbs):
    """
    Make sure the info is identical no matter if it comes from a local or
    from a remote database.
    """
    # Remote and local database.
    r_db = all_remote_dbs
    l_db = instaseis.open_db(r_db._client.filepath)
    # Mock responses to get the tornado testing to work.
    _add_callback(r_db._client)

    r_info = copy.deepcopy(r_db.info)
    l_info = copy.deepcopy(l_db.info)

    np.testing.assert_allclose(r_info.slip, l_info.slip)
    np.testing.assert_allclose(r_info.sliprate, l_info.sliprate)

    for key in ["directory", "slip", "sliprate"]:
        del r_info[key]
        del l_info[key]

    assert r_info.__dict__ == l_info.__dict__
Ejemplo n.º 29
0
def get_ns(all_conf, insta=False):
    # Nr of time steps in traces
    if insta:
        # get path to instaseis db
        dbpath = all_conf.config['wavefield_path']

        # open
        db = instaseis.open_db(dbpath)
        # get a test seismogram to determine...
        stest = db.get_seismograms(source=instaseis.ForceSource(latitude=0.0,
                                                                longitude=0.),
                                   receiver=instaseis.Receiver(latitude=10.,
                                                               longitude=0.),
                                   dt=1. / all_conf.config
                                   ['wavefield_sampling_rate'])[0]
        nt = stest.stats.npts
        Fs = stest.stats.sampling_rate
    else:
        any_wavefield = glob(os.path.join(all_conf.config['project_path'],
                                          'greens', '*.h5'))[-1]
        with WaveField(any_wavefield) as wf1:
            nt = int(wf1.stats['nt'])
            Fs = round(wf1.stats['Fs'], 8)
            n = wf1.stats['npad']
    # # Necessary length of zero padding
    # # for carrying out frequency domain correlations/convolutions
    # n = next_fast_len(2 * nt - 1)

    # Number of time steps for synthetic correlation
    n_lag = int(all_conf.source_config['max_lag'] * Fs)
    if nt - 2 * n_lag <= 0:
        n_lag_old = n_lag
        n_lag = nt // 2
        warn('Resetting maximum lag to %g seconds:\
 Synthetics are too short for %g seconds.' % (n_lag / Fs, n_lag_old / Fs))

    n_corr = 2 * n_lag + 1

    return nt, n, n_corr, Fs
Ejemplo n.º 30
0
def get_ns(wf1, source_conf, insta):

    # Nr of time steps in traces
    if insta:
        # get path to instaseis db
        #ToDo: ugly.
        dbpath = json.load(
            open(os.path.join(source_conf['project_path'],
                              'config.json')))['wavefield_path']
        # open
        db = instaseis.open_db(dbpath)
        # get a test seismogram to determine...
        stest = db.get_seismograms(source=instaseis.ForceSource(latitude=0.0,
                                                                longitude=0.0),
                                   receiver=instaseis.Receiver(latitude=10.,
                                                               longitude=0.0),
                                   dt=1. / source_conf['sampling_rate'])[0]

        nt = stest.stats.npts
        Fs = stest.stats.sampling_rate
    else:
        with WaveField(wf1) as wf1:
            nt = int(wf1.stats['nt'])
            Fs = round(wf1.stats['Fs'], 8)

    # Necessary length of zero padding for carrying out
    # frequency domain correlations/convolutions
    n = next_fast_len(2 * nt - 1)

    # Number of time steps for synthetic correlation
    n_lag = int(source_conf['max_lag'] * Fs)
    if nt - 2 * n_lag <= 0:
        click.secho('Resetting maximum lag to %g seconds: Synthetics are too\
 short for a maximum lag of %g seconds.' % (nt // 2 / Fs, n_lag / Fs))
        n_lag = nt // 2

    n_corr = 2 * n_lag + 1

    return nt, n, n_corr, Fs
Ejemplo n.º 31
0
def test_initialization_failures():
    """
    Tests various initialization failures for the remote instaseis db.
    """
    # Random error during init.
    with mock.patch("instaseis.database_interfaces.remote_instaseis_db"
                    ".RemoteInstaseisDB._download_url") as p:
        p.side_effect = ValueError("random")
        with pytest.raises(instaseis.InstaseisError) as err:
            instaseis.open_db("http://localhost:8765432")

    assert err.value.args[0] == ("Failed to connect to remote Instaseis "
                                 "server due to: random")

    # Invalid JSON returned.
    with mock.patch("instaseis.database_interfaces.remote_instaseis_db"
                    ".RemoteInstaseisDB._download_url") as p:
        p.return_value = {"a": "b"}
        with pytest.raises(instaseis.InstaseisError) as err:
            instaseis.open_db("http://localhost:8765432")

    assert err.value.args[0].startswith("Instaseis server responded with "
                                        "invalid response:")

    # Incompatible version number - should raise a warning.
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        with mock.patch("instaseis.database_interfaces.remote_instaseis_db"
                        ".RemoteInstaseisDB._download_url") as p_1:
            p_1.return_value = {
                "type": "Instaseis Remote Server",
                "datetime": "2001-01-01",
                "version": "test version",
            }
            try:
                instaseis.open_db("http://localhost:8765432")
            except Exception:
                pass

    assert len(w) == 1
    assert w[0].message.args[0].startswith("Instaseis versions on server")
Ejemplo n.º 32
0
def test_initialization_failures():
    """
    Tests various initialization failures for the remote instaseis db.
    """
    # Random error during init.
    with mock.patch("instaseis.database_interfaces.remote_instaseis_db"
                    ".RemoteInstaseisDB._download_url") as p:
        p.side_effect = ValueError("random")
        with pytest.raises(instaseis.InstaseisError) as err:
            instaseis.open_db("http://localhost:8765432")

    assert err.value.args[0] == ("Failed to connect to remote Instaseis "
                                 "server due to: random")

    # Invalid JSON returned.
    with mock.patch("instaseis.database_interfaces.remote_instaseis_db"
                    ".RemoteInstaseisDB._download_url") as p:
        p.return_value = {"a": "b"}
        with pytest.raises(instaseis.InstaseisError) as err:
            instaseis.open_db("http://localhost:8765432")

    assert err.value.args[0].startswith("Instaseis server responded with "
                                        "invalid response:")

    # Incompatible version number - should raise a warning.
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        with mock.patch("instaseis.database_interfaces.remote_instaseis_db"
                        ".RemoteInstaseisDB._download_url") as p_1:
            p_1.return_value = {"type": "Instaseis Remote Server",
                                "datetime": "2001-01-01",
                                "version": "test version"}
            try:
                instaseis.open_db("http://localhost:8765432")
            except:
                pass

    assert len(w) == 1
    assert w[0].message.args[0].startswith('Instaseis versions on server')
Ejemplo n.º 33
0
stf = 2 * np.exp(-(t/t_half)**2) * t / t_half**2
nsample = len(t)

f = open('../stf_test.txt', 'w')

f.write('%d %f\n'%(nsample, dt))
for y in stf:
    f.write('%f\n'%(y))

f.close()

print sum(stf*dt)



db = instaseis.open_db('../wavefield/bwd')
receiver = instaseis.Receiver(latitude = 30, longitude = 0, network='MC', station='kerner')
source = instaseis.Source(latitude = 90.0, 
                          longitude = 0.0, 
                          depth_in_m=0.0, 
                          m_rr=1.0e13,
                          m_tt=1.0e13, 
                          m_pp=1.0e13, 
                          m_rt=0.0, 
                          m_rp=0.0, 
                          m_tp=0.0,
                          time_shift=None, 
                          sliprate=stf, 
                          dt=dt)
source.resample_sliprate(db.info.dt, db.info.npts)
Ejemplo n.º 34
0
def compute_correlation(input_files, all_conf, nsrc, all_ns, taper,
                        insta=False):
    """
    Compute noise cross-correlations from two .h5 'wavefield' files.
    Noise source distribution and spectrum is given by starting_model.h5
    It is assumed that noise sources are delta-correlated in space.

    Metainformation: Include the reference station names for both stations
    from wavefield files, if possible. Do not include geographic information
    from .csv file as this might be error-prone. Just add the geographic
    info later if needed.
    """

    wf1, wf2 = input_files
    ntime, n, n_corr, Fs = all_ns
    ntraces = nsrc.src_loc[0].shape[0]
    correlation = np.zeros(n_corr)

    if insta:
        # open database
        dbpath = all_conf.config['wavefield_path']

        # open
        db = instaseis.open_db(dbpath)
        # get receiver locations
        station1 = wf1[0]
        station2 = wf2[0]
        lat1 = geograph_to_geocent(float(wf1[2]))
        lon1 = float(wf1[3])
        rec1 = instaseis.Receiver(latitude=lat1, longitude=lon1)
        lat2 = geograph_to_geocent(float(wf2[2]))
        lon2 = float(wf2[3])
        rec2 = instaseis.Receiver(latitude=lat2, longitude=lon2)
    else:
        wf1 = WaveField(wf1)
        wf2 = WaveField(wf2)
        station1 = wf1.stats['reference_station']
        station2 = wf2.stats['reference_station']

        # Make sure all is consistent
        if False in (wf1.sourcegrid[1, 0:10] == wf2.sourcegrid[1, 0:10]):
            raise ValueError("Wave fields not consistent.")

        if False in (wf1.sourcegrid[1, -10:] == wf2.sourcegrid[1, -10:]):
            raise ValueError("Wave fields not consistent.")

        if False in (wf1.sourcegrid[0, -10:] == nsrc.src_loc[0, -10:]):
            raise ValueError("Wave field and source not consistent.")

    # Loop over source locations
    print_each_n = max(5, round(max(ntraces // 5, 1), -1))
    for i in range(ntraces):

        # noise source spectrum at this location
        S = nsrc.get_spect(i)

        if S.sum() == 0.:
            # If amplitude is 0, continue. (Spectrum has 0 phase anyway.)
            continue

        if insta:
            # get source locations
            lat_src = geograph_to_geocent(nsrc.src_loc[1, i])
            lon_src = nsrc.src_loc[0, i]
            fsrc = instaseis.ForceSource(latitude=lat_src,
                                         longitude=lon_src,
                                         f_r=1.e12)
            Fs = all_conf.config['wavefield_sampling_rate']
            s1 = db.get_seismograms(source=fsrc, receiver=rec1,
                                    dt=1. / Fs)[0].data * taper
            s2 = db.get_seismograms(source=fsrc, receiver=rec2,
                                    dt=1. / Fs)[0].data * taper
            s1 = np.ascontiguousarray(s1)
            s2 = np.ascontiguousarray(s2)
            spec1 = np.fft.rfft(s1, n)
            spec2 = np.fft.rfft(s2, n)

        else:
            if not wf1.fdomain:
                # read Green's functions
                s1 = np.ascontiguousarray(wf1.data[i, :] * taper)
                s2 = np.ascontiguousarray(wf2.data[i, :] * taper)
                # Fourier transform for greater ease of convolution
                spec1 = np.fft.rfft(s1, n)
                spec2 = np.fft.rfft(s2, n)
            else:
                spec1 = np.ascontiguousarray(wf1.data[i, :])
                spec2 = np.ascontiguousarray(wf2.data[i, :])

        # convolve G1G2
        g1g2_tr = np.multiply(np.conjugate(spec1), spec2)

        # convolve noise source
        c = np.multiply(g1g2_tr, S)

        # transform back
        correlation += my_centered(np.fft.fftshift(np.fft.irfft(c, n)),
                                   n_corr) * nsrc.surf_area[i]
        # occasional info
        if i % print_each_n == 0 and all_conf.config['verbose']:
            print("Finished {} of {} source locations.".format(i, ntraces))
# end of loop over all source locations #######################################
    return(correlation, station1, station2)
Ejemplo n.º 35
0
def dosynthetics(database_path, quake_origins, inv=None, inv_list=None, stream=None, stream_file_name='insta_stream.pickle'):
	"""
	Routine to create synthetic data using instaseis.


	:param database_path:
	:type  database_path:

	:param quake_origins: 
	:type  quake_origins: dict from create_quake_origins
	"""
	if None in quake_origins.viewvalues():
		msg = '"None" values in quake_origins are not allowed, check and set again'
		raise IOError(msg)

	db = ins.open_db(database_path)
	source = ins.Source(
		latitude  	= quake_origins['latitude'], 
		longitude 	= quake_origins['longitude'], 
		depth_in_m 	= quake_origins['depth_in_m'],
		m_rr 		= quake_origins['m_rr'],
		m_tt 		= quake_origins['m_tt'],
		m_pp 		= quake_origins['m_pp'],
		m_rt 		= quake_origins['m_rt'],
		m_rp 		= quake_origins['m_rp'],
		m_tp 		= quake_origins['m_rp'],
		origin_time	= quake_origins['tofe']
		)

	# Prepare synthetic stream and inv.
	if stream_file_name:
		prefix 			= stream_file_name.split('.')
		inv_file_name	= prefix[0] + '_inv.xml'
		cat_file_name	= prefix[0] + '_cat.xml'

	else:
		inv_file_name 	= 'inv_tmp.xml'
		cat_file_name 	= 'cat_tmp.xml'


	receiver_synth 	= []
	invlist 		= []

	if stream:
		for trace in stream:
			receiver_synth.append(ins.Receiver(latitude=str(trace.stats.coordinates['latitude']), longitude=str(trace.stats.coordinates['longitude']),
								 network=str(trace.stats.network), station=str(trace.stats.station)))

	elif inv and not stream:
		network = inv[0]
		for station in network:
			receiver_synth.append(ins.Receiver(latitude=str(station.latitude), longitude=str(station.longitude), network=str(network.code), station=str(station.code) ))

	elif inv_list:
		for i, station in enumerate(inv_list):
			receiver_synth.append(ins.Receiver(latitude=str(station[0]), longitude=str(station[1]), network=str('X'), station='X' + str(i) ))

	st_synth = []    
	for i in range(len(x)):
		st_synth.append(db.get_seismograms(source=source, receiver=receiver_synth[i]))

	stream=st_synth[0]
	for i in range(len(st_synth))[1:]:
		stream.append(st_synth[i][0])

	writeQuakeML(cat_file_name, quake_origins['tofe'], quake_origins['latitude'], quake_origins['longitude'], quake_origins['depth_in_m'])

	if not inv:
		writeStationML(inv_file_name)
		inv=read_inv(inv_file_name)

	cat=read_cat(cat_file_name)		

	if stream_file_name:
		stream.write(stream_file_name, format='pickle')
	else:
		os.remove(cat_file_name)	
		if inv:
			os.remove(inv_file_name)



	return stream, inv, cat
def test_finite_source_retrieval(reciprocal_clients, usgs_param):
    """
    Tests if the finite sources requested from the server are identical to
    the one requested with the local instaseis client with some required
    tweaks.
    """
    client = reciprocal_clients

    db = instaseis.open_db(client.filepath)

    basic_parameters = {
        "receiverlongitude": 11,
        "receiverlatitude": 22,
        "receiverdepthinmeters": 0,
        "format": "miniseed"
    }

    with io.open(usgs_param, "rb") as fh:
        body = fh.read()

    # default parameters
    params = copy.deepcopy(basic_parameters)
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats._format == "MSEED"

    # Parse the finite source.
    fs = _parse_finite_source(usgs_param)
    rec = instaseis.Receiver(latitude=22,
                             longitude=11,
                             network="XX",
                             station="SYN",
                             location="SE")

    st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec)
    # The origin time is the time of the first sample in the route.
    for tr in st_db:
        # Cut away the first ten samples as they have been previously added.
        tr.data = tr.data[10:]
        tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1)

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing is very similar but not equal due to floating point
        # accuracy.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        tr_server.stats.delta = tr_db.stats.delta
        del tr_server.stats._format
        del tr_server.stats.mseed

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data, tr_server.data)

    # Once again but this time request a SAC file.
    params = copy.deepcopy(basic_parameters)
    params["format"] = "saczip"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_server = obspy.Stream()
    zip_obj = zipfile.ZipFile(request.buffer)
    for name in zip_obj.namelist():
        st_server += obspy.read(io.BytesIO(zip_obj.read(name)))
    for tr in st_server:
        assert tr.stats._format == "SAC"

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing is very similar but not equal due to floating point
        # accuracy.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        tr_server.stats.delta = tr_db.stats.delta
        del tr_server.stats._format
        del tr_server.stats.sac

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data, tr_server.data)

    # One with a label.
    params = copy.deepcopy(basic_parameters)
    params["label"] = "random_things"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200

    cd = request.headers["Content-Disposition"]
    assert cd.startswith("attachment; filename=random_things_")
    assert cd.endswith(".mseed")

    # One simulating a crash in the underlying function.
    params = copy.deepcopy(basic_parameters)

    with mock.patch("instaseis.database_interfaces.base_instaseis_db"
                    ".BaseInstaseisDB.get_seismograms_finite_source") as p:
        p.side_effect = ValueError("random crash")
        request = client.fetch(_assemble_url('finite_source', **params),
                               method="POST",
                               body=body)

    assert request.code == 400
    assert request.reason == ("Could not extract finite source seismograms. "
                              "Make sure, the parameters are valid, and the "
                              "depth settings are correct.")

    # Simulating a logic error that should not be able to happen.
    params = copy.deepcopy(basic_parameters)
    with mock.patch("instaseis.database_interfaces.base_instaseis_db"
                    ".BaseInstaseisDB.get_seismograms_finite_source") as p:
        # Longer than the database returned stream thus the endtime is out
        # of bounds.
        st = obspy.read()

        p.return_value = st
        request = client.fetch(_assemble_url('finite_source', **params),
                               method="POST",
                               body=body)

    assert request.code == 500
    assert request.reason.startswith("Endtime larger than the extracted "
                                     "endtime")

    # One more with resampling parameters and different units.
    params = copy.deepcopy(basic_parameters)
    # We must have a sampling rate that cleanly fits in the existing one,
    # otherwise we cannot fake the cutting.
    dt_new = 24.724845445855724 / 10
    params["dt"] = dt_new
    params["kernelwidth"] = 2
    params["units"] = "acceleration"

    st_db = db.get_seismograms_finite_source(sources=fs,
                                             receiver=rec,
                                             dt=dt_new,
                                             kernelwidth=2,
                                             kind="acceleration")
    # The origin time is the time of the first sample in the route.
    for tr in st_db:
        # Cut away the first ten samples as they have been previously added.
        tr.data = tr.data[100:]
        tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1)

    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)

    # Cut some parts in the middle to avoid any potential boundary effects.
    st_db.trim(obspy.UTCDateTime(1900, 1, 1, 0, 4),
               obspy.UTCDateTime(1900, 1, 1, 0, 14))
    st_server.trim(obspy.UTCDateTime(1900, 1, 1, 0, 4),
                   obspy.UTCDateTime(1900, 1, 1, 0, 14))

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing and times are very similar but not identical due to
        # floating point inaccuracies in the arithmetics.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        np.testing.assert_allclose(tr_server.stats.starttime.timestamp,
                                   tr_db.stats.starttime.timestamp)
        tr_server.stats.delta = tr_db.stats.delta
        tr_server.stats.starttime = tr_db.stats.starttime
        del tr_server.stats._format
        del tr_server.stats.mseed
        del tr_server.stats.processing
        del tr_db.stats.processing

        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data,
                                   tr_server.data,
                                   rtol=1E-7,
                                   atol=tr_db.data.ptp() * 1E-7)

    # Testing network and station code parameters.
    # Default values.
    params = copy.deepcopy(basic_parameters)
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats.network == "XX"
        assert tr.stats.station == "SYN"
        assert tr.stats.location == "SE"

    # Setting all three.
    params = copy.deepcopy(basic_parameters)
    params["networkcode"] = "AA"
    params["stationcode"] = "BB"
    params["locationcode"] = "CC"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats.network == "AA"
        assert tr.stats.station == "BB"
        assert tr.stats.location == "CC"

    # Setting only the location code.
    params = copy.deepcopy(basic_parameters)
    params["locationcode"] = "AA"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats.network == "XX"
        assert tr.stats.station == "SYN"
        assert tr.stats.location == "AA"

    # Test the scale parameter.
    params = copy.deepcopy(basic_parameters)
    params["scale"] = 33.33
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats._format == "MSEED"

    # Parse the finite source.
    fs = _parse_finite_source(usgs_param)
    rec = instaseis.Receiver(latitude=22,
                             longitude=11,
                             network="XX",
                             station="SYN",
                             location="SE")

    st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec)
    # The origin time is the time of the first sample in the route.
    for tr in st_db:
        # Multiply with scale parameter.
        tr.data *= 33.33
        # Cut away the first ten samples as they have been previously added.
        tr.data = tr.data[10:]
        tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1)

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing is very similar but not equal due to floating point
        # accuracy.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        tr_server.stats.delta = tr_db.stats.delta
        del tr_server.stats._format
        del tr_server.stats.mseed

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data,
                                   tr_server.data,
                                   atol=1E-6 * tr_db.data.ptp())
Ejemplo n.º 37
0
                    help='Number of seismograms to be calculated for each '
                         'benchmark. Overwrites any time limitations if '
                         'given.')
parser.add_argument('--save', action="store_true",
                    help='save output to txt file')
args = parser.parse_args()
path = os.path.abspath(args.folder) if "://" not in args.folder \
    else args.folder

print(colorama.Fore.GREEN + 79 * "=" + "\nInstaseis Benchmark Suite\n")
print("It enables to gauge the speed of Instaseis for a certain DB.")
print(79 * "=" + colorama.Fore.RESET)
print(colorama.Fore.RED + "\nIt does not deal with OS level caches! So "
      "interpret the results accordingly!\n" + colorama.Fore.RESET)

db = open_db(path, read_on_demand=True, buffer_size_in_mb=0)
if not db.info.is_reciprocal:
    print("Benchmark currently only works with a reciprocal database.")
    sys.exit(1)
print(db)


# Recursively get all subclasses of the benchmark class.
def get_subclasses(cls):
    subclasses = []

    sub = cls.__subclasses__()
    subclasses.extend(sub)

    for subclass in sub:
        subclasses.extend(get_subclasses(subclass))
Ejemplo n.º 38
0
 def setup(self):
     self.db = open_db(self.path, read_on_demand=True, buffer_size_in_mb=0)
     self.max_depth = self.db.info.max_radius - self.db.info.min_radius
Ejemplo n.º 39
0
def test_finite_source_retrieval(reciprocal_clients, usgs_param):
    """
    Tests if the finite sources requested from the server are identical to
    the one requested with the local instaseis client with some required
    tweaks.
    """
    client = reciprocal_clients

    db = instaseis.open_db(client.filepath)

    basic_parameters = {
        "receiverlongitude": 11,
        "receiverlatitude": 22,
        "receiverdepthinmeters": 0,
        "format": "miniseed"}

    with io.open(usgs_param, "rb") as fh:
        body = fh.read()

    # default parameters
    params = copy.deepcopy(basic_parameters)
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats._format == "MSEED"

    # Parse the finite source.
    fs = _parse_finite_source(usgs_param)
    rec = instaseis.Receiver(latitude=22, longitude=11, network="XX",
                             station="SYN", location="SE")

    st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec)
    # The origin time is the time of the first sample in the route.
    for tr in st_db:
        # Cut away the first ten samples as they have been previously added.
        tr.data = tr.data[10:]
        tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1)

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing is very similar but not equal due to floating point
        # accuracy.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        tr_server.stats.delta = tr_db.stats.delta
        del tr_server.stats._format
        del tr_server.stats.mseed

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data, tr_server.data)

    # Once again but this time request a SAC file.
    params = copy.deepcopy(basic_parameters)
    params["format"] = "saczip"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_server = obspy.Stream()
    zip_obj = zipfile.ZipFile(request.buffer)
    for name in zip_obj.namelist():
        st_server += obspy.read(io.BytesIO(zip_obj.read(name)))
    for tr in st_server:
        assert tr.stats._format == "SAC"

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing is very similar but not equal due to floating point
        # accuracy.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        tr_server.stats.delta = tr_db.stats.delta
        del tr_server.stats._format
        del tr_server.stats.sac

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data, tr_server.data)

    # One with a label.
    params = copy.deepcopy(basic_parameters)
    params["label"] = "random_things"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200

    cd = request.headers["Content-Disposition"]
    assert cd.startswith("attachment; filename=random_things_")
    assert cd.endswith(".mseed")

    # One simulating a crash in the underlying function.
    params = copy.deepcopy(basic_parameters)

    with mock.patch("instaseis.database_interfaces.base_instaseis_db"
                    ".BaseInstaseisDB.get_seismograms_finite_source") as p:
        p.side_effect = ValueError("random crash")
        request = client.fetch(_assemble_url('finite_source', **params),
                               method="POST", body=body)

    assert request.code == 400
    assert request.reason == ("Could not extract finite source seismograms. "
                              "Make sure, the parameters are valid, and the "
                              "depth settings are correct.")

    # Simulating a logic error that should not be able to happen.
    params = copy.deepcopy(basic_parameters)
    with mock.patch("instaseis.database_interfaces.base_instaseis_db"
                    ".BaseInstaseisDB.get_seismograms_finite_source") as p:
        # Longer than the database returned stream thus the endtime is out
        # of bounds.
        st = obspy.read()

        p.return_value = st
        request = client.fetch(_assemble_url('finite_source', **params),
                               method="POST", body=body)

    assert request.code == 500
    assert request.reason.startswith("Endtime larger than the extracted "
                                     "endtime")

    # One more with resampling parameters and different units.
    params = copy.deepcopy(basic_parameters)
    # We must have a sampling rate that cleanly fits in the existing one,
    # otherwise we cannot fake the cutting.
    dt_new = 24.724845445855724 / 10
    params["dt"] = dt_new
    params["kernelwidth"] = 2
    params["units"] = "acceleration"

    st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec,
                                             dt=dt_new, kernelwidth=2,
                                             kind="acceleration")
    # The origin time is the time of the first sample in the route.
    for tr in st_db:
        # Cut away the first ten samples as they have been previously added.
        tr.data = tr.data[100:]
        tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1)

    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)

    # Cut some parts in the middle to avoid any potential boundary effects.
    st_db.trim(obspy.UTCDateTime(1900, 1, 1, 0, 4),
               obspy.UTCDateTime(1900, 1, 1, 0, 14))
    st_server.trim(obspy.UTCDateTime(1900, 1, 1, 0, 4),
                   obspy.UTCDateTime(1900, 1, 1, 0, 14))

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing and times are very similar but not identical due to
        # floating point inaccuracies in the arithmetics.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        np.testing.assert_allclose(tr_server.stats.starttime.timestamp,
                                   tr_db.stats.starttime.timestamp)
        tr_server.stats.delta = tr_db.stats.delta
        tr_server.stats.starttime = tr_db.stats.starttime
        del tr_server.stats._format
        del tr_server.stats.mseed
        del tr_server.stats.processing
        del tr_db.stats.processing

        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data, tr_server.data,
                                   rtol=1E-7, atol=tr_db.data.ptp() * 1E-7)

    # Testing network and station code parameters.
    # Default values.
    params = copy.deepcopy(basic_parameters)
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats.network == "XX"
        assert tr.stats.station == "SYN"
        assert tr.stats.location == "SE"

    # Setting all three.
    params = copy.deepcopy(basic_parameters)
    params["networkcode"] = "AA"
    params["stationcode"] = "BB"
    params["locationcode"] = "CC"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats.network == "AA"
        assert tr.stats.station == "BB"
        assert tr.stats.location == "CC"

    # Setting only the location code.
    params = copy.deepcopy(basic_parameters)
    params["locationcode"] = "AA"
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats.network == "XX"
        assert tr.stats.station == "SYN"
        assert tr.stats.location == "AA"

    # Test the scale parameter.
    params = copy.deepcopy(basic_parameters)
    params["scale"] = 33.33
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_server = obspy.read(request.buffer)
    for tr in st_server:
        assert tr.stats._format == "MSEED"

    # Parse the finite source.
    fs = _parse_finite_source(usgs_param)
    rec = instaseis.Receiver(latitude=22, longitude=11, network="XX",
                             station="SYN", location="SE")

    st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec)
    # The origin time is the time of the first sample in the route.
    for tr in st_db:
        # Multiply with scale parameter.
        tr.data *= 33.33
        # Cut away the first ten samples as they have been previously added.
        tr.data = tr.data[10:]
        tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1)

    for tr_db, tr_server in zip(st_db, st_server):
        # Sample spacing is very similar but not equal due to floating point
        # accuracy.
        np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta)
        tr_server.stats.delta = tr_db.stats.delta
        del tr_server.stats._format
        del tr_server.stats.mseed

        assert tr_db.stats == tr_server.stats
        np.testing.assert_allclose(tr_db.data, tr_server.data,
                                   atol=1E-6 * tr_db.data.ptp())
dt = 1.0
t_half = 10.0
t = np.arange(0,100,dt)
stf = 2 * np.exp(-(t/t_half)**2) * t / t_half**2
nsample = len(t)

f = open('./stf_pyffproc.dat', 'w')

f.write('%d %f\n'%(nsample, dt))
for y in stf:
    f.write('%f\n'%(y))

f.close()

# Open instaseis db
db = instaseis.open_db("/scratch/auerl/wavefield_pyffproc_10s_647km_prem_ani/bwd/")
#db = instaseis.open_db('../wavefield/bwd')

receiver = instaseis.Receiver(latitude=-51.68, longitude=-58.06, network="AB", station="EFI")
source = instaseis.Source(latitude   = -13.8200,
                          longitude  = -67.2500,
                          depth_in_m = 647100,
                          m_rr = -7.590000e+27 / 1E7,
                          m_tt =  7.750000e+27 / 1E7,
                          m_pp = -1.600000e+26 / 1E7,
                          m_rt = -2.503000e+28 / 1E7,
                          m_rp =  4.200000e+26 / 1E7,
                          m_tp = -2.480000e+27 / 1E7,
                          time_shift=None, 
                          sliprate=stf, 
                          dt=dt)
Ejemplo n.º 41
0
    parser = argparse.ArgumentParser(description=helptext,
                                     formatter_class=formatter_class)

    helptext = "Database 1 directory name. \n"
    parser.add_argument('db_1_name', help=helptext)
    helptext = "Database 2 directory name. \n"
    parser.add_argument('db_2_name', help=helptext)

    return parser


if __name__ == "__main__":
    parser = define_arguments()
    args = parser.parse_args()

    db1 = instaseis.open_db(args.db_1_name)
    db2 = instaseis.open_db(args.db_2_name)

    src = instaseis.Source(latitude=0.0, longitude=0.0, m_rr=1e20)

    rec = instaseis.Receiver(latitude=45.0, longitude=0.0)

    st1 = db1.get_seismograms(src, rec)
    st2 = db2.get_seismograms(src, rec)

    plt.plot(st1[0].times(), st1[0].data, label=args.db_1_name)
    plt.plot(st2[0].times(), st2[0].data, label=args.db_2_name)

    plt.xlabel('time / seconds')
    plt.legend()
Ejemplo n.º 42
0
def calc_static_offset(src_file, db_dir, nx=200, ny=200):
    import obspy
    import instaseis

    db = instaseis.open_db(db_dir)
    print db

    source = instaseis.FiniteSource.from_usgs_param_file(src_file, dt=db.info.dt)
    print source

    pntsrc_lats = np.zeros(source.npointsources)
    pntsrc_lons = np.zeros(source.npointsources)
    pntsrc_depth = np.zeros(source.npointsources)
    
    for i in range(0, source.npointsources):
        pntsrc_depth[i] = source.pointsources[i].depth_in_m
        pntsrc_lats[i] = source.pointsources[i].latitude
        pntsrc_lons[i] = source.pointsources[i].longitude

    x_extent = source.max_longitude - source.min_longitude
    y_extent = source.max_latitude - source.min_latitude
    extent = np.max((x_extent, y_extent))

    latmin = source.hypocenter_latitude - extent * 1.5
    lonmin = source.hypocenter_longitude - extent * 1.5
    latmax = source.hypocenter_latitude + extent * 1.5
    lonmax = source.hypocenter_longitude + extent * 1.5

    print 'Window: ', latmin, latmax, lonmin, lonmax

    lats = []
    lons = []
    offset = []
    lats, lons = np.meshgrid(np.linspace(latmin, latmax, nx), 
                             np.linspace(lonmin, lonmax, ny))
    offset_E = np.zeros_like(lats)
    offset_N = np.zeros_like(lats)
    offset_Z = np.zeros_like(lats)

    # Define progress bar
    widgets = ['Calculating displacement: ', Percentage(), ' ',
               Bar(), ' ', ETA()]
    pbar = ProgressBar(widgets=widgets, maxval=nx).start()

    for ix in range(0, lats.shape[0]):
        for iy in range(0, lats.shape[1]):
            npts = db.info.npts
            rec = instaseis.Receiver(latitude=lats[ix, iy], longitude=lons[ix, iy])
            st = db.get_seismograms_finite_source(sources=source, receiver=rec, components='ENZ')
            offset_E[ix, iy] = np.mean(st.select(channel='LXE')[0].data[npts*0.5:npts])
            offset_N[ix, iy] = np.mean(st.select(channel='LXN')[0].data[npts*0.5:npts])
            offset_Z[ix, iy] = np.mean(st.select(channel='LXZ')[0].data[npts*0.5:npts])        

        # Update Progress Bar
        pbar.update(ix)

        all_data = [lats, lons, offset_E, offset_N, offset_Z]
        fnam = '%s.npy' % (os.path.splitext(src_file)[0])
        np.save(fnam, all_data, allow_pickle=True)

    return all_data
Ejemplo n.º 43
0
def g1g2_kern(wf1str,wf2str,kernel,adjt,
    src,source_conf,insta):
        
    measr_conf = json.load(open(os.path.join(source_conf['source_path'],
        'measr_config.json')))


    bandpass = measr_conf['bandpass']

    if bandpass == None:
        filtcnt = 1
    elif type(bandpass) == list:
        if type(bandpass[0]) != list:
            filtcnt = 1
        else:
            filtcnt = len(bandpass)    
    
    ntime, n, n_corr, Fs = get_ns(wf1str,source_conf,insta)
    # use a one-sided taper: The seismogram probably has a non-zero end, 
    # being cut off whereever the solver stopped running.
    taper = cosine_taper(ntime,p=0.01)
    taper[0:ntime//2] = 1.0

    
########################################################################
# Prepare filenames and adjoint sources
########################################################################   

    filenames = []
    adjt_srcs = []
    adjt_srcs_cnt = 0

    for ix_f in range(filtcnt):
    
        filename = kernel+'.{}.npy'.format(ix_f)
        filenames.append(filename)
        #if os.path.exists(filename):
         #   continue

        f = Stream()
        for a in adjt:
            adjtfile = a + '*.{}.sac'.format(ix_f)
            adjtfile = glob(adjtfile)
            try:    
                f += read(adjtfile[0])[0]
                f[-1].data = my_centered(f[-1].data,n_corr)
                adjt_srcs_cnt += 1
            except IndexError:
                print('No adjoint source found: {}\n'.format(a))
                break

        adjt_srcs.append(f)
        
    

########################################################################
# Compute the kernels
######################################################################## 


    with NoiseSource(src) as nsrc:

        
        ntraces = nsrc.src_loc[0].shape[0]


        if insta:
            # open database
            dbpath = json.load(open(os.path.join(source_conf['project_path'],
                'config.json')))['wavefield_path']
            # open and determine Fs, nt
            db = instaseis.open_db(dbpath)
            # get receiver locations
            lat1 = geograph_to_geocent(float(wf1[2]))
            lon1 = float(wf1[3])
            rec1 = instaseis.Receiver(latitude=lat1,longitude=lon1)
            lat2 = geograph_to_geocent(float(wf2[2]))
            lon2 = float(wf2[3])
            rec2 = instaseis.Receiver(latitude=lat2,longitude=lon2)

        else:
            wf1 = WaveField(wf1str)
            wf2 = WaveField(wf2str)

        kern = np.zeros((filtcnt,ntraces,len(adjt)))

    

        
            
        
        ########################################################################
        # Loop over locations
        ########################################################################            
        for i in range(ntraces):

            # noise source spectrum at this location
            # For the kernel, this contains only the basis functions of the 
            # spectrum without weights; might still be location-dependent, 
            # for example when constraining sensivity to ocean
            S = nsrc.get_spect(i)
            

            if S.sum() == 0.: 
            # The spectrum has 0 phase so only checking absolute value here
                continue

            ####################################################################
            # Get synthetics
            ####################################################################                
            if insta:
            # get source locations
                lat_src = geograph_to_geocent(nsrc.src_loc[1,i])
                lon_src = nsrc.src_loc[0,i]
                fsrc = instaseis.ForceSource(latitude=lat_src,
                    longitude=lon_src,f_r=1.e12)
                
                s1 = np.ascontiguousarray(db.get_seismograms(source=fsrc,
                    receiver=rec1,
                    dt=1./source_conf['sampling_rate'])[0].data*taper)
                s2 = np.ascontiguousarray(db.get_seismograms(source=fsrc,
                    receiver=rec2,
                    dt=1./source_conf['sampling_rate'])[0].data*taper)
                

            else:
                s1 = np.ascontiguousarray(wf1.data[i,:]*taper)
                s2 = np.ascontiguousarray(wf2.data[i,:]*taper)
            
            

            spec1 = np.fft.rfft(s1,n)
            spec2 = np.fft.rfft(s2,n)
            
          
            g1g2_tr = np.multiply(np.conjugate(spec1),spec2)
            c = np.multiply(g1g2_tr,S)

        #######################################################################
        # Get Kernel at that location
        #######################################################################   
            corr_temp = my_centered(np.fft.ifftshift(np.fft.irfft(c,n)),n_corr)
            
        #######################################################################
        # Apply the 'adjoint source'
        #######################################################################
            for ix_f in range(filtcnt):
                f = adjt_srcs[ix_f]

                if f==None:
                    continue
                for j in range(len(f)):
                    delta = f[j].stats.delta
                    
                    kern[ix_f,i,j] = np.dot(corr_temp,f[j].data) * delta
                    

                    #elif measr_conf['mtype'] in ['envelope']:
                    #    if j == 0:
                    #        corr_temp_h = corr_temp
                    #        print(corr_temp_h)
                    #    if j == 1:
                    #        corr_temp_h = hilbert(corr_temp)
                    #        print(corr_temp_h)
                    #    
                    #    kern[ix_f,i,j] = np.dot(corr_temp,f[j].data) * delta
                    
           
            
            if i%50000 == 0:
                print("Finished {} source locations.".format(i))


    if not insta:
        wf1.file.close()
        wf2.file.close()

    for ix_f in range(filtcnt):
        filename = filenames[ix_f]
        if kern[ix_f,:,:].sum() != 0:
            np.save(filename,kern[ix_f,:,:]) 
    return()
Ejemplo n.º 44
0
    "given.",
)
parser.add_argument("--save",
                    action="store_true",
                    help="save output to txt file")
args = parser.parse_args()
path = (os.path.abspath(args.folder)
        if "://" not in args.folder else args.folder)

print(colorama.Fore.GREEN + 79 * "=" + "\nInstaseis Benchmark Suite\n")
print("It enables to gauge the speed of Instaseis for a certain DB.")
print(79 * "=" + colorama.Fore.RESET)
print(colorama.Fore.RED + "\nIt does not deal with OS level caches! So "
      "interpret the results accordingly!\n" + colorama.Fore.RESET)

db = open_db(path, read_on_demand=True, buffer_size_in_mb=0)
if not db.info.is_reciprocal:
    print("Benchmark currently only works with a reciprocal database.")
    sys.exit(1)
print(db)


# Recursively get all subclasses of the benchmark class.
def get_subclasses(cls):
    subclasses = []

    sub = cls.__subclasses__()
    subclasses.extend(sub)

    for subclass in sub:
        subclasses.extend(get_subclasses(subclass))
Ejemplo n.º 45
0
 def setup(self):
     self.db = open_db(self.path,
                       read_on_demand=False,
                       buffer_size_in_mb=250)
def syngine_client():
    return instaseis.open_db("syngine://test")
def test_seismogram_extraction(syngine_client):
    """
    Test the seismogram extraction from local and syngine databases.
    """
    # syngine and local database.
    s_db = syngine_client
    l_db = instaseis.open_db(db_path)

    source = instaseis.Source(
        latitude=4., longitude=3.0, depth_in_m=0, m_rr=4.71e+17, m_tt=3.81e+17,
        m_pp=-4.74e+17, m_rt=3.99e+17, m_rp=-8.05e+17, m_tp=-1.23e+17)

    receiver = instaseis.Receiver(latitude=10., longitude=20., depth_in_m=None)

    kwargs = {"source": source, "receiver": receiver,
              "components": ["Z", "N", "E", "R", "T"]}
    _compare_streams(s_db, l_db, kwargs)

    # Test velocity and acceleration.
    kwargs = {"source": source, "receiver": receiver,
              "components": ["Z", "N", "E", "R", "T"], "kind": "velocity"}
    _compare_streams(s_db, l_db, kwargs)
    kwargs = {"source": source, "receiver": receiver,
              "components": ["Z", "N", "E", "R", "T"], "kind": "acceleration"}
    _compare_streams(s_db, l_db, kwargs)

    # Test remove source shift.
    kwargs = {"source": source, "receiver": receiver,
              "components": ["Z", "N", "E", "R", "T"],
              "remove_source_shift": False}
    _compare_streams(s_db, l_db, kwargs)

    # Test resampling.
    kwargs = {"source": source, "receiver": receiver,
              "components": ["Z", "N", "E", "R", "T"],
              "dt": 1.0, "kernelwidth": 6}
    _compare_streams(s_db, l_db, kwargs)

    # Force sources currently raise an error.
    source = instaseis.ForceSource(
        latitude=89.91, longitude=0.0, depth_in_m=12000,
        f_r=1.23E10,
        f_t=2.55E10,
        f_p=1.73E10)
    kwargs = {"source": source, "receiver": receiver}
    with pytest.raises(ValueError) as e:
        _compare_streams(s_db, l_db, kwargs)

    assert e.value.args[0] == (
        "The Syngine Instaseis client does currently not "
        "support force sources. You can still download "
        "data from the Syngine service for force "
        "sources manually.")

    # Test less components and a latitude of 45 degrees to have the maximal
    # effect of geocentric vs geographic coordinates.
    source = instaseis.Source(
        latitude=45.0, longitude=3.0, depth_in_m=0, m_rr=4.71e+17,
        m_tt=3.81e+17, m_pp=-4.74e+17, m_rt=3.99e+17, m_rp=-8.05e+17,
        m_tp=-1.23e+17)

    receiver = instaseis.Receiver(latitude=-45.0, longitude=20.0,
                                  depth_in_m=None)
    kwargs = {"source": source, "receiver": receiver,
              "components": ["Z", "N", "E", "R", "T"]}
    _compare_streams(s_db, l_db, kwargs)
Ejemplo n.º 48
0
t_half = 20.0
t = np.arange(0, 100, dt)
stf = 2 * np.exp(-(t / t_half)**2) * t / t_half**2
nsample = len(t)

f = open('../stf_test.txt', 'w')

f.write('%d %f\n' % (nsample, dt))
for y in stf:
    f.write('%f\n' % (y))

f.close()

print sum(stf * dt)

db = instaseis.open_db('../wavefield/bwd')
receiver = instaseis.Receiver(latitude=30,
                              longitude=0,
                              network='MC',
                              station='kerner')
source = instaseis.Source(latitude=90.0,
                          longitude=0.0,
                          depth_in_m=0.0,
                          m_rr=1.0e13,
                          m_tt=1.0e13,
                          m_pp=1.0e13,
                          m_rt=0.0,
                          m_rp=0.0,
                          m_tp=0.0,
                          time_shift=None,
                          sliprate=stf,
Ejemplo n.º 49
0
from geopy.distance import VincentyDistance

print(instaseis.__path__)

# input directory with PICKLE data as argument
event = sys.argv[1]
dr = '/raid3/zl382/Data/' + event + '/'
###!!!! All previous synthetics can be removed from the PICKLE by this script
cleansyn = input("Do you want all previous synethetics removed? (y/n) ")
if cleansyn == 'y':
    clean = True
else:
    clean = False

    # Load database with Green Functions
db = instaseis.open_db("syngine://prem_a_2s")

# Directory needs to contain a CMTSOLUTION source text file!!!
cat = obspy.read_events(dir + 'CMTSOLUTION')
# Read in source
source = instaseis.Source(
    latitude=cat[0].origins[0].latitude,
    longitude=cat[0].origins[0].longitude,
    depth_in_m=cat[0].origins[0].depth,
    m_rr=cat[0].focal_mechanisms[0].moment_tensor.tensor.m_rr,
    m_tt=cat[0].focal_mechanisms[0].moment_tensor.tensor.m_tt,
    m_pp=cat[0].focal_mechanisms[0].moment_tensor.tensor.m_pp,
    m_rt=cat[0].focal_mechanisms[0].moment_tensor.tensor.m_rt,
    m_rp=cat[0].focal_mechanisms[0].moment_tensor.tensor.m_rp,
    m_tp=cat[0].focal_mechanisms[0].moment_tensor.tensor.m_tp,
    origin_time=cat[0].origins[0].time)
Ejemplo n.º 50
0
 def setup(self):
     self.db = open_db(self.path, read_on_demand=True,
                       buffer_size_in_mb=0)
     self.max_depth = self.db.info.max_radius - self.db.info.min_radius
def test_more_complex_queries(reciprocal_clients_all_callbacks, usgs_param):
    """
    These are not exhaustive tests but test that the queries do something.
    Elsewhere they are tested in more details.

    Test phase relative offsets.

    + must be encoded with %2B
    - must be encoded with %2D
    """
    client = reciprocal_clients_all_callbacks
    db = instaseis.open_db(client.filepath)

    basic_parameters = {
        "receiverlongitude": 11,
        "receiverlatitude": 22,
        "components": "".join(db.available_components),
        "format": "miniseed"
    }

    with io.open(usgs_param, "rb") as fh:
        body = fh.read()

    # default parameters
    params = copy.deepcopy(basic_parameters)
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st = obspy.read(request.buffer)

    # Now request one starting ten seconds later.
    params = copy.deepcopy(basic_parameters)
    params["starttime"] = 10
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    assert st[0].stats.starttime + 10 == st_2[0].stats.starttime

    # The rest of data should still be identical.
    np.testing.assert_allclose(st.slice(starttime=10)[0].data, st_2[0].data)

    # Try with the endtime.
    params = copy.deepcopy(basic_parameters)
    params["endtime"] = 20
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    assert st_2[0].stats.endtime == st[0].stats.starttime + 20

    # The rest of data should still be identical.
    np.testing.assert_allclose(
        st.slice(endtime=st[0].stats.starttime + 20)[0].data, st_2[0].data)

    # Phase relative start and endtimes.
    params = copy.deepcopy(basic_parameters)
    params["starttime"] = "P%2D5"
    params["endtime"] = "P%2B5"
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    # Just make sure they actually do something.
    assert st_2[0].stats.starttime > st[0].stats.starttime
    assert st_2[0].stats.endtime < st[0].stats.endtime

    # Mixing things
    params = copy.deepcopy(basic_parameters)
    params["starttime"] = "P%2D5"
    params["endtime"] = "50"
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    # Just make sure they actually do something.
    assert st_2[0].stats.starttime > st[0].stats.starttime
    assert st_2[0].stats.endtime < st[0].stats.endtime

    if "Z" not in db.available_components:
        return

    # Network and station searches.
    params = {
        "network": "IU,B*",
        "station": "ANT*,ANM?",
        "components": "Z",
        "format": "miniseed"
    }
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST",
                           body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    assert sorted(tr.id for tr in st_2) == ["IU.ANMO..LXZ", "IU.ANTO..LXZ"]
Ejemplo n.º 52
0
def g1g2_corr(wf1,wf2,corr_file,src,source_conf,insta):
    """
    Compute noise cross-correlations from two .h5 'wavefield' files.
    Noise source distribution and spectrum is given by starting_model.h5
    It is assumed that noise sources are delta-correlated in space.
    """
    
    
    #ToDo: check whether to include autocorrs from user (now hardcoded off)
    #ToDo: Parallel loop(s)
    #ToDo tests
    

    # Metainformation: Include the reference station names for both stations
    # from wavefield files, if possible. Do not include geographic information
    # from .csv file as this might be error-prone. Just add the geographic 
    # info later if needed.

    with NoiseSource(src) as nsrc:

        ntime, n, n_corr, Fs = get_ns(wf1,source_conf,insta)

    # use a one-sided taper: The seismogram probably has a non-zero end, 
    # being cut off whereever the solver stopped running.
        taper = cosine_taper(ntime,p=0.01)
        taper[0:ntime//2] = 1.0
        ntraces = nsrc.src_loc[0].shape[0]
        print(taper.shape)
        correlation = np.zeros(n_corr)

        if insta:
            # open database
            dbpath = json.load(open(os.path.join(source_conf['project_path'],
                'config.json')))['wavefield_path']
            # open and determine Fs, nt
            db = instaseis.open_db(dbpath)
            # get receiver locations
            lat1 = geograph_to_geocent(float(wf1[2]))
            lon1 = float(wf1[3])
            rec1 = instaseis.Receiver(latitude=lat1,longitude=lon1)
            lat2 = geograph_to_geocent(float(wf2[2]))
            lon2 = float(wf2[3])
            rec2 = instaseis.Receiver(latitude=lat2,longitude=lon2)

        else:
            wf1 = WaveField(wf1)
            wf2 = WaveField(wf2)

            
        # Loop over source locations
        for i in range(ntraces):

            # noise source spectrum at this location
            S = nsrc.get_spect(i)
            

            if S.sum() == 0.: 
            #If amplitude is 0, continue. (Spectrum has 0 phase anyway. )
                continue

           
            if insta:
            # get source locations
                lat_src = geograph_to_geocent(nsrc.src_loc[1,i])
                lon_src = nsrc.src_loc[0,i]
                fsrc = instaseis.ForceSource(latitude=lat_src,
                    longitude=lon_src,f_r=1.e12)
                
                s1 = np.ascontiguousarray(db.get_seismograms(source=fsrc,
                    receiver=rec1,
                    dt=1./source_conf['sampling_rate'])[0].data*taper)
                s2 = np.ascontiguousarray(db.get_seismograms(source=fsrc,
                    receiver=rec2,
                    dt=1./source_conf['sampling_rate'])[0].data*taper)
                

            else:
            # read Green's functions
                s1 = np.ascontiguousarray(wf1.data[i,:]*taper)
                s2 = np.ascontiguousarray(wf2.data[i,:]*taper)
            
            
            # Fourier transform for greater ease of convolution
            spec1 = np.fft.rfft(s1,n)
            spec2 = np.fft.rfft(s2,n)
            
            # convolve G1G2
            g1g2_tr = np.multiply(np.conjugate(spec1),spec2)
            
            # convolve noise source
            c = np.multiply(g1g2_tr,S)
            
            # transform back    
            correlation += my_centered(np.fft.ifftshift(np.fft.irfft(c,n)),
                n_corr) * nsrc.surf_area[i]
            
            # occasional info
            if i%50000 == 0:
                print("Finished {} source locations.".format(i))
###################### end of loop over all source locations ###################

        if not insta:
            wf1.file.close()
            wf2.file.close()

        # save output
        trace = Trace()
        trace.stats.sampling_rate = Fs
        trace.data = correlation
# try to add some meta data
        try:
            sta1 = wf1.stats['reference_station']
            sta2 = wf2.stats['reference_station']
            trace.stats.station = sta1.split('.')[1]
            trace.stats.network = sta1.split('.')[0]
            trace.stats.location = sta1.split('.')[2]
            trace.stats.channel = sta1.split('.')[3]
            trace.stats.sac = {}
            trace.stats.sac['kuser0']  =   sta2.split('.')[1]
            trace.stats.sac['kuser1']  =   sta2.split('.')[0]
            trace.stats.sac['kuser2']  =  sta2.split('.')[2]
            trace.stats.sac['kevnm']   =   sta2.split('.')[3]
        except:
            pass

        trace.write(filename=corr_file,format='SAC')
Ejemplo n.º 53
0
def test_more_complex_queries(reciprocal_clients_all_callbacks,
                              usgs_param):
    """
    These are not exhaustive tests but test that the queries do something.
    Elsewhere they are tested in more details.

    Test phase relative offsets.

    + must be encoded with %2B
    - must be encoded with %2D
    """
    client = reciprocal_clients_all_callbacks
    db = instaseis.open_db(client.filepath)

    basic_parameters = {
        "receiverlongitude": 11,
        "receiverlatitude": 22,
        "components": "".join(db.available_components),
        "format": "miniseed"}

    with io.open(usgs_param, "rb") as fh:
        body = fh.read()

    # default parameters
    params = copy.deepcopy(basic_parameters)
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st = obspy.read(request.buffer)

    # Now request one starting ten seconds later.
    params = copy.deepcopy(basic_parameters)
    params["starttime"] = 10
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    assert st[0].stats.starttime + 10 == st_2[0].stats.starttime

    # The rest of data should still be identical.
    np.testing.assert_allclose(st.slice(starttime=10)[0].data, st_2[0].data)

    # Try with the endtime.
    params = copy.deepcopy(basic_parameters)
    params["endtime"] = 20
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    assert st_2[0].stats.endtime == st[0].stats.starttime + 18

    # The rest of data should still be identical.
    np.testing.assert_allclose(
        st.slice(endtime=st[0].stats.starttime + 18)[0].data,
        st_2[0].data)

    # Phase relative start and endtimes.
    params = copy.deepcopy(basic_parameters)
    params["starttime"] = "P%2D5"
    params["endtime"] = "P%2B5"
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    # Just make sure they actually do something.
    assert st_2[0].stats.starttime > st[0].stats.starttime
    assert st_2[0].stats.endtime < st[0].stats.endtime

    # Mixing things
    params = copy.deepcopy(basic_parameters)
    params["starttime"] = "P%2D5"
    params["endtime"] = "50"
    params["dt"] = 2
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    # Just make sure they actually do something.
    assert st_2[0].stats.starttime > st[0].stats.starttime
    assert st_2[0].stats.endtime < st[0].stats.endtime

    if "Z" not in db.available_components:
        return

    # Network and station searches.
    params = {
        "network": "IU,B*",
        "station": "ANT*,ANM?",
        "components": "Z",
        "format": "miniseed"}
    request = client.fetch(_assemble_url('finite_source', **params),
                           method="POST", body=body)
    assert request.code == 200
    st_2 = obspy.read(request.buffer)

    assert sorted(tr.id for tr in st_2) == ["IU.ANMO..LXZ", "IU.ANTO..LXZ"]
Ejemplo n.º 54
0
rank = comm.Get_rank()


# get config
source_config=json.load(open('source_config.json'))
config = json.load(open('../config.json'))
Fs = source_config['sampling_rate']
path_to_db = config['wavefield_path']
channel = source_config['channel']

# read sourcegrid
f_sources = np.load('../sourcegrid.npy')
ntraces = f_sources.shape[-1]

# open the database
db = instaseis.open_db(path_to_db)

# get: synthetics duration and sampling rate in Hz
stest = db.get_seismograms(source=instaseis.ForceSource(latitude=0.0,
            longitude=0.0),receiver=instaseis.Receiver(latitude=10.,
            longitude=0.0),dt=1./source_config['sampling_rate'])[0]
ntimesteps = stest.stats.npts


# read station from file
stationlist = read_csv('../stationlist.csv')
net = stationlist.at[rank,'net']
sta = stationlist.at[rank,'sta']
lat = stationlist.at[rank,'lat']
lon = stationlist.at[rank,'lon']
print(net,sta,lat,lon)
Ejemplo n.º 55
0
def g1g2_kern(wf1str, wf2str, kernel, adjt, src, source_conf, insta):

    measr_conf = json.load(
        open(os.path.join(source_conf['source_path'], 'measr_config.json')))

    bandpass = measr_conf['bandpass']

    if bandpass == None:
        filtcnt = 1
    elif type(bandpass) == list:
        if type(bandpass[0]) != list:
            filtcnt = 1
        else:
            filtcnt = len(bandpass)

    ntime, n, n_corr, Fs = get_ns(wf1str, source_conf, insta)
    # use a one-sided taper: The seismogram probably has a non-zero end,
    # being cut off whereever the solver stopped running.
    taper = cosine_taper(ntime, p=0.01)
    taper[0:ntime // 2] = 1.0

    ########################################################################
    # Prepare filenames and adjoint sources
    ########################################################################

    filenames = []
    adjt_srcs = []
    adjt_srcs_cnt = 0

    for ix_f in range(filtcnt):

        filename = kernel + '.{}.npy'.format(ix_f)
        filenames.append(filename)
        #if os.path.exists(filename):
        #   continue

        f = Stream()
        for a in adjt:
            adjtfile = a + '*.{}.sac'.format(ix_f)
            adjtfile = glob(adjtfile)
            try:
                f += read(adjtfile[0])[0]
                f[-1].data = my_centered(f[-1].data, n_corr)
                adjt_srcs_cnt += 1
            except IndexError:
                print('No adjoint source found: {}\n'.format(a))
                break

        adjt_srcs.append(f)


########################################################################
# Compute the kernels
########################################################################

    with NoiseSource(src) as nsrc:

        ntraces = nsrc.src_loc[0].shape[0]

        if insta:
            # open database
            dbpath = json.load(
                open(os.path.join(source_conf['project_path'],
                                  'config.json')))['wavefield_path']
            # open and determine Fs, nt
            db = instaseis.open_db(dbpath)
            # get receiver locations
            lat1 = geograph_to_geocent(float(wf1[2]))
            lon1 = float(wf1[3])
            rec1 = instaseis.Receiver(latitude=lat1, longitude=lon1)
            lat2 = geograph_to_geocent(float(wf2[2]))
            lon2 = float(wf2[3])
            rec2 = instaseis.Receiver(latitude=lat2, longitude=lon2)

        else:
            wf1 = WaveField(wf1str)
            wf2 = WaveField(wf2str)

        kern = np.zeros((filtcnt, ntraces, len(adjt)))

        ########################################################################
        # Loop over locations
        ########################################################################
        for i in range(ntraces):

            # noise source spectrum at this location
            # For the kernel, this contains only the basis functions of the
            # spectrum without weights; might still be location-dependent,
            # for example when constraining sensivity to ocean
            S = nsrc.get_spect(i)

            if S.sum() == 0.:
                # The spectrum has 0 phase so only checking absolute value here
                continue

            ####################################################################
            # Get synthetics
            ####################################################################
            if insta:
                # get source locations
                lat_src = geograph_to_geocent(nsrc.src_loc[1, i])
                lon_src = nsrc.src_loc[0, i]
                fsrc = instaseis.ForceSource(latitude=lat_src,
                                             longitude=lon_src,
                                             f_r=1.e12)

                s1 = np.ascontiguousarray(
                    db.get_seismograms(
                        source=fsrc,
                        receiver=rec1,
                        dt=1. / source_conf['sampling_rate'])[0].data * taper)
                s2 = np.ascontiguousarray(
                    db.get_seismograms(
                        source=fsrc,
                        receiver=rec2,
                        dt=1. / source_conf['sampling_rate'])[0].data * taper)

            else:
                s1 = np.ascontiguousarray(wf1.data[i, :] * taper)
                s2 = np.ascontiguousarray(wf2.data[i, :] * taper)

            spec1 = np.fft.rfft(s1, n)
            spec2 = np.fft.rfft(s2, n)

            g1g2_tr = np.multiply(np.conjugate(spec1), spec2)
            c = np.multiply(g1g2_tr, S)

            #######################################################################
            # Get Kernel at that location
            #######################################################################
            corr_temp = my_centered(np.fft.ifftshift(np.fft.irfft(c, n)),
                                    n_corr)

            #######################################################################
            # Apply the 'adjoint source'
            #######################################################################
            for ix_f in range(filtcnt):
                f = adjt_srcs[ix_f]

                if f == None:
                    continue
                for j in range(len(f)):
                    delta = f[j].stats.delta

                    kern[ix_f, i, j] = np.dot(corr_temp, f[j].data) * delta

                    #elif measr_conf['mtype'] in ['envelope']:
                    #    if j == 0:
                    #        corr_temp_h = corr_temp
                    #        print(corr_temp_h)
                    #    if j == 1:
                    #        corr_temp_h = hilbert(corr_temp)
                    #        print(corr_temp_h)
                    #
                    #    kern[ix_f,i,j] = np.dot(corr_temp,f[j].data) * delta

            if i % 50000 == 0:
                print("Finished {} source locations.".format(i))

    if not insta:
        wf1.file.close()
        wf2.file.close()

    for ix_f in range(filtcnt):
        filename = filenames[ix_f]
        if kern[ix_f, :, :].sum() != 0:
            np.save(filename, kern[ix_f, :, :])
    return ()
Ejemplo n.º 56
0
def Acces_Normal():
    # Initiate Parameters:

    get_parameters = Get_Paramters()
    PARAMETERS = get_parameters.get_unkown()
    PRIOR = get_parameters.get_prior()
    VALUES = get_parameters.specifications()

    VALUES[
        'directory'] = '/home/nienke/Documents/Applied_geophysics/Thesis/anaconda/Final'

    ## DISCUSS THIS!!!!
    PRIOR['az'] = PARAMETERS['az']
    PRIOR['baz'] = PARAMETERS['baz']

    # Initiate the databases from instaseis:
    db = instaseis.open_db(PRIOR['VELOC'])
    create = Create_observed(PRIOR, db)

    d_obs, tr_obs, source = create.get_seis_automatic(
        parameters=PARAMETERS,
        prior=PRIOR,
        noise_model=VALUES['noise'],
        sdr=VALUES['sdr'])
    time_at_receiver = create.get_receiver_time(PARAMETERS['epi'],
                                                PARAMETERS['depth_s'],
                                                PARAMETERS['origin_time'])
    traces_obs, p_obs, s_obs, start_time_p, start_time_s = create.get_window_obspy(
        tr_obs, PARAMETERS['epi'], PARAMETERS['depth_s'],
        PARAMETERS['origin_time'], VALUES['npts'])

    PRIOR['var_est'] = create.get_var_data(start_time_p, tr_obs)
    # PRIOR['var_est'] =1

    sw = Surface_waves(PRIOR)
    R_env_obs = sw.rayleigh_pick(tr_obs.traces[0],
                                 PARAMETERS['la_s'],
                                 PARAMETERS['lo_s'],
                                 PARAMETERS['depth_s'],
                                 VALUES['directory'],
                                 PARAMETERS['origin_time'],
                                 VALUES['npts'],
                                 plot_modus=False)
    L_env_obs = sw.love_pick(tr_obs.traces[2],
                             PARAMETERS['la_s'],
                             PARAMETERS['lo_s'],
                             PARAMETERS['depth_s'],
                             VALUES['directory'],
                             PARAMETERS['origin_time'],
                             VALUES['npts'],
                             plot_modus=False)
    # tr_obs.plot()
    # ------------------------------------------------------------------

    seis = Seismogram(PRIOR, db)
    window_code = Source_code(PRIOR['VELOC_taup'])
    misfit = Misfit(VALUES['directory'])

    # start_sample_path = '/home/nienke/Documents/Applied_geophysics/Thesis/anaconda/Final/close_sample.txt'
    start_sample_path = None

    m = MCMC_stream(R_env_obs,
                    L_env_obs,
                    traces_obs,
                    p_obs,
                    s_obs,
                    PRIOR,
                    db,
                    VALUES,
                    PARAMETERS['origin_time'],
                    start_time_p,
                    start_time_s,
                    start_sample_path,
                    None,
                    full_obs_trace=tr_obs)
    m.start_MCMC(VALUES['directory'] + '/Exploring.txt')
Ejemplo n.º 57
0
            dirname = dirname.lstrip("'")
            dirname = dirname.rstrip("'")
            print dirname
            f.close()
            dir_not_found_yet = False
    except ValueError:
        str = f.readline()




### Load instaseis database

# In[3]:

db = instaseis.open_db(os.path.join(kerner_dir, dirname))
print db


### Load CMTSOLUTION file

# In[4]:

src = instaseis.Source.parse(os.path.join(kerner_dir, 'CMTSOLUTION'))
print src


### Parse receivers.dat file

# In[ ]:
Ejemplo n.º 58
0
def Normal_check():
    # Initiate Parameters:

    get_parameters = Get_Paramters()
    PARAMETERS = get_parameters.get_unkown()
    PRIOR = get_parameters.get_prior()
    VALUES = get_parameters.specifications()
    VALUES['blind'] = False
    VALUES[
        'directory'] = '/home/nienke/Documents/Applied_geophysics/Thesis/anaconda/Final/check_waveforms'

    ## DISCUSS THIS!!!!
    PRIOR['az'] = PARAMETERS['az']
    PRIOR['baz'] = PARAMETERS['baz']

    # Initiate the databases from instaseis:
    db = instaseis.open_db(PRIOR['VELOC'])
    create = Create_observed(PRIOR, db)

    d_obs, tr_obs, source = create.get_seis_automatic(
        parameters=PARAMETERS,
        prior=PRIOR,
        noise_model=VALUES['noise'],
        sdr=VALUES['sdr'])
    traces_obs, p_obs, s_obs, p_time_obs, s_time_obs = create.get_window_obspy(
        tr_obs, PARAMETERS['epi'], PARAMETERS['depth_s'],
        PARAMETERS['origin_time'], VALUES['npts'])
    time_at_receiver = create.get_receiver_time(PARAMETERS['epi'],
                                                PARAMETERS['depth_s'],
                                                PARAMETERS['origin_time'])

    PRIOR['var_est'] = create.get_var_data(p_time_obs, tr_obs)

    sw = Surface_waves(PRIOR)
    R_env_obs = sw.rayleigh_pick(tr_obs.traces[0],
                                 PARAMETERS['la_s'],
                                 PARAMETERS['lo_s'],
                                 PARAMETERS['depth_s'],
                                 VALUES['directory'],
                                 PARAMETERS['origin_time'],
                                 VALUES['npts'],
                                 plot_modus=False)
    L_env_obs = sw.love_pick(tr_obs.traces[2],
                             PARAMETERS['la_s'],
                             PARAMETERS['lo_s'],
                             PARAMETERS['depth_s'],
                             VALUES['directory'],
                             PARAMETERS['origin_time'],
                             VALUES['npts'],
                             plot_modus=False)
    # tr_obs.plot()
    # ------------------------------------------------------------------

    seis = Seismogram(PRIOR, db)
    window_code = Source_code(PRIOR['VELOC_taup'])
    misfit = Misfit(VALUES['directory'])

    epi = PARAMETERS['epi']
    depth = PARAMETERS['depth_s']
    strike = PARAMETERS['strike']
    dip = PARAMETERS['dip']
    rake = PARAMETERS['rake']
    time = PARAMETERS['origin_time']

    # ---------------------------------------------------------------------------------------------------------------  #
    dict = geo.Geodesic(a=PRIOR['radius'], f=0).ArcDirect(lat1=PRIOR['la_r'],
                                                          lon1=PRIOR['lo_r'],
                                                          azi1=PRIOR['baz'],
                                                          a12=epi,
                                                          outmask=1929)
    d_syn, traces_syn, sources = seis.get_seis_manual(la_s=dict['lat2'],
                                                      lo_s=dict['lon2'],
                                                      depth=depth,
                                                      strike=strike,
                                                      dip=dip,
                                                      rake=rake,
                                                      time=time,
                                                      M0=PRIOR['M0'],
                                                      sdr=VALUES['sdr'])

    R_env_syn = sw.rayleigh_pick(Z_trace=traces_syn.traces[0],
                                 la_s=dict['lat2'],
                                 lo_s=dict['lon2'],
                                 depth=depth,
                                 save_directory=VALUES['directory'],
                                 time_at_rec=time,
                                 npts=VALUES['npts'],
                                 plot_modus=True)
    L_env_syn = sw.love_pick(T_trace=traces_syn.traces[2],
                             la_s=dict['lat2'],
                             lo_s=dict['lon2'],
                             depth=depth,
                             save_directory=VALUES['directory'],
                             time_at_rec=time,
                             npts=VALUES['npts'],
                             plot_modus=False)

    traces_syn.plot(outfile=VALUES['directory'] + '/syntethic')
    total_syn, p_syn, s_syn, p_time_syn, s_time_syn = window_code.get_window_obspy(
        traces_syn, epi, depth, time, VALUES['npts'])

    ax1 = plt.subplot2grid((5, 1), (0, 0))
    ax1.plot(zero_to_nan(p_syn.traces[0].data), c='r', linewidth=0.3)
    ax1.plot(zero_to_nan(p_obs.traces[0].data),
             c='k',
             linestyle=':',
             linewidth=0.3)
    plt.tight_layout()
    ax2 = plt.subplot2grid((5, 1), (1, 0))
    ax2.plot(zero_to_nan(p_syn.traces[1].data), c='r', linewidth=0.3)
    ax2.plot(zero_to_nan(p_obs.traces[1].data),
             c='k',
             linestyle=':',
             linewidth=0.3)
    plt.tight_layout()
    ax3 = plt.subplot2grid((5, 1), (2, 0))
    ax3.plot(zero_to_nan(s_syn.traces[0].data), c='r', linewidth=0.3)
    ax3.plot(zero_to_nan(s_obs.traces[0].data), c='k', linewidth=0.3)
    plt.tight_layout()
    ax4 = plt.subplot2grid((5, 1), (3, 0))
    ax4.plot(zero_to_nan(s_syn.traces[1].data), c='r', linewidth=0.3)
    ax4.plot(zero_to_nan(s_obs.traces[1].data), c='k', linewidth=0.3)
    plt.tight_layout()
    ax5 = plt.subplot2grid((5, 1), (4, 0))
    ax5.plot(zero_to_nan(s_syn.traces[2].data), c='r', linewidth=0.3)
    ax5.plot(zero_to_nan(s_obs.traces[2].data), c='k', linewidth=0.3)
    plt.tight_layout()

    plt.savefig(VALUES['directory'] + '/%.2f_%.2f.pdf' % (epi, depth))
    plt.close()

    ax1 = plt.subplot2grid((3, 1), (0, 0))
    ax1.plot(zero_to_nan(total_syn.traces[0].data), c='r', linewidth=0.5)
    ax1.plot(zero_to_nan(traces_obs.traces[0].data),
             c='k',
             linestyle=':',
             linewidth=0.5)
    ax1.set_title('SYNTHETIC: = epi: %.2f  REAL: epi = %.2f (depth fixed' %
                  (epi, epi))
    plt.tight_layout()
    ax2 = plt.subplot2grid((3, 1), (1, 0))
    ax2.plot(zero_to_nan(total_syn.traces[1].data), c='r', linewidth=0.5)
    ax2.plot(zero_to_nan(traces_obs.traces[1].data),
             c='k',
             linestyle=':',
             linewidth=0.5)
    plt.tight_layout()
    ax3 = plt.subplot2grid((3, 1), (2, 0))
    ax3.plot(zero_to_nan(total_syn.traces[2].data), c='r', linewidth=0.5)
    ax3.plot(zero_to_nan(traces_obs.traces[2].data),
             c='k',
             linestyle=':',
             linewidth=0.5)
    plt.tight_layout()

    plt.savefig(VALUES['directory'] + '/PS_%.2f_%.2f.pdf' % (epi, depth))
    plt.close()

    Xi_bw_new, time_shift_new, amplitude = misfit.CC_stream(
        p_obs, p_syn, s_obs, s_syn, p_time_obs, p_time_syn)
    s_z_new = 0.1 * Xi_bw_new[0]
    s_r_new = 0.1 * Xi_bw_new[1]
    s_t_new = 1 * Xi_bw_new[2]
    p_z_new = 5 * Xi_bw_new[3]
    p_r_new = 5 * Xi_bw_new[4]
    bw_new = s_z_new + s_r_new + s_t_new + p_z_new + p_r_new
    Xi_R_new = misfit.SW_L2(R_env_obs, R_env_syn, PRIOR['var_est'], amplitude)
    Xi_L_new = misfit.SW_L2(L_env_obs, L_env_syn, PRIOR['var_est'], amplitude)

    R_dict_new = {}
    rw_new = 0
    for j, v in enumerate(Xi_R_new):
        R_dict_new.update({'R_%i_new' % j: v})
        rw_new += v

    L_dict_new = {}
    lw_new = 0
    for j, v in enumerate(Xi_L_new):
        L_dict_new.update({'L_%i_new' % j: v})
        lw_new += v
    Xi_new = bw_new + rw_new + lw_new
    a = 1
Ejemplo n.º 59
0
def create_insta_from_invcat(network, event, database):
	import obspy
	from obspy.geodetics.base import gps2dist_azimuth, kilometer2degrees, locations2degrees
	from obspy import read as read_st
	from obspy import read_inventory as read_inv
	from obspy import read_events as read_cat
	from obspy.taup import TauPyModel

	import numpy
	import numpy as np
	import matplotlib.pyplot as plt
	import matplotlib as mpl
	import scipy as sp
	import scipy.signal as signal
	from numpy import genfromtxt

	from sipy.util.array_util import get_coords

	import os
	import datetime

	import sipy.filter.fk as fk
	from sipy.filter.fk import fk_filter
	import sipy.util.fkutil as fku
	import instaseis as ins

	"""
	This function creates synthetic data using the given network and event information, with the database of instaseis

	:param network: Desired Network, for which the data is generated
	:type  network: obspy.core.inventory.Network

	:param event: Event, for wich the data is generated. The event must have stored the moment tensor (e.g. given by glogalcmt.org)
	:type  event: obspy.core.event.Event

	:param database: Link to the database, e.g. the path on your harddrive
	:type  database: str
	"""


	db 		= ins.open_db(database)

	tofe 	= event.origins[0].time
	lat 	= event.origins[0].latitude 
	lon 	= event.origins[0].longitude
	depth 	= event.origins[0].depth

	source = ins.Source(
	latitude=lat, longitude=lon, depth_in_m=depth,
	m_rr = event.MomentTensor.m_rr,
	m_tt = event.MomentTensor.m_tt,
	m_pp = event.MomentTensor.m_pp,
	m_rt = event.MomentTensor.m_rt,
	m_rp = event.MomentTensor.m_rp,
	m_tp = event.MomentTensor.m_tp,
	origin_time=tofe
	)

	stream = Stream()
	tmp = []
	for station in network:
		rec = ins.Receiver(latitude=str(station.latitude), longitude=str(station.longitude), network=str(network.code), station=str(station.code) )
		tmp.append(db.get_seismograms(source=source, receiver=rec))

	for x in tmp:
		stream += x

	return stream
Ejemplo n.º 60
0
 def setup(self):
     self.db = open_db(self.path, read_on_demand=False,
                       buffer_size_in_mb=250)