21 from __future__
import annotations
23 __all__ = [
"CalibRepoConverter"]
28 from typing
import TYPE_CHECKING, Dict, Iterator, Tuple, Optional
30 from .repoConverter
import RepoConverter
31 from .repoWalker
import RepoWalker
32 from .translators
import makeCalibrationLabel
35 from lsst.daf.butler
import StorageClass, FormatterParameter
36 from .repoWalker.scanner
import PathElementHandler
37 from ..cameraMapper
import CameraMapper
38 from ..mapping
import Mapping
as CameraMapperMapping
42 """A specialization of `RepoConverter` for calibration repositories.
46 mapper : `CameraMapper`
47 Gen2 mapper for the data repository. The root associated with the
48 mapper is ignored and need not match the root of the repository.
50 Additional keyword arguments are forwarded to (and required by)
54 def __init__(self, *, mapper: CameraMapper, **kwds):
61 return datasetTypeName
in self.
task.config.curatedCalibrations
63 def iterMappings(self) -> Iterator[Tuple[str, CameraMapperMapping]]:
65 yield from self.
mapper.calibrations.items()
68 storageClass: StorageClass, formatter: FormatterParameter =
None,
69 targetHandler: Optional[PathElementHandler] =
None,
70 ) -> RepoWalker.Target:
72 target = RepoWalker.Target(
73 datasetTypeName=datasetTypeName,
74 storageClass=storageClass,
77 instrument=self.
task.instrument.getName(),
78 universe=self.
task.registry.dimensions,
80 targetHandler=targetHandler,
81 translatorFactory=self.
task.translatorFactory,
91 db = sqlite3.connect(os.path.join(self.
root,
"calibRegistry.sqlite3"))
92 db.row_factory = sqlite3.Row
95 if "calibration_label" not in datasetType.dimensions:
97 fields = [
"validStart",
"validEnd",
"calibDate"]
98 if "detector" in datasetType.dimensions.names:
99 fields.append(self.
task.config.ccdKey)
101 fields.append(f
"NULL AS {self.task.config.ccdKey}")
102 if (
"physical_filter" in datasetType.dimensions.names
103 or "abstract_filter" in datasetType.dimensions.names):
104 fields.append(
"filter")
106 fields.append(
"NULL AS filter")
107 query = f
"SELECT DISTINCT {', '.join(fields)} FROM {datasetType.name};"
109 results = db.execute(query)
110 except sqlite3.OperationalError
as e:
111 if (self.
mapper.mappings[datasetType.name].tables
is None
112 or len(self.
mapper.mappings[datasetType.name].tables) == 0):
113 self.
task.log.warn(
"Could not extract calibration ranges for %s in %s: %r",
114 datasetType.name, self.
root, e)
117 name = self.
mapper.mappings[datasetType.name].tables[0]
118 query = f
"SELECT DISTINCT {', '.join(fields)} FROM {name};"
120 results = db.execute(query)
121 except sqlite3.OperationalError
as e:
122 self.
task.log.warn(
"Could not extract calibration ranges for %s in %s: %r",
123 datasetType.name, self.
root, e)
127 ccd=row[self.
task.config.ccdKey], filter=row[
"filter"])
130 day = astropy.time.TimeDelta(1, format=
"jd", scale=
"tai")
132 "instrument": self.
task.instrument.getName(),
134 "datetime_begin": astropy.time.Time(row[
"validStart"], format=
"iso", scale=
"tai"),
135 "datetime_end": astropy.time.Time(row[
"validEnd"], format=
"iso", scale=
"tai") + day
138 self.
task.registry.insertDimensionData(
"calibration_label", *records)
144 """Gen2 mapper associated with this repository.