Changeset 46


Ignore:
Timestamp:
Mar 25, 2010 7:49:27 PM (15 years ago)
Author:
vondreel
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/GSASIIcomp.py

    r39 r46  
    1515else:
    1616    bindir = 'bin'
     17
    1718if ospath.exists(ospath.join(sys.path[0],bindir)) and ospath.join(sys.path[0],bindir) not in sys.path:
    1819    sys.path.insert(0,ospath.join(sys.path[0],bindir))
     20
    1921try:
    2022    import pypowder as pyp
     
    3032    exit()
    3133import fitellipse as fte
     34import GSASIIplot as G2plt
    3235
    3336# trig functions in degrees
     
    3639tand = lambda x: math.tan(x*math.pi/180.)
    3740atand = lambda x: 180.*math.atan(x)/math.pi
     41atan2d = lambda y,x: 180.*math.atan2(y,x)/math.pi
    3842cosd = lambda x: math.cos(x*math.pi/180.)
    3943acosd = lambda x: 180.*math.acos(x)/math.pi
     
    114118    if background[1]:
    115119        Bv = B
    116     x,y,w,yc,yb,yd = data
     120    x,y,w,yc,yb,yd = data               #these are numpy arrays!
    117121    V = []
    118122    A = []
     
    496500    A = nl.inv(B)
    497501    return A,B
    498                
     502   
     503def 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                   
    499510def MaxIndex(dmin,A):
    500511    #finds maximum allowed hkl for given A within dmin
     
    12911302   
    12921303def 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   
     1313def GetEllipse(dsp,data):
     1314    dist = data['distance']
     1315    cent = data['center']
     1316    tilt = data['tilt']
     1317    phi = data['rotation']
    13001318    radii = [0,0]
    1301     tth = 2.0*asind(wave/(2.*dsp))
     1319    tth = 2.0*asind(data['wavelength']/(2.*dsp))
    13021320    ttth = tand(tth)
    13031321    stth = sind(tth)
     
    13071325    sinp = sind(phi)
    13081326    cosp = cosd(phi)
    1309     radius = dist*tand(tth)
     1327    radius = dist*ttth
    13101328    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   
     1337def 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   
     1353def GetTth(x,y,data):
     1354    return GetTthDspAzm(x,y,data)[0]
     1355   
     1356def GetDsp(x,y,data):
     1357    return GetTthDspAzm(x,y,data)[1]
     1358       
     1359def GetDetector(data):
     1360    return
     1361
    13161362def ImageCompress(image,scale):
    13171363    if scale == 1:
     
    13461392    if Ring:
    13471393        ellipse = FitEllipse(Ring)
     1394        Ring = makeRing(ellipse,20,cutoff,scalex,scaley,self.ImageZ)    #do again
     1395        ellipse = FitEllipse(Ring)
    13481396    else:
    13491397        print '1st ring not sufficiently complete to proceed'
     
    13511399    print 'inner ring:',ellipse
    13521400    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)
    13551403   
    13561404    #setup for calibration
     
    13781426    cent1 = []
    13791427    cent2 = []
    1380     Zsign = 1.
    13811428    xSum = 0
    13821429    ySum = 0
     1430    zxSum = 0
     1431    zySum = 0
    13831432    phiSum = 0
    13841433    tiltSum = 0
     
    13961445        radii[0] = math.sqrt(radii[1]*radius*cosB)
    13971446        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)
    14011449        cent = data['center']
    1402         elcent = [cent[0]+zdis*sinp,cent[1]-zdis*cosp]
     1450        elcent = [cent[0]+zsinp,cent[1]-zcosp]
    14031451        ratio = radii[1]/radii[0]
    14041452        Ring = makeRing(ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ)
    14051453        if Ring:
    14061454            numZ = len(Ring)
    1407             data['rings'].append(Ring)
     1455            data['rings'].append(np.array(Ring))
    14081456            elcent,phi,radii = ellipse = FitEllipse(Ring)
    14091457            if abs(phi) > 45. and phi < 0.:
     
    14201468                    return False
    14211469            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]))
    14281475            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
    14311477                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
    14341479                    data['center'] = cent1[-1]
    1435                     Zsign *= -1.
    1436 #                    Tilt *= Zsign               
    14371480                else:
    14381481                    data['center'] = cent2[-1]
    1439                     Zsign = 1.
    14401482                Zsum += numZ
    14411483                phiSum += numZ*phi
     
    14431485                xSum += numZ*data['center'][0]
    14441486                ySum += numZ*data['center'][1]
    1445                 tiltSum += numZ*Tilt
     1487                tiltSum += numZ*abs(Tilt)
    14461488            cent = data['center']
    14471489            print 'for ring # %2i dist %.3f rotate %6.2f tilt %6.2f Xcent %.3f Ycent %.3f Npts %d' \
    14481490                %(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)
    14511493        else:
    14521494            break
     
    14541496    if 2*radii[1] < .9*fullSize:
    14551497        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:
    14721499        print 'Only one ring fitted. Check your wavelength.'
    14731500        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)
    14751529       
    14761530    return True
Note: See TracChangeset for help on using the changeset viewer.