Package diffpy :: Package srrietveld :: Module export
[frames] | no frames]

Source Code for Module diffpy.srrietveld.export

  1  ############################################################################## 
  2  # 
  3  # diffpy.srrietveld by DANSE Diffraction group 
  4  #                   Simon J. L. Billinge 
  5  #                   (c) 2010 Trustees of the Columbia University 
  6  #                   in the City of New York.  All rights reserved. 
  7  # 
  8  # File coded by:    Yingrui Shang 
  9  # 
 10  # See AUTHORS.txt for a list of people who contributed. 
 11  # See LICENSE.txt for license information. 
 12  # 
 13  ############################################################################## 
 14  __id__ = "$Id: export.py 6718 2011-08-23 21:33:20Z yshang $" 
 15   
 16  '''The functions to export the results from SrRietveld''' 
 17  import tempfile, shutil, os 
 18  from diffpy.pygsas.expfile import EXPFile 
 19  from diffpy.pyfullprof.pcrfilewriter import pcrFileWriter 
 20  from diffpy.srrietveld.convert.datafile import DataFile 
 21  import copy 
 22  import diffpy.srrietveld.gui.srrguiglobals as GLOBALS 
 23  import diffpy.srrietveld.utility as UTIILS 
 24  from diffpy.srrietveld.exceptions import SrrRunTimeError 
 25   
