Ejemplo n.º 1
0
def calibrate(source,filtername,survey="UKIDSS"):

    flag_limit = 2

    sex = sextractor.SExtractor()
    my_catalog = sex.catalog()
    g = Table(my_catalog)
    #print(g)
    
    my_catalog = g[(g['FLAGS'] < flag_limit)]
    #print(my_catalog)
    alpha = []
    delta = []
    for star in my_catalog:
        alpha.append(star['ALPHA_J2000'])
        delta.append(star['DELTA_J2000'])

    if survey == "2MASS":
        t = Table.read(os.path.join(source+"_data",source+"_2MASS_cat.vot"),format='votable')
        
        #print(t)
        if filtername == "K_1":
            twomass_magname = "k_m"
            twomass_errname = "k_msigcom"

        if filtername == "H":
            twomass_magname = "h_m"
            twomass_errname = "h_msigcom"

        if filtername == "J":
            twomass_magname = "j_m"
            twomass_errname = "j_msigcom"
        t = t[(t[twomass_magname] < 16) & (t[twomass_magname] > 10) & (t[twomass_errname] > 0)  & (t[twomass_errname] < 0.1)]
        idxs1, idxs2, ds = pyspherematch.spherematch(np.array(alpha),np.array(delta),np.array(t['ra']),
                        np.array(t['dec']),tol=1./3600.)
    else:
        #This might be broken now
        t = Table.read(os.path.join(source+"_data",source+"_"+survey+"_cat.fits"),type='fits')
        ukidss_filter = filtername+"AperMag3"
        ukidss_err = filtername+"AperMag3Err"
        idxs1, idxs2, ds = pyspherematch.spherematch(np.array(alpha),np.array(delta),t['RA'],
                        t['Dec'],tol=2/3600.)    
    
    my_mag = my_catalog[idxs1]['MAG_APER']
    catalog_mag = t[idxs2][twomass_magname]
    errs = np.sqrt(t[idxs2][twomass_errname]**2+my_catalog[idxs1]['MAGERR_APER']**2)
    
    
    a = np.average(np.array(my_mag)-np.array(catalog_mag),weights = np.array(errs))
    b = np.median(np.array(my_mag)-np.array(catalog_mag))
    poss_zp = np.array([a,b])
    #ii = np.argmin(np.absolute(poss_zp))
    #zp = poss_zp[ii]
    #This median just seems far more reliable
    zp = b
    
    plt.clf()
    plt.plot(my_mag,catalog_mag,'ko')
    plt.plot(my_mag-zp,catalog_mag,'ro')
    
    plt.xlabel("Raw magnitude")
    plt.ylabel("Catalog magnitude [2MASS]")
    plt.plot([11,17],[11,17],color='red')
    plt.xlim([11,18])
    plt.ylim([11,18])
    plt.savefig(os.path.join(source+"_data",source+"_zp_"+filtername+".png"))
    plt.close('all')    
    
    
    #zp = np.min(a,b)
    print(source)
    print("Possible ZP:")
    print(poss_zp)
    print(filtername)
    print("ZP: "+str(round(zp,3)))
    return(zp)
