Trees | Indices | Help |
|
---|
|
1 ############################################################################## 2 # 3 # diffpy.srrietveld by DANSE Diffraction group 4 # Simon J. L. Billinge 5 # (c) 2009 Trustees of the Columbia University 6 # in the City of New York. All rights reserved. 7 # 8 # File coded by: Peng Tian 9 # 10 # See AUTHORS.txt for a list of people who contributed. 11 # See LICENSE.txt for license information. 12 # 13 ############################################################################## 14 """ Launch Rietveld refinement and control the refinement. """ 15 __id__ = "$Id: refine.py 5658 2010-07-09 20:05:34Z juhas $" 16 17 from diffpy.srrietveld.convert.xmlparser import * 18 from diffpy.srrietveld.project import Project 19 from diffpy.srrietveld.manager import Manager, SeqManager 20 from diffpy.srrietveld.fitrt import FitRT 2123 """ Parse input options and read xml files """230 231 239 24325 """ Initialization """ 26 import sys 27 28 argv = sys.argv 29 if len(argv) < 2: 30 msg = "Automatic Launched Refinement On Rietveld Refinement\n" 31 msg += "\n%-10s -h For Help"% (argv[0].split("/")[-1]) 32 print msg 33 sys.exit(1) 34 35 self.parseOptions() 36 self.processInputs() 37 self.run() 38 return3941 """ Parse the input options """ 42 from optparse import OptionParser 43 import diffpy.srrietveld.applications.srhelp as srhelp 44 # init 45 optparser = OptionParser() 46 47 # define the input options, will be removed to a common place 48 optparser.add_option("-i", "--instrument", dest="instrument", 49 help = srhelp.helpstring["instrument"]) 50 optparser.add_option("-s", "--structure" , dest="structure", 51 help = srhelp.helpstring["structure"]) 52 optparser.add_option("-d", "--data", dest="datafile", 53 help = srhelp.helpstring["data"]) 54 optparser.add_option("-r", "--strategy", dest="strategyfile", 55 help = srhelp.helpstring["strategy"], default="calibrated") 56 optparser.add_option("--bank", dest="banks", 57 help = srhelp.helpstring["bank"], default = "1") 58 optparser.add_option("-b", "--background", dest="backgroundfile", 59 help = srhelp.helpstring["background"]) 60 optparser.add_option("-e", "--engine", dest="engine", 61 help = srhelp.helpstring["engine"], default="fullprof") 62 optparser.add_option("--excludedregion", dest = "excludedregionfile", 63 help = srhelp.helpstring["excludedregion"]) 64 optparser.add_option("--infofile", dest="infofile", 65 help="Information file. Information file contains 2 columns for" + 66 "datafile and value of environment quantity (e.g. temperature)") 67 optparser.add_option("--datadirectory", dest="datadirectory", 68 help="Directory of source data file") 69 70 (options, args) = optparser.parse_args() 71 self.options = options 72 return7375 """ Process input files and pass into dictionaries. """ 76 from diffpy.srrietveld.utility import checkFormat, parseXYToList 77 from diffpy.srrietveld.convert.gsasinstparser import GSASInstFile 78 from diffpy.srrietveld.addon.datafile import DataFileConverter, createDataDict 79 from diffpy.srrietveld.addon.strategyfile import StrategyFileParser 80 from diffpy.srrietveld.addon.structurefile import StructureFileConverter 81 82 # Set engine and banklist 83 self.engine = self.options.engine.lower() 84 try: 85 self.banklist = self.options.banks.split(",") 86 except: 87 self.banklist = self.options.banks 88 89 # Instrument info process 90 instrumentfile = self.options.instrument 91 instfileformat = checkFormat(instrumentfile) 92 if instfileformat == "xml": 93 instxml = InstrumentXML(instrumentfile) 94 self.instdict = instxml.parseInstrumentXML() 95 elif instfileformat in ["iparm", "prm"]: 96 gsasinst = GSASInstFile(instrumentfile, self.banklist, self.engine) 97 self.instdict = gsasinst.parseInstInfo() 98 else: 99 raise NotImplementedError 100 101 # Structure info process 102 structurefile = self.options.structure 103 strufileformat = checkFormat(structurefile) 104 if strufileformat == "xml": 105 struxml = StructureXML(structurefile) 106 self.strudict = struxml.parseStructureXML() 107 elif strufileformat == "cif": 108 strufile = StructureFileConverter(structurefile) 109 self.strudict = strufile.cifToDict() 110 else: 111 raise NotImplementedError 112 113 # Data file process 114 datafile = self.options.datafile 115 infofile = self.options.infofile 116 if datafile != None: 117 # single dataset refinement 118 self.datadict = {1:{"Name": datafile}} 119 elif infofile != None: 120 # multiple datasets refinement 121 datadirectory = self.options.datadirectory 122 self.datadict = createDataDict(infofile, datadirectory) 123 else: 124 raise NotImplementedError 125 126 for key, value in self.datadict.items(): 127 datafilename = value["Name"] 128 dataformat = checkFormat(datafilename) 129 datafileconverter = DataFileConverter(datafilename, instrumentfile, self.banklist) 130 for bankid in self.banklist: 131 if self.engine == "gsas" and dataformat in ["dat", "chi"]: 132 gsasfile = datafilename.split(dataformat)[0] + "gsa" 133 datafileconverter.toGSASFile(datafilename, gsasfile, int(bankid)) 134 elif self.engine == "gsas" and dataformat in ["gsa", "gda", "raw"]: 135 datafileconverter.readGSASFile(datafilename, int(bankid)) 136 elif self.engine == "fullprof" and dataformat in ["xye", "chi", "gsa", "gda", "raw"]: 137 fullproffile = datafilename.split(dataformat)[0] + "dat" 138 datafileconverter.toFPFile(datafilename, fullproffile, int(bankid)) 139 else: 140 raise NotImplementedError 141 self.datadict[key] = datafileconverter.datadict 142 143 # Refine Strategy file process 144 strategyfile = self.options.strategyfile 145 strategyfileformat = checkFormat(strategyfile) 146 if strategyfileformat == "txt": 147 strategytxt = StrategyFileParser(strategyfile) 148 self.strategy = strategytxt.parseTXTFormat() 149 else: 150 raise NotImplementedError 151 152 # Optional files: background and excludedregion: 153 backgroundfile = self.options.backgroundfile 154 if backgroundfile != None: 155 bkgdformat = checkFormat(backgroundfile) 156 if bkgdformat == "xml": 157 bkgd = BackgroundXML(backgroundfile) 158 bkgd.addToInstDict(self.instdict) 159 elif bkgdformat == "dat": 160 self.bkgdlist = parseXYToList(backgroundfile) 161 else: 162 raise NotImplementedError 163 excludedregionfile = self.options.excludedregionfile 164 if self.options.excludedregionfile != None: 165 exre = ExcludedRegionXML(excludedregionfile) 166 self.exredict = exre.xmlToDict(exre.exrexml) 167 return168170 """ Main controller of the refinement """ 171 from diffpy.srrietveld.buildfit import BuildFit 172 from diffpy.refinementdata.plot import backend 173 174 backend.use('wx') 175 176 # 1. Build Fit Object 177 sortedkeys = sorted(self.datadict.keys()) 178 buildfit = BuildFit(self.engine, self.strudict, self.instdict, 179 self.datadict[sortedkeys[0]]) 180 srrfit = buildfit.buildFit() 181 182 # 2. Add optional background and excludedregion 183 if hasattr(self, "bkgdlist"): 184 buildfit.addBkgdPoints(srrfit, self.bkgdlist) 185 if hasattr(self, "exredict"): 186 buildfit.addExre(srrfit, self.exredict) 187 188 # 3. check whether fit is ready 189 srrfit.validate() 190 191 # force to rewrite everytime 192 project = Project('temp.srr','w') 193 194 # Also create a fitlist to build a manager 195 fitlist = [] 196 197 # 4. Refine 198 datafilenum = len(self.datadict.keys()) 199 if datafilenum > 1: 200 datalist = [] 201 fit = project.importEngineFit(srrfit, 'f1', shape = (datafilenum, )) 202 fitlist.append(fit) 203 datalist.append(self.datadict[sortedkeys[0]]) 204 i = 1 205 for v in sortedkeys[1:]: 206 patterns = fit.fitdata.getObject("Pattern") 207 pattern = patterns.getObject(0) 208 text = pattern.get('Datafile') 209 text[i] = self.datadict[v][1]["Name"] 210 # also update: 211 fitlist.append(FitRT(project, fit.fitdata, i)) 212 i += 1 213 datalist.append(self.datadict[v]) 214 215 project.plot(fit.fitdata, 0) 216 # Now create a manager to run the refinement. 217 manager = SeqManager(fitlist, datalist, self.strategy) 218 else: 219 fitrt = project.importEngineFit(srrfit, 'f1') 220 fitlist.append(fitrt) 221 #project.plot(fitrt.fit) 222 manager = Manager(fitlist, self.strategy) 223 project.managers.append(manager) 224 # run fit 225 project.start() 226 backend.show() 227 # save project 228 project.close() 229 return
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sun Aug 28 23:35:58 2011 | http://epydoc.sourceforge.net |