Beispiel #1
0
 def calc_snr(self):
     """ Calculates the signal to noise ratio of the spectra """
     b, a = scipy.signal.butter(self.pars.butter_order,
                                self.pars.butter_cut)
     smoothed = scipy.signal.filtfilt(b, a, self.signal['flux'])
     noise = tools.fit_rms(smoothed, self.signal['flux'])
     signal = tools.rms(self.signal['flux'])
     self.snr = signal / noise
Beispiel #2
0
	def zernikesurface(self, label = True, zlim=[], matrix = False):
		"""
		------------------------------------------------
		zernikesurface(self, label_1 = True):

		Return a 3D Zernike Polynomials surface figure

		label_1: default show label

		------------------------------------------------
		"""
		theta = __np__.linspace(0, 2*__np__.pi, 100)
		rho = __np__.linspace(0, 1, 100)
		[u,r] = __np__.meshgrid(theta,rho)
		X = r*__cos__(u)
		Y = r*__sin__(u)
		Z = __zernikepolar__(self.__coefficients__,r,u)
		fig = __plt__.figure(figsize=(12, 8), dpi=80)
		ax = fig.gca(projection='3d')
		surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=__cm__.RdYlGn,
	        linewidth=0, antialiased=False, alpha = 0.6)
		
		if zlim == []:
			v = max(abs(Z.max()),abs(Z.min()))
			ax.set_zlim(-v*5, v*5)
			cset = ax.contourf(X, Y, Z, zdir='z', offset=-v*5, cmap=__cm__.RdYlGn)
		else:
			ax.set_zlim(zlim[0], zlim[1])
			cset = ax.contourf(X, Y, Z, zdir='z', offset=zlim[0], cmap=__cm__.RdYlGn)

		ax.zaxis.set_major_locator(__LinearLocator__(10))
		ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f'))
		fig.colorbar(surf, shrink=1, aspect=30)


		p2v = round(__tools__.peak2valley(Z),5)
		rms1 = round(__tools__.rms(Z),5)

		label_1 = self.listcoefficient()[0]+"P-V: "+str(p2v)+"\n"+"RMS: "+str(rms1)
		if label == True:
			__plt__.title('Zernike Polynomials Surface',fontsize=18)
			ax.text2D(0.02, 0.1, label_1, transform=ax.transAxes,fontsize=14)
		else:
			pass
		__plt__.show()
		
		if matrix == True:
			return Z
		else:
			pass
