lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
assembleImage.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 #
3 # LSST Data Management System
4 # Copyright 2008, 2009, 2010 LSST Corporation.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
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 LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 from builtins import zip
24 import lsst.afw.geom as afwGeom
25 from . import copyDetector
26 
27 __ALL__ = ['assembleAmplifierImage', 'assembleAmplifierRawImage',
28  'updateAmpGeometryForAssembledCcd']
29 
30 # dict of doFlip: slice
31 _SliceDict = {
32  False: slice(None, None, 1),
33  True: slice(None, None, -1),
34 }
35 
36 
37 def _insertPixelChunk(outView, inView, amplifier, hasArrays):
38  # For the sake of simplicity and robustness, this code does not short-circuit the case flipX=flipY=False.
39  # However, it would save a bit of time, including the cost of making numpy array views.
40  # If short circuiting is wanted, do it here.
41 
42  xSlice = _SliceDict[amplifier.getRawFlipX()]
43  ySlice = _SliceDict[amplifier.getRawFlipY()]
44  if hasArrays:
45  # MaskedImage
46  inArrList = inView.getArrays()
47  outArrList = outView.getArrays()
48  else:
49  inArrList = [inView.getArray()]
50  outArrList = [outView.getArray()]
51 
52  for inArr, outArr in zip(inArrList, outArrList):
53  # y,x because numpy arrays are transposed w.r.t. afw Images
54  outArr[:] = inArr[ySlice, xSlice]
55 
56 
57 def assembleAmplifierImage(destImage, rawImage, amplifier):
58  """!Assemble the amplifier region of an image from a raw image
59 
60  @param[in,out] destImage assembled image (lsst.afw.image.Image or MaskedImage);
61  the region amplifier.getBBox() is overwritten with the assembled amplifier image
62  @param[in] rawImage raw image (same type as destImage)
63  @param[in] amplifier amplifier geometry: lsst.afw.cameraGeom.Amplifier with raw amplifier info
64 
65  @throw RuntimeError if:
66  - image types do not match
67  - amplifier has no raw amplifier info
68  """
69  if not amplifier.getHasRawInfo():
70  raise RuntimeError("amplifier must contain raw amplifier info")
71  if type(destImage.Factory) != type(rawImage.Factory):
72  raise RuntimeError("destImage type = %s != %s = rawImage type" %
73  type(destImage.Factory).__name__, type(rawImage.Factory).__name__)
74  inView = rawImage.Factory(rawImage, amplifier.getRawDataBBox())
75  outView = destImage.Factory(destImage, amplifier.getBBox())
76 
77  _insertPixelChunk(outView, inView, amplifier,
78  hasattr(rawImage, "getArrays"))
79 
80 
81 def assembleAmplifierRawImage(destImage, rawImage, amplifier):
82  """!Assemble the amplifier region of a raw CCD image
83 
84  For most cameras this is a no-op: the raw image already is an assembled CCD image.
85  However, it is useful for camera such as LSST for which each amplifier image is a separate image.
86 
87  @param[in,out] destImage CCD image (lsst.afw.image.Image or MaskedImage);
88  the region amplifier.getRawAmplifier().getBBox() is overwritten with the raw amplifier image
89  @param[in] rawImage raw image (same type as destImage)
90  @param[in] amplifier amplifier geometry: lsst.afw.cameraGeom.Amplifier with raw amplifier info
91 
92  @throw RuntimeError if:
93  - image types do not match
94  - amplifier has no raw amplifier info
95  """
96  if not amplifier.getHasRawInfo():
97  raise RuntimeError("amplifier must contain raw amplifier info")
98  if type(destImage.Factory) != type(rawImage.Factory):
99  raise RuntimeError("destImage type = %s != %s = rawImage type" %
100  type(destImage.Factory).__name__, type(rawImage.Factory).__name__)
101  inBBox = amplifier.getRawBBox()
102  inView = rawImage.Factory(rawImage, inBBox)
103  outBBox = amplifier.getRawBBox()
104  outBBox.shift(amplifier.getRawXYOffset())
105  outView = destImage.Factory(destImage, outBBox)
106 
107  _insertPixelChunk(outView, inView, amplifier,
108  hasattr(rawImage, "getArrays"))
109 
110 
112  """Return a Detector that has had the definitions of amplifier geometry updated post assembly
113  """
114  ampInfoCatalog = ccd.getAmpInfoCatalog().copy(deep=True)
115 
116  for amp in ampInfoCatalog:
117  assert amp.getHasRawInfo()
118  awidth, aheight = amp.getRawBBox().getDimensions()
119  for bboxName in ("",
120  "HorizontalOverscan",
121  "Data",
122  "VerticalOverscan",
123  "Prescan"):
124  bbox = getattr(amp, "getRaw%sBBox" % bboxName)()
125  if amp.getRawFlipX():
126  bbox.flipLR(awidth)
127  if amp.getRawFlipY():
128  bbox.flipTB(aheight)
129  bbox.shift(amp.getRawXYOffset())
130 
131  getattr(amp, "setRaw%sBBox" % bboxName)(bbox)
132  #
133  # All of these have now been transferred to the per-amp geometry
134  #
135  amp.setRawXYOffset(afwGeom.ExtentI(0, 0))
136  amp.setRawFlipX(False)
137  amp.setRawFlipY(False)
138 
139  return copyDetector(ccd, ampInfoCatalog)
def copyDetector
Return a copy of a Detector.
def assembleAmplifierRawImage
Assemble the amplifier region of a raw CCD image.
def assembleAmplifierImage
Assemble the amplifier region of an image from a raw image.