def showme():

    #!	open HD19467B and read in the arrays for wavelength, flux, and uncertainty
    file = open(
        '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/HD19467NewSpect.txt'
    )
    W_obj, F_obj, U_obj = [], [], []
    for line in file:
        columns = line.split()
        wav = float(columns[0])
        flu = float(columns[1])
        un = float(columns[2])
        W_obj.append(wav)
        F_obj.append(flu)
        U_obj.append(un)
    file.close()
    W_obj_t = W_obj[4:30]  #t = trimmed
    F_obj_t = F_obj[4:30]
    U_obj_t = U_obj[4:30]

    myInt = 2.3630917239963894e-17
    F_obj_tn = [x / myInt for x in F_obj_t]  #n = normalized
    U_obj_tn = [x / myInt for x in U_obj_t]
    before = np.array([W_obj_t, F_obj_t, U_obj_t])
    after = np.array([W_obj_t, F_obj_tn, U_obj_tn])
    after = u.norm_spec(
        after,
        before)  #to make sure all spaces in between points are the same length
    after = [i.value for i in after]
    # 	plt.errorbar(before[0],before[1],yerr=before[2])
    # 	plt.errorbar(after[0],after[1],yerr=after[2])
    plt.plot(before[0], before[1])
    plt.show()
    plt.clf()
    plt.plot(after[0], after[1])
    plt.show()
