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