Ejemplo n.º 1
0
def get_offset_center(f, plot=False, interactive=False):
    '''
    Given a fits image, returns the offset in Ra, DEC, that needs to be applied for the telescope tp go
    from the current pointing position, to the coodinates of the object specified in the fits file.
    '''
    
    if(not os.path.isfile(f)):
        print "File %s does not exist! Returning Zero offsets..."%f
        return -1, 0,0
    else:
        image = pf.open(f)
        wcs = pywcs.WCS(image[0].header)
        rra, rdec = cc.hour2deg(image[0].header['OBJRA'],image[0].header['OBJDEC'] )
        x, y = np.round(wcs.wcs_sky2pix(rra, rdec, 0), 0)
        pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]] , np.float_), 0)[0]
        dra, ddec = cc.get_offset(pra, pdec, rra, rdec)
            
        xl, yu = np.round(wcs.wcs_sky2pix(rra+90./3600, rdec-90./3600, 0), 0)
        xu, yl = np.round(wcs.wcs_sky2pix(rra-90./3600, rdec+90./3600, 0), 0)

        imageloc = image[0].data.T[xl:xu,yl:yu]

        if imageloc.shape[0]==0 or imageloc.shape[1]==0:
            logger.warn( "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution")
            logger.error("Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution")
            print "Pixels are", xl, xu, yl, yu
            try:
                code, dra, ddec = get_offset_center_failed_astro(f, plot=plot, interactive=interactive)
                return 2, dra, ddec
            except:
                return -1,0,0
        if(plot):
            plt.figure(figsize=(8,8))
            
            zmin, zmax = zscale.zscale(imageloc)

            #print zmin, zmax, imageloc, (xl,xu,yl,yu)
    
            obj = fitsutils.get_par(f, "OBJECT")
            plt.suptitle(obj, fontsize=20)
            plt.imshow(imageloc.T,  extent=(xl[0],xu[0],yl[0],yu[0]), aspect="equal", interpolation="none", origin="lower", vmin=zmin, vmax=zmax)
            plt.plot(1293., 1280., "ws", ms=7, label="Current pointing")
            plt.plot(x, y, "b*", ms=10, label="Target pointing")
            plt.gca().invert_xaxis()
            plt.legend()
            if (interactive):
                plt.show()
            else:
                plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_a.png")))
            plt.clf()


        return 0, dra, ddec
Ejemplo n.º 2
0
def get_offset_ref_pixel(f):
    
    if(not os.path.isfile(f)):
        print "File %s does not exist! Returning Zero offsets..."%f
        return 0,0
    else:
        image = pf.open(f)
        wcs = pywcs.WCS(image[0].header)
        rra, rdec = cc.hour2deg(image[0].header['OBRA'],image[0].header['OBDEC'] )
        pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]] , np.float_), 1)[0]
        dra, ddec = cc.get_offset(pra, pdec, rra, rdec)
        
        return dra, ddec
Ejemplo n.º 3
0
def plot_offset_shift(dirname):
    dras = []
    ddecs = []
    d = []
    r = []
    m = []
    for f in glob.glob(os.path.join(dirname, "*new")):
        image = pf.open(f)
        wcs = pywcs.WCS(image[0].header)
        rra, rdec = cc.hour2deg(image[0].header['RA'], image[0].header['DEC'])
        pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]], np.float_),
                                    1)[0]
        dra, ddec = cc.get_offset(pra, pdec, rra, rdec)

        if np.abs(dra) > 100 or np.abs(ddec) > 100:
            continue
        print f, image[
            0].data.shape, "(", rra, rdec, ")  vs. (", pra, pdec, ")", dra, ddec
        dras.append(dra)
        ddecs.append(ddec)
        d.append(rdec)
        r.append(rra)
        m.append(image[0].header['JD'])

    dras = np.array(dras)
    ddecs = np.array(ddecs)

    plt.scatter(dras, ddecs, c=np.array(r), cmap=matplotlib.cm.jet, s=130)
    plt.xlabel('dRA [arcsec]')
    plt.ylabel('dDEC [arcsec]')
    cb = plt.colorbar(label='RA [deg]')

    f = plt.figure()
    plt.scatter(dras, ddecs, c=np.array(d), cmap=matplotlib.cm.jet, s=120)
    plt.xlabel('dRA [arcsec]')
    plt.ylabel('dDEC [arcsec]')
    cb = plt.colorbar(label='DEC [deg]')

    f = plt.figure()
    plt.scatter(dras,
                ddecs,
                c=(np.array(m) - np.min(m)) * 24,
                cmap=matplotlib.cm.jet,
                s=130)
    plt.xlabel('dRA [arcsec]')
    plt.ylabel('dDEC [arcsec]')
    cb = plt.colorbar(label='JD [hours since first image]')

    plt.show()
