예제 #1
0
파일: iqlmi.py 프로젝트: jicapone/python
def lmi_defringe(imlist, filts=["SDSS-Z"], fkey="FILTER", skybkg="SKYBKG", skysub="SKYSUB", fpfx="F"):

    """Create and apply fringe frame."""

    ims = glob.glob(imlist)
    ims.sort()

    for filt in filts:

        # Identify all the images in a given filter
        fims = []
        for im in ims:
            hdr = pyfits.open(im)
            if hdr[0].header[fkey] == filt:
                fims.append(im)

        if len(fims) == 0:
            continue

        fimlist = ""
        for fim in fims:

            # Run iqobjs to get mask
            iqpkg.iqobjs(fim, 2.0, 50000.0, wtimage="", verbose=no)

            fimlist += "%s," % fim

        iqpkg.iqfringe(fimlist[:-1], "Fringe-%s.fits" % filt, verbose=no)

        iqpkg.iqdefringe(fimlist[:-1], "Fringe-%s.fits" % filt, outpfx="f", verbose=no)

        for fim in fims:
            shutil.move("f%s" % fim, "%s%s" % (fpfx, fim[-13:]))
예제 #2
0
def lmi_stats(imlist, outf):
    '''Basic image statistics.'''

    ims = glob.glob(imlist)
    ims.sort()
    outfile = open(outf, "w")

    for im in ims:

        hdr = pyfits.open(im)
        ra = hdr[0].header["RA"]
        dec = hdr[0].header["DEC"]
        obj = hdr[0].header["OBJECT"]
        filt = hdr[0].header["FILTER"]
        exptime = hdr[0].header["EXPTIME"]
        tobs = hdr[0].header["DATE-OBS"]
        skybkg = hdr[0].header["SKYBKG"]
        skysig = hdr[0].header["SKYSIG"]
        iqpkg.iqobjs(im, 3.0, 50000.0, wtimage="", verbose=no)
        os.system("getsdss.pl -r 10.0 -f %s.reg -p %s %s %s.txt" %
                  (obj, ra, dec, obj))
        stars = iqutils.Starlist("%s.stars" % im)
        refstars = iqutils.Starlist("%s.reg" % obj)
        if len(refstars) == 0:
            continue
        refstars.wcs2pix(im)
        if not FDICT.has_key(filt):
            continue
        refstars.set_mag(FDICT[filt])
        a, b = stars.match(refstars, maxnum=1000)
        if len(a) == 0:
            continue
        fwhm = np.median(a.fwhms()) * LMIPIXSCALE
        zp, zpu = stars.zeropt(refstars, method="mean", rejout=1)
        if (zp == 0.0) or (zpu > 0.20):
            tzp = 99.0
            lmag1 = 99.0
            lmag2 = 99.0
        else:
            tzp = 25.0 + zp - 2.5 * np.log10(float(exptime))
            area = np.pi * np.power(1.2 * fwhm / LMIPIXSCALE, 2)
            lmag1 = -2.5 * np.log10(3 * np.sqrt(area) * skysig / exptime) + tzp
            lmag2 = -2.5 * np.log10(
                3 * np.sqrt(area) * skysig / np.sqrt(100.0 * exptime)) + tzp

        outfile.write(
            "%s%25s%15s%8s%8.2f%10.2f%10.2f%8.2f%10.3f%10.3f%10.3f%10.3f\n" %
            (im, tobs, obj, filt, exptime, skybkg, skysig, fwhm, tzp, zpu,
             lmag1, lmag2))

    outfile.close()
    return
예제 #3
0
파일: iqlmi.py 프로젝트: jicapone/python
def lmi_stats(imlist, outf):

    """Basic image statistics."""

    ims = glob.glob(imlist)
    ims.sort()
    outfile = open(outf, "w")

    for im in ims:

        hdr = pyfits.open(im)
        ra = hdr[0].header["RA"]
        dec = hdr[0].header["DEC"]
        obj = hdr[0].header["OBJECT"]
        filt = hdr[0].header["FILTER"]
        exptime = hdr[0].header["EXPTIME"]
        tobs = hdr[0].header["DATE-OBS"]
        skybkg = hdr[0].header["SKYBKG"]
        skysig = hdr[0].header["SKYSIG"]
        iqpkg.iqobjs(im, 3.0, 50000.0, wtimage="", verbose=no)
        os.system("getsdss.pl -r 10.0 -f %s.reg -p %s %s %s.txt" % (obj, ra, dec, obj))
        stars = iqutils.Starlist("%s.stars" % im)
        refstars = iqutils.Starlist("%s.reg" % obj)
        if len(refstars) == 0:
            continue
        refstars.wcs2pix(im)
        if not FDICT.has_key(filt):
            continue
        refstars.set_mag(FDICT[filt])
        a, b = stars.match(refstars, maxnum=1000)
        if len(a) == 0:
            continue
        fwhm = np.median(a.fwhms()) * LMIPIXSCALE
        zp, zpu = stars.zeropt(refstars, method="mean", rejout=1)
        if (zp == 0.0) or (zpu > 0.20):
            tzp = 99.0
            lmag1 = 99.0
            lmag2 = 99.0
        else:
            tzp = 25.0 + zp - 2.5 * np.log10(float(exptime))
            area = np.pi * np.power(1.2 * fwhm / LMIPIXSCALE, 2)
            lmag1 = -2.5 * np.log10(3 * np.sqrt(area) * skysig / exptime) + tzp
            lmag2 = -2.5 * np.log10(3 * np.sqrt(area) * skysig / np.sqrt(100.0 * exptime)) + tzp

        outfile.write(
            "%s%25s%15s%8s%8.2f%10.2f%10.2f%8.2f%10.3f%10.3f%10.3f%10.3f\n"
            % (im, tobs, obj, filt, exptime, skybkg, skysig, fwhm, tzp, zpu, lmag1, lmag2)
        )

    outfile.close()
    return
