LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.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("readImageI", [](Fits &self) {
181  ndarray::Vector<int, 2> const offset; // initialized to zero by default
182  ndarray::Vector<ndarray::Size, 2> shape = self.getImageShape<2>();
183  ndarray::Array<int, 2, 2> result = ndarray::allocate(shape[0], shape[1]);
184  self.readImage(result, offset);
185  return result;
186  });
187 
188  cls.def("gotoFirstHdu", [](Fits & self) { self.setHdu(DEFAULT_HDU); });
189 
190  cls.def("setImageCompression", &Fits::setImageCompression);
191  cls.def("getImageCompression", &Fits::getImageCompression);
192  cls.def("checkCompressedImagePhu", &Fits::checkCompressedImagePhu);
193 
194  cls.def_readonly("status", &Fits::status);
195 }

◆ 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 
67 
72 
73  cls.def_readonly("algorithm", &ImageCompressionOptions::algorithm);
74  cls.def_readonly("tiles", &ImageCompressionOptions::tiles);
75  cls.def_readonly("quantizeLevel", &ImageCompressionOptions::quantizeLevel);
76 }

◆ 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 }

◆ 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 }

◆ PYBIND11_MODULE()

PYBIND11_MODULE ( fits  ,
mod   
)

Definition at line 198 of file fits.cc.

198  {
199  py::class_<MemFileManager> clsMemFileManager(mod, "MemFileManager");
200 
201  py::module::import("lsst.pex.exceptions");
202 
203  lsst::pex::exceptions::python::declareException<FitsError, lsst::pex::exceptions::IoError>(
204  mod, "FitsError", "IoError");
205  // lsst::pex::exceptions::python::declareException<FitsTypeError, FitsError>(mod, "FitsTypeError",
206  // "FitsError");
207 
208  clsMemFileManager.def(py::init<>());
209  clsMemFileManager.def(py::init<size_t>());
210 
211  /* TODO: We should really revisit persistence and pickling as this is quite ugly.
212  * But it is what Swig did (sort of, it used the cdata.i extension), so I reckon this
213  * is cleaner because it does not expose casting to the Python side. */
214  clsMemFileManager.def("getLength", &MemFileManager::getLength);
215  clsMemFileManager.def("getData", [](MemFileManager &m) {
216  return py::bytes(static_cast<char *>(m.getData()), m.getLength());
217  });
218  clsMemFileManager.def("setData", [](MemFileManager &m, py::bytes const &d, size_t size) {
219  memcpy(m.getData(), PyBytes_AsString(d.ptr()), size);
220  });
221  clsMemFileManager.def("readMetadata",
222  [](MemFileManager & self, int hdu=DEFAULT_HDU, bool strip=false) {
223  return readMetadata(self, hdu, strip);
224  }, "hdu"_a=DEFAULT_HDU, "strip"_a=false);
225 
228  declareImageScale(mod);
230  declareFits(mod);
231 
232  mod.attr("DEFAULT_HDU") = DEFAULT_HDU;
233  mod.def("combineMetadata", combineMetadata, "first"_a, "second"_a);
234  mod.def("makeLimitedFitsHeader", &makeLimitedFitsHeader, "metadata"_a,
235  "excludeNames"_a = std::set<std::string>());
236  mod.def("readMetadata",
237  [](std::string const& filename, int hdu=DEFAULT_HDU, bool strip=false) {
238  return readMetadata(filename, hdu, strip);
239  }, "fileName"_a, "hdu"_a=DEFAULT_HDU, "strip"_a=false);
240  mod.def("setAllowImageCompression", &setAllowImageCompression, "allow"_a);
241  mod.def("getAllowImageCompression", &getAllowImageCompression);
242 
243  mod.def("compressionAlgorithmFromString", &compressionAlgorithmFromString);
244  mod.def("compressionAlgorithmToString", &compressionAlgorithmToString);
245  mod.def("scalingAlgorithmFromString", &scalingAlgorithmFromString);
246  mod.def("scalingAlgorithmToString", &scalingAlgorithmToString);
247 }
lsst::afw::fits::readMetadata
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: fits.cc:1657
std::string
STL class.
lsst::afw::image::Mask
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
lsst::afw::fits::DEFAULT_HDU
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
lsst::afw::fits::Fits
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
std::vector< std::string >
declareImageWriteOptions
void declareImageWriteOptions(py::module &mod)
Definition: fits.cc:134
lsst::afw::fits::combineMetadata
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:1628
strip
bool strip
Definition: fits.cc:911
lsst::afw::fits::MemFileManager
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
declareImageScalingOptions
void declareImageScalingOptions(py::module &mod)
Definition: fits.cc:84
declareImageScale
void declareImageScale(py::module &mod)
Definition: fits.cc:122
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
declareImageCompression
void declareImageCompression(py::module &mod)
Definition: fits.cc:43
lsst::afw::fits::ImageScalingOptions
Options for scaling image pixels.
Definition: fitsCompression.h:355
lsst::afw::fits::compressionAlgorithmFromString
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
Definition: fitsCompression.cc:20
lsst::afw::fits::setAllowImageCompression
void setAllowImageCompression(bool allow)
Definition: fits.cc:1546
result
py::object result
Definition: _schema.cc:429
bytes
table::Key< table::Array< std::uint8_t > > bytes
Definition: Transform.cc:199
lsst::afw::fits::getAllowImageCompression
bool getAllowImageCompression()
Definition: fits.cc:1548
lsst::afw::fits::scalingAlgorithmToString
std::string scalingAlgorithmToString(ImageScalingOptions::ScalingAlgorithm algorithm)
Provide string version of compression algorithm.
Definition: fitsCompression.cc:109
lsst::afw::fits::compressionAlgorithmToString
std::string compressionAlgorithmToString(ImageCompressionOptions::CompressionAlgorithm algorithm)
Provide string version of compression algorithm.
Definition: fitsCompression.cc:31
declareFits
void declareFits(py::module &mod)
Definition: fits.cc:161
std::memcpy
T memcpy(T... args)
lsst::afw::image::Image
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:58
lsst::utils.tests.init
def init()
Definition: tests.py:59
scaling
table::Key< double > scaling
Definition: PixelAreaBoundedField.cc:133
lsst::afw::fits::makeLimitedFitsHeader
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:457
lsst.pex.config.wrap.validate
validate
Definition: wrap.py:295
lsst::afw::fits::ImageScalingOptions::ScalingAlgorithm
ScalingAlgorithm
Definition: fitsCompression.h:357
lsst.pipe.tasks.mergeDetections.writeMetadata
def writeMetadata(self, dataRefList)
No metadata to write, and not sure how to write it for a list of dataRefs.
Definition: mergeDetections.py:405
std::set
STL class.
lsst::afw::fits::scalingAlgorithmFromString
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
Definition: fitsCompression.cc:99
m
int m
Definition: SpanSet.cc:49