21 from __future__
import annotations
23 __all__ = [
"CalibRepoConverter"]
28 from typing
import TYPE_CHECKING, Dict, Iterator, Tuple, Optional
30 from lsst.daf.butler
import Butler
as Butler3
32 from .repoConverter
import RepoConverter
33 from .repoWalker
import RepoWalker
34 from .translators
import makeCalibrationLabel
37 from lsst.daf.butler
import StorageClass, FormatterParameter
38 from .repoWalker.scanner
import PathElementHandler
39 from ..cameraMapper
import CameraMapper
40 from ..mapping
import Mapping
as CameraMapperMapping
44 """A specialization of `RepoConverter` for calibration repositories. 48 mapper : `CameraMapper` 49 Gen2 mapper for the data repository. The root associated with the 50 mapper is ignored and need not match the root of the repository. 52 Additional keyword arguments are forwarded to (and required by) 56 def __init__(self, *, mapper: CameraMapper, **kwds):
63 return datasetTypeName
in self.
task.config.curatedCalibrations
65 def iterMappings(self) -> Iterator[Tuple[str, CameraMapperMapping]]:
67 yield from self.
mapper.calibrations.items()
70 storageClass: StorageClass, formatter: FormatterParameter =
None,
71 targetHandler: Optional[PathElementHandler] =
None,
72 ) -> RepoWalker.Target:
74 target = RepoWalker.Target(
75 datasetTypeName=datasetTypeName,
76 storageClass=storageClass,
79 instrument=self.
task.instrument.getName(),
80 universe=self.
task.registry.dimensions,
82 targetHandler=targetHandler,
92 db = sqlite3.connect(os.path.join(self.
root,
"calibRegistry.sqlite3"))
93 db.row_factory = sqlite3.Row
96 if "calibration_label" not in datasetType.dimensions:
98 fields = [
"validStart",
"validEnd",
"calibDate"]
99 if "detector" in datasetType.dimensions.names:
100 fields.append(self.
task.config.ccdKey)
102 fields.append(f
"NULL AS {self.task.config.ccdKey}")
103 if (
"physical_filter" in datasetType.dimensions.names
104 or "abstract_filter" in datasetType.dimensions.names):
105 fields.append(
"filter")
107 fields.append(
"NULL AS filter")
108 query = f
"SELECT DISTINCT {', '.join(fields)} FROM {datasetType.name};" 110 results = db.execute(query)
111 except sqlite3.OperationalError
as e:
112 if (self.
mapper.mappings[datasetType.name].tables
is None 113 or len(self.
mapper.mappings[datasetType.name].tables) == 0):
114 self.
task.log.warn(
"Could not extract calibration ranges for %s in %s: %r",
115 datasetType.name, self.
root, e)
118 name = self.
mapper.mappings[datasetType.name].tables[0]
119 query = f
"SELECT DISTINCT {', '.join(fields)} FROM {name};" 121 results = db.execute(query)
122 except sqlite3.OperationalError
as e:
123 self.
task.log.warn(
"Could not extract calibration ranges for %s in %s: %r",
124 datasetType.name, self.
root, e)
128 ccd=row[self.
task.config.ccdKey], filter=row[
"filter"])
131 day = astropy.time.TimeDelta(1, format=
"jd", scale=
"tai")
133 "instrument": self.
task.instrument.getName(),
135 "datetime_begin": astropy.time.Time(row[
"validStart"], format=
"iso", scale=
"tai"),
136 "datetime_end": astropy.time.Time(row[
"validEnd"], format=
"iso", scale=
"tai") + day
139 self.
task.registry.insertDimensionData(
"calibration_label", *records)
143 if self.
task.config.doWriteCuratedCalibrations:
144 butler3 = Butler3(butler=self.
task.butler3, run=self.
_run)
145 self.
task.instrument.writeCuratedCalibrations(butler3)
152 """Gen2 mapper associated with this repository.
def insertDimensionData(self)