Ejemplo n.º 4
0
def plot_offset_shift(dirname):
    dras = []
    ddecs = []
    d = []
    r = []
    m = []
    for f in glob.glob(os.path.join(dirname,"*new")):
        image = pf.open(f)
        wcs = pywcs.WCS(image[0].header)
        rra, rdec = cc.hour2deg(image[0].header['RA'],image[0].header['DEC'] )
        pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]] , np.float_), 1)[0]
        dra, ddec = cc.get_offset(pra, pdec, rra, rdec)
        
        if np.abs(dra) > 100 or np.abs(ddec)>100:
            continue
        print f, image[0].data.shape , "(",rra, rdec, ")  vs. (",  pra, pdec, ")", dra, ddec
        dras.append(dra)
        ddecs.append(ddec)
        d.append(rdec)
        r.append(rra)
        m.append(image[0].header['JD'])
        
    dras = np.array(dras)
    ddecs = np.array(ddecs)
    
    plt.scatter(dras, ddecs, c=np.array(r), cmap=matplotlib.cm.jet, s=130)
    plt.xlabel('dRA [arcsec]')
    plt.ylabel('dDEC [arcsec]')
    cb = plt.colorbar(label='RA [deg]')

    f = plt.figure()
    plt.scatter(dras, ddecs, c=np.array(d), cmap=matplotlib.cm.jet, s=120)
    plt.xlabel('dRA [arcsec]')
    plt.ylabel('dDEC [arcsec]')
    cb = plt.colorbar(label='DEC [deg]')

    
    f = plt.figure()
    plt.scatter(dras, ddecs, c=(np.array(m)-np.min(m))*24, cmap=matplotlib.cm.jet, s=130)
    plt.xlabel('dRA [arcsec]')
    plt.ylabel('dDEC [arcsec]')
    cb = plt.colorbar(label='JD [hours since first image]')
    
    plt.show()
Ejemplo n.º 5
0
def get_offset_center(f, plot=False, interactive=False):
    '''
    Given a fits image, returns the offset in Ra, DEC, that needs to be applied for the telescope tp go
    from the current pointing position, to the coodinates of the object specified in the fits file.
    '''

    if (not os.path.isfile(f)):
        print "File %s does not exist! Returning Zero offsets..." % f
        return -1, 0, 0
    else:
        image = pf.open(f)
        wcs = pywcs.WCS(image[0].header)
        rra, rdec = cc.hour2deg(image[0].header['OBJRA'],
                                image[0].header['OBJDEC'])
        x, y = np.round(wcs.wcs_sky2pix(rra, rdec, 0), 0)
        pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]], np.float_),
                                    0)[0]
        dra, ddec = cc.get_offset(pra, pdec, rra, rdec)

        xl, yu = np.round(
            wcs.wcs_sky2pix(rra + 90. / 3600, rdec - 90. / 3600, 0), 0)
        xu, yl = np.round(
            wcs.wcs_sky2pix(rra - 90. / 3600, rdec + 90. / 3600, 0), 0)

        imageloc = image[0].data.T[xl:xu, yl:yu]

        if imageloc.shape[0] == 0 or imageloc.shape[1] == 0:
            logger.warn(
                "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution"
            )
            logger.error(
                "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution"
            )
            print "Pixels are", xl, xu, yl, yu
            try:
                code, dra, ddec = get_offset_center_failed_astro(
                    f, plot=plot, interactive=interactive)
                return 2, dra, ddec
            except:
                return -1, 0, 0
        if (plot):
            plt.figure(figsize=(8, 8))

            zmin, zmax = zscale.zscale(imageloc)

            #print zmin, zmax, imageloc, (xl,xu,yl,yu)

            obj = fitsutils.get_par(f, "OBJECT")
            plt.suptitle(obj, fontsize=20)
            plt.imshow(imageloc.T,
                       extent=(xl[0], xu[0], yl[0], yu[0]),
                       aspect="equal",
                       interpolation="none",
                       origin="lower",
                       vmin=zmin,
                       vmax=zmax)
            plt.plot(1293., 1280., "ws", ms=7, label="Current pointing")
            plt.plot(x, y, "b*", ms=10, label="Target pointing")
            plt.gca().invert_xaxis()
            plt.legend()
            if (interactive):
                plt.show()
            else:
                plt.savefig(
                    os.path.join(
                        os.path.dirname(f).replace("raw", "phot"),
                        os.path.basename(f).replace(".fits", "_a.png")))
            plt.clf()

        return 0, dra, ddec
