Exemple #1
0
def getxycenter(image,
                x,
                y,
                ext=0,
                radec=False,
                fitsconvention=False,
                verbose=False):
    """ Use a gaussian centroid algorithm to locate the center of a star
    near position x,y.

    :param image: any valid input to get_header_and_data(), namely:
      a string giving a fits filename, a pyfits hdulist or hdu, a pyfits
      primaryhdu object, a tuple or list giving [hdr,data]
    :param ext: (optional) extension number for science array
    :param radec: input coordinates are in RA,Dec instead of pixels
    :param fitsconvention: if True, then the input and output x,y pixel
        positions use the fits pixel indexing convention, with the center of
        the lower left pixel at (1,1). Otherwise this function uses the
        numpy/scipy convention which sets the center of the lower left pixel
        at (0,0).
    :param verbose: report progress verbosely
    :return: string giving the filter name as it appears in the fits header.
    """
    from PythonPhot import cntrd
    from astropy.io import fits as pyfits

    if radec:
        x, y = radec2xy(image, x, y, ext=ext)
    fwhmpix = getfwhmpix(image, ext=ext)
    imdat = pyfits.getdata(image, ext=ext)
    if fitsconvention:
        x, y = x - 1, y - 1
    xc, yc = cntrd.cntrd(imdat, x, y, fwhmpix, verbose=verbose)
    if xc == -1:
        if verbose:
            print('Recentering within a 5-pixel box')
        xc, yc = cntrd.cntrd(imdat,
                             x,
                             y,
                             fwhmpix,
                             verbose=verbose,
                             extendbox=5)
        if xc == -1: raise RuntimeError('Error : centroiding failed!')
    if fitsconvention:
        xc, yc = xc + 1, yc + 1
    return xc, yc
Exemple #2
0
def getxycenter(image, x, y, ext=0, radec=False,
                fitsconvention=False, verbose=False):
    """ Use a gaussian centroid algorithm to locate the center of a star
    near position x,y.

    :param image: any valid input to get_header_and_data(), namely:
      a string giving a fits filename, a pyfits hdulist or hdu, a pyfits
      primaryhdu object, a tuple or list giving [hdr,data]
    :param ext: (optional) extension number for science array
    :param radec: input coordinates are in RA,Dec instead of pixels
    :param fitsconvention: if True, then the input and output x,y pixel
        positions use the fits pixel indexing convention, with the center of
        the lower left pixel at (1,1). Otherwise this function uses the
        numpy/scipy convention which sets the center of the lower left pixel
        at (0,0).
    :param verbose: report progress verbosely
    :return: string giving the filter name as it appears in the fits header.
    """
    from PythonPhot import cntrd
    from astropy.io import fits as pyfits

    if radec:
        x, y = radec2xy(image, x, y, ext=ext)
    fwhmpix = getfwhmpix(image, ext=ext)
    imdat = pyfits.getdata(image, ext=ext)
    if fitsconvention:
        x, y = x - 1, y - 1
    xc, yc = cntrd.cntrd(imdat, x, y, fwhmpix, verbose=verbose)
    if xc == -1:
        if verbose:
            print('Recentering within a 5-pixel box')
        xc, yc = cntrd.cntrd(imdat, x, y, fwhmpix,
                             verbose=verbose, extendbox=5)
    if fitsconvention:
        xc, yc = xc + 1, yc + 1
    return xc, yc