예제 #4
0
파일: p48utils.py 프로젝트: cenko/python
def p48coaddphot(inlis, refstars, ot, filter, outfile):

    outf = open(outfile, "w")
    
    for image in inlis:

        root = image.split(".")[0]
        
        # Get the seeing in the image
        iqpkg.iqobjs(image, SIGMA, get_head(image, "SATURATE"), skyval="0.0")
        seepix = get_head(image, "SEEPIX")

        # Calculate zeropoint
        refstars.wcs2pix(image)
        refstars.set_mag(filter)
        xyfile = open("%s.xy" % root, "w")
        for star in refstars:
            xyfile.write("%10.3f%10.3f\n" % (star.xval, star.yval))
        xyfile.close()
        iraf.phot(image, coords="%s.xy" % root, output="%s.mag" % root,
                  aperture=1.2*float(seepix), interac=no)
        stars = Starlist("%s.mag" % root)
        zp, zpu = stars.zeropt(refstars,method="mean",rejout=0)

        # Measure source
        ot.wcs2pix(image)
        coofile = open("%s.coo" % root, "w")
        coofile.write("%10.3f%10.3f\n" % (ot[0].xval, ot[0].yval))
        coofile.close()
        iraf.phot(image, coords="%s.coo" % root, output="%s.ot" % root,
                  aperture=1.2*float(seepix), calgorithm="none", interac=no)
        stars = Starlist("%s.ot" % root)

        # Just return 99 for non-detection
        if len(stars)==1:
            smag = stars[0].mag
            smagu = stars[0].magu
            mag = smag + zp
        else:
            mag = 99.0
            smagu = 99.0
        mjdobs = DateTimeFrom(get_head(image, OBSDATEKEY)).mjd
        exptime = get_head(image, EXPTIMEKEY)
        outf.write("%15.3f%10.1f%10.3f%10.3f%10.3f\n" % (mjdobs, exptime, mag, smagu, zpu))

    outf.close()
예제 #5
0
def lmi_defringe(imlist,
                 filts=["SDSS-Z"],
                 fkey="FILTER",
                 skybkg="SKYBKG",
                 skysub="SKYSUB",
                 fpfx="F"):
    '''Create and apply fringe frame.'''

    ims = glob.glob(imlist)
    ims.sort()

    for filt in filts:

        # Identify all the images in a given filter
        fims = []
        for im in ims:
            hdr = pyfits.open(im)
            if hdr[0].header[fkey] == filt:
                fims.append(im)

        if len(fims) == 0:
            continue

        fimlist = ""
        for fim in fims:

            # Run iqobjs to get mask
            iqpkg.iqobjs(fim, 2.0, 50000.0, wtimage="", verbose=no)

            fimlist += "%s," % fim

        iqpkg.iqfringe(fimlist[:-1], "Fringe-%s.fits" % filt, verbose=no)

        iqpkg.iqdefringe(fimlist[:-1],
                         "Fringe-%s.fits" % filt,
                         outpfx="f",
                         verbose=no)

        for fim in fims:
            shutil.move("f%s" % fim, "%s%s" % (fpfx, fim[-13:]))
예제 #6
0
def id_ot(inlis,
          refstars,
          tmass,
          ra,
          dec,
          radius,
          output="Cands.reg",
          sigma=3.0,
          satval=60000.0,
          wtroot="/Users/scenko/CALIB/p48/mask_C",
          pixtol=5.0):

    fchips = {}  # Stores field / chip dictionary
    center = astrocoords(ra, dec)  # Center of error region
    newcands = []  # New candidate OTs

    for image in inlis:

        # Identify all field and chip combinations
        [field, chip] = get_head(image, ["PTFFIELD", "CCDID"])
        if not fchips.has_key(field):
            fchips[field] = [chip]
        else:
            try:
                ind = fchips[field].index(chip)
            except ValueError:
                fchips[field].append(chip)

        # Also grab objects
        iqpkg.iqobjs(image,
                     sigma,
                     satval,
                     skyval="0.0",
                     wtimage="%s%02i.fits" % (wtroot, int(chip)),
                     wtcut=0.1,
                     fwhm=1.5,
                     pix=1.0)

    for (field, chips) in fchips.iteritems():

        for chip in chips:

            nonmatches = []
            for image in inlis:

                # Only look at images for the given field/chip pair
                [nfield, nchip] = get_head(image, ["PTFFIELD", "CCDID"])
                if (nfield == field) and (nchip == chip):

                    # Grab all the objects from this image that don't appear in
                    # reference star list
                    stars = Starlist(get_head(image, "STARFILE"))
                    refstars.wcs2pix(image)
                    new = stars.nomatch(refstars, tol=pixtol)

                    # Make sure the new objects fall inside error circle
                    new.pix2wcs(image)
                    new_incircle = []
                    for star in new:
                        temp = astrocoords(star.radeg(), star.dcdeg())
                        dist = temp.diff(center, degree=1)
                        if sqrt(pow(dist[0], 2) + pow(dist[1], 2)) < radius:
                            new_incircle.append(star)

                    # If first image from field/chip pair, add everything to
                    # nonmatches.  Otherwise, only add objects if they have shown
                    # up already
                    if len(nonmatches) == 0:
                        nonmatches = new_incircle
                    else:
                        s1 = Starlist(stars=new_incircle)
                        sr = Starlist(stars=nonmatches)
                        s1.wcs2pix(image)
                        sr.wcs2pix(image)
                        temp, junk = sr.match(s1, maxnum=10000, useflags=no)
                        nonmatches = temp.stars

            # When done with a field/chip pair, add candidates to list
            for star in nonmatches:
                newcands.append(star)

    # Final comparison with 2MASS
    temp = Starlist(stars=newcands)
    temp.wcs2pix(image)
    tmass.wcs2pix(image)
    result = temp.nomatch(tmass, tol=pixtol)
    result.write(output)
