LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
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_property_readonly("width", &ExposureT::getWidth);
102  cls.def_property_readonly("height", &ExposureT::getHeight);
103  cls.def("getDimensions", &ExposureT::getDimensions);
104  cls.def("getX0", &ExposureT::getX0);
105  cls.def("getY0", &ExposureT::getY0);
106  cls.def_property_readonly("x0", &ExposureT::getX0);
107  cls.def_property_readonly("y0", &ExposureT::getY0);
108  cls.def("getXY0", &ExposureT::getXY0);
109  cls.def("setXY0", &ExposureT::setXY0, "xy0"_a);
110  cls.def("getBBox", &ExposureT::getBBox, "origin"_a = PARENT);
111  cls.def("getWcs", (std::shared_ptr<geom::SkyWcs>(ExposureT::*)()) & ExposureT::getWcs);
112  cls.def_property_readonly(
113  "wcs", (std::shared_ptr<geom::SkyWcs>(ExposureT::*)()) & ExposureT::getWcs);
114  cls.def("setWcs", &ExposureT::setWcs, "wcs"_a);
115  cls.def("hasWcs", &ExposureT::hasWcs);
116  cls.def("getDetector", &ExposureT::getDetector);
117  cls.def_property_readonly("detector", &ExposureT::getDetector);
118  cls.def("setDetector", &ExposureT::setDetector, "detector"_a);
119  cls.def("getFilter", &ExposureT::getFilter);
120  cls.def("setFilter", &ExposureT::setFilter, "filter"_a);
121  cls.def("getFilterLabel", &ExposureT::getFilterLabel);
122  cls.def_property_readonly("filterLabel", &ExposureT::getFilterLabel);
123  cls.def("setFilterLabel", &ExposureT::setFilterLabel, "filterLabel"_a);
124 
125  cls.def("getPhotoCalib", &ExposureT::getPhotoCalib);
126  cls.def_property_readonly("photoCalib", &ExposureT::getPhotoCalib);
127  cls.def("setPhotoCalib", &ExposureT::setPhotoCalib, "photoCalib"_a);
128  cls.def("getPsf", (std::shared_ptr<detection::Psf>(ExposureT::*)()) & ExposureT::getPsf);
129  cls.def_property_readonly(
130  "psf", (std::shared_ptr<detection::Psf>(ExposureT::*)()) & ExposureT::getPsf);
131  cls.def("setPsf", &ExposureT::setPsf, "psf"_a);
132  cls.def("hasPsf", &ExposureT::hasPsf);
133  cls.def("getInfo", (std::shared_ptr<ExposureInfo>(ExposureT::*)()) & ExposureT::getInfo);
134  cls.def_property_readonly(
135  "info", (std::shared_ptr<ExposureInfo>(ExposureT::*)()) & ExposureT::getInfo);
136  cls.def("setInfo", &ExposureT::setInfo, "exposureInfo"_a);
137 
138  cls.def_property_readonly("visitInfo",
139  [](ExposureT &self) { return self.getInfo()->getVisitInfo(); });
140 
141  cls.def("subset", &ExposureT::subset, "bbox"_a, "origin"_a = PARENT);
142 
143  cls.def("writeFits", (void (ExposureT::*)(std::string const &) const) & ExposureT::writeFits);
144  cls.def("writeFits",
145  (void (ExposureT::*)(fits::MemFileManager &) const) & ExposureT::writeFits);
146  cls.def("writeFits", [](ExposureT &self, fits::Fits &fits) { self.writeFits(fits); });
147 
148  cls.def(
149  "writeFits",
150  [](ExposureT &self, std::string const &filename,
151  fits::ImageWriteOptions const &imageOptions,
152  fits::ImageWriteOptions const &maskOptions,
153  fits::ImageWriteOptions const &varianceOptions) {
154  self.writeFits(filename, imageOptions, maskOptions, varianceOptions);
155  },
156  "filename"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
157  cls.def(
158  "writeFits",
159  [](ExposureT &self, fits::MemFileManager &manager,
160  fits::ImageWriteOptions const &imageOptions,
161  fits::ImageWriteOptions const &maskOptions,
162  fits::ImageWriteOptions const &varianceOptions) {
163  self.writeFits(manager, imageOptions, maskOptions, varianceOptions);
164  },
165  "manager"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
166  cls.def(
167  "writeFits",
168  [](ExposureT &self, fits::Fits &fits, fits::ImageWriteOptions const &imageOptions,
169  fits::ImageWriteOptions const &maskOptions,
170  fits::ImageWriteOptions const &varianceOptions) {
171  self.writeFits(fits, imageOptions, maskOptions, varianceOptions);
172  },
173  "fits"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
174 
175  cls.def_static("readFits", (ExposureT(*)(std::string const &))ExposureT::readFits);
176  cls.def_static("readFits", (ExposureT(*)(fits::MemFileManager &))ExposureT::readFits);
177 
178  cls.def("getCutout", &ExposureT::getCutout, "center"_a, "size"_a);
179  });
180 }
181 } // namespace
182 PYBIND11_MODULE(_exposure, mod) {
183  lsst::utils::python::WrapperCollection wrappers(mod, "lsst.afw.image.exposure");
184  wrappers.addSignatureDependency("lsst.afw.image.apCorrMap");
185  wrappers.addSignatureDependency("lsst.afw.geom");
186  wrappers.addSignatureDependency("lsst.afw.detection");
187  wrappers.addSignatureDependency("lsst.afw.image.maskedImage");
188 
189  auto clsExposureF = declareExposure<float>(wrappers, "F");
190  auto clsExposureD = declareExposure<double>(wrappers, "D");
191  declareExposure<int>(wrappers, "I");
192  declareExposure<std::uint16_t>(wrappers, "U");
193  declareExposure<std::uint64_t>(wrappers, "L");
194 
195  // Declare constructors for casting all exposure types to to float and double
196  // (the only two types of casts that Python supports)
197  declareCastConstructor<int, float>(clsExposureF);
198  declareCastConstructor<int, double>(clsExposureD);
199 
200  declareCastConstructor<float, double>(clsExposureD);
201  declareCastConstructor<double, float>(clsExposureF);
202 
203  declareCastConstructor<std::uint16_t, float>(clsExposureF);
204  declareCastConstructor<std::uint16_t, double>(clsExposureD);
205 
206  declareCastConstructor<std::uint64_t, float>(clsExposureF);
207  declareCastConstructor<std::uint64_t, double>(clsExposureD);
208 
209  wrappers.finish();
210 }
211 } // namespace image
212 } // namespace afw
213 } // namespace lsst
Fits * fits
Definition: FitsWriter.cc:90
Implementation of the Photometric Calibration class.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
PYBIND11_MODULE(_exposure, mod)
Definition: _exposure.cc:182
def writeFits(filename, stamps, metadata, type_name, write_mask, write_variance, write_archive=False)
Definition: stamps.py:42
A base class for image defects.