Exemplo n.º 1
0
def walkfiles(existdir, mindepth=1, maxdepth=5, matchme=""):
    """Walk through directory and subdirectories and return list of files.
        Args:
            existdir <str>: directory under which to search.
            mindepth <int>: minimum depth of folders to search;
                            default 1 = within that directory
            maxdepth <int>: maximum depth of folders to search; default 5
            matchme <str>: string to match; every file ending in matchme
                            will be found, since a * is required at the front
                            to match the full paths.
        Returns:
            <list of str>: list of full file paths
    """
    if not (os.path.exists(existdir)):
        raise MASTError("utility", "No directory at " + existdir)
#   walk and make main list
    walkme = ""
    walkme = os.walk(existdir)
    if walkme == ():
        raise MASTError("utility walkfiles", "No folders found in " + existdir)
    filetree = ""
    diritem = ""
    fileitem = ""
    fullfile = ""
    filelist = []
    filetree = os.walk(existdir)
    for diritem in filetree:
        for fileitem in diritem[2]:
            fullfile = diritem[0] + '/' + fileitem
            filelist.append(fullfile)
    #for fullfile in filelist:
    #    print fullfile


#   #   sort and pare file list
    paredfilelist = []
    numsep = 0
    minnumsep = 0
    maxnumsep = 0
    numsep = existdir.count('/')  # find base number of forward slashes
    minnumsep = numsep + mindepth
    maxnumsep = numsep + maxdepth
    onefile = ""
    onenumsep = 0
    for onefile in filelist:
        onenumsep = onefile.count('/')
        if (onenumsep >= minnumsep) and (onenumsep <= maxnumsep):
            paredfilelist.append(onefile)
    paredfilelist.sort()
    if not matchme == "":
        matchfilelist = []
        if not matchme[0] == "*":
            matchme = "*" + matchme
        for myfile in paredfilelist:
            if fnmatch.fnmatch(myfile, matchme):
                matchfilelist.append(myfile)
        return matchfilelist
    else:
        return paredfilelist
Exemplo n.º 2
0
    def print_python_section(self, secname):
        """Print python commands that will recreate the InputOptions section
            when the commands are run in python.

            secname <str>: section name
        """
        pln = list()
        if not type(secname) == str:
            errorstr = "Section name " + str(secname) + "must be a string."
            raise MASTError(self.__class__.__name__, errorstr)
        if not secname in self.options.keys():
            errorstr = "No section " + str(secname) + "in self.options."
            raise MASTError(self.__class__.__name__, errorstr)
        initheader = secname
        pln.append(initheader + " = dict()")
        for key1, val1 in self.options[secname].iteritems():
            if key1 == 'structure':
                pln.append("#ignoring created structure")
                continue
            if type(key1) == str:
                header = initheader + "[" + "'" + key1 + "'" + "]"
            else:
                header = initheader + "[" + key1 + "]"
            if not type(key1) == str:
                errorstr = "One of the keys in section names is not a string."
                raise MASTError(self.__class__.__name__, errorstr)
            if type(val1) == str:
                mystr = header + " = " + "'" + val1 + "'"
                pln.append(mystr)
            elif type(val1) in [int, bool, float]:
                mystr = header + " = " + str(val1)
                pln.append(mystr)
            elif val1 == None:
                mystr = header + " = None"
                pln.append(mystr)
            elif type(val1) == np.ndarray:
                pln.append(header + "=" + "np.array(" + str(val1.tolist()) +
                           ")")
            elif type(val1) == list:
                mystr = header + " = list()"
                pln.append(mystr)
                for myitem in val1:
                    if type(myitem) == str:
                        mystr = header + ".append(" + "'" + myitem + "'" + ")"
                        pln.append(mystr)
                    else:
                        pln.append(header + ".append(" + str(myitem) + ")")
            elif type(val1) == dict:
                mystr = header + " = dict()"
                pln.append(mystr)
                pln.extend(self.print_secondlevel_dict(header, val1))
            else:
                errstr = "UNSUPPORTED TYPE" + str(type(val1))
                raise MASTError(self.__class__.__name__, errstr)
        return pln
