def Fit_IQ_Noise(freq, sweepcomplexIQ):
    """
    Fit Circle
    Rotate and move data  to the origin
    """
    n = len(freq)
    x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(sweepcomplexIQ.real, sweepcomplexIQ.imag)
    Z_ii = (x_c + 1j*y_c - sweepcomplexIQ)#*np.exp(-1j * np.arctan(y_c/x_c))
    """
    ### Fit result and data
    #Fitter.plot_data_circle(complexIQ.real, complexIQ.imag, x_c, y_c, radius)
    plt.plot(Z_ii.real, Z_ii.imag)
    plt.show()
    """
    ### Fit Phase
    data_phase = np.arctan2(Z_ii.imag, Z_ii.real)
    rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[n-1])
    Z_iir = Z_ii*np.exp(1j * rotate_angle)
    phase_rotated = np.arctan2(Z_iir.imag, Z_iir.real)
    theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5
    para_phase = theta0_guess, fr_guess, Qr_guess
    theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase)
    print fit_report_phase
#filename = '20160518_-10dBm_4.99347845_0.0025_707.804mK.csv'
#folder = '../../../MeasurementResult/20160516_Nb154nmCry3/'
#span = 2e6
#a = SimpleQ(folder, filename, span)
#b = MillionQi(folder, filename, span)
#result = Fit_SingleKID(folder, filename, span)
#result = Fit_SingleKID_Lo(folder, filename, span)
#result = Fit_7parameter(folder, filename)
#print result
def Fit_IQ_Sweep(freq, complexIQ):
    n = len(freq)
    """
    Fit Circle
    Rotate and move data  to the origin
    """
    x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(complexIQ.real, complexIQ.imag)
    Z_ii = (x_c + 1j*y_c - complexIQ)#*np.exp(-1j * np.arctan(y_c/x_c))
    """
    ### Fit result and data
    #Fitter.plot_data_circle(complexIQ.real, complexIQ.imag, x_c, y_c, radius)
    plt.plot(Z_ii.real, Z_ii.imag)
    plt.show()
    """
    ### Fit Phase
    data_phase = np.arctan2(Z_ii.imag, Z_ii.real)
    rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[n-1])
    Z_iir = Z_ii*np.exp(1j * rotate_angle)
    phase_rotated = np.arctan2(Z_iir.imag, Z_iir.real)
    theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5
    para_phase = theta0_guess, fr_guess, Qr_guess
    theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase)
    print fit_report_phase

    ### Qc and Qc error
    Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis
    Qc_err0 = np.sqrt(2*(x_c**4*x_c_err**2 + y_c**4*y_c_err**2))/(x_c**2 + y_c**2)
    Qc_err1 = np.sqrt(Qc_err0**2/(x_c**2+y_c**2) + radius**2*radius_err**2)/(np.sqrt(x_c**2+y_c**2)+radius)
    Qc_err2 = np.sqrt(Qc_err1**2 + radius_err**2)
    Qc_err = np.sqrt(Qc_err2**2 + Qr_err**2)
    
    ### Qi and Qi error
    Qi = Qr * Qc/(Qc - Qr)
    Qi_err0 = np.sqrt(Qc_err**2 + Qr_err**2)
    Qi_err1 = np.sqrt(Qc**2*Qc_err**2 + Qr**2*Qr_err**2)/(Qc-Qr)
    Qi_err = np.sqrt(Qi_err0**2 + Qi_err1**2)

    return x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report, theta0, theta0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, Qi, Qi_err, fit_report_phase
