LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+4b710797af,21.0.0-1-gfc31b0f+3b24369756,21.0.0-10-g2408eff+50e97f2f47,21.0.0-10-g560fb7b+0803ad37c5,21.0.0-10-g5daeb2b+f9b8dc6d5a,21.0.0-10-g8d1d15d+77a6b82ebf,21.0.0-10-gcf60f90+c961be884d,21.0.0-11-g25eff31+7692554667,21.0.0-17-g6590b197+a14a01c114,21.0.0-2-g103fe59+b79afc2051,21.0.0-2-g1367e85+1003a3501c,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+1003a3501c,21.0.0-2-g7f82c8f+c2a1919b98,21.0.0-2-g8f08a60+fd0b970de5,21.0.0-2-ga326454+c2a1919b98,21.0.0-2-gde069b7+ca45a81b40,21.0.0-2-gecfae73+afcaaec585,21.0.0-2-gfc62afb+1003a3501c,21.0.0-21-g5d80ea29e+5e3c9a3766,21.0.0-3-g357aad2+c67f36f878,21.0.0-3-g4be5c26+1003a3501c,21.0.0-3-g65f322c+02b1f88459,21.0.0-3-g7d9da8d+3b24369756,21.0.0-3-ge02ed75+a423c2ae7a,21.0.0-4-g591bb35+a423c2ae7a,21.0.0-4-g65b4814+0803ad37c5,21.0.0-4-g88306b8+199c7497e5,21.0.0-4-gccdca77+a631590478,21.0.0-4-ge8a399c+b923ff878e,21.0.0-5-gd00fb1e+d8b1e95daa,21.0.0-53-ge728e5d5+3cb64fea8e,21.0.0-6-g2d4f3f3+04719a4bac,21.0.0-7-g04766d7+8d320c19d5,21.0.0-7-g98eecf7+205433fbda,21.0.0-9-g39e06b5+a423c2ae7a,master-gac4afde19b+a423c2ae7a,w.2021.11
LSST Data Management Base Package
exposure.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2017 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include "pybind11/pybind11.h"
24 #include "lsst/utils/python.h"
25 
27 #include "lsst/afw/geom/SkyWcs.h"
29 #include "lsst/afw/image/Filter.h"
32 #include "lsst/afw/detection/Psf.h"
33 
34 namespace py = pybind11;
35 using namespace pybind11::literals;
36 
37 namespace lsst {
38 namespace afw {
39 namespace image {
40 namespace {
41 
42 template <typename PixelT>
43 using PyExposure = py::class_<Exposure<PixelT>, std::shared_ptr<Exposure<PixelT>>>;
44 
45 /*
46 Declare a constructor that takes an Exposure of FromPixelT and returns an Exposure cast to ToPixelT
47 
48 The mask and variance must be of the standard types.
49 
50 @param[in] cls The pybind11 class to which add the constructor
51 */
52 template <typename FromPixelT, typename ToPixelT>
53 void declareCastConstructor(PyExposure<ToPixelT> &cls) {
54  cls.def(py::init<Exposure<FromPixelT> const &, bool const>(), "src"_a, "deep"_a);
55 }
56 
57 template <typename PixelT> // only the image type varies; mask and variance are fixed to default tparams
58 PyExposure<PixelT> declareExposure(lsst::utils::python::WrapperCollection &wrappers,
59  const std::string &suffix) {
60  using ExposureT = Exposure<PixelT>;
61  using MaskedImageT = typename ExposureT::MaskedImageT;
62  wrappers.wrap([](auto &mod) {
63  mod.def("makeExposure", &makeExposure<PixelT, MaskPixel, VariancePixel>, "maskedImage"_a,
65  });
66  return wrappers.wrapType(
67  PyExposure<PixelT>(wrappers.module, ("Exposure" + suffix).c_str()), [](auto &mod, auto &cls) {
68  /* Constructors */
69  cls.def(py::init<unsigned int, unsigned int, std::shared_ptr<geom::SkyWcs const>>(),
70  "width"_a, "height"_a, "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
71  cls.def(py::init<lsst::geom::Extent2I const &, std::shared_ptr<geom::SkyWcs const>>(),
72  "dimensions"_a = lsst::geom::Extent2I(),
73  "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
74  cls.def(py::init<lsst::geom::Box2I const &, std::shared_ptr<geom::SkyWcs const>>(), "bbox"_a,
75  "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
76  cls.def(py::init<MaskedImageT &, std::shared_ptr<geom::SkyWcs const>>(), "maskedImage"_a,
77  "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
78  cls.def(py::init<MaskedImageT &, std::shared_ptr<ExposureInfo>>(), "maskedImage"_a,
79  "exposureInfo"_a);
80  cls.def(py::init<std::string const &, lsst::geom::Box2I const &, ImageOrigin, bool, bool>(),
81  "fileName"_a, "bbox"_a = lsst::geom::Box2I(), "origin"_a = PARENT,
82  "conformMasks"_a = false, "allowUnsafe"_a = false);
83  cls.def(py::init<fits::MemFileManager &, lsst::geom::Box2I const &, ImageOrigin, bool,
84  bool>(),
85  "manager"_a, "bbox"_a = lsst::geom::Box2I(), "origin"_a = PARENT,
86  "conformMasks"_a = false, "allowUnsafe"_a = false);
87  cls.def(py::init<ExposureT const &, bool>(), "other"_a, "deep"_a = false);
88  cls.def(py::init<ExposureT const &, lsst::geom::Box2I const &, ImageOrigin, bool>(),
89  "other"_a, "bbox"_a, "origin"_a = PARENT, "deep"_a = false);
90 
91  /* Members */
92  cls.def("getMaskedImage", (MaskedImageT(ExposureT::*)()) & ExposureT::getMaskedImage);
93  cls.def("setMaskedImage", &ExposureT::setMaskedImage, "maskedImage"_a);
94  cls.def_property("maskedImage", (MaskedImageT(ExposureT::*)()) & ExposureT::getMaskedImage,
95  &ExposureT::setMaskedImage);
96  cls.def("getMetadata", &ExposureT::getMetadata);
97  cls.def("setMetadata", &ExposureT::setMetadata, "metadata"_a);
98  cls.def("getWidth", &ExposureT::getWidth);
99  cls.def("getHeight", &ExposureT::getHeight);
100  cls.def("getDimensions", &ExposureT::getDimensions);
101  cls.def("getX0", &ExposureT::getX0);
102  cls.def("getY0", &ExposureT::getY0);
103  cls.def("getXY0", &ExposureT::getXY0);
104  cls.def("setXY0", &ExposureT::setXY0, "xy0"_a);
105  cls.def("getBBox", &ExposureT::getBBox, "origin"_a = PARENT);
106  cls.def("getWcs", (std::shared_ptr<geom::SkyWcs>(ExposureT::*)()) & ExposureT::getWcs);
107  cls.def("setWcs", &ExposureT::setWcs, "wcs"_a);
108  cls.def("hasWcs", &ExposureT::hasWcs);
109  cls.def("getDetector", &ExposureT::getDetector);
110  cls.def("setDetector", &ExposureT::setDetector, "detector"_a);
111  cls.def("getFilter", &ExposureT::getFilter);
112  cls.def("setFilter", &ExposureT::setFilter, "filter"_a);
113  cls.def("getFilterLabel", &ExposureT::getFilterLabel);
114  cls.def("setFilterLabel", &ExposureT::setFilterLabel, "filterLabel"_a);
115 
116  cls.def("getPhotoCalib", &ExposureT::getPhotoCalib);
117  cls.def("setPhotoCalib", &ExposureT::setPhotoCalib, "photoCalib"_a);
118  cls.def("getPsf", (std::shared_ptr<detection::Psf>(ExposureT::*)()) & ExposureT::getPsf);
119  cls.def("setPsf", &ExposureT::setPsf, "psf"_a);
120  cls.def("hasPsf", &ExposureT::hasPsf);
121  cls.def("getInfo", (std::shared_ptr<ExposureInfo>(ExposureT::*)()) & ExposureT::getInfo);
122  cls.def("setInfo", &ExposureT::setInfo, "exposureInfo"_a);
123 
124  cls.def("subset", &ExposureT::subset, "bbox"_a, "origin"_a = PARENT);
125 
126  cls.def("writeFits", (void (ExposureT::*)(std::string const &) const) & ExposureT::writeFits);
127  cls.def("writeFits",
128  (void (ExposureT::*)(fits::MemFileManager &) const) & ExposureT::writeFits);
129  cls.def("writeFits", [](ExposureT &self, fits::Fits &fits) { self.writeFits(fits); });
130 
131  cls.def(
132  "writeFits",
133  [](ExposureT &self, std::string const &filename,
134  fits::ImageWriteOptions const &imageOptions,
135  fits::ImageWriteOptions const &maskOptions,
136  fits::ImageWriteOptions const &varianceOptions) {
137  self.writeFits(filename, imageOptions, maskOptions, varianceOptions);
138  },
139  "filename"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
140  cls.def(
141  "writeFits",
142  [](ExposureT &self, fits::MemFileManager &manager,
143  fits::ImageWriteOptions const &imageOptions,
144  fits::ImageWriteOptions const &maskOptions,
145  fits::ImageWriteOptions const &varianceOptions) {
146  self.writeFits(manager, imageOptions, maskOptions, varianceOptions);
147  },
148  "manager"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
149  cls.def(
150  "writeFits",
151  [](ExposureT &self, fits::Fits &fits, fits::ImageWriteOptions const &imageOptions,
152  fits::ImageWriteOptions const &maskOptions,
153  fits::ImageWriteOptions const &varianceOptions) {
154  self.writeFits(fits, imageOptions, maskOptions, varianceOptions);
155  },
156  "fits"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
157 
158  cls.def_static("readFits", (ExposureT(*)(std::string const &))ExposureT::readFits);
159  cls.def_static("readFits", (ExposureT(*)(fits::MemFileManager &))ExposureT::readFits);
160 
161  cls.def("getCutout", &ExposureT::getCutout, "center"_a, "size"_a);
162  });
163 }
164 
165 PYBIND11_MODULE(exposure, mod) {
166  lsst::utils::python::WrapperCollection wrappers(mod, "lsst.afw.image.exposure");
167  wrappers.addSignatureDependency("lsst.afw.image.exposureInfo");
168  wrappers.addSignatureDependency("lsst.afw.image.maskedImage");
169 
170  auto clsExposureF = declareExposure<float>(wrappers, "F");
171  auto clsExposureD = declareExposure<double>(wrappers, "D");
172  declareExposure<int>(wrappers, "I");
173  declareExposure<std::uint16_t>(wrappers, "U");
174  declareExposure<std::uint64_t>(wrappers, "L");
175 
176  // Declare constructors for casting all exposure types to to float and double
177  // (the only two types of casts that Python supports)
178  declareCastConstructor<int, float>(clsExposureF);
179  declareCastConstructor<int, double>(clsExposureD);
180 
181  declareCastConstructor<float, double>(clsExposureD);
182  declareCastConstructor<double, float>(clsExposureF);
183 
184  declareCastConstructor<std::uint16_t, float>(clsExposureF);
185  declareCastConstructor<std::uint16_t, double>(clsExposureD);
186 
187  declareCastConstructor<std::uint64_t, float>(clsExposureF);
188  declareCastConstructor<std::uint64_t, double>(clsExposureD);
189 
190  wrappers.finish();
191 }
192 } // namespace
193 } // namespace image
194 } // namespace afw
195 } // 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
PYBIND11_MODULE(imageUtils, mod)
Definition: imageUtils.cc:33
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
def init()
Definition: tests.py:59
A base class for image defects.