LSSTApplications  18.1.0
LSSTDataManagementBasePackage
packers.py
Go to the documentation of this file.
1 # This file is part of skymap.
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/>.
21 
22 __all__ = ("SkyMapDataIdPacker",)
23 
24 from lsst.daf.butler import DataIdPacker, DataId
25 
26 
27 class SkyMapDataIdPacker(DataIdPacker):
28  """A `DataIdPacker` for tract, patch and optionally abstract_filter, given
29  a SkyMap.
30 
31  Parameters
32  ----------
33  dimensions : `DataIdPackerDimensions`
34  Struct containing dimensions related to this `DataIdPacker`. Must
35  have skymap as the only given dimension, tract, patch, and possibly
36  abstract_filter as the covered dimensions, and all of these as required
37  dimensions.
38  skymap : `str`
39  skymap name from `Registry`.
40  tractMax : `int`
41  Maximum (exclusive) tract index for this skymap.
42  patchNxMax : `int`
43  Maximum (exclusive) patch index in the x direction.
44  patchNyMax : `int`
45  Maximum (exclusive) patch index in the y direction.
46  """
47 
48  SUPPORTED_FILTERS = [None] + list("ugrizyUBGVRIZYJHK") # split string into single chars
49  """abstract_filter names supported by this packer.
50 
51  New filters should be added to the end of the list to maximize
52  compatibility with existing IDs.
53  """
54 
55  @classmethod
56  def getIntFromFilter(cls, name):
57  """Return an integer that represents the abstract_filter with the given
58  name.
59  """
60  try:
61  return cls.SUPPORTED_FILTERS.index(name)
62  except ValueError:
63  raise NotImplementedError(f"abstract_filter '{name}' not supported by this ID packer.")
64 
65  @classmethod
66  def getFilterNameFromInt(cls, num):
67  """Return an abstract_filter name from its integer representation.
68  """
69  return cls.SUPPORTED_FILTERS[num]
70 
71  @classmethod
73  return len(cls.SUPPORTED_FILTERS)
74 
75  @classmethod
76  def configure(cls, dimensions):
77  # Docstring inherited from DataIdPacker.configure
78  assert dimensions.given == ["skymap"]
79  assert dimensions.required.issuperset(["tract", "patch"])
80  metadata = {"skymap": ["tract_max", "patch_nx_max", "patch_ny_max"]}
81  kwds = {}
82  return metadata, kwds
83 
84  def __init__(self, dimensions, skymap, tractMax, patchNxMax, patchNyMax):
85  self._skyMapName = skymap
86  self._patchMax = patchNxMax*patchNyMax
87  self._tractPatchMax = self._patchMax*tractMax
88  if "abstract_filter" in dimensions.required:
89  self._filterMax = self.getMaxIntForFilters()
90  else:
91  self._filterMax = None
92 
93  @property
94  def maxBits(self):
95  # Docstring inherited from DataIdPacker.maxBits
96  packedMax = self._tractPatchMax
97  if self._filterMax is not None:
98  packedMax *= self._filterMax
99  return packedMax.bit_length()
100 
101  def _pack(self, dataId):
102  # Docstring inherited from DataIdPacker.pack
103  packed = dataId["patch"] + self._patchMax*dataId["tract"]
104  if self._filterMax is not None:
105  packed += self.getIntFromFilter(dataId["abstract_filter"])*self._tractPatchMax
106  return packed
107 
108  def unpack(self, packedId):
109  # Docstring inherited from DataIdPacker.unpack
110  d = {"skymap": self._skyMapName}
111  if self._filterMax is not None:
112  d["abstract_filter"] = self.getFilterNameFromInt(packedId // self._tractPatchMax)
113  packedId %= self._tractPatchMax
114  d["tract"] = packedId // self._patchMax
115  d["patch"] = packedId % self._patchMax
116  return DataId(d, dimensions=self.dimensions.required)
def __init__(self, dimensions, skymap, tractMax, patchNxMax, patchNyMax)
Definition: packers.py:84
def configure(cls, dimensions)
Definition: packers.py:76
daf::base::PropertyList * list
Definition: fits.cc:885