Package reflectometry :: Package model1d :: Package model :: Module staj2model

Source Code for Module reflectometry.model1d.model.staj2model

  1  import numpy 
  2  import os 
  3  import sys 
  4   
  5  FWHM_to_sigma = numpy.sqrt( numpy.log(256.0) ) 
  6  Pi16        = 16.0*numpy.pi 
  7   
8 -def parseLayers( layerLists ):
9 """Separate layer info into depth, rough, rho, mu lists""" 10 11 # TODO: use zip 12 depth=[]; rough=[]; rho=[]; rhom=[]; mu=[] 13 for x in xrange( len(layerLists) ): 14 rho.append( layerLists[x][0] ) 15 rhom.append( layerLists[x][1] ) 16 depth.append( layerLists[x][2] ) 17 rough.append( layerLists[x][3] ) 18 mu.append( layerLists[x][4] ) 19 20 # TODO: return a,b,c rather than return [a,b,c] 21 return [ depth, rough, rho, mu, rhom ]
22 23 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 24 # Parse staj data ( non magnetic case ) 25 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26 -def parseQRange( QLists ):
27 28 qmin=[]; qmax=[]; nQ=[] 29 for i in xrange( len(QLists) ): 30 qmin.append( QLists[i][0] ) 31 qmax.append( QLists[i][1] ) 32 nQ.append( QLists[i][2] ) 33 34 return [ qmin, qmax, nQ ]
35 36 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 37 # Separate layers infos into: ( magnetic case ) 38 # depth,rough,rho,mu, depthm,roughm,rhom,theta lists 39 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40 -def parseMLayers( layerLists ):
41 42 depth =[]; rough =[]; rho =[]; mu =[] 43 depthm=[]; roughm=[]; rhom=[]; theta=[] 44 45 for x in xrange( len(layerLists) ): 46 rho.append( layerLists[x][0]); rhom.append( layerLists[x][4]) 47 depth.append(layerLists[x][1]); depthm.append(layerLists[x][5]) 48 rough.append(layerLists[x][2]); roughm.append(layerLists[x][6]) 49 mu.append( layerLists[x][3]); theta.append( layerLists[x][7]) 50 51 return [ rho,rhom, depth,depthm, rough,roughm, mu,theta ]
52 53 54 55 56 57 #==================================================================== 58 # Translate the Staj file into model 59 # 60 # Non magnetic case 61 #====================================================================
62 -class Staj2Model:
63
64 - def __init__(self, 65 filename, # file which contains staj, 66 scale = False 67 ):
68 # scale factor for rho, mu,etc 69 if scale == True: 70 self.scale = 1.0e6 71 else: 72 self.scale = 1.0 73 74 fin = open(filename,'r') 75 self.lines = fin.readlines() 76 self.nL = len(self.lines) 77 self.build()
78 79
80 - def _read_n_layers( self, strLine ):
81 """Read the top line of the staj file giving the number of top 82 middle and bottom layers, number of fit parameters and number of 83 roughness steps. 84 """ 85 86 _strL = strLine.split() 87 self._nTLayer = _str2Int( _strL[0] ) 88 self._nMLayer = _str2Int( _strL[1] ) 89 self._nBLayer = _str2Int( _strL[2] ) 90 self._nRepeat = _str2Int( _strL[3] ) 91 self._nFit = _str2Int( _strL[4] ) 92 self._nRough = _str2Int( _strL[5] ) 93 94 self.nLayers = self._nTLayer + self._nMLayer + self._nBLayer + 3
95 96
97 - def _read_Metadata1( self, strLine ):
98 """Read in line 2 of the staj file: L dL dTheta and theta offset 99 """ 100 _strL = strLine.split() 101 102 self._WaveLength = _str2Float( _strL[0] ) 103 self._WaveLengthDiv = _str2Float( _strL[1] ) 104 self._AngularDiv = _str2Float( _strL[2] )
105 106
107 - def _read_Metadata2( self, strLine ):
108 """ 109 Read in third line of the staj file: Io Ibkg Qmin Qmax #Q 110 """ 111 _strL = strLine.split() 112 113 self._Intensity = _str2Float( _strL[0] ) 114 self._Background = _str2Float( _strL[1] ) 115 self._Qmin = _str2Float( _strL[2] ) 116 self._Qmax = _str2Float( _strL[3] ) 117 self._nQ = _str2Int( _strL[4] )
118 119 120
121 - def _read_a_profile(self, strLine ):
122 """ 123 Read one layer from the staj file: rho, rho_M d roughness mu 124 """ 125 _strL = strLine.split() 126 127 _rho = _str2Float( _strL[0] ) * self.scale/Pi16 128 _rho_m = _str2Float( _strL[1] ) * self.scale 129 _depth = _str2Float( _strL[2] ) 130 _rough = _str2Float( _strL[3] ) / FWHM_to_sigma 131 _mu = _str2Float( _strL[4] ) * self.scale 132 133 return [ _rho, _rho_m, _depth, _rough, _mu ]
134 135 136
137 - def build(self):
138 """Interpret all the lines of the staj file 139 """ 140 curr = 0 141 self._read_n_layers( self.lines[ curr ] ) 142 143 curr += 1 144 self._read_Metadata1( self.lines[ curr ] ) 145 146 curr += 1 147 self._read_Metadata2( self.lines[ curr ] ) 148 149 self._proftyp = self.lines[ curr+1 ].strip() 150 self._datafile = self.lines[ curr+2 ].strip() 151 self._outputfile = self.lines[ curr+3 ].strip() 152 153 curr += 4 154 155 self.layers = [ ] 156 for i in xrange( self._nTLayer+1 ) : 157 ret = self._read_a_profile( self.lines[ curr ] ) 158 self.layers.append( ret ) 159 curr += 1 160 161 curr += 1 162 for i in xrange( self._nMLayer ) : 163 ret = self._read_a_profile( self.lines[ curr ] ) 164 self.layers.append( ret ) 165 curr += 1 166 167 curr += 1 168 for i in xrange( self._nBLayer ) : 169 ret = self._read_a_profile( self.lines[ curr ] ) 170 self.layers.append( ret ) 171 curr += 1 172 173 (self.depth, self.rough, self.rho, self.mu, self.rhom) = \ 174 parseLayers( self.layers )
175 176
177 - def _toModel(self):
178 """ 179 Build model from information loaded from staj. 180 Must call self.build() first. 181 """ 182 _metadata = [ self._WaveLength, self._WaveLengthDiv, self._AngularDiv, 183 self._Intensity, self._Background ] 184 185 _layers = [ self.depth, self.rough, self.rho, self.mu, self.rhom] 186 _Qrange = [ self._Qmin, self._Qmax, self._nQ ] 187 188 _nLayers = [ self._nTLayer, self._nMLayer, self._nBLayer, 189 self._nRepeat, self._nFit, self._nRough ] 190 191 _typeFiles= [ self._proftyp, self._datafile, self._outputfile ] 192 193 ret = [ ] 194 ret.append( _layers ) 195 ret.append( _metadata ) 196 ret.append( _nLayers ) 197 ret.append( _Qrange ) 198 ret.append( _typeFiles ) 199 200 return ret
201 202 203 204 205 #================================================ 206 # Translate the Staj file into model 207 # 208 # magnetic case 209 #================================================
210 -class Staj2MModel:
211
212 - def __init__(self, 213 filename, 214 scale = False 215 ):
216 # scale factor for rho, mu,etc 217 if scale == True: 218 self.scale = 1.0e6 219 else: 220 self.scale = 1.0 221 222 fin = open(filename,'r') 223 self.lines = fin.readlines() 224 self.nL = len(self.lines) 225 self.curr = 0 226 self.build()
227 228
229 - def _read_n_layers( self, strLine ):
230 """Read the top line of the staj file giving the number of top 231 middle and bottom layers, number of fit parameters and number of 232 roughness steps. 233 """ 234 235 _strL = strLine.split() 236 self._nLayer = _str2Int( _strL[0] ) 237 self._nRough = _str2Int( _strL[1] ) 238 self._nFit = _str2Int( _strL[2] )
239 240
241 - def _read_Metadata1( self, strLine ):
242 """Read in line 2 of the staj file: L dL dTheta and theta offset 243 """ 244 _strL = strLine.split() 245 246 self._WaveLength = _str2Float( _strL[0] ) 247 self._WaveLengthDiv = _str2Float( _strL[1] ) 248 self._AngularDiv = _str2Float( _strL[2] ) 249 self._Aguide = _str2Float( _strL[3] )
250 251
252 - def _read_Metadata2( self, strLine ):
253 """Read in third line of the staj file: Io Ibkg Qmin Qmax #Q 254 """ 255 256 _strL = strLine.split() 257 258 self._Intensity = _str2Float( _strL[0] ) 259 self._Background = _str2Float( _strL[1] )
260 261
262 - def _read_Infos( self, strLine ):
263 """ 264 Read in third line of the staj file: Io Ibkg Qmin Qmax #Q 265 """ 266 _strL = strLine.split() 267 _Qmin = _str2Float( _strL[0] ) 268 _Qmax = _str2Float( _strL[1] ) 269 _nQ = _str2Int( _strL[2] ) 270 271 return [ _Qmin, _Qmax, _nQ ]
272 273
274 - def _read_a_profile(self, strLine ):
275 """Read one layer from the staj file: rho, rho_M d roughness mu 276 """ 277 _strL = strLine.split() 278 279 _rho = _str2Float( _strL[0] ) * self.scale/Pi16 280 _depth = _str2Float( _strL[1] ) 281 _rough = _str2Float( _strL[2] ) / RoughFactor 282 _mu = _str2Float( _strL[3] ) * self.scale 283 284 return [ _rho, _depth, _rough, _mu ]
285 286
287 - def _read_a_mprofiles(self, strLine, strLine2 ):
288 """ 289 Read one layer from the staj file: rho, rho_M d roughness mu 290 """ 291 _strL = strLine.split() 292 _mrho = _str2Float( _strL[0] ) * self.scale/Pi16 293 _mdepth = _str2Float( _strL[1] ) 294 _mrough = _str2Float( _strL[2] ) / RoughFactor 295 296 _strL = strLine2.split() 297 _mtheta = _str2Float( _strL[0] ) 298 299 return [ _mrho, _mdepth, _mrough, _mtheta ]
300 301 302
303 - def build(self):
304 """Interpret all the lines of the staj file 305 """ 306 n = 0 307 self._read_Metadata1( self.lines[ n ] ) 308 309 n += 1 310 self._read_Metadata2( self.lines[ n ] ) 311 312 n += 1 313 self._read_n_layers( self.lines[n] ) 314 315 316 self.QRange=[] 317 for i in xrange(4): 318 n += 1 319 self.QRange.append( self._read_Infos( self.lines[n] ) ) 320 321 self._type = self.lines[n+1].strip() 322 self._suffix = self.lines[n+2].strip() 323 self._outfile = self.lines[n+3].strip() 324 325 n += 5 326 self.layers = [ ] 327 for i in xrange( self._nLayer+1 ) : 328 (_rho, _depth, _rough, _mu) = \ 329 self._read_a_profile( self.lines[ n ] ) 330 ( _mrho, _mdepth, _mrough, _mtheta ) = \ 331 self._read_a_mprofiles( self.lines[n+1], self.lines[n+2] ) 332 333 n += 3 334 self.layers.append( [ _rho, _depth, _rough, _mu, 335 _mrho, _mdepth, _mrough, _mtheta ] ) 336 337 [ self.rho, self.phi, self.depth,self.depthm, 338 self.rough,self.roughm, self.mu, self.theta ] = \ 339 parseMLayers( self.layers )
340 341 342
343 - def _toModel(self):
344 """Build model from information loaded from staj; must call 345 self.build() first. 346 """ 347 _metadata = [ self._WaveLength, self._WaveLengthDiv, self._AngularDiv, 348 self._Intensity, self._Background, self._Aguide 349 ] 350 _layers = [ self.rho, self.phi, self.depth,self.depthm, 351 self.rough,self.roughm, self.mu, self.theta 352 ] 353 ret = [ ] 354 ret.append( _metadata ) 355 ret.append( _layers ) 356 ret.append( self.QRange ) 357 ret.append( [ self._nLayer, self._nRough, self._nFit ] ) 358 ret.append( [ self._type, self._suffix, self._outfile ] ) 359 360 return ret
361 362 363 ############################ TEST CODE ################################
364 -def demo1():
365 Filename = "xrayl523c6A15.staj" 366 s2m = Staj2Model( Filename ) 367 i = 4 368 369 listL = s2m._toModel()[0] 370 for x in xrange( len(listL) ): 371 print listL[x][0],'\t',listL[x][2],'\t',listL[x][3],'\t',listL[x][4]
372 373
374 -def demo2():
375 Filename = "n101G.sta" 376 s2m = Staj2MModel( Filename ) 377 378 print "Qmin Qmax nQ" 379 for i in xrange( len(s2m.QRange) ): 380 print "%.4e %.4e %d"%(s2m.QRange[i][0], 381 s2m.QRange[i][1], 382 s2m.QRange[i][2]) 383 384 print "-----------------------------------------------" 385 print "QC^2 depth rough mu Qm^2 theta_M" 386 for i in xrange( len(s2m.layers) ): 387 print "%.3e %.3e %.3e %.3e %.3e %.1f"%( 388 s2m.layers[i][0]*(16*numpy.pi), 389 s2m.layers[i][1], 390 s2m.layers[i][2], 391 s2m.layers[i][3], 392 s2m.layers[i][4]*(16*numpy.pi), 393 s2m.layers[i][7] 394 )
395 396 397 if __name__ == '__main__': 398 demo1() 399