示例#1
0
    def change_band(self):
        """
        Combo to change band.

        Returns
        -------
        None.

        """
        i = self.combobox1.currentText()
        mode = self.combomode.currentText()
        n = self.hs_profnum.value()

        edi_file = self.data[i].fn
        save_path = edi_file[:-4] + '-' + mode

        if not os.path.exists(save_path):
            return
        if os.path.exists(save_path):
            r = glob.glob(save_path + r'\*.resp')
            if len(r) == 0:
                return

        iterfn = os.path.join(save_path, mode + '_' + f'{n:03}' + '.iter')
        respfn = os.path.join(save_path, mode + '_' + f'{n:03}' + '.resp')
        model_fn = os.path.join(save_path, 'Model1D')
        data_fn = os.path.join(save_path, 'Occam1d_DataFile_' + mode + '.dat')

        oc1m = occam1d.Model(model_fn=model_fn)
        oc1m.read_iter_file(iterfn)

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

        rough = float(oc1m.itdict['Roughness Value'])
        rms = float(oc1m.itdict['Misfit Value'])
        rough = f'{rough:.1f}'
        rms = f'{rms:.1f}'

        title = 'RMS: ' + rms + '    Roughness: ' + rough

        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)

        x = 1 / oc1d.freq
        rdata = [oc1d.data['resxy'][0], oc1d.data['resxy'][2]]
        pdata = [oc1d.data['phasexy'][0], oc1d.data['phasexy'][2]]

        self.mmc.update_line(x, pdata, rdata, depths, res, title)
示例#2
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))
示例#3
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))
    def _main_func(self, path2edifile):
        """
        test function should be successful with a default path2edifile
        :return:
        """
        edifile_name = os.path.basename(path2edifile)
        tmpdir = edifile_name[:-4] + "_dir"  # remove the trailing .edi
        tmp_save_path = os.path.join(self._output_dir, tmpdir)
        ufun.clean_recreate(tmp_save_path)

        # create data file
        ocd = mtoc1d.Data()  # create an object and assign values to arguments

        ocd.write_data_file(
            edi_file=path2edifile,
            mode='det',
            # mode, can be te, tm, det (for res/phase) or tez, tmz, zdet for real/imag impedance tensor values
            save_path=tmp_save_path,
            res_errorfloor=5,  # percent error floor
            phase_errorfloor=1,  # error floor in degrees
            z_errorfloor=2.5,
            remove_outofquadrant=True)

        # create model file
        ocm = mtoc1d.Model(
            n_layers=100,  # number of layers
            target_depth=10000,  # target depth in metres, before padding
            z1_layer=10  # first layer thickness in metres
        )
        ocm.write_model_file(save_path=tmp_save_path)

        # create startup file
        ocs = mtoc1d.Startup(
            data_fn=ocd.
            data_fn,  # basename of data file *default* is Occam1DDataFile
            model_fn=ocm.
            model_fn,  # basename for model file *default* is Model1D
            max_iter=200,  # maximum number of iterations to run
            target_rms=0.0)

        ocs.write_startup_file()

        return tmp_save_path
示例#5
0
import os.path as op
import os
import mtpy.modeling.occam1d as mtoc1d  # Wrapper class to interact with Occam1D

# directory to save created input files
savepath = r'/home/workshop/MT/Occam1d/07E1_input_output'
if not op.exists(savepath):
    os.mkdir(savepath)

# edi path and file name
edipath = r'/home/workshop/MT/Occam1d/edi'
edifilename = '07E1_AMT.edi'

# create data file
ocd = mtoc1d.Data()  #create an object and assign values to arguments
ocd.write_data_file(
    edi_file=op.join(edipath, edifilename),
    mode='det',  # det mode
    save_path=savepath,
    res_errorfloor=5,  # error floor in percentage
    phase_errorfloor=1,  # error floor in degrees
    remove_outofquadrant=True)

