Changeset 1230
- Timestamp:
- Feb 25, 2014 3:06:18 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/imports/G2sad_xye.py
r1225 r1230 23 23 npasind = lambda x: 180.*np.arcsin(x)/np.pi 24 24 25 class xye_ReaderClass(G2IO.ImportSmallAngleData):26 'Routines to import q SAXD data from a . xyefile'25 class txt_ReaderClass(G2IO.ImportSmallAngleData): 26 'Routines to import q SAXD data from a .txt or .dat file' 27 27 def __init__(self): 28 28 super(self.__class__,self).__init__( # fancy way to self-reference 29 extensionlist=('. xye','.txt','.dat'),29 extensionlist=('.txt','.dat'), 30 30 strictExtension=False, 31 formatName = 'q step xye',32 longFormatName = 'q stepped data file'31 formatName = 'q step QIE data', 32 longFormatName = 'q stepped text data file in Q,I,E order; E optional' 33 33 ) 34 34 … … 36 36 def ContentsValidator(self, filepointer): 37 37 'Look through the file for expected types of lines in a valid q-step file' 38 gotCcomment = False39 begin = True40 for i,S in enumerate(filepointer):41 if i > 1000: break42 if begin:43 if gotCcomment and S.find('*/') > -1:44 begin = False45 continue46 if S.strip().startswith('/*'):47 gotCcomment = True48 continue49 if S[0] == '#':50 continue #ignore comments, if any51 elif len(S) == 1:52 continue #ignore blank lines53 elif not gotCcomment:54 begin = False55 # valid line to read?56 if begin:57 continue58 vals = S.split()59 if 2 <= len(vals) <= 3:60 continue61 else:62 self.errors = 'Unexpected information in line: '+str(i+1)63 self.errors += ' '+str(S)64 return False65 return True # no errors encountered66 67 def Reader(self,filename,filepointer, ParentFrame=None, **unused):68 'Read a q-step file'69 x = []70 y = []71 w = []72 try:73 wave = 1.5428 #Cuka default74 Temperature = 30075 gotCcomment = False76 begin = True77 for i,S in enumerate(filepointer):78 self.errors = 'Error reading line: '+str(i+1)79 # or a block of comments delimited by /* and */80 # or (GSAS style) each line can begin with '#'81 if begin:82 if 'Wave' in S.split('=')[0]:83 try:84 wave = float(S.split('=')[1])85 except:86 pass87 if gotCcomment:88 if S.find('*/') == -1:89 self.comments.append(S[:-1])90 else:91 self.comments.append(S[:-1])92 begin = False93 continue94 if S.strip().startswith('/*'):95 self.comments.append(S[:-1])96 gotCcomment = True97 continue98 if S[0] == '#':99 self.comments.append(S[:-1])100 continue #ignore comments, if any101 elif len(S) == 1: #blank line only CR/LF102 continue103 elif not gotCcomment:104 begin = False105 # valid line to read106 if begin:107 continue108 vals = S.split()109 try:110 x.append(float(vals[0]))111 # x.append(2.*npasind(wave*float(vals[0])/(4.*np.pi)))112 f = float(vals[1])113 if f <= 0.0:114 y.append(0.0)115 w.append(1.0)116 elif len(vals) == 3:117 y.append(float(vals[1]))118 w.append(1.0/float(vals[2])**2)119 else:120 y.append(float(vals[1]))121 w.append(1.0/float(vals[1]))122 except ValueError:123 msg = 'Error in line '+str(i+1)124 print msg125 break126 N = len(x)127 for S in self.comments:128 if 'Temp' in S.split('=')[0]:129 try:130 Temperature = float(S.split('=')[1])131 except:132 pass133 self.instdict['wave'] = wave134 self.instdict['type'] = 'LXC'135 x = np.array(x)136 if np.any(x > 2.): #nanometers-1?137 x /= 10.138 self.smallangledata = [139 x, # x-axis values - q140 np.array(y), # small angle pattern intensities141 np.array(w), # 1/sig(intensity)^2 values (weights)142 np.zeros(N), # calc. intensities (zero)143 np.zeros(N), # obs-calc profiles144 ]145 self.smallangleentry[0] = filename146 self.smallangleentry[2] = 1 # xye file only has one bank147 self.idstring = ospath.basename(filename)148 # scan comments for temperature149 self.Sample['Temperature'] = Temperature150 151 return True152 except Exception as detail:153 self.errors += '\n '+str(detail)154 print self.formatName+' read error:'+str(detail) # for testing155 import traceback156 traceback.print_exc(file=sys.stdout)157 return False158 159 class txt_ReaderClass(G2IO.ImportSmallAngleData):160 'Routines to import q SAXD data from a .txt file'161 def __init__(self):162 super(self.__class__,self).__init__( # fancy way to self-reference163 extensionlist=('.txt','.dat'),164 strictExtension=False,165 formatName = 'q step txt',166 longFormatName = 'q stepped text data file'167 )168 169 # Validate the contents -- make sure we only have valid lines170 def ContentsValidator(self, filepointer):171 'Look through the file for expected types of lines in a valid txt q-step file'172 38 Ndata = 0 173 39 for i,S in enumerate(filepointer): 174 40 vals = S.split() 175 if 2 <= len(vals):41 if len(vals) >= 2: 176 42 try: 177 43 data = [float(val) for val in vals] … … 185 51 186 52 def Reader(self,filename,filepointer, ParentFrame=None, **unused): 187 'Read a q-stepfile'53 print 'Read a q-step text file' 188 54 x = [] 189 55 y = [] … … 205 71 continue 206 72 vals = S.split() 207 if 2 <= len(vals):73 if len(vals) >= 2: 208 74 try: 209 75 data = [float(val) for val in vals] … … 235 101 if np.any(x > 2.): #q must be nm-1 236 102 x /= 10. 237 # x = 2.*npasind(wave*x)/(4.*np.pi) #convert to 2-theta238 103 self.smallangledata = [ 239 104 x, # x-axis values q
Note: See TracChangeset
for help on using the changeset viewer.