LSSTApplications  18.1.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 #include "ndarray/pybind11.h"
25 
26 #include <memory>
27 
28 #include "lsst/afw/detection/Psf.h" // forward-declared by Exposure.h
29 #include "lsst/afw/fits.h"
30 #include "lsst/geom/Box.h"
31 #include "lsst/geom/Point.h"
32 #include "lsst/afw/geom/polygon/Polygon.h" // forward-declared by Exposure.h
33 #include "lsst/afw/geom/SkyWcs.h" // forward-declared by Transform.h
34 #include "lsst/afw/image/ApCorrMap.h" // forward-declared by Exposure.h
35 #include "lsst/afw/image/PhotoCalib.h" // forward-declared by Exposure.h
36 #include "lsst/afw/image/VisitInfo.h" // forward-declared by Exposure.h
37 #include "lsst/afw/image/TransmissionCurve.h" // forward-declared by Exposure.h
38 #include "lsst/afw/cameraGeom/Detector.h" // forward-declared by Exposure.h
41 #include "lsst/afw/table/Catalog.h"
46 
47 namespace py = pybind11;
48 using namespace pybind11::literals;
49 
50 namespace lsst {
51 namespace afw {
52 namespace table {
53 
54 namespace {
55 
56 using PyExposureRecord = py::class_<ExposureRecord, std::shared_ptr<ExposureRecord>, BaseRecord>;
57 using PyExposureTable = py::class_<ExposureTable, std::shared_ptr<ExposureTable>, BaseTable>;
58 using PyExposureCatalog =
59  py::class_<ExposureCatalogT<ExposureRecord>, std::shared_ptr<ExposureCatalogT<ExposureRecord>>,
60  SortedCatalogT<ExposureRecord>>;
61 
62 PyExposureRecord declareExposureRecord(py::module &mod) {
63  PyExposureRecord cls(mod, "ExposureRecord");
64  cls.def("getId", &ExposureRecord::getId);
65  cls.def("setId", &ExposureRecord::setId, "id"_a);
66  cls.def("getBBox", &ExposureRecord::getBBox);
67  cls.def("setBBox", &ExposureRecord::setBBox, "bbox"_a);
68  cls.def("getTable", &ExposureRecord::getTable);
69  cls.def_property_readonly("table", &ExposureRecord::getTable);
70  cls.def("contains",
71  (bool (ExposureRecord::*)(lsst::geom::SpherePoint const &, bool) const) &
73  "coord"_a, "includeValidPolygon"_a = false);
74  cls.def("contains",
75  (bool (ExposureRecord::*)(lsst::geom::Point2D const &, geom::SkyWcs const &, bool) const) &
76  ExposureRecord::contains,
77  "point"_a, "wcs"_a, "includeValidPolygon"_a = false);
78  cls.def("getWcs", &ExposureRecord::getWcs);
79  cls.def("setWcs", &ExposureRecord::setWcs, "wcs"_a);
80  cls.def("getPsf", &ExposureRecord::getPsf);
81  cls.def("setPsf", &ExposureRecord::setPsf, "psf"_a);
82 
83  // Deprecated methods
84  cls.def("_getCalib", &ExposureRecord::getCalib);
85  cls.def("_setCalib", &ExposureRecord::setCalib, "photoCalib"_a);
86 
87  cls.def("getPhotoCalib", &ExposureRecord::getPhotoCalib);
88  cls.def("setPhotoCalib", &ExposureRecord::setPhotoCalib, "photoCalib"_a);
89  cls.def("getApCorrMap", &ExposureRecord::getApCorrMap);
90  cls.def("setApCorrMap", &ExposureRecord::setApCorrMap, "apCorrMap"_a);
91  cls.def("getValidPolygon", &ExposureRecord::getValidPolygon);
92 
93  // Workaround for DM-10289.
94  cls.def("setValidPolygon",
95  [](ExposureRecord &self, py::object polygon) {
96  if (polygon.is(py::none())) {
97  self.setValidPolygon(nullptr);
98  } else {
99  self.setValidPolygon(py::cast<std::shared_ptr<afw::geom::polygon::Polygon>>(polygon));
100  }
101  },
102  "polygon"_a);
103 
104  cls.def("getVisitInfo", &ExposureRecord::getVisitInfo);
105  cls.def("setVisitInfo", &ExposureRecord::setVisitInfo, "visitInfo"_a);
106  cls.def("getTransmissionCurve", &ExposureRecord::getTransmissionCurve);
107  cls.def("setTransmissionCurve", &ExposureRecord::setTransmissionCurve, "transmissionCurve"_a);
108  cls.def("getDetector", &ExposureRecord::getDetector);
109  cls.def("setDetector", &ExposureRecord::setDetector, "detector"_a);
110  return cls;
111 }
112 
113 PyExposureTable declareExposureTable(py::module &mod) {
114  PyExposureTable cls(mod, "ExposureTable");
115  cls.def_static("make", &ExposureTable::make);
116  cls.def_static("makeMinimalSchema", &ExposureTable::makeMinimalSchema);
117  cls.def_static("checkSchema", &ExposureTable::checkSchema, "schema"_a);
118 
119  cls.def_static("getIdKey", &ExposureTable::getIdKey);
120  cls.def_static("getBBoxMinKey", &ExposureTable::getBBoxMinKey);
121  cls.def_static("getBBoxMaxKey", &ExposureTable::getBBoxMaxKey);
122 
123  cls.def("clone", &ExposureTable::clone);
124  cls.def("makeRecord", &ExposureTable::makeRecord);
125  cls.def("copyRecord", (std::shared_ptr<ExposureRecord>(ExposureTable::*)(BaseRecord const &)) &
126  ExposureTable::copyRecord);
127  cls.def("copyRecord",
128  (std::shared_ptr<ExposureRecord>(ExposureTable::*)(BaseRecord const &, SchemaMapper const &)) &
129  ExposureTable::copyRecord);
130  return cls;
131 }
132 
133 PyExposureCatalog declareExposureCatalog(py::module &mod) {
134  using Catalog = ExposureCatalogT<ExposureRecord>;
135  table::python::declareSortedCatalog<ExposureRecord>(mod, "Exposure", true);
136 
137  // We need py::dynamic_attr() below to support our Python-side caching of the associated ColumnView.
138  PyExposureCatalog cls(mod, "ExposureCatalog", py::dynamic_attr());
139 
140  cls.def(py::init<Schema const &>(), "schema"_a);
142  cls.def(py::init<Catalog const &>(), "other"_a);
143  // Constructor taking C++ iterators not wrapped; we recommend .extend() (defined in pure Python) instead.
144  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits, "filename"_a,
145  "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
146  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
147  "manager"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
148  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
149 
150  cls.def("subset", (Catalog(Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset,
151  "mask"_a);
152  cls.def("subset",
153  (Catalog(Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) & Catalog::subset,
154  "startd"_a, "stopd"_a, "step"_a);
155  cls.def("subsetContaining",
156  (Catalog(Catalog::*)(lsst::geom::SpherePoint const &, bool) const) & Catalog::subsetContaining,
157  "coord"_a, "includeValidPolygon"_a = false);
158  cls.def("subsetContaining",
159  (Catalog(Catalog::*)(lsst::geom::Point2D const &, geom::SkyWcs const &, bool) const) &
160  Catalog::subsetContaining,
161  "point"_a, "wcs"_a, "includeValidPolygon"_a = false);
162  return cls;
163 };
164 
165 } // anonymous namespace
166 
167 PYBIND11_MODULE(exposure, mod) {
168  py::module::import("lsst.afw.table.simple");
169  py::module::import("lsst.afw.geom");
170  // afw.image and afw.detection cannot be imported due to circular dependencies
171 
172  auto clsExposureRecord = declareExposureRecord(mod);
173  auto clsExposureTable = declareExposureTable(mod);
174  auto clsExposureColumnView = table::python::declareColumnView<ExposureRecord>(mod, "Exposure");
175  auto clsExposureCatalog = declareExposureCatalog(mod);
176 
177  clsExposureRecord.attr("Table") = clsExposureTable;
178  clsExposureRecord.attr("ColumnView") = clsExposureColumnView;
179  clsExposureRecord.attr("Catalog") = clsExposureCatalog;
180  clsExposureTable.attr("Record") = clsExposureRecord;
181  clsExposureTable.attr("ColumnView") = clsExposureColumnView;
182  clsExposureTable.attr("Catalog") = clsExposureCatalog;
183  clsExposureCatalog.attr("Record") = clsExposureRecord;
184  clsExposureCatalog.attr("Table") = clsExposureTable;
185  clsExposureCatalog.attr("ColumnView") = clsExposureColumnView;
186 }
187 } // namespace table
188 } // namespace afw
189 } // namespace lsst
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
def init()
Definition: tests.py:75
STL class.
A base class for image defects.
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
PYBIND11_MODULE(exposure, mod)
Definition: exposure.cc:167
Implementation of the Photometric Calibration class.
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18