LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
_exposure.cc
Go to the documentation of this file.
1 /*
2  * This file is part of afw.
3  *
4  * Developed for the LSST Data Management System.
5  * This product includes software developed by the LSST Project
6  * (https://www.lsst.org).
7  * See the COPYRIGHT file at the top-level directory of this distribution
8  * for details of code ownership.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #include "pybind11/pybind11.h"
25 #include "lsst/utils/python.h"
26 
28 #include "lsst/afw/geom/SkyWcs.h"
30 #include "lsst/afw/image/Filter.h"
33 #include "lsst/afw/detection/Psf.h"
34 
35 namespace py = pybind11;
36 using namespace pybind11::literals;
37 
38 namespace lsst {
39 namespace afw {
40 namespace image {
41 namespace {
42 
43 template <typename PixelT>
44 using PyExposure = py::class_<Exposure<PixelT>, std::shared_ptr<Exposure<PixelT>>>;
45 
46 /*
47 Declare a constructor that takes an Exposure of FromPixelT and returns an Exposure cast to ToPixelT
48 
49 The mask and variance must be of the standard types.
50 
51 @param[in] cls The pybind11 class to which add the constructor
52 */
53 template <typename FromPixelT, typename ToPixelT>
54 void declareCastConstructor(PyExposure<ToPixelT> &cls) {
55  cls.def(py::init<Exposure<FromPixelT> const &, bool const>(), "src"_a, "deep"_a);
56 }
57 
58 template <typename PixelT> // only the image type varies; mask and variance are fixed to default tparams
59 PyExposure<PixelT> declareExposure(lsst::utils::python::WrapperCollection &wrappers,
60  const std::string &suffix) {
61  using ExposureT = Exposure<PixelT>;
62  using MaskedImageT = typename ExposureT::MaskedImageT;
63  wrappers.wrap([](auto &mod) {
64  mod.def("makeExposure", &makeExposure<PixelT, MaskPixel, VariancePixel>, "maskedImage"_a,
66  });
67  return wrappers.wrapType(
68  PyExposure<PixelT>(wrappers.module, ("Exposure" + suffix).c_str()), [](auto &mod, auto &cls) {
69  /* Constructors */
70  cls.def(py::init<unsigned int, unsigned int, std::shared_ptr<geom::SkyWcs const>>(),
71  "width"_a, "height"_a, "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
72  cls.def(py::init<lsst::geom::Extent2I const &, std::shared_ptr<geom::SkyWcs const>>(),
73  "dimensions"_a = lsst::geom::Extent2I(),
74  "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
75  cls.def(py::init<lsst::geom::Box2I const &, std::shared_ptr<geom::SkyWcs const>>(), "bbox"_a,
76  "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
77  cls.def(py::init<MaskedImageT &, std::shared_ptr<geom::SkyWcs const>>(), "maskedImage"_a,
78  "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
79  cls.def(py::init<MaskedImageT &, std::shared_ptr<ExposureInfo>>(), "maskedImage"_a,
80  "exposureInfo"_a);
81  cls.def(py::init<std::string const &, lsst::geom::Box2I const &, ImageOrigin, bool, bool>(),
82  "fileName"_a, "bbox"_a = lsst::geom::Box2I(), "origin"_a = PARENT,
83  "conformMasks"_a = false, "allowUnsafe"_a = false);
84  cls.def(py::init<fits::MemFileManager &, lsst::geom::Box2I const &, ImageOrigin, bool,
85  bool>(),
86  "manager"_a, "bbox"_a = lsst::geom::Box2I(), "origin"_a = PARENT,
87  "conformMasks"_a = false, "allowUnsafe"_a = false);
88  cls.def(py::init<ExposureT const &, bool>(), "other"_a, "deep"_a = false);
89  cls.def(py::init<ExposureT const &, lsst::geom::Box2I const &, ImageOrigin, bool>(),
90  "other"_a, "bbox"_a, "origin"_a = PARENT, "deep"_a = false);
91 
92  /* Members */
93  cls.def("getMaskedImage", (MaskedImageT(ExposureT::*)()) & ExposureT::getMaskedImage);
94  cls.def("setMaskedImage", &ExposureT::setMaskedImage, "maskedImage"_a);
95  cls.def_property("maskedImage", (MaskedImageT(ExposureT::*)()) & ExposureT::getMaskedImage,
96  &ExposureT::setMaskedImage);
97  cls.def("getMetadata", &ExposureT::getMetadata);
98  cls.def("setMetadata", &ExposureT::setMetadata, "metadata"_a);
99  cls.def("getWidth", &ExposureT::getWidth);
100  cls.def("getHeight", &ExposureT::getHeight);
101  cls.def("getDimensions", &ExposureT::getDimensions);
102  cls.def("getX0", &ExposureT::getX0);
103  cls.def("getY0", &ExposureT::getY0);
104  cls.def("getXY0", &ExposureT::getXY0);
105  cls.def("setXY0", &ExposureT::setXY0, "xy0"_a);
106  cls.def("getBBox", &ExposureT::getBBox, "origin"_a = PARENT);
107  cls.def("getWcs", (std::shared_ptr<geom::SkyWcs>(ExposureT::*)()) & ExposureT::getWcs);
108  cls.def("setWcs", &ExposureT::setWcs, "wcs"_a);
109  cls.def("hasWcs", &ExposureT::hasWcs);
110  cls.def("getDetector", &ExposureT::getDetector);
111  cls.def("setDetector", &ExposureT::setDetector, "detector"_a);
112  cls.def("getFilter", &ExposureT::getFilter);
113  cls.def("setFilter", &ExposureT::setFilter, "filter"_a);
114  cls.def("getFilterLabel", &ExposureT::getFilterLabel);
115  cls.def("setFilterLabel", &ExposureT::setFilterLabel, "filterLabel"_a);
116 
117  cls.def("getPhotoCalib", &ExposureT::getPhotoCalib);
118  cls.def("setPhotoCalib", &ExposureT::setPhotoCalib, "photoCalib"_a);
119  cls.def("getPsf", (std::shared_ptr<detection::Psf>(ExposureT::*)()) & ExposureT::getPsf);
120  cls.def("setPsf", &ExposureT::setPsf, "psf"_a);
121  cls.def("hasPsf", &ExposureT::hasPsf);
122  cls.def("getInfo", (std::shared_ptr<ExposureInfo>(ExposureT::*)()) & ExposureT::getInfo);
123  cls.def("setInfo", &ExposureT::setInfo, "exposureInfo"_a);
124 
125  cls.def("subset", &ExposureT::subset, "bbox"_a, "origin"_a = PARENT);
126 
127  cls.def("writeFits", (void (ExposureT::*)(std::string const &) const) & ExposureT::writeFits);
128  cls.def("writeFits",
129  (void (ExposureT::*)(fits::MemFileManager &) const) & ExposureT::writeFits);
130  cls.def("writeFits", [](ExposureT &self, fits::Fits &fits) { self.writeFits(fits); });
131 
132  cls.def(
133  "writeFits",
134  [](ExposureT &self, std::string const &filename,
135  fits::ImageWriteOptions const &imageOptions,
136  fits::ImageWriteOptions const &maskOptions,
137  fits::ImageWriteOptions const &varianceOptions) {
138  self.writeFits(filename, imageOptions, maskOptions, varianceOptions);
139  },
140  "filename"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
141  cls.def(
142  "writeFits",
143  [](ExposureT &self, fits::MemFileManager &manager,
144  fits::ImageWriteOptions const &imageOptions,
145  fits::ImageWriteOptions const &maskOptions,
146  fits::ImageWriteOptions const &varianceOptions) {
147  self.writeFits(manager, imageOptions, maskOptions, varianceOptions);
148  },
149  "manager"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
150  cls.def(
151  "writeFits",
152  [](ExposureT &self, fits::Fits &fits, fits::ImageWriteOptions const &imageOptions,
153  fits::ImageWriteOptions const &maskOptions,
154  fits::ImageWriteOptions const &varianceOptions) {
155  self.writeFits(fits, imageOptions, maskOptions, varianceOptions);
156  },
157  "fits"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
158 
159  cls.def_static("readFits", (ExposureT(*)(std::string const &))ExposureT::readFits);
160  cls.def_static("readFits", (ExposureT(*)(fits::MemFileManager &))ExposureT::readFits);
161 
162  cls.def("getCutout", &ExposureT::getCutout, "center"_a, "size"_a);
163  });
164 }
165 } // namespace
166 PYBIND11_MODULE(_exposure, mod) {
167  lsst::utils::python::WrapperCollection wrappers(mod, "lsst.afw.image.exposure");
168  wrappers.addSignatureDependency("lsst.afw.image.apCorrMap");
169  wrappers.addSignatureDependency("lsst.afw.geom");
170  wrappers.addSignatureDependency("lsst.afw.detection");
171  wrappers.addSignatureDependency("lsst.afw.image.maskedImage");
172 
173  auto clsExposureF = declareExposure<float>(wrappers, "F");
174  auto clsExposureD = declareExposure<double>(wrappers, "D");
175  declareExposure<int>(wrappers, "I");
176  declareExposure<std::uint16_t>(wrappers, "U");
177  declareExposure<std::uint64_t>(wrappers, "L");
178 
179  // Declare constructors for casting all exposure types to to float and double
180  // (the only two types of casts that Python supports)
181  declareCastConstructor<int, float>(clsExposureF);
182  declareCastConstructor<int, double>(clsExposureD);
183 
184  declareCastConstructor<float, double>(clsExposureD);
185  declareCastConstructor<double, float>(clsExposureF);
186 
187  declareCastConstructor<std::uint16_t, float>(clsExposureF);
188  declareCastConstructor<std::uint16_t, double>(clsExposureD);
189 
190  declareCastConstructor<std::uint64_t, float>(clsExposureF);
191  declareCastConstructor<std::uint64_t, double>(clsExposureD);
192 
193  wrappers.finish();
194 }
195 } // namespace image
196 } // namespace afw
197 } // namespace lsst
Fits * fits
Definition: FitsWriter.cc:90
Implementation of the Photometric Calibration class.
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition: python.h:448
void finish()
Invoke all deferred wrapper-declaring callables.
Definition: python.h:435
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...
Definition: python.h:391
void wrap(WrapperCallback function)
Add a set of wrappers without defining a class.
Definition: python.h:369
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
Definition: python.h:357
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
PYBIND11_MODULE(_exposure, mod)
Definition: _exposure.cc:166
def init()
Definition: tests.py:59
A base class for image defects.