LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
LSSTDataManagementBasePackage
Functions
fits.cc File Reference
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "ndarray/pybind11.h"
#include "lsst/pex/exceptions/Exception.h"
#include "lsst/pex/exceptions/Runtime.h"
#include "lsst/pex/exceptions/python/Exception.h"
#include "lsst/daf/base.h"
#include "lsst/afw/image/Image.h"
#include "lsst/afw/fits.h"

Go to the source code of this file.

Functions

void declareImageCompression (py::module &mod)
 
template<typename T >
void declareImageScalingOptionsTemplates (py::class_< ImageScalingOptions > &cls)
 
void declareImageScalingOptions (py::module &mod)
 
template<typename T >
void declareImageScaleTemplates (py::class_< ImageScale > &cls, std::string const &suffix)
 
void declareImageScale (py::module &mod)
 
void declareImageWriteOptions (py::module &mod)
 
void declareFits (py::module &mod)
 
 PYBIND11_MODULE (fits, mod)
 

Function Documentation

◆ declareFits()

void declareFits ( py::module &  mod)

Definition at line 161 of file fits.cc.

161  {
162  py::class_<Fits> cls(mod, "Fits");
163 
164  cls.def(py::init<std::string const&, std::string const&, int>(), "filename"_a, "mode"_a,
165  "behavior"_a=Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
166  cls.def(py::init<MemFileManager &, std::string const&, int>(), "manager"_a, "mode"_a,
167  "behavior"_a=Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
168 
169  cls.def("closeFile", &Fits::closeFile);
170  cls.def("getFileName", &Fits::getFileName);
171  cls.def("getHdu", &Fits::getHdu);
172  cls.def("setHdu", &Fits::setHdu, "hdu"_a, "relative"_a=false);
173  cls.def("countHdus", &Fits::countHdus);
174 
175  cls.def("writeMetadata", &Fits::writeMetadata);
176  cls.def("readMetadata", [](Fits & self, bool strip=false) { return readMetadata(self, strip); },
177  "strip"_a=false);
178  cls.def("createEmpty", &Fits::createEmpty);
179 
180  cls.def("gotoFirstHdu", [](Fits & self) { self.setHdu(DEFAULT_HDU); });
181 
182  cls.def("setImageCompression", &Fits::setImageCompression);
183  cls.def("getImageCompression", &Fits::getImageCompression);
184  cls.def("checkCompressedImagePhu", &Fits::checkCompressedImagePhu);
185 
186  cls.def_readonly("status", &Fits::status);
187 }
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
def writeMetadata(self, dataRefList)
No metadata to write, and not sure how to write it for a list of dataRefs.
bool strip
Definition: fits.cc:901
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: fits.cc:1640

◆ declareImageCompression()

void declareImageCompression ( py::module &  mod)

Definition at line 43 of file fits.cc.

43  {
44  py::class_<ImageCompressionOptions> cls(mod, "ImageCompressionOptions");
45 
46  py::enum_<ImageCompressionOptions::CompressionAlgorithm>(cls, "CompressionAlgorithm").
47  value("NONE", ImageCompressionOptions::CompressionAlgorithm::NONE).
48  value("GZIP", ImageCompressionOptions::CompressionAlgorithm::GZIP).
49  value("GZIP_SHUFFLE", ImageCompressionOptions::CompressionAlgorithm::GZIP_SHUFFLE).
50  value("RICE", ImageCompressionOptions::CompressionAlgorithm::RICE).
51  value("PLIO", ImageCompressionOptions::CompressionAlgorithm::PLIO).
52  export_values();
53 
54  cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm, ImageCompressionOptions::Tiles, float>(),
55  "algorithm"_a, "tiles"_a, "quantizeLevel"_a=0.0);
56  cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm, int, float>(), "algorithm"_a, "rows"_a=1,
57  "quantizeLevel"_a=0.0);
58 
61  cls.def(py::init<lsst::afw::image::Image<short> const&>());
62  cls.def(py::init<lsst::afw::image::Image<int> const&>());
64  cls.def(py::init<lsst::afw::image::Image<float> const&>());
65  cls.def(py::init<lsst::afw::image::Image<double> const&>());
67 
70  cls.def(py::init<lsst::afw::image::Mask<short> const&>());
72 
73  cls.def_readonly("algorithm", &ImageCompressionOptions::algorithm);
74  cls.def_readonly("tiles", &ImageCompressionOptions::tiles);
75  cls.def_readonly("quantizeLevel", &ImageCompressionOptions::quantizeLevel);
76 }
def init()
Definition: tests.py:64
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:58

