def __init__(self, axis=None, angle=None, R=None): if isinstance(R, dnp.ndarray): if R.shape != (3, 3): raise Exception('R must be a 3 by 3 matrix') self.R = R angle = dnp.arccos(0.5*(sum(dnp.diag(R))-1)) self.angle = float(dnp.rad2deg(angle)) x = (R[2][1]-R[1][2])/(2*dnp.sin(angle)) y = (R[0][2]-R[2][0])/(2*dnp.sin(angle)) z = (R[1][0]-R[0][1])/(2*dnp.sin(angle)) axis = Vector(x,y,z) axis = axis.unit() self.axis = axis return if isinstance(angle, dnp.ndarray): angle = angle[0] elif isinstance(angle, int): angle = float(angle) if not isinstance(angle, float): raise Exception('angle must be a float not {}'.format(type(angle))) axis = axis.unit() self.axis = axis self.angle = angle angle = float(dnp.radians(angle)) M = dnp.array([[0.0, -axis.z, axis.y], [axis.z, 0.0, -axis.x], [-axis.y, axis.x, 0.0]]) N = axis.tensor_product(axis) I = dnp.identity(3) self.R = I*dnp.cos(angle) + M*dnp.sin(angle) + (1-dnp.cos(angle))*N angle = 56
def dofourier(xarray, yarray, printres=False): nvals = len(yarray) avht = yarray.mean() angles = dnp.linspace(0, 2 * dnp.pi, len(yarray)) # print angles ycarray = yarray - avht cosines = dnp.cos(angles) sines = dnp.sin(angles) cosint = cosines * ycarray sinint = sines * ycarray if printres: print "Yarray:", yarray print "Ycarray:", ycarray print "Sinint:", sinint print "Cosint:", cosint cosum = cosint[:-1].sum() sinsum = sinint[:-1].sum() cosfactor = cosum / float(nvals - 1) sinfactor = sinsum / float(nvals - 1) if printres: print "sinfactor, cosfactor: %.4f %.4f" % (sinfactor, cosfactor) ratio = sinfactor / cosfactor delta = math.atan(ratio) deltadeg = math.degrees(delta) if printres: print "Delta degrees %06.4f" % deltadeg halfmag = math.sqrt(sinfactor ** 2 + cosfactor ** 2) mag = 2.0 * halfmag if printres: print "Magnitude: %06.4f" % mag print "vertical centre %7.4f" % avht calcresult = avht + mag * dnp.cos(angles - delta) if printres: print "Calcresult:", calcresult for idx in range(0, len(yarray)): print yarray[idx], calcresult[idx] return (avht, mag, delta, calcresult)
def dofourier(xarray,yarray,printres=False): nvals=len(yarray) avht=yarray.mean() angles=dnp.linspace(0,2*dnp.pi,len(yarray)) #print angles ycarray=yarray-avht cosines=dnp.cos(angles) sines=dnp.sin(angles) cosint=cosines*ycarray sinint=sines*ycarray if printres: print "Yarray:",yarray print "Ycarray:",ycarray print "Sinint:",sinint print "Cosint:",cosint cosum=cosint[:-1].sum() sinsum=sinint[:-1].sum() cosfactor=cosum/float(nvals-1) sinfactor=sinsum/float(nvals-1) if printres: print "sinfactor, cosfactor: %.4f %.4f"%(sinfactor,cosfactor) ratio=sinfactor/cosfactor delta=math.atan(ratio) deltadeg=math.degrees(delta) if printres: print"Delta degrees %06.4f"%deltadeg halfmag=math.sqrt(sinfactor**2+cosfactor**2) mag=2.0*halfmag if printres: print"Magnitude: %06.4f"%mag print "vertical centre %7.4f"%avht calcresult=avht+mag*dnp.cos(angles-delta) if printres: print"Calcresult:",calcresult for idx in range(0,len(yarray)): print yarray[idx],calcresult[idx] return(avht,mag,delta,calcresult)
def readTestInputs( filename="height.txt", idealname="ideal.txt", visit_directory="/dls/i12/data/2014/cm4963-2", input_directory="tmp", printres=False, ): infilename = "%s/%s/%s" % (visit_directory, input_directory, filename) idealfilename = "%s/%s/%s" % (visit_directory, input_directory, idealname) inlist = [] ideallist = [] try: infile = open(infilename, "r") except: print ("File %s not opened properly" % infilename) return None n = 0 for line in infile: if printres: print n, line n = n + 1 inlist.append(float(line)) infile.close() try: infile = open(idealfilename, "r") except: print ("File %s not opened properly" % idealfilename) return None for line in infile: if printres: print n, line n = n + 1 ideallist.append(float(line)) infile.close() if printres: print inlist print ideallist inarray = dnp.array(inlist) idealarray = dnp.array(ideallist) print len(inarray) angarray = dnp.linspace(0, dnp.pi * 2.0, len(inarray), True) horizarray = 2500.0 * dnp.cos(angarray) ofile = open("%s/%s/testpoints.txt" % (visit_directory, input_directory), "w") ofile.write("angle,X,idealY,noisyY\n") for i in range(0, len(angarray)): ofile.write("%g,%g,%g,%g\n" % (angarray[i], horizarray[i], idealarray[i], inarray[i])) ofile.close() return (horizarray, inarray, idealarray)
def readTestInputs(filename='height.txt',idealname='ideal.txt',visit_directory='/dls/i12/data/2014/cm4963-2',input_directory='tmp',printres=False): infilename="%s/%s/%s"%(visit_directory,input_directory,filename) idealfilename="%s/%s/%s"%(visit_directory,input_directory,idealname) inlist=[] ideallist=[] try: infile=open(infilename,'r') except: print("File %s not opened properly"%infilename) return(None) n=0 for line in infile: if printres: print n,line n=n+1 inlist.append(float(line)) infile.close() try: infile=open(idealfilename,'r') except: print("File %s not opened properly"%idealfilename) return(None) for line in infile: if printres: print n,line n=n+1 ideallist.append(float(line)) infile.close() if printres: print inlist print ideallist inarray=dnp.array(inlist) idealarray=dnp.array(ideallist) print len(inarray) angarray=((dnp.linspace(0,dnp.pi*2.0,len(inarray),True))) horizarray=2500.0*dnp.cos(angarray) ofile=open("%s/%s/testpoints.txt"%(visit_directory,input_directory),'w') ofile.write("angle,X,idealY,noisyY\n") for i in range(0,len(angarray)): ofile.write("%g,%g,%g,%g\n"%(angarray[i],horizarray[i],idealarray[i],inarray[i])) ofile.close() return(horizarray,inarray,idealarray)
def python_cos(ds): '''Performs a cos() on ds input''' return dnp.cos(ds)
def project(self, energy, UB, pixels, gamma, delta, omega, alpha, nu): # put the detector at the right position dx, dy, dz = pixels # convert angles to radians gamma, delta, alpha, omega, nu = numpy.radians( (gamma, delta, alpha, omega, nu)) RGam = numpy.matrix([[1, 0, 0], [0, cos(gamma), -sin(gamma)], [0, sin(gamma), cos(gamma)]]) RDel = (numpy.matrix([[cos(delta), -sin(delta), 0], [sin(delta), cos(delta), 0], [0, 0, 1]])).getI() RNu = numpy.matrix([[cos(nu), 0, sin(nu)], [0, 1, 0], [-sin(nu), 0, cos(nu)]]) # calculate Cartesian coordinates for each pixel using clever matrix stuff M = numpy.mat( numpy.concatenate( (dx.flatten(0), dy.flatten(0), dz.flatten(0))).reshape(3, dx.shape[0] * dx.shape[1])) XYZp = RGam * RDel * RNu * M xp = dnp.array(XYZp[0]).reshape(dx.shape) yp = dnp.array(XYZp[1]).reshape(dy.shape) zp = dnp.array(XYZp[2]).reshape(dz.shape) # don't bother with the part about slits... # Calculate effective gamma and delta for each pixel d_ds = dnp.sqrt(xp**2 + yp**2 + zp**2) Gam = dnp.arctan2(zp, yp) Del = -1 * dnp.arcsin(-xp / d_ds) # wavenumber k = 2 * math.pi / 12.398 * energy # Define the needed matrices. The notation follows the article by Bunk & # Nielsen. J.Appl.Cryst. (2004) 37, 216-222. M1 = k * numpy.matrix( cos(omega) * sin(Del) - sin(omega) * (cos(alpha) * (cos(Gam) * cos(Del) - 1) + sin(alpha) * sin(Gam) * cos(Del))) M2 = k * numpy.matrix( sin(omega) * sin(Del) + cos(omega) * (cos(alpha) * (cos(Gam) * cos(Del) - 1) + sin(alpha) * sin(Gam) * cos(Del))) M3 = k * numpy.matrix(-sin(alpha) * (cos(Gam) * cos(Del) - 1) + cos(alpha) * sin(Gam) * cos(Del)) # invert UB matrix UBi = numpy.matrix(UB).getI() # calculate HKL H = UBi[0, 0] * M1 + UBi[0, 1] * M2 + UBi[0, 2] * M3 K = UBi[1, 0] * M1 + UBi[1, 1] * M2 + UBi[1, 2] * M3 L = UBi[2, 0] * M1 + UBi[2, 1] * M2 + UBi[2, 2] * M3 return (H, K, L)
def project(self, energy, UB, pixels, gamma, delta, omega, alpha, nu): # put the detector at the right position dx,dy,dz = pixels # convert angles to radians gamma, delta, alpha, omega, nu = numpy.radians((gamma, delta, alpha, omega, nu)) RGam = numpy.matrix([[1,0,0],[0,cos(gamma),-sin(gamma)],[0,sin(gamma),cos(gamma)]]) RDel = (numpy.matrix([[cos(delta),-sin(delta),0],[sin(delta),cos(delta),0],[0,0,1]])).getI() RNu = numpy.matrix([[cos(nu),0,sin(nu)],[0,1,0],[-sin(nu),0,cos(nu)]]) # calculate Cartesian coordinates for each pixel using clever matrix stuff M = numpy.mat(numpy.concatenate((dx.flatten(0), dy.flatten(0), dz.flatten(0))).reshape(3,dx.shape[0]*dx.shape[1])) XYZp = RGam * RDel * RNu * M xp = dnp.array(XYZp[0]).reshape(dx.shape) yp = dnp.array(XYZp[1]).reshape(dy.shape) zp = dnp.array(XYZp[2]).reshape(dz.shape) # don't bother with the part about slits... # Calculate effective gamma and delta for each pixel d_ds = dnp.sqrt(xp**2 + yp**2 + zp**2) Gam = dnp.arctan2(zp, yp) Del = -1 * dnp.arcsin(-xp/d_ds) # wavenumber k = 2 * math.pi / 12.398 * energy # Define the needed matrices. The notation follows the article by Bunk & # Nielsen. J.Appl.Cryst. (2004) 37, 216-222. M1 = k * numpy.matrix(cos(omega) * sin(Del) - sin(omega) * (cos(alpha) * (cos(Gam) * cos(Del)-1) + sin(alpha) * sin(Gam) * cos(Del))) M2 = k * numpy.matrix(sin(omega) * sin(Del) + cos(omega) * (cos(alpha) * (cos(Gam) * cos(Del)-1) + sin(alpha) * sin(Gam) * cos(Del))) M3 = k * numpy.matrix(-sin(alpha) * (cos(Gam) * cos(Del)-1) + cos(alpha) * sin(Gam) * cos(Del)) # invert UB matrix UBi = numpy.matrix(UB).getI() # calculate HKL H = UBi[0,0]*M1 + UBi[0,1]*M2 + UBi[0,2]*M3 K = UBi[1,0]*M1 + UBi[1,1]*M2 + UBi[1,2]*M3 L = UBi[2,0]*M1 + UBi[2,1]*M2 + UBi[2,2]*M3 return (H, K, L)