24 import lsst.pex.config
 
   32     minMag = lsst.pex.config.Field(dtype=float, default=18.0, doc=
"Minimum magnitude for mock objects")
 
   33     maxMag = lsst.pex.config.Field(dtype=float, default=20.0, doc=
"Maximum magnitude for mock objects")
 
   34     maxRadius = lsst.pex.config.Field(
 
   35         dtype=float, default=10.0,
 
   36         doc=(
"Maximum radius of an object in arcseconds; only used " 
   37              "when determining which objects are in an exposure.")
 
   39     spacing = lsst.pex.config.Field(
 
   40         dtype=float, default=20.0,
 
   41         doc=
"Distance between objects (in arcseconds)." 
   43     seed = lsst.pex.config.Field(dtype=int, default=1, doc=
"Seed for numpy random number generator")
 
   47     """Task that generates simple mock objects and draws them on images, intended as a subtask of 
   50     May be subclassed to generate things other than stars. 
   53     ConfigClass = MockObjectConfig
 
   56         lsst.pipe.base.Task.__init__(self, **kwds)
 
   58         self.
center = lsst.afw.table.Point2DKey.addFields(self.
schema, 
"center",
 
   59                                                           "center position in tract WCS", 
"pixel")
 
   60         self.
magKey = self.
schema.addField(
"mag", type=float, doc=
"exact true magnitude")
 
   61         self.
rng = numpy.random.RandomState(self.
config.seed)
 
   63     def run(self, tractInfo, catalog=None):
 
   64         """Add records to the truth catalog and return it, delegating to makePositions and defineObject. 
   66         If the given catalog is not None, add records to this catalog and return it instead 
   67         of creating a new one. 
   69         Subclasses should generally not need to override this method. 
   75                 raise ValueError(
"Catalog schema does not match Task schema")
 
   77             record = catalog.addNew()
 
   78             record.setCoord(coord)
 
   79             record.set(self.
center, center)
 
   84         """Generate the centers (as a (coord, point) tuple) of mock objects (the point returned is 
   85         in the tract coordinate system). 
   87         Default implementation puts objects on a grid that is square in the tract's image coordinate 
   88         system, with spacing approximately given by config.spacings. 
   90         The return value is a Python iterable over (coord, point) pairs; the default implementation 
   91         is actually an iterator (i.e. the function is a "generator"), but derived-class overrides may 
   94         wcs = tractInfo.getWcs()
 
   95         spacing = self.
config.spacing / wcs.getPixelScale().asArcseconds()  
 
   96         bbox = tractInfo.getBBox()
 
   97         for y 
in numpy.arange(bbox.getMinY() + 0.5 * spacing, bbox.getMaxY(), spacing):
 
   98             for x 
in numpy.arange(bbox.getMinX() + 0.5 * spacing, bbox.getMaxX(), spacing):
 
  102         """Fill in additional fields in a truth catalog record (id and coord will already have 
  106         record.setD(self.
magKey, mag)
 
  109         """Draw the given truth catalog record on the given exposure, makings use of its Psf, Wcs, 
  110         PhotoCalib, and possibly filter to transform it appropriately. 
  112         The mask and variance planes of the Exposure are ignored. 
  114         The 'buffer' parameter is used to expand the source's bounding box when testing whether it 
  115         is considered fully part of the image. 
  117         Returns 0 if the object does not appear on the given image at all, 1 if it appears partially, 
  118         and 2 if it appears fully (including the given buffer). 
  120         wcs = exposure.getWcs()
 
  121         center = wcs.skyToPixel(record.getCoord())
 
  123             psfImage = exposure.getPsf().computeImage(center).convertF()
 
  126         psfBBox = psfImage.getBBox()
 
  127         overlap = exposure.getBBox()
 
  128         overlap.clip(psfBBox)
 
  129         if overlap.isEmpty():
 
  131         flux = exposure.getPhotoCalib().magnitudeToInstFlux(record.getD(self.
magKey))
 
  132         normalization = flux / psfImage.getArray().sum()
 
  133         if psfBBox != overlap:
 
  134             psfImage = psfImage.Factory(psfImage, overlap)
 
  140                 bufferedBBox.grow(buffer)
 
  141                 bufferedOverlap = exposure.getBBox()
 
  142                 bufferedOverlap.clip(bufferedBBox)
 
  143                 if bufferedOverlap != bufferedBBox:
 
  145         image = exposure.getMaskedImage().getImage()
 
  146         subImage = image.Factory(image, overlap)
 
  147         subImage.scaledPlus(normalization, psfImage)