◆ declareImageScale()

void declareImageScale ( py::module &  mod)

Definition at line 122 of file fits.cc.

122  {
123  py::class_<ImageScale> cls(mod, "ImageScale");
124  cls.def(py::init<int, double, double>(), "bitpix"_a, "bscale"_a, "bzero"_a);
125  cls.def_readonly("bitpix", &ImageScale::bitpix);
126  cls.def_readonly("bscale", &ImageScale::bscale);
127  cls.def_readonly("bzero", &ImageScale::bzero);
128  cls.def_readonly("blank", &ImageScale::blank);
129 
130  declareImageScaleTemplates<float>(cls, "F");
131  declareImageScaleTemplates<double>(cls, "D");
132 }

◆ declareImageScaleTemplates()

template<typename T >
void declareImageScaleTemplates ( py::class_< ImageScale > &  cls,
std::string const &  suffix 
)

Definition at line 116 of file fits.cc.

116  {
117  cls.def("toFits", &ImageScale::toFits<T>, "image"_a, "forceNonfiniteRemoval"_a=false, "fuzz"_a=true,
118  "tiles"_a=ndarray::Array<long, 1, 1>(), "seed"_a=1);
119  cls.def("fromFits", &ImageScale::fromFits<T>);
120 }

◆ declareImageScalingOptions()

void declareImageScalingOptions ( py::module &  mod)

Definition at line 84 of file fits.cc.