def Fit_SingleKID(folder, filename, span):
    ### Read data from file
    result, MeasState = Read_File(folder, filename)
    freq, linear, phase, mag = Get_Data(result, span)
    n = len(freq)
    real = [linear[i] * np.cos(np.deg2rad(phase[i])) for i in range(0, n)]
    imag = [linear[i] * np.sin(np.deg2rad(phase[i])) for i in range(0, n)]
    
    x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(real, imag)
    
    Z_i = [real[i] + 1j*imag[i] for i in range(0, n)]
    Z_ii = (x_c + 1j*y_c - Z_i)*np.exp(-1j * np.arctan(y_c/x_c))

    #Fitter.plot_data_circle(real, imag, x_c, y_c, radius)
    #plt.show()
    
    ### Fit Phase
    data_phase = np.arctan2(Z_ii.imag, Z_ii.real)
    rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[len(Z_ii)-1])
    Z_iic = Z_ii*np.exp(1j * rotate_angle)
    phase_rotated = np.arctan2(Z_iic.imag, Z_iic.real)

    ### Phase fit
    theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5
    para_phase = theta0_guess, fr_guess, Qr_guess
    theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase)
    print fit_report_phase
    
    ### Qc and Qc error
    Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis
    Qc_err0 = np.sqrt(2*(x_c**4*x_c_err**2 + y_c**4*y_c_err**2))/(x_c**2 + y_c**2)
    Qc_err1 = np.sqrt(Qc_err0**2/(x_c**2+y_c**2) + radius**2*radius_err**2)/(np.sqrt(x_c**2+y_c**2)+radius)
    Qc_err2 = np.sqrt(Qc_err1**2 + radius_err**2)
    Qc_err = np.sqrt(Qc_err2**2 + Qr_err**2)
    
    ### Qi and Qi error
    Qi = Qr * Qc/(Qc - Qr)
    Qi_err0 = np.sqrt(Qc_err**2 + Qr_err**2)
    Qi_err1 = np.sqrt(Qc**2*Qc_err**2 + Qr**2*Qr_err**2)/(Qc-Qr)
    Qi_err = np.sqrt(Qi_err0**2 + Qi_err1**2)
    print Qi_err, Qc_err, x_c_err, y_c_err, radius_err, Qr_err
    
    fitcurve = [-theta0 + 2*np.arctan(2 * Qr * (1 - freq[i]/fr)) for i in range(0, n)]
    #plt.plot(freq, data_phase)
    #plt.plot(freq, phase_rotated)
    #plt.plot(freq, fitcurve, 'r')
    #plt.show()
    #print "theta0", params['theta0'].value    
    #print "Temperature (mK)", MeasState[0], "fr from MagS21 (GHz)", MeasState[1], "fr from phase fit (GHz)", fr
    print "Qr", Qr, "Qi", Qi, "Qc", Qc
    #print "Circle fit residue", circle_residue #, "Phase fit residue", result.residual
    finalresult = []
    finalresult.append(MeasState[2])
    finalresult.append(MeasState[0])
    finalresult.append(MeasState[1])
    finalresult.append(fr/1e9)
    finalresult.append(fr_err)
    finalresult.append(Qr)
    finalresult.append(Qr_err)
    finalresult.append(Qc)
    finalresult.append(Qc_err)
    finalresult.append(Qi)
    finalresult.append(Qi_err)
    finalresult.append(filename)
    return finalresult
def Fit_7parameterIQ2(freq, comp, para_guess):

    #Fit Circle
    #Rotate and move data to the origin
    n = len(freq)
    I = comp.real
    Q = comp.imag
    x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(I, Q)

    Z_i = [I[i] + 1j*Q[i] for i in range(0, n)]
    Z_ii = (x_c + 1j*y_c - Z_i)*np.exp(-1j * np.arctan(y_c/x_c))
    
    #Fitter.plot_data_circle(Z_ii.real, Z_ii.imag, x_c, y_c, radius)
    #plt.show()

    ### Fit Phase
    data_phase = np.arctan2(Z_ii.imag, Z_ii.real)

    rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[len(Z_ii)-1])
    Z_iic = Z_ii*np.exp(1j * rotate_angle)
    phase_rotated = np.arctan2(Z_iic.imag, Z_iic.real)

    ### Phase fit
    theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5
    para_phase = theta0_guess, fr_guess, Qr_guess
    theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase)
    print fit_report_phase
    Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis
    Qi = Qr * Qc/(Qc - Qr)
    print Qi
    """
    fitphasecurve = [-theta0 + 2*np.arctan(2 * Qr * (1 - freq[i]/fr)) for i in range(0, n)]
    plt.plot(freq, phase_rotated)
    plt.plot(freq, fitphasecurve, 'r')
    plt.show()
    """
    a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult = Fitter.Fit_7para(freq, comp, para_guess)
    sevenpara_result = a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult
    fit7paracurve = a * np.exp(1j*alpha) * np.exp(-2*np.pi*1j*freq*tau) * (1 - (Qr/Qc*np.exp(1j*phi0))/(1 + 2*1j*Qr*(freq-fr)/fr))

    #plt.plot(comp.real, comp.imag)
    #plt.plot(fit7paracurve.real, fit7paracurve.imag, 'r')
    #plt.show()
    
    Qi = sevenpara_result[10] * sevenpara_result[12]/(sevenpara_result[12] - sevenpara_result[10])
    print Qi
    
    finalresult = []
    finalresult.append(a)
    finalresult.append(a_err/a)
    finalresult.append(alpha)
    finalresult.append(alpha_err/alpha)
    finalresult.append(tau)
    finalresult.append(tau_err/(tau+1))
    finalresult.append(phi0)
    finalresult.append(phi0_err/phi0)
    finalresult.append(fr)
    finalresult.append(fr_err/fr)
    finalresult.append(Qr)
    finalresult.append(Qr_err/Qr)
    finalresult.append(Qc)
    finalresult.append(Qc_err/Qc)
    finalresult.append(x_c)
    finalresult.append(x_c_err/x_c)
    finalresult.append(y_c)
    finalresult.append(y_c_err/y_c)
    finalresult.append(radius)
    finalresult.append(radius_err/radius)
    
    return finalresult
