Ejemplo n.º 1
0
def test_sensitivity_kernel():
    class args(object):
        def __init__(self):
            self.source_model = os.path.join('test', 'testdata_v1',
                                             'testsource_v1')
            self.step = 0
            self.steplengthrun = False,
            self.ignore_network = True

    args = args()
    all_config = config_params(args, comm, size, rank)
    ns = get_ns(all_config)
    p = define_kernel_tasks(all_config, comm, size, rank)
    assert len(p[0]) == 3
    assert p[0][2][1].split()[-1] == 'STA2'

    input_files = input_files_kernel(p[0][1], all_config)

    nsrc = os.path.join('test', 'testdata_v1', 'testsource_v1',
                        'spectral_model.h5')
    output_file = "test"
    # use a one-sided taper: The seismogram probably has a non-zero end,
    # being cut off wherever the solver stopped running.
    taper = cosine_taper(ns[0], p=0.01)
    taper[0:ns[0] // 2] = 1.0
    kernel = compute_kernel(input_files[0], output_file, all_config,
                            NoiseSource(nsrc), ns, taper)
    np.save("newtestkernel.npy", kernel)
    saved_kernel = np.load(
        os.path.join('test', 'testdata_v1', 'testdata',
                     'NET.STA1..MXZ--NET.STA2..MXZ.0.npy'))
    assert np.allclose(kernel / kernel.max(),
                       saved_kernel / saved_kernel.max())
Ejemplo n.º 2
0
def test_forward_model():
    class args(object):
        def __init__(self):
            self.source_model = os.path.join('test', 'testdata_v1',
                                             'testsource_v1')
            self.step = 0
            self.steplengthrun = False,
            self.ignore_network = True

    args = args()
    all_config = config_params(args, comm, size, rank)
    assert all_config.auto_corr

    ns = get_ns(all_config)
    assert (ns[0] == 3600)
    assert (ns[1] == 7200)

    p = define_correlationpairs(all_config.source_config['project_path'],
                                all_config.auto_corr)
    assert len(p) == 3
    assert p[0][0].split()[-1] == 'STA1'

    input_files = add_input_files(p[1], all_config)[0]
    assert os.path.basename(input_files[0]) == 'NET.STA1..MXZ.h5'

    nsrc = os.path.join('test', 'testdata_v1', 'testsource_v1', 'iteration_0',
                        'starting_model.h5')
    # use a one-sided taper: The seismogram probably has a non-zero end,
    # being cut off wherever the solver stopped running.
    taper = cosine_taper(ns[0], p=0.01)
    taper[0:ns[0] // 2] = 1.0
    correlation, sta1, sta2 = compute_correlation(input_files, all_config,
                                                  NoiseSource(nsrc), ns, taper)
    corr_saved = np.load(
        os.path.join('test', 'testdata_v1', 'testdata',
                     'NET.STA1..MXZ--NET.STA2..MXZ.npy'))

    assert np.allclose(correlation, corr_saved)
Ejemplo n.º 3
0

class args(object):
    def __init__(self):
        self.source_model = os.path.join('test', 'testdata_v1',
                                         'testsource_v1')
        self.step = 0
        self.steplengthrun = False,
        self.ignore_network = True


args = args()
all_config = config_params(args, comm, size, rank)
m_a_options = {'g_speed': g_speed, 'window_params': window_params}
m_func = rm.get_measure_func(mtype)
all_ns = get_ns(all_config)
ntime, n, n_corr, Fs = all_ns
taper = cosine_taper(ntime, p=0.01)
taper[0:ntime // 2] = 1.0

with NoiseSource(source_file) as n:
    surf_area = n.surf_area

# sum the dimension that relates to filters
grad = grad.sum(axis=1)

# apply surface elements for integration
for j in range(grad.shape[0]):
    grad[j, :, 0] *= surf_area

# measurement
Ejemplo n.º 4
0
def run_kern(args, comm, size, rank):

    args.steplengthrun = False  # by default
    all_conf = config_params(args, comm, size, rank)

    kernel_tasks, n_p_p, n_p = define_kernel_tasks(all_conf, comm, size, rank)
    if len(kernel_tasks) == 0:
        return ()
    if all_conf.config['verbose']:
        print('Rank number %g' % rank)
        print('working on pair nr. %g to %g of %g.' %
              (rank * n_p_p, rank * n_p_p + n_p_p, n_p))

    # Current model for the noise source
    nsrc = os.path.join(all_conf.source_config['project_path'],
                        all_conf.source_config['source_name'],
                        'spectral_model.h5')

    # Smart numbers
    all_ns = get_ns(all_conf)  # ntime, n, n_corr, Fs

    # use a one-sided taper: The seismogram probably has a non-zero end,
    # being cut off wherever the solver stopped running.
    taper = cosine_taper(all_ns[0], p=0.01)
    taper[0:all_ns[0] // 2] = 1.0

    with NoiseSource(nsrc) as nsrc:
        for kp in kernel_tasks:
            try:
                input_file_list = add_input_files(kp, all_conf)
                output_files = add_output_files(kp, all_conf)
            except (IOError, IndexError):
                if all_conf.config['verbose']:
                    print('Could not find input for: %s\
\nCheck if wavefield .h5 file and base_model file are available.' % kp)
                continue

            for i, input_files in enumerate(input_file_list):
                kern = compute_kernel(input_files, output_files[i], all_conf,
                                      nsrc, all_ns, taper)
                if kern is None:
                    continue

                for ix_f in range(all_conf.filtcnt):
                    if not all_conf.source_config[
                            "rotate_horizontal_components"]:
                        if kern[:, ix_f, :, :].sum() != 0:
                            filename = output_files[i] + '.{}.npy'.format(ix_f)
                            np.save(filename, kern[:, ix_f, :, :])
                        else:
                            continue

            # Rotation
        if all_conf.source_config["rotate_horizontal_components"]:
            for kp in kernel_tasks:
                try:
                    input_file_list = add_input_files(kp, all_conf)
                    output_files = add_output_files(kp, all_conf)

                except (IOError, IndexError):
                    continue

                output_files = [
                    re.sub("MXE", "MXT", of) for of in output_files
                ]
                output_files = [
                    re.sub("MXN", "MXR", of) for of in output_files
                ]
                # - get all the adjoint sources:
                # for all channels, all filter bands, both branches
                adjt_srcs = []
                for infile in input_file_list:
                    adjt_srcs.append(
                        open_adjoint_sources(all_conf, infile[2], all_ns[2]))
                # - open 9 temp files
                if all_conf.source_config["rotate_horizontal_components"]:
                    it_dir = os.path.join(
                        all_conf.source_config['project_path'],
                        all_conf.source_config['source_name'],
                        'iteration_' + str(all_conf.step))
                    tfname = os.path.join(
                        it_dir, "kern",
                        "*{}*{}*_temp".format(kp[0].split()[1].strip(),
                                              kp[1].split()[1].strip()))
                    kern_temp_files = glob(tfname)
                    if kern_temp_files == []:
                        continue

                    kern_temp_files.sort()
                    if len(kern_temp_files) == 9:
                        assemble_rotated_kernel(
                            kern_temp_files, output_files, adjt_srcs,
                            os.path.join(
                                all_conf.source_config["project_path"],
                                "stationlist.csv"))
    return ()