示例#2
0
def showme():

    #!	open HD19467B and read in the arrays for wavelength, flux, and uncertainty
    W, F, U = db.query.execute(
        "select wavelength, flux, unc from spectra where id=15").fetchone()

    binned_wl = 0
    jump = 0.0267097  #defined by the wavelength points of HD19467B data
    start = 1.1826776 - (jump / 2)  #start half way before 1st flux point
    W, F, U = u.scrub([W, F, U])
    W = W.value
    F = F.value
    U = U.value
    flux2 = 0
    W_obj, F_obj, U_obj = [], [], []
    unc2 = 0
    w1 = start + (jump / 2)  #first wl point is at start+the step/2
    wavelength = 0
    # 		W_temp, F_temp, U_temp=u.rebin_spec([W,F,U],wava)
    #!		loop through 21 iterations for the amount of wavelength ranges I have
    for i in range(0, 21):
        binned_wl = jump + start  #range is half below and half above w1

        flux2 = sum(F[np.where(np.logical_and(W > start,
                                              W < binned_wl))])  #binning flux
        unc2 = np.sqrt(
            sum(U[np.where(np.logical_and(
                W > start,
                W < binned_wl))]**2))  #bin uncertainty in quadrature

        start = binned_wl  #set new range start

        F_obj.append(flux2)
        U_obj.append(unc2)
        wavelength = w1 + (i * jump
                           )  #this is where the flux point will go on plot
        W_obj.append(wavelength)

    object = np.array([
        W_obj, F_obj, U_obj
    ])  #to make sure all spaces in between points are the same length

    #! 	pick wavelength range between points on object plot										#not necessary to this code
    wavelength_points = [
        1.1826776, 1.2093873000000002, 1.236097, 1.2628067, 1.2895164,
        1.3162261000000002, 1.3429358, 1.3696455, 1.3963552,
        1.4230649000000002, 1.4497746, 1.4764843, 1.503194, 1.5299037000000002,
        1.5566134, 1.5833231, 1.6100328, 1.6367425, 1.6634522, 1.6901619,
        1.7168716
    ]

    #!	get source_id for all T dwarfs from database, turn it into a list and use list to get spectra_id for each source's spex data
    sources = db.query.execute(
        "select distinct source_id from spectral_types where spectral_type>=20 and spectral_type<=30 and regime='IR'"
    ).fetchall()
    sourcelist, ids, idlist = [], [], []
    index = 0
    while index < len(sources):
        sourcelist.append(sources[index][0])
        index = index + 1
    for i in sourcelist:
        id = db.query.execute(
            "SELECT distinct id FROM spectra WHERE source_id='{}' AND instrument_id=6"
            .format(i)).fetchone()
        ids.append(id)
    ids = filter(None, ids)
    index = 0
    while index < len(ids):
        idlist.append(ids[index][0])
        index = index + 1
    print len(idlist)

    #!	loop through each object
    chisquare, sptlist, chilist, namelist, specidlist = [], [], [], [], []

    for j in idlist:
        name, W, F, U = db.query.execute(
            "SELECT source_id, wavelength, flux, unc from spectra where id='{}'"
            .format(j)).fetchone()
        binned_wl = 0
        jump = 0.0267097  #defined by the wavelength points of HD19467B data
        start = 1.1826776 - (jump / 2)  #start half way before 1st flux point
        W, F, U = u.scrub([W, F, U])
        W = W.value
        F = F.value
        U = U.value
        original = np.array([W, F, U])
        source, spectype = db.query.execute(
            "SELECT spectral_types.source_id, spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectral_types.spectral_type>=20 and spectral_types.spectral_type<=30 and spectra.id='{}'"
            .format(j)).fetchone()
        print name, source
        flux2 = 0
        W_temp, F_temp, U_temp = [], [], []
        unc2 = 0
        w1 = start + (jump / 2)  #first wl point is at start+the step/2
        wavelength = 0
        # 		W_temp, F_temp, U_temp=u.rebin_spec([W,F,U],wava)
        #!		loop through 21 iterations for the amount of wavelength ranges I have
        for i in range(0, 21):
            binned_wl = jump + start  #range is half below and half above w1

            flux2 = sum(F[np.where(np.logical_and(
                W > start, W < binned_wl))])  #binning flux
            unc2 = np.sqrt(
                sum(U[np.where(np.logical_and(
                    W > start,
                    W < binned_wl))]**2))  #bin uncertainty in quadrature

            start = binned_wl  #set new range start

            F_temp.append(flux2)
            U_temp.append(unc2)
            wavelength = w1 + (i * jump
                               )  #this is where the flux point will go on plot
            W_temp.append(wavelength)

        template = np.array([W_temp, F_temp,
                             U_temp])  #create arrays for ease of use
        template = u.norm_spec(
            template, original)  #Joe's code to normalize plots to each other
        template = [i.value for i in template]
        #!		plot template over template's original flux
        plt.plot(template[0], template[1])
        plt.plot(original[0], original[1])
        plt.savefig(
            '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/SanityCheckplots/binned_flux_with_original/'
            + '{}'.format(name) + '.pdf')
        plt.clf()
        template = np.array([W_temp, F_temp, U_temp])
        template = u.norm_spec(
            template, object)  #Joe's code to normalize plots to each other
        template = [i.value for i in template]
        #!		plot template over object
        plt.errorbar(template[0], template[1], yerr=template[2], marker="o")
        plt.errorbar(object[0], object[1], yerr=object[2], marker="o")
        plt.savefig(
            '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/SanityCheckplots/template_over_object/'
            + '{}'.format(name) + '_HD19467.pdf')
        plt.clf()
        #-----------------------------------------------------------------------------------------
        try:
            chi = sum(((template[1] - object[1])**2) /
                      ((template[2]**2) +
                       (object[2]**2)))  #both data sets have uncertainties
            namelist.append(source)
            sptlist.append(spectype)
            chilist.append(chi)
            specidlist.append(j)
