2 from astropy.io
import fits
14 galList = lsst.pex.config.Field(dtype=str, doc=
"catalog of galaxies to add")
15 margin = lsst.pex.config.Field(dtype=int, default=
None, optional=
True,
16 doc=
"Size of margin at edge that should not be added")
17 seed = lsst.pex.config.Field(dtype=int, default=1,
18 doc=
"Seed for random number generator")
19 galType = lsst.pex.config.ChoiceField(dtype=str, default=
'sersic',
20 allowed={
'dsersic':
'double sersic galaxies added',
21 'sersic':
'single sersic galaxies added',
22 'real':
'real HST galaxy images added'},
23 doc=
'type of GalSim galaxies to add')
24 nGal = lsst.pex.config.Field(dtype=int, doc=
"""number of galaxies to add, if 0, then everything in catalog,
25 otherwise a random subset of nGal from the catalog""", default=0)
29 ConfigClass = RandomGalSimFakesConfig
32 BaseFakeSourcesTask.__init__(self, **kwargs)
33 print(
"RNG seed:", self.
config.seed)
38 def run(self, exposure, background):
40 self.
log.
info(
"Adding fake random galaxies")
41 psf = exposure.getPsf()
42 psfBBox = psf.computeImage().getBBox()
43 minMargin = int(np.floor(
max(psfBBox.getWidth(), psfBBox.getHeight())/2)) + 1
44 md = exposure.getMetadata()
45 expBBox = exposure.getBBox()
46 scalingMatrix = np.array([[0.0, 1.0], [1.0, 0.0]]) / exposure.getWcs().pixelScale().asArcseconds()
54 for igal, gal
in doGal:
61 flux = exposure.getCalib().getFlux(float(gal[
'mag']))
63 raise KeyError(
"No mag column in %s table"%self.
config.galList)
67 if self.
config.margin
is not None:
68 margin = self.
config.margin
71 bboxI = (exposure.getBBox(lsst.afw.image.PARENT))
73 bboxD = lsst.afw.geom.BoxD(bboxI)
74 x = self.
rng.flat(bboxD.getMinX(), bboxD.getMaxX())
75 y = self.
rng.flat(bboxD.getMinY(), bboxD.getMaxY())
78 psfImage = psf.computeKernelImage(lsst.afw.geom.Point2D(x, y))
79 galArray = makeFake.makeGalaxy(flux, gal, psfImage.getArray(), self.
config.galType,
80 transform=scalingMatrix)
81 galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
82 galBBox = galImage.getBBox(lsst.afw.image.PARENT)
84 x - galBBox.getWidth()/2.0 + 0.5,
85 y - galBBox.getHeight()/2.0 + 0.5,
87 galBBox = galImage.getBBox(lsst.afw.image.PARENT)
90 if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT))
is False:
91 newBBox = galImage.getBBox(lsst.afw.image.PARENT)
93 self.
log.
info(
"Cropping FAKE%d from %s to %s"%(galident, str(galBBox), str(newBBox)))
94 galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
97 galMaskedImage = lsst.afw.image.MaskedImageF(galImage)
99 mask = galMaskedImage.getMask()
102 md.set(
"FAKE%d" % gal[
'ID'],
"%.3f, %.3f" % (x, y))
103 self.
log.
info(
"Adding fake at: %.1f,%.1f" % (x, y))
107 subMaskedImage = exposure.getMaskedImage().
Factory(exposure.getMaskedImage(),
108 galMaskedImage.getBBox(lsst.afw.image.PARENT),
109 lsst.afw.image.PARENT)
110 subMaskedImage += galMaskedImage