Exemplo n.º 3
0
 def _replace_my_displacements(self):
     """
         In VASP, 0.01 0 0 in DYNMAT from phonons is 1/Angstroms in the 
         x-direction (XDATCAR shows that it makes fractional coord 
         displacements for all 3 lattice vectors in a non-cubic system to get 
         this strictly-x-direction) 
         In PHON, 0.01 0 0 means 0.01 multiplied by lattice vector a.
         Back out the fractional displacements used by VASP from the XDATCAR, 
         match them up, and use them.
         Konfig =1 is the un-displaced cell.
         Now for NFREE=2,
         there are two Konfigs for each displacment; the first is positive
         POTIM in the x-direction (for example, POTIM = 0.01), then negative
         POTIM in the x-direction, then y, then z.
         So one unfrozen atom has seven Konfigs.
         DYNMAT, however, reports the AVERAGE force from each Konfig pair.
         So we only want Konfigs 2, 4, and 6, corresponding to POTIM 0 0, 
         0 POTIM 0, and 0 0 POTIM
     """
     name = self.keywords['name']
     if not os.path.isfile(name + "/XDATCAR"):
         raise MASTError("checker/phon_checker",
                         "No XDATCAR found in %s." % name)
     myvc = VaspChecker(name=self.keywords['name'],
                        program_keys=self.keywords['program_keys'],
                        structure=self.keywords['structure'])
     myxdat = myvc.read_my_displacement_file(name)
     if not os.path.isfile(name + "/DYNMAT"):
         raise MASTError("checker/phon_checker",
                         "No DYNMAT found in %s." % name)
     myforces = myvc.read_my_dynamical_matrix_file(name)
     atomlist = myforces['atoms'].keys()
     atomlist.sort()
     #first disp needs kfg 2
     #second disp needs kfg 4
     #third disp needs kfg 6...
     dispct = 0
     for atom in atomlist:
         displist = myforces['atoms'][atom].keys()
         displist.sort()
         for disp in displist:
             dispct = dispct + 1
             kfgidx = dispct * 2
             atomline = myxdat['configs'][kfgidx][
                 atom -
                 1]  #indexing of atoms starts at config list entry 0 for atom 1
             baseline = myxdat['configs'][1][atom - 1]
             atomcoords = np.array(atomline.strip().split(), float)
             basecoords = np.array(baseline.strip().split(), float)
             dispcoords = atomcoords - basecoords
             displine = str(dispcoords[0]) + " " + str(
                 dispcoords[1]) + " " + str(dispcoords[2])
             myforces['atoms'][atom][disp]['displine'] = displine
     myvc.write_my_dynamical_matrix_file(myforces, name, "DYNMAT_mod_1")
Exemplo n.º 4
0
 def run_a_method(self, iname, methodstring, minputs):
     """Run a method. Evaluates the method to run.
         Args:
             iname <str>: ingredient name
             methodstring <str>: string of class.method, e.g.:
                     ChopIngredient.write_singlerun
                     If class is not given,
                     ChopIngredient is assumed.
                 Will look in MAST.ingredients.
             minputs <list>: method inputs
         Returns:
             Results of the method, whatever it returns.
     """
     self.logger.debug("Attempt to run method %s with inputs %s" % (methodstring, minputs))
     len_inputs = len(minputs)
     #Is it a ChopIngredient method?
     myclass = ""
     methodname = ""
     if not '.' in methodstring:
         myclass = "ChopIngredient"
         methodname = methodstring
     else:
         myclass = methodstring.split('.')[0]
         methodname = methodstring.split('.')[1]
     choplib_mast = importlib.import_module("MAST.ingredients")
     chopmods_mast = inspect.getmembers(choplib_mast, predicate=inspect.ismodule)
     chopclasses = list()
     for chopmod_mast in chopmods_mast:
         chopclasses.extend(inspect.getmembers(chopmod_mast[1], predicate=inspect.isclass))
     for classitem in chopclasses:
         if classitem[0] == myclass:
             mymembers = inspect.getmembers(classitem[1], predicate=inspect.ismethod)
             for classmember in mymembers:
                 if methodname == classmember[0]:
                     my_ing = classitem[1](name = self.ingred_input_options[iname]['name'], program_keys = self.ingred_input_options[iname]['program_keys'], structure = self.ingred_input_options[iname]['structure'])
                     if len_inputs == 0:
                         mresult = classmember[1](my_ing)
                     elif len_inputs == 1:
                         mresult = classmember[1](my_ing, minputs[0])
                     elif len_inputs == 2:
                         mresult = classmember[1](my_ing, minputs[0], minputs[1])
                     elif len_inputs == 3:
                         mresult = classmember[1](my_ing, minputs[0], minputs[1], minputs[2])
                     elif len_inputs == 4:
                         mresult = classmember[1](my_ing, minputs[0], minputs[1], minputs[2], minputs[3])
                     else:
                         raise MASTError(self.__class__.__name__, "Function %s for ChopIngredient requires too many inputs (> 4)." % methodname)
                     self.logger.info("Results for method name %s: %s" % (methodname, mresult))
                     return mresult
     raise MASTError(self.__class__.__name__,"Could not find method %s in class %s" % (methodname, myclass))
     return None