# 			output=np.array([name,spectype,chi])
        except ValueError:
            pass

    output = np.array([namelist, specidlist, sptlist, chilist])
    print len(sptlist), chilist

    #!	plotting the best fits, or minimum chi squared values over the object
    min_vals = np.where(output[3] < 400)
    for i in min_vals:
        print "min_vals = {0}".format(i)
        print output[0][i], output[1][i], output[2][i], output[3][i]

        for k in output[1][i]:
            name, W, F, U = db.query.execute(
                "SELECT source_id, wavelength, flux, unc from spectra where id='{}'"
                .format(k)).fetchone()
            binned_wl = 0
            jump = 0.0267097  #defined by the wavelength points of HD19467B data
            start = 1.1826776 - (jump / 2
                                 )  #start half way before 1st flux point

            source, spectype = db.query.execute(
                "SELECT spectral_types.source_id, spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectral_types.spectral_type>=20 and spectral_types.spectral_type<=30 and spectra.id='{}'"
                .format(k)).fetchone()
            print source, spectype
            flux2 = 0
            W_temp, F_temp, U_temp, = [], [], []
            unc2 = 0
            w1 = start + (jump / 2)  #first wl point is at start+the step/2
            wavelength = 0

            #!		loop through 21 iterations for the amount of wavelength ranges I have
            for i in range(0, 21):
                binned_wl = jump + start  #range is half below and half above w1
                flux2 = sum(F[np.where(np.logical_and(
                    W > start, W < binned_wl))])  #binning flux
                unc2 = np.sqrt(
                    sum(U[np.where(np.logical_and(
                        W > start,
                        W < binned_wl))]**2))  #bin uncertainty in quadrature
                start = binned_wl  #set new range start

                F_temp.append(flux2)
                U_temp.append(unc2)
                wavelength = w1 + (
                    i * jump)  #this is where the flux point will go on plot
                W_temp.append(wavelength)

            template = np.array([W_temp, F_temp, U_temp])
            template = u.norm_spec(template, object)

            plt.errorbar(template[0],
                         template[1],
                         yerr=template[2],
                         marker="o",
                         label=str(spectype))
            plt.legend(loc='upper right')
    plt.errorbar(object[0],
                 object[1],
                 yerr=object[2],
                 marker="o",
                 label='HD19467B')
    plt.savefig(
        '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/SanityCheckplots/bestfits_w_object.pdf'
    )
    plt.clf()

    chisquare = [sptlist, chilist]

    #!	plot spectral type vs chi square
    plt.scatter(chisquare[0], chisquare[1])
    chisquare = zip(*sorted(zip(*chisquare)))
    pfit = np.polyfit(chisquare[0], chisquare[1],
                      3)  # Fit a 2nd order polynomial to (x, y) data
    yfit = np.polyval(pfit, chisquare[0])  # Evaluate the polynomial at x
    plt.plot(chisquare[0], yfit)
    plt.savefig(
        '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/SanityCheckplots/chisquare_distribution.pdf'
    )
    plt.clf()
示例#3
0
def showme(ax):
	file=open('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/HD19467B.txt')
	W_obj,F_obj,U_obj=[],[],[]
	for line in file:
		columns=line.split()
		wav=float(columns[0])
		flu=float(columns[1])
		un=float(columns[2])
		W_obj.append(wav) ; F_obj.append(flu) ; U_obj.append(un)
	file.close()	
	object=np.array([W_obj,F_obj,U_obj])
	lamb=np.gradient(W_obj)
	xer=lamb/2

# 	T6
	original, template6=m.bin_down(7982,0.0267097,1.1826776,21)	
	source,spectype=db.query.execute("SELECT spectral_types.source_id, spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectral_types.spectral_type>=20 and spectral_types.spectral_type<=30 and spectra.id=7982").fetchone()
	spectype=a.specType(spectype)
	template_plot26=u.norm_spec(template6,object)												#Joe's code to normalize plots to each other
	template_plot26=[i.value for i in template_plot26]
	ax.plot(template_plot26[0],template_plot26[1],'c:',linewidth=2,label=str(spectype))
	
		
# 	T7
	original, template7=m.bin_down(3121,0.0267097,1.1826776,21)	
	source,spectype=db.query.execute("SELECT spectral_types.source_id, spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectral_types.spectral_type>=20 and spectral_types.spectral_type<=30 and spectra.id=3121").fetchone()
	spectype=a.specType(spectype)
	template_plot27=u.norm_spec(template7,object)												#Joe's code to normalize plots to each other
	template_plot27=[i.value for i in template_plot27]
	ax.plot(template_plot27[0],template_plot27[1],'g-.',linewidth=2,label=str(spectype))

# 	T7.5
	original, template=m.bin_down(5308,0.0267097,1.1826776,21)	
	source,spectype=db.query.execute("SELECT spectral_types.source_id, spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectral_types.spectral_type>=20 and spectral_types.spectral_type<=30 and spectra.id=5308").fetchone()
	spectype=a.specType(spectype)
	template_plot2=u.norm_spec(template,object)												#Joe's code to normalize plots to each other
	template_plot2=[i.value for i in template_plot2]
	ax.plot(template_plot2[0],template_plot2[1],'r--',linewidth=2,label=str(spectype))
	