예제 #7
0
def iq_ratir(chip="C1", filt="i", reflist="sdss.reg"):
    '''Process RATIR data for given chip and filter'''

    # Check and see if bias frame exists.  If not, create one
    if not os.path.exists("Bias-%s.fits" % chip):

        imlist = glob.glob("[0-9]*T[0-9]*%sb.fits" % chip)
        hdr = pyfits.getheader(imlist[0])

        # Setup zerocombine
        zerocombine = iraf.ccdred.zerocombine
        zerocombine.combine = 'median'
        zerocombine.reject = 'avsigclip'
        zerocombine.ccdtype = ''
        zerocombine.process = no
        zerocombine.delete = no
        zerocombine.clobber = no
        zerocombine.scale = 'none'
        zerocombine.statsec = '*'
        zerocombine.nlow = 0
        zerocombine.nhigh = 1
        zerocombine.nkeep = 1
        zerocombine.mclip = yes
        zerocombine.lsigma = 3.0
        zerocombine.hsigma = 3.0
        zerocombine.rdnoise = 0.0
        zerocombine.gain = hdr['SOFTGAIN']
        zerocombine.snoise = 0
        zerocombine.pclip = -0.5
        zerocombine.blank = 0.0

        # Run zerocombine
        bstr = ",".join(imlist)
        zerocombine(input=bstr, output="Bias-%s.fits" % chip)

    # Bias subtract flat frames
    imlist = glob.glob("[0-9]*T[0-9]*%sf.fits" % chip)
    flis = []
    for im in imlist:
        hdr = pyfits.getheader(im)
        if (hdr["FILTER"] == filt) and (not os.path.exists("b%s" % im)):

            # Subtract bias frame
            ccdproc = iraf.ccdred.ccdproc
            ccdproc.ccdtype = ""
            ccdproc.noproc = no
            ccdproc.fixpix = no
            ccdproc.overscan = no
            ccdproc.trim = no
            ccdproc.zerocor = yes
            ccdproc.darkcor = no
            ccdproc.flatcor = no
            ccdproc.illumcor = no
            ccdproc.fringecor = no
            ccdproc.readcor = no
            ccdproc.scancor = no
            ccdproc.readaxis = 'line'
            ccdproc.fixfile = ""
            ccdproc.zero = "Bias-%s.fits" % chip
            ccdproc.interactive = no
            ccdproc.function = "legendre"
            ccdproc.order = 1
            ccdproc.sample = "*"
            ccdproc.naverage = 1
            ccdproc.niterate = 1
            ccdproc.low_reject = 3.0
            ccdproc.high_reject = 3.0
            ccdproc.grow = 0
            ccdproc(images=im, output="b%s" % im)

            flis.append("b%s" % im)
        elif (hdr["FILTER"] == filt):
            flis.append("b%s" % im)

    # Create flat field
    if not os.path.exists("Flat-%s-%s.fits" % (chip, filt)):

        glis = []
        for im in flis:
            iraf.iterstat.nsigrej = 5.0
            iraf.iterstat.maxiter = 10
            iraf.iterstat.verbose = globver
            iraf.iterstat.lower = INDEF
            iraf.iterstat.upper = INDEF
            iraf.iterstat(im)
            if (iraf.iterstat.median <
                    RATIRSAT[chip]) and (iraf.iterstat.median > 1000.0):
                glis.append(im)

        # Set up flatcombine
        flatcombine = iraf.ccdred.flatcombine
        flatcombine.combine = 'median'
        flatcombine.reject = 'avsigclip'
        flatcombine.ccdtype = ''
        flatcombine.scale = 'median'
        flatcombine.statsec = ''
        flatcombine.nlow = 1
        flatcombine.nhigh = 1
        flatcombine.nkeep = 1
        flatcombine.mclip = yes
        flatcombine.lsigma = 3.0
        flatcombine.hsigma = 3.0
        flatcombine.rdnoise = 0.0
        flatcombine.gain = hdr["SOFTGAIN"]
        flatcombine.snoise = 0.0
        flatcombine.pclip = -0.5

        # Run flatcombine
        fstr = ",".join(glis[:10])
        flatcombine(fstr, output="Flat-%s-%s.fits" % (chip, filt))

        # Normalize
        iraf.iterstat.nsigrej = 5.0
        iraf.iterstat.maxiter = 10
        iraf.iterstat.verbose = globver
        iraf.iterstat.lower = INDEF
        iraf.iterstat.upper = INDEF
        iraf.iterstat("Flat-%s-%s.fits" % (chip, filt))
        iraf.imarith("Flat-%s-%s.fits" % (chip, filt), "/",
                     iraf.iterstat.median, "Flat-%s-%s.fits" % (chip, filt))

    # Bias subtract and flat-field science images
    imlist = glob.glob("[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in imlist:
        hdr = pyfits.getheader(im)
        if (hdr["FILTER"] == filt) and (not os.path.exists("fb%s" % im)):

            hdr = pyfits.getheader(im)

            # Subtract bias frame
            ccdproc = iraf.ccdred.ccdproc
            ccdproc.ccdtype = ""
            ccdproc.noproc = no
            ccdproc.fixpix = no
            ccdproc.overscan = no
            ccdproc.trim = no
            ccdproc.zerocor = yes
            ccdproc.darkcor = no
            ccdproc.flatcor = yes
            ccdproc.illumcor = no
            ccdproc.fringecor = no
            ccdproc.readcor = no
            ccdproc.scancor = no
            ccdproc.readaxis = 'line'
            ccdproc.fixfile = ""
            ccdproc.zero = "Bias-%s.fits" % chip
            ccdproc.flat = "Flat-%s-%s.fits" % (chip, filt)
            ccdproc.interactive = no
            ccdproc.function = "legendre"
            ccdproc.order = 1
            ccdproc.sample = "*"
            ccdproc.naverage = 1
            ccdproc.niterate = 1
            ccdproc.low_reject = 3.0
            ccdproc.high_reject = 3.0
            ccdproc.grow = 0
            ccdproc(images=im, output="fb%s" % im)

    # Create sky frame (including dark current!)
    if not os.path.exists("Sky-%s-%s.fits" % (chip, filt)):
        imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
        hdr = pyfits.getheader(imlist[0])
        slis = []
        for im in imlist:
            if (hdr["FILTER"] == filt):
                slis.append(im)

        # Set up combine
        imcombine = iraf.immatch.imcombine
        imcombine.headers = ""
        imcombine.bpmasks = ""
        imcombine.rejmasks = ""
        imcombine.nrejmasks = ""
        imcombine.expmasks = ""
        imcombine.sigmas = ""
        imcombine.combine = "median"
        imcombine.reject = "avsigclip"
        imcombine.project = no
        imcombine.outtype = "real"
        imcombine.offsets = "none"
        imcombine.masktype = "none"
        imcombine.scale = "median"
        imcombine.zero = "none"
        imcombine.weight = "none"
        imcombine.statsec = ""
        imcombine.lsigma = 3.0
        imcombine.hsigma = 3.0
        imcombine.rdnoise = 0.0
        imcombine.gain = hdr["SOFTGAIN"]

        sstr = ",".join(slis[:10])
        imcombine(sstr, "Sky-%s-%s.fits" % (chip, filt))

        # Normalize
        iraf.iterstat("Sky-%s-%s.fits" % (chip, filt))
        iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "/",
                     iraf.iterstat.median, "Sky-%s-%s.fits" % (chip, filt))

    # Subtract sky frame (and dark!)
    imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in imlist:
        if not os.path.exists("s%s" % im):

            iraf.iterstat(im)
            iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "*",
                         iraf.iterstat.median, "temp.fits")
            iraf.imarith(im, "-", "temp.fits", "s%s" % im)
            os.remove("temp.fits")
            fimg = pyfits.open("s%s" % im, "update")
            fimg[0].header["SKYMED"] = iraf.iterstat.median
            fimg[0].header["SKYSIG"] = iraf.iterstat.sigma
            fimg[0].header["SKYSUB"] = 1
            fimg.flush()

    # Remove cosmic rays
    imlist = glob.glob("sfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in imlist:
        if not os.path.exists("c%s" % im):
            fimg = pyfits.open(im)
            iraf.lacos_im(im,
                          "c%s" % im,
                          "c%s.mask.fits" % im[:-5],
                          gain=hdr['SOFTGAIN'],
                          readn=0.0,
                          statsec="*,*",
                          skyval=fimg[0].header["SKYMED"],
                          sigclip=4.5,
                          sigfrac=0.5,
                          objlim=1.0,
                          niter=3,
                          verbose=no)

    # Add WCS keywords
    clis = glob.glob("csfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in clis:

        fimg = pyfits.open(im, mode='update')
        if fimg[0].header.get("RA") == None:

            ra0 = fimg[0].header["STRCURA"]
            dec0 = fimg[0].header["STRCUDE"]
            nax = [fimg[0].header["NAXIS1"], fimg[0].header["NAXIS2"]]
            fimg[0].header["RA"] = ra0
            fimg[0].header["DEC"] = dec0
            fimg[0].header["PIXSCALE"] = RATIRPIXSCALE[chip]
            fimg[0].header["PIXSCAL1"] = RATIRPIXSCALE[chip]
            fimg[0].header["PIXSCAL2"] = RATIRPIXSCALE[chip]
            fimg[0].header["CTYPE1"] = "RA---TAN"
            fimg[0].header["CTYPE2"] = "DEC--TAN"
            fimg[0].header["WCSDIM"] = 2
            fimg[0].header["WAT0_001"] = "system=image"
            fimg[0].header["WAT1_001"] = "wtype=tan axtype=ra"
            fimg[0].header["WAT2_001"] = "wtype=tan axtype=dec"
            fimg[0].header["LTM1_1"] = 1.0
            fimg[0].header["LTM2_2"] = 1.0
            fimg[0].header["CRPIX1"] = nax[0] / 2.0
            fimg[0].header["CRPIX2"] = nax[1] / 2.0
            fimg[0].header["CRVAL1"] = ra0
            fimg[0].header["CRVAL2"] = dec0
            fimg[0].header["CD1_1"] = -RATIRPIXSCALE[chip] / 3600.0
            fimg[0].header["CD1_2"] = 0.0
            fimg[0].header["CD2_1"] = 0.0
            fimg[0].header["CD2_2"] = RATIRPIXSCALE[chip] / 3600.0

        fimg.flush()

    # Crude astrometry
    for im in clis:

        if not os.path.exists("a%s" % im):
            os.system("python %s %s" % (PYASTROM, im))

    # Refine Astrometry
    o1 = open("daofind.param", "w")
    o1.write(
        "NUMBER\nXWIN_IMAGE\nYWIN_IMAGE\nMAG_AUTO\nFLAGS\nA_IMAGE\nB_IMAGE\n")
    o1.write("ELONGATION\nFWHM_IMAGE\nXWIN_WORLD\nYWIN_WORLD\n")
    o1.write(
        "ERRAWIN_IMAGE\nERRBWIN_IMAGE\nERRTHETAWIN_IMAGE\nERRAWIN_WORLD\n")
    o1.write(
        "ERRBWIN_WORLD\nERRTHETAWIN_WORLD\nFLUX_AUTO\nFLUX_RADIUS\nFLUXERR_AUTO"
    )
    o1.close()

    o2 = open("default.conv", "w")
    o2.write(
        "CONV NORM\n# 5x5 convolution mask of a gaussian PSF with FWHM = 3.0 pixels.\n0.092163 0.221178 0.296069 0.221178 0.092163\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.296069 0.710525 0.951108 0.710525 0.296069\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.092163 0.221178 0.296069 0.221178 0.092163"
    )
    o2.close()

    alis = glob.glob("acsfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in alis:

        # Detect sources
        os.system(
            "sex -CATALOG_NAME %s.cat -CATALOG_TYPE FITS_LDAC -PARAMETERS_NAME daofind.param -DETECT_THRESH 2.0 -ANALYSIS_THRESH 2.0 -GAIN_KEY SOFTGAIN -PIXEL_SCALE 0 %s"
            % (im[:-5], im))

    # Run scamp for alignment
    imlist = " ".join(alis)
    os.system(
        "scamp -ASTREF_CATALOG SDSS-R7 -DISTORTDEG 1 -SOLVE_PHOTOM N -SN_THRESHOLDS 3.0,10.0 -CHECKPLOT_DEV NULL %s"
        % imlist.replace(".fits", ".cat"))

    # Update header
    for im in alis:
        os.system("missfits %s" % im)
        os.system("rm %s.head %s.back" % (im[:-5], im))

    # Find good images for coadd
    slis = []
    for im in alis:
        hdr = pyfits.getheader(im)
        rms1 = hdr["ASTRRMS1"]
        rms2 = hdr["ASTRRMS2"]
        if (rms1 < 1.0e-4) and (rms1 > 5.0e-6) and (rms2 < 1.0e-4) and (
                rms2 > 5.0e-6):
            slis.append(im)

    # Initial (unweighted) coadd
    sstr = " ".join(slis)
    os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)

    # Identify sources in the coadded frame
    hdr = pyfits.getheader("coadd.fits")
    iqpkg.iqobjs("coadd.fits",
                 10.0,
                 RATIRSAT[chip],
                 skyval="0.0",
                 pix=RATIRPIXSCALE[chip],
                 gain=hdr["GAIN"],
                 aperture=20.0,
                 wtimage="coadd.weight.fits")
    hdr = pyfits.getheader("coadd.fits")
    cpsfdiam = 1.34 * float(hdr["SEEPIX"])
    iqpkg.iqobjs("coadd.fits",
                 10.0,
                 RATIRSAT[chip],
                 skyval="0.0",
                 pix=RATIRPIXSCALE[chip],
                 gain=hdr["GAIN"],
                 aperture=cpsfdiam,
                 wtimage="coadd.weight.fits")

    refstars1 = Starlist("coadd.fits.stars")
    refstars1.pix2wcs("coadd.fits")
    truemags = np.array(refstars1.mags())
    maglist = []
    errlist = []

    # (Relative) Zeropoints for individual images
    for im in slis:
        hdr = pyfits.getheader(im)
        iqpkg.iqobjs(im,
                     3.0,
                     RATIRSAT[chip],
                     skyval="!SKYMED",
                     pix=RATIRPIXSCALE[chip],
                     gain=hdr["SOFTGAIN"],
                     aperture=20.0)
        hdr = pyfits.getheader(im)
        psfdiam = 1.34 * float(hdr["SEEPIX"])
        iqpkg.iqobjs(im,
                     3.0,
                     RATIRSAT[chip],
                     skyval="!SKYMED",
                     pix=RATIRPIXSCALE[chip],
                     gain=hdr["SOFTGAIN"],
                     aperture=psfdiam)
        stars = Starlist("%s.stars" % im)
        refstars1.wcs2pix(im)
        a, b = stars.match(refstars1, tol=10.0, maxnum=1000)
        newmags = np.zeros(len(refstars1))
        newwts = np.zeros(len(refstars1))
        for i in range(len(refstars1)):
            for j in range(len(a)):
                if b[j].mag == truemags[i]:
                    newmags[i] = a[j].mag
                    newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01), 2)
                    continue
        maglist.append(newmags)
        errlist.append(newwts)

    obsmags = np.array(maglist)
    wts = np.array(errlist)

    [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)

    # Update headers
    medzp = np.median(zpts)
    for i in range(len(slis)):
        im = slis[i]
        fimg = pyfits.open(im, mode="update")
        fimg[0].header["RELZPT"] = zpts[i]
        fimg[0].header["RELZPTSC"] = scatt[i]
        fimg[0].header["RELZPRMS"] = rms[i]
        fimg[0].header["FLXSCALE"] = 1.0 / np.power(10,
                                                    (zpts[i] - medzp) / 2.5)
        fimg.flush()

    # Final coadd
    os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)

    # Calibration
    iqpkg.iqobjs("coadd.fits",
                 10.0,
                 RATIRSAT[chip],
                 skyval="0.0",
                 pix=RATIRPIXSCALE[chip],
                 gain=hdr["GAIN"],
                 aperture=cpsfdiam,
                 wtimage="coadd.weight.fits")
    stars = Starlist("coadd.fits.stars")
    refstars = Starlist(reflist)
    refstars.wcs2pix("coadd.fits")
    refstars.set_mag("%sMAG" % filt.upper())
    truemags = np.array(refstars.mags())
    maglist = []
    errlist = []

    a, b = stars.match(refstars, tol=10.0, maxnum=1000)
    newmags = np.zeros(len(refstars))
    newwts = np.zeros(len(refstars))
    for i in range(len(refstars)):
        for j in range(len(a)):
            if b[j].mag == truemags[i]:
                newmags[i] = a[j].mag
                newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01), 2)
                continue
    maglist.append(newmags)
    errlist.append(newwts)

    obsmags = np.array(maglist)
    wts = np.array(errlist)

    [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)
    fimg = pyfits.open("coadd.fits", mode="update")
    fimg[0].header["ABSZPT"] = zpts[0] + 25
    fimg[0].header["ABSZPTSC"] = scatt[0]
    fimg[0].header["ABZPRMS"] = rms[0]

    print "Zeropoint for coadded image: %.3f" % (25.0 + zpts[0])
    print "Robust scatter for coadded image: %.3f" % scatt[0]
    print "RMS for coadded image: %.3f" % rms[0]
