Package diffpy :: Package srrietveld :: Package convert :: Module datafile
[frames] | no frames]

Source Code for Module diffpy.srrietveld.convert.datafile

  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:    Yingrui Shang 
  9  # 
 10  # See AUTHORS.txt for a list of people who contributed. 
 11  # See LICENSE.txt for license information. 
 12  # 
 13  ############################################################################## 
 14  '''Read the information from the data file. Currently an interface. Using the  
 15  functionality in addon/datafile.py, will improve later''' 
 16   
 17  __id__ = "$Id: datafile.py 6718 2011-08-23 21:33:20Z yshang $" 
 18   
 19  from diffpy.srrietveld.addon.datafile import DataFileConverter 
 20  from diffpy.srrietveld.exceptions import SrrFileError 
 21  import os 
 22   
23 -class DataFile:
24 '''The data file class'''
25 - def __init__(self, filepath = None, engine = None, 26 xobs = None, yobs = None, ysig = None, rootname = None):
27 '''Constructor, the DataFile can be initiated either from a file path 28 or arrays of data 29 filepath - the filepath of the data file 30 engine - the engine type 31 xobs, yobs, ysig - the data arrays to initialize the data file 32 rootname - the rootname of the file when constructed from x/y data arrays. 33 NOTE: the extension name is always dat in this case''' 34 35 if filepath is not None: 36 if not os.path.exists(filepath): 37 raise SrrFileError('The data file ' + filepath + ' does not exist.') 38 if not os.path.isfile(filepath): 39 raise SrrFileError('The data file path' + filepath + 'is not' + \ 40 'a path to a file. ') 41 42 self.engine = engine 43 self.filename = os.path.basename(filepath) 44 self.data = open(filepath, 'rb').read() 45 46 self.xobs = [] 47 self.yobs = [] 48 self.ysig = [] 49 50 if xobs is not None: 51 self.xobs = xobs 52 self.yobs = yobs 53 self.ysig = ysig 54 55 self.filename = 'datafile.dat' if rootname is None else rootname + '.dat' 56 57 # the length of the data is determined by xobs 58 line = [] 59 for idx, xx in enumerate(xobs): 60 if self.ysig is not None: 61 line.append("%s %s %s" %(xx, self.yobs[idx], self.ysig[idx])) 62 else: 63 line.append("%s %s" %(xx, self.yobs[idx])) 64 65 self.data = '\n'.join(line) 66 67 return
68
69 - def __str__(self):
70 '''String conversion''' 71 return str(self.data)
72
73 - def __isConversionNeeded(self):
74 '''Check if the file needs conversion 75 engine - gsas or fullprof 76 return - True if conversion is needed''' 77 engine = self.engine 78 79 root, ext = os.path.splitext(self.filename) 80 if engine.lower() == 'gsas': 81 if ext in [".chi", ".dat", ".txt", 'xye']: 82 return True 83 elif ext in [".gsa", ".gda", "fxye", "raw"]: 84 return False 85 if engine.lower() == 'fullprof': 86 if ext in [".chi", ".gsa", ".gda", "fxye", "raw"]: 87 return True 88 elif ext in [".dat"]: 89 return False 90 else: 91 from diffpy.srrietveld.utility import printWarning 92 __warningMsg = 'Data file extension is not recognized. The ' + \ 93 'refinement may not run correctly. ' 94 printWarning(__warningMsg) 95 96 return False
97
98 - def dump(self, directory, basename = None):
99 '''dump the file to the directory 100 dir: the directory to dump the file 101 rootname: the new file name, if not none will replace self.filename 102 return the file path where the file dumped''' 103 nm = self.filename if basename is None else basename 104 filepath = os.path.join(directory, nm) 105 with open(filepath, 'wb') as f: 106 f.write(self.data) 107 return filepath
108
109 - def write(self, filepath):
110 '''Write to filepath''' 111 with open(filepath, 'wb') as f: 112 f.write(self.data) 113 return
114
115 - def prepDataFile(self, newDir, instrumentFile = None, filename = None):
116 '''Prepare the data file for different engines, and dump the prepared 117 file to a path. If conversion is not needed, the origianl file will be 118 dumped new directory - the path to dump the file 119 engine - the engine 120 rootName - the file name if different from that saved in the object 121 return the path to the prepared data file''' 122 from diffpy.srrietveld.addon.datafile import DataFileConverter 123 from diffpy.srrietveld.utility import printWarning 124 125 if not instrumentFile: 126 instrumentFile = None 127 128 fullpath = self.dump(newDir, filename) 129 130 root, ext = os.path.splitext(fullpath) 131 132 if filename: 133 root = os.path.splitext(filename)[0] 134 135 newPath = fullpath 136 137 if self.__isConversionNeeded(): 138 dataFile = DataFileConverter(fullpath) 139 if self.engine.lower() == 'gsas': 140 newPath = root + '.gsa' 141 # bank id is always 1 142 dataFile.toGSASFile(fullpath, newPath, 1) 143 if self.engine.lower() == 'fullprof': 144 newPath = root + '.dat' 145 146 if ext.lower() != '.chi': 147 __warningMsg = 'To convert GSAS data file, installation of ' + \ 148 'GSAS is needed and an instrument file is required. ' 149 150 printWarning(__warningMsg) 151 # FIXM: convert gsas data file name to fullprof data file name 152 # search the local directory for EXP files, may implement a 153 # better algrethm 154 dfc = DataFileConverter(fullpath, instrumentFile) 155 import glob 156 desfile = os.path.join(os.getcwd(), "*.EXP") 157 expfilename = None 158 for fn in glob.glob(desfile): 159 expfilename = fn 160 if expfilename: 161 dfc.toFPFile(fullpath, newPath, 1, expfilename) 162 elif ext.lower() == '.chi': 163 dfc.toFPFile(fullpath, newPath, 1) 164 else: 165 __warningMsg = "To convert GSAS data file, GSAS is needed " + \ 166 "to be installed and an instrument file is required." + \ 167 "The gsas data file can not be converted. The refinment may " + \ 168 "not run. " 169 printWarning(__warningMsg) 170 171 return newPath
172