def do_phot(sex,source,survey="UKIDSS"):

    print("Doing H photometry...")

    sex.run(os.path.join(source+"_data",source+"_"+survey+"_H.fits"))
    #print(os.path.join(source+"_data",source+"_"+survey+"_trim_H.fits"))
    #Hcatalog = sex.catalog("py-sextractor.cat")
    Hcatalog = sex.catalog()


    print("Doing K photometry...")

    sex.run(os.path.join(source+"_data",source+"_"+survey+"_K.fits"))
    Kcatalog = sex.catalog()
    try:
        k_correct = determine_zp.calibrate(source,"K_1",survey="2MASS")
    except ValueError:
        print("Failed to calibrate, assuming no correction")
        k_correct = 0

    if ((k_correct > 0.5) or (k_correct < -0.5)):
        print("*** Large ZP correction for K ("+str(k_correct)+") ***")
        print("Completeness for "+source+" likely wrong")

    print("Doing J photometry...")
    sex.run(os.path.join(source+"_data",source+"_"+survey+"_J.fits"))
    Jcatalog = sex.catalog()
    try:
        j_correct = determine_zp.calibrate(source,"J",survey="2MASS")
    except IndexError:
        print("Failed to calibrate, assuming no correction")
        j_correct = 0

    Kcatalog = Table(Kcatalog)
    Jcatalog = Table(Jcatalog)
    
    Kcatalog = Kcatalog[(Kcatalog['FLAGS'] < flag_limit)]
    Jcatalog = Jcatalog[(Jcatalog['FLAGS'] < flag_limit)]
    
    #print(Kcatalog)
    idxs1, idxs2, ds = pyspherematch.spherematch(np.array(Kcatalog['ALPHA_J2000']),
                                                 np.array(Kcatalog['DELTA_J2000']),
                                                 np.array(Jcatalog['ALPHA_J2000']),
                                                 np.array(Jcatalog['DELTA_J2000']),tol=1./3600.)
    
    Kcatalog['MAG_APER'] -= k_correct
    Jcatalog['MAG_APER'] -= j_correct
    ra  = Kcatalog[idxs1]['ALPHA_J2000']
    dec = Kcatalog[idxs1]['DELTA_J2000']
    
    gc = coordinates.ICRS(ra,dec, unit=(u.degree, u.degree))
    galcoords = gc.galactic
    L = galcoords.l.degree
    B = galcoords.b.degree
    
    Jmag = Jcatalog[idxs2]['MAG_APER']
    Kmag = Kcatalog[idxs1]['MAG_APER']
    JminK = Jmag - Kmag
    
    #print(JminK)
    t = Table([ra,dec,L,B,Jmag,Kmag,JminK],names=('RA','Dec','L','B','JMag','KMag','JminK'))

    try:
        os.remove(os.path.join(source+"_data",source+"_MyCatalog_"+survey+".vot"))
    except:
        pass
    t.write(os.path.join(source+"_data",source+"_MyCatalog_"+survey+".vot"),format='votable')

    catlist = [Kcatalog,Hcatalog,Jcatalog]
    filterlist = ["K","H","J"]
    for cat,filtername in zip(catlist,filterlist):
        ra = []
        dec = []
        mag = []
        magerr = []
        flags = []
        L = []
        B = []

        for star in cat:
            ra.append(star['ALPHA_J2000'])
            dec.append(star['DELTA_J2000'])
            mag.append(star['MAG_APER'])
            magerr.append(star['MAGERR_APER'])
            flags.append(star['FLAGS'])
        t = Table([ra,dec,mag,magerr,flags],names=('RA','Dec','Mag'+filtername,'MagErr'+filtername,'Flags'+filtername))

        try:
            os.remove(os.path.join(source+"_data",source+"_MyCatalog_"+survey+"_"+filtername+".vot"))
        except:
            pass
        t.write(os.path.join(source+"_data",source+"_MyCatalog_"+survey+"_"+filtername+".vot"),format='votable')


    sex.clean(config=True,catalog=True,check=True)
    
    f = open(os.path.join(source+"_data/",source+"_zpcorr_"+survey+".pkl"),'w')
    pickle.dump(k_correct,f)
    f.close()
    
    return(k_correct)