def Fit_7parameter(folder, filename, span):
    ### Read data from file
    result, MeasState = Read_File(folder, filename)
    freq, linear, phase, mag = Get_Data(result, span)
    n = len(freq)
    real = [linear[i] * np.cos(np.deg2rad(phase[i])) for i in range(0, n)]
    imag = [linear[i] * np.sin(np.deg2rad(phase[i])) for i in range(0, n)]
    comp = [real[i]+imag[i]*1j for i in range(0, n)]

    """
    Fit Circle
    Rotate and move data  to the origin
    """
    x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(real, imag)

    Z_i = [real[i] + 1j*imag[i] for i in range(0, n)]
    Z_ii = (x_c + 1j*y_c - Z_i)*np.exp(-1j * np.arctan(y_c/x_c))
    
    #Fitter.plot_data_circle(Z_ii.real, Z_ii.imag, x_c, y_c, radius)
    #plt.show()

    ### Fit Phase
    data_phase = np.arctan2(Z_ii.imag, Z_ii.real)

    rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[len(Z_ii)-1])
    Z_iic = Z_ii*np.exp(1j * rotate_angle)
    phase_rotated = np.arctan2(Z_iic.imag, Z_iic.real)

    ### Phase fit
    theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5
    para_phase = theta0_guess, fr_guess, Qr_guess
    theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase)
    print fit_report_phase
    Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis
    Qi = Qr * Qc/(Qc - Qr)
    print Qi
    """
    fitphasecurve = [-theta0 + 2*np.arctan(2 * Qr * (1 - freq[i]/fr)) for i in range(0, n)]
    plt.plot(freq, phase_rotated)
    plt.plot(freq, fitphasecurve, 'r')
    plt.show()
    """
    #print "theta0", params['theta0'].value    
    #print "Temperature (mK)", MeasState[0], "fr from MagS21 (GHz)", MeasState[1], "fr from phase fit (GHz)", fr
    #print "Qr", Qr, "Qi", Qi, "Qc", Qc
    #print "Circle fit residue", circle_residue #, "Phase fit residue", result.residual
    a = 3.636
    alpha = 0.381
    tau = 0
    phi0 = theta0-np.arctan2(x_c, y_c)+rotate_angle + np.pi
    estimateparas = [a,alpha,tau,phi0,fr,Qr,Qc]
    
    a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult = Fitter.Fit_7para(freq, comp, estimateparas)
    sevenpara_result = a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult
    fit7paracurve = sevenpara_result[0]*np.exp(1j*sevenpara_result[2]) * np.exp(-2*np.pi*1j*freq*sevenpara_result[4]) * (1 - (sevenpara_result[10]/sevenpara_result[12]*np.exp(1j*sevenpara_result[6]))/(1 + 2*1j*sevenpara_result[10]*(freq-sevenpara_result[8])/sevenpara_result[8]))
    #plt.plot(real,imag)
    #plt.plot(fit7paracurve.real, fit7paracurve.imag, 'r')
    #plt.show()
    
    Qi = sevenpara_result[10] * sevenpara_result[12]/(sevenpara_result[12] - sevenpara_result[10])
    print Qi
    
    finalresult = []
    finalresult.append(MeasState[2])
    finalresult.append(MeasState[0])
    finalresult.append(MeasState[1])
    finalresult.append(fr/1e9)
    finalresult.append(fr_err/fr)
    finalresult.append(Qr)
    finalresult.append(Qr_err/Qr)
    finalresult.append(Qc)
    finalresult.append(Qi)
    finalresult.append(filename)
    
    return finalresult