24 #include "pybind11/pybind11.h"
27 #include "ndarray/pybind11.h"
47 using namespace pybind11::literals;
57 using PyImageFitsReader = py::class_<ImageFitsReader, std::shared_ptr<ImageFitsReader>>;
58 using PyMaskFitsReader = py::class_<MaskFitsReader, std::shared_ptr<MaskFitsReader>>;
59 using PyMaskedImageFitsReader = py::class_<MaskedImageFitsReader, std::shared_ptr<MaskedImageFitsReader>>;
60 using PyExposureFitsReader = py::class_<ExposureFitsReader, std::shared_ptr<ExposureFitsReader>>;
64 template <
typename Class,
typename... Args>
65 void declareCommonMethods(py::class_<Class, Args...> &cls) {
66 cls.def(
"readBBox", &Class::readBBox,
"origin"_a =
PARENT);
68 cls.def(
"getFileName", &Class::getFileName);
69 cls.def_property_readonly(
"fileName", &Class::getFileName);
73 template <
typename Class,
typename... Args>
74 void declareSinglePlaneMethods(py::class_<Class, Args...> &cls) {
75 cls.def(py::init<std::string const &, int>(),
"fileName"_a,
"hdu"_a =
fits::DEFAULT_HDU);
76 cls.def(py::init<fits::MemFileManager &, int>(),
"manager"_a,
"hdu"_a =
fits::DEFAULT_HDU);
78 cls.def(
"readDType", [](Class &
self) {
return py::dtype(
self.readDType()); });
79 cls.def(
"getHdu", &Class::getHdu);
80 cls.def_property_readonly(
"hdu", &Class::getHdu);
85 if (
dtype.is(py::none())) {
88 return utils::python::TemplateInvoker().apply(
90 return self.template readArray<decltype(t)>(
bbox, origin, allowUnsafe);
93 utils::python::TemplateInvoker::Tag<
std::uint16_t, int, float, double,
97 "dtype"_a = py::none());
101 template <
typename Class,
typename... Args>
102 void declareMultiPlaneMethods(py::class_<Class, Args...> &cls) {
103 cls.def(
"readImageDType", [](Class &
self) {
return py::dtype(
self.readImageDType()); });
104 cls.def(
"readMaskDType", [](Class &
self) {
return py::dtype(
self.readMaskDType()); });
105 cls.def(
"readVarianceDType", [](Class &
self) {
return py::dtype(
self.readVarianceDType()); });
110 if (
dtype.is(py::none())) {
113 return utils::python::TemplateInvoker().apply(
115 return self.template readImage<decltype(t)>(
bbox, origin, allowUnsafe);
118 utils::python::TemplateInvoker::Tag<
std::uint16_t, int, float, double,
122 "dtype"_a = py::none());
127 if (
dtype.is(py::none())) {
130 return utils::python::TemplateInvoker().apply(
132 return self.template readImageArray<decltype(t)>(
bbox, origin, allowUnsafe);
135 utils::python::TemplateInvoker::Tag<
std::uint16_t, int, float, double,
139 "dtype"_a = py::none());
143 bool allowUnsafe, py::object
dtype) {
144 if (
dtype.is(py::none())) {
147 return utils::python::TemplateInvoker().apply(
149 return self.template readMask<decltype(t)>(
bbox, origin, conformMasks,
152 py::dtype(
dtype), utils::python::TemplateInvoker::Tag<MaskPixel>());
155 "allowUnsafe"_a =
false,
"dtype"_a = py::none());
160 if (
dtype.is(py::none())) {
163 return utils::python::TemplateInvoker().apply(
165 return self.template readMaskArray<decltype(t)>(
bbox, origin, allowUnsafe);
167 py::dtype(
dtype), utils::python::TemplateInvoker::Tag<MaskPixel>());
170 "dtype"_a = py::none());
175 if (
dtype.is(py::none())) {
178 return utils::python::TemplateInvoker().apply(
180 return self.template readVariance<decltype(t)>(
bbox, origin, allowUnsafe);
182 py::dtype(
dtype), utils::python::TemplateInvoker::Tag<VariancePixel>());
185 "dtype"_a = py::none());
190 if (
dtype.is(py::none())) {
193 return utils::python::TemplateInvoker().apply(
195 return self.template readVarianceArray<decltype(t)>(
bbox, origin, allowUnsafe);
197 py::dtype(
dtype), utils::python::TemplateInvoker::Tag<VariancePixel>());
200 "dtype"_a = py::none());
204 wrappers.
wrapType(PyImageFitsReader(wrappers.
module,
"ImageFitsReader"), [](
auto &mod,
auto &cls) {
205 declareCommonMethods(cls);
206 declareSinglePlaneMethods(cls);
209 [](ImageFitsReader &self, lsst::geom::Box2I const &bbox, ImageOrigin origin, bool allowUnsafe,
211 if (dtype.is(py::none())) {
212 dtype = py::dtype(self.readDType());
214 return utils::python::TemplateInvoker().apply(
215 [&](auto t) { return self.read<decltype(t)>(bbox, origin, allowUnsafe); },
217 utils::python::TemplateInvoker::Tag<std::uint16_t, int, float, double,
221 "dtype"_a = py::none());
226 wrappers.
wrapType(PyMaskFitsReader(wrappers.
module,
"MaskFitsReader"), [](
auto &mod,
auto &cls) {
227 declareCommonMethods(cls);
228 declareSinglePlaneMethods(cls);
231 [](MaskFitsReader &self, lsst::geom::Box2I const &bbox, ImageOrigin origin, bool conformMasks,
232 bool allowUnsafe, py::object dtype) {
233 if (dtype.is(py::none())) {
234 dtype = py::dtype(self.readDType());
236 return utils::python::TemplateInvoker().apply(
238 return self.read<decltype(t)>(bbox, origin, conformMasks, allowUnsafe);
240 py::dtype(dtype), utils::python::TemplateInvoker::Tag<MaskPixel>());
243 "allowUnsafe"_a =
false,
"dtype"_a = py::none());
249 wrappers.
wrapType(PyMaskedImageFitsReader(wrappers.
module,
"MaskedImageFitsReader"), [](
auto &mod,
251 cls.def(py::init<std::string const &, int>(),
"fileName"_a,
"hdu"_a = fits::DEFAULT_HDU);
252 cls.def(py::init<fits::MemFileManager &, int>(),
"manager"_a,
"hdu"_a = fits::DEFAULT_HDU);
253 declareCommonMethods(cls);
254 declareMultiPlaneMethods(cls);
255 cls.def(
"readPrimaryMetadata", &MaskedImageFitsReader::readPrimaryMetadata);
256 cls.def(
"readImageMetadata", &MaskedImageFitsReader::readImageMetadata);
257 cls.def(
"readMaskMetadata", &MaskedImageFitsReader::readMaskMetadata);
258 cls.def(
"readVarianceMetadata", &MaskedImageFitsReader::readVarianceMetadata);
261 [](MaskedImageFitsReader &self, lsst::geom::Box2I const &bbox, ImageOrigin origin,
262 bool conformMasks, bool needAllHdus, bool allowUnsafe, py::object dtype) {
263 if (dtype.is(py::none())) {
264 dtype = py::dtype(self.readImageDType());
266 return utils::python::TemplateInvoker().apply(
268 return self.read<decltype(t)>(bbox, origin, conformMasks, allowUnsafe);
271 utils::python::TemplateInvoker::Tag<std::uint16_t, int, float, double,
275 "needAllHdus"_a =
false,
"allowUnsafe"_a =
false,
"dtype"_a = py::none());
280 wrappers.
wrapType(PyExposureFitsReader(wrappers.
module,
"ExposureFitsReader"), [](
auto &mod,
auto &cls) {
281 cls.def(py::init<std::string const &>(),
"fileName"_a);
282 cls.def(py::init<fits::MemFileManager &>(),
"manager"_a);
283 declareCommonMethods(cls);
284 declareMultiPlaneMethods(cls);
285 cls.def(
"readSerializationVersion", &ExposureFitsReader::readSerializationVersion);
286 cls.def(
"readMetadata", &ExposureFitsReader::readMetadata);
287 cls.def(
"readWcs", &ExposureFitsReader::readWcs);
288 cls.def(
"readFilter", &ExposureFitsReader::readFilter);
289 cls.def(
"readFilterLabel", &ExposureFitsReader::readFilterLabel);
290 cls.def(
"readPhotoCalib", &ExposureFitsReader::readPhotoCalib);
291 cls.def(
"readPsf", &ExposureFitsReader::readPsf);
292 cls.def(
"readValidPolygon", &ExposureFitsReader::readValidPolygon);
293 cls.def(
"readApCorrMap", &ExposureFitsReader::readApCorrMap);
294 cls.def(
"readCoaddInputs", &ExposureFitsReader::readCoaddInputs);
295 cls.def(
"readVisitInfo", &ExposureFitsReader::readVisitInfo);
296 cls.def(
"readTransmissionCurve", &ExposureFitsReader::readTransmissionCurve);
297 cls.def(
"readComponent", &ExposureFitsReader::readComponent);
298 cls.def(
"readDetector", &ExposureFitsReader::readDetector);
299 cls.def(
"readExposureInfo", &ExposureFitsReader::readExposureInfo);
302 [](ExposureFitsReader &self, lsst::geom::Box2I const &bbox, ImageOrigin origin,
303 bool conformMasks, bool allowUnsafe, py::object dtype) {
304 if (dtype.is(py::none())) {
305 dtype = py::dtype(self.readImageDType());
307 return utils::python::TemplateInvoker().apply(
309 return self.readMaskedImage<decltype(t)>(bbox, origin, conformMasks,
313 utils::python::TemplateInvoker::Tag<std::uint16_t, int, float, double,
317 "allowUnsafe"_a =
false,
"dtype"_a = py::none());
321 bool conformMasks,
bool allowUnsafe, py::object
dtype) {
322 if (
dtype.is(py::none())) {
323 dtype = py::dtype(self.readImageDType());
325 return utils::python::TemplateInvoker().apply(
327 return self.read<decltype(t)>(bbox, origin, conformMasks, allowUnsafe);
330 utils::python::TemplateInvoker::Tag<
std::uint16_t,
int,
float,
double,
334 "allowUnsafe"_a =
false,
"dtype"_a = py::none());
345 declareImageFitsReader(wrappers);
346 declareMaskFitsReader(wrappers);
347 declareMaskedImageFitsReader(wrappers);
348 declareExposureFitsReader(wrappers);
Implementation of the Photometric Calibration class.
An integer coordinate rectangle.
A helper class for subdividing pybind11 module across multiple translation units (i....
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
void finish()
Invoke all deferred wrapper-declaring callables.
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
PYBIND11_MODULE(imageUtils, mod)
const int DEFAULT_HDU
Specify that the default HDU should be read.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
A base class for image defects.