lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
camera.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2014 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 from __future__ import absolute_import, division, print_function
23 from builtins import range
24 from .cameraGeomLib import CameraPoint, CameraSysPrefix, PIXELS
25 from .detectorCollection import DetectorCollection
26 from .pupil import PupilFactory
27 import lsst.afw.geom as afwGeom
28 
29 
30 class Camera(DetectorCollection):
31  """!A collection of Detectors that also supports coordinate transformation
32  """
33 
34  def __init__(self, name, detectorList, transformMap, pupilFactoryClass=PupilFactory):
35  """!Construct a Camera
36 
37  @param[in] name name of camera
38  @param[in] detectorList a sequence of detectors in index order
39  @param[in] transformMap a CameraTransformMap whose native system is FOCAL_PLANE
40  and that at least supports PUPIL coordinates
41  @param[in] pupilFactoryClass a PupilFactory class for this camera
42  [default: afw.cameraGeom.PupilFactory]
43  """
44  self._name = name
45  self._transformMap = transformMap
46  self._nativeCameraSys = self._transformMap.getNativeCoordSys()
47  self._pupilFactoryClass = pupilFactoryClass
48  super(Camera, self).__init__(detectorList)
49 
50  def getName(self):
51  """!Return the camera name
52  """
53  return self._name
54 
55  def getPupilFactory(self, visitInfo, pupilSize, npix):
56  """!Construct a PupilFactory.
57 
58  @param[in] visitInfo VisitInfo object for a particular exposure.
59  @param[in] pupilSize Size in meters of constructed Pupil array.
60  Note that this may be larger than the actual
61  diameter of the illuminated pupil to
62  accommodate zero-padding.
63  @param[in] npix Constructed Pupils will be npix x npix.
64  """
65  return self._pupilFactoryClass(visitInfo, pupilSize, npix)
66 
67  def _transformFromNativeSys(self, nativePoint, toSys):
68  """!Transform a point in the native coordinate system to another coordinate system.
69 
70  @param[in] nativePoint CameraPoint in the native system for the camera
71  @param[in] toSys destination CameraSys
72  @return CameraPoint in the destination CameraSys
73  """
74  if isinstance(toSys, CameraSysPrefix):
75  # Must be in a detector. Find the detector and transform it.
76  detList = self.findDetectors(nativePoint)
77  if len(detList) == 0:
78  raise RuntimeError("No detectors found")
79  elif len(detList) > 1:
80  raise RuntimeError("More than one detector found")
81  else:
82  det = detList[0]
83  return det.transform(nativePoint, toSys)
84  else:
85  return self._transformSingleSys(nativePoint, toSys)
86 
87  def _transformSingleSysArray(self, positionList, fromSys, toSys):
88  """!Transform an array of points from once CameraSys to another CameraSys
89  @warning This method only handles a single jump, not a transform linked by a common native sys.
90 
91  @param[in] positionList List of Point2D objects, one per position
92  @param[in] fromSys Initial camera coordinate system
93  @param[in] toSys Destination camera coordinate system
94 
95  @returns an array of Point2D objects containing the transformed coordinates in the destination system.
96  """
97  if fromSys.hasDetectorName():
98  det = self[fromSys.getDetectorname()]
99  detTrans = det.getTransfromMap()
100  return detTrans.transform(positionList, fromSys, toSys)
101  elif toSys.hasDetectorName():
102  det = self[toSys.getDetectorName()]
103  detTrans = det.getTransformMap()
104  return detTrans.transform(positionList, fromSys, toSys)
105  elif toSys in self._transformMap:
106  # use camera transform map
107  return self._transformMap.transform(positionList, fromSys, toSys)
108  raise RuntimeError(
109  "Could not find mapping from %s to %s"%(fromSys, toSys))
110 
111  def _transformSingleSys(self, cameraPoint, toSys):
112  """!Transform a CameraPoint with a CameraSys to another CameraSys.
113 
114  @warning This method only handles a single jump, not a transform linked by a common native sys.
115 
116  @param[in] cameraPoint CameraPoint to transform
117  @param[in] toSys Destination coordinate system
118  """
119  fromSys = cameraPoint.getCameraSys()
120  if fromSys.hasDetectorName():
121  # use from detector to transform
122  det = self[fromSys.getDetectorName()]
123  return det.transform(cameraPoint, toSys)
124  elif toSys.hasDetectorName():
125  # use the to detector to transform
126  det = self[toSys.getDetectorName()]
127  return det.transform(cameraPoint, toSys)
128  elif toSys in self._transformMap:
129  # use camera transform map
130  outPoint = self._transformMap.transform(
131  cameraPoint.getPoint(), cameraPoint.getCameraSys(), toSys)
132  return CameraPoint(outPoint, toSys)
133  raise RuntimeError("Could not find mapping from %s to %s" %
134  (cameraPoint.getCameraSys(), toSys))
135 
136  def findDetectors(self, cameraPoint):
137  """!Find the detectors that cover a given cameraPoint, or empty list
138 
139  @param[in] cameraPoint position to use in lookup
140  @return a list of zero or more Detectors that overlap the specified point
141  """
142  # first convert to focalPlane since the point may be in another
143  # overlapping detector
144  nativePoint = self._transformSingleSys(
145  cameraPoint, self._nativeCameraSys)
146 
147  detectorList = []
148  for detector in self:
149  cameraSys = detector.makeCameraSys(PIXELS)
150  detPoint = detector.transform(nativePoint, cameraSys)
151  # This is safe because CameraPoint is not templated and getPoint()
152  # returns a Point2D.
153  if afwGeom.Box2D(detector.getBBox()).contains(detPoint.getPoint()):
154  detectorList.append(detector)
155  return detectorList
156 
157  def findDetectorsList(self, cameraPointList, coordSys):
158  """!Find the detectors that cover a list of points specified by x and y coordinates in any system
159 
160  @param[in] cameraPointList a list of cameraPoints
161  @param[in] coordSys the camera coordinate system in which cameraPointList is defined
162  @return a list of lists; each list contains the names of all detectors which contain the
163  corresponding point
164  """
165 
166  # transform the points to the native coordinate system
167  nativePointList = self._transformSingleSysArray(
168  cameraPointList, coordSys, self._nativeCameraSys)
169 
170  detectorList = []
171  for i in range(len(cameraPointList)):
172  detectorList.append([])
173 
174  for detector in self:
175  coordMap = detector.getTransformMap()
176  cameraSys = detector.makeCameraSys(PIXELS)
177  detectorPointList = coordMap.transform(
178  nativePointList, self._nativeCameraSys, cameraSys)
179  box = afwGeom.Box2D(detector.getBBox())
180  for i, pt in enumerate(detectorPointList):
181  if box.contains(pt):
182  detectorList[i].append(detector)
183 
184  return detectorList
185 
186  def getTransformMap(self):
187  """!Obtain the transform registry.
188 
189  @return a TransformMap
190 
191  @note: TransformRegistries are immutable, so this should be safe.
192  """
193  return self._transformMap
194 
195  def transform(self, cameraPoint, toSys):
196  """!Transform a CameraPoint to a different CameraSys
197 
198  @param[in] cameraPoint CameraPoint to transform
199  @param[in] toSys Transform to this CameraSys
200  @return a CameraPoint in the the specified CameraSys
201  """
202  # All transform maps should know about the native coordinate system
203  nativePoint = self._transformSingleSys(
204  cameraPoint, self._nativeCameraSys)
205  return self._transformFromNativeSys(nativePoint, toSys)
206 
207  def makeCameraPoint(self, point, cameraSys):
208  """!Make a CameraPoint from a Point2D and a CameraSys
209 
210  @param[in] point an lsst.afw.geom.Point2D
211  @param[in] cameraSys a CameraSys
212  @return the CameraPoint
213  """
214  if isinstance(cameraSys, CameraSysPrefix):
215  raise TypeError(
216  "Use the detector method to make a camera point from a CameraSysPrefix.")
217  if cameraSys in self._transformMap:
218  return CameraPoint(point, cameraSys)
219  if cameraSys.hasDetectorName():
220  if cameraSys in self[cameraSys.getDetectorName()].getTransformMap():
221  return CameraPoint(point, cameraSys)
222  raise RuntimeError("Could not find %s in any transformMap"%(cameraSys))
def makeCameraPoint
Make a CameraPoint from a Point2D and a CameraSys.
Definition: camera.py:207
def getPupilFactory
Construct a PupilFactory.
Definition: camera.py:55
def getTransformMap
Obtain the transform registry.
Definition: camera.py:186
def _transformSingleSysArray
Transform an array of points from once CameraSys to another CameraSys.
Definition: camera.py:87
def findDetectorsList
Find the detectors that cover a list of points specified by x and y coordinates in any system...
Definition: camera.py:157
def transform
Transform a CameraPoint to a different CameraSys.
Definition: camera.py:195
def __init__
Construct a Camera.
Definition: camera.py:34
A collection of Detectors that also supports coordinate transformation.
Definition: camera.py:30
def findDetectors
Find the detectors that cover a given cameraPoint, or empty list.
Definition: camera.py:136
def getName
Return the camera name.
Definition: camera.py:50
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
def _transformSingleSys
Transform a CameraPoint with a CameraSys to another CameraSys.
Definition: camera.py:111
def _transformFromNativeSys
Transform a point in the native coordinate system to another coordinate system.
Definition: camera.py:67