lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
25 #include "lsst/daf/base/Persistable.h"
27 #include "lsst/afw/image/Calib.h"
28 #include "lsst/afw/image/Filter.h"
29 #include "lsst/afw/image/Wcs.h"
31 #include "lsst/afw/detection/Psf.h"
32 
33 namespace py = pybind11;
34 using namespace pybind11::literals;
35 
36 namespace lsst {
37 namespace afw {
38 namespace image {
39 namespace {
40 
41 template <typename PixelT>
42 using PyExposure =
43  py::class_<Exposure<PixelT>, std::shared_ptr<Exposure<PixelT>>, lsst::daf::base::Persistable>;
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(py::module &mod, const std::string &suffix) {
59  using ExposureT = Exposure<PixelT>;
60  using MaskedImageT = typename ExposureT::MaskedImageT;
61 
62  PyExposure<PixelT> cls(mod, ("Exposure" + suffix).c_str());
63 
64  mod.def("makeExposure", &makeExposure<PixelT, MaskPixel, VariancePixel>, "maskedImage"_a,
65  "wcs"_a = std::shared_ptr<Wcs const>());
66 
67  /* Constructors */
68  cls.def(py::init<unsigned int, unsigned int, std::shared_ptr<Wcs const>>(), "width"_a, "height"_a,
69  "wcs"_a = std::shared_ptr<Wcs const>());
70  cls.def(py::init<geom::Extent2I const &, std::shared_ptr<Wcs const>>(), "dimensions"_a = geom::Extent2I(),
71  "wcs"_a = std::shared_ptr<Wcs const>());
72  cls.def(py::init<geom::Box2I const &, std::shared_ptr<Wcs const>>(), "bbox"_a,
73  "wcs"_a = std::shared_ptr<Wcs const>());
74  cls.def(py::init<MaskedImageT &, std::shared_ptr<Wcs const>>(), "maskedImage"_a,
75  "wcs"_a = std::shared_ptr<Wcs const>());
76  cls.def(py::init<MaskedImageT &, std::shared_ptr<ExposureInfo>>(), "maskedImage"_a, "exposureInfo"_a);
77  cls.def(py::init<std::string const &, geom::Box2I const &, ImageOrigin, bool>(), "fileName"_a,
78  "bbox"_a = geom::Box2I(), "origin"_a = PARENT, "conformMasks"_a = false);
79  cls.def(py::init<fits::MemFileManager &, geom::Box2I const &, ImageOrigin, bool>(), "manager"_a,
80  "bbox"_a = geom::Box2I(), "origin"_a = PARENT, "conformMasks"_a = false);
81  cls.def(py::init<ExposureT const &, bool>(), "other"_a, "deep"_a = false);
82  cls.def(py::init<ExposureT const &, geom::Box2I const &, ImageOrigin, bool>(), "other"_a, "bbox"_a,
83  "origin"_a = PARENT, "deep"_a = false);
84 
85  /* Members */
86  cls.def("getMaskedImage", (MaskedImageT (ExposureT::*)()) & ExposureT::getMaskedImage);
87  cls.def("setMaskedImage", &ExposureT::setMaskedImage, "maskedImage"_a);
88  cls.def_property(
89  "maskedImage",
90  (MaskedImageT (ExposureT::*)()) & ExposureT::getMaskedImage,
91  &ExposureT::setMaskedImage
92  );
93  cls.def("getMetadata", &ExposureT::getMetadata);
94  cls.def("setMetadata", &ExposureT::setMetadata, "metadata"_a);
95  cls.def("getWidth", &ExposureT::getWidth);
96  cls.def("getHeight", &ExposureT::getHeight);
97  cls.def("getDimensions", &ExposureT::getDimensions);
98  cls.def("getX0", &ExposureT::getX0);
99  cls.def("getY0", &ExposureT::getY0);
100  cls.def("getXY0", &ExposureT::getXY0);
101  cls.def("setXY0", &ExposureT::setXY0, "xy0"_a);
102  cls.def("getBBox", &ExposureT::getBBox, "origin"_a = PARENT);
103  cls.def("getWcs", (std::shared_ptr<Wcs> (ExposureT::*)()) & ExposureT::getWcs);
104  cls.def("setWcs", &ExposureT::setWcs, "wcs"_a);
105  cls.def("hasWcs", &ExposureT::hasWcs);
106  cls.def("getDetector", &ExposureT::getDetector);
107  cls.def("setDetector", &ExposureT::setDetector, "detector"_a);
108  cls.def("getFilter", &ExposureT::getFilter);
109  cls.def("setFilter", &ExposureT::setFilter, "filter"_a);
110  cls.def("getCalib", (std::shared_ptr<Calib> (ExposureT::*)()) & ExposureT::getCalib);
111  cls.def("setCalib", &ExposureT::setCalib, "calib"_a);
112  cls.def("getPsf", (std::shared_ptr<detection::Psf> (ExposureT::*)()) & ExposureT::getPsf);
113  cls.def("setPsf", &ExposureT::setPsf, "psf"_a);
114  cls.def("hasPsf", &ExposureT::hasPsf);
115  cls.def("getInfo", (std::shared_ptr<ExposureInfo> (ExposureT::*)()) & ExposureT::getInfo);
116  cls.def("setInfo", &ExposureT::setInfo, "exposureInfo"_a);
117 
118  cls.def("writeFits", (void (ExposureT::*)(std::string const &) const) & ExposureT::writeFits);
119  cls.def("writeFits", (void (ExposureT::*)(fits::MemFileManager &) const) & ExposureT::writeFits);
120 
121  cls.def_static("readFits", (ExposureT(*)(std::string const &))ExposureT::readFits);
122  cls.def_static("readFits", (ExposureT(*)(fits::MemFileManager &))ExposureT::readFits);
123 
124  return cls;
125 }
126 
127 PYBIND11_PLUGIN(exposure) {
128  py::module mod("exposure");
129 
130  py::module::import("lsst.afw.image.exposureInfo");
131  py::module::import("lsst.afw.image.maskedImage");
132 
133  auto clsExposureF = declareExposure<float>(mod, "F");
134  auto clsExposureD = declareExposure<double>(mod, "D");
135  declareExposure<int>(mod, "I");
136  declareExposure<std::uint16_t>(mod, "U");
137  declareExposure<std::uint64_t>(mod, "L");
138 
139  // Declare constructors for casting all exposure types to to float and double
140  // (the only two types of casts that Python supports)
141  declareCastConstructor<int, float>(clsExposureF);
142  declareCastConstructor<int, double>(clsExposureD);
143 
144  declareCastConstructor<float, double>(clsExposureD);
145 
146  declareCastConstructor<double, float>(clsExposureF);
147 
148  declareCastConstructor<std::uint16_t, float>(clsExposureF);
149  declareCastConstructor<std::uint16_t, double>(clsExposureD);
150 
151  declareCastConstructor<std::uint64_t, float>(clsExposureF);
152  declareCastConstructor<std::uint64_t, double>(clsExposureD);
153 
154  return mod.ptr();
155 }
156 }
157 }
158 }
159 } // namespace lsst::afw::image::<anonymous>
Extent< int, 2 > Extent2I
Definition: Extent.h:341
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
double const _a
Definition: RandomImage.cc:77
afw::table::Key< afw::table::Array< ImagePixelT > > image