Exemple #1
0
    def build_mesh(self):
        """
        create a mesh using occam2d
        """

        # create an occam2d setup object
        so = o2d.Setup(wd=self.working_directory,
                       edi_directory=self.edi_directory,
                       edifiles=self.edifiles,
                       configfile=self.occam_configfile,
                       strike=self.parameters_data['strike'],
                       **self.parameters_model)

        so.read_edifiles(edi_dir=self.edi_directory)
        # create an occam2d data object
        so.Data = o2d.Data(edilist=self.edifiles,
                           wd=so.wd, **so.parameters_data)
        # set up meshlocations
        so.setup_mesh_and_model()
        self.stationlocations = np.array(so.Data.stationlocations) / 1000.

        # set occam mesh attributes to pek2d object
        for attribute in ['meshlocations_x', 'meshlocations_z',
                          'meshblockwidths_x', 'meshblockthicknesses_z',
                          'profile_easts', 'profile_norths', 'Data']:
            if 'mesh' in attribute:
                attvalue = np.array(getattr(so, attribute)) / 1000.
            else:
                attvalue = getattr(so, attribute)
            setattr(self, attribute, attvalue)

        for attribute in ['firstlayer_thickness', 'model_depth',
                          'no_sideblockelements', 'no_bottomlayerelements',
                          'no_layers', 'max_blockwidth']:
            self.parameters_model[attribute] = so.parameters_inmodel[attribute]

        self.meshlocations_z = np.array([0.] + list(self.meshlocations_z))

        # get block centres
        self.blockcentres_x = [np.mean(self.meshlocations_x[i:i + 2]) for i in
                               range(len(self.meshlocations_x) - 1)]
        self.blockcentres_z = [np.mean(self.meshlocations_z[k:k + 2]) for k in
                               range(len(self.meshlocations_z) - 1)]
Exemple #2
0
    def build_inputfiles(self):
        """
        Set up collection of required input files files for OCCAM.
        
        - data
        - startup
        - model
        - mesh
        """
        returnvalue = 0

        #1. Build OCCAM data file, or use existing one (if field is checked in GUI)

        D = self.parameters

        #print D['check_usedatafile']
        #print D['olddatafile']

        olddatafile = D['olddatafile']

        if olddatafile is not None:
            datafile = op.abspath(op.join(D['wd'], olddatafile))
            messagetext = ''

            returnvalue = 0
            try:
                data_object = MTo2.Data()
                data_object.readfile(datafile)

                D['strike'] = data_object.strike
                D['azimuth'] = data_object.azimuth
                D['stationlocations'] = data_object.stationlocations

                messagetext += "<P><FONT COLOR='#000000'>Working directory: "\
                    "{0}  </FONT></P> \n".format(data_object.wd)

                messagetext += "<P><b><FONT COLOR='#008080'>Read old data file:</FONT></b></P><br>{0}".format(
                    datafile)
            except:
                messagetext += "<P><b><FONT COLOR='#800000'>Error: Cannot read old data file: {0}  </FONT></b></P> ".format(
                    datafile)
                returnvalue = 1

            QtGui.QMessageBox.about(self, "Data file generation", messagetext)
            if returnvalue == 1:
                return

        else:

            outfilename = D['datafile']
            edidirectory = D['edi_dir']

            def make_stationlist(listfilename):
                """
                Read in stations from file.

                """
                FH = file(listfilename, 'r')
                raw_string = FH.read()
                FH.close()
                raw_list1 = raw_string.strip().split()
                raw_list2 = []
                for i in raw_list1:
                    if len(i.split(',')) == 1:
                        raw_list2.append(i)
                    else:
                        for j in i.split(','):
                            raw_list2.append(j)
                return raw_list2

            #define internal station list
            stationlist = None
            if D['use_stationfile']:
                stationlist = make_stationlist(D['stationlistfile'])

            D['stationlist'] = stationlist

            #make data file  -------------------------------------------
            returnvalue = 0
            messagetext = ''
            try:
                setup_object = MTo2.Setup(**D)
            except:
                messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
                "generate setup object - check input parameters!  </FONT></b></P> \n"
                QtGui.QMessageBox.about(self, "Input files generation",
                                        messagetext)
                return 1

            try:
                edi_dir = D['edi_dir']
                setup_object.read_edifiles(edi_dir)
                datafile = D['datafile']
                setup_object.datafile = datafile
                try:
                    setup_object.write_datafile()
                except:
                    raise
                datafilename = setup_object.datafile
                self.parameters[
                    'stationlocations'] = setup_object.Data.stationlocations
                messagetext += "<P><FONT COLOR='#000000'>Working directory: "\
                        "{0}  </FONT></P> \n".format(setup_object.wd)

                messagetext += "<P><b><FONT COLOR='#008080'>"\
                "Data file: {0}  </FONT></b></P> \n".format(op.split(setup_object.datafile)[1])
            except:
                messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
                "write data file: {0}  </FONT></b></P> \n".format(setup_object.datafile)
                returnvalue = 1

            QtGui.QMessageBox.about(self, "Data file generation", messagetext)

        #---------------
        #2. other input files:

        D = self.parameters

        #datafile = D['datafile']
        messagetext = ''
        # try:
        #     #1. make startup file
        #     self._setup_startupfile()
        # except:
        #     messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not generate startup file!  </FONT></b></P> \n"

        if olddatafile is not None:
            try:
                setup_object = MTo2.Setup(**D)
            except:
                messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
                "generate setup object - check input parameters!  </FONT></b></P> \n"
                QtGui.QMessageBox.about(self, "Input files generation",
                                        messagetext)

                return 1

        # edi_dir = D['edi_dir']
        # if not D['check_usedatafile']:
        #     try:
        #         setup_object.read_edifiles(edi_dir)
        #         setup_object.datafile = D['datafilename']
        #         setup_object.write_datafile()
        #         messagetext += "<P><b><FONT COLOR='#008080'>Wrote "\
        #         "data file: {0}  </FONT></b></P> \n".format(setup_object.datafile)
        #     except:
        #         messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
        #         "write data file: {0}  </FONT></b></P> \n".format(setup_object.datafile)

        #     QtGui.QMessageBox.about(self, "Data file generation", messagetext )

        try:
            setup_object.setup_mesh_and_model()
        except:
            messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
            "set up mesh and model!  </FONT></b></P> \n"
            returnvalue = 1
        messagetext += "<P><FONT COLOR='#000000'>Working directory: "\
            "{0}  </FONT></P> \n".format(setup_object.wd)
        try:
            setup_object.write_meshfile()
            messagetext += "<P><b><FONT COLOR='#008080'>"\
            "Mesh file: {0}  </FONT></b></P> \n".format(setup_object.meshfile)
        except:
            messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
            "write mesh file: {0}  </FONT></b></P> \n".format(setup_object.meshfile)
            returnvalue = 1

        try:
            setup_object.write_inmodelfile()
            messagetext += "<P><b><FONT COLOR='#008080'>"\
            "Inmodel file: {0}  </FONT></b></P> \n".format(setup_object.inmodelfile)
        except:
            messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
            "write inmodel file: {0}  </FONT></b></P> \n".format(setup_object.inmodelfile)
            returnvalue = 1

        if D['check_useiterationfile']:
            try:
                setup_object.startupfile = D['iterationfile']
                base, short_fn = op.split(setup_object.startupfile)
                if base != setup_object.wd:
                    new_startupfile = op.abspath(
                        op.join(setup_object.wd, short_fn))
                    shutil.copy(setup_object.startupfile, new_startupfile)
                setup_object.startupfile = short_fn
                messagetext += "<P><b><FONT COLOR='#008080'>Using old "\
                "iteration file for startup: {0}  </FONT></b></P> \n".format(setup_object.startupfile)
                D['startupfile'] = D['iterationfile']
            except:
                messagetext += "<P><b><FONT COLOR='#800000'>Error: Could not "\
                "find old iteration file: {0}  </FONT></b></P> \nUsing default 'startup' instead ".format(D['iterationfile'])
                D['startupfile'] = 'startup'
                returnvalue = 1

        else:
            try:
                setup_object.write_startupfile()
                messagetext += "<P><b><FONT COLOR='#008080'>"\
                "Startup file: {0}  </FONT></b></P> \n".format(setup_object.startupfile)
                D['startupfile'] = setup_object.startupfile

            except:
                messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
                "write startup file: {0}  </FONT></b></P> \n".format(setup_object.startupfile)
                D['startupfile'] = 'startup'
                returnvalue = 1

        try:
            setup_object.write_configfile()
            messagetext += "<P><b><FONT COLOR='#008080'>"\
            "Configuration file: {0}  </FONT></b></P> \n".format(op.split(setup_object.configfile)[1])
        except:
            messagetext += "<P><b><FONT COLOR='#800000'>Error:  Could not "\
            "write configuration file: {0}  </FONT></b></P> \n".format(op.split(setup_object.configfile)[1])
            returnvalue = 1

        QtGui.QMessageBox.about(self, "Input files generation", messagetext)

        return returnvalue