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 <memory>
26 
27 #include "lsst/afw/coord/Coord.h"
28 #include "lsst/afw/detection/Psf.h" // forward-declared by Exposure.h
29 #include "lsst/afw/fits.h"
30 #include "lsst/afw/geom/Box.h"
31 #include "lsst/afw/geom/Point.h"
32 #include "lsst/afw/geom/polygon/Polygon.h" // forward-declared by Exposure.h
33 #include "lsst/afw/image/ApCorrMap.h" // forward-declared by Exposure.h
34 #include "lsst/afw/image/Calib.h" // forward-declared by Exposure.h
35 #include "lsst/afw/image/VisitInfo.h" // forward-declared by Exposure.h
36 #include "lsst/afw/image/Wcs.h" // forward-declared by Exposure.h
39 #include "lsst/afw/table/Catalog.h"
44 
45 namespace py = pybind11;
46 using namespace pybind11::literals;
47 
48 namespace lsst {
49 namespace afw {
50 namespace table {
51 
52 namespace {
53 
54 using PyExposureRecord = py::class_<ExposureRecord, std::shared_ptr<ExposureRecord>, BaseRecord>;
55 using PyExposureTable = py::class_<ExposureTable, std::shared_ptr<ExposureTable>, BaseTable>;
56 using PyExposureCatalog =
57  py::class_<ExposureCatalogT<ExposureRecord>, std::shared_ptr<ExposureCatalogT<ExposureRecord>>,
58  SortedCatalogT<ExposureRecord>>;
59 
60 PyExposureRecord declareExposureRecord(py::module &mod) {
61  PyExposureRecord cls(mod, "ExposureRecord");
62  cls.def("getId", &ExposureRecord::getId);
63  cls.def("setId", &ExposureRecord::setId, "id"_a);
64  cls.def("getBBox", &ExposureRecord::getBBox);
65  cls.def("setBBox", &ExposureRecord::setBBox, "bbox"_a);
66  cls.def("getTable", &ExposureRecord::getTable);
67  cls.def_property_readonly("table", &ExposureRecord::getTable);
68  cls.def("contains",
69  (bool (ExposureRecord::*)(coord::Coord const &, bool) const) & ExposureRecord::contains,
70  "coord"_a, "includeValidPolygon"_a = false);
71  cls.def("contains", (bool (ExposureRecord::*)(geom::Point2D const &, image::Wcs const &, bool) const) &
72  ExposureRecord::contains,
73  "point"_a, "wcs"_a, "includeValidPolygon"_a = false);
74  cls.def("getWcs", &ExposureRecord::getWcs);
75  cls.def("setWcs", &ExposureRecord::setWcs, "wcs"_a);
76  cls.def("getPsf", &ExposureRecord::getPsf);
77  cls.def("setPsf", &ExposureRecord::setPsf, "psf"_a);
78  cls.def("getCalib", &ExposureRecord::getCalib);
79  cls.def("setCalib", &ExposureRecord::setCalib, "calib"_a);
80  cls.def("getApCorrMap", &ExposureRecord::getApCorrMap);
81  cls.def("setApCorrMap", &ExposureRecord::setApCorrMap, "appCorrMap"_a);
82  cls.def("getValidPolygon", &ExposureRecord::getValidPolygon);
83 
84  // Workaround for DM-10289.
85  cls.def("setValidPolygon",
86  [](ExposureRecord &self, py::object polygon) {
87  if (polygon == py::none()) {
88  self.setValidPolygon(nullptr);
89  } else {
90  self.setValidPolygon(py::cast<std::shared_ptr<afw::geom::polygon::Polygon>>(polygon));
91  }
92  },
93  "polygon"_a);
94 
95  cls.def("getVisitInfo", &ExposureRecord::getVisitInfo);
96  cls.def("setVisitInfo", &ExposureRecord::setVisitInfo, "visitInfo"_a);
97  return cls;
98 }
99 
100 PyExposureTable declareExposureTable(py::module &mod) {
101  PyExposureTable cls(mod, "ExposureTable");
102  cls.def_static("make", &ExposureTable::make);
103  cls.def_static("makeMinimalSchema", &ExposureTable::makeMinimalSchema);
104  cls.def_static("checkSchema", &ExposureTable::checkSchema, "schema"_a);
105 
106  cls.def_static("getIdKey", &ExposureTable::getIdKey);
107  cls.def_static("getBBoxMinKey", &ExposureTable::getBBoxMinKey);
108  cls.def_static("getBBoxMaxKey", &ExposureTable::getBBoxMaxKey);
109 
110  cls.def("clone", &ExposureTable::clone);
111  cls.def("makeRecord", &ExposureTable::makeRecord);
112  cls.def("copyRecord", (std::shared_ptr<ExposureRecord> (ExposureTable::*)(BaseRecord const &)) &
113  ExposureTable::copyRecord);
114  cls.def("copyRecord",
115  (std::shared_ptr<ExposureRecord> (ExposureTable::*)(BaseRecord const &, SchemaMapper const &)) &
116  ExposureTable::copyRecord);
117  return cls;
118 }
119 
120 PyExposureCatalog declareExposureCatalog(py::module &mod) {
121  using Catalog = ExposureCatalogT<ExposureRecord>;
122  table::python::declareSortedCatalog<ExposureRecord>(mod, "Exposure", true);
123 
124  // We need py::dynamic_attr() below to support our Python-side caching of the associated ColumnView.
125  PyExposureCatalog cls(mod, "ExposureCatalog", py::dynamic_attr());
126 
127  cls.def(py::init<Schema const &>(), "schema"_a);
128  cls.def(py::init<std::shared_ptr<ExposureTable> const &>(), "table"_a = std::shared_ptr<ExposureTable>());
129  cls.def(py::init<Catalog const &>(), "other"_a);
130  // Constructor taking C++ iterators not wrapped; we recommend .extend() (defined in pure Python) instead.
131  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits, "filename"_a,
132  "hdu"_a = INT_MIN, "flags"_a = 0);
133  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
134  "manager"_a, "hdu"_a = INT_MIN, "flags"_a = 0);
135  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
136 
137  cls.def("subset", (Catalog (Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset,
138  "mask"_a);
139  cls.def("subset",
140  (Catalog (Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) & Catalog::subset,
141  "startd"_a, "stopd"_a, "step"_a);
142  cls.def("subsetContaining",
143  (Catalog (Catalog::*)(coord::Coord const &, bool) const) & Catalog::subsetContaining, "coord"_a,
144  "includeValidPolygon"_a = false);
145  cls.def("subsetContaining",
146  (Catalog (Catalog::*)(geom::Point2D const &, image::Wcs const &, bool) const) &
147  Catalog::subsetContaining,
148  "point"_a, "wcs"_a, "includeValidPolygon"_a = false);
149  return cls;
150 };
151 
152 } // anonymous namespace
153 
154 PYBIND11_PLUGIN(exposure) {
155  py::module mod("exposure");
156  py::module::import("lsst.afw.table.simple");
157  py::module::import("lsst.afw.geom");
158  // afw.image and afw.detection cannot be imported due to circular dependencies
159 
160  auto clsExposureRecord = declareExposureRecord(mod);
161  auto clsExposureTable = declareExposureTable(mod);
162  auto clsExposureColumnView = table::python::declareColumnView<ExposureRecord>(mod, "Exposure");
163  auto clsExposureCatalog = declareExposureCatalog(mod);
164 
165  clsExposureRecord.attr("Table") = clsExposureTable;
166  clsExposureRecord.attr("ColumnView") = clsExposureColumnView;
167  clsExposureRecord.attr("Catalog") = clsExposureCatalog;
168  clsExposureTable.attr("Record") = clsExposureRecord;
169  clsExposureTable.attr("ColumnView") = clsExposureColumnView;
170  clsExposureTable.attr("Catalog") = clsExposureCatalog;
171  clsExposureCatalog.attr("Record") = clsExposureRecord;
172  clsExposureCatalog.attr("Table") = clsExposureTable;
173  clsExposureCatalog.attr("ColumnView") = clsExposureColumnView;
174 
175  return mod.ptr();
176 }
177 }
178 }
179 } // namespace lsst::afw::table
lsst::afw::coord::Coord Coord
Definition: misc.h:33
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:104
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
Point< double, 2 > Point2D
Definition: Point.h:273
double const _a
Definition: RandomImage.cc:77