24#include <pybind11/pybind11.h>
25#include <pybind11/stl.h>
26#include <pybind11/native_enum.h>
30#include "ndarray/pybind11.h"
42using namespace pybind11::literals;
47void declareCompression(lsst::cpputils::python::WrapperCollection &wrappers) {
48 py::native_enum<CompressionAlgorithm>(wrappers.
module,
"CompressionAlgorithm",
"enum.Enum")
53 py::native_enum<DitherAlgorithm>(wrappers.
module,
"DitherAlgorithm",
"enum.Enum")
58 py::native_enum<ScalingAlgorithm>(wrappers.
module,
"ScalingAlgorithm",
"enum.Enum")
65 py::class_<QuantizationOptions>(wrappers.
module,
"QuantizationOptions"),
66 [](
auto &mod,
auto &cls) {
68 py::init<DitherAlgorithm, ScalingAlgorithm, std::vector<std::string>, float, int>(),
70 "dither"_a = DitherAlgorithm::NO_DITHER_,
71 "scaling"_a = ScalingAlgorithm::STDEV_MASKED,
72 "mask_planes"_a = py::list(),
76 cls.def_readwrite(
"dither", &QuantizationOptions::dither);
77 cls.def_readwrite(
"scaling", &QuantizationOptions::scaling);
78 cls.def_readwrite(
"mask_planes", &QuantizationOptions::mask_planes);
79 cls.def_readwrite(
"level", &QuantizationOptions::level);
80 cls.def_readwrite(
"seed", &QuantizationOptions::seed);
83 py::class_<CompressionOptions>(wrappers.
module,
"CompressionOptions"),
84 [](
auto &mod,
auto &cls) {
90 std::optional<QuantizationOptions>
93 "algorithm"_a = CompressionAlgorithm::GZIP_2_,
96 "quantization"_a = py::none()
98 cls.def_readwrite(
"algorithm", &CompressionOptions::algorithm);
99 cls.def_readwrite(
"tile_width", &CompressionOptions::tile_width);
100 cls.def_readwrite(
"tile_height", &CompressionOptions::tile_height);
101 cls.def_readwrite(
"quantization", &CompressionOptions::quantization);
109void declareFits(lsst::cpputils::python::WrapperCollection &wrappers) {
110 wrappers.
wrapType(py::class_<Fits>(wrappers.
module,
"Fits"), [](
auto &mod,
auto &cls) {
111 cls.def(py::init<std::string const &, std::string const &, int>(),
"filename"_a,
"mode"_a,
112 "behavior"_a = Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
113 cls.def(py::init<MemFileManager &, std::string const &, int>(),
"manager"_a,
"mode"_a,
114 "behavior"_a = Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
116 cls.def(
"closeFile", &Fits::closeFile);
117 cls.def(
"getFileName", &Fits::getFileName);
118 cls.def(
"getHdu", &Fits::getHdu);
119 cls.def(
"setHdu", py::overload_cast<int, bool>(&Fits::setHdu),
"hdu"_a,
"relative"_a = false);
121 "setHdu", [](Fits &self, std::string const &name) { self.setHdu(name); },
"name"_a);
126 "readMetadata", [](
Fits &self,
bool strip =
false) {
return readMetadata(self, strip); },
130 cls.def(
"readImageI", [](
Fits &self) {
131 ndarray::Vector<int, 2>
const offset;
132 ndarray::Vector<ndarray::Size, 2> shape = self.getImageShape<2>();
133 ndarray::Array<int, 2, 2>
result = ndarray::allocate(shape[0], shape[1]);
134 self.readImage(result, offset);
146void declareFitsModule(lsst::cpputils::python::WrapperCollection &wrappers) {
147 wrappers.
wrap([](
auto &mod) {
148 py::class_<MemFileManager> clsMemFileManager(mod,
"MemFileManager");
150 clsMemFileManager.def(py::init<>());
151 clsMemFileManager.def(py::init<size_t>());
158 return py::bytes(
static_cast<char *
>(m.getData()), m.getLength());
160 clsMemFileManager.def(
"setData", [](
MemFileManager &m, py::bytes
const &d,
size_t size) {
161 memcpy(m.getData(), PyBytes_AsString(d.ptr()), size);
163 clsMemFileManager.def(
172 py::overload_cast<daf::base::PropertyList const&, daf::base::PropertyList const &>(
175 "first"_a,
"second"_a
178 "excludeNames"_a = std::set<std::string>());
181 [](std::string
const &filename,
int hdu =
DEFAULT_HDU,
bool strip =
false) {
184 "fileName"_a,
"hdu"_a =
DEFAULT_HDU,
"strip"_a =
false);
188 [](std::string
const &filename, std::string
const &hduname,
bool strip =
false) {
191 "fileName"_a,
"hduName"_a,
"strip"_a =
false);
202 cls.def(py::init<std::string const &>());
203 declareCompression(wrappers);
204 declareFits(wrappers);
205 declareFitsModule(wrappers);
An exception thrown when problems are found when reading or writing FITS files.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
int countHdus()
Return the number of HDUs in the file.
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
Lifetime-management for memory that goes into FITS memory files.
std::size_t getLength() const
Return the buffer length.
A helper class for subdividing pybind11 module across multiple translation units (i....
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
void wrap(WrapperCallback function)
Add a set of wrappers without defining a class.
void addInheritanceDependency(std::string const &name)
Indicate an external module that provides a base class for a subsequent addType call.
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...
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
void finish()
Invoke all deferred wrapper-declaring callables.
auto wrapException(std::string const &pyName, std::string const &pyBase, bool setModuleName=true)
Wrap a C++ exception as a Python exception.
Reports errors in external input/output operations.
PYBIND11_MODULE(_gauss2d, m)
std::shared_ptr< daf::base::PropertyList > combineMetadata(daf::base::PropertyList const &first, daf::base::PropertyList const &second)
Combine two sets of metadata in a FITS-appropriate fashion.
const int DEFAULT_HDU
Specify that the default HDU should be read.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
@ RANGE
Scale to preserve dynamic range with bad pixels msasked out.
@ STDEV_MASKED
Scale based on the standard deviation with bad pixels masked out.
@ STDEV_CFITSIO
Let CFITSIO work out the scaling (per-tile; does not respect mask planes)
@ MANUAL
Scale set manually.
std::string makeLimitedFitsHeader(lsst::daf::base::PropertySet const &metadata, std::set< std::string > const &excludeNames={})
Format a PropertySet into an FITS header string in a simplistic fashion.