예제 #8
0
def psfphot(image,
            clobber=globclob,
            verbose=globver,
            pixtol=3.0,
            maxnpsf=5,
            interact=yes):
    """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and  
        also on a set of comparison stars, using daophot.  simultaneously 
        perform aperture photometry on all the comparison stars (after 
        subtracting off contributions from neighbors) to enable absolute 
        photometry by comparison to aperture photometry of standard stars 
        observed in other fields """

    # Defaults / constants
    psfmult = 5.0  #standard factor (multiplied by fwhm to get psfradius)
    psfmultsmall = 3.0  #similar to psfmult, adjusted for nstar and substar

    # Necessary package
    iraf.imutil()

    # Detect stars
    iqpkg.iqobjs(image, 3.0, 50000.0, wtimage="", skyval="!MEDSKY")

    root = image[:-5]
    [gain, rnoise, fwhm] = get_head(image, ["GAIN", "READNOI", "SEEPIX"])
    fwhm = float(fwhm)
    rnoise = float(rnoise)

    iraf.iterstat(image)

    # Saturation level
    if not check_head(image, "SATURATE"):
        saturate = 60000.0
    else:
        saturate = get_head(image, "SATURATE")

    # Update datapars and daopars
    iraf.datapars.fwhmpsf = fwhm
    iraf.datapars.sigma = iraf.iterstat.sigma
    iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma
    iraf.datapars.datamax = 70000.0
    iraf.datapars.readnoise = rnoise
    iraf.datapars.epadu = gain
    iraf.daopars.psfrad = psfmult * fwhm
    iraf.daopars.fitrad = fwhm
    iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1"

    # coo file
    stars = Starlist("%s.stars" % image)
    outf = open("%s.coo.1" % image[:-5], "w")
    for star in stars:
        outf.write("%10.3f%10.3f\n" % (star.xval, star.yval))
    outf.close()

    #initial photometry
    iraf.daophot.phot(root,
                      'default',
                      'default',
                      aperture=fwhm,
                      verify=no,
                      verbose=verbose)

    iraf.datapars.datamax = 30000.0
    iraf.pstselect(root,
                   'default',
                   'default',
                   maxnpsf,
                   interactive=yes,
                   verify=no,
                   verbose=verbose)

    iraf.psf(root,
             'default',
             'default',
             'default',
             'default',
             'default',
             interactive=interact,
             verify=no,
             verbose=verbose)

    iraf.allstar(root,
                 'default',
                 'default',
                 'default',
                 'default',
                 'default',
                 verify=no,
                 verbose=verbose)

    iraf.iterstat("%s.sub.fits" % root)

    iraf.datapars.sigma = iraf.iterstat.sigma
    iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma

    iraf.datapars.datamax = 70000.0
    iraf.daophot.phot("%s.sub.fits" % root,
                      "SN.coo",
                      'default',
                      'default',
                      aperture=fwhm,
                      verify=no,
                      verbose=verbose)

    iraf.datapars.datamax = 30000.0
    iraf.daopars.fitrad = fwhm * 2.0
    iraf.allstar("%s.sub.fits" % root,
                 'default',
                 "%s.psf.1.fits" % root,
                 'default',
                 'default',
                 'default',
                 verify=no,
                 verbose=no)
