LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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
23 from builtins import range
24 from .cameraGeomLib import CameraPoint, CameraSysPrefix, PIXELS
25 from .detectorCollection import DetectorCollection
26 import lsst.afw.geom as afwGeom
27 
28 class Camera(DetectorCollection):
29  """!A collection of Detectors that also supports coordinate transformation
30  """
31  def __init__(self, name, detectorList, transformMap):
32  """!Construct a Camera
33 
34  @param[in] name name of camera
35  @param[in] detectorList a sequence of detectors in index order
36  @param[in] transformMap a CameraTransformMap whose native system is FOCAL_PLANE
37  and that at least supports PUPIL coordinates
38  """
39  self._name = name
40  self._transformMap = transformMap
41  self._nativeCameraSys = self._transformMap.getNativeCoordSys()
42  super(Camera, self).__init__(detectorList)
43 
44  def getName(self):
45  """!Return the camera name
46  """
47  return self._name
48 
49  def _transformFromNativeSys(self, nativePoint, toSys):
50  """!Transform a point in the native coordinate system to another coordinate system.
51 
52  @param[in] nativePoint CameraPoint in the native system for the camera
53  @param[in] toSys destination CameraSys
54  @return CameraPoint in the destination CameraSys
55  """
56  if isinstance(toSys, CameraSysPrefix):
57  # Must be in a detector. Find the detector and transform it.
58  detList = self.findDetectors(nativePoint)
59  if len(detList) == 0:
60  raise RuntimeError("No detectors found")
61  elif len(detList) > 1:
62  raise RuntimeError("More than one detector found")
63  else:
64  det = detList[0]
65  return det.transform(nativePoint, toSys)
66  else:
67  return self._transformSingleSys(nativePoint, toSys)
68 
69  def _transformSingleSysArray(self, positionList, fromSys, toSys):
70  """!Transform an array of points from once CameraSys to another CameraSys
71  @warning This method only handles a single jump, not a transform linked by a common native sys.
72 
73  @param[in] positionList List of Point2D objects, one per position
74  @param[in] fromSys Initial camera coordinate system
75  @param[in] toSys Destination camera coordinate system
76 
77  @returns an array of Point2D objects containing the transformed coordinates in the destination system.
78  """
79  if fromSys.hasDetectorName():
80  det = self[fromSys.getDetectorname()]
81  detTrans = det.getTransfromMap()
82  return detTrans.transform(positionList, fromSys, toSys)
83  elif toSys.hasDetectorName():
84  det = self[toSys.getDetectorName()]
85  detTrans = det.getTransformMap()
86  return detTrans.transform(positionList, fromSys, toSys)
87  elif toSys in self._transformMap:
88  # use camera transform map
89  return self._transformMap.transform(positionList, fromSys, toSys)
90  raise RuntimeError("Could not find mapping from %s to %s"%(fromSys, toSys))
91 
92  def _transformSingleSys(self, cameraPoint, toSys):
93  """!Transform a CameraPoint with a CameraSys to another CameraSys.
94 
95  @warning This method only handles a single jump, not a transform linked by a common native sys.
96 
97  @param[in] cameraPoint CameraPoint to transform
98  @param[in] toSys Destination coordinate system
99  """
100  fromSys = cameraPoint.getCameraSys()
101  if fromSys.hasDetectorName():
102  # use from detector to transform
103  det = self[fromSys.getDetectorName()]
104  return det.transform(cameraPoint, toSys)
105  elif toSys.hasDetectorName():
106  # use the to detector to transform
107  det = self[toSys.getDetectorName()]
108  return det.transform(cameraPoint, toSys)
109  elif toSys in self._transformMap:
110  # use camera transform map
111  outPoint = self._transformMap.transform(cameraPoint.getPoint(), cameraPoint.getCameraSys(), toSys)
112  return CameraPoint(outPoint, toSys)
113  raise RuntimeError("Could not find mapping from %s to %s"%(cameraPoint.getCameraSys(), toSys))
114 
115  def findDetectors(self, cameraPoint):
116  """!Find the detectors that cover a given cameraPoint, or empty list
117 
118  @param[in] cameraPoint position to use in lookup
119  @return a list of zero or more Detectors that overlap the specified point
120  """
121  # first convert to focalPlane since the point may be in another overlapping detector
122  nativePoint = self._transformSingleSys(cameraPoint, self._nativeCameraSys)
123 
124  detectorList = []
125  for detector in self:
126  cameraSys = detector.makeCameraSys(PIXELS)
127  detPoint = detector.transform(nativePoint, cameraSys)
128  #This is safe because CameraPoint is not templated and getPoint() returns a Point2D.
129  if afwGeom.Box2D(detector.getBBox()).contains(detPoint.getPoint()):
130  detectorList.append(detector)
131  return detectorList
132 
133  def findDetectorsList(self, cameraPointList, coordSys):
134  """!Find the detectors that cover a list of points specified by x and y coordinates in any system
135 
136  @param[in] cameraPointList a list of cameraPoints
137  @param[in] coordSys the camera coordinate system in which cameraPointList is defined
138  @return a list of lists; each list contains the names of all detectors which contain the
139  corresponding point
140  """
141 
142  #transform the points to the native coordinate system
143  nativePointList = self._transformSingleSysArray(cameraPointList, coordSys, self._nativeCameraSys)
144 
145  detectorList = []
146  for i in range(len(cameraPointList)):
147  detectorList.append([])
148 
149  for detector in self:
150  coordMap = detector.getTransformMap()
151  cameraSys = detector.makeCameraSys(PIXELS)
152  detectorPointList = coordMap.transform(nativePointList, self._nativeCameraSys, cameraSys)
153  box = afwGeom.Box2D(detector.getBBox())
154  for i, pt in enumerate(detectorPointList):
155  if box.contains(pt):
156  detectorList[i].append(detector)
157 
158  return detectorList
159 
160  def getTransformMap(self):
161  """!Obtain the transform registry.
162 
163  @return a TransformMap
164 
165  @note: TransformRegistries are immutable, so this should be safe.
166  """
167  return self._transformMap
168 
169  def transform(self, cameraPoint, toSys):
170  """!Transform a CameraPoint to a different CameraSys
171 
172  @param[in] cameraPoint CameraPoint to transform
173  @param[in] toSys Transform to this CameraSys
174  @return a CameraPoint in the the specified CameraSys
175  """
176  # All transform maps should know about the native coordinate system
177  nativePoint = self._transformSingleSys(cameraPoint, self._nativeCameraSys)
178  return self._transformFromNativeSys(nativePoint, toSys)
179 
180  def makeCameraPoint(self, point, cameraSys):
181  """!Make a CameraPoint from a Point2D and a CameraSys
182 
183  @param[in] point an lsst.afw.geom.Point2D
184  @param[in] cameraSys a CameraSys
185  @return the CameraPoint
186  """
187  if isinstance(cameraSys, CameraSysPrefix):
188  raise TypeError("Use the detector method to make a camera point from a CameraSysPrefix.")
189  if cameraSys in self._transformMap:
190  return CameraPoint(point, cameraSys)
191  if cameraSys.hasDetectorName():
192  if cameraSys in self[cameraSys.getDetectorName()].getTransformMap():
193  return CameraPoint(point, cameraSys)
194  raise RuntimeError("Could not find %s in any transformMap"%(cameraSys))
def makeCameraPoint
Make a CameraPoint from a Point2D and a CameraSys.
Definition: camera.py:180
def getTransformMap
Obtain the transform registry.
Definition: camera.py:160
def _transformSingleSysArray
Transform an array of points from once CameraSys to another CameraSys.
Definition: camera.py:69
def findDetectorsList
Find the detectors that cover a list of points specified by x and y coordinates in any system...
Definition: camera.py:133
def transform
Transform a CameraPoint to a different CameraSys.
Definition: camera.py:169
def __init__
Construct a Camera.
Definition: camera.py:31
A collection of Detectors that also supports coordinate transformation.
Definition: camera.py:28
def findDetectors
Find the detectors that cover a given cameraPoint, or empty list.
Definition: camera.py:115
def getName
Return the camera name.
Definition: camera.py:44
A floating-point coordinate rectangle geometry.
Definition: Box.h:271
def _transformSingleSys
Transform a CameraPoint with a CameraSys to another CameraSys.
Definition: camera.py:92
def _transformFromNativeSys
Transform a point in the native coordinate system to another coordinate system.
Definition: camera.py:49