# create model file
ocm = mtoc1d.Model(
    n_layers=100,  # number of layers
    target_depth=4000,  # target depth in metres, before padding
    bottom_layer=10000,
    z1_layer=10  # first layer thickness in metres
)
                )
                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)

            # --> write occam1d data file
            ocd = occam1d.Data()
            ocd.save_path = os.path.join(save_dir, mt_obj.station)
            ocd.write_data_file(rp_tuple=rp_tup,
                                mode="det",
                                res_err=10,
                                phase_err=2.5)
            data_tm_fn = ocd.data_fn

            # --> write occam1d model file
            ocm = occam1d.Model()
            ocm.save_path = ocd.save_path
            ocm.write_model_file(model_depth=mdm.grid_z[1:])

            # --> write occam1d startup file
            ocs = occam1d.Startup()
            ocs.data_fn = data_tm_fn
示例#7
0
        iterNo = int(iterf.split('_')[-1].split('.')[0])
        iterDict[iterNo] = iterf

    iterfile = iterDict[np.amax(iterDict.keys())]
    respfile = respDict[np.amax(respDict.keys())]

# 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',
示例#8
0
    def apply(self):
        """
        Apply.

        Returns
        -------
        None.

        """
        parm = {}

        parm['tdepth'] = tonumber(self.targetdepth.text())
        parm['nlayers'] = tonumber(self.nlayers.text())
        parm['blayer'] = tonumber(self.bottomlayer.text())
        parm['alayer'] = tonumber(self.airlayer.text())
        parm['z1layer'] = tonumber(self.z1layer.text())
        parm['miter'] = tonumber(self.maxiter.text())
        parm['trms'] = tonumber(self.targetrms.text())
        parm['rerr'] = tonumber(self.errres.text(), 'data')
        parm['perr'] = tonumber(self.errphase.text(), 'data')
        parm['perrflr'] = tonumber(self.errfloorphase.text())
        parm['rerrflr'] = tonumber(self.errfloorres.text())

        if -999 in parm.values():
            return

        mode = self.combomode.currentText()
        i = self.combobox1.currentText()
        edi_file = self.data[i].fn

        save_path = edi_file[:-4] + '-' + mode

        if os.path.exists(save_path):
            r = glob.glob(save_path + r'\*')
            for i in r:
                os.remove(i)
        else:
            os.makedirs(save_path)

        d1 = occam1d.Data()
        d1.write_data_file(edi_file=edi_file,
                           mode=mode,
                           save_path=save_path,
                           res_err=parm['rerr'],
                           phase_err=parm['perr'],
                           res_errorfloor=parm['rerrflr'],
                           phase_errorfloor=parm['perrflr'],
                           remove_outofquadrant=True)

        m1 = occam1d.Model(target_depth=parm['tdepth'],
                           n_layers=parm['nlayers'],
                           bottom_layer=parm['blayer'],
                           z1_layer=parm['z1layer'],
                           air_layer_height=parm['alayer'])
        m1.write_model_file(save_path=d1.save_path)

        s1 = occam1d.Startup(data_fn=d1.data_fn,
                             model_fn=m1.model_fn,
                             max_iter=parm['miter'],
                             target_rms=parm['trms'])

        s1.write_startup_file()

        self.mmc.figure.clear()
        self.mmc.figure.set_facecolor('r')
        self.mmc.figure.suptitle('Busy, please wait...', fontsize=14, y=0.5)
        #        ax = self.mmc.figure.gca()
        #        ax.text(0.5, 0.5, 'Busy, please wait...')
        self.mmc.figure.canvas.draw()
        QtWidgets.QApplication.processEvents()

        occam_path = os.path.dirname(__file__)[:-2] + r'\bin\occam1d.exe'

        occam1d.Run(s1.startup_fn, occam_path, mode='TE')

        self.mmc.figure.set_facecolor('w')

        allfiles = glob.glob(save_path + r'\*.resp')
        self.hs_profnum.setMaximum(len(allfiles))
        self.hs_profnum.setMinimum(1)

        self.change_band()
示例#9
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()