LSSTApplications  18.1.0
LSSTDataManagementBasePackage
assembleImage.py
Go to the documentation of this file.
1 # This file is part of afw.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (https://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 
22 __all__ = ['assembleAmplifierImage', 'assembleAmplifierRawImage',
23  'makeUpdatedDetector']
24 
25 import lsst.geom
26 from . import copyDetector
27 
28 # dict of doFlip: slice
29 _SliceDict = {
30  False: slice(None, None, 1),
31  True: slice(None, None, -1),
32 }
33 
34 
35 def _insertPixelChunk(outView, inView, amplifier, hasArrays):
36  # For the sake of simplicity and robustness, this code does not short-circuit the case flipX=flipY=False.
37  # However, it would save a bit of time, including the cost of making numpy array views.
38  # If short circuiting is wanted, do it here.
39 
40  xSlice = _SliceDict[amplifier.getRawFlipX()]
41  ySlice = _SliceDict[amplifier.getRawFlipY()]
42  if hasArrays:
43  # MaskedImage
44  inArrList = inView.getArrays()
45  outArrList = outView.getArrays()
46  else:
47  inArrList = [inView.getArray()]
48  outArrList = [outView.getArray()]
49 
50  for inArr, outArr in zip(inArrList, outArrList):
51  # y,x because numpy arrays are transposed w.r.t. afw Images
52  outArr[:] = inArr[ySlice, xSlice]
53 
54 
55 def assembleAmplifierImage(destImage, rawImage, amplifier):
56  """Assemble the amplifier region of an image from a raw image.
57 
58  Parameters
59  ----------
60  destImage : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
61  Assembled image; the region amplifier.getBBox() is overwritten with
62  the assembled amplifier image.
63  rawImage : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
64  Raw image (same type as destImage).
65  amplifier : `lsst.afw.table.AmpInfoRecord`
66  Amplifier geometry, with raw amplifier info.
67 
68  Raises
69  ------
70  RuntimeError
71  Raised if image types do not match or amplifier has no raw amplifier info.
72  """
73  if not amplifier.getHasRawInfo():
74  raise RuntimeError("amplifier must contain raw amplifier info")
75  if type(destImage.Factory) != type(rawImage.Factory): # noqa: E721
76  raise RuntimeError("destImage type = %s != %s = rawImage type" %
77  type(destImage.Factory).__name__, type(rawImage.Factory).__name__)
78  inView = rawImage.Factory(rawImage, amplifier.getRawDataBBox())
79  outView = destImage.Factory(destImage, amplifier.getBBox())
80 
81  _insertPixelChunk(outView, inView, amplifier,
82  hasattr(rawImage, "getArrays"))
83 
84 
85 def assembleAmplifierRawImage(destImage, rawImage, amplifier):
86  """Assemble the amplifier region of a raw CCD image.
87 
88  For most cameras this is a no-op: the raw image already is an assembled
89  CCD image.
90  However, it is useful for camera such as LSST for which each amplifier
91  image is a separate image.
92 
93  Parameters
94  ----------
95  destImage : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
96  CCD Image; the region amplifier.getRawAmplifier().getBBox()
97  is overwritten with the raw amplifier image.
98  rawImage : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
99  Raw image (same type as destImage).
100  amplifier : `lsst.afw.table.AmpInfoRecord`
101  Amplifier geometry with raw amplifier info
102 
103  Raises
104  ------
105  RuntimeError
106  Raised if image types do not match or amplifier has no raw amplifier info.
107  """
108  if not amplifier.getHasRawInfo():
109  raise RuntimeError("amplifier must contain raw amplifier info")
110  if type(destImage.Factory) != type(rawImage.Factory): # noqa: E721
111  raise RuntimeError("destImage type = %s != %s = rawImage type" %
112  type(destImage.Factory).__name__, type(rawImage.Factory).__name__)
113  inBBox = amplifier.getRawBBox()
114  inView = rawImage.Factory(rawImage, inBBox)
115  outBBox = amplifier.getRawBBox()
116  outBBox.shift(amplifier.getRawXYOffset())
117  outView = destImage.Factory(destImage, outBBox)
118 
119  _insertPixelChunk(outView, inView, amplifier,
120  hasattr(rawImage, "getArrays"))
121 
122 
124  """Return a Detector that has had the definitions of amplifier geometry
125  updated post assembly.
126 
127  Parameters
128  ----------
129  ccd : `lsst.afw.image.Detector`
130  The detector to copy and update.
131  """
132  ampInfoCatalog = ccd.getAmpInfoCatalog().copy(deep=True)
133 
134  for amp in ampInfoCatalog:
135  assert amp.getHasRawInfo()
136 
137  bbox = amp.getRawBBox()
138  awidth, aheight = bbox.getDimensions()
139  #
140  # Figure out how far flipping the amp LR and/or TB offsets the bboxes
141  #
142  boxMin0 = bbox.getMin() # initial position of rawBBox's LLC corner
143  if amp.getRawFlipX():
144  bbox.flipLR(awidth)
145  if amp.getRawFlipY():
146  bbox.flipTB(aheight)
147  shift = boxMin0 - bbox.getMin()
148 
149  for bboxName in ("",
150  "HorizontalOverscan",
151  "Data",
152  "VerticalOverscan",
153  "Prescan"):
154  bbox = getattr(amp, "getRaw%sBBox" % bboxName)()
155  if amp.getRawFlipX():
156  bbox.flipLR(awidth)
157  if amp.getRawFlipY():
158  bbox.flipTB(aheight)
159  bbox.shift(amp.getRawXYOffset() + shift)
160 
161  getattr(amp, "setRaw%sBBox" % bboxName)(bbox)
162  #
163  # All of these have now been transferred to the per-amp geometry
164  #
165  amp.setRawXYOffset(lsst.geom.ExtentI(0, 0))
166  amp.setRawFlipX(False)
167  amp.setRawFlipY(False)
168 
169  return copyDetector(ccd, ampInfoCatalog)
def copyDetector(detector, ampInfoCatalog=None)
table::Key< int > type
Definition: Detector.cc:167
def assembleAmplifierImage(destImage, rawImage, amplifier)
def assembleAmplifierRawImage(destImage, rawImage, amplifier)