4 from __future__
import print_function
5 from __future__
import absolute_import
6 from builtins
import str
16 from lsst.pipe.tasks.fakes
import BaseFakeSourcesConfig, BaseFakeSourcesTask
18 from .
import FakeSourceLib
as fsl
22 starList = afwConfig.Field(dtype=str,
23 doc=
"Catalog of stars with mags ra/dec")
24 seed = afwConfig.Field(dtype=int, default=1,
25 doc=
"Seed for random number generator")
29 ConfigClass = PositionStarFakesConfig
32 BaseFakeSourcesTask.__init__(self, **kwargs)
33 print(
"RNG seed:", self.config.seed)
34 self.
rng = afwMath.Random(seed=self.config.seed)
35 self.
npRand = np.random.RandomState(self.config.seed)
37 self.
starData = fits.open(self.config.starList)[1].data
41 def run(self, exposure, background):
43 self.log.info(
"Adding fake stars at real positions")
44 psf = exposure.getPsf()
45 psfBBox = psf.computeImage().getBBox()
46 margin = max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
48 PARENT = afwImage.PARENT
49 md = exposure.getMetadata()
50 expBBox = exposure.getBBox(PARENT)
51 wcs = exposure.getWcs()
53 for istar, star
in enumerate(self.
starData):
55 starident = star[
"ID"]
60 flux = exposure.getCalib().getFlux(
float(star[
'mag']))
62 raise KeyError(
"No mag column in %s" % self.config.starList)
65 starCoord = afwGeom.SpherePoint(star[
'RA'], star[
'DEC'], afwGeom.degrees)
67 raise KeyError(
"No RA/DEC column in table".format(self.config.starList))
69 starXY = wcs.skyToPixel(starCoord)
70 bboxI = exposure.getBBox(PARENT)
71 bboxI.grow(
int(margin))
72 if not bboxI.contains(afwGeom.Point2I(starXY)):
75 starImage = psf.computeImage(starXY)
77 starBBox = starImage.getBBox(PARENT)
80 if expBBox.contains(starBBox)
is False:
81 newBBox = starImage.getBBox(PARENT)
83 if newBBox.getArea() <= 0:
84 self.log.info(
"Skipping fake %d" % starident)
86 self.log.info(
"Cropping FAKE%d from %s to %s" % (starident,
87 str(starBBox),
str(newBBox)))
88 starImage = starImage.Factory(starImage, newBBox, PARENT)
91 starMaskedImage = afwImage.MaskedImageF(starImage.convertF())
93 starMaskedImage.getMask().set(self.bitmask)
95 md.set(
"FAKE%s" %
str(starident),
"%.3f, %.3f" % (starXY.getX(),
97 self.log.info(
"Adding fake %s at: %.1f,%.1f" % (
str(starident),
101 maskedImage = exposure.getMaskedImage()
102 BBox = starMaskedImage.getBBox(PARENT)
103 subMaskedImage = maskedImage.Factory(exposure.getMaskedImage(),
106 subMaskedImage += starMaskedImage
def __init__(self, kwargs)
def run(self, exposure, background)