LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
standardRepoConverter.py
Go to the documentation of this file.
1 # This file is part of obs_base.
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 <http://www.gnu.org/licenses/>.
21 from __future__ import annotations
22 
23 __all__ = ["StandardRepoConverter"]
24 
25 import os.path
26 from dataclasses import dataclass
27 from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Tuple
28 
29 from lsst.log import Log
30 from lsst.log.utils import temporaryLogLevel
31 from lsst.daf.persistence import Butler as Butler2
32 from lsst.daf.butler import DatasetType, DatasetRef, DataCoordinate, FileDataset
33 from .repoConverter import RepoConverter
34 from .repoWalker import RepoWalker
35 
36 SKYMAP_DATASET_TYPES = {
37  coaddName: f"{coaddName}Coadd_skyMap" for coaddName in ("deep", "goodSeeing", "dcr")
38 }
39 
40 if TYPE_CHECKING:
41  from lsst.skymap import BaseSkyMap
42  from lsst.daf.butler import StorageClass, FormatterParameter
43  from .cameraMapper import CameraMapper
44  from .repoWalker.scanner import PathElementHandler
45  from ..mapping import Mapping as CameraMapperMapping # disambiguate from collections.abc.Mapping
46 
47 
48 @dataclass
50  """Struct containing information about a SkyMap found in a Gen2 repository.
51  """
52 
53  name: str
54  """Name of the skymap used in Gen3 data IDs.
55  """
56 
57  instance: BaseSkyMap
58  """An instance of the actual skymap class.
59  """
60 
61  coaddName: str
62  """The coadd name used as a prefix for the dataset type this skymap was
63  found in.
64  """
65 
66  ref: DatasetRef
67  """A `DatasetRef` that can be used to ingest the skymap dataset into a
68  Gen3 repository.
69  """
70 
71  filename: str
72  """Name of the file containing the skymap dataset, relative to the
73  repository root.
74  """
75 
76 
78  """A specialization of `RepoConverter` for non-calibration repositories.
79 
80  Parameters
81  ----------
82  kwds
83  Keyword arguments are forwarded to (and required by) `RepoConverter`.
84  """
85 
86  def __init__(self, **kwds):
87  super().__init__(**kwds)
88  # Shush noisy log messages from Gen2 Mapper classes.
89  with temporaryLogLevel("CameraMapper", Log.ERROR):
90  with temporaryLogLevel("HscMapper", Log.ERROR):
91  self.butler2 = Butler2(self.root)
92  self.mapper = self.butler2.getMapperClass(self.root)(root=self.root)
93  self._foundSkyMapsByCoaddName = {}
94  self._chain = []
95 
96  def isDatasetTypeSpecial(self, datasetTypeName: str) -> bool:
97  # Docstring inherited from RepoConverter.
98  return datasetTypeName in SKYMAP_DATASET_TYPES.values()
99 
100  def prep(self):
101  # Docstring inherited from RepoConverter.
102  self.task.log.info(f"Looking for skymaps in root {self.root}.")
103  for coaddName, datasetTypeName in SKYMAP_DATASET_TYPES.items():
104  if not self.task.isDatasetTypeIncluded(datasetTypeName):
105  continue
106  try:
107  exists = self.butler2.datasetExists(datasetTypeName)
108  except AttributeError:
109  # This mapper doesn't even define this dataset type.
110  continue
111  if not exists:
112  continue
113  instance = self.butler2.get(datasetTypeName)
114  name = self.task.useSkyMap(instance, datasetTypeName)
115  datasetType = DatasetType(datasetTypeName, dimensions=["skymap"],
116  storageClass="SkyMap", universe=self.task.universe)
117  dataId = DataCoordinate.standardize(skymap=name, universe=self.task.universe)
118  struct = FoundSkyMap(name=name, instance=instance, coaddName=coaddName,
119  ref=DatasetRef(datasetType, dataId),
120  filename=self.butler2.getUri(datasetTypeName))
121  self._foundSkyMapsByCoaddName[coaddName] = struct
122  self.task.log.info("Found skymap %s in %s in %s.", name, datasetTypeName, self.root)
123  super().prep()
124 
125  def iterMappings(self) -> Iterator[Tuple[str, CameraMapperMapping]]:
126  # Docstring inherited from RepoConverter.
127  for datasetTypeName, mapping in self.mapper.mappings.items():
128  if datasetTypeName not in self.mapper.calibrations:
129  yield datasetTypeName, mapping
130 
131  def findMatchingSkyMap(self, datasetTypeName: str) -> Tuple[Optional[BaseSkyMap], Optional[str]]:
132  """Return the appropriate SkyMap for the given dataset type.
133 
134  Parameters
135  ----------
136  datasetTypeName : `str`
137  Name of the dataset type for which a skymap is sought.
138 
139  Returns
140  -------
141  skyMap : `BaseSkyMap` or `None`
142  The `BaseSkyMap` instance, or `None` if there was no match.
143  skyMapName : `str` or `None`
144  The Gen3 name for the SkyMap, or `None` if there was no match.
145  """
146  # Use deepCoadd_skyMap by default; there are some dataset types
147  # that use it but don't have "deep" anywhere in their name.
148  struct = self._foundSkyMapsByCoaddName.get("deep")
149  for coaddName in SKYMAP_DATASET_TYPES.keys():
150  if coaddName in datasetTypeName:
151  try:
152  struct = self._foundSkyMapsByCoaddName[coaddName]
153  break
154  except KeyError:
155  # Don't use the default, since we did find a specific
156  # coaddName.
157  struct = None
158  self.task.log.debug(
159  ("Dataset %s looks like it might need a skymap, but no %sCoadd_skyMap "
160  "found in repo %s."),
161  datasetTypeName, coaddName, self.root
162  )
163  if struct is not None:
164  return struct.instance, struct.name
165  else:
166  return None, None
167 
168  def makeRepoWalkerTarget(self, datasetTypeName: str, template: str, keys: Dict[str, type],
169  storageClass: StorageClass,
170  formatter: FormatterParameter = None,
171  targetHandler: Optional[PathElementHandler] = None,
172  ) -> RepoWalker.Target:
173  # Docstring inherited from RepoConverter.
174  skyMap, skyMapName = self.findMatchingSkyMap(datasetTypeName)
175  return RepoWalker.Target(
176  datasetTypeName=datasetTypeName,
177  storageClass=storageClass,
178  template=template,
179  keys=keys,
180  universe=self.task.registry.dimensions,
181  instrument=self.task.instrument.getName(),
182  skyMap=skyMap,
183  skyMapName=skyMapName,
184  formatter=formatter,
185  targetHandler=targetHandler,
186  translatorFactory=self.task.translatorFactory,
187  )
188 
189  def iterDatasets(self) -> Iterator[FileDataset]:
190  # Docstring inherited from RepoConverter.
191  for struct in self._foundSkyMapsByCoaddName.values():
192  if self.task.isDatasetTypeIncluded(struct.ref.datasetType.name):
193  yield FileDataset(path=os.path.join(self.root, struct.filename), refs=struct.ref)
194  yield from super().iterDatasets()
195 
196  def getRun(self, datasetTypeName: str, calibDate: Optional[str] = None) -> str:
197  # Docstring inherited from RepoConverter.
198  run = self.task.config.runsForced.get(datasetTypeName)
199  if run is None:
200  if self._run is not None:
201  run = self._run
202  else:
203  run = self.task.config.runs.get(datasetTypeName)
204  if run is None:
205  raise ValueError(f"No default run for repo at {self.root}, and no "
206  f"override for dataset {datasetTypeName}.")
207  if run not in self._chain:
208  self._chain.append(run)
209  return run
210 
211  def getCollectionChain(self) -> List[str]:
212  """Return run names that can be used to construct a chained collection
213  that refers to the converted repository (`list` [ `str` ]).
214  """
215  return self._chain
216 
217  # Class attributes that will be shadowed by public instance attributes;
218  # defined here only for documentation purposes.
219 
220  butler2: Butler2
221  """Gen2 butler associated with this repository.
222  """
223 
224  mapper: CameraMapper
225  """Gen2 mapper associated with this repository.
226  """
lsst.obs.base.gen2to3.repoConverter.RepoConverter.root
root
Definition: repoConverter.py:211
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.getCollectionChain
List[str] getCollectionChain(self)
Definition: standardRepoConverter.py:211
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.__init__
def __init__(self, **kwds)
Definition: standardRepoConverter.py:86
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.iterDatasets
Iterator[FileDataset] iterDatasets(self)
Definition: standardRepoConverter.py:189
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.getRun
str getRun(self, str datasetTypeName, Optional[str] calibDate=None)
Definition: standardRepoConverter.py:196
ast::append
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Definition: functional.cc:33
lsst.obs.base.gen2to3.standardRepoConverter.FoundSkyMap
Definition: standardRepoConverter.py:49
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter._foundSkyMapsByCoaddName
_foundSkyMapsByCoaddName
Definition: standardRepoConverter.py:93
lsst::log.utils
Definition: utils.py:1
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.iterMappings
Iterator[Tuple[str, CameraMapperMapping]] iterMappings(self)
Definition: standardRepoConverter.py:125
lsst.obs.base.gen2to3.repoConverter.RepoConverter.task
task
Definition: repoConverter.py:210
lsst::log
Definition: Log.h:706
lsst::log.utils.temporaryLogLevel
def temporaryLogLevel(name, level)
Definition: utils.py:49
lsst.obs.base.gen2to3.repoConverter.RepoConverter._run
_run
Definition: repoConverter.py:214
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.findMatchingSkyMap
Tuple[Optional[BaseSkyMap], Optional[str]] findMatchingSkyMap(self, str datasetTypeName)
Definition: standardRepoConverter.py:131
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter._chain
_chain
Definition: standardRepoConverter.py:94
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.isDatasetTypeSpecial
bool isDatasetTypeSpecial(self, str datasetTypeName)
Definition: standardRepoConverter.py:96
lsst.skymap
Definition: __init__.py:1
lsst::daf::persistence
Definition: Utils.h:50
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.makeRepoWalkerTarget
RepoWalker.Target makeRepoWalkerTarget(self, str datasetTypeName, str template, Dict[str, type] keys, StorageClass storageClass, FormatterParameter formatter=None, Optional[PathElementHandler] targetHandler=None)
Definition: standardRepoConverter.py:168
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter
Definition: standardRepoConverter.py:77
lsst.obs.base.gen2to3.repoConverter.RepoConverter
Definition: repoConverter.py:180
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.mapper
mapper
Definition: standardRepoConverter.py:92
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.prep
def prep(self)
Definition: standardRepoConverter.py:100
lsst.obs.base.gen2to3.standardRepoConverter.StandardRepoConverter.butler2
butler2
Definition: standardRepoConverter.py:91