2 from astropy.io 
import fits
 
    7 import lsst.pex.config 
as afwConfig
 
   14     starList = afwConfig.Field(dtype=str,
 
   15                                doc=
"Catalog of stars with mags ra/dec")
 
   16     seed = afwConfig.Field(dtype=int, default=1,
 
   17                            doc=
"Seed for random number generator")
 
   21     ConfigClass = PositionStarFakesConfig
 
   24         BaseFakeSourcesTask.__init__(self, **kwargs)
 
   25         print(
"RNG seed:", self.
config.seed)
 
   33     def run(self, exposure, background):
 
   35         self.
log.
info(
"Adding fake stars at real positions")
 
   36         psf = exposure.getPsf()
 
   37         psfBBox = psf.computeImage().getBBox()
 
   38         margin = 
max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
 
   40         PARENT = afwImage.PARENT
 
   41         md = exposure.getMetadata()
 
   42         expBBox = exposure.getBBox(PARENT)
 
   43         wcs = exposure.getWcs()
 
   45         for istar, star 
in enumerate(self.
starData):
 
   47                 starident = star[
"ID"]
 
   52                 flux = exposure.getCalib().getFlux(float(star[
'mag']))
 
   54                 raise KeyError(
"No mag column in %s" % self.
config.starList)
 
   57                 starCoord = afwGeom.SpherePoint(star[
'RA'], star[
'DEC'], afwGeom.degrees)
 
   59                 raise KeyError(
"No RA/DEC column in table".
format(self.
config.starList))
 
   61             starXY = wcs.skyToPixel(starCoord)
 
   62             bboxI = exposure.getBBox(PARENT)
 
   63             bboxI.grow(int(margin))
 
   64             if not bboxI.contains(afwGeom.Point2I(starXY)):
 
   68                 starImage = psf.computeImage(starXY)
 
   69             except InvalidParameterError:
 
   73                 logmsg = 
"Skipping fake {} because no input images present at point {}" 
   74                 self.
log.
info(logmsg.format(starident, starXY))
 
   78             starBBox = starImage.getBBox(PARENT)
 
   81             if expBBox.contains(starBBox) 
is False:
 
   82                 newBBox = starImage.getBBox(PARENT)
 
   84                 if newBBox.getArea() <= 0:
 
   85                     self.
log.
info(
"Skipping fake %d" % starident)
 
   87                 self.
log.
info(
"Cropping FAKE%d from %s to %s" % (starident,
 
   88                                                                  str(starBBox), str(newBBox)))
 
   89                 starImage = starImage.Factory(starImage, newBBox, PARENT)
 
   92             starMaskedImage = afwImage.MaskedImageF(starImage.convertF())
 
   96             md.set(
"FAKE%s" % str(starident), 
"%.3f, %.3f" % (starXY.getX(),
 
   98             self.
log.
info(
"Adding fake %s at: %.1f,%.1f" % (str(starident),
 
  102             maskedImage = exposure.getMaskedImage()
 
  103             BBox = starMaskedImage.getBBox(PARENT)
 
  104             subMaskedImage = maskedImage.Factory(exposure.getMaskedImage(),
 
  107             subMaskedImage += starMaskedImage