Ejemplo n.º 6
0
def get_offset_center_failed_astro(f, plot=False, interactive=True):
    '''
    For fields where astrometry is challenging, there is a simple solution.
    Find the brightest peak within the pointing error of the telescope.
    As this fields will usually be centered in Standard stars and very short exposure time,
    the fit intends to 
    
    '''

    image = pf.open(f)
    data = image[0].data
    wcs = pywcs.WCS(image[0].header)
    ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC'])

    pra, pdec = wcs.wcs_sky2pix(ra, dec, 0)
    #Get a local image
    #xl, yl = np.array(wcs.wcs_sky2pix(ra+(60./3600)*np.cos(np.deg2rad(dec)), dec-60./3600, 0), dtype=np.int)
    #xu, yu = np.array(wcs.wcs_sky2pix(ra-(60./3600)*np.cos(np.deg2rad(dec)), dec+60./3600, 0), dtype=np.int)
    imageloc = image[0].data.T[1293 - 150:1293 + 150, 1280 - 150:1280 + 150]

    nx = 300
    ny = 300

    def_x = np.argmax(np.sum(imageloc, axis=0))
    def_y = np.argmax(np.sum(imageloc, axis=1))

    newx = pra - nx / 2. + def_x
    newy = pdec - ny / 2. + def_y

    pra, pdec = wcs.wcs_pix2sky(np.array([[newx[0], newy[0]]], np.float_),
                                0)[0]
    dra, ddec = cc.get_offset(ra, dec, pra, pdec)

    print "Offset", dra, ddec, "Position RA,DEC", pra, pdec

    x, y, fwhmx, fwhmy, bkg, amp = fit_utils.fit_gauss(imageloc)

    if (plot):
        plt.figure(figsize=(8, 8))
        obj = fitsutils.get_par(f, "OBJECT")

        plt.suptitle(obj, fontsize=20)
        zmin, zmax = zscale.zscale(imageloc)
        plt.imshow(imageloc,
                   aspect="auto",
                   interpolation="none",
                   origin="lower",
                   vmin=zmin,
                   vmax=zmax,
                   extent=(0, +300, 0, +300))
        plt.plot(x, y, "go", ms=20, label="Centroid using gaussiuan fit.")
        plt.plot(def_x, def_y, "b*", ms=20, label="Centroid using max/min.")
        plt.plot(150, 150, "wo", ms=20, label="Initial pointing")
        plt.legend()
        '''zmin, zmax = zscale.zscale(data)
        plt.imshow(data, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax)
        plt.plot(newx, newy, "go")    
        plt.show()'''

        if (interactive):
            plt.show()
        else:
            plt.savefig(
                os.path.join(
                    os.path.dirname(f).replace("raw", "phot"),
                    os.path.basename(f).replace(".fits", "_std.png")))

        plt.clf()

    return 1, ddec, dra
Ejemplo n.º 7
0
def get_offset_center_failed_astro(f, plot=False, interactive=True):
    '''
    For fields where astrometry is challenging, there is a simple solution.
    Find the brightest peak within the pointing error of the telescope.
    As this fields will usually be centered in Standard stars and very short exposure time,
    the fit intends to 
    
    '''

    image = pf.open(f)
    data = image[0].data
    wcs = pywcs.WCS(image[0].header)
    ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC'] )

    pra, pdec = wcs.wcs_sky2pix(ra, dec, 0)
    #Get a local image
    #xl, yl = np.array(wcs.wcs_sky2pix(ra+(60./3600)*np.cos(np.deg2rad(dec)), dec-60./3600, 0), dtype=np.int)
    #xu, yu = np.array(wcs.wcs_sky2pix(ra-(60./3600)*np.cos(np.deg2rad(dec)), dec+60./3600, 0), dtype=np.int)
    imageloc = image[0].data.T[1293-150:1293+150,1280-150:1280+150]
    
    nx = 300
    ny=300
            
    def_x = np.argmax(np.sum(imageloc, axis=0))
    def_y = np.argmax(np.sum(imageloc, axis=1))
    
    newx = pra-nx/2.+def_x
    newy = pdec-ny/2.+def_y
    
    
    pra, pdec = wcs.wcs_pix2sky(np.array([[newx[0], newy[0]]] , np.float_), 0)[0]
    dra, ddec = cc.get_offset(ra, dec, pra, pdec)
    
    print "Offset", dra, ddec, "Position RA,DEC", pra, pdec

    x,y, fwhmx, fwhmy, bkg, amp = fit_utils.fit_gauss(imageloc)   
    
    if (plot):
        plt.figure(figsize=(8,8))
        obj = fitsutils.get_par(f, "OBJECT")

        plt.suptitle(obj, fontsize=20)
        zmin, zmax = zscale.zscale(imageloc)
        plt.imshow(imageloc, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax, extent=(0,+300,0,+300))
        plt.plot(x, y, "go", ms=20, label="Centroid using gaussiuan fit.")    
        plt.plot(def_x, def_y, "b*", ms=20, label="Centroid using max/min.")
        plt.plot(150,150,"wo", ms=20, label="Initial pointing")
        plt.legend()
            
        '''zmin, zmax = zscale.zscale(data)
        plt.imshow(data, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax)
        plt.plot(newx, newy, "go")    
        plt.show()'''
        
        if (interactive):
            plt.show()
        else:
            plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_std.png")))

        plt.clf()
        
    
    return 1, ddec, dra