Coverage for python/lsst/daf/butler/instrument.py: 90%
25 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-30 02:18 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-30 02:18 -0700
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://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 <http://www.gnu.org/licenses/>.
22__all__ = ["ObservationDimensionPacker"]
24from lsst.daf.butler import DataCoordinate, DimensionGraph, DimensionPacker
27class ObservationDimensionPacker(DimensionPacker):
28 """A `DimensionPacker` for visit+detector or exposure+detector, given an
29 instrument.
30 """
32 def __init__(self, fixed: DataCoordinate, dimensions: DimensionGraph):
33 super().__init__(fixed, dimensions)
34 self._instrumentName = fixed["instrument"]
35 record = fixed.records["instrument"]
36 assert record is not None
37 if self.dimensions.required.names == set(["instrument", "visit", "detector"]):
38 self._observationName = "visit"
39 obsMax = record.visit_max
40 elif dimensions.required.names == set(["instrument", "exposure", "detector"]): 40 ↛ 44line 40 didn't jump to line 44, because the condition on line 40 was never false
41 self._observationName = "exposure"
42 obsMax = record.exposure_max
43 else:
44 raise ValueError(f"Invalid dimensions for ObservationDimensionPacker: {dimensions.required}")
45 self._detectorMax = record.detector_max
46 self._maxBits = (obsMax * self._detectorMax).bit_length()
48 @property
49 def maxBits(self) -> int:
50 # Docstring inherited from DimensionPacker.maxBits
51 return self._maxBits
53 def _pack(self, dataId: DataCoordinate) -> int:
54 # Docstring inherited from DimensionPacker._pack
55 return dataId["detector"] + self._detectorMax * dataId[self._observationName]
57 def unpack(self, packedId: int) -> DataCoordinate:
58 # Docstring inherited from DimensionPacker.unpack
59 observation, detector = divmod(packedId, self._detectorMax)
60 return DataCoordinate.standardize(
61 {
62 "instrument": self._instrumentName,
63 "detector": detector,
64 self._observationName: observation,
65 },
66 graph=self.dimensions,
67 )