Exemple #3
0
def update_sn_coordinates(nickname, datfile='candels_sn_hostphot.txt',
                          verbose=True, clobber=False):
    """ display IR diff image, let the user select the epochs where the SN is
    visible, make a stack, get an updated measurement of the SN position
    :return:
    """
    cwd = os.path.abspath('.')
    sndir = os.path.join(cwd, nickname.lower())
    if not os.path.isdir(sndir):
        raise exceptions.RuntimeError("No directory %s" % sndir)
    diffimlist = glob.glob(sndir+"/*f1*sub_masked.fits")
    combofile = os.path.join(sndir, '%s_composite_sub_masked.fits' %
                             nickname.lower())
    indat = ascii.read(datfile, format='commented_header',
                       header_start=-1, data_start=0)
    nicknamelist = np.array([nick.lower() for nick in indat['nickname']])
    isn = np.where(nicknamelist==nickname)[0]
    raHMS = indat['RA_SN'][isn]
    decDMS = indat['DEC_SN'][isn]
    raDD, decDD = convert_hmsdms_to_deg(raHMS, decDMS)

    if not os.path.isfile(combofile) or clobber:
        fig = pl.figure(1, figsize=[12, 4])
        fig.subplots_adjust(left=0.03, bottom=0.03, right=0.97, top=0.97)
        if verbose:
            print("Constructing composite image %s from: %s" % (
                combofile,
                str([os.path.basename(diffim) for diffim in combinelist])))
        fig.clf()
        fig.suptitle('%s Recentering' % nickname)
        iax = 0
        if verbose:
            print "plotting diff images to select for composite image"
        for diffim in diffimlist:
            imname = os.path.basename(diffim)
            imnameparts = imname.split('_')
            filter = imnameparts[1]
            epoch = imnameparts[2]
            iax += 1

            ax = fig.add_subplot(1, len(diffimlist), iax)
            show_sn_on_image(nickname, diffim, datfile=datfile,
                             imsizearcsec=1.0, circleradarcsec=0.2)
            ax.text(0.0, 1.2, '%i: %s \n%s' % (iax, filter, epoch),
                    transform=ax.transAxes, ha='left', va='bottom')
        pl.draw()
        iwithsn = input("Recentering for %s\n" % nickname +
                        "Enter a comma-sep'd list of image numbers in which"
                        " the SN is bright enough for centroiding:"
                        "\n   ")
        combinelist = np.array(diffimlist)[list(iwithsn)]
        combofile = imageops.imaverage(combinelist, combofile, clobber=True)
    elif os.path.isfile(combofile) and not clobber:
        print "%s exists.  Not clobbering." % combofile

    fig = pl.figure(10, figsize=[4, 4])
    fig.clf()
    ax = fig.add_subplot(1, 1, 1)
    show_sn_on_image(nickname, combofile, datfile=datfile,
                     imsizearcsec=1.0, circleradarcsec=0.2, color='cyan')

    # locate the sn centroid position
    xsn, ysn = hstphot.radec2xy(combofile, raDD, decDD)
    combodat = pyfits.getdata(combofile)
    xnew, ynew = cntrd.cntrd(combodat, xsn, ysn, 5)
    xnew += 1 # convert from iraf to python-style coordinates
    ynew += 1 # convert from iraf to python-style coordinates
    ranew, decnew = hstphot.xy2radec(combofile, xnew, ynew)
    ranew = ranew[0]
    decnew = decnew[0]
    raHMSnew, decDMSnew =  convert_deg_to_hmsdms(ranew, decnew)
    oldstr = '%-15s old(cyan): %11s %-11s  %10.6f %10.6f %9.3f %9.3f'%(
        nickname.lower(), raHMS[0], decDMS[0], raDD, decDD, xsn, ysn)
    newstr = '%-15s new(red) : %11s %-11s  %10.6f %10.6f %9.3f %9.3f'%(
        nickname.lower(), raHMSnew, decDMSnew, ranew, decnew, xnew, ynew)
    print oldstr
    print newstr

    # show the new position as a red circle
    pixscale = imageops.getpixscale(combofile)
    circleradpix = 0.2 / pixscale
    imsizepix = 1.0 / pixscale
    halfimsizepix = round(imsizepix/2)
    c = Circle((halfimsizepix - 1 + int(xnew)-int(xsn) + (xnew % 1),
                halfimsizepix - 1 + int(ynew)-int(ysn) + (ynew % 1)),
                radius=circleradpix, edgecolor='red', facecolor='None')
    ax.add_patch(c)
    fig.suptitle('%s : red=new position' % nickname)
    return raHMSnew, decDMSnew, ranew, decnew