Exemplo n.º 5
0
 def make_coordinate_and_element_list_from_manifest(self,
                                                    manname,
                                                    ing_label=""):
     """
         Args:
             ing_label <str>: If blank, use orig_frac_coords.
                             Otherwise, use <ing_label>_frac_coords
     """
     if ing_label == "":
         ing_label = "original"
     coordlist = list()
     elemlist = list()
     mlist = list(self.read_manifest_file("%s/%s" % (self.sdir, manname)))
     for aidxline in mlist:
         aidxsplit = aidxline.split(";")
         aidx = aidxsplit[0]
         idxtorepl = ""
         if len(aidxsplit) > 1:
             idxtorepl = aidxsplit[1]
         ameta = Metadata(metafile="%s/atom_index_%s" % (self.sdir, aidx))
         if idxtorepl == "":
             frac_coords = ameta.read_data("%s_frac_coords" % ing_label)
             if frac_coords == None:
                 raise MASTError(
                     self.__class__.__name__,
                     "No coordinates for %s_frac_coords building from manifest %s/%s using atom index %s"
                     % (ing_label, self.sdir, manname, aidx))
         elif idxtorepl == "int":  #interstitial
             frac_coords = ameta.read_data("original_frac_coords")
             if frac_coords == None:
                 raise MASTError(
                     self.__class__.__name__,
                     "No coordinates for %s_frac_coords building from manifest %s/%s using atom index %s"
                     % (ing_label, self.sdir, manname, aidx))
         else:  #substitution
             replmeta = Metadata(metafile="%s/atom_index_%s" %
                                 (self.sdir, idxtorepl))
             frac_coords = replmeta.read_data("%s_frac_coords" % ing_label)
             if frac_coords == None:
                 raise MASTError(
                     self.__class__.__name__,
                     "No coordinates for %s_frac_coords building from manifest %s/%s using atom index %s for coordinates and atom index %s for element"
                     % (ing_label, self.sdir, manname, idxtorepl, aidx))
         frac_coords = frac_coords.split("[")[1]
         frac_coords = frac_coords.split("]")[0]
         frac_array = np.array(frac_coords.split(), 'float')
         elem = ameta.read_data("element")
         coordlist.append(frac_array)
         elemlist.append(elem)
     return [coordlist, elemlist]
Exemplo n.º 6
0
 def to_file(self, file_path):
     """Writes data to a file (overwrites existing file)."""
     #TTM+2 10/7/11 add error checking in case of no path
     if (file_path == "") or (file_path == None):
         raise MASTError(self.__class__.__name__,
                         "Refusing to copy to empty path.")
     if self.data == []:
         raise MASTError(self.__class__.__name__,
                         "Empty file not copied to " + file_path)
     dirutil.lock_directory(os.path.dirname(file_path))
     writef = open(file_path, 'wb')
     for line in self.data:
         writef.write(line)
     writef.close()
     dirutil.unlock_directory(os.path.dirname(file_path))
