Пример #1
0
def show_sn_on_image(nickname, imfilename,
                     datfile='candels_sn_hostphot.txt',
                     imsizearcsec=2.5, circleradarcsec=0.2,
                     color='red'):
    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)

    cwd = os.path.abspath('.')
    sndir = os.path.join(cwd, nickname.lower())
    ax = pl.gca()
    combofile = os.path.join(sndir, imfilename )
    combodat = pyfits.getdata(combofile)
    xsn, ysn = hstphot.radec2xy(combofile, raDD, decDD)
    pixscale = imageops.getpixscale(combofile)
    imsizepix = imsizearcsec / pixscale
    halfimsizepix = round(imsizepix/2)
    imdatcenter = combodat[
                  int(ysn-halfimsizepix):int(ysn+halfimsizepix),
                  int(xsn-halfimsizepix):int(xsn+halfimsizepix)]

    ax.imshow(imdatcenter, aspect='equal', interpolation='nearest',
              vmin=-0.05, vmax=0.15, cmap=cm.Greys_r)
    circleradpix = circleradarcsec / pixscale
    c = Circle((halfimsizepix - 1 + (xsn % 1), halfimsizepix - 1 + (ysn % 1)),
                radius=circleradpix, edgecolor=color, facecolor='None')
    ax.add_patch(c)
    ax.set_xticks([])
    ax.set_yticks([])
    return
Пример #2
0
def show_sn_on_image(nickname,
                     imfilename,
                     datfile='candels_sn_hostphot.txt',
                     imsizearcsec=2.5,
                     circleradarcsec=0.2,
                     color='red'):
    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)

    cwd = os.path.abspath('.')
    sndir = os.path.join(cwd, nickname.lower())
    ax = pl.gca()
    combofile = os.path.join(sndir, imfilename)
    combodat = pyfits.getdata(combofile)
    xsn, ysn = hstphot.radec2xy(combofile, raDD, decDD)
    pixscale = imageops.getpixscale(combofile)
    imsizepix = imsizearcsec / pixscale
    halfimsizepix = round(imsizepix / 2)
    imdatcenter = combodat[int(ysn - halfimsizepix):int(ysn + halfimsizepix),
                           int(xsn - halfimsizepix):int(xsn + halfimsizepix)]

    ax.imshow(imdatcenter,
              aspect='equal',
              interpolation='nearest',
              vmin=-0.05,
              vmax=0.15,
              cmap=cm.Greys_r)
    circleradpix = circleradarcsec / pixscale
    c = Circle((halfimsizepix - 1 + (xsn % 1), halfimsizepix - 1 + (ysn % 1)),
               radius=circleradpix,
               edgecolor=color,
               facecolor='None')
    ax.add_patch(c)
    ax.set_xticks([])
    ax.set_yticks([])
    return
Пример #3
0
def measure_host_photometry(nickname, datfile='candels_sn_hostphot.txt',
                            imsizearcsec=2.5, aparcsec=0.2, verbose=True):
    """ measure the photometry of the host galaxy at the site of the SN
    :param nickname:
    :param verbose:
    :return:  host galaxy surface brightness in a 0.2" radius aperture
    """
    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]

    cwd = os.path.abspath('.')
    sndir = os.path.join(cwd, nickname.lower())
    if not os.path.isdir(sndir):
        raise exceptions.RuntimeError("No directory %s" % sndir)
    hostimlist = glob.glob(sndir+"/%s*e00_reg_dr?_sci.fits" % nickname.lower())

    if verbose :
        fig = pl.figure(2, figsize=[12, 4])
        fig.subplots_adjust(left=0.03, bottom=0.03, right=0.97, top=0.97)
        fig.clf()

    raDD, decDD = convert_hmsdms_to_deg(raHMS, decDMS)
    iax = 0
    sbdict = {}
    sberrdict = {}
    hstphotoutstr = ''
    for hostim in hostimlist:
        imname = os.path.basename(hostim)
        imnameparts = imname.split('_')
        filtername = imnameparts[1]
        if filtername.startswith('~'):
            continue

        # Measure the surface brightness at the SN position
        x,y = hstphot.radec2xy(hostim, raDD, decDD)
        hstphotout = hstphot.dophot(hostim, x, y, aparcsec=aparcsec,
                                    system='AB', printstyle='short',
                                    recenter=False)
        mag = float(hstphotout[0].split()[5])
        magerr = float(hstphotout[0].split()[6])
        surfacebrightness = mag + 2.5*np.log10(np.pi*aparcsec**2)
        hstphotoutstr += hstphotout[0] +'\n'
        sbdict[filtername] = surfacebrightness
        sberrdict[filtername] = magerr
        iax += 1

        # show the host galaxy image with the SN position highlighted
        if verbose:
            ax = fig.add_subplot(1, len(hostimlist)+1, iax)
            show_sn_on_image(nickname, imname, datfile=datfile,
                             imsizearcsec=imsizearcsec, circleradarcsec=0.2,
                             color='red')
            epoch = imnameparts[2]
            ax.text(0.0, 1.2, '%i: %s \n%s' % (iax-1, filtername, epoch),
                    transform=ax.transAxes, ha='left', va='bottom')
        continue
    if verbose:
        print hstphotoutstr
    ax = fig.add_subplot(1, len(hostimlist)+1, len(hostimlist)+1)
    combofile = '%s_composite_sub_masked.fits' % nickname.lower()
    show_sn_on_image(nickname, combofile, datfile=datfile,
                     imsizearcsec=imsizearcsec, circleradarcsec=0.2)
    fig.suptitle('Host Images for %s' % nickname)
    pl.draw()
    raw_input("Showing %s.   Hit return to continue." % nickname)
    return hstphotoutstr, sbdict, sberrdict
