LSSTApplications  18.1.0
LSSTDataManagementBasePackage
makeGen3SkyMap.py
Go to the documentation of this file.
1 # This file is part of pipe_tasks.
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 import lsst.pex.config as pexConfig
22 import lsst.pipe.base as pipeBase
23 from lsst.daf.butler import DatasetType
24 from lsst.skymap import skyMapRegistry
25 
26 
27 class MakeGen3SkyMapConfig(pexConfig.Config):
28  """Config for MakeGen3SkyMapTask
29  """
30  datasetTypeName = pexConfig.Field(
31  doc="Name assigned to created skymap in butler registry",
32  dtype=str,
33  default="deepCoadd_skyMap",
34  )
35  name = pexConfig.Field(
36  doc="Name assigned to created skymap in butler registry",
37  dtype=str,
38  default=None,
39  optional=True
40  )
41  skyMap = skyMapRegistry.makeField(
42  doc="type of skyMap",
43  default="dodeca",
44  )
45 
46  def validate(self):
47  if self.name is None:
48  raise ValueError("The name field must be set to the name of the specific "
49  "skymap to use when writing to the butler")
50 
51 
52 class MakeGen3SkyMapTask(pipeBase.Task):
53  ConfigClass = MakeGen3SkyMapConfig
54  _DefaultName = "makeGen3SkyMap"
55 
56  """This is a task to construct and optionally save a SkyMap into a gen3
57  butler repository.
58 
59  Parameters
60  ----------
61  config : `MakeGen3SkyMapConfig` or None
62  Instance of a configuration class specifying task options, a default
63  config is created if value is None
64  """
65 
66  def __init__(self, *, config=None, **kwargs):
67  super().__init__(config=config, **kwargs)
68 
69  def run(self, butler):
70  """Construct and optionally save a SkyMap into a gen3 repository
71  Parameters
72  ----------
73  butler : `lsst.daf.butler.Butler`
74  Butler repository to which the new skymap will be written
75  """
76  skyMap = self.config.skyMap.apply()
77  skyMap.logSkyMapInfo(self.log)
78  skyMapHash = skyMap.getSha1()
79  try:
80  existing, = butler.registry.query("SELECT skymap FROM skymap WHERE hash=:hash",
81  hash=skyMapHash)
82  raise RuntimeError(
83  (f"SkyMap with name {existing.name} and hash {skyMapHash} already exist in "
84  f"the butler collection {self.collection}, SkyMaps must be unique within "
85  "a collection")
86  )
87  except ValueError:
88  self.log.info(f"Inserting SkyMap {self.config.name} with hash={skyMapHash}")
89  with butler.registry.transaction():
90  skyMap.register(self.config.name, butler.registry)
91  butler.registry.registerDatasetType(DatasetType(name=self.config.datasetTypeName,
92  dimensions=["skymap"],
93  storageClass="SkyMap"))
94  butler.put(skyMap, self.config.datasetTypeName, {"skymap": self.config.name})
95 
96  return pipeBase.Struct(
97  skyMap=skyMap
98  )