24 from __future__
import annotations
26 __all__ = (
"FilterFormatter",
"FilterTranslator",)
29 from lsst.afw.image
import Filter
37 from lsst.afw.image
import FilterLabel
38 from lsst.daf.butler.formatters.file
import FileFormatter
39 from lsst.daf.butler
import StorageClassDelegate
43 """Read and write `~lsst.afw.image.Filter` filter information."""
47 unsupportedParameters =
None
48 """This formatter does not support any parameters."""
50 def _readFile(self, path: str, pytype: Type[Any] =
None) -> Any:
51 """Read a file from the path in YAML format.
56 Path to use to open the file.
57 pytype : `class`, optional
58 The type expected to be returned.
63 Either data as Python object read from YAML file, or None
64 if the file could not be opened.
67 with open(path,
"rb")
as fd:
69 except FileNotFoundError:
74 def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] =
None) -> Any:
75 """Read the bytes object as a python object.
79 serializedDataset : `bytes`
80 Bytes object to unserialize.
81 pytype : `type`, optional
82 Expected python type to be returned.
86 inMemoryDataset : `lsst.afw.image.Filter`
87 The requested data as an object.
89 data = yaml.load(serializedDataset, Loader=yaml.SafeLoader)
96 filter = pytype(data[
"canonicalName"], force=
True)
100 def _writeFile(self, inMemoryDataset: Any) ->
None:
101 """Write the in memory dataset to file on disk.
105 inMemoryDataset : `lsst.afw.image.Filter`
111 Raised if the file could not be written or the dataset could not be
114 with open(self.fileDescriptor.location.path,
"wb")
as fd:
115 fd.write(self.
_toBytes(inMemoryDataset))
117 def _toBytes(self, inMemoryDataset: Any) -> bytes:
118 """Write the in memory dataset to a bytestring.
122 inMemoryDataset : `lsst.afw.image.Filter`
127 serializedDataset : `bytes`
128 YAML string encoded to bytes.
133 Raised if the object could not be serialized.
141 filter[
"canonicalName"] = inMemoryDataset.getCanonicalName()
142 filter[
"name"] = inMemoryDataset.getName()
143 filter[
"aliases"] = inMemoryDataset.getAliases()
145 return yaml.dump(filter).encode()
149 """Derived-component converter for a Filter that has been stored as
157 """Derive a Filter from a FilterLabel.
161 label : `~lsst.afw.image.FilterLabel`
162 The object to convert.
164 Name of type to convert to. Only "filter" is supported.
169 The converted type. Can be `None`.
174 An unknown component was requested.
176 if derivedName ==
"filter":
183 if label == FilterLabel(band=
"r", physical=
"HSC-R2"):
184 return Filter(
"r2", force=
True)
185 elif label == FilterLabel(band=
"i", physical=
"HSC-I2"):
186 return Filter(
"i2", force=
True)
187 elif label == FilterLabel(physical=
"solid plate 0.0 0.0"):
188 return Filter(
"SOLID", force=
True)
189 elif label.hasBandLabel():
190 return Filter(label.bandLabel, force=
True)
194 return Filter(label.physicalLabel, force=
True)
196 raise AttributeError(f
"Do not know how to convert {type(label)} to {derivedName}")