# 	T8
	with open('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/LateTdata/WISEJ050003.04-122343.2_SpeX.txt') as f:
		lines_after_17 = f.readlines()[7:]  
		Wtxt,Ftxt,Utxt=[],[],[]
		for line in lines_after_17:
			columns=line.split()
			wavx=float(columns[0])
			flux=float(columns[1])
			unx=float(columns[2])
			Wtxt.append(wavx) ; Ftxt.append(flux) ; Utxt.append(unx)
		f.close()	
	spectype=28
	spectype=a.specType(spectype)
	binned_wl=0																	#defined by the wavelength points of HD19467B data
	start=1.1826776-(0.0267097/2) 															#start half way before 1st flux point
	W,F,U=u.scrub([Wtxt,Ftxt,Utxt])
	W=W.value
	F=F.value
	U=U.value
	original=np.array([Wtxt,Ftxt,Utxt])
	flux2=0
	W_temp, F_temp, U_temp=[],[],[]
	unc2=0																	#first wl point is at start+the step/2
	wavelength=0
	for i in range(21):
		binned_wl=0.0267097 + start 															#range is half below and half above w1
		flux2=sum(F[np.where(np.logical_and(W>start,W<binned_wl))])					#binning flux
		unc2=np.sqrt(sum(U[np.where(np.logical_and(W>start,W<binned_wl))]**2))			#bin uncertainty in quadrature
		start=binned_wl																	#set new range start
		F_temp.append(flux2)
		U_temp.append(unc2)
		wavelength=1.1826776+(i*0.0267097)															#this is where the flux point will go on plot
		W_temp.append(wavelength)	
	template28=np.array([W_temp, F_temp, U_temp])
	template_plot28=u.norm_spec(template28,object)												#Joe's code to normalize plots to each other
	template_plot28=[i.value for i in template_plot28]
	ax.plot(template_plot28[0],template_plot28[1],'b',linewidth=3,label=str(spectype))
	
#	T8.5
	with open('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/LateTdata/WISEJ004945.61+215120.0_SpeX.txt') as f:
		lines_after_17 = f.readlines()[7:]  
		Wtxt,Ftxt,Utxt=[],[],[]
		for line in lines_after_17:
			columns=line.split()
			wavx=float(columns[0])
			flux=float(columns[1])
			unx=float(columns[2])
			Wtxt.append(wavx) ; Ftxt.append(flux) ; Utxt.append(unx)
		f.close()	
	spectype=28.5
	spectype=a.specType(spectype)
	binned_wl=0																	#defined by the wavelength points of HD19467B data
	start=1.1826776-(0.0267097/2) 															#start half way before 1st flux point
	W,F,U=u.scrub([Wtxt,Ftxt,Utxt])
	W=W.value
	F=F.value
	U=U.value
	original=np.array([Wtxt,Ftxt,Utxt])
	flux2=0
	W_temp, F_temp, U_temp=[],[],[]
	unc2=0																	#first wl point is at start+the step/2
	wavelength=0
	for i in range(21):
		binned_wl=0.0267097 + start 															#range is half below and half above w1
		flux2=sum(F[np.where(np.logical_and(W>start,W<binned_wl))])					#binning flux
		unc2=np.sqrt(sum(U[np.where(np.logical_and(W>start,W<binned_wl))]**2))			#bin uncertainty in quadrature
		start=binned_wl																	#set new range start
		F_temp.append(flux2)
		U_temp.append(unc2)
		wavelength=1.1826776+(i*0.0267097)															#this is where the flux point will go on plot
		W_temp.append(wavelength)	
	template29=np.array([W_temp, F_temp, U_temp])
	template_plot29=u.norm_spec(template29,object)												#Joe's code to normalize plots to each other
	template_plot29=[i.value for i in template_plot29]
	ax.plot(template_plot29[0],template_plot29[1],'m--',linewidth=2,label=str(spectype))
	
	
