22 __all__ = [
"BackgroundList"]
31 from .
import mathLib
as afwMath
35 """A list-like class to contain a list of (`lsst.afw.math.Background`,
36 `lsst.afw.math.Interpolate.Style`, `~lsst.afw.math.UndersampleStyle`)
41 *args : `tuple` or `~lsst.afw.math.Background`
42 A sequence of arguments, each of which becomes an element of the list.
43 In deference to the deprecated-but-not-yet-removed
44 `~lsst.afw.math.Background.getImageF()` API, we also accept a single
45 `lsst.afw.math.Background` and extract the ``interpStyle`` and
46 ``undersampleStyle`` from the as-used values.
66 bkgd, interpStyle, undersampleStyle, approxStyle, \
67 approxOrderX, approxOrderY, approxWeighting = val
68 if interpStyle
is None or undersampleStyle
is None:
69 interpStyle = bkgd.getAsUsedInterpStyle()
70 undersampleStyle = bkgd.getAsUsedUndersampleStyle()
71 actrl = bkgd.getBackgroundControl().getApproximateControl()
72 approxStyle = actrl.getStyle()
73 approxOrderX = actrl.getOrderX()
74 approxOrderY = actrl.getOrderY()
75 approxWeighting = actrl.getWeighting()
76 self.
_backgrounds[i] = (bkgd, interpStyle, undersampleStyle,
77 approxStyle, approxOrderX, approxOrderY, approxWeighting)
88 bkgd, interpStyle, undersampleStyle, approxStyle, \
89 approxOrderX, approxOrderY, approxWeighting = val
91 warnings.warn(
"Passing Background objects to BackgroundList is deprecated; "
92 "use a (Background, Interpolation.Style, UndersampleStyle, "
93 "ApproximateControl.Style, int, int, bool) tuple instead.",
94 category=FutureWarning, stacklevel=2)
97 undersampleStyle =
None
101 approxWeighting =
None
103 bgInfo = (bkgd, interpStyle, undersampleStyle, approxStyle,
104 approxOrderX, approxOrderY, approxWeighting)
108 """Return a shallow copy
110 Shallow copies do not share backgrounds that are appended after copying,
111 but do share changes to contained background objects.
116 """Save our list of Backgrounds to a file.
123 Flags to control details of writing; currently unused, but present
124 for consistency with `lsst.afw.table.BaseCatalog.writeFits`.
127 for i, bkgd
in enumerate(self):
128 (bkgd, interpStyle, undersampleStyle, approxStyle, approxOrderX, approxOrderY,
129 approxWeighting) = bkgd
131 statsImage = bkgd.getStatsImage()
134 md.set(
"INTERPSTYLE", int(interpStyle))
135 md.set(
"UNDERSAMPLESTYLE", int(undersampleStyle))
136 md.set(
"APPROXSTYLE", int(approxStyle))
137 md.set(
"APPROXORDERX", approxOrderX)
138 md.set(
"APPROXORDERY", approxOrderY)
139 md.set(
"APPROXWEIGHTING", approxWeighting)
140 bbox = bkgd.getImageBBox()
141 md.set(
"BKGD_X0", bbox.getMinX())
142 md.set(
"BKGD_Y0", bbox.getMinY())
143 md.set(
"BKGD_WIDTH", bbox.getWidth())
144 md.set(
"BKGD_HEIGHT", bbox.getHeight())
146 statsImage.getImage().
writeFits(fileName, md,
"w" if i == 0
else "a")
147 statsImage.getMask().
writeFits(fileName, md,
"a")
148 statsImage.getVariance().
writeFits(fileName, md,
"a")
152 """Read our list of Backgrounds from a file.
159 First Header/Data Unit to attempt to read from
161 Flags to control details of reading; currently unused, but present
162 for consistency with `lsst.afw.table.BaseCatalog.readFits`.
168 if not isinstance(fileName, MemFileManager)
and not os.path.exists(fileName):
169 raise RuntimeError(f
"File not found: {fileName}")
173 f =
Fits(fileName,
'r')
174 nHdus = f.countHdus()
177 raise RuntimeError(f
"BackgroundList FITS file {fileName} has {nHdus} HDUs;"
178 f
"expected a multiple of 3 (compression is not supported).")
180 for hdu
in range(0, nHdus, 3):
187 statsImage = afwImage.MaskedImageF(imageReader.read(), maskReader.read(), varianceReader.read())
188 md = imageReader.readMetadata()
192 width = md[
"BKGD_WIDTH"]
193 height = md[
"BKGD_HEIGHT"]
196 interpStyle = afwMath.Interpolate.Style(md[
"INTERPSTYLE"])
203 approxStyle = md.get(
"APPROXSTYLE", afwMath.ApproximateControl.UNKNOWN)
204 approxStyle = afwMath.ApproximateControl.Style(approxStyle)
205 approxOrderX = md.get(
"APPROXORDERX", 1)
206 approxOrderY = md.get(
"APPROXORDERY", -1)
207 approxWeighting = md.get(
"APPROXWEIGHTING",
True)
210 bctrl = bkgd.getBackgroundControl()
214 with suppress_deprecations():
215 bctrl.setInterpStyle(interpStyle)
217 bctrl.setUndersampleStyle(undersampleStyle)
219 bctrl.setApproximateControl(actrl)
220 bgInfo = (bkgd, interpStyle, undersampleStyle, approxStyle,
221 approxOrderX, approxOrderY, approxWeighting)
227 """Compute and return a full-resolution image from our list of
228 (Background, interpStyle, undersampleStyle).
232 for (bkgd, interpStyle, undersampleStyle, approxStyle,
233 approxOrderX, approxOrderY, approxWeighting)
in self:
235 bkgdImage = bkgd.getImageF(interpStyle, undersampleStyle)
237 bkgdImage += bkgd.getImageF(interpStyle, undersampleStyle)