示例#1
0
    def run(self, exposure, background):
        self.log.info("Adding fake galaxies at real positions")
        PARENT = lsst.afw.image.PARENT
        psf = exposure.getPsf()
        md = exposure.getMetadata()
        calib = exposure.getCalib()
        expBBox = exposure.getBBox(PARENT)
        wcs = exposure.getWcs()
        skyToPixelMatrix = (wcs.getLinearTransform().invert().getMatrix() /
                            3600.0)

        """Deal with the skipped ones."""
        skipLog = 'runAddFake.skipped'
        if not os.path.isfile(skipLog):
            os.system('touch ' + skipLog)

        if self.config.galType is 'cosmos':
            import galsim
            exLevel = self.config.exclusionLevel
            cosmosCat = galsim.COSMOSCatalog(exclusion_level=exLevel)
        else:
            cosmosCat = None

        for igal, gal in enumerate(self.galData):
            try:
                galident = gal["ID"]
            except KeyError:
                galident = igal + 1

            try:
                flux = calib.getFlux(float(gal['mag']))
            except KeyError:
                raise KeyError("No mag column in %s" % self.config.galList)

            try:
                coordAdd = lsst.afw.geom.Point2D(gal['RA'], gal['DEC'])
                galCoord = lsst.afw.coord.Coord(coordAdd)
            except KeyError:
                raise("No RA/DEC column in %s table" % self.config.galList)

            galXY = wcs.skyToPixel(galCoord)
            bboxI = exposure.getBBox(PARENT)
            bboxI.grow(self.config.maxMargin)
            if not bboxI.contains(lsst.afw.geom.Point2I(galXY)):
                # Will just skip this object
                continue

            # Check the magnitude
            if gal['mag'] <= 0:
                self.log.info("Mag <= 0: Skipping %d" % galident)
                self.log.info("  mag: %7.3d" % gal['mag'])
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , negMag\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue

            # This is extrapolating for the PSF, probably not a good idea
            #  Return an Image of the PSF, in a form suitable for convolution.
            #  The returned image is normalized to sum to unity.
            psfImage = psf.computeKernelImage(galXY)
            try:
                addShear = self.config.addShear
                prec = self.config.sersic_prec
                galType = self.config.galType
                if self.config.galType is not 'cosmos':
                    galArray = makeFake.makeGalaxy(flux, gal,
                                                   psfImage.getArray(),
                                                   galType=galType,
                                                   cosmosCat=None,
                                                   calib=None,
                                                   addShear=addShear,
                                                   transform=skyToPixelMatrix)
                else:
                    galArray = makeFake.makeGalaxy(flux, gal,
                                                   psfImage.getArray(),
                                                   cosmosCat=cosmosCat,
                                                   calib=calib,
                                                   galType=galType,
                                                   addShear=addShear,
                                                   sersic_prec=prec,
                                                   transform=skyToPixelMatrix)
            except IndexError as ierr:
                self.log.info("GalSim Index Error: Skipping %d" % galident)
                self.log.info(ierr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimI\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except KeyError as kerr:
                self.log.info("GalSim Key Error: Skipping %d" % galident)
                self.log.info(kerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimK\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except ValueError as verr:
                self.log.info("GalSim Value Error: Skipping %d" % galident)
                self.log.info(verr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimV\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except RuntimeError as rerr:
                self.log.info("GalSim Runtime Error: Skipping %d" % galident)
                self.log.info(rerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimR\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except Exception as uerr:
                self.log.info("Unexpected Error: Skipping %d" % galident)
                self.log.info(uerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , Unexpected\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue

            galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
            galBBox = galImage.getBBox(PARENT)
            galX0 = (galXY.getX() - galBBox.getWidth()/2.0 + 0.5)
            galY0 = (galXY.getY() - galBBox.getHeight()/2.0 + 0.5)
            galImage = lsst.afw.math.offsetImage(galImage,
                                                 galX0, galY0,
                                                 'lanczos3')
            galBBox = galImage.getBBox(PARENT)

            # Check that we're within the larger exposure, otherwise crop
            parentBox = galImage.getBBox(PARENT)
            if expBBox.contains(parentBox) is False:
                newBBox = galImage.getBBox(PARENT)
                newBBox.clip(expBBox)
                if newBBox.getArea() <= 0:
                    self.log.info("BBoxEdge Error: Skipping %d" % galident)
                    with open(skipLog, "a") as slog:
                        try:
                            fcntl.flock(slog, fcntl.LOCK_EX)
                            slog.write("%8d , bboxEdge\n" % galident)
                            fcntl.flock(slog, fcntl.LOCK_UN)
                        except IOError:
                            continue
                    continue
                self.log.info("Cropping FAKE%d from %s to %s" % (galident,
                              str(galBBox), str(newBBox)))
                galImage = galImage.Factory(galImage, newBBox,
                                            PARENT)
                galBBox = newBBox

            # Add Noise: Optional?
            galMaskedImage = fsl.addNoise(galImage, exposure.getDetector(),
                                          rand_gen=self.npRand)

            # Put information of the added fake galaxies into the header
            md.set("FAKE%s" % str(galident), "%.3f, %.3f" % (galXY.getX(),
                                                             galXY.getY()))
            self.log.info("Adding fake %s at: %.1f,%.1f" % (str(galident),
                                                            galXY.getX(),
                                                            galXY.getY()))

            galMaskedImage.getMask().set(self.bitmask)
            try:
                galMaskedImage.getMask().removeAndClearMaskPlane('FAKE',
                                                                 True)
            except Exception:
                pass
            try:
                galMaskedImage.getMask().removeAndClearMaskPlane('CROSSTALK',
                                                                 True)
            except Exception:
                pass
            try:
                galMaskedImage.getMask().removeAndClearMaskPlane('UNMASKEDNAN',
                                                                 True)
            except Exception:
                pass

            maskedImage = exposure.getMaskedImage()
            try:
                maskedImage.getMask().removeAndClearMaskPlane('CROSSTALK',
                                                              True)
            except Exception:
                pass
            try:
                maskedImage.getMask().removeAndClearMaskPlane('UNMASKEDNAN',
                                                              True)
            except Exception:
                pass
            try:
                maskedImage.getMask().removeAndClearMaskPlane('FAKE', True)
            except Exception:
                pass

            BBox = galMaskedImage.getBBox(PARENT)
            subMaskedImage = maskedImage.Factory(exposure.getMaskedImage(),
                                                 BBox,
                                                 PARENT)
            subMaskedImage += galMaskedImage

            """
示例#2
0
    def run(self, exposure, background):

        self.log.info("Adding fake random galaxies")
        psf = exposure.getPsf()
        psfBBox = psf.computeImage().getBBox()
        minMargin = max(psfBBox.getWidth(), psfBBox.getHeight()) / 2 + 1
        md = exposure.getMetadata()
        expBBox = exposure.getBBox()
        scalingMatrix = np.array([[0.0, 1.0], [1.0, 0.0]]) / exposure.getWcs().pixelScale().asArcseconds()

        if self.config.nGal == 0:
            doGal = enumerate(self.galData)
        else:
            inds = self.npRand.choice(range(len(self.galData)), size=self.config.nGal, replace=False)
            doGal = zip(inds, self.galData[inds])

        for igal, gal in doGal:
            try:
                galident = gal["ID"]
            except KeyError:
                galident = igal + 1

            try:
                flux = exposure.getCalib().getFlux(float(gal["mag"]))
            except KeyError:
                raise KeyError("No mag column in %s table" % self.config.galList)

            # don't put the galaxy within one PSF box of the edge
            # or within the given pixel margin
            if self.config.margin is not None:
                margin = self.config.margin
            else:
                margin = minMargin
            bboxI = exposure.getBBox(lsst.afw.image.PARENT)
            bboxI.grow(-margin)
            bboxD = lsst.afw.geom.BoxD(bboxI)
            x = self.rng.flat(bboxD.getMinX(), bboxD.getMaxX())
            y = self.rng.flat(bboxD.getMinY(), bboxD.getMaxY())
            # TODO: check for overlaps here and regenerate x,y if necessary

            psfImage = psf.computeKernelImage(lsst.afw.geom.Point2D(x, y))
            galArray = makeFake.makeGalaxy(flux, gal, psfImage.getArray(), self.config.galType, transform=scalingMatrix)
            galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)
            galImage = lsst.afw.math.offsetImage(
                galImage, x - galBBox.getWidth() / 2.0 + 0.5, y - galBBox.getHeight() / 2.0 + 0.5, "lanczos3"
            )
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)

            # check that we're within the larger exposure, otherwise crop
            if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT)) is False:
                newBBox = galImage.getBBox(lsst.afw.image.PARENT)
                newBBox.clip(expBBox)
                self.log.info("Cropping FAKE%d from %s to %s" % (galident, str(galBBox), str(newBBox)))
                galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
                galBBox = newBBox

            galMaskedImage = fsl.addNoise(galImage, exposure.getDetector(), rand_gen=self.npRand)
            mask = galMaskedImage.getMask()
            mask.set(self.bitmask)

            md.set("FAKE%d" % gal["ID"], "%.3f, %.3f" % (x, y))
            self.log.info("Adding fake at: %.1f,%.1f" % (x, y))

            # TODO: set the mask
            galMaskedImage.getMask().set(self.bitmask)
            subMaskedImage = exposure.getMaskedImage().Factory(
                exposure.getMaskedImage(), galMaskedImage.getBBox(lsst.afw.image.PARENT), lsst.afw.image.PARENT
            )
            subMaskedImage += galMaskedImage
示例#3
0
    def run(self, exposure, background):

        self.log.info("Adding fake galaxies at real positions")
        psf = exposure.getPsf()
        md = exposure.getMetadata()
        expBBox = exposure.getBBox(lsst.afw.image.PARENT)
        wcs = exposure.getWcs()
        skyToPixelMatrix = wcs.getLinearTransform().invert().getMatrix() / 3600.0

        """ Deal with the skipped ones """
        skipLog = 'runAddFake.skipped'
        if not os.path.isfile(skipLog):
            dum = os.system('touch ' + skipLog)

        measCat = self.makeCatalog()

        for igal, gal in enumerate(self.galData):
            try:
                galident = gal["ID"]
            except KeyError:
                galident = igal + 1

            try:
                flux = exposure.getCalib().getFlux(float(gal['mag']))
            except KeyError:
                raise KeyError("No mag column in %s table"%self.config.galList)

            try:
                galCoord = lsst.afw.coord.Coord(lsst.afw.geom.Point2D(gal['RA'], gal['DEC']))
            except KeyError:
                raise("No RA/DEC column in %s table"%self.config.galList)

            galXY = wcs.skyToPixel(galCoord)
            bboxI = exposure.getBBox(lsst.afw.image.PARENT)
            bboxI.grow(self.config.maxMargin)
            if not bboxI.contains(lsst.afw.geom.Point2I(galXY)):
                #self.log.info("Not Useful: Skipping fake %d" % galident)
                continue

            #this is extrapolating for the PSF, probably not a good idea
            psfImage = psf.computeKernelImage(galXY)
            try:
                galArray = makeFake.makeGalaxy(flux, gal, psfImage.getArray(),
                                               self.config.galType,
                                               transform = skyToPixelMatrix,
                                               addShear=self.config.addShear)
            except KeyError as kerr:
                self.log.info("GalSim Key Error: Skipping fake %d"%galident)
                self.log.info("       Mag, nSer, Reff, b/a: %5.2f, %5.2f, %6.2f, %4.1f"%(
                    gal['mag'], gal['sersic_n'], gal['reff'], gal['b_a']))
                self.log.info(kerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimK\n"%galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except ValueError as verr:
                self.log.info("GalSim Value Error: Skipping fake %d"%galident)
                self.log.info("       Mag, nSer, Reff, b/a: %5.2f, %5.2f, %6.2f, %4.1f"%(
                    gal['mag'], gal['sersic_n'], gal['reff'], gal['b_a']))
                self.log.info(verr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimV\n"%galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except RuntimeError as rerr:
                self.log.info("GalSim Runtime Error: Skipping fake %d"%galident)
                self.log.info("       Mag, nSer, Reff, b/a: %5.2f, %5.2f, %6.2f, %4.1f"%(
                    gal['mag'], gal['sersic_n'], gal['reff'], gal['b_a']))
                self.log.info(rerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimR\n"%galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue

            galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)
            galImage = lsst.afw.math.offsetImage(galImage,
                                                 galXY.getX() - galBBox.getWidth()/2.0 + 0.5,
                                                 galXY.getY() - galBBox.getHeight()/2.0 + 0.5,
                                                 'lanczos3')
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)


            #check that we're within the larger exposure, otherwise crop
            if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT)) is False:
                newBBox = galImage.getBBox(lsst.afw.image.PARENT)
                newBBox.clip(expBBox)
                if newBBox.getArea() <= 0:
                    self.log.info("BBoxEdge Error: Skipping fake %d"%galident)
                    with open(skipLog, "a") as slog:
                        try:
                            fcntl.flock(slog, fcntl.LOCK_EX)
                            slog.write("%8d , bboxEdge\n"%galident)
                            fcntl.flock(slog, fcntl.LOCK_UN)
                        except IOError:
                            continue
                    continue
                self.log.info("Cropping FAKE%d from %s to %s"%(galident, str(galBBox), str(newBBox)))
                galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
                galBBox = newBBox

            self.measureFake(exposure, galImage, galXY, measCat)

            galMaskedImage = fsl.addNoise(galImage, exposure.getDetector(),
                                          rand_gen=self.npRand)

            # Put information of the added fake galaxies into the header
            md.set("FAKE%s" % str(galident), "%.3f, %.3f" % (galXY.getX(), galXY.getY()))
            self.log.info("Adding fake %s at: %.1f,%.1f"% (str(galident), galXY.getX(), galXY.getY()))

            galMaskedImage.getMask().set(self.bitmask)
            subMaskedImage = exposure.getMaskedImage().Factory(exposure.getMaskedImage(),
                                                               galMaskedImage.getBBox(lsst.afw.image.PARENT),
                                                               lsst.afw.image.PARENT)
            subMaskedImage += galMaskedImage

        self.measureParents(exposure, measCat)

        return measCat
示例#4
0
    def run(self, exposure, sources, background):

        self.log.info("Adding fake random galaxies")
        psf = exposure.getPsf()
        psfBBox = psf.computeImage().getBBox()
        minMargin =  max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
        md = exposure.getMetadata()
        expBBox = exposure.getBBox()

        for igal, gal in enumerate(self.galData):

            try:
                galident = gal["ID"]
            except KeyError:
                galident = igal + 1

            try:
                flux = exposure.getCalib().getFlux(float(gal['mag']))
            except KeyError:
                raise KeyError("No mag column in %s table"%self.config.galList)

            #don't put the galaxy within one PSF box of the edge
            #or within the given pixel margin
            if self.config.margin is not None:
                margin = self.config.margin
            else:
                margin = minMargin
            bboxI = (exposure.getBBox(lsst.afw.image.PARENT))
            bboxI.grow(-margin)
            bboxD = lsst.afw.geom.BoxD(bboxI)
            x = self.rng.flat(bboxD.getMinX(), bboxD.getMaxX())
            y = self.rng.flat(bboxD.getMinY(), bboxD.getMaxY())
            #TODO: check for overlaps here and regenerate x,y if necessary

            psfImage = psf.computeKernelImage(lsst.afw.geom.Point2D(x, y))

            galArray = makeFake.makeGalaxy(flux, gal, psfImage.getArray(),
                                           self.config.galType )
            galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)
            galImage = lsst.afw.math.offsetImage(galImage,
                                                 x - galBBox.getWidth()/2.0 + 0.5,
                                                 y - galBBox.getHeight()/2.0 + 0.5,
                                                 'lanczos3')
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)


           #check that we're within the larger exposure, otherwise crop
            if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT)) is False:
                newBBox = galImage.getBBox(lsst.afw.image.PARENT)
                newBBox.clip(expBBox)
                self.log.info("Cropping FAKE%d from %s to %s"%(galident, str(galBBox), str(newBBox)))
                galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
                galBBox = newBBox

            detector = exposure.getDetector()
            ccd =  lsst.afw.cameraGeom.cast_Ccd(detector)
            amp = ccd.findAmp(lsst.afw.geom.Point2I(int(x), int(y)))
            gain = amp.getElectronicParams().getGain()
            #TODO: this is gaussian noise right now, probably good enough
            varImage = lsst.afw.image.ImageF(galImage, True)
            varImage /= gain
            noiseArray = self.npRand.normal(loc=0.0,
                                            scale=np.sqrt(np.abs(varImage.getArray())),
                                             size=(galBBox.getHeight(),
                                                   galBBox.getWidth()))
            noiseImage = lsst.afw.image.ImageF(noiseArray.astype(np.float32))
            galImage += noiseImage

            md.set("FAKE%d" % gal['ID'], "%.3f, %.3f" % (x, y))
            self.log.info("Adding fake at: %.1f,%.1f"% (x, y))

            galMaskedImage = lsst.afw.image.MaskedImageF(galImage, None, varImage)
            #TODO: set the mask
            galMaskedImage.getMask().set(self.bitmask)
            subMaskedImage = exposure.getMaskedImage().Factory(exposure.getMaskedImage(),
                                                               galMaskedImage.getBBox(lsst.afw.image.PARENT),
                                                               lsst.afw.image.PARENT)
            subMaskedImage += galMaskedImage
    def run(self, exposure, background):
        self.log.info("Adding fake galaxies at real positions")
        PARENT = lsst.afw.image.PARENT
        psf = exposure.getPsf()
        md = exposure.getMetadata()
        calib = exposure.getCalib()
        expBBox = exposure.getBBox(PARENT)
        wcs = exposure.getWcs()
        skyToPixelMatrix = (wcs.getLinearTransform().invert().getMatrix() /
                            3600.0)

        """Deal with the skipped ones."""
        skipLog = 'runAddFake.skipped'
        if not os.path.isfile(skipLog):
            os.system('touch ' + skipLog)

        if self.config.galType is 'cosmos':
            import galsim
            exLevel = self.config.exclusionLevel
            cosmosCat = galsim.COSMOSCatalog(exclusion_level=exLevel)
        else:
            cosmosCat = None

        for igal, gal in enumerate(self.galData):
            try:
                galident = gal["ID"]
            except KeyError:
                galident = igal + 1

            try:
                flux = calib.getFlux(float(gal['mag']))
            except KeyError:
                raise KeyError("No mag column in %s" % self.config.galList)

            try:
                coordAdd = lsst.afw.geom.Point2D(gal['RA'], gal['DEC'])
                galCoord = lsst.afw.coord.Coord(coordAdd)
            except KeyError:
                raise("No RA/DEC column in %s table" % self.config.galList)

            galXY = wcs.skyToPixel(galCoord)
            bboxI = exposure.getBBox(PARENT)
            bboxI.grow(self.config.maxMargin)
            if not bboxI.contains(lsst.afw.geom.Point2I(galXY)):
                # Will just skip this object
                continue

            # Check the magnitude
            if gal['mag'] <= 0:
                self.log.info("Mag <= 0: Skipping %d" % galident)
                self.log.info("  mag: %7.3d" % gal['mag'])
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , negMag\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue

            # This is extrapolating for the PSF, probably not a good idea
            #  Return an Image of the PSF, in a form suitable for convolution.
            #  The returned image is normalized to sum to unity.
            psfImage = psf.computeKernelImage(galXY)
            try:
                addShear = self.config.addShear
                prec = self.config.sersic_prec
                galType = self.config.galType
                if self.config.galType is not 'cosmos':
                    galArray = makeFake.makeGalaxy(flux, gal,
                                                   psfImage.getArray(),
                                                   galType=galType,
                                                   cosmosCat=None,
                                                   calib=None,
                                                   addShear=addShear,
                                                   transform=skyToPixelMatrix)
                else:
                    galArray = makeFake.makeGalaxy(flux, gal,
                                                   psfImage.getArray(),
                                                   cosmosCat=cosmosCat,
                                                   calib=calib,
                                                   galType=galType,
                                                   addShear=addShear,
                                                   sersic_prec=prec,
                                                   transform=skyToPixelMatrix)
            except IndexError as ierr:
                self.log.info("GalSim Index Error: Skipping %d" % galident)
                self.log.info(ierr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimI\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except KeyError as kerr:
                self.log.info("GalSim Key Error: Skipping %d" % galident)
                self.log.info(kerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimK\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except ValueError as verr:
                self.log.info("GalSim Value Error: Skipping %d" % galident)
                self.log.info(verr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimV\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except RuntimeError as rerr:
                self.log.info("GalSim Runtime Error: Skipping %d" % galident)
                self.log.info(rerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , galsimR\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue
            except Exception as uerr:
                self.log.info("Unexpected Error: Skipping %d" % galident)
                self.log.info(uerr.message)
                with open(skipLog, "a") as slog:
                    try:
                        fcntl.flock(slog, fcntl.LOCK_EX)
                        slog.write("%8d , Unexpected\n" % galident)
                        fcntl.flock(slog, fcntl.LOCK_UN)
                    except IOError:
                        continue
                continue

            galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
            galBBox = galImage.getBBox(PARENT)
            galX0 = (galXY.getX() - galBBox.getWidth()/2.0 + 0.5)
            galY0 = (galXY.getY() - galBBox.getHeight()/2.0 + 0.5)
            galImage = lsst.afw.math.offsetImage(galImage,
                                                 galX0, galY0,
                                                 'lanczos3')
            galBBox = galImage.getBBox(PARENT)

            # Check that we're within the larger exposure, otherwise crop
            parentBox = galImage.getBBox(PARENT)
            if expBBox.contains(parentBox) is False:
                newBBox = galImage.getBBox(PARENT)
                newBBox.clip(expBBox)
                if newBBox.getArea() <= 0:
                    self.log.info("BBoxEdge Error: Skipping %d" % galident)
                    with open(skipLog, "a") as slog:
                        try:
                            fcntl.flock(slog, fcntl.LOCK_EX)
                            slog.write("%8d , bboxEdge\n" % galident)
                            fcntl.flock(slog, fcntl.LOCK_UN)
                        except IOError:
                            continue
                    continue
                self.log.info("Cropping FAKE%d from %s to %s" % (galident,
                              str(galBBox), str(newBBox)))
                galImage = galImage.Factory(galImage, newBBox,
                                            PARENT)
                galBBox = newBBox

            # Add Noise: Optional?
            galMaskedImage = fsl.addNoise(galImage, exposure.getDetector(),
                                          rand_gen=self.npRand)

            # Put information of the added fake galaxies into the header
            md.set("FAKE%s" % str(galident), "%.3f, %.3f" % (galXY.getX(),
                                                             galXY.getY()))
            self.log.info("Adding fake %s at: %.1f,%.1f" % (str(galident),
                                                            galXY.getX(),
                                                            galXY.getY()))
            # Set bit mask
            galMaskedImage.getMask().set(self.bitmask)

            maskedImage = exposure.getMaskedImage()
            try:
                maskedImage.getMask().removeAndClearMaskPlane('CROSSTALK', True)
            except Exception:
                pass
            try:
                maskedImage.getMask().removeAndClearMaskPlane('UNMASKEDNAN', True)
            except Exception:
                pass
            try:
                maskedImage.getMask().removeAndClearMaskPlane('FAKE', True)
            except Exception:
                pass

            BBox = galMaskedImage.getBBox(PARENT)
            subMaskedImage = maskedImage.Factory(exposure.getMaskedImage(),
                                                 BBox,
                                                 PARENT)
            subMaskedImage += galMaskedImage
示例#6
0
    def run(self, exposure, background):

        self.log.info("Adding fake random galaxies")
        psf = exposure.getPsf()
        psfBBox = psf.computeImage().getBBox()
        minMargin =  max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
        md = exposure.getMetadata()
        expBBox = exposure.getBBox()
        scalingMatrix = np.array([[0.0,1.0],[1.0,0.0]]) / exposure.getWcs().pixelScale().asArcseconds()

        if self.config.nGal==0:
            doGal = enumerate(self.galData)
        else:
            inds = self.npRand.choice(range(len(self.galData)), size=self.config.nGal, replace=False)
            doGal = zip(inds, self.galData[inds])

        for igal, gal in doGal:
            try:
                galident = gal["ID"]
            except KeyError:
                galident = igal + 1

            try:
                flux = exposure.getCalib().getFlux(float(gal['mag']))
            except KeyError:
                raise KeyError("No mag column in %s table"%self.config.galList)

            #don't put the galaxy within one PSF box of the edge
            #or within the given pixel margin
            if self.config.margin is not None:
                margin = self.config.margin
            else:
                margin = minMargin
            bboxI = (exposure.getBBox(lsst.afw.image.PARENT))
            bboxI.grow(-margin)
            bboxD = lsst.afw.geom.BoxD(bboxI)
            x = self.rng.flat(bboxD.getMinX(), bboxD.getMaxX())
            y = self.rng.flat(bboxD.getMinY(), bboxD.getMaxY())
            #TODO: check for overlaps here and regenerate x,y if necessary

            psfImage = psf.computeKernelImage(lsst.afw.geom.Point2D(x, y))
            galArray = makeFake.makeGalaxy( flux, gal, psfImage.getArray(), self.config.galType,
                                            transform = scalingMatrix)
            galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)
            galImage = lsst.afw.math.offsetImage(galImage,
                                                 x - galBBox.getWidth()/2.0 + 0.5,
                                                 y - galBBox.getHeight()/2.0 + 0.5,
                                                 'lanczos3')
            galBBox = galImage.getBBox(lsst.afw.image.PARENT)

            
           #check that we're within the larger exposure, otherwise crop
            if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT)) is False:
                newBBox = galImage.getBBox(lsst.afw.image.PARENT)
                newBBox.clip(expBBox)
                self.log.info("Cropping FAKE%d from %s to %s"%(galident, str(galBBox), str(newBBox)))
                galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
                galBBox = newBBox

            
            galMaskedImage = fsl.addNoise(galImage, exposure.getDetector(), rand_gen=self.npRand)
            mask = galMaskedImage.getMask()
            mask.set(self.bitmask)
            
            md.set("FAKE%d" % gal['ID'], "%.3f, %.3f" % (x, y))
            self.log.info("Adding fake at: %.1f,%.1f"% (x, y))

            #TODO: set the mask
            galMaskedImage.getMask().set(self.bitmask)
            subMaskedImage = exposure.getMaskedImage().Factory(exposure.getMaskedImage(),
                                                               galMaskedImage.getBBox(lsst.afw.image.PARENT),
                                                               lsst.afw.image.PARENT)
            subMaskedImage += galMaskedImage