1
2
3
4
5
6
7
8
9
10
11
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
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
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
70 '''String conversion'''
71 return str(self.data)
72
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
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
152
153
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