Exemplo n.º 7
0
    def run(self, verbose=0, single_recipe=0, single_ingred=0):
        """Run the MAST monitor.
        """
        if (single_ingred == 0) and ("dagman" in dirutil.get_mast_platform()):
            return None  #Do not auto-run from __init__ method for CHTC/DAGMan
        curdir = os.getcwd()
        try:
            os.chdir(self.scratch)
        except:
            os.chdir(curdir)
            errorstr = "Could not change directories to MAST_SCRATCH at %s" % self.scratch
            raise MASTError(self.__class__.__name__, errorstr)

        #dirutil.lock_directory(self.scratch, 1) # Wait 5 seconds
        #Directory is now locked by mast initially, but gets
        #unlocked at the end of the mastmon run.
        if single_ingred == 0:
            recipe_dirs = dirutil.walkdirs(self.scratch, 1, 1)
        else:
            recipe_dirs = list()
            recipe_dirs.append(single_recipe)

        if verbose == 1:
            self.logger.info("================================")
            self.logger.info("Recipe directories:")
            for recipe_dir in recipe_dirs:
                self.logger.info(recipe_dir)
            self.logger.info("================================")

        for recipe_dir in recipe_dirs:
            self.check_recipe_dir(recipe_dir, verbose, single_ingred)

        dirutil.unlock_directory(self.scratch)  #unlock directory
        os.chdir(curdir)
Exemplo n.º 8
0
 def do_ingredient_methods(self, iname, methodtype, childname=""):
     """Do the ingredient methods.
         Args:
             iname <str>: ingredient name
             methodtype <str>: method type (write, run, etc.)
         The method list should have the format:
         [[method1name,arg1,arg2,...][method2name,arg1,...]]
     """
     if methodtype == 'mast_write_method':
         mlist = self.write_methods[iname]
     elif methodtype == 'mast_run_method':
         mlist = self.run_methods[iname]
     elif methodtype == 'mast_ready_method':
         mlist = self.ready_methods[iname]
     elif methodtype == 'mast_complete_method':
         mlist = self.complete_methods[iname]
     elif methodtype == 'mast_update_children_method':
         mlist = self.update_methods[iname][childname]
     else:
         raise MASTError(self.__class__.__name__,"Bad call to do_ingredient_methods with method type %s" % methodtype)
     allresults = list()
     self.logger.debug("Do methods for %s" % methodtype)
     for methoditem in mlist:
         minputs = list(methoditem[1:])
         if methodtype == 'mast_update_children_method':
             minputs.append(childname)
         mresult = self.run_a_method(iname, methoditem[0], minputs)
         allresults.append(mresult)
     return allresults
Exemplo n.º 9
0
 def get_structure_from_file(self, myfilepath=""):
     """Get the structure from a specified file path.
         For LAMMPS, this is a trajectory-type file.
         Args:
             myfilepath <str>: File path for structure.
     """
     dir = os.path.dirname(myfilepath)
     if dir == '':
         dir = os.getcwd()
     filelist = os.listdir(dir)
     atomsymbolsfile = None
     for onefile in filelist:
         if 'atom_symbols' in onefile:
             atomsymbolsfile = os.path.join(dir, onefile)
     #Read the atom types from the atoms symbol file
     f = open(atomsymbolsfile, 'r')
     atomslist = list()
     for line in f.readlines():
         atomslist.append(line.strip())
     f.close()
     if "DATA" in myfilepath:
         atms = read_lammps_data(myfilepath, atomslist)
     elif "TRAJECTORY" in myfilepath:
         atms, velocities, forces = read_lammps_trajectory(
             myfilepath, -1, atomslist)
     else:
         raise MASTError(
             self.__class__.__name__,
             "Unknown file type for receiving structure from LAMMPS: %s" %
             myfilepath)
     return AseAtomsAdaptor.get_structure(atms)
Exemplo n.º 10
0
 def softlink_a_file(self, childpath, filename):
     """Softlink a parent file to a matching name in the child folder.
         Args:
             childpath <str>: path to child ingredient
             filename <str>: file name (e.g. "CHGCAR")
     """
     parentpath = self.keywords['name']
     dirutil.lock_directory(childpath)
     import subprocess
     #print "cwd: ", os.getcwd()
     #print parentpath
     #print childpath
     if os.path.isfile("%s/%s" % (parentpath, filename)):
         if not os.path.isfile("%s/%s" % (childpath, filename)):
             curpath = os.getcwd()
             os.chdir(childpath)
             mylink = subprocess.Popen("ln -s %s/%s %s" %
                                       (parentpath, filename, filename),
                                       shell=True)
             mylink.wait()
             os.chdir(curpath)
         else:
             self.logger.warning(
                 "%s already exists in %s. Parent %s not softlinked." %
                 (filename, childpath, filename))
     else:
         raise MASTError(
             self.__class__.__name__,
             "No file in parent path %s named %s. Cannot create softlink." %
             (parentpath, filename))
     dirutil.unlock_directory(childpath)
