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
19 from .
import FakeSourceLib
as fsl
23 starList = afwConfig.Field(dtype=str,
24 doc=
"Catalog of stars with mags ra/dec")
25 seed = afwConfig.Field(dtype=int, default=1,
26 doc=
"Seed for random number generator")
30 ConfigClass = PositionStarFakesConfig
33 BaseFakeSourcesTask.__init__(self, **kwargs)
34 print(
"RNG seed:", self.config.seed)
35 self.
rng = afwMath.Random(seed=self.config.seed)
36 self.
npRand = np.random.RandomState(self.config.seed)
38 self.
starData = fits.open(self.config.starList)[1].data
42 def run(self, exposure, background):
44 self.log.info(
"Adding fake stars at real positions")
45 psf = exposure.getPsf()
46 psfBBox = psf.computeImage().getBBox()
47 margin = max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
49 PARENT = afwImage.PARENT
50 md = exposure.getMetadata()
51 expBBox = exposure.getBBox(PARENT)
52 wcs = exposure.getWcs()
54 for istar, star
in enumerate(self.
starData):
56 starident = star[
"ID"]
61 flux = exposure.getCalib().getFlux(
float(star[
'mag']))
63 raise KeyError(
"No mag column in %s" % self.config.starList)
66 starCoord = afwGeom.SpherePoint(star[
'RA'], star[
'DEC'], afwGeom.degrees)
68 raise KeyError(
"No RA/DEC column in table".format(self.config.starList))
70 starXY = wcs.skyToPixel(starCoord)
71 bboxI = exposure.getBBox(PARENT)
72 bboxI.grow(
int(margin))
73 if not bboxI.contains(afwGeom.Point2I(starXY)):
77 starImage = psf.computeImage(starXY)
78 except InvalidParameterError:
82 logmsg =
"Skipping fake {} because no input images present at point {}" 83 self.log.info(logmsg.format(starident, starXY))
87 starBBox = starImage.getBBox(PARENT)
90 if expBBox.contains(starBBox)
is False:
91 newBBox = starImage.getBBox(PARENT)
93 if newBBox.getArea() <= 0:
94 self.log.info(
"Skipping fake %d" % starident)
96 self.log.info(
"Cropping FAKE%d from %s to %s" % (starident,
97 str(starBBox),
str(newBBox)))
98 starImage = starImage.Factory(starImage, newBBox, PARENT)
101 starMaskedImage = afwImage.MaskedImageF(starImage.convertF())
103 starMaskedImage.getMask().set(self.bitmask)
105 md.set(
"FAKE%s" %
str(starident),
"%.3f, %.3f" % (starXY.getX(),
107 self.log.info(
"Adding fake %s at: %.1f,%.1f" % (
str(starident),
111 maskedImage = exposure.getMaskedImage()
112 BBox = starMaskedImage.getBBox(PARENT)
113 subMaskedImage = maskedImage.Factory(exposure.getMaskedImage(),
116 subMaskedImage += starMaskedImage
def __init__(self, kwargs)
def run(self, exposure, background)