lsst.afw  14.0-35-g29d3a87a7
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, FOCAL_PLANE, PIXELS
25 from .detectorCollection import DetectorCollection
26 from .pupil import PupilFactory
27 import lsst.afw.geom as afwGeom
28 
29 
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 TransformMap that at least supports
40  FOCAL_PLANE and FIELD_ANGLE 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 = FOCAL_PLANE
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, **kwargs):
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, **kwargs)
66 
67  @property
68  def telescopeDiameter(self):
69  return self._pupilFactoryClass.telescopeDiameter
70 
71  def _transformFromNativeSys(self, nativePoint, toSys):
72  """!Transform a point in the native coordinate system to another coordinate system.
73 
74  @param[in] nativePoint CameraPoint in the native system for the camera
75  @param[in] toSys destination CameraSys
76  @return CameraPoint in the destination CameraSys
77  """
78  if isinstance(toSys, CameraSysPrefix):
79  # Must be in a detector. Find the detector and transform it.
80  detList = self.findDetectors(nativePoint)
81  if len(detList) == 0:
82  raise RuntimeError("No detectors found")
83  elif len(detList) > 1:
84  raise RuntimeError("More than one detector found")
85  else:
86  det = detList[0]
87  return det.transform(nativePoint, toSys)
88  else:
89  return self._transformSingleSys(nativePoint, toSys)
90 
91  def _transformSingleSysArray(self, positionList, fromSys, toSys):
92  """!Transform an array of points from once CameraSys to another CameraSys
93  @warning This method only handles a single jump, not a transform linked by a common native sys.
94 
95  @param[in] positionList List of Point2D objects, one per position
96  @param[in] fromSys Initial camera coordinate system
97  @param[in] toSys Destination camera coordinate system
98 
99  @returns an array of Point2D objects containing the transformed coordinates in the destination system.
100  """
101  if fromSys.hasDetectorName():
102  det = self[fromSys.getDetectorname()]
103  detTrans = det.getTransfromMap()
104  return detTrans.transform(positionList, fromSys, toSys)
105  elif toSys.hasDetectorName():
106  det = self[toSys.getDetectorName()]
107  detTrans = det.getTransformMap()
108  return detTrans.transform(positionList, fromSys, toSys)
109  elif toSys in self._transformMap:
110  # use camera transform map
111  return self._transformMap.transform(positionList, fromSys, toSys)
112  raise RuntimeError(
113  "Could not find mapping from %s to %s"%(fromSys, toSys))
114 
115  def _transformSingleSys(self, cameraPoint, toSys):
116  """!Transform a CameraPoint with a CameraSys to another CameraSys.
117 
118  @warning This method only handles a single jump, not a transform linked by a common native sys.
119 
120  @param[in] cameraPoint CameraPoint to transform
121  @param[in] toSys Destination coordinate system
122  """
123  fromSys = cameraPoint.getCameraSys()
124  if fromSys.hasDetectorName():
125  # use from detector to transform
126  det = self[fromSys.getDetectorName()]
127  return det.transform(cameraPoint, toSys)
128  elif toSys.hasDetectorName():
129  # use the to detector to transform
130  det = self[toSys.getDetectorName()]
131  return det.transform(cameraPoint, toSys)
132  elif toSys in self._transformMap:
133  # use camera transform map
134  outPoint = self._transformMap.transform(
135  cameraPoint.getPoint(), cameraPoint.getCameraSys(), toSys)
136  return CameraPoint(outPoint, toSys)
137  raise RuntimeError("Could not find mapping from %s to %s" %
138  (cameraPoint.getCameraSys(), toSys))
139 
140  def findDetectors(self, cameraPoint):
141  """!Find the detectors that cover a given cameraPoint, or empty list
142 
143  @param[in] cameraPoint position to use in lookup
144  @return a list of zero or more Detectors that overlap the specified point
145  """
146  # first convert to focalPlane since the point may be in another
147  # overlapping detector
148  nativePoint = self._transformSingleSys(
149  cameraPoint, self._nativeCameraSys)
150 
151  detectorList = []
152  for detector in self:
153  cameraSys = detector.makeCameraSys(PIXELS)
154  detPoint = detector.transform(nativePoint, cameraSys)
155  # This is safe because CameraPoint is not templated and getPoint()
156  # returns a Point2D.
157  if afwGeom.Box2D(detector.getBBox()).contains(detPoint.getPoint()):
158  detectorList.append(detector)
159  return detectorList
160 
161  def findDetectorsList(self, cameraPointList, coordSys):
162  """!Find the detectors that cover a list of points specified by x and y coordinates in any system
163 
164  @param[in] cameraPointList a list of cameraPoints
165  @param[in] coordSys the camera coordinate system in which cameraPointList is defined
166  @return a list of lists; each list contains the names of all detectors which contain the
167  corresponding point
168  """
169 
170  # transform the points to the native coordinate system
171  nativePointList = self._transformSingleSysArray(
172  cameraPointList, coordSys, self._nativeCameraSys)
173 
174  detectorList = []
175  for i in range(len(cameraPointList)):
176  detectorList.append([])
177 
178  for detector in self:
179  coordMap = detector.getTransformMap()
180  cameraSys = detector.makeCameraSys(PIXELS)
181  detectorPointList = coordMap.transform(
182  nativePointList, self._nativeCameraSys, cameraSys)
183  box = afwGeom.Box2D(detector.getBBox())
184  for i, pt in enumerate(detectorPointList):
185  if box.contains(pt):
186  detectorList[i].append(detector)
187 
188  return detectorList
189 
190  def getTransformMap(self):
191  """!Obtain the transform registry.
192 
193  @return a TransformMap
194 
195  @note: TransformRegistries are immutable, so this should be safe.
196  """
197  return self._transformMap
198 
199  def transform(self, cameraPoint, toSys):
200  """!Transform a CameraPoint to a different CameraSys
201 
202  @param[in] cameraPoint CameraPoint to transform
203  @param[in] toSys Transform to this CameraSys
204  @return a CameraPoint in the the specified CameraSys
205  """
206  # All transform maps should know about the native coordinate system
207  nativePoint = self._transformSingleSys(
208  cameraPoint, self._nativeCameraSys)
209  return self._transformFromNativeSys(nativePoint, toSys)
210 
211  def makeCameraPoint(self, point, cameraSys):
212  """!Make a CameraPoint from a Point2D and a CameraSys
213 
214  @param[in] point an lsst.afw.geom.Point2D
215  @param[in] cameraSys a CameraSys
216  @return the CameraPoint
217  """
218  if isinstance(cameraSys, CameraSysPrefix):
219  raise TypeError(
220  "Use the detector method to make a camera point from a CameraSysPrefix.")
221  if cameraSys in self._transformMap:
222  return CameraPoint(point, cameraSys)
223  if cameraSys.hasDetectorName():
224  if cameraSys in self[cameraSys.getDetectorName()].getTransformMap():
225  return CameraPoint(point, cameraSys)
226  raise RuntimeError("Could not find %s in any transformMap"%(cameraSys))
def _transformFromNativeSys(self, nativePoint, toSys)
Transform a point in the native coordinate system to another coordinate system.
Definition: camera.py:71
def __init__(self, name, detectorList, transformMap, pupilFactoryClass=PupilFactory)
Construct a Camera.
Definition: camera.py:34
def getTransformMap(self)
Obtain the transform registry.
Definition: camera.py:190
def findDetectors(self, cameraPoint)
Find the detectors that cover a given cameraPoint, or empty list.
Definition: camera.py:140
def getName(self)
Return the camera name.
Definition: camera.py:50
def getPupilFactory(self, visitInfo, pupilSize, npix, kwargs)
Construct a PupilFactory.
Definition: camera.py:55
def _transformSingleSysArray(self, positionList, fromSys, toSys)
Transform an array of points from once CameraSys to another CameraSys.
Definition: camera.py:91
def makeCameraPoint(self, point, cameraSys)
Make a CameraPoint from a Point2D and a CameraSys.
Definition: camera.py:211
def findDetectorsList(self, cameraPointList, coordSys)
Find the detectors that cover a list of points specified by x and y coordinates in any system...
Definition: camera.py:161
def transform(self, cameraPoint, toSys)
Transform a CameraPoint to a different CameraSys.
Definition: camera.py:199
def _transformSingleSys(self, cameraPoint, toSys)
Transform a CameraPoint with a CameraSys to another CameraSys.
Definition: camera.py:115
A collection of Detectors that also supports coordinate transformation.
Definition: camera.py:30
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
An immutable collection of Detectors that can be accessed by name or ID.