Пример #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
Пример #5
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
Пример #6
0
def measure_host_photometry(nickname,
                            datfile='candels_sn_hostphot.txt',
                            imsizearcsec=2.5,
                            aparcsec=0.2,
                            verbose=True):
    """ measure the photometry of the host galaxy at the site of the SN
    :param nickname:
    :param verbose:
    :return:  host galaxy surface brightness in a 0.2" radius aperture
    """
    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]

    cwd = os.path.abspath('.')
    sndir = os.path.join(cwd, nickname.lower())
    if not os.path.isdir(sndir):
        raise exceptions.RuntimeError("No directory %s" % sndir)
    hostimlist = glob.glob(sndir +
                           "/%s*e00_reg_dr?_sci.fits" % nickname.lower())

    if verbose:
        fig = pl.figure(2, figsize=[12, 4])
        fig.subplots_adjust(left=0.03, bottom=0.03, right=0.97, top=0.97)
        fig.clf()

    raDD, decDD = convert_hmsdms_to_deg(raHMS, decDMS)
    iax = 0
    sbdict = {}
    sberrdict = {}
    hstphotoutstr = ''
    for hostim in hostimlist:
        imname = os.path.basename(hostim)
        imnameparts = imname.split('_')
        filtername = imnameparts[1]
        if filtername.startswith('~'):
            continue

        # Measure the surface brightness at the SN position
        x, y = hstphot.radec2xy(hostim, raDD, decDD)
        hstphotout = hstphot.dophot(hostim,
                                    x,
                                    y,
                                    aparcsec=aparcsec,
                                    system='AB',
                                    printstyle='short',
                                    recenter=False)
        mag = float(hstphotout[0].split()[5])
        magerr = float(hstphotout[0].split()[6])
        surfacebrightness = mag + 2.5 * np.log10(np.pi * aparcsec**2)
        hstphotoutstr += hstphotout[0] + '\n'
        sbdict[filtername] = surfacebrightness
        sberrdict[filtername] = magerr
        iax += 1

        # show the host galaxy image with the SN position highlighted
        if verbose:
            ax = fig.add_subplot(1, len(hostimlist) + 1, iax)
            show_sn_on_image(nickname,
                             imname,
                             datfile=datfile,
                             imsizearcsec=imsizearcsec,
                             circleradarcsec=0.2,
                             color='red')
            epoch = imnameparts[2]
            ax.text(0.0,
                    1.2,
                    '%i: %s \n%s' % (iax - 1, filtername, epoch),
                    transform=ax.transAxes,
                    ha='left',
                    va='bottom')
        continue
    if verbose:
        print hstphotoutstr
    ax = fig.add_subplot(1, len(hostimlist) + 1, len(hostimlist) + 1)
    combofile = '%s_composite_sub_masked.fits' % nickname.lower()
    show_sn_on_image(nickname,
                     combofile,
                     datfile=datfile,
                     imsizearcsec=imsizearcsec,
                     circleradarcsec=0.2)
    fig.suptitle('Host Images for %s' % nickname)
    pl.draw()
    raw_input("Showing %s.   Hit return to continue." % nickname)
    return hstphotoutstr, sbdict, sberrdict