Example #1
0
def test_fun():
    """
    test function
    :return: T/F
    """
    # AK's working directory
    # idir = r'C:\Git\mtpy\examples\model_files\Occam1d'
    # savepath = r'C:\Git\mtpy\examples\plots\Occam1d'

    #FZ's workdir
    idir = os.path.join(SAMPLE_DIR, 'Occam1d')
    savepath = TEMP_OUT_DIR

    # model and data file names
    modelfn = op.join(idir, 'Model1D')
    datafn = op.join(idir, 'Occam1d_DataFile_DET.dat')

    # list of all the 'smooth' files, exclude the log file
    fn_list = np.array([ff for ff in os.listdir(idir)])

    #go to model results directory and find the latest iteration file
    #iterfile = 'ITER_135.iter'
    #respfile = 'ITER_135.resp'
    iterfile = 'ITER_97.iter'
    respfile = 'ITER_97.resp'

    # get maximum iteration
    iterfn = op.join(idir, iterfile)
    respfn = op.join(idir, respfile)

    # read in the model, don't need these lines to view the output but useful if you want to analyse the data
    oc1m = mtoc1d.Model(model_fn=modelfn)
    oc1m.read_iter_file(iterfn)

    # read in the data file
    oc1d = mtoc1d.Data(data_fn=datafn)
    oc1d.read_data_file(data_fn=datafn)
    oc1d.read_resp_file(resp_fn=respfn, data_fn=datafn)

    # plot the model output
    pr = mtoc1d.Plot1DResponse(
        data_te_fn=datafn,
        data_tm_fn=datafn,
        model_fn=modelfn,
        resp_te_fn=respfn,
        iter_te_fn=iterfn,
        resp_tm_fn=respfn,
        iter_tm_fn=iterfn,
        depth_limits=(0, 1),
    )
    pr.axm.set_xlim(1e-1, 1e3)
    pr.axr.set_ylim(1, 100)

    p2file = op.join(savepath, 'occam1dplot.png')
    pr.save_figure(p2file)

    assert (os.path.exists(p2file))
Example #2
0
    def test_view_outputs(self):
        # FZ's workdir
        savepath = self._temp_dir

        # model and data file names
        modelfn = os.path.join(self._expected_output_dir, 'Model1D')
        datafn = os.path.join(self._expected_output_dir,
                              'Occam1d_DataFile_DET.dat')

        # list of all the 'smooth' files, exclude the log file
        fn_list = np.array(
            [ff for ff in os.listdir(self._expected_output_dir)])

        # go to model results directory and find the latest iteration file
        # iterfile = 'ITER_135.iter'
        # respfile = 'ITER_135.resp'
        iterfile = 'ITER_97.iter'
        respfile = 'ITER_97.resp'

        # get maximum iteration
        iterfn = os.path.join(self._expected_output_dir, iterfile)
        respfn = os.path.join(self._expected_output_dir, respfile)

        # read in the model, don't need these lines to view the output but useful if you want to analyse the data
        oc1m = mtoc1d.Model(model_fn=modelfn)
        oc1m.read_iter_file(iterfn)

        # read in the data file
        oc1d = mtoc1d.Data(data_fn=datafn)
        oc1d.read_data_file(data_fn=datafn)
        oc1d.read_resp_file(resp_fn=respfn, data_fn=datafn)

        # plot the model output
        pr = mtoc1d.Plot1DResponse(data_te_fn=datafn,
                                   data_tm_fn=datafn,
                                   model_fn=modelfn,
                                   resp_te_fn=respfn,
                                   iter_te_fn=iterfn,
                                   depth_limits=(0, 1))
        pr.axm.set_xlim(1e-1, 1e3)
        pr.axr.set_ylim(1, 100)

        p2file = os.path.join(savepath, 'occam1dplot.png')
        pr.save_figure(p2file, close_plot='n')

        tests.imaging.plt_wait(1)
        tests.imaging.plt_close()

        assert (os.path.exists(p2file))
            oned_res_arr[dd]["station"] = mt_obj.station
            oned_res_arr[dd]["grid_east"] = mt_obj.grid_east
            oned_res_arr[dd]["grid_north"] = mt_obj.grid_north
            oned_res_arr[dd]["grid_elev"] = mt_obj.grid_elev

            # need to find elevation
            depth_index = np.where(mdm.grid_z == mt_obj.grid_elev)[0][0]
            depth_res = np.repeat(fill_res, depth_index)
            depth_res = np.append(depth_res, ocm.model_res[2:, 1])
            oned_res_arr[dd]["res"] = depth_res[0:mdm.res_model.shape[2] + 1]

            if plot:
                pr = occam1d.Plot1DResponse(
                    data_te_fn=os.path.join(occam_sv_path,
                                            "Occam1d_DataFile_DET.dat"),
                    model_fn=os.path.join(occam_sv_path, "Model1D"),
                    resp_te_fn=resp_fn,
                    iter_te_fn=iter_fn,
                    depth_limits=(0, 20),
                )
                pr.save_figure(
                    os.path.join(save_dir,
                                 "{0}_1d_resp.png".format(mt_obj.station)),
                    fig_dpi=900,
                )
        else:
            mt_obj.Z.compute_resistivity_phase()
            rho = mt_obj.Z.resistivity
            rho_err = mt_obj.Z.resistivity_err
            phi = mt_obj.Z.phase
            phi_err = mt_obj.Z.phase_err
            rp_tup = (mt_obj.Z.freq, rho, rho_err, phi, phi_err)
    ocs.write_startup_file()
    occam1d.Run(ocs.startup_fn, occam_path=opath, mode='TE')

