1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 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   
 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       
 35       
 36       
 37       
 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   
 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       
 63      try: 
 64          shutil.rmtree(tmpDir) 
 65      except Exception, e: 
 66          print e.message 
 67   
 68      return 
  69       
 70   
 71   
 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       
 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       
 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       
 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       
106      try: 
107          shutil.rmtree(tmpDir) 
108      except Exception, e: 
109          print e.message 
110   
111      return 
 112   
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       
123       
124      patterns = fit.getObject('Pattern') 
125       
126       
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       
137      ds = fit.get('enginefile') 
138       
139      expFile = EXPFile(expdata = str(ds[index])) 
140       
141      numPatterns = len(fit.getObject("Pattern")) 
142      for ii in range(numPatterns): 
143          if dataFiles[ii]: 
144               
145              keybase = ('HST%i' % (ii + 1),) 
146              hstkey = lambda *a : keybase + a 
147               
148               
149               
150              expFile.setValue(hstkey("HFIL"), dataFiles[ii]) 
151               
152      expFile.write(os.path.join(tmpDir, rootName+'.EXP')) 
153       
154      return tmpDir 
 155   
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       
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       
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       
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