26 -def __generateCifFile(fit, rootName, index):
27 '''generate the exp and gsa file in the workingDir 28 fit -- the fit object 29 workingDir -- the directory to put all the generated files 30 rootName -- The rootName for the files, etc rootName.EXP, rootName.gsa 31 index -- the index of the data in the fit to be exported 32 return status of runDISAGL 33 ''' 34 # save the exp file first 35 #tmpDir = __generateEXPFile(fit, rootName, index) 36 #from diffpy.pygsas.genles import runPowGen 37 #runPowGen(rootName, tmpDir, 'refine', 1, 'r') 38 39 tmpDir = __generateDISAGLFile(fit, rootName, index) 40 41 from diffpy.pygsas.export import runGSAS2CIF 42 43 runGSAS2CIF(rootName, tmpDir) 44 45 return tmpDir
46
47 -def exportCifFile(fit, exportDir, rootname, index):
48 '''Save the EXP file based on user selections and inputs''' 49 50 et = fit.getEngineType() 51 if et != 'gsas': 52 __msg = 'Exporting to CIF file is only supported for GSAS projects.' 53 UTIILS.printWarning(__msg) 54 55 tmpDir = __generateCifFile(fit, rootname, index) 56 57 fileList = [os.path.join(tmpDir, f) 58 for f in os.listdir(tmpDir)] 59 60 for f in fileList: 61 shutil.copyfile(f, os.path.join(exportDir ,os.path.basename(f))) 62 # delete the temp working directory 63 try: 64 shutil.rmtree(tmpDir) 65 except Exception, e: 66 print e.message 67 68 return
69 70 # End class ExportCifDialog 71
72 -def __generateDISAGLFile(fit, rootName, index):
73 '''generate the exp and gsa file in the workingDir 74 fit -- the fit object 75 workingDir -- the directory to put all the generated files 76 rootName -- The rootName for the files, etc rootName.EXP, rootName.gsa 77 index -- the index of the data in the fit to be exported 78 return status of runDISAGL 79 ''' 80 # Generate the EXP file first 81 tmpDir = __generateEXPFile(fit, rootName , index) 82 from diffpy.pygsas.genles import runPowGen 83 from diffpy.pygsas.export import runDISAGL 84 runPowGen(rootName, tmpDir, 'refine', 1, 'r') 85 status = runDISAGL(rootName, tmpDir) 86 87 return tmpDir
88
89 -def exportDISAGLFile(fit, exportDir, rootName, index):
90 '''save the bond length angle file''' 91 92 et = fit.getEngineType() 93 if et != 'gsas': 94 __msg = 'Exporting to DISAGL file is only supported for GSAS projects.' 95 UTIILS.printWarning(__msg) 96 97 # Generate the disagl file 98 tmpDir = __generateDISAGLFile(fit, rootName, index) 99 100 fileList = [os.path.join(tmpDir, f) 101 for f in os.listdir(tmpDir)] 102 103 for f in fileList: 104 shutil.copyfile(f, os.path.join(exportDir ,os.path.basename(f))) 105 # delete the temp working directory 106 try: 107 shutil.rmtree(tmpDir) 108 except Exception, e: 109 print e.message 110 111 return
112
113 -def __generateEXPFile(fit, rootName, index):
114 '''generate the exp and gsa file in the workingDir 115 fit -- the fit object 116 workingDir -- the directory to put all the generated files 117 rootName -- The rootName for the files, etc rootName.EXP, rootName.gsa 118 index -- the index of the data in the fit to be exported 119 return no return value 120 ''' 121 tmpDir = tempfile.mkdtemp() 122 # Export the supporting files from pattern: data file, MFIL (gsas only), 123 # instrument file, etc 124 patterns = fit.getObject('Pattern') 125 126 # dump the instrument file, MFIL, and data files into the directory 127 dataFiles = [] 128 for bid, pt in enumerate(patterns): 129 instFilePath = fit.dumpInstrumentFile(tmpDir, index, bid) 130 fit.dumpIncidentSpectrumFile(tmpDir, index, bid) 131 132 dataFilePath = fit.dumpDataFile(tmpDir, index, bid) 133 df = DataFile(dataFilePath, fit.getEngineType()) 134 dataFiles.append(os.path.basename(df.prepDataFile(tmpDir, instFilePath))) 135 136 # export the exp engine file 137 ds = fit.get('enginefile') 138 139 expFile = EXPFile(expdata = str(ds[index])) 140 # Get number of patterns 141 numPatterns = len(fit.getObject("Pattern")) 142 for ii in range(numPatterns): 143 if dataFiles[ii]: 144 # histogram key generator 145 keybase = ('HST%i' % (ii + 1),) 146 hstkey = lambda *a : keybase + a 147 # histogram name 148 #expFile.setValue(hstkey("HNAM"), pattern.get("HNAM")) 149 # histogram data file 150 expFile.setValue(hstkey("HFIL"), dataFiles[ii]) 151 152 expFile.write(os.path.join(tmpDir, rootName+'.EXP')) 153 154 return tmpDir
155
156 -def exportEngineFile(fit, exportDir, rootName, index):
157 '''Save the EXP file based on user selections and inputs 158 fit - the fit object 159 exportDir - the directory to export the files 160 rootName - the root name of the files to separate files for different datasets 161 index - the index of the file''' 162 try: 163 if fit.getEngineType() == 'gsas': 164 tmpDir = __generateEXPFile(fit, rootName, index) 165 elif fit.getEngineType() == 'fullprof': 166 tmpDir = __generatePcrFile(fit, rootName, index) 167 except Exception, e: 168 if GLOBALS.isDebug: 169 UTIILS.printDebugInfo() 170 else: 171 raise SrrRunTimeError('Error generating engine files:' + e.message) 172 # copy out the exp file and data file 173 fileList = [os.path.join(tmpDir, f) 174 for f in os.listdir(tmpDir)] 175 176 for f in fileList: 177 shutil.copyfile(f, os.path.join(exportDir,os.path.basename(f))) 178 try: 179 shutil.rmtree(tmpDir) 180 except Exception, e: 181 print e.message
182
183 -def __generatePcrFile(fit, rootName, index):
184 '''generate the pcr and data file in the workingDir 185 fit -- the fit object 186 rootName -- The rootName for the files, etc rootName.pcr, rootName.dat 187 index -- the index of the data in the fit to be exported 188 return the extension name for the data file 189 ''' 190 191 tmpDir = tempfile.mkdtemp() 192 193 patterns = fit.getObject('Pattern') 194 dataFiles = [] 195 for bid, pt in enumerate(patterns): 196 dataFilePath = fit.dumpDataFile(tmpDir, index, bid) 197 dataFiles.append(os.path.basename(dataFilePath)) 198 199 enginefit = fit.owner.exportEngineFit(fit, index) 200 201 # change the data file path in the engine fit 202 ef = copy.deepcopy(enginefit) 203 for bid, pattern in enumerate(ef.get('Pattern')): 204 pattern.set('Datafile', dataFiles[bid]) 205 206 pcrFileWriter(ef, str(os.path.join(tmpDir, rootName+'.pcr')), [], 'r') 207 208 return tmpDir
209