# 	T9
	with open('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/LateTdata/T9_UGPS0722-0540_STD.txt') as f:
		lines_after_17 = f.readlines()[7:]  
		Wtxt,Ftxt,Utxt=[],[],[]
		for line in lines_after_17:
			columns=line.split()
			wavx=float(columns[0])
			flux=float(columns[1])
			unx=float(columns[2])
			Wtxt.append(wavx) ; Ftxt.append(flux) ; Utxt.append(unx)
		f.close()	
	spectype=29
	spectype=a.specType(spectype)
	binned_wl=0																	#defined by the wavelength points of HD19467B data
	start=1.1826776-(0.0267097/2) 															#start half way before 1st flux point
	W,F,U=u.scrub([Wtxt,Ftxt,Utxt])
	W=W.value
	F=F.value
	U=U.value
	original=np.array([Wtxt,Ftxt,Utxt])
	flux2=0
	W_temp, F_temp, U_temp=[],[],[]
	unc2=0																	#first wl point is at start+the step/2
	wavelength=0
	for i in range(21):
		binned_wl=0.0267097 + start 															#range is half below and half above w1
		flux2=sum(F[np.where(np.logical_and(W>start,W<binned_wl))])					#binning flux
		unc2=np.sqrt(sum(U[np.where(np.logical_and(W>start,W<binned_wl))]**2))			#bin uncertainty in quadrature
		start=binned_wl																	#set new range start
		F_temp.append(flux2)
		U_temp.append(unc2)
		wavelength=1.1826776+(i*0.0267097)															#this is where the flux point will go on plot
		W_temp.append(wavelength)	
	template29=np.array([W_temp, F_temp, U_temp])
	template_plot29=u.norm_spec(template29,object)												#Joe's code to normalize plots to each other
	template_plot29=[i.value for i in template_plot29]
	ax.plot(template_plot29[0],template_plot29[1],'g-.',linewidth=2,label=str(spectype))
	
	
	ax.errorbar(object[0],object[1],xerr=xer,yerr=object[2],fmt=None,ecolor='k',marker='o',label='HD19467B')
	ax.set_ylim(0.001,1.1)
	ax.set_xlim(1.15,1.75)
	ax.legend( loc='upper right',fontsize=10,prop={'size':15}, ncol=2, numpoints=1)
	ax.set_xlabel(r'Wavelength ($\mu$m)', fontsize="x-large")
	ax.set_ylabel('Normalized Flux', fontsize="x-large")
	ax.tick_params(labelsize="large")
	
# 	plt.savefig('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/figure.pdf')
# 	plt.clf()
# 	return p2
def showme():
    file = open(
        '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/HD19467B.txt'
    )
    wava = []
    flua = []
    una = []
    for line in file:
        columns = line.split()
        wav = float(columns[0])
        flu = float(columns[1])
        un = float(columns[2])
        wava.append(wav)
        flua.append(flu)
        una.append(un)
    file.close()

    # 	pick wavelength range between points on object plot

    wavelength_points = [
        1.1826776, 1.2093873000000002, 1.236097, 1.2628067, 1.2895164,
        1.3162261000000002, 1.3429358, 1.3696455, 1.3963552,
        1.4230649000000002, 1.4497746, 1.4764843, 1.503194, 1.5299037000000002,
        1.5566134, 1.5833231, 1.6100328, 1.6367425, 1.6634522, 1.6901619,
        1.7168716
    ]
    sources = db.query.execute(
        "select distinct source_id from spectral_types where spectral_type>=20 and spectral_type<=30 and regime='IR'"
    ).fetchall()

    sourcelist = []

    index = 0
    while index < len(sources):
        sourcelist.append(sources[index][0])
        index = index + 1
    ids = []
    for i in sourcelist:
        id = db.query.execute(
            "SELECT distinct id FROM spectra WHERE source_id='{}' AND instrument_id=6"
            .format(i)).fetchone()
        ids.append(id)
    ids = filter(None, ids)

    idlist = []

    index = 0
    while index < len(ids):
        idlist.append(ids[index][0])
        index = index + 1

    print len(idlist)
    output = []
    chisquare = []
    sptlist = []
    chilist = []
    for j in idlist:
        name, W, F, unc = db.query.execute(
            "SELECT source_id, wavelength, flux, unc from spectra where id='{}'"
            .format(j)).fetchone()
        binned_wl = 0
        jump = 0.0267097
        start = 1.1826776 + (jump / 2)  #start half way after 1st flux point
        times = 21
        end = 1.73022645
        spectype = db.query.execute(
            "SELECT spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectra.id='{}'"
            .format(j)).fetchone()

        #	normalize the flux incase it's not already
        NF = F * 1.27 / np.interp(1.27, W, F)
        NU = unc * 1.27 / np.interp(1.27, W, unc)
        flux2 = 0
        newflux = []
        unc2 = 0
        newunc = []
        w1 = start + (jump / 2)  #first wl point is at start+the step/2
        newwl = []
        wavelength = 0

        for i in range(0, 21):
            binned_wl = jump + start  #range is half below and half above w1
            flux2 = sum(NF[np.where(np.logical_and(W > start, W < binned_wl))])
            unc2 = np.sqrt(
                sum(NU[np.where(np.logical_and(W > start, W < binned_wl))]**2))
            start = binned_wl

            newflux.append(flux2)
            newunc.append(unc2)
            wavelength = w1 + (i * jump)
            newwl.append(wavelength)