if plot == 'y':
    try:
        resp_tm_list = [os.path.join(save_dir, station, 'TM_{0}.resp'.format(rr))
                     for rr in range(1, iter_num+1)]
        iter_tm_list = [os.path.join(save_dir, station, 'TM_{0}.iter'.format(rr))
                     for rr in range(1, iter_num+1)]
        resp_te_list = [os.path.join(save_dir, station, 'TE_{0}.resp'.format(rr))
                     for rr in range(1, iter_num+1)]
        iter_te_list = [os.path.join(save_dir, station, 'TE_{0}.iter'.format(rr))
                     for rr in range(1, iter_num+1)]
        model_fn = os.path.join(ocd.save_path, 'Model1D')
        
        p1 = occam1d.Plot1DResponse(data_tm_fn=data_tm_fn,
                                    data_te_fn=data_te_fn,
                                    model_fn=model_fn,
                                    resp_tm_fn=resp_tm_list[-1],
                                    iter_tm_fn=iter_tm_list[-1],
                                    resp_te_fn=resp_te_list[-1],
                                    iter_te_fn=iter_te_list[-1],
                                    depth_limits=(0, 50),
                                    title_str=station)
        if plot_save == 'y':                            
            sv_path = os.path.join(save_dir, 'Plots', station+'.png')
            p1.save_figure(sv_path, fig_dpi=600, close_plot='n')
    except IOError:
        print '{0} did not run properly, check occam1d files'.format(station)
    
Example #5
0
# get iteration file to plot
iterfn = op.join(idir, iterfile)
respfn = op.join(idir, respfile)

# read in the model, don't need these lines to view the output but useful if you want to analyse the data
oc1m = mtoc1d.Model(model_fn=modelfn)
oc1m.read_iter_file(iterfn)

# read in the data file
oc1d = mtoc1d.Data(data_fn=datafn)
oc1d.read_data_file(data_fn=datafn)
oc1d.read_resp_file(resp_fn=respfn, data_fn=datafn)

# plot the model output
pr = mtoc1d.Plot1DResponse(
    data_te_fn=datafn,
    data_tm_fn=datafn,
    model_fn=modelfn,
    resp_te_fn=respfn,
    iter_te_fn=iterfn,
    resp_tm_fn=respfn,
    iter_tm_fn=iterfn,
    fig_size=(10, 5),
    depth_units='m',
    override_legend_subscript='DET',
    depth_limits=(0, 500),
)
pr.axm.set_xlim(1e-1, 1e5)
pr.save_figure(op.join(idir, plot_file_name))
Example #6
0
import mtpy.modeling.occam1d as mtoc1d
import os.path as op

# working directory
wd = r'C:\mtpywin\mtpy\examples\model_files\Occam1d'

# model and data file names
modelfn = op.join(wd, 'Model1D')
datafn = op.join(wd, 'Occam1d_DataFile_DET.dat')

iterfn = op.join(wd, 'ITER_97.iter')
respfn = op.join(wd, 'ITER_97.resp')

# read in the model, don't need these lines to view the model but useful if you want to analyse the data
oc1m = mtoc1d.Model(model_fn=modelfn)
oc1m.read_iter_file(iterfn)

# read in the data file
oc1d = mtoc1d.Data(data_fn=datafn)
oc1d.read_data_file(data_fn=datafn)
oc1d.read_resp_file(resp_fn=respfn, data_fn=datafn)

# plot the model and response
pr = mtoc1d.Plot1DResponse(data_te_fn=datafn,
                           data_tm_fn=datafn,
                           model_fn=modelfn,
                           resp_te_fn=respfn,
                           iter_te_fn=iterfn,
                           resp_tm_fn=respfn,
                           iter_tm_fn=iterfn,
                           depth_limits=(0, 10))
