LSSTApplications  20.0.0
LSSTDataManagementBasePackage
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 
26 #include "lsst/afw/geom/SkyWcs.h"
28 #include "lsst/afw/image/Filter.h"
30 #include "lsst/afw/detection/Psf.h"
31 
32 namespace py = pybind11;
33 using namespace pybind11::literals;
34 
35 namespace lsst {
36 namespace afw {
37 namespace image {
38 namespace {
39 
40 template <typename PixelT>
41 using PyExposure = py::class_<Exposure<PixelT>, std::shared_ptr<Exposure<PixelT>>>;
42 
43 /*
44 Declare a constructor that takes an Exposure of FromPixelT and returns an Exposure cast to ToPixelT
45 
46 The mask and variance must be of the standard types.
47 
48 @param[in] cls The pybind11 class to which add the constructor
49 */
50 template <typename FromPixelT, typename ToPixelT>
51 void declareCastConstructor(PyExposure<ToPixelT> &cls) {
52  cls.def(py::init<Exposure<FromPixelT> const &, bool const>(), "src"_a, "deep"_a);
53 }
54 
55 template <typename PixelT> // only the image type varies; mask and variance are fixed to default tparams
56 PyExposure<PixelT> declareExposure(py::module &mod, const std::string &suffix) {
57  using ExposureT = Exposure<PixelT>;
58  using MaskedImageT = typename ExposureT::MaskedImageT;
59 
60  PyExposure<PixelT> cls(mod, ("Exposure" + suffix).c_str());
61 
62  mod.def("makeExposure", &makeExposure<PixelT, MaskPixel, VariancePixel>, "maskedImage"_a,
64 
65  /* Constructors */
66  cls.def(py::init<unsigned int, unsigned int, std::shared_ptr<geom::SkyWcs const>>(), "width"_a,
67  "height"_a, "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
69  "dimensions"_a = lsst::geom::Extent2I(), "wcs"_a = std::shared_ptr<geom::SkyWcs const>());
72  cls.def(py::init<MaskedImageT &, std::shared_ptr<geom::SkyWcs const>>(), "maskedImage"_a,
74  cls.def(py::init<MaskedImageT &, std::shared_ptr<ExposureInfo>>(), "maskedImage"_a, "exposureInfo"_a);
75  cls.def(py::init<std::string const &, lsst::geom::Box2I const &, ImageOrigin, bool, bool>(), "fileName"_a,
76  "bbox"_a = lsst::geom::Box2I(), "origin"_a = PARENT, "conformMasks"_a = false,
77  "allowUnsafe"_a = false);
78  cls.def(py::init<fits::MemFileManager &, lsst::geom::Box2I const &, ImageOrigin, bool, bool>(),
79  "manager"_a, "bbox"_a = lsst::geom::Box2I(), "origin"_a = PARENT, "conformMasks"_a = false,
80  "allowUnsafe"_a = false);
81  cls.def(py::init<ExposureT const &, bool>(), "other"_a, "deep"_a = false);
82  cls.def(py::init<ExposureT const &, lsst::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("maskedImage", (MaskedImageT(ExposureT::*)()) & ExposureT::getMaskedImage,
89  &ExposureT::setMaskedImage);
90  cls.def("getMetadata", &ExposureT::getMetadata);
91  cls.def("setMetadata", &ExposureT::setMetadata, "metadata"_a);
92  cls.def("getWidth", &ExposureT::getWidth);
93  cls.def("getHeight", &ExposureT::getHeight);
94  cls.def("getDimensions", &ExposureT::getDimensions);
95  cls.def("getX0", &ExposureT::getX0);
96  cls.def("getY0", &ExposureT::getY0);
97  cls.def("getXY0", &ExposureT::getXY0);
98  cls.def("setXY0", &ExposureT::setXY0, "xy0"_a);
99  cls.def("getBBox", &ExposureT::getBBox, "origin"_a = PARENT);
100  cls.def("getWcs", (std::shared_ptr<geom::SkyWcs>(ExposureT::*)()) & ExposureT::getWcs);
101  cls.def("setWcs", &ExposureT::setWcs, "wcs"_a);
102  cls.def("hasWcs", &ExposureT::hasWcs);
103  cls.def("getDetector", &ExposureT::getDetector);
104  cls.def("setDetector", &ExposureT::setDetector, "detector"_a);
105  cls.def("getFilter", &ExposureT::getFilter);
106  cls.def("setFilter", &ExposureT::setFilter, "filter"_a);
107 
108  cls.def("getPhotoCalib", &ExposureT::getPhotoCalib);
109  cls.def("setPhotoCalib", &ExposureT::setPhotoCalib, "photoCalib"_a);
110  cls.def("getPsf", (std::shared_ptr<detection::Psf>(ExposureT::*)()) & ExposureT::getPsf);
111  cls.def("setPsf", &ExposureT::setPsf, "psf"_a);
112  cls.def("hasPsf", &ExposureT::hasPsf);
113  cls.def("getInfo", (std::shared_ptr<ExposureInfo>(ExposureT::*)()) & ExposureT::getInfo);
114  cls.def("setInfo", &ExposureT::setInfo, "exposureInfo"_a);
115 
116  cls.def("subset", &ExposureT::subset, "bbox"_a, "origin"_a = PARENT);
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  cls.def("writeFits", [](ExposureT &self, fits::Fits &fits) { self.writeFits(fits); });
121 
122  cls.def("writeFits",
123  [](ExposureT &self, std::string const &filename, fits::ImageWriteOptions const &imageOptions,
124  fits::ImageWriteOptions const &maskOptions, fits::ImageWriteOptions const &varianceOptions) {
125  self.writeFits(filename, imageOptions, maskOptions, varianceOptions);
126  },
127  "filename"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
128  cls.def("writeFits",
129  [](ExposureT &self, fits::MemFileManager &manager, fits::ImageWriteOptions const &imageOptions,
130  fits::ImageWriteOptions const &maskOptions, fits::ImageWriteOptions const &varianceOptions) {
131  self.writeFits(manager, imageOptions, maskOptions, varianceOptions);
132  },
133  "manager"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
134  cls.def("writeFits",
135  [](ExposureT &self, fits::Fits &fits, fits::ImageWriteOptions const &imageOptions,
136  fits::ImageWriteOptions const &maskOptions, fits::ImageWriteOptions const &varianceOptions) {
137  self.writeFits(fits, imageOptions, maskOptions, varianceOptions);
138  },
139  "fits"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a);
140 
141  cls.def_static("readFits", (ExposureT(*)(std::string const &))ExposureT::readFits);
142  cls.def_static("readFits", (ExposureT(*)(fits::MemFileManager &))ExposureT::readFits);
143 
144  cls.def("getCutout", &ExposureT::getCutout, "center"_a, "size"_a);
145 
146  return cls;
147 }
148 
149 PYBIND11_MODULE(exposure, mod) {
150  py::module::import("lsst.afw.image.exposureInfo");
151  py::module::import("lsst.afw.image.maskedImage");
152 
153  auto clsExposureF = declareExposure<float>(mod, "F");
154  auto clsExposureD = declareExposure<double>(mod, "D");
155  declareExposure<int>(mod, "I");
156  declareExposure<std::uint16_t>(mod, "U");
157  declareExposure<std::uint64_t>(mod, "L");
158 
159  // Declare constructors for casting all exposure types to to float and double
160  // (the only two types of casts that Python supports)
161  declareCastConstructor<int, float>(clsExposureF);
162  declareCastConstructor<int, double>(clsExposureD);
163 
164  declareCastConstructor<float, double>(clsExposureD);
165 
166  declareCastConstructor<double, float>(clsExposureF);
167 
168  declareCastConstructor<std::uint16_t, float>(clsExposureF);
169  declareCastConstructor<std::uint16_t, double>(clsExposureD);
170 
171  declareCastConstructor<std::uint64_t, float>(clsExposureF);
172  declareCastConstructor<std::uint64_t, double>(clsExposureD);
173 }
174 } // namespace
175 } // namespace image
176 } // namespace afw
177 } // namespace lsst
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
std::string
STL class.
std::shared_ptr
STL class.
Psf.h
lsst::afw
Definition: imageAlgorithm.dox:1
SkyWcs.h
Filter.h
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
fits
Fits * fits
Definition: FitsWriter.cc:90
lsstDebug.getInfo
getInfo
Definition: lsstDebug.py:87
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
PhotoCalib.h
Implementation of the Photometric Calibration class.
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw::image::PARENT
@ PARENT
Definition: ImageBase.h:94
pybind11
Definition: _GenericMap.cc:40
lsst::utils.tests.init
def init()
Definition: tests.py:58
Exposure.h
lsst::geom::Extent< int, 2 >
lsst::meas::modelfit.psf.psfContinued.module
module
Definition: psfContinued.py:42
lsst::afw::cameraGeom::PYBIND11_MODULE
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:133
Detector.h