Beispiel #3
0
def fitting(Z,n,remain3D=False,remain2D=False,barchart=False,interferogram=False,removepiston=True):
	"""
	------------------------------------------------
	fitting(Z,n)

	Fitting an aberration to several orthonormal Zernike
	polynomials.

	Return: n-th Zernike coefficients for a fitting surface aberration
			Zernike coefficients barchart
			Remaining aberration
			Fiting surface plot
	Input: 
	Z: A surface or aberration matrix measure from inteferometer
	   or something else.

	n: How many order of Zernike Polynomials you want to fit

	reamin(default==Flase): show the surface after remove fitting
	aberrations.

	removepiston: if remove piston, default = True
	------------------------------------------------
	"""


	fitlist = []
	l = len(Z)
	x2 = __np__.linspace(-1, 1, l)
	y2 = __np__.linspace(-1, 1, l)
	[X2,Y2] = __np__.meshgrid(x2,y2)
	r = __np__.sqrt(X2**2 + Y2**2)
	u = __np__.arctan2(Y2, X2)
	for i in range(n):
		C = [0]*i+[1]+[0]*(37-i-1)
		ZF = __zernikepolar__(C,r,u)
		for i in range(l):
			for j in range(l):
				if x2[i]**2+y2[j]**2>1:
					ZF[i][j]=0
		a = sum(sum(Z*ZF))*2*2/l/l/__np__.pi
		fitlist.append(round(a,3))


	l1 = len(fitlist)
	fitlist = fitlist+[0]*(37-l1)
	Z_new = Z - __zernikepolar__(fitlist,r,u)
	for i in range(l):
		for j in range(l):
			if x2[i]**2+y2[j]**2>1:
				Z_new[i][j]=0

	#plot bar chart of zernike
	if barchart == True:
		fitlist1 = fitlist[0:n]
		index = __np__.arange(n)
		fig = __plt__.figure(figsize=(9, 6), dpi=80)
		xticklist = []
		width = 0.6
		for i in index:
			xticklist.append('Z'+str(i+1))
		barfigure = __plt__.bar(index, fitlist1, width,color = '#2E9AFE',edgecolor = '#2E9AFE')
		__plt__.xticks( index+width/2, xticklist )
		__plt__.xlabel('Zernike Polynomials',fontsize=18)  
		__plt__.ylabel('Coefficient',fontsize=18)  
		__plt__.title('Fitting Zernike Polynomials Coefficient',fontsize=18)  

		__plt__.show()  
	else:
		pass


	if remain3D == True:
		
		fig = __plt__.figure(figsize=(12, 8), dpi=80)
		ax = fig.gca(projection='3d')
		surf = ax.plot_surface(X2, Y2, Z_new, rstride=1, cstride=1, cmap=__cm__.RdYlGn,
	        linewidth=0, antialiased=False, alpha = 0.6)
		v = max(abs(Z.max()),abs(Z.min()))
		ax.set_zlim(-v, v)
		ax.zaxis.set_major_locator(__LinearLocator__(10))
		ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f'))
		cset = ax.contourf(X2, Y2, Z_new, zdir='z', offset=-v, cmap=__cm__.RdYlGn)
		fig.colorbar(surf, shrink=1, aspect=30)
		__plt__.title('Remaining Aberration',fontsize=18)
		p2v = round(__tools__.peak2valley(Z_new),5)
		rms1 = round(__tools__.rms(Z_new),5)
		label_new = "P-V: "+str(p2v)+"\n"+"RMS: "+str(rms1)
		ax.text2D(0.02, 0.1,label_new, transform=ax.transAxes)
		__plt__.show()		
	else:
		pass

	if remain2D == True:
		fig = __plt__.figure(figsize=(9, 6), dpi=80)
		ax = fig.gca()
		im = __plt__.pcolormesh(X2, Y2, Z_new, cmap=__cm__.RdYlGn)
		__plt__.colorbar()
		__plt__.title('Remaining Aberration',fontsize=18)
		ax.set_aspect('equal', 'datalim')
		__plt__.show()
	else:
		pass

	if interferogram == True:
		zernike_coefficient = Coefficient(fitlist)
		__interferometer__.twyman_green(zernike_coefficient)
	else:
		pass
	if removepiston == True:
		fitlist[0] = 0
	else:
		pass
	C = Coefficient(fitlist)  #output zernike Coefficient class
	__tools__.zernikeprint(fitlist)
	return fitlist,C
Beispiel #4
0
    def zernikesurface(self, label=True, zlim=[], matrix=False):
        """
		------------------------------------------------
		zernikesurface(self, label_1 = True):

		Return a 3D Zernike Polynomials surface figure

		label_1: default show label

		------------------------------------------------
		"""
        theta = __np__.linspace(0, 2 * __np__.pi, 100)
        rho = __np__.linspace(0, 1, 100)
        [u, r] = __np__.meshgrid(theta, rho)
        X = r * __cos__(u)
        Y = r * __sin__(u)
        Z = __zernikepolar__(self.__coefficients__, r, u)
        fig = __plt__.figure(figsize=(12, 8), dpi=80)
        ax = fig.gca(projection='3d')
        surf = ax.plot_surface(X,
                               Y,
                               Z,
                               rstride=1,
                               cstride=1,
                               cmap=__cm__.RdYlGn,
                               linewidth=0,
                               antialiased=False,
                               alpha=0.6)

        if zlim == []:
            v = max(abs(Z.max()), abs(Z.min()))
            ax.set_zlim(-v * 5, v * 5)
            cset = ax.contourf(X,
                               Y,
                               Z,
                               zdir='z',
                               offset=-v * 5,
                               cmap=__cm__.RdYlGn)
        else:
            ax.set_zlim(zlim[0], zlim[1])
            cset = ax.contourf(X,
                               Y,
                               Z,
                               zdir='z',
                               offset=zlim[0],
                               cmap=__cm__.RdYlGn)

        ax.zaxis.set_major_locator(__LinearLocator__(10))
        ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f'))
        fig.colorbar(surf, shrink=1, aspect=30)

        p2v = round(__tools__.peak2valley(Z), 5)
        rms1 = round(__tools__.rms(Z), 5)

        label_1 = self.listcoefficient()[0] + "P-V: " + str(
            p2v) + "\n" + "RMS: " + str(rms1)
        if label == True:
            __plt__.title('Zernike Polynomials Surface', fontsize=18)
            ax.text2D(0.02, 0.1, label_1, transform=ax.transAxes, fontsize=14)
        else:
            pass
        __plt__.show()

        if matrix == True:
            return Z
        else:
            pass