84  {
85  py::class_<ImageScalingOptions> cls(mod, "ImageScalingOptions");
86  py::enum_<ImageScalingOptions::ScalingAlgorithm>(cls, "ScalingAlgorithm").
87  value("NONE", ImageScalingOptions::ScalingAlgorithm::NONE).
88  value("RANGE", ImageScalingOptions::ScalingAlgorithm::RANGE).
89  value("STDEV_POSITIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_POSITIVE).
90  value("STDEV_NEGATIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_NEGATIVE).
91  value("STDEV_BOTH", ImageScalingOptions::ScalingAlgorithm::STDEV_BOTH).
92  value("MANUAL", ImageScalingOptions::ScalingAlgorithm::MANUAL).
93  export_values();
94 
95  cls.def(py::init<>());
97  unsigned long, float, float, bool, double, double>(),
98  "algorithm"_a, "bitpix"_a, "maskPlanes"_a=std::vector<std::string>(), "seed"_a=1,
99  "quantizeLevel"_a=4.0, "quantizePad"_a=5.0, "fuzz"_a=true, "bscale"_a=1.0, "bzero"_a=0.0);
100 
101  cls.def_readonly("algorithm", &ImageScalingOptions::algorithm);
102  cls.def_readonly("bitpix", &ImageScalingOptions::bitpix);
103  cls.def_readonly("maskPlanes", &ImageScalingOptions::maskPlanes);
104  cls.def_readonly("seed", &ImageScalingOptions::seed);
105  cls.def_readonly("quantizeLevel", &ImageScalingOptions::quantizeLevel);
106  cls.def_readonly("quantizePad", &ImageScalingOptions::quantizePad);
107  cls.def_readonly("fuzz", &ImageScalingOptions::fuzz);
108  cls.def_readonly("bscale", &ImageScalingOptions::bscale);
109  cls.def_readonly("bzero", &ImageScalingOptions::bzero);
110 
111  declareImageScalingOptionsTemplates<float>(cls);
112  declareImageScalingOptionsTemplates<double>(cls);
113 }
def init()
Definition: tests.py:64

◆ declareImageScalingOptionsTemplates()

template<typename T >
void declareImageScalingOptionsTemplates ( py::class_< ImageScalingOptions > &  cls)

Definition at line 80 of file fits.cc.

80  {
81  cls.def("determine", &ImageScalingOptions::determine<T>);
82 }

◆ declareImageWriteOptions()

void declareImageWriteOptions ( py::module &  mod)

Definition at line 134 of file fits.cc.

134  {
135  py::class_<ImageWriteOptions> cls(mod, "ImageWriteOptions");
136 
142 
144 
145  cls.def(py::init<ImageCompressionOptions const&, ImageScalingOptions const&>(),
146  "compression"_a, "scaling"_a=ImageScalingOptions());
147  cls.def(py::init<ImageScalingOptions const&>());
148 
149  cls.def(py::init<lsst::daf::base::PropertySet const&>());
150 
151  cls.def_readonly("compression", &ImageWriteOptions::compression);
152  cls.def_readonly("scaling", &ImageWriteOptions::scaling);
153 
154  cls.def_static("validate", &ImageWriteOptions::validate);
155 }
def init()
Definition: tests.py:64
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
Options for scaling image pixels.
table::Key< double > scaling
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:58

◆ PYBIND11_MODULE()

PYBIND11_MODULE ( fits  ,
mod   
)

Definition at line 190 of file fits.cc.

190  {
191  py::class_<MemFileManager> clsMemFileManager(mod, "MemFileManager");
192 
193  py::module::import("lsst.pex.exceptions");
194 
195  lsst::pex::exceptions::python::declareException<FitsError, lsst::pex::exceptions::IoError>(
196  mod, "FitsError", "IoError");
197  // lsst::pex::exceptions::python::declareException<FitsTypeError, FitsError>(mod, "FitsTypeError",
198  // "FitsError");
199 
200  clsMemFileManager.def(py::init<>());
201  clsMemFileManager.def(py::init<size_t>());
202 
203  /* TODO: We should really revisit persistence and pickling as this is quite ugly.
204  * But it is what Swig did (sort of, it used the cdata.i extension), so I reckon this
205  * is cleaner because it does not expose casting to the Python side. */
206  clsMemFileManager.def("getLength", &MemFileManager::getLength);
207  clsMemFileManager.def("getData", [](MemFileManager &m) {
208  return py::bytes(static_cast<char *>(m.getData()), m.getLength());
209  });
210  clsMemFileManager.def("setData", [](MemFileManager &m, py::bytes const &d, size_t size) {
211  memcpy(m.getData(), PyBytes_AsString(d.ptr()), size);
212  });
213  clsMemFileManager.def("readMetadata",
214  [](MemFileManager & self, int hdu=DEFAULT_HDU, bool strip=false) {
215  return readMetadata(self, hdu, strip);
216  }, "hdu"_a=DEFAULT_HDU, "strip"_a=false);
217 
220  declareImageScale(mod);
222  declareFits(mod);
223 
224  mod.attr("DEFAULT_HDU") = DEFAULT_HDU;
225  mod.def("combineMetadata", combineMetadata, "first"_a, "second"_a);
226  mod.def("makeLimitedFitsHeader", &makeLimitedFitsHeader, "metadata"_a,
227  "excludeNames"_a = std::set<std::string>());
228  mod.def("readMetadata",
229  [](std::string const& filename, int hdu=DEFAULT_HDU, bool strip=false) {
230  return readMetadata(filename, hdu, strip);
231  }, "fileName"_a, "hdu"_a=DEFAULT_HDU, "strip"_a=false);
232  mod.def("setAllowImageCompression", &setAllowImageCompression, "allow"_a);
233  mod.def("getAllowImageCompression", &getAllowImageCompression);
234 
235  mod.def("compressionAlgorithmFromString", &compressionAlgorithmFromString);
236  mod.def("compressionAlgorithmToString", &compressionAlgorithmToString);
237  mod.def("scalingAlgorithmFromString", &scalingAlgorithmFromString);
238  mod.def("scalingAlgorithmToString", &scalingAlgorithmToString);
239 }
std::shared_ptr< daf::base::PropertyList > combineMetadata(std::shared_ptr< const daf::base::PropertyList > first, std::shared_ptr< const daf::base::PropertyList > second)
Combine two sets of metadata in a FITS-appropriate fashion.
Definition: fits.cc:1611
void declareImageWriteOptions(py::module &mod)
Definition: fits.cc:134
void declareImageScale(py::module &mod)
Definition: fits.cc:122
STL class.
void declareImageScalingOptions(py::module &mod)
Definition: fits.cc:84
T memcpy(T... args)
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
void * getData() const
Return the buffer.
Definition: fits.h:191
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
std::string scalingAlgorithmToString(ImageScalingOptions::ScalingAlgorithm algorithm)
Provide string version of compression algorithm.
std::string compressionAlgorithmToString(ImageCompressionOptions::CompressionAlgorithm algorithm)
Provide string version of compression algorithm.
void declareImageCompression(py::module &mod)
Definition: fits.cc:43
STL class.
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:194
void setAllowImageCompression(bool allow)
Definition: fits.cc:1529
table::Key< table::Array< std::uint8_t > > bytes
Definition: Transform.cc:199
int m
Definition: SpanSet.cc:49
bool getAllowImageCompression()
Definition: fits.cc:1531
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
bool strip
Definition: fits.cc:901
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.
Definition: fits.cc:456
void declareFits(py::module &mod)
Definition: fits.cc:161
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: fits.cc:1640