24#include "pybind11/pybind11.h"
25#include "pybind11/stl.h"
28#include "ndarray/pybind11.h"
47using namespace pybind11::literals;
57using PyImageFitsReader = py::class_<ImageFitsReader, std::shared_ptr<ImageFitsReader>>;
58using PyMaskFitsReader = py::class_<MaskFitsReader, std::shared_ptr<MaskFitsReader>>;
59using PyMaskedImageFitsReader = py::class_<MaskedImageFitsReader, std::shared_ptr<MaskedImageFitsReader>>;
60using PyExposureFitsReader = py::class_<ExposureFitsReader, std::shared_ptr<ExposureFitsReader>>;
64template <
typename Class,
typename... Args>
65void declareCommonMethods(py::class_<Class, Args...> &cls) {
66 cls.def(
"readBBox", &Class::readBBox,
"origin"_a = PARENT);
67 cls.def(
"readXY0", &Class::readXY0,
"bbox"_a =
lsst::geom::Box2I(),
"origin"_a = PARENT);
68 cls.def(
"getFileName", &Class::getFileName);
69 cls.def_property_readonly(
"fileName", &Class::getFileName);
73template <
typename Class,
typename... Args>
74void 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);
77 cls.def(
"readMetadata", &Class::readMetadata);
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())) {
86 dtype = py::dtype(self.readDType());
88 return cpputils::python::TemplateInvoker().apply(
90 return self.template readArray<decltype(t)>(
bbox, origin, allowUnsafe);
97 "dtype"_a = py::none());
101template <
typename Class,
typename... Args>
102void 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())) {
111 dtype = py::dtype(self.readImageDType());
113 return cpputils::python::TemplateInvoker().apply(
115 return self.template readImage<decltype(t)>(
bbox, origin, allowUnsafe);
122 "dtype"_a = py::none());
127 if (dtype.is(py::none())) {
128 dtype = py::dtype(self.readImageDType());
130 return cpputils::python::TemplateInvoker().apply(
132 return self.template readImageArray<decltype(t)>(
bbox, origin, allowUnsafe);
139 "dtype"_a = py::none());
143 bool allowUnsafe, py::object dtype) {
144 if (dtype.is(py::none())) {
145 dtype = py::dtype(self.readMaskDType());
147 return cpputils::python::TemplateInvoker().apply(
149 return self.template readMask<decltype(t)>(
bbox, origin, conformMasks,
152 py::dtype(dtype), cpputils::python::TemplateInvoker::Tag<MaskPixel>());
155 "allowUnsafe"_a =
false,
"dtype"_a = py::none());
160 if (dtype.is(py::none())) {
161 dtype = py::dtype(self.readMaskDType());
163 return cpputils::python::TemplateInvoker().apply(
165 return self.template readMaskArray<decltype(t)>(
bbox, origin, allowUnsafe);
167 py::dtype(dtype), cpputils::python::TemplateInvoker::Tag<MaskPixel>());
170 "dtype"_a = py::none());
175 if (dtype.is(py::none())) {
176 dtype = py::dtype(self.readVarianceDType());
178 return cpputils::python::TemplateInvoker().apply(
180 return self.template readVariance<decltype(t)>(
bbox, origin, allowUnsafe);
182 py::dtype(dtype), cpputils::python::TemplateInvoker::Tag<VariancePixel>());
185 "dtype"_a = py::none());
190 if (dtype.is(py::none())) {
191 dtype = py::dtype(self.readVarianceDType());
193 return cpputils::python::TemplateInvoker().apply(
195 return self.template readVarianceArray<decltype(t)>(
bbox, origin, allowUnsafe);
197 py::dtype(dtype), cpputils::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 cpputils::python::TemplateInvoker().apply(
215 [&](auto t) { return self.read<decltype(t)>(bbox, origin, allowUnsafe); },
217 cpputils::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 cpputils::python::TemplateInvoker().apply(
238 return self.read<decltype(t)>(bbox, origin, conformMasks, allowUnsafe);
240 py::dtype(dtype), cpputils::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 cpputils::python::TemplateInvoker().apply(
268 return self.read<decltype(t)>(bbox, origin, conformMasks, allowUnsafe);
271 cpputils::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(
"readExposureId", &ExposureFitsReader::readExposureId);
287 cls.def(
"readMetadata", &ExposureFitsReader::readMetadata);
288 cls.def(
"readWcs", &ExposureFitsReader::readWcs);
289 cls.def(
"readFilter", &ExposureFitsReader::readFilter);
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 cpputils::python::TemplateInvoker().apply(
309 return self.readMaskedImage<decltype(t)>(bbox, origin, conformMasks,
313 cpputils::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 cpputils::python::TemplateInvoker().apply(
327 return self.read<decltype(t)>(bbox, origin, conformMasks, allowUnsafe);
330 cpputils::python::TemplateInvoker::Tag<
std::uint16_t,
int,
float,
double,
334 "allowUnsafe"_a =
false,
"dtype"_a = py::none());
344 declareImageFitsReader(wrappers);
345 declareMaskFitsReader(wrappers);
346 declareMaskedImageFitsReader(wrappers);
347 declareExposureFitsReader(wrappers);
Implementation of the Photometric Calibration class.
A helper class for subdividing pybind11 module across multiple translation units (i....
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
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...
An integer coordinate rectangle.
const int DEFAULT_HDU
Specify that the default HDU should be read.
void wrapReaders(lsst::cpputils::python::WrapperCollection &)