예제 #9
0
파일: id_ot.py 프로젝트: cenko/python
def id_ot(inlis, refstars, tmass, ra, dec, radius, output="Cands.reg", 
          sigma=3.0, satval=60000.0, wtroot="/Users/scenko/CALIB/p48/mask_C", 
          pixtol=5.0):

     fchips = {}     # Stores field / chip dictionary 
     center = astrocoords(ra, dec)     # Center of error region
     newcands = []     # New candidate OTs
     
     for image in inlis:
         
         # Identify all field and chip combinations
         [field, chip] = get_head(image, ["PTFFIELD", "CCDID"])
         if not fchips.has_key(field):
             fchips[field] = [chip]
         else:
             try:
                 ind = fchips[field].index(chip)
             except ValueError:
                 fchips[field].append(chip)

         # Also grab objects
         iqpkg.iqobjs(image, sigma, satval, skyval="0.0", wtimage="%s%02i.fits" %
                (wtroot, int(chip)), wtcut=0.1, fwhm=1.5, pix=1.0)

     for (field, chips) in fchips.iteritems():

         for chip in chips:

             nonmatches = []
             for image in inlis:

                 # Only look at images for the given field/chip pair
                 [nfield, nchip] = get_head(image, ["PTFFIELD", "CCDID"])
                 if (nfield == field) and (nchip == chip):

                     # Grab all the objects from this image that don't appear in
                     # reference star list
                     stars = Starlist(get_head(image, "STARFILE"))
                     refstars.wcs2pix(image)
                     new = stars.nomatch(refstars, tol=pixtol)

                     # Make sure the new objects fall inside error circle
                     new.pix2wcs(image)
                     new_incircle = []
                     for star in new:
                         temp = astrocoords(star.radeg(), star.dcdeg())
                         dist = temp.diff(center, degree=1)
                         if sqrt(pow(dist[0],2) + pow(dist[1],2)) < radius:
                             new_incircle.append(star)

                     # If first image from field/chip pair, add everything to
                     # nonmatches.  Otherwise, only add objects if they have shown
                     # up already
                     if len(nonmatches) == 0:
                         nonmatches = new_incircle
                     else:
                         s1 = Starlist(stars=new_incircle)
                         sr = Starlist(stars=nonmatches)
                         s1.wcs2pix(image)
                         sr.wcs2pix(image)
                         temp, junk = sr.match(s1, maxnum=10000, useflags=no)
                         nonmatches = temp.stars

             # When done with a field/chip pair, add candidates to list
             for star in nonmatches:
                 newcands.append(star)

     # Final comparison with 2MASS 
     temp = Starlist(stars=newcands)
     temp.wcs2pix(image)
     tmass.wcs2pix(image)
     result = temp.nomatch(tmass, tol=pixtol)
     result.write(output)