#
# 		G,C=goodness([wava,flua,una],[newwl,newflux,newunc])
# 		newflux=C*np.array(newflux)
# 		newunc=C*np.array(newunc)

# 		plt.errorbar(newwl,newflux,yerr=newunc,marker="o")
# 		plt.errorbar(wava,flua,yerr=una,marker="o")
# 	calculate a chi squared
# 		wava*=q.um
# 		flua*=q.erg/q.s/q.cm**2/q.AA
# 		una*=q.erg/q.s/q.cm**2/q.AA
# #

        template = np.array([newwl, newflux, newunc])
        object = np.array([wava, flua, una])

        template = u.norm_spec(template, object)
        # 		plt.errorbar(template[0],template[1],yerr=template[2],marker="o")
        # 		plt.errorbar(object[0],object[1],yerr=object[2],marker="o")
        # 		plt.savefig('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/'+'{}'.format(name)+ '.pdf')
        # 		plt.clf()

        try:
            chi, p = s.chisquare(
                template[1], object[1])  #len of template and object dont match
            output.append([chi, name, spectype])
            sptlist.append(spectype)
            chilist.append(chi)

        except ValueError:
            pass

    chisquare = [sptlist, chilist]

    plt.scatter(chisquare[0], chisquare[1])
    # 	plt.savefig('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/chisquare_distribution.pdf')
    # 	plt.clf()
    plt.show()
def showme():

    #!	open HD19467B and read in the arrays for wavelength, flux, and uncertainty
    file = open(
        '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/HD19467B.txt'
    )
    W_obj, F_obj, U_obj = [], [], []
    for line in file:
        columns = line.split()
        wav = float(columns[0])
        flu = float(columns[1])
        un = float(columns[2])
        W_obj.append(wav)
        F_obj.append(flu)
        U_obj.append(un)
    file.close()
    object = np.array([W_obj, F_obj, U_obj])
    print np.gradient(
        W_obj)  #to make sure all spaces in between points are the same length

    #!	get source_id for all T dwarfs from database, turn it into a list and use list to get spectra_id for each source's spex data
    sources = db.query.execute(
        "select distinct source_id from spectral_types where spectral_type>=20 and spectral_type<=30 and regime='IR'"
    ).fetchall()
    sourcelist, ids, idlist = [], [], []
    index = 0
    while index < len(sources):
        sourcelist.append(sources[index][0])
        index = index + 1
    for i in sourcelist:
        id = db.query.execute(
            "SELECT distinct id FROM spectra WHERE source_id='{}' AND instrument_id=6"
            .format(i)).fetchone()
        ids.append(id)

    ids = filter(None, ids)
    index = 0
    while index < len(ids):
        idlist.append(ids[index][0])
        index = index + 1
    print len(idlist)

    #!	loop through each object
    chisquare, chilist, namelist, specidlist, sptlist = [], [], [], [], []

    for j in idlist:
        original, template = m.bin_down(j, 0.0267097, 1.1826776,
                                        21)  #create arrays for ease of use

        source, spectype = db.query.execute(
            "SELECT spectral_types.source_id, spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectral_types.spectral_type>=20 and spectral_types.spectral_type<=30 and spectra.id='{}'"
            .format(j)).fetchone()

        template_plot1 = u.norm_spec(
            template, original)  #Joe's code to normalize plots to each other
        template_plot1 = [i.value for i in template_plot1]
        # #!		plot template over template's original flux
        # 		plt.plot(template_plot1[0],template_plot1[1])
        # 		plt.plot(original[0],original[1])
        # 		plt.savefig('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/binned_flux_with_original/'+'{}'.format(source)+ '.pdf')
        # 		plt.clf()

        template_plot2 = u.norm_spec(
            template, object)  #Joe's code to normalize plots to each other
        template_plot2 = [i.value for i in template_plot2]
        # #!		plot template over object
        # 		plt.errorbar(template_plot2[0],template_plot2[1],yerr=template_plot2[2],marker="o")
        # 		plt.errorbar(object[0],object[1],yerr=object[2],marker="o")
        # 		plt.savefig('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/template_over_object/'+'{}'.format(source)+ '_HD19467.pdf')
        # 		plt.clf()

        template = u.norm_spec(template, object)
        template = [i.value for i in template]
        chi = sum(((template[1] - object[1])**2) /
                  ((template[2]**2) +
                   (object[2]**2)))  #both data sets have uncertainties
        namelist.append(source)
        sptlist.append(spectype)
        chilist.append(float(chi))
        specidlist.append(j)