Exemplo n.º 11
0
def get_mast_platform():
    getplatform = os.getenv('MAST_PLATFORM')
    if getplatform == None:
        raise MASTError(
            "utility dirutil",
            "No platform set in environment variable MAST_PLATFORM")
    return getplatform.strip().lower()
Exemplo n.º 12
0
 def graft_coordinates_onto_structure(self, coordstruc):
     """Graft coordinates from mast_coordinates Structure objects
         onto the appropriate structure
         Args:
             coordstruc <Structure>: Structure object with
                 the coordinates for grafting
             self.keywords['struc_work1'] will contain
                 the elements and lattice parameter, which
                 will not be touched.
         Returns:
             modified Structure object <Structure>
     """
     goodstruc = self.keywords['struc_work1'].copy()
     lengoodsites = len(goodstruc.sites)
     lencoordsites = len(coordstruc.sites)
     if not (lengoodsites == lencoordsites):
         raise MASTError(
             self.__class__.__name__,
             "Original and coordinate structures do not have the same amount of sites in %s"
             % self.keywords['name'])
     cct = 0
     newsites = list()
     mylattice = goodstruc.lattice
     while cct < lengoodsites:
         newcoords = coordstruc.sites[cct].frac_coords
         oldspecie = goodstruc.sites[cct].specie
         newsite = PeriodicSite(oldspecie, newcoords, mylattice)
         newsites.append(newsite)
         cct = cct + 1
     goodstruc.remove_sites(range(0, lengoodsites))
     for cct in range(0, lengoodsites):
         goodstruc.append(newsites[cct].specie, newsites[cct].frac_coords)
     return goodstruc
Exemplo n.º 13
0
 def change_my_status(self, newstatus):
     """Change an ingredient status by writing the new status to 
         change_status.txt in the ingredient folder, to get picked
         up by the recipe plan.
         Args:
             newstatus <str>: New status to which to change the ingredient.
     """
     ingdir = self.keywords['name']
     oneup = os.path.dirname(ingdir)
     tryrecipe = os.path.basename(oneup)
     statuspath = ""
     if dirutil.dir_is_in_scratch(tryrecipe):
         statuspath = "%s/change_status.txt" % ingdir
     else:
         twoup = os.path.dirname(oneup)
         tryrecipe = os.path.basename(twoup)
         if dirutil.dir_is_in_scratch(tryrecipe):
             statuspath = "%s/change_status.txt" % oneup
         else:
             raise MASTError(
                 self.__class__.__name__,
                 "Cannot change status of ingredient %s as recipe %s or %s is not found in $MAST_SCRATCH."
                 % (self.keywords['name'], oneup, twoup))
     if os.path.isfile(statuspath):
         statusfile = MASTFile(statuspath)
     else:
         statusfile = MASTFile()
     statusfile.data.append("%s:recommend:%s" % (newstatus, time.asctime()))
     statusfile.to_file(statuspath)
     self.logger.info("Recommending status change to %s" % newstatus)
Exemplo n.º 14
0
 def get_energy_from_energy_file(self):
     """Get the energy from the energy file.
         For VASP neb, this is the last E0 energy from each
         OSZICAR,
         and this function should be used if vasprun.xml
         is corrupted or not available.
         Args:
             mydir <str>: Directory in which to look.
         Returns:
             <str>: all last E0 energies from OSZICAR files.
     """
     myct = 0
     mystr = ""
     while myct <= self.keywords['program_keys']['mast_neb_settings'][
             'images'] + 1:
         fullpath = os.path.join(self.keywords['name'],
                                 str(myct).zfill(2), "OSZICAR")
         if not os.path.isfile(fullpath):
             raise MASTError(self.__class__.__name__,
                             "No OSZICAR file at %s" % fullpath)
         myosz = MASTFile(fullpath)
         mye0 = myosz.get_segment_from_last_line_match("E0", "E0=", "d E =")
         mye0 = float(mye0)
         mystr = mystr + "%3.3f" % mye0 + ';'
         myct = myct + 1
     mystr = mystr[0:-1]  #remove last semicolon
     return mystr
