1 from __future__
import print_function
2 from builtins
import zip
3 from builtins
import str
4 from builtins
import range
12 from lsst.pipe.tasks.fakes
import BaseFakeSourcesConfig, BaseFakeSourcesTask
20 galList = lsst.pex.config.Field(dtype=str, doc=
"catalog of galaxies to add")
21 margin = lsst.pex.config.Field(dtype=int, default=
None, optional=
True,
22 doc=
"Size of margin at edge that should not be added")
23 seed = lsst.pex.config.Field(dtype=int, default=1,
24 doc=
"Seed for random number generator")
25 galType = lsst.pex.config.ChoiceField(dtype=str, default=
'sersic',
26 allowed={
'dsersic':
'double sersic galaxies added',
27 'sersic':
'single sersic galaxies added',
28 'real':
'real HST galaxy images added'},
29 doc=
'type of GalSim galaxies to add')
30 nGal = lsst.pex.config.Field(dtype=int, doc=
"""number of galaxies to add, if 0, then everything in catalog, 31 otherwise a random subset of nGal from the catalog""", default=0)
35 ConfigClass = RandomGalSimFakesConfig
38 BaseFakeSourcesTask.__init__(self, **kwargs)
39 print(
"RNG seed:", self.config.seed)
41 self.
npRand = np.random.RandomState(self.config.seed)
42 self.
galData = fits.open(self.config.galList)[1].data
44 def run(self, exposure, background):
46 self.log.info(
"Adding fake random galaxies")
47 psf = exposure.getPsf()
48 psfBBox = psf.computeImage().getBBox()
49 minMargin =
int(np.floor(max(psfBBox.getWidth(), psfBBox.getHeight())/2)) + 1
50 md = exposure.getMetadata()
51 expBBox = exposure.getBBox()
52 scalingMatrix = np.array([[0.0, 1.0], [1.0, 0.0]]) / exposure.getWcs().pixelScale().asArcseconds()
54 if self.config.nGal == 0:
57 inds = self.
npRand.choice(list(range(len(self.
galData))), size=self.config.nGal, replace=
False)
58 doGal = list(zip(inds, self.
galData[inds]))
60 for igal, gal
in doGal:
67 flux = exposure.getCalib().getFlux(
float(gal[
'mag']))
69 raise KeyError(
"No mag column in %s table"%self.config.galList)
73 if self.config.margin
is not None:
74 margin = self.config.margin
77 bboxI = (exposure.getBBox(lsst.afw.image.PARENT))
80 x = self.
rng.flat(bboxD.getMinX(), bboxD.getMaxX())
81 y = self.
rng.flat(bboxD.getMinY(), bboxD.getMaxY())
85 galArray = makeFake.makeGalaxy(flux, gal, psfImage.getArray(), self.config.galType,
86 transform=scalingMatrix)
87 galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
88 galBBox = galImage.getBBox(lsst.afw.image.PARENT)
90 x - galBBox.getWidth()/2.0 + 0.5,
91 y - galBBox.getHeight()/2.0 + 0.5,
93 galBBox = galImage.getBBox(lsst.afw.image.PARENT)
96 if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT))
is False:
97 newBBox = galImage.getBBox(lsst.afw.image.PARENT)
99 self.log.info(
"Cropping FAKE%d from %s to %s"%(galident,
str(galBBox),
str(newBBox)))
100 galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
103 galMaskedImage = lsst.afw.image.MaskedImageF(galImage)
105 mask = galMaskedImage.getMask()
106 mask.set(self.bitmask)
108 md.set(
"FAKE%d" % gal[
'ID'],
"%.3f, %.3f" % (x, y))
109 self.log.info(
"Adding fake at: %.1f,%.1f" % (x, y))
112 galMaskedImage.getMask().set(self.bitmask)
113 subMaskedImage = exposure.getMaskedImage().Factory(exposure.getMaskedImage(),
114 galMaskedImage.getBBox(lsst.afw.image.PARENT),
115 lsst.afw.image.PARENT)
116 subMaskedImage += galMaskedImage
def run(self, exposure, background)
def __init__(self, kwargs)
std::shared_ptr< ImageT > offsetImage(ImageT const &image, float dx, float dy, std::string const &algorithmName="lanczos5", unsigned int buffer=0)