# 	filenamelist=['WISEJ225540.75-311842.0_SpeX','WISEJ222623.05+044004.0_SpeX','WISEJ171104.60+350036.8_SpeX','WISEJ165311.05+444422.8_SpeX','WISEJ132233.64-234016.8_SpeX','WISEJ062309.94-045624.6_SpeX','WISEJ045853.89+643452.5_SpeX','WISEJ050003.04-122343.2_SpeX','WISEJ025409.51+022358.6_SpeX','T9_UGPS0722-0540_STD','WISEJ004945.61+215120.0_SpeX','WISEJ024512.62-345047.8_SpeX']
# 	spectypelist=[28,28,28,28,28,28,28.5,28,28,29,28.5,28]
# 	for k in range(12):
# 		with open('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/LateTdata/'+'{}'.format(filenamelist[k])+'.txt') as f:
# 			lines_after_17 = f.readlines()[7:]
# 			Wtxt,Ftxt,Utxt=[],[],[]
# 			for line in lines_after_17:
# 				columns=line.split()
# 				wavx=float(columns[0])
# 				flux=float(columns[1])
# 				unx=float(columns[2])
# 				Wtxt.append(wavx) ; Ftxt.append(flux) ; Utxt.append(unx)
# 			f.close()
# 		spectype=spectypelist[k]
#
# 		binned_wl=0																	#defined by the wavelength points of HD19467B data
# 		start=1.1826776-(0.0267097/2) 															#start half way before 1st flux point
# 		W,F,U=u.scrub([Wtxt,Ftxt,Utxt])
# 		W=W.value
# 		F=F.value
# 		U=U.value
# 		original=np.array([Wtxt,Ftxt,Utxt])
# 		flux2=0
# 		W_temp, F_temp, U_temp=[],[],[]
# 		unc2=0																	#first wl point is at start+the step/2
# 		wavelength=0
#
# 		#!		loop through 21 iterations for the amount of wavelength ranges I have
# 		for i in range(21):
# 			binned_wl=0.0267097 + start 															#range is half below and half above w1
#
# 			flux2=sum(F[np.where(np.logical_and(W>start,W<binned_wl))])					#binning flux
# 			unc2=np.sqrt(sum(U[np.where(np.logical_and(W>start,W<binned_wl))]**2))			#bin uncertainty in quadrature
#
# 			start=binned_wl																	#set new range start
#
# 			F_temp.append(flux2)
# 			U_temp.append(unc2)
# 			wavelength=1.1826776+(i*0.0267097)															#this is where the flux point will go on plot
# 			W_temp.append(wavelength)
#
# 		template=np.array([W_temp, F_temp, U_temp])
#
# 		template_plot1=u.norm_spec(template,original)												#Joe's code to normalize plots to each other
# 		template_plot1=[i.value for i in template_plot1]
# # #!		plot template over template's original flux
# 		plt.plot(template_plot1[0],template_plot1[1])
# 		plt.plot(original[0],original[1])
# 		plt.savefig('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/binned_flux_with_original/'+'{}'.format(filenamelist[k])+ '.pdf')
# 		plt.clf()
#
# 		template_plot2=u.norm_spec(template,object)												#Joe's code to normalize plots to each other
# 		template_plot2=[i.value for i in template_plot2]
# # #!		plot template over object
# 		plt.errorbar(template_plot2[0],template_plot2[1],yerr=template_plot2[2],marker="o")
# 		plt.errorbar(object[0],object[1],yerr=object[2],marker="o")
# 		plt.savefig('/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/template_over_object/'+'{}'.format(filenamelist[k])+ '_HD19467.pdf')
# 		plt.clf()
#
# 		template=u.norm_spec(template,object)
# 		template=[i.value for i in template]
# 		chi=sum(((template[1]-object[1])**2)/((template[2]**2)+(object[2]**2)))     	#both data sets have uncertainties
# # 		print filenamelist[k],type(chi), spectype
# 		namelist.append(filenamelist[k])
# 		sptlist.append(spectypelist[k])
# 		chilist.append(float(chi))
# 		specidlist.append('None')

