Changeset 46
- Timestamp:
- Mar 25, 2010 7:49:27 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/GSASIIcomp.py ¶
r39 r46 15 15 else: 16 16 bindir = 'bin' 17 17 18 if ospath.exists(ospath.join(sys.path[0],bindir)) and ospath.join(sys.path[0],bindir) not in sys.path: 18 19 sys.path.insert(0,ospath.join(sys.path[0],bindir)) 20 19 21 try: 20 22 import pypowder as pyp … … 30 32 exit() 31 33 import fitellipse as fte 34 import GSASIIplot as G2plt 32 35 33 36 # trig functions in degrees … … 36 39 tand = lambda x: math.tan(x*math.pi/180.) 37 40 atand = lambda x: 180.*math.atan(x)/math.pi 41 atan2d = lambda y,x: 180.*math.atan2(y,x)/math.pi 38 42 cosd = lambda x: math.cos(x*math.pi/180.) 39 43 acosd = lambda x: 180.*math.acos(x)/math.pi … … 114 118 if background[1]: 115 119 Bv = B 116 x,y,w,yc,yb,yd = data 120 x,y,w,yc,yb,yd = data #these are numpy arrays! 117 121 V = [] 118 122 A = [] … … 496 500 A = nl.inv(B) 497 501 return A,B 498 502 503 def makeMat(Angle,Axis): 504 #Make rotation matrix from Angle in degrees,Axis =0 for rotation about x, =1 for about y, etc. 505 cs = cosd(Angle) 506 ss = sind(Angle) 507 M = np.array(([1,0,0],[0,cs,-ss],[0,ss,cs])) 508 return np.roll(np.roll(M,Axis,axis=0),Axis,axis=1) 509 499 510 def MaxIndex(dmin,A): 500 511 #finds maximum allowed hkl for given A within dmin … … 1291 1302 1292 1303 def calcZdisCosB(radius,tth,radii): 1293 sinb = cosB = min(1.0,radii[0]**2/(radius*radii[1])) 1294 cosb = math.sqrt(1.-sinb**2) 1295 ttth = tand(tth) 1296 zdis = radii[1]*ttth*cosb/sinb 1297 return zdis,cosB 1298 1299 def GetEllipse(dsp,wave,dist,cent,tilt,phi): 1304 cosB = sinb = radii[0]**2/(radius*radii[1]) 1305 if cosB > 1.: 1306 return 0.,1. 1307 else: 1308 cosb = math.sqrt(1.-sinb**2) 1309 ttth = tand(tth) 1310 zdis = radii[1]*ttth*cosb/sinb 1311 return zdis,cosB 1312 1313 def GetEllipse(dsp,data): 1314 dist = data['distance'] 1315 cent = data['center'] 1316 tilt = data['tilt'] 1317 phi = data['rotation'] 1300 1318 radii = [0,0] 1301 tth = 2.0*asind( wave/(2.*dsp))1319 tth = 2.0*asind(data['wavelength']/(2.*dsp)) 1302 1320 ttth = tand(tth) 1303 1321 stth = sind(tth) … … 1307 1325 sinp = sind(phi) 1308 1326 cosp = cosd(phi) 1309 radius = dist*t and(tth)1327 radius = dist*ttth 1310 1328 radii[1] = dist*stth*ctth*sinb/(sinb**2-stth**2) 1311 radii[0] = math.sqrt(radii[1]*radius*sinb) 1312 zdis = radii[1]*ttth*cosb/sinb 1313 elcent = [cent[0]-zdis*sinp,cent[1]+zdis*cosp] 1314 return elcent,phi,radii 1315 1329 if radii[1] > 0: 1330 radii[0] = math.sqrt(radii[1]*radius*sinb) 1331 zdis = radii[1]*ttth*cosb/sinb 1332 elcent = [cent[0]-zdis*sinp,cent[1]+zdis*cosp] 1333 return elcent,phi,radii 1334 else: 1335 return False 1336 1337 def GetTthDspAzm(x,y,data): 1338 wave = data['wavelength'] 1339 dist = data['distance'] 1340 cent = data['center'] 1341 tilt = data['tilt'] 1342 phi = data['rotation'] 1343 dx = x-cent[0] 1344 dy = y-cent[1] 1345 X = np.array(([dx,dy,0])) 1346 X = np.sum(X*makeMat(-phi,2),axis=1) 1347 X = np.sum(X*makeMat(-tilt,0),axis=1) 1348 tth = atand(math.sqrt(dx**2+dy**2-X[2]**2)/(dist-X[2])) 1349 dsp = wave/(2.*sind(tth/2.)) 1350 azm = atan2d(dy,dx) 1351 return tth,dsp,azm 1352 1353 def GetTth(x,y,data): 1354 return GetTthDspAzm(x,y,data)[0] 1355 1356 def GetDsp(x,y,data): 1357 return GetTthDspAzm(x,y,data)[1] 1358 1359 def GetDetector(data): 1360 return 1361 1316 1362 def ImageCompress(image,scale): 1317 1363 if scale == 1: … … 1346 1392 if Ring: 1347 1393 ellipse = FitEllipse(Ring) 1394 Ring = makeRing(ellipse,20,cutoff,scalex,scaley,self.ImageZ) #do again 1395 ellipse = FitEllipse(Ring) 1348 1396 else: 1349 1397 print '1st ring not sufficiently complete to proceed' … … 1351 1399 print 'inner ring:',ellipse 1352 1400 data['center'] = copy.copy(ellipse[0]) #not right!! (but useful for now) 1353 data['ellipses'].append(ellipse[:] )1354 self.PlotImage()1401 data['ellipses'].append(ellipse[:]+('r',)) 1402 G2plt.PlotImage(self) 1355 1403 1356 1404 #setup for calibration … … 1378 1426 cent1 = [] 1379 1427 cent2 = [] 1380 Zsign = 1.1381 1428 xSum = 0 1382 1429 ySum = 0 1430 zxSum = 0 1431 zySum = 0 1383 1432 phiSum = 0 1384 1433 tiltSum = 0 … … 1396 1445 radii[0] = math.sqrt(radii[1]*radius*cosB) 1397 1446 zdis,cosB = calcZdisCosB(radius,tth,radii) 1398 zdis *= Zsign 1399 sinp = sind(phi) 1400 cosp = cosd(phi) 1447 zsinp = zdis*sind(phi) 1448 zcosp = zdis*cosd(phi) 1401 1449 cent = data['center'] 1402 elcent = [cent[0]+z dis*sinp,cent[1]-zdis*cosp]1450 elcent = [cent[0]+zsinp,cent[1]-zcosp] 1403 1451 ratio = radii[1]/radii[0] 1404 1452 Ring = makeRing(ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ) 1405 1453 if Ring: 1406 1454 numZ = len(Ring) 1407 data['rings'].append( Ring)1455 data['rings'].append(np.array(Ring)) 1408 1456 elcent,phi,radii = ellipse = FitEllipse(Ring) 1409 1457 if abs(phi) > 45. and phi < 0.: … … 1420 1468 return False 1421 1469 zdis,cosB = calcZdisCosB(radius,tth,radii) 1422 Tilt = acosd(cosB) 1423 sinp = sind(ellipse[1]) 1424 cosp = cosd(ellipse[1]) 1425 cent1.append(np.array([elcent[0]+zdis*sinp,elcent[1]-zdis*cosp])) 1426 cent2.append(np.array([elcent[0]-zdis*sinp,elcent[1]+zdis*cosp])) 1427 dist1 = dist2 = 0 1470 Tilt = acosd(cosB) # 0 <= tilt <= 90 1471 zsinp = zdis*sind(ellipse[1]) 1472 zcosp = zdis*cosd(ellipse[1]) 1473 cent1.append(np.array([elcent[0]+zsinp,elcent[1]-zcosp])) 1474 cent2.append(np.array([elcent[0]-zsinp,elcent[1]+zcosp])) 1428 1475 if i: 1429 d1 = cent1[-1]-cent1[-2] 1430 dist1 = np.dot(d1,d1) 1476 d1 = cent1[-1]-cent1[-2] #get shift of 2 possible center solutions 1431 1477 d2 = cent2[-1]-cent2[-2] 1432 dist2 = np.dot(d2,d2) 1433 if dist2 > dist1: 1478 if np.dot(d2,d2) > np.dot(d1,d1): #right solution is the larger shift 1434 1479 data['center'] = cent1[-1] 1435 Zsign *= -1.1436 # Tilt *= Zsign1437 1480 else: 1438 1481 data['center'] = cent2[-1] 1439 Zsign = 1.1440 1482 Zsum += numZ 1441 1483 phiSum += numZ*phi … … 1443 1485 xSum += numZ*data['center'][0] 1444 1486 ySum += numZ*data['center'][1] 1445 tiltSum += numZ* Tilt1487 tiltSum += numZ*abs(Tilt) 1446 1488 cent = data['center'] 1447 1489 print 'for ring # %2i dist %.3f rotate %6.2f tilt %6.2f Xcent %.3f Ycent %.3f Npts %d' \ 1448 1490 %(i,dist,phi,Tilt,cent[0],cent[1],numZ) 1449 data['ellipses'].append(copy.deepcopy(ellipse ))1450 self.PlotImage()1491 data['ellipses'].append(copy.deepcopy(ellipse+('r',))) 1492 G2plt.PlotImage(self) 1451 1493 else: 1452 1494 break … … 1454 1496 if 2*radii[1] < .9*fullSize: 1455 1497 print 'Are all usable rings (>25% visible) used? Try reducing Min ring I/Ib' 1456 if Zsum: 1457 data['center'] = [xSum/Zsum,ySum/Zsum] 1458 data['tilt'] = tiltSum/Zsum 1459 data['distance'] = distSum/Zsum 1460 data['rotation'] = phiSum/Zsum 1461 print data['center'],data['tilt'],data['distance'],data['rotation'] 1462 cent = data['center'] 1463 tilt = data['tilt'] 1464 dist = data['distance'] 1465 wave = data['wavelength'] 1466 phi = data['rotation'] 1467 N = len(data['ellipses']) 1468 for H in HKL[:N]: 1469 ellipse = GetEllipse(H[3],wave,dist,cent,tilt,phi) 1470 data['ellipses'].append(copy.deepcopy(ellipse)) 1471 else: 1498 if not Zsum: 1472 1499 print 'Only one ring fitted. Check your wavelength.' 1473 1500 return False 1474 self.PlotImage() 1501 cent = data['center'] = [xSum/Zsum,ySum/Zsum] 1502 wave = data['wavelength'] 1503 dist = data['distance'] = distSum/Zsum 1504 1505 #possible error if no. of rings < 3! Might need trap here 1506 d1 = cent1[-1]-cent1[1] #compare last ring to 2nd ring 1507 d2 = cent2[-1]-cent2[1] 1508 Zsign = 1 1509 len1 = math.sqrt(np.dot(d1,d1)) 1510 len2 = math.sqrt(np.dot(d2,d2)) 1511 t1 = d1/len1 1512 t2 = d2/len2 1513 if len2 > len1: 1514 print 'len2 > len1' 1515 if -135. < atan2d(t2[1],t2[0]) < 45.: 1516 Zsign = -1 1517 else: 1518 print 'len2 < len1' 1519 if -135. < atan2d(t1[1],t1[0]) < 45.: 1520 Zsign = -1 1521 1522 tilt = data['tilt'] = Zsign*tiltSum/Zsum 1523 phi = data['rotation'] = phiSum/Zsum 1524 N = len(data['ellipses']) 1525 for H in HKL[:N]: 1526 ellipse = GetEllipse(H[3],data) 1527 data['ellipses'].append(copy.deepcopy(ellipse+('b',))) 1528 G2plt.PlotImage(self) 1475 1529 1476 1530 return True
Note: See TracChangeset
for help on using the changeset viewer.