def do_phot(sex, source, survey="UKIDSS"):

    print("Doing H photometry...")

    sex.run(os.path.join(source + "_data", source + "_" + survey + "_H.fits"))
    #print(os.path.join(source+"_data",source+"_"+survey+"_trim_H.fits"))
    #Hcatalog = sex.catalog("py-sextractor.cat")
    Hcatalog = sex.catalog()

    print("Doing K photometry...")

    sex.run(os.path.join(source + "_data", source + "_" + survey + "_K.fits"))
    Kcatalog = sex.catalog()
    try:
        k_correct = determine_zp.calibrate(source, "K_1", survey="2MASS")
    except ValueError:
        print("Failed to calibrate, assuming no correction")
        k_correct = 0

    if ((k_correct > 0.5) or (k_correct < -0.5)):
        print("*** Large ZP correction for K (" + str(k_correct) + ") ***")
        print("Completeness for " + source + " likely wrong")

    print("Doing J photometry...")
    sex.run(os.path.join(source + "_data", source + "_" + survey + "_J.fits"))
    Jcatalog = sex.catalog()
    try:
        j_correct = determine_zp.calibrate(source, "J", survey="2MASS")
    except IndexError:
        print("Failed to calibrate, assuming no correction")
        j_correct = 0

    Kcatalog = Table(Kcatalog)
    Jcatalog = Table(Jcatalog)

    Kcatalog = Kcatalog[(Kcatalog['FLAGS'] < flag_limit)]
    Jcatalog = Jcatalog[(Jcatalog['FLAGS'] < flag_limit)]

    #print(Kcatalog)
    idxs1, idxs2, ds = pyspherematch.spherematch(
        np.array(Kcatalog['ALPHA_J2000']),
        np.array(Kcatalog['DELTA_J2000']),
        np.array(Jcatalog['ALPHA_J2000']),
        np.array(Jcatalog['DELTA_J2000']),
        tol=1. / 3600.)

    Kcatalog['MAG_APER'] -= k_correct
    Jcatalog['MAG_APER'] -= j_correct
    ra = Kcatalog[idxs1]['ALPHA_J2000']
    dec = Kcatalog[idxs1]['DELTA_J2000']

    gc = coordinates.ICRS(ra, dec, unit=(u.degree, u.degree))
    galcoords = gc.galactic
    L = galcoords.l.degree
    B = galcoords.b.degree

    Jmag = Jcatalog[idxs2]['MAG_APER']
    Kmag = Kcatalog[idxs1]['MAG_APER']
    JminK = Jmag - Kmag

    #print(JminK)
    t = Table([ra, dec, L, B, Jmag, Kmag, JminK],
              names=('RA', 'Dec', 'L', 'B', 'JMag', 'KMag', 'JminK'))

    try:
        os.remove(
            os.path.join(source + "_data",
                         source + "_MyCatalog_" + survey + ".vot"))
    except:
        pass
    t.write(os.path.join(source + "_data",
                         source + "_MyCatalog_" + survey + ".vot"),
            format='votable')

    catlist = [Kcatalog, Hcatalog, Jcatalog]
    filterlist = ["K", "H", "J"]
    for cat, filtername in zip(catlist, filterlist):
        ra = []
        dec = []
        mag = []
        magerr = []
        flags = []
        L = []
        B = []

        for star in cat:
            ra.append(star['ALPHA_J2000'])
            dec.append(star['DELTA_J2000'])
            mag.append(star['MAG_APER'])
            magerr.append(star['MAGERR_APER'])
            flags.append(star['FLAGS'])
        t = Table([ra, dec, mag, magerr, flags],
                  names=('RA', 'Dec', 'Mag' + filtername,
                         'MagErr' + filtername, 'Flags' + filtername))

        try:
            os.remove(
                os.path.join(
                    source + "_data", source + "_MyCatalog_" + survey + "_" +
                    filtername + ".vot"))
        except:
            pass
        t.write(os.path.join(
            source + "_data",
            source + "_MyCatalog_" + survey + "_" + filtername + ".vot"),
                format='votable')

    sex.clean(config=True, catalog=True, check=True)

    f = open(
        os.path.join(source + "_data/", source + "_zpcorr_" + survey + ".pkl"),
        'w')
    pickle.dump(k_correct, f)
    f.close()

    return (k_correct)