예제 #10
0
def iq_ratir(chip="C1", filt="i", reflist="sdss.reg"):

	'''Process RATIR data for given chip and filter'''
	
	# Check and see if bias frame exists.  If not, create one
	if not os.path.exists("Bias-%s.fits" % chip):
	
		imlist = glob.glob("[0-9]*T[0-9]*%sb.fits" % chip)
		hdr = pyfits.getheader(imlist[0])
		
		# Setup zerocombine
		zerocombine = iraf.ccdred.zerocombine
		zerocombine.combine = 'median'
		zerocombine.reject = 'avsigclip'
		zerocombine.ccdtype = ''
		zerocombine.process = no
		zerocombine.delete = no
		zerocombine.clobber = no
		zerocombine.scale = 'none'
		zerocombine.statsec = '*'
		zerocombine.nlow = 0
		zerocombine.nhigh = 1
		zerocombine.nkeep = 1
		zerocombine.mclip = yes
		zerocombine.lsigma = 3.0
		zerocombine.hsigma = 3.0
		zerocombine.rdnoise = 0.0
		zerocombine.gain = hdr['SOFTGAIN']
		zerocombine.snoise = 0
		zerocombine.pclip = -0.5
		zerocombine.blank = 0.0
		
		# Run zerocombine
		bstr = ",".join(imlist)
		zerocombine(input=bstr, output="Bias-%s.fits" % chip)
		
	# Bias subtract flat frames
	imlist = glob.glob("[0-9]*T[0-9]*%sf.fits" % chip)
	flis = []
	for im in imlist:
		hdr = pyfits.getheader(im)
		if (hdr["FILTER"] == filt) and (not os.path.exists("b%s" % im)):
			
			# Subtract bias frame
			ccdproc = iraf.ccdred.ccdproc	
			ccdproc.ccdtype = ""
			ccdproc.noproc = no
			ccdproc.fixpix = no
			ccdproc.overscan = no
			ccdproc.trim = no
			ccdproc.zerocor = yes
			ccdproc.darkcor = no
			ccdproc.flatcor = no
			ccdproc.illumcor = no
			ccdproc.fringecor = no
			ccdproc.readcor = no
			ccdproc.scancor = no
			ccdproc.readaxis = 'line'
			ccdproc.fixfile = ""
			ccdproc.zero = "Bias-%s.fits" % chip
			ccdproc.interactive = no
			ccdproc.function = "legendre"
			ccdproc.order = 1
			ccdproc.sample = "*"
			ccdproc.naverage = 1
			ccdproc.niterate = 1
			ccdproc.low_reject = 3.0
			ccdproc.high_reject = 3.0
			ccdproc.grow = 0
			ccdproc(images=im, output="b%s" % im)
				
			flis.append("b%s" % im)
		elif (hdr["FILTER"] == filt):
			flis.append("b%s" % im)

	# Create flat field
	if not os.path.exists("Flat-%s-%s.fits" % (chip, filt)):
	
		glis = []
		for im in flis:
			iraf.iterstat.nsigrej = 5.0
			iraf.iterstat.maxiter = 10
			iraf.iterstat.verbose = globver
			iraf.iterstat.lower = INDEF
			iraf.iterstat.upper = INDEF
			iraf.iterstat(im)
			if (iraf.iterstat.median < RATIRSAT[chip]) and (iraf.iterstat.median > 1000.0):
				glis.append(im)
		     	
		# Set up flatcombine
		flatcombine = iraf.ccdred.flatcombine
		flatcombine.combine = 'median'
		flatcombine.reject = 'avsigclip'
		flatcombine.ccdtype = ''
		flatcombine.scale = 'median'
		flatcombine.statsec = ''
		flatcombine.nlow = 1
		flatcombine.nhigh = 1
		flatcombine.nkeep = 1
		flatcombine.mclip = yes
		flatcombine.lsigma = 3.0
		flatcombine.hsigma = 3.0
		flatcombine.rdnoise = 0.0
		flatcombine.gain = hdr["SOFTGAIN"]
		flatcombine.snoise = 0.0
		flatcombine.pclip = -0.5
			
		# Run flatcombine
		fstr = ",".join(glis[:10])
		flatcombine(fstr, output="Flat-%s-%s.fits" % (chip, filt))
			
		# Normalize
		iraf.iterstat.nsigrej = 5.0
		iraf.iterstat.maxiter = 10
		iraf.iterstat.verbose = globver
		iraf.iterstat.lower = INDEF
		iraf.iterstat.upper = INDEF
		iraf.iterstat("Flat-%s-%s.fits" % (chip, filt))
		iraf.imarith("Flat-%s-%s.fits" % (chip, filt), "/", 
			         iraf.iterstat.median, "Flat-%s-%s.fits" % (chip, filt))
			         	
	# Bias subtract and flat-field science images
	imlist = glob.glob("[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in imlist:
		hdr = pyfits.getheader(im)
		if (hdr["FILTER"] == filt) and (not os.path.exists("fb%s" % im)):
			
			hdr = pyfits.getheader(im)
			
			# Subtract bias frame
			ccdproc = iraf.ccdred.ccdproc	
			ccdproc.ccdtype = ""
			ccdproc.noproc = no
			ccdproc.fixpix = no
			ccdproc.overscan = no
			ccdproc.trim = no
			ccdproc.zerocor = yes
			ccdproc.darkcor = no
			ccdproc.flatcor = yes
			ccdproc.illumcor = no
			ccdproc.fringecor = no
			ccdproc.readcor = no
			ccdproc.scancor = no
			ccdproc.readaxis = 'line'
			ccdproc.fixfile = ""
			ccdproc.zero = "Bias-%s.fits" % chip
			ccdproc.flat = "Flat-%s-%s.fits" % (chip, filt)
			ccdproc.interactive = no
			ccdproc.function = "legendre"
			ccdproc.order = 1
			ccdproc.sample = "*"
			ccdproc.naverage = 1
			ccdproc.niterate = 1
			ccdproc.low_reject = 3.0
			ccdproc.high_reject = 3.0
			ccdproc.grow = 0
			ccdproc(images=im, output="fb%s" % im)
			
	# Create sky frame (including dark current!)
	if not os.path.exists("Sky-%s-%s.fits" % (chip, filt)):
		imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
		hdr = pyfits.getheader(imlist[0])
		slis = []
		for im in imlist:
			if (hdr["FILTER"] == filt):
				slis.append(im)
				
		# Set up combine
		imcombine = iraf.immatch.imcombine
		imcombine.headers = ""
		imcombine.bpmasks = ""
		imcombine.rejmasks = ""
		imcombine.nrejmasks = ""
		imcombine.expmasks = ""
		imcombine.sigmas = ""
		imcombine.combine = "median"
		imcombine.reject = "avsigclip"
		imcombine.project = no
		imcombine.outtype = "real"
		imcombine.offsets = "none"
		imcombine.masktype = "none"
		imcombine.scale = "median"
		imcombine.zero = "none"
		imcombine.weight = "none"
		imcombine.statsec = ""
		imcombine.lsigma = 3.0
		imcombine.hsigma = 3.0
		imcombine.rdnoise = 0.0
		imcombine.gain = hdr["SOFTGAIN"]
		
		sstr = 	",".join(slis[:10])
		imcombine(sstr, "Sky-%s-%s.fits" % (chip, filt))
		
		# Normalize
		iraf.iterstat("Sky-%s-%s.fits" % (chip, filt))
		iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "/", 
			         iraf.iterstat.median, "Sky-%s-%s.fits" % (chip, filt))
			         
	# Subtract sky frame (and dark!)
	imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in imlist:
		if not os.path.exists("s%s" % im):
		
			iraf.iterstat(im)
			iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "*", iraf.iterstat.median,
			             "temp.fits")
			iraf.imarith(im, "-", "temp.fits", "s%s" % im)
			os.remove("temp.fits")
			fimg = pyfits.open("s%s" % im, "update")
			fimg[0].header["SKYMED"] = iraf.iterstat.median
			fimg[0].header["SKYSIG"] = iraf.iterstat.sigma
			fimg[0].header["SKYSUB"] = 1
			fimg.flush()
								
	# Remove cosmic rays
	imlist = glob.glob("sfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in imlist:
		if not os.path.exists("c%s" % im):
			fimg = pyfits.open(im)
			iraf.lacos_im(im, "c%s" % im, "c%s.mask.fits" % im[:-5],
			              gain=hdr['SOFTGAIN'], readn=0.0, statsec="*,*",
			              skyval=fimg[0].header["SKYMED"], sigclip=4.5, 
			              sigfrac=0.5, objlim=1.0, niter=3, verbose=no)

	# Add WCS keywords
	clis = glob.glob("csfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in clis:
	
		fimg = pyfits.open(im, mode='update')
		if fimg[0].header.get("RA")==None:
		
			ra0 = fimg[0].header["STRCURA"]; dec0 = fimg[0].header["STRCUDE"]
			nax = [fimg[0].header["NAXIS1"], fimg[0].header["NAXIS2"]]
			fimg[0].header["RA"] = ra0
			fimg[0].header["DEC"] = dec0		
			fimg[0].header["PIXSCALE"] = RATIRPIXSCALE[chip]
			fimg[0].header["PIXSCAL1"] = RATIRPIXSCALE[chip]
			fimg[0].header["PIXSCAL2"] = RATIRPIXSCALE[chip]
			fimg[0].header["CTYPE1"] = "RA---TAN"
			fimg[0].header["CTYPE2"] = "DEC--TAN"
			fimg[0].header["WCSDIM"] = 2
			fimg[0].header["WAT0_001"] = "system=image"
			fimg[0].header["WAT1_001"] = "wtype=tan axtype=ra"
			fimg[0].header["WAT2_001"] = "wtype=tan axtype=dec"
			fimg[0].header["LTM1_1"] = 1.0
			fimg[0].header["LTM2_2"] = 1.0	
			fimg[0].header["CRPIX1"] = nax[0] / 2.0
			fimg[0].header["CRPIX2"] = nax[1] / 2.0
			fimg[0].header["CRVAL1"] = ra0
			fimg[0].header["CRVAL2"] = dec0
			fimg[0].header["CD1_1"] = -RATIRPIXSCALE[chip] / 3600.0
			fimg[0].header["CD1_2"] = 0.0
			fimg[0].header["CD2_1"] = 0.0
			fimg[0].header["CD2_2"] = RATIRPIXSCALE[chip] / 3600.0
			
		fimg.flush()
		
	# Crude astrometry
	for im in clis:
	
		if not os.path.exists("a%s" % im):
			os.system("python %s %s" % (PYASTROM, im))
				
	# Refine Astrometry
	o1 = open("daofind.param", "w")
	o1.write("NUMBER\nXWIN_IMAGE\nYWIN_IMAGE\nMAG_AUTO\nFLAGS\nA_IMAGE\nB_IMAGE\n")
	o1.write("ELONGATION\nFWHM_IMAGE\nXWIN_WORLD\nYWIN_WORLD\n")
	o1.write("ERRAWIN_IMAGE\nERRBWIN_IMAGE\nERRTHETAWIN_IMAGE\nERRAWIN_WORLD\n")
	o1.write("ERRBWIN_WORLD\nERRTHETAWIN_WORLD\nFLUX_AUTO\nFLUX_RADIUS\nFLUXERR_AUTO")
	o1.close()
	
	o2 = open("default.conv", "w")
	o2.write("CONV NORM\n# 5x5 convolution mask of a gaussian PSF with FWHM = 3.0 pixels.\n0.092163 0.221178 0.296069 0.221178 0.092163\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.296069 0.710525 0.951108 0.710525 0.296069\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.092163 0.221178 0.296069 0.221178 0.092163")
	o2.close()
	
	alis = glob.glob("acsfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in alis:
	
		# Detect sources
		os.system("sex -CATALOG_NAME %s.cat -CATALOG_TYPE FITS_LDAC -PARAMETERS_NAME daofind.param -DETECT_THRESH 2.0 -ANALYSIS_THRESH 2.0 -GAIN_KEY SOFTGAIN -PIXEL_SCALE 0 %s" % (im[:-5], im))
		
	# Run scamp for alignment
	imlist = " ".join(alis)
	os.system("scamp -ASTREF_CATALOG SDSS-R7 -DISTORTDEG 1 -SOLVE_PHOTOM N -SN_THRESHOLDS 3.0,10.0 -CHECKPLOT_DEV NULL %s" % imlist.replace(".fits", ".cat"))
	
	# Update header
	for im in alis:
		os.system("missfits %s" % im)
		os.system("rm %s.head %s.back" % (im[:-5], im))
		
	# Find good images for coadd
	slis = []
	for im in alis:
		hdr = pyfits.getheader(im)
		rms1 = hdr["ASTRRMS1"]; rms2 = hdr["ASTRRMS2"]
		if (rms1 < 1.0e-4) and (rms1 > 5.0e-6) and (rms2 < 1.0e-4) and (rms2 > 5.0e-6):
			slis.append(im)
			
	# Initial (unweighted) coadd
	sstr = " ".join(slis)
	os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)
	
	# Identify sources in the coadded frame
	hdr = pyfits.getheader("coadd.fits")
	iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0",
	             pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=20.0,
	             wtimage="coadd.weight.fits")
	hdr = pyfits.getheader("coadd.fits")
	cpsfdiam = 1.34 * float(hdr["SEEPIX"])
	iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0",
	             pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam,
	             wtimage="coadd.weight.fits")
	             
	refstars1 = Starlist("coadd.fits.stars")
	refstars1.pix2wcs("coadd.fits")
	truemags = np.array(refstars1.mags())
	maglist = []; errlist = []
	
	# (Relative) Zeropoints for individual images
	for im in slis:
		hdr = pyfits.getheader(im)
		iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", 
		       pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=20.0)
		hdr = pyfits.getheader(im)
		psfdiam = 1.34 * float(hdr["SEEPIX"])
		iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", 
		       pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=psfdiam)
		stars = Starlist("%s.stars" % im)
		refstars1.wcs2pix(im)
		a,b = stars.match(refstars1,tol=10.0,maxnum=1000)
		newmags = np.zeros(len(refstars1)); newwts = np.zeros(len(refstars1))
		for i in range(len(refstars1)):
			for j in range(len(a)):
				if b[j].mag == truemags[i]:
					newmags[i] = a[j].mag
					newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01),2)
					continue
		maglist.append(newmags); errlist.append(newwts)
		
	obsmags = np.array(maglist)
	wts = np.array(errlist)
	
	[zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)
	
	# Update headers
	medzp = np.median(zpts)
	for i in range(len(slis)):
		im = slis[i]
		fimg = pyfits.open(im, mode="update")
		fimg[0].header["RELZPT"] = zpts[i]
		fimg[0].header["RELZPTSC"] = scatt[i]
		fimg[0].header["RELZPRMS"] = rms[i]
		fimg[0].header["FLXSCALE"] = 1.0 / np.power(10, (zpts[i] - medzp) / 2.5)
		fimg.flush()
		
	# Final coadd
	os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)
	
	# Calibration
	iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0",
	             pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam,
	             wtimage="coadd.weight.fits")
	stars = Starlist("coadd.fits.stars")
	refstars = Starlist(reflist)
	refstars.wcs2pix("coadd.fits")
	refstars.set_mag("%sMAG" % filt.upper())
	truemags = np.array(refstars.mags())
	maglist = []; errlist = []
	
	a,b = stars.match(refstars, tol=10.0, maxnum=1000)
	newmags = np.zeros(len(refstars)); newwts = np.zeros(len(refstars))
	for i in range(len(refstars)):
		for j in range(len(a)):
			if b[j].mag == truemags[i]:
				newmags[i] = a[j].mag
				newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01),2)
				continue
	maglist.append(newmags); errlist.append(newwts)
	
	obsmags = np.array(maglist)
	wts = np.array(errlist)
	
	[zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)
	fimg = pyfits.open("coadd.fits", mode="update")
	fimg[0].header["ABSZPT"] = zpts[0] + 25
	fimg[0].header["ABSZPTSC"] = scatt[0]
	fimg[0].header["ABZPRMS"] = rms[0]
	
	print "Zeropoint for coadded image: %.3f" % (25.0+zpts[0])
	print "Robust scatter for coadded image: %.3f" % scatt[0]
	print "RMS for coadded image: %.3f" % rms[0]