Changeset 3209 for trunk/GSASIIscriptable.py
- Timestamp:
- Dec 28, 2017 12:50:40 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIscriptable.py
r3208 r3209 563 563 file.close() 564 564 565 def ImportPowder(reader,filename):566 '''Use a reader to import a powder diffraction data file567 568 :param str reader: a scriptable reader569 :param str filename: full name of powder data file; can be "multi-Bank" data570 571 :returns: list rdlist: list of reader objects containing powder data, one for each572 "Bank" of data encountered in file. Items in reader object of interest are:573 574 * rd.comments: list of str: comments found on powder file575 * rd.dnames: list of str: data nammes suitable for use in GSASII data tree NB: duplicated in all rd entries in rdlist576 * rd.powderdata: list of numpy arrays: pos,int,wt,zeros,zeros,zeros as needed for a PWDR entry in GSASII data tree.577 '''578 rdfile,rdpath,descr = imp.find_module(reader)579 rdclass = imp.load_module(reader,rdfile,rdpath,descr)580 rd = rdclass.GSAS_ReaderClass()581 if not rd.scriptable:582 print(u'**** ERROR: '+reader+u' is not a scriptable reader')583 return None584 rdlist = []585 if rd.ContentsValidator(filename):586 repeat = True587 rdbuffer = {} # create temporary storage for file reader588 block = 0589 while repeat: # loop if the reader asks for another pass on the file590 block += 1591 repeat = False592 rd.objname = ospath.basename(filename)593 flag = rd.Reader(filename,None,buffer=rdbuffer,blocknum=block,)594 if flag:595 rdlist.append(copy.deepcopy(rd)) # save the result before it is written over596 if rd.repeat:597 repeat = True598 return rdlist599 print(rd.errors)600 return None565 # def ImportPowder(reader,filename): 566 # '''Use a reader to import a powder diffraction data file 567 568 # :param str reader: a scriptable reader 569 # :param str filename: full name of powder data file; can be "multi-Bank" data 570 571 # :returns: list rdlist: list of reader objects containing powder data, one for each 572 # "Bank" of data encountered in file. Items in reader object of interest are: 573 574 # * rd.comments: list of str: comments found on powder file 575 # * rd.dnames: list of str: data nammes suitable for use in GSASII data tree NB: duplicated in all rd entries in rdlist 576 # * rd.powderdata: list of numpy arrays: pos,int,wt,zeros,zeros,zeros as needed for a PWDR entry in GSASII data tree. 577 # ''' 578 # rdfile,rdpath,descr = imp.find_module(reader) 579 # rdclass = imp.load_module(reader,rdfile,rdpath,descr) 580 # rd = rdclass.GSAS_ReaderClass() 581 # if not rd.scriptable: 582 # print(u'**** ERROR: '+reader+u' is not a scriptable reader') 583 # return None 584 # rdlist = [] 585 # if rd.ContentsValidator(filename): 586 # repeat = True 587 # rdbuffer = {} # create temporary storage for file reader 588 # block = 0 589 # while repeat: # loop if the reader asks for another pass on the file 590 # block += 1 591 # repeat = False 592 # rd.objname = ospath.basename(filename) 593 # flag = rd.Reader(filename,None,buffer=rdbuffer,blocknum=block,) 594 # if flag: 595 # rdlist.append(copy.deepcopy(rd)) # save the result before it is written over 596 # if rd.repeat: 597 # repeat = True 598 # return rdlist 599 # print(rd.errors) 600 # return None 601 601 602 602 def SetDefaultDData(dType,histoName,NShkl=0,NDij=0): … … 1166 1166 :param list phases: Phases to link to the new histogram 1167 1167 :param str fmthint: If specified, only importers where the format name 1168 (reader.formatName, as shown in Import menu) contain ingthe1168 (reader.formatName, as shown in Import menu) contains the 1169 1169 supplied string will be tried as importers. If not specified, all 1170 1170 importers consistent with the file extension will be tried … … 1196 1196 return self.histogram(histname) 1197 1197 1198 def add_simulated_powder_histogram(self, histname, iparams, Tmin, Tmax, Tstep, 1199 wavelength=None, scale=None, phases=[]): 1200 """Loads a powder data histogram into the project. 1201 1202 Requires an instrument parameter file. 1203 Note that in unix fashion, "~" can be used to indicate the 1204 home directory (e.g. ~/G2data/data.prm). The instrument parameter file 1205 will determine if the histogram is x-ray, CW neutron, TOF, etc. as well 1206 as the instrument type. 1207 1208 :param str histname: A name for the histogram to be created. 1209 :param str iparams: The instrument parameters file, a filename. 1210 :param float Tmin: Minimum 2theta or TOF (ms) for dataset to be simulated 1211 :param float Tmax: Maximum 2theta or TOF (ms) for dataset to be simulated 1212 :param float Tstep: Step size in 2theta or TOF (ms) for dataset to be simulated 1213 :param float wavelength: Wavelength for CW instruments, overriding the value 1214 in the instrument parameters file if specified. 1215 :param float scale: Histogram scale factor which multiplies the pattern. Note that 1216 simulated noise is added to the pattern, so that if the maximum intensity is 1217 small, the noise will mask the computed pattern. The scale 1218 needs to be a large number for CW neutrons. 1219 The default, None, provides a scale of 1 for x-rays and TOF; 10,000 for CW neutrons. 1220 :param list phases: Phases to link to the new histogram. Use proj.phases() to link to 1221 all defined phases. 1222 1223 :returns: A :class:`G2PwdrData` object representing the histogram 1224 """ 1225 LoadG2fil() 1226 iparams = os.path.abspath(os.path.expanduser(iparams)) 1227 if not os.path.exists(iparams): 1228 raise G2ScriptException("File does not exist:"+iparams) 1229 rd = G2obj.ImportPowderData( # Initialize a base class reader 1230 extensionlist=tuple(), 1231 strictExtension=False, 1232 formatName = 'Simulate dataset', 1233 longFormatName = 'Compute a simulated pattern') 1234 rd.powderentry[0] = '' # no filename 1235 rd.powderentry[2] = 1 # only one bank 1236 rd.comments.append('This is a dummy dataset for powder pattern simulation') 1237 #Iparm1, Iparm2 = load_iprms(iparams, rd) 1238 if Tmax < Tmin: 1239 Tmin,Tmax = Tmax,Tmin 1240 Tstep = abs(Tstep) 1241 if 'TOF' in rd.idstring: 1242 N = (np.log(Tmax)-np.log(Tmin))/Tstep 1243 x = np.exp((np.arange(0,N))*Tstep+np.log(Tmin*1000.)) 1244 N = len(x) 1245 else: 1246 N = int((Tmax-Tmin)/Tstep)+1 1247 x = np.linspace(Tmin,Tmax,N,True) 1248 N = len(x) 1249 if N < 3: 1250 raise G2ScriptException("Error: Range is too small or step is too large, <3 points") 1251 rd.powderdata = [ 1252 np.array(x), # x-axis values 1253 np.zeros_like(x), # powder pattern intensities 1254 np.ones_like(x), # 1/sig(intensity)^2 values (weights) 1255 np.zeros_like(x), # calc. intensities (zero) 1256 np.zeros_like(x), # calc. background (zero) 1257 np.zeros_like(x), # obs-calc profiles 1258 ] 1259 Tmin = rd.powderdata[0][0] 1260 Tmax = rd.powderdata[0][-1] 1261 rd.idstring = histname 1262 histname, new_names, pwdrdata = load_pwd_from_reader(rd, iparams, 1263 [h.name for h in self.histograms()]) 1264 if histname in self.data: 1265 print("Warning - redefining histogram", histname) 1266 elif self.names[-1][0] == 'Phases': 1267 self.names.insert(-1, new_names) 1268 else: 1269 self.names.append(new_names) 1270 if scale is not None: 1271 pwdrdata['Sample Parameters']['Scale'][0] = scale 1272 elif pwdrdata['Instrument Parameters'][0]['Type'][0].startswith('PNC'): 1273 pwdrdata['Sample Parameters']['Scale'][0] = 10000. 1274 self.data[histname] = pwdrdata 1275 self.update_ids() 1276 1277 for phase in phases: 1278 phase = self.phase(phase) 1279 self.link_histogram_phase(histname, phase) 1280 1281 return self.histogram(histname) 1282 1198 1283 def add_phase(self, phasefile, phasename=None, histograms=[], fmthint=None): 1199 1284 """Loads a phase into the project from a .cif file … … 1202 1287 :param str phasename: The name of the new phase, or None for the default 1203 1288 :param list histograms: The names of the histograms to associate with 1204 this phase 1289 this phase. Use proj.Histograms() to add to all histograms. 1205 1290 :param str fmthint: If specified, only importers where the format name 1206 (reader.formatName, as shown in Import menu) contain ingthe1291 (reader.formatName, as shown in Import menu) contains the 1207 1292 supplied string will be tried as importers. If not specified, all 1208 1293 importers consistent with the file extension will be tried … … 1369 1454 Gives an object representing the specified phase in this project. 1370 1455 1371 :param str phasename: The name of the desired phase. Either the name 1372 (str), the phase's ranId, or the phase's index 1456 :param str phasename: A reference to the desired phase. Either the phase 1457 name (str), the phase's ranId, the phase's index (both int) or 1458 a phase object (:class:`G2Phase`) 1373 1459 :returns: A :class:`G2Phase` object 1374 1460 :raises: KeyError … … 1379 1465 :meth:`G2Project.phases` 1380 1466 """ 1467 if isinstance(phasename, G2Phase): 1468 if phasename.proj == self: 1469 return phasename 1381 1470 phases = self.data['Phases'] 1382 1471 if phasename in phases:
Note: See TracChangeset
for help on using the changeset viewer.