Beispiel #5
0
def fitting(Z,
            n,
            remain3D=False,
            remain2D=False,
            barchart=False,
            interferogram=False,
            removepiston=True):
    """
	------------------------------------------------
	fitting(Z,n)

	Fitting an aberration to several orthonormal Zernike
	polynomials.

	Return: n-th Zernike coefficients for a fitting surface aberration
			Zernike coefficients barchart
			Remaining aberration
			Fiting surface plot
	Input: 
	Z: A surface or aberration matrix measure from inteferometer
	   or something else.

	n: How many order of Zernike Polynomials you want to fit

	reamin(default==Flase): show the surface after remove fitting
	aberrations.

	removepiston: if remove piston, default = True
	------------------------------------------------
	"""

    fitlist = []
    l = len(Z)
    x2 = __np__.linspace(-1, 1, l)
    y2 = __np__.linspace(-1, 1, l)
    [X2, Y2] = __np__.meshgrid(x2, y2)
    r = __np__.sqrt(X2**2 + Y2**2)
    u = __np__.arctan2(Y2, X2)
    for i in range(n):
        C = [0] * i + [1] + [0] * (37 - i - 1)
        ZF = __zernikepolar__(C, r, u)
        for i in range(l):
            for j in range(l):
                if x2[i]**2 + y2[j]**2 > 1:
                    ZF[i][j] = 0
        a = sum(sum(Z * ZF)) * 2 * 2 / l / l / __np__.pi
        fitlist.append(round(a, 3))

    l1 = len(fitlist)
    fitlist = fitlist + [0] * (37 - l1)
    Z_new = Z - __zernikepolar__(fitlist, r, u)
    for i in range(l):
        for j in range(l):
            if x2[i]**2 + y2[j]**2 > 1:
                Z_new[i][j] = 0

    #plot bar chart of zernike
    if barchart == True:
        fitlist1 = fitlist[0:n]
        index = __np__.arange(n)
        fig = __plt__.figure(figsize=(9, 6), dpi=80)
        xticklist = []
        width = 0.6
        for i in index:
            xticklist.append('Z' + str(i + 1))
        barfigure = __plt__.bar(index,
                                fitlist1,
                                width,
                                color='#2E9AFE',
                                edgecolor='#2E9AFE')
        __plt__.xticks(index + width / 2, xticklist)
        __plt__.xlabel('Zernike Polynomials', fontsize=18)
        __plt__.ylabel('Coefficient', fontsize=18)
        __plt__.title('Fitting Zernike Polynomials Coefficient', fontsize=18)

        __plt__.show()
    else:
        pass

    if remain3D == True:

        fig = __plt__.figure(figsize=(12, 8), dpi=80)
        ax = fig.gca(projection='3d')
        surf = ax.plot_surface(X2,
                               Y2,
                               Z_new,
                               rstride=1,
                               cstride=1,
                               cmap=__cm__.RdYlGn,
                               linewidth=0,
                               antialiased=False,
                               alpha=0.6)
        v = max(abs(Z.max()), abs(Z.min()))
        ax.set_zlim(-v, v)
        ax.zaxis.set_major_locator(__LinearLocator__(10))
        ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f'))
        cset = ax.contourf(X2,
                           Y2,
                           Z_new,
                           zdir='z',
                           offset=-v,
                           cmap=__cm__.RdYlGn)
        fig.colorbar(surf, shrink=1, aspect=30)
        __plt__.title('Remaining Aberration', fontsize=18)
        p2v = round(__tools__.peak2valley(Z_new), 5)
        rms1 = round(__tools__.rms(Z_new), 5)
        label_new = "P-V: " + str(p2v) + "\n" + "RMS: " + str(rms1)
        ax.text2D(0.02, 0.1, label_new, transform=ax.transAxes)
        __plt__.show()
    else:
        pass

    if remain2D == True:
        fig = __plt__.figure(figsize=(9, 6), dpi=80)
        ax = fig.gca()
        im = __plt__.pcolormesh(X2, Y2, Z_new, cmap=__cm__.RdYlGn)
        __plt__.colorbar()
        __plt__.title('Remaining Aberration', fontsize=18)
        ax.set_aspect('equal', 'datalim')
        __plt__.show()
    else:
        pass

    if interferogram == True:
        zernike_coefficient = Coefficient(fitlist)
        __interferometer__.twyman_green(zernike_coefficient)
    else:
        pass
    if removepiston == True:
        fitlist[0] = 0
    else:
        pass
    C = Coefficient(fitlist)  #output zernike Coefficient class
    __tools__.zernikeprint(fitlist)
    return fitlist, C