Exemple #4
0
def update_sn_coordinates(nickname,
                          datfile='candels_sn_hostphot.txt',
                          verbose=True,
                          clobber=False):
    """ display IR diff image, let the user select the epochs where the SN is
    visible, make a stack, get an updated measurement of the SN position
    :return:
    """
    cwd = os.path.abspath('.')
    sndir = os.path.join(cwd, nickname.lower())
    if not os.path.isdir(sndir):
        raise exceptions.RuntimeError("No directory %s" % sndir)
    diffimlist = glob.glob(sndir + "/*f1*sub_masked.fits")
    combofile = os.path.join(sndir,
                             '%s_composite_sub_masked.fits' % nickname.lower())
    indat = ascii.read(datfile,
                       format='commented_header',
                       header_start=-1,
                       data_start=0)
    nicknamelist = np.array([nick.lower() for nick in indat['nickname']])
    isn = np.where(nicknamelist == nickname)[0]
    raHMS = indat['RA_SN'][isn]
    decDMS = indat['DEC_SN'][isn]
    raDD, decDD = convert_hmsdms_to_deg(raHMS, decDMS)

    if not os.path.isfile(combofile) or clobber:
        fig = pl.figure(1, figsize=[12, 4])
        fig.subplots_adjust(left=0.03, bottom=0.03, right=0.97, top=0.97)
        if verbose:
            print("Constructing composite image %s from: %s" %
                  (combofile,
                   str([os.path.basename(diffim) for diffim in combinelist])))
        fig.clf()
        fig.suptitle('%s Recentering' % nickname)
        iax = 0
        if verbose:
            print "plotting diff images to select for composite image"
        for diffim in diffimlist:
            imname = os.path.basename(diffim)
            imnameparts = imname.split('_')
            filter = imnameparts[1]
            epoch = imnameparts[2]
            iax += 1

            ax = fig.add_subplot(1, len(diffimlist), iax)
            show_sn_on_image(nickname,
                             diffim,
                             datfile=datfile,
                             imsizearcsec=1.0,
                             circleradarcsec=0.2)
            ax.text(0.0,
                    1.2,
                    '%i: %s \n%s' % (iax, filter, epoch),
                    transform=ax.transAxes,
                    ha='left',
                    va='bottom')
        pl.draw()
        iwithsn = input("Recentering for %s\n" % nickname +
                        "Enter a comma-sep'd list of image numbers in which"
                        " the SN is bright enough for centroiding:"
                        "\n   ")
        combinelist = np.array(diffimlist)[list(iwithsn)]
        combofile = imageops.imaverage(combinelist, combofile, clobber=True)
    elif os.path.isfile(combofile) and not clobber:
        print "%s exists.  Not clobbering." % combofile

    fig = pl.figure(10, figsize=[4, 4])
    fig.clf()
    ax = fig.add_subplot(1, 1, 1)
    show_sn_on_image(nickname,
                     combofile,
                     datfile=datfile,
                     imsizearcsec=1.0,
                     circleradarcsec=0.2,
                     color='cyan')

    # locate the sn centroid position
    xsn, ysn = hstphot.radec2xy(combofile, raDD, decDD)
    combodat = pyfits.getdata(combofile)
    xnew, ynew = cntrd.cntrd(combodat, xsn, ysn, 5)
    xnew += 1  # convert from iraf to python-style coordinates
    ynew += 1  # convert from iraf to python-style coordinates
    ranew, decnew = hstphot.xy2radec(combofile, xnew, ynew)
    ranew = ranew[0]
    decnew = decnew[0]
    raHMSnew, decDMSnew = convert_deg_to_hmsdms(ranew, decnew)
    oldstr = '%-15s old(cyan): %11s %-11s  %10.6f %10.6f %9.3f %9.3f' % (
        nickname.lower(), raHMS[0], decDMS[0], raDD, decDD, xsn, ysn)
    newstr = '%-15s new(red) : %11s %-11s  %10.6f %10.6f %9.3f %9.3f' % (
        nickname.lower(), raHMSnew, decDMSnew, ranew, decnew, xnew, ynew)
    print oldstr
    print newstr

    # show the new position as a red circle
    pixscale = imageops.getpixscale(combofile)
    circleradpix = 0.2 / pixscale
    imsizepix = 1.0 / pixscale
    halfimsizepix = round(imsizepix / 2)
    c = Circle(
        (halfimsizepix - 1 + int(xnew) - int(xsn) +
         (xnew % 1), halfimsizepix - 1 + int(ynew) - int(ysn) + (ynew % 1)),
        radius=circleradpix,
        edgecolor='red',
        facecolor='None')
    ax.add_patch(c)
    fig.suptitle('%s : red=new position' % nickname)
    return raHMSnew, decDMSnew, ranew, decnew