Example #7
0
        ocs.save_path = s_dir
        ocs.max_iter = 10
        ocs.write_startup_file()

        occam1d.Run(ocs.startup_fn, occam_path=occam1d_path, mode='Det')
        try:
            m_ocm = occam1d.Model()
            m_ocm.read_iter_file(os.path.join(s_dir, 'Det_5.iter'),
                                 ocm.model_fn)
            res_arr[ii, d_index:] = m_ocm.model_res[1:-(d_index + 2), 1]
            if os.path.join(sv_dir, '{0}.png'.format(
                    d_arr['station'])) is False:
                pr = occam1d.Plot1DResponse(
                    data_te_fn=os.path.join(s_dir,
                                            r"Occam1d_DataFile_DET.dat"),
                    resp_te_fn=os.path.join(s_dir, r"Det_5.resp"),
                    iter_te_fn=os.path.join(s_dir, r"Det_5.iter"),
                    model_fn=os.path.join(s_dir, r"Model1D"),
                    depth_limits=(-1, 30))
                pr.save_figure(os.path.join(sv_dir, '{0}.png'.format(
                    d_arr['station'])),
                               fig_dpi=600)
        except:
            print('Occam did not run for {0}'.format(d_arr['station']))

    with open(pickle_fn, 'wb') as fid:
        pickle.dump(x, fid)
        pickle.dump(y, fid)
        pickle.dump(res_arr, fid)

# =============================================================================
Example #8
0
def modelling(datadir):
    """ modelling """
    edi_file = os.path.join(datadir, r"synth02.edi")
    save_path = os.path.join(datadir, 'TM')

    allfiles = glob.glob(save_path + '\\*.*')
    for i in allfiles:
        os.remove(i)
    try:
        os.remove(save_path + '\\Model1D')
        os.remove(save_path + '\\OccamStartup1D')
    except:
        pass

    d1 = occam1d.Data()
    d1.write_data_file(edi_file=edi_file,
                       mode='TE',
                       save_path=save_path,
                       res_err='data',
                       phase_err='data',
                       res_errorfloor=4.,
                       phase_errorfloor=2.,
                       remove_outofquadrant=True)

    m1 = occam1d.Model(target_depth=40000,
                       n_layers=100,
                       bottom_layer=100000,
                       z1_layer=10)
    m1.write_model_file(save_path=d1.save_path)

    #--> make a startup file
    s1 = occam1d.Startup(data_fn=d1.data_fn,
                         model_fn=m1.model_fn,
                         max_iter=200,
                         target_rms=1.0)

    s1.write_startup_file()

    #--> run occam1d from python
    occam_path = r"C:\Work\Programming\pygmi\pygmi\bin\occam1d.exe"
    occam1d.Run(s1.startup_fn, occam_path, mode='TM')

    #--plot the L2 curve
    #    l2 = occam1d.PlotL2(d1.save_path, m1.model_fn,
    #                        fig_dpi=100,
    #                        fig_size=(2,2)
    #                        )

    #--> see that iteration 7 is the optimum model to plot

    iterfn = os.path.join(save_path, 'TM_005.iter')
    respfn = os.path.join(save_path, 'TM_005.resp')

    p1 = occam1d.Plot1DResponse(
        data_te_fn=d1.data_fn,
        model_fn=m1.model_fn,
        iter_te_fn=iterfn,
        resp_te_fn=respfn,
        #                                depth_limits=(0,20),
        fig_dpi=100,
        fig_size=(2, 2))

    oc1m = occam1d.Model(model_fn=m1.model_fn)

    allfiles = glob.glob(save_path + '\*.iter')

    roughness = []
    rms = []
    for iterfn in allfiles:
        oc1m.read_iter_file(iterfn)
        roughness.append(float(oc1m.itdict['Roughness Value']))
        rms.append(float(oc1m.itdict['Misfit Value']))

    roughness.pop(0)
    rms.pop(0)

    plt.plot(roughness, rms)
    plt.xlabel('Roughness')
    plt.ylabel('RMS')
    plt.show()
    plt.plot(rms)
    plt.xlabel('Iteration')
    plt.ylabel('RMS')
    plt.show()

    iterfn = os.path.join(save_path, 'TM_005.iter')
    respfn = os.path.join(save_path, 'TM_005.resp')

    oc1m.read_iter_file(iterfn)

    oc1d = occam1d.Data(data_fn=d1.data_fn)
    oc1d.read_resp_file(respfn)

    depths = []
    res = []

    for i, val in enumerate(oc1m.model_res[:, 1]):
        if i == 0:
            continue
        if i > 1:
            depths.append(-oc1m.model_depth[i - 1])
            res.append(val)

        depths.append(-oc1m.model_depth[i])
        res.append(val)

    plt.plot(res, depths)

    plt.xlabel('Res')
    plt.ylabel('Depth')

    plt.show()

    plt.plot(1 / oc1d.freq, oc1d.data['resxy'][0], 'bs')
    plt.plot(1 / oc1d.freq, oc1d.data['resxy'][2], 'r')
    plt.xscale('log')
    plt.yscale('log')
    plt.grid(True)
    plt.show()

    plt.plot(1 / oc1d.freq, oc1d.data['phasexy'][0], 'bs')
    plt.plot(1 / oc1d.freq, oc1d.data['phasexy'][2], 'r')
    plt.xscale('log')
    plt.yscale('log')
    plt.grid(True)
    plt.show()

    breakpoint()