21 from __future__
import annotations
23 __all__ = [
"ConvertRepoConfig",
"ConvertRepoTask",
"ConvertRepoSkyMapConfig"]
27 from dataclasses
import dataclass
28 from typing
import Iterable, Optional, List, Dict
31 from lsst.daf.butler
import (
35 from lsst.pex.config
import Config, ConfigurableField, ConfigDictField, DictField, ListField, Field
36 from lsst.pipe.base
import Task
37 from lsst.skymap
import skyMapRegistry, BaseSkyMap
39 from ..ingest
import RawIngestTask
40 from .repoConverter
import ConversionSubset
41 from .rootRepoConverter
import RootRepoConverter
42 from .calibRepoConverter
import CalibRepoConverter
43 from .standardRepoConverter
import StandardRepoConverter
48 """Struct containing information about a skymap that may appear in a Gen2 53 """Name of the skymap used in Gen3 data IDs. 57 """Hash computed by `BaseSkyMap.getSha1`. 61 """Name of the skymap used in Gen3 data IDs. 65 """Whether this skymap has been found in at least one repository being 71 """Sub-config used to hold the parameters of a SkyMap. 75 This config only needs to exist because we can't put a 76 `~lsst.pex.config.RegistryField` directly inside a 77 `~lsst.pex.config.ConfigDictField`. 79 It needs to have its only field named "skyMap" for compatibility with the 80 configuration of `lsst.pipe.tasks.MakeSkyMapTask`, which we want so we can 81 use one config file in an obs package to configure both. 83 This name leads to unfortunate repetition with the field named 84 "skymap" that holds it - "skyMap[name].skyMap" - but that seems 87 skyMap = skyMapRegistry.makeField(
88 doc=
"Type and parameters for the SkyMap itself.",
94 raws = ConfigurableField(
95 "Configuration for subtask responsible for ingesting raws and adding " 96 "visit and exposure dimension entries.",
99 skyMaps = ConfigDictField(
100 "Mapping from Gen3 skymap name to the parameters used to construct a " 101 "BaseSkyMap instance. This will be used to associate names with " 102 "existing skymaps found in the Gen2 repo.",
104 itemtype=ConvertRepoSkyMapConfig,
107 rootSkyMapName = Field(
108 "Name of a Gen3 skymap (an entry in ``self.skyMaps``) to assume for " 109 "datasets in the root repository when no SkyMap is found there. ",
114 collections = DictField(
115 "Special collections (values) for certain dataset types (keys). " 116 "These are used in addition to rerun collections for datasets in " 117 "reruns. The 'raw' dataset must have an entry here if it is to be " 122 "deepCoadd_skyMap":
"skymaps",
123 "brightObjectMask":
"masks",
126 storageClasses = DictField(
127 "Mapping from dataset type name or Gen2 policy entry (e.g. 'python' " 128 "or 'persistable') to the Gen3 StorageClass name.",
135 "defects":
"Defects",
136 "BaseSkyMap":
"SkyMap",
137 "BaseCatalog":
"Catalog",
138 "BackgroundList":
"Background",
140 "MultilevelParquetTable":
"DataFrame",
141 "ParquetTable":
"DataFrame",
145 formatterClasses = DictField(
146 "Mapping from dataset type name to formatter class. " 147 "By default these are derived from the formatters listed in the" 148 " Gen3 datastore configuration.",
153 targetHandlerClasses = DictField(
154 "Mapping from dataset type name to target handler class.",
159 doRegisterInstrument = Field(
160 "If True (default), add dimension records for the Instrument and its " 161 "filters and detectors to the registry instead of assuming they are " 166 doWriteCuratedCalibrations = Field(
167 "If True (default), ingest human-curated calibrations directly via " 168 "the Instrument interface. Note that these calibrations are never " 169 "converted from Gen2 repositories.",
174 "The names of reference catalogs (subdirectories under ref_cats) to " 179 fileIgnorePatterns = ListField(
180 "Filename globs that should be ignored instead of being treated as " 183 default=[
"README.txt",
"*~?",
"butler.yaml",
"gen3.sqlite3",
184 "registry.sqlite3",
"calibRegistry.sqlite3",
"_mapper",
185 "_parent",
"repositoryCfg.yaml"]
187 datasetIncludePatterns = ListField(
188 "Glob-style patterns for dataset type names that should be converted.",
192 datasetIgnorePatterns = ListField(
193 "Glob-style patterns for dataset type names that should not be " 194 "converted despite matching a pattern in datasetIncludePatterns.",
199 "Key used for the Gen2 equivalent of 'detector' in data IDs.",
204 "If True (default), only convert datasets that are related to the " 205 "ingested visits. Ignored unless a list of visits is passed to " 210 curatedCalibrations = ListField(
211 "Dataset types that are handled by `Instrument.writeCuratedCalibrations()` " 212 "and thus should not be converted using the standard calibration " 213 "conversion system.",
216 "transmission_sensor",
217 "transmission_filter",
218 "transmission_optics",
219 "transmission_atmosphere",
225 return self.
raws.transfer
229 self.
raws.transfer = value
233 return self.
raws.instrument
237 self.
raws.instrument = value
247 """A task that converts one or more related Gen2 data repositories to a 248 single Gen3 data repository (with multiple collections). 252 config: `ConvertRepoConfig` 253 Configuration for this task. 254 butler3: `lsst.daf.butler.Butler` 255 Gen3 Butler instance that represents the data repository datasets will 256 be ingested into. The collection and/or run associated with this 257 Butler will be ignored in favor of collections/runs passed via config 260 Other keyword arguments are forwarded to the `Task` constructor. 264 Most of the work of converting repositories is delegated to instances of 265 the `RepoConverter` hierarchy. The `ConvertRepoTask` instance itself holds 266 only state that is relevant for all Gen2 repositories being ingested, while 267 each `RepoConverter` instance holds only state relevant for the conversion 268 of a single Gen2 repository. Both the task and the `RepoConverter` 269 instances are single use; `ConvertRepoTask.run` and most `RepoConverter` 270 methods may only be called once on a particular instance. 273 ConfigClass = ConvertRepoConfig
275 _DefaultName =
"convertRepo" 277 def __init__(self, config=None, *, butler3: Butler3, **kwds):
284 self.makeSubtask(
"raws", butler=butler3)
288 self.
instrument = doImport(self.config.instrument)()
291 for name, config
in self.config.skyMaps.items():
292 instance = config.skyMap.apply()
296 def _populateSkyMapDicts(self, name, instance):
297 struct =
ConfiguredSkyMap(name=name, sha1=instance.getSha1(), instance=instance)
302 """Return `True` if configuration indicates that the given dataset type 305 This method is intended to be called primarily by the 306 `RepoConverter` instances used interally by the task. 311 Name of the dataset type. 316 Whether the dataset should be included in the conversion. 319 any(fnmatch.fnmatchcase(datasetTypeName, pattern)
320 for pattern
in self.config.datasetIncludePatterns)
321 and not any(fnmatch.fnmatchcase(datasetTypeName, pattern)
322 for pattern
in self.config.datasetIgnorePatterns)
325 def useSkyMap(self, skyMap: BaseSkyMap, skyMapName: str) -> str:
326 """Indicate that a repository uses the given SkyMap. 328 This method is intended to be called primarily by the 329 `RepoConverter` instances used interally by the task. 333 skyMap : `lsst.skymap.BaseSkyMap` 334 SkyMap instance being used, typically retrieved from a Gen2 337 The name of the gen2 skymap, for error reporting. 342 The name of the skymap in Gen3 data IDs. 347 Raised if the specified skymap cannot be found. 349 sha1 = skyMap.getSha1()
354 except KeyError
as err:
355 msg = f
"SkyMap '{skyMapName}' with sha1={sha1} not included in configuration." 356 raise LookupError(msg)
from err
361 """Register all skymaps that have been marked as used. 363 This method is intended to be called primarily by the 364 `RepoConverter` instances used interally by the task. 368 subset : `ConversionSubset`, optional 369 Object that will be used to filter converted datasets by data ID. 370 If given, it will be updated with the tracts of this skymap that 371 overlap the visits in the subset. 375 struct.instance.register(struct.name, self.
registry)
376 if subset
is not None and self.config.relatedOnly:
377 subset.addSkyMap(self.
registry, struct.name)
380 """Indicate that a repository uses the given SkyPix dimension. 382 This method is intended to be called primarily by the 383 `RepoConverter` instances used interally by the task. 387 dimension : `lsst.daf.butler.SkyPixDimension` 388 Dimension represening a pixelization of the sky. 393 """Register all skymaps that have been marked as used. 395 This method is intended to be called primarily by the 396 `RepoConverter` instances used interally by the task. 400 subset : `ConversionSubset`, optional 401 Object that will be used to filter converted datasets by data ID. 402 If given, it will be updated with the pixelization IDs that 403 overlap the visits in the subset. 405 if subset
is not None and self.config.relatedOnly:
407 subset.addSkyPix(self.
registry, dimension)
409 def run(self, root: str, collections: List[str], *,
410 calibs: Dict[str, List[str]] =
None,
411 reruns: Dict[str, List[str]] =
None,
412 visits: Optional[Iterable[int]] =
None):
413 """Convert a group of related data repositories. 418 Complete path to the root Gen2 data repository. This should be 419 a data repository that includes a Gen2 registry and any raw files 420 and/or reference catalogs. 421 collections : `list` of `str` 422 Gen3 collections that datasets from the root repository should be 423 associated with. This should include any rerun collection that 424 these datasets should also be considered to be part of; because of 425 structural difference between Gen2 parent/child relationships and 426 Gen3 collections, these cannot be reliably inferred. 428 Dictionary mapping calibration repository path to the collections 429 that the repository's datasets should be associated with. The path 430 may be relative to ``root`` or absolute. Collections should 431 include child repository collections as appropriate (see 432 documentation for ``collections``). 434 Dictionary mapping rerun repository path to the collections that 435 the repository's datasets should be associated with. The path may 436 be relative to ``root`` or absolute. Collections should include 437 child repository collections as appropriate (see documentation for 439 visits : iterable of `int`, optional 440 The integer IDs of visits to convert. If not provided, all visits 441 in the Gen2 root repository will be converted. 448 if visits
is not None:
451 if self.config.relatedOnly:
452 self.log.warn(
"config.relatedOnly is True but all visits are being ingested; " 453 "no filtering will be done.")
461 if self.config.doRegisterInstrument:
469 rootConverter =
RootRepoConverter(task=self, root=root, collections=collections, subset=subset)
471 converters.append(rootConverter)
473 for root, collections
in calibs.items():
474 if not os.path.isabs(root):
475 root = os.path.join(rootConverter.root, root)
477 mapper=rootConverter.mapper,
478 subset=rootConverter.subset)
480 converters.append(converter)
482 for root, collections
in reruns.items():
483 if not os.path.isabs(root):
484 root = os.path.join(rootConverter.root, root)
486 subset=rootConverter.subset)
488 converters.append(converter)
505 for converter
in converters:
506 converter.insertDimensionData()
519 for converter
in converters:
520 converter.findDatasets()
523 for converter
in converters:
524 converter.expandDataIds()
527 for converter
in converters:
def isDatasetTypeIncluded
def _populateSkyMapDicts(self, name, instance)