#------------------------------
    output = np.array([chilist, namelist, specidlist, sptlist])
    chilistfloat = np.array([float(n) for n in output[0]])
    print len(sptlist)
    top5 = chilistfloat.argsort()[:10]
    print top5
    for i in top5:
        print output[0][i], output[1][i], output[2][i], output[3][i]
        if output[2][i] != 'None':
            k = output[2][i]
            original, template_min = m.bin_down(
                k, 0.0267097, 1.1826776, 21)  #create arrays for ease of use

            source, spectype = db.query.execute(
                "SELECT spectral_types.source_id, spectral_types.spectral_type from spectral_types join spectra on spectral_types.source_id=spectra.source_id where spectral_types.regime='IR' and spectral_types.spectral_type>=20 and spectral_types.spectral_type<=30 and spectra.id='{}'"
                .format(k)).fetchone()

            template_min = u.norm_spec(template_min, object)
            template_min = [i.value for i in template_min]
            plt.errorbar(template_min[0],
                         template_min[1],
                         yerr=template_min[2],
                         marker="o",
                         label=str(spectype))
        else:
            with open(
                    '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/LateTdata/'
                    + '{}'.format(output[1][i]) + '.txt') as f:
                lines_after_17 = f.readlines()[7:]
                Wtxt, Ftxt, Utxt = [], [], []
                for line in lines_after_17:
                    columns = line.split()
                    wavx = float(columns[0])
                    flux = float(columns[1])
                    unx = float(columns[2])
                    Wtxt.append(wavx)
                    Ftxt.append(flux)
                    Utxt.append(unx)
                f.close()
            spectype = output[3][i]

            binned_wl = 0  #defined by the wavelength points of HD19467B data
            start = 1.1826776 - (0.0267097 / 2
                                 )  #start half way before 1st flux point
            W, F, U = u.scrub([Wtxt, Ftxt, Utxt])
            W = W.value
            F = F.value
            U = U.value
            original = np.array([Wtxt, Ftxt, Utxt])
            flux2 = 0
            W_temp, F_temp, U_temp = [], [], []
            unc2 = 0  #first wl point is at start+the step/2
            wavelength = 0

            #!		loop through 21 iterations for the amount of wavelength ranges I have
            for i in range(21):
                binned_wl = 0.0267097 + start  #range is half below and half above w1

                flux2 = sum(F[np.where(np.logical_and(
                    W > start, W < binned_wl))])  #binning flux
                unc2 = np.sqrt(
                    sum(U[np.where(np.logical_and(
                        W > start,
                        W < binned_wl))]**2))  #bin uncertainty in quadrature

                start = binned_wl  #set new range start

                F_temp.append(flux2)
                U_temp.append(unc2)
                wavelength = 1.1826776 + (
                    i * 0.0267097
                )  #this is where the flux point will go on plot
                W_temp.append(wavelength)

            template = np.array([W_temp, F_temp, U_temp])
            template_min = u.norm_spec(template, object)
            template_min = [i.value for i in template_min]
            plt.errorbar(template_min[0],
                         template_min[1],
                         yerr=template_min[2],
                         marker="o",
                         label=str(spectype))
    plt.legend(loc='upper right')
    plt.xlim(1.15, 1.75)
    plt.ylim(0.001, 1.2)
    plt.errorbar(object[0],
                 object[1],
                 yerr=object[2],
                 marker="o",
                 label='HD19467B')
    plt.savefig(
        '/Users/paigegiorla/Code/Python/BDNYC/TDwarfplotting/HD19467B_project/plots/bestfits_w_object.pdf'
    )
    plt.clf()

    return sptlist, chilist
    chisquare = [sptlist, chilist]