Exemplo n.º 15
0
 def process_phononlines(self, processing_lines):
     """add phonon information to the metadata. Does not change line info.
     """
     raise MASTError(self.__class__.__name__, "This function is obsolete.")
     for line in processing_lines:
         if 'ingredient' in line and 'phonon_' in line:
             nameval = line.split()[1]
             [dataline, dataval] = self.metafile.search_data(nameval)
             okay = 0
             if not (dataval == None):
                 datapcs = dataval.split(',')
                 for datapc in datapcs:
                     dlabel = datapc.split(":")[0].strip()
                     dval = datapc.split(":")[1].strip()
                     if dlabel == 'neblabel' or dlabel == 'defect_label':
                         data = 'phononlabel: %s' % dval
                         self.metafile.write_data(nameval, data)
                         okay = 1
                         break
             if okay == 0:
                 if 'perfect' in line:
                     data = 'phononlabel: perfect'
                     self.metafile.write_data(nameval, data)
                     okay = 1
                 else:
                     data = 'phononlabel: %s' % nameval
                     self.metafile.write_data(nameval, data)
                     okay = 1
     return
Exemplo n.º 16
0
    def parse_recipe_template(self):
        """Parses the recipe template file."""
        ipkeys = self.input_options.get_sections()
        if 'personal_recipe' in ipkeys:
            raise MASTError(
                self.__class__.__name__,
                "Personal recipe section still exists in input.inp. Remove this section and try the command again."
            )
            return
        recipe_file_contents = self.input_options.get_item(
            'recipe', 'recipe_file')

        parser_obj = RecipeTemplateParser(templateFile=recipe_file_contents,
                                          inputOptions=self.input_options,
                                          personalRecipe=os.path.join(
                                              self.recdir, 'input.inp'),
                                          working_directory=self.recdir)
        personal_recipe_list = parser_obj.parse()
        #print personal_recipe_list
        if not personal_recipe_list:
            self.logger.info(
                "Within mast/parse_recipe_template: Personal Recipe List is empty. Check whether input.inp has a personal_recipe section!"
            )
        else:
            self.input_options.set_item('personal_recipe',
                                        'personal_recipe_list',
                                        personal_recipe_list)
Exemplo n.º 17
0
 def copy_a_file(self, childpath, pfname, cfname):
     """Copy a parent file to an arbitrary name in the child folder.
         Args:
             childpath <str>: path to child ingredient
             pfname <str>: file name in parent folder (e.g. "CONTCAR")
             cfname <str>: file name for child folder (e.g. "POSCAR")
     """
     parentpath = self.keywords['name']
     dirutil.lock_directory(childpath)
     self.logger.info("Attempting copy of %s/%s into %s/%s" %
                      (parentpath, pfname, childpath, cfname))
     if os.path.isfile("%s/%s" % (parentpath, pfname)):
         if not os.path.isfile("%s/%s" % (childpath, cfname)):
             shutil.copy("%s/%s" % (parentpath, pfname),
                         "%s/%s" % (childpath, cfname))
         else:
             self.logger.warning(
                 "%s already exists in %s. Parent file %s not copied from %s into child %s."
                 % (cfname, childpath, pfname, parentpath, cfname))
     else:
         raise MASTError(
             self.__class__.__name__,
             "No file in parent path %s named %s. Cannot copy into child path as %s."
             % (parentpath, pfname, cfname))
     dirutil.unlock_directory(childpath)
Exemplo n.º 18
0
 def set_structure_from_inputs(self, input_options):
     """Make a pymatgen structure and update the
         structure key.
         Args:
             input_options <InputOptions>
     """
     strposfile = input_options.get_item('structure', 'posfile')
     if strposfile is None:
         iopscoords = input_options.get_item('structure', 'coordinates')
         iopslatt = input_options.get_item('structure', 'lattice')
         iopsatoms = input_options.get_item('structure', 'atom_list')
         iopsctype = input_options.get_item('structure', 'coord_type')
         structure = MAST2Structure(lattice=iopslatt,
                                    coordinates=iopscoords,
                                    atom_list=iopsatoms,
                                    coord_type=iopsctype)
     elif ('poscar' in strposfile.lower()):
         from pymatgen.io.vaspio import Poscar
         structure = Poscar.from_file(strposfile).structure
     elif ('cif' in strposfile.lower()):
         from pymatgen.io.cifio import CifParser
         structure = CifParser(strposfile).get_structures()[0]
     else:
         error = 'Cannot build structure from file %s' % strposfile
         raise MASTError(self.__class__.__name__, error)
     input_options.update_item('structure', 'structure', structure)
     if input_options.get_item('structure', 'use_structure_index') in [
             'True', 'true', 'T', 't'
     ]:
         self.do_structure_indexing(input_options)
     return
Exemplo n.º 19
0
 def process_system_name(self, processing_lines, system_name):
     """replace <sys> with the system name from the input options
     """
     raise MASTError(self.__class__.__name__, "This function is obsolete.")
     for index in xrange(len(processing_lines)):
         processing_lines[index] = processing_lines[index].replace(
             '<sys>', system_name)
     return processing_lines
Exemplo n.º 20
0
 def check_recipe_dir(self, fulldir, verbose, single_ingred_mode):
     """Check a recipe directory.
         Args:
             fulldir <str>: full path of recipe directory
             verbose <int>: verbosity
             single_ingred_mode <str>: 0 for checking all ingredients
                        ingredient name for checking a single ingredient               
     """
     shortdir = os.path.basename(fulldir)  #only the recipe directory name
     if not os.path.exists(fulldir):
         raise MASTError(self.__class__.__name__,
                         "No recipe directory at %s" % fulldir)
     if os.path.exists(os.path.join(fulldir, "MAST_SKIP")):
         self.logger.warning(
             "Skipping recipe %s due to the presence of a MAST_SKIP file in the recipe directory."
             % shortdir)
         return
     if os.path.exists(os.path.join(fulldir, "MAST_ERROR")):
         self.logger.error(
             "ATTENTION!: Skipping recipe %s due to the presence of a MAST_ERROR file in the recipe directory."
             % shortdir)
         return
     self.logger.info("--------------------------------")
     self.logger.info("Processing recipe %s" % shortdir)
     self.logger.info("--------------------------------")
     my_recipe_plan_object = self.set_up_recipe_plan(fulldir, verbose)
     os.chdir(fulldir)  #need to change directories in order to submit jobs?
     try:
         my_recipe_plan_object.check_recipe_status(verbose,
                                                   single_ingred_mode)
     except Exception:
         import sys, traceback
         #ex_type, ex, trbck = sys.exc_info()
         errortext = traceback.format_exc()
         #del trbck
         errorfile = open(os.path.join(fulldir, "MAST_ERROR"), "ab")
         errorfile.write("ERROR LOGGED %s\n" % time.asctime())
         errorfile.write("%s\n" % errortext)
         errorfile.close()
         self.logger.warning(
             "ERROR in recipe %s. Check MAST_ERROR file in the %s directory."
             % (shortdir, fulldir))
         #raise MASTError(self.__class__.__name__,"Error in recipe %s as follows: %s %s %s" % (shortdir, ex_type, ex, errortext))
     os.chdir(self.scratch)
     if my_recipe_plan_object.status == "C":
         shutil.move(fulldir, self._ARCHIVE)
         summarypath = "%s/%s/SUMMARY.txt" % (self._ARCHIVE, shortdir)
         if os.path.isfile(summarypath):
             self.logger.info("Recipe %s completed." % shortdir)
             self.logger.info("SUMMARY.txt below:")
             summarytext = MASTFile(summarypath)
             for myline in summarytext.data:
                 self.logger.info(myline.strip())
     self.logger.info("-----------------------------")
     self.logger.info("Recipe %s processed." % shortdir)
     self.logger.info("-----------------------------")
Exemplo n.º 21
0
 def get_statuses_from_file(self):
     """Get status of each ingredient from a status.txt
         file in the recipe directory.
     """
     statpath = os.path.join(self.working_directory,'status.txt')
     if not os.path.isfile(statpath):
         raise MASTError(self.__class__.__name__, "Could not get status file in %s" % statpath)
     statfile = MASTFile(statpath)
     for statline in statfile.data:
         if statline[0] == "#":
             pass
         else:
             statsplit = statline.strip().split(':')
             oneingred = statsplit[0].strip()
             onestatus = statsplit[1].strip()
             if oneingred in self.ingredients.keys():
                 self.ingredients[oneingred] = onestatus
             else:
                 raise MASTError(self.__class__.__name__, "Ingredient %s is not in the original recipe's ingredients list." % oneingred)
Exemplo n.º 22
0
def get_mast_recipe_path():
    raise NotImplementedError(
        "MAST_RECIPE_PATH is an obsolete environment variable and should no longer be used."
    )
    getpath = os.getenv('MAST_RECIPE_PATH')
    if getpath == None:
        raise MASTError(
            "utility dirutil",
            "No path set in environment variable MAST_RECIPE_PATH")
    return getpath
Exemplo n.º 23
0
def parse_arguments():
    if len(sys.argv) < 3:
        raise MASTError("DAG for MAST", "Not enough arguments.")
    recipe_name_raw = sys.argv[1]
    script_head_dir = sys.argv[2]
    recipe_name = recipe_name_raw
    sys.stdout.write("Parse arguments.\n")
    print "RECIPE: %s" % recipe_name
    #print_to_file(recipe_name, ing_name, "PARSED ARGUMENTS: %s" % (recipe_name, ing_name))
    return recipe_name, script_head_dir
Exemplo n.º 24
0
 def make_structure_index_directory(self):
     """Make structure index directory
     """
     if os.path.isdir(self.sdir):
         import time
         time.sleep(1)
     if os.path.isdir(self.sdir):
         raise MASTError(self.__class__.__name__,
                         "Structure index directory already exists!")
     os.mkdir(self.sdir)
     return
Exemplo n.º 25
0
 def do_interpolation(self, numim):
     """Do interpolation.
         Both struc_work1 and struc_work2 should be defined.
         Args:
             numim <int>: number of images
         Returns:
             <list of Structure>: list of interpolated 
              structures, including the two endpoints
     """
     if self.keywords['struc_work1'] == None:
         raise MASTError(
             self.__class__.__name__,
             "No initial state structure for %s" % self.keywords['name'])
     if self.keywords['struc_work2'] == None:
         raise MASTError(
             self.__class__.__name__,
             "No final state structure for %s" % self.keywords['name'])
     structure_list = self.keywords['struc_work1'].interpolate(
         self.keywords['struc_work2'], numim + 1)
     return structure_list
Exemplo n.º 26
0
 def make_metadata_entries(self, processing_lines):
     """Add metadata entry for all ingredients. 
         Does not change line information.
     """
     raise MASTError(self.__class__.__name__, "This function is obsolete.")
     for line in processing_lines:
         if 'ingredient' in line:
             nameval = line.split()[1]
             data = 'name: %s' % nameval
             self.metafile.write_data(nameval, data)
     return
Exemplo n.º 27
0
def parse_arguments():
    if len(sys.argv) < 3:
        raise MASTError("DAG for MAST", "Not enough arguments.")
    recipe_name_raw = sys.argv[1]
    ing_name_raw = sys.argv[2]
    recipe_name = recipe_name_raw
    ing_name = ing_name_raw
    sys.stdout.write("Parse arguments.\n")
    print "RECIPE: %s" % recipe_name
    print "ING: %s" % ing_name
    return recipe_name, ing_name
Exemplo n.º 28
0
    def start(self):
        """Starts the setup process, parse the recipe file
           Use the input options and recipe info to
           create directories and classes required
        """
        if not self.recipe_file:
            raise MASTError(self.__class__.__name__,
                            "Recipe contents are empty!")
        """if not os.path.exists(self.recipe_file):
            raise MASTError(self.__class__.__name__, "Recipe file not Found!")"""

        if not self.input_options:
            raise MASTError(self.__class__.__name__,
                            "Input Options not provided!")

        #print 'DEBUG:, ingredients info =',
        #for ingredient, value in ingredients_info.items():
        #    print ingredient, value
        recipe_plan = self.create_recipe_plan()
        return recipe_plan
Exemplo n.º 29
0
 def make_working_directory(self):
     """Initialize the directory for writing the 
         recipe and ingredient folders.
     """
     try:
         os.mkdir(self.working_directory)
     except:
         MASTError(
             self.__class__.__name__,
             "Cannot create working directory %s !!!" %
             self.working_directory)
Exemplo n.º 30
0
 def from_file(self, file_path):
     """Reads data from a file and stores it in a list"""
     self.data = []
     #TTM+2 10/7/11 add error checking in case of no file
     if not os.path.isfile(file_path):
         raise MASTError(self.__class__.__name__,
                         "No such file at " + file_path)
     readf = open(file_path, 'rb')
     lines = readf.readlines()
     for line in lines:
         self.data.append(line)
     readf.close()