22 __all__ = [
"BackgroundList"]
 
   29 from . 
import mathLib 
as afwMath
 
   33     """A list-like class to contain a list of (`lsst.afw.math.Background`, 
   34     `lsst.afw.math.Interpolate.Style`, `~lsst.afw.math.UndersampleStyle`) 
   39     *args : `tuple` or `~lsst.afw.math.Background` 
   40         A sequence of arguments, each of which becomes an element of the list. 
   41         We also accept a single `lsst.afw.math.Background` and extract the 
   42         ``interpStyle`` and ``undersampleStyle`` from the as-used values. 
   62             bkgd, interpStyle, undersampleStyle, approxStyle, \
 
   63                 approxOrderX, approxOrderY, approxWeighting = val
 
   64             if interpStyle 
is None or undersampleStyle 
is None:
 
   65                 interpStyle = bkgd.getAsUsedInterpStyle()
 
   66                 undersampleStyle = bkgd.getAsUsedUndersampleStyle()
 
   67                 actrl = bkgd.getBackgroundControl().getApproximateControl()
 
   68                 approxStyle = actrl.getStyle()
 
   69                 approxOrderX = actrl.getOrderX()
 
   70                 approxOrderY = actrl.getOrderY()
 
   71                 approxWeighting = actrl.getWeighting()
 
   72                 self.
_backgrounds[i] = (bkgd, interpStyle, undersampleStyle,
 
   73                                         approxStyle, approxOrderX, approxOrderY, approxWeighting)
 
   84             bkgd, interpStyle, undersampleStyle, approxStyle, \
 
   85                 approxOrderX, approxOrderY, approxWeighting = val
 
   89             undersampleStyle = 
None 
   93             approxWeighting = 
None 
   95         bgInfo = (bkgd, interpStyle, undersampleStyle, approxStyle,
 
   96                   approxOrderX, approxOrderY, approxWeighting)
 
  100         """Return a shallow copy 
  102         Shallow copies do not share backgrounds that are appended after copying, 
  103         but do share changes to contained background objects. 
  108         """Save our list of Backgrounds to a file. 
  115             Flags to control details of writing; currently unused, but present 
  116             for consistency with `lsst.afw.table.BaseCatalog.writeFits`. 
  119         for i, bkgd 
in enumerate(self):
 
  120             (bkgd, interpStyle, undersampleStyle, approxStyle, approxOrderX, approxOrderY,
 
  121              approxWeighting) = bkgd
 
  123             statsImage = bkgd.getStatsImage()
 
  126             md.set(
"INTERPSTYLE", int(interpStyle))
 
  127             md.set(
"UNDERSAMPLESTYLE", int(undersampleStyle))
 
  128             md.set(
"APPROXSTYLE", int(approxStyle))
 
  129             md.set(
"APPROXORDERX", approxOrderX)
 
  130             md.set(
"APPROXORDERY", approxOrderY)
 
  131             md.set(
"APPROXWEIGHTING", approxWeighting)
 
  132             bbox = bkgd.getImageBBox()
 
  133             md.set(
"BKGD_X0", bbox.getMinX())
 
  134             md.set(
"BKGD_Y0", bbox.getMinY())
 
  135             md.set(
"BKGD_WIDTH", bbox.getWidth())
 
  136             md.set(
"BKGD_HEIGHT", bbox.getHeight())
 
  138             statsImage.getImage().
writeFits(fileName, md, 
"w" if i == 0 
else "a")
 
  139             statsImage.getMask().
writeFits(fileName, md, 
"a")
 
  140             statsImage.getVariance().
writeFits(fileName, md, 
"a")
 
  144         """Read our list of Backgrounds from a file. 
  151             First Header/Data Unit to attempt to read from 
  153             Flags to control details of reading; currently unused, but present 
  154             for consistency with `lsst.afw.table.BaseCatalog.readFits`. 
  160         if not isinstance(fileName, MemFileManager) 
and not os.path.exists(fileName):
 
  161             raise RuntimeError(f
"File not found: {fileName}")
 
  165         f = 
Fits(fileName, 
'r')
 
  166         nHdus = f.countHdus()
 
  169             raise RuntimeError(f
"BackgroundList FITS file {fileName} has {nHdus} HDUs;" 
  170                                f
"expected a multiple of 3 (compression is not supported).")
 
  172         for hdu 
in range(0, nHdus, 3):
 
  179             statsImage = afwImage.MaskedImageF(imageReader.read(), maskReader.read(), varianceReader.read())
 
  180             md = imageReader.readMetadata()
 
  184             width = md[
"BKGD_WIDTH"]
 
  185             height = md[
"BKGD_HEIGHT"]
 
  188             interpStyle = afwMath.Interpolate.Style(md[
"INTERPSTYLE"])
 
  195             approxStyle = md.get(
"APPROXSTYLE", afwMath.ApproximateControl.UNKNOWN)
 
  196             approxStyle = afwMath.ApproximateControl.Style(approxStyle)
 
  197             approxOrderX = md.get(
"APPROXORDERX", 1)
 
  198             approxOrderY = md.get(
"APPROXORDERY", -1)
 
  199             approxWeighting = md.get(
"APPROXWEIGHTING", 
True)
 
  202             bctrl = bkgd.getBackgroundControl()
 
  203             bctrl.setInterpStyle(interpStyle)
 
  204             bctrl.setUndersampleStyle(undersampleStyle)
 
  206             bctrl.setApproximateControl(actrl)
 
  207             bgInfo = (bkgd, interpStyle, undersampleStyle, approxStyle,
 
  208                       approxOrderX, approxOrderY, approxWeighting)
 
  214         """Compute and return a full-resolution image from our list of 
  215         (Background, interpStyle, undersampleStyle). 
  219         for (bkgd, interpStyle, undersampleStyle, approxStyle,
 
  220              approxOrderX, approxOrderY, approxWeighting) 
in self:
 
  222                 if approxStyle != afwMath.ApproximateControl.UNKNOWN:
 
  223                     bkgdImage = bkgd.getImageF()
 
  225                     bkgdImage = bkgd.getImageF(interpStyle, undersampleStyle)
 
  227                 if approxStyle != afwMath.ApproximateControl.UNKNOWN:
 
  228                     bkgdImage += bkgd.getImageF()
 
  230                     bkgdImage += bkgd.getImageF(interpStyle, undersampleStyle)