Coverage for python/lsst/cbp/maskInfo.py: 26%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of cbp.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
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 GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21"""MaskInfo class: information about a CBP mask"""
23__all__ = ["MaskInfo"]
25from collections import OrderedDict
27from lsst.geom import Point2D
30class MaskInfo:
31 """Information about a CBP mask.
33 Parameters
34 ----------
35 name : `str`
36 Name of mask.
37 defaultHole : `str` or `int`
38 Name or index of default hole.
39 holePositions : `iterable` of (`float`, `float`)
40 Position of a fiducial point for each hole in the mask (x, y mm).
41 holeNames : `iterable` of `str` or None
42 Name of each hole in the mask, in the same order as
43 ``holePositions``.
44 If None then set name = str(index) for each hole.
46 Raises
47 ------
48 ValueError
49 If ``defaultHole`` is `None`.
50 LookupError
51 If ``defaultHole`` is not a valid name or index.
52 ValueError
53 If ``holeNames`` is not `None` and has a different length
54 than ``holePositions``.
56 Notes
57 -----
58 **Attributes**
60 name : `str`
61 Name of mask.
62 defaultBeam : `str`
63 Name of default hole or beam.
64 """
66 def __init__(self, name, defaultHole, holePositions, holeNames=None):
67 self.name = name
68 if holeNames is not None:
69 if len(holePositions) != len(holeNames):
70 raise ValueError("Number of hole positions = {} != Number of hole names = {}".format(
71 len(holePositions), len(holeNames)
72 ))
73 else:
74 holeNames = [str(i) for i in range(len(holePositions))]
75 self._holePosDict = OrderedDict()
76 for holeName, holePos in zip(holeNames, holePositions):
77 self._holePosDict[holeName] = Point2D(*holePos)
78 # parse "defaultBeam" after "holes", so we can check the default
79 if defaultHole is None:
80 raise ValueError("defaultHole cannot be None")
81 self.defaultBeam = self.asHoleName(defaultHole)
83 def asHoleName(self, hole):
84 """Read a hole index, name or None as a name, and validate it.
86 Parameters
87 ----------
88 hole : `int`, `str` or `None`
89 If `None` then return the default beam.
90 If an integer index then return the corresponding beam name.
91 If a string then return unchanged.
93 Raises
94 ------
95 `LookupError` if beam is an integer and is out of range
96 or if beam is a string and the name is unknown.
97 """
98 if hole is None:
99 return self.defaultBeam
100 if isinstance(hole, int):
101 return [name for name in self._holePosDict][hole]
102 if hole not in self._holePosDict:
103 raise KeyError("Unknown name {!r}".format(hole))
104 return hole
106 def getHolePos(self, beam):
107 """Return the position of a hole in focal plane x,y mm.
109 Parameters
110 ----------
111 hole : `int`, `str` or `None`
112 If `None` then use the default beam.
113 If an integer index then use the corresponding beam name.
114 """
115 return self._holePosDict[self.asHoleName(beam)]
117 @property
118 def numHoles(self):
119 """The number of holes (read only)"""
120 return len(self._holePosDict)
122 @property
123 def holeNames(self):
124 """An iterable of hole names, in index order (read only)"""
125 return self._holePosDict.keys()