LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | List of all members
lsst::afw::image::MaskFitsReader Class Referencefinal

A FITS reader class for Masks. More...

#include <MaskFitsReader.h>

Inheritance diagram for lsst::afw::image::MaskFitsReader:
lsst::afw::image::ImageBaseFitsReader

Public Member Functions

template<typename PixelT >
Mask< PixelT > read (lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
 Read the Mask. More...
 
 ImageBaseFitsReader (std::string const &fileName, int hdu=fits::DEFAULT_HDU)
 Construct a FITS reader object. More...
 
 ImageBaseFitsReader (fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
 Construct a FITS reader object. More...
 
 ImageBaseFitsReader (fits::Fits *fitsFile)
 Construct a FITS reader object. More...
 
 ImageBaseFitsReader (ImageBaseFitsReader const &)=delete
 
 ImageBaseFitsReader (ImageBaseFitsReader &&)=delete
 
std::string readDType () const
 Read a string describing the pixel type of the on-disk image. More...
 
lsst::geom::Box2I readBBox (ImageOrigin origin=PARENT)
 Read the bounding box of the on-disk image. More...
 
lsst::geom::Point2I readXY0 (lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
 Read the image origin from the on-disk image or a subimage thereof. More...
 
std::shared_ptr< daf::base::PropertyListreadMetadata ()
 Read the image's FITS header. More...
 
template<typename T >
ndarray::Array< T, 2, 2 > readArray (lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT, bool allowUnsafe=false)
 Read the image's data array. More...
 
int getHdu () const noexcept
 Return the HDU this reader targets. More...
 
std::string getFileName () const
 Return the name of the file this reader targets. More...
 

Detailed Description

A FITS reader class for Masks.

Exception Safety
All MaskFitsReader methods provide strong exception safety, but exceptions thrown by the internal fits::Fits object itself may change its status variable or HDU pointer; MaskFitsReader guards against this by resetting those before any use of the Fits object.

Definition at line 39 of file MaskFitsReader.h.

Member Function Documentation

◆ getFileName()

std::string lsst::afw::image::ImageBaseFitsReader::getFileName ( ) const
inlineinherited

Return the name of the file this reader targets.

Definition at line 141 of file ImageBaseFitsReader.h.

141 { return _fitsFile->getFileName(); }
std::string getFileName() const
Return the file name associated with the FITS object or "<unknown>" if there is none.
Definition: fits.cc:498

◆ getHdu()

int lsst::afw::image::ImageBaseFitsReader::getHdu ( ) const
inlinenoexceptinherited

Return the HDU this reader targets.

Definition at line 136 of file ImageBaseFitsReader.h.

136 { return _hdu; }

◆ ImageBaseFitsReader() [1/5]

lsst::afw::image::ImageBaseFitsReader::ImageBaseFitsReader
explicit

Construct a FITS reader object.

Parameters
fitsFilePointer to a CFITSIO file object. Lifetime will not be managed by the Reader object.

Definition at line 74 of file ImageBaseFitsReader.cc.

47  :
48  _ownsFitsFile(false),
49  _hdu(0),
50  _fitsFile(fitsFile)
51 {
52  if (_fitsFile) {
53  if (_fitsFile->getHdu() == 0 && _fitsFile->getImageDim() == 0) {
54  _fitsFile->setHdu(fits::DEFAULT_HDU);
55  }
56  _fitsFile->checkCompressedImagePhu();
57  _hdu = _fitsFile->getHdu();
58  }
59 }
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18

◆ ImageBaseFitsReader() [2/5]

lsst::afw::image::ImageBaseFitsReader::ImageBaseFitsReader
explicit

Construct a FITS reader object.

Parameters
managerMemory block containing a FITS file.
hduHDU index, where 0 is the primary HDU and DEFAULT_HDU is the first non-empty HDU.

Definition at line 66 of file ImageBaseFitsReader.cc.

37  :
38  _ownsFitsFile(true),
39  _hdu(0),
40  _fitsFile(new fits::Fits(manager, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK))
41 {
42  _fitsFile->setHdu(hdu);
43  _fitsFile->checkCompressedImagePhu();
44  _hdu = _fitsFile->getHdu();
45 }

◆ ImageBaseFitsReader() [3/5]

lsst::afw::image::ImageBaseFitsReader::ImageBaseFitsReader
delete

◆ ImageBaseFitsReader() [4/5]

lsst::afw::image::ImageBaseFitsReader::ImageBaseFitsReader
delete

◆ ImageBaseFitsReader() [5/5]

lsst::afw::image::ImageBaseFitsReader::ImageBaseFitsReader
explicit

Construct a FITS reader object.

Parameters
fileNameName of a file to open.
hduHDU index, where 0 is the primary HDU and DEFAULT_HDU is the first non-empty HDU.

Definition at line 57 of file ImageBaseFitsReader.cc.

27  :
28  _ownsFitsFile(true),
29  _hdu(0),
30  _fitsFile(new fits::Fits(fileName, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK))
31 {
32  _fitsFile->setHdu(hdu);
33  _fitsFile->checkCompressedImagePhu();
34  _hdu = _fitsFile->getHdu();
35 }

◆ read()

template<typename PixelT >
template Mask< MaskPixel > lsst::afw::image::MaskFitsReader::read ( lsst::geom::Box2I const &  bbox = lsst::geom::Box2I(),
ImageOrigin  origin = PARENT,
bool  conformMasks = false,
bool  allowUnsafe = false 
)

Read the Mask.

Parameters
bboxA bounding box used to defined a subimage, or an empty box (default) to read the whole image.
originCoordinate system convention for the given box.
conformMasksIf True, conform the global mask dict to match this file.
allowUnsafePermit reading into the requested pixel type even when on-disk values may overflow or truncate.

In Python, this templated method is wrapped with an additional dtype argument to provide the type to read. This defaults to the type of the on-disk image.

Definition at line 28 of file MaskFitsReader.cc.

29  {
30  Mask<PixelT> result(readArray<PixelT>(bbox, origin, allowUnsafe), false, readXY0(bbox, origin));
31  auto metadata = readMetadata();
32  // look for mask planes in the file
35  if (*fileMD == *detail::MaskDict::getDefault()) { // file is already consistent with Mask
36  return result;
37  }
38  if (conformMasks) { // adopt the definitions in the file
40  result._maskDict = fileMD;
41  }
42  result.conformMaskPlanes(fileMaskDict); // convert planes defined by fileMaskDict to the order
43  // defined by Mask::_maskPlaneDict
44  return result;
45 }
py::object result
Definition: _schema.cc:429
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
lsst::geom::Point2I readXY0(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Read the image origin from the on-disk image or a subimage thereof.
std::shared_ptr< daf::base::PropertyList > readMetadata()
Read the image's FITS header.
static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr< lsst::daf::base::PropertySet const > metadata)
Given a PropertySet that contains the MaskPlane assignments, setup the MaskPlanes.
Definition: Mask.cc:597
static std::shared_ptr< MaskDict > getDefault()
Definition: MaskDict.cc:115
static void setDefault(std::shared_ptr< MaskDict > dict)
Definition: MaskDict.cc:119
static std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &dict)
Definition: MaskDict.cc:111
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:58

◆ readArray()

template<typename T >
template ndarray::Array< std::uint64_t, 2, 2 > lsst::afw::image::ImageBaseFitsReader::readArray ( lsst::geom::Box2I const &  bbox,
ImageOrigin  origin = PARENT,
bool  allowUnsafe = false 
)
inherited

Read the image's data array.

Parameters
bboxA bounding box used to defined a subimage, or an empty box (default) to read the whole image.
originCoordinate system convention for the given box.
allowUnsafePermit reading into the requested pixel type even when on-disk values may overflow or truncate.

Definition at line 171 of file ImageBaseFitsReader.cc.

172  {
173  checkFitsFile(_fitsFile);
174  auto fullBBox = readBBox(origin);
175  auto subBBox = bbox;
176  if (subBBox.isEmpty()) {
177  subBBox = fullBBox;
178  } else if (!fullBBox.contains(subBBox)) {
179  throw LSST_EXCEPT(
180  pex::exceptions::LengthError,
181  str(boost::format("Subimage box (%d,%d) %dx%d doesn't fit in image (%d,%d) %dx%d in HDU %d") %
182  subBBox.getMinX() % subBBox.getMinY() % subBBox.getWidth() % subBBox.getHeight() %
183  fullBBox.getMinX() % fullBBox.getMinY() % fullBBox.getWidth() % fullBBox.getHeight() % _hdu)
184  );
185  }
186  fits::HduMoveGuard guard(*_fitsFile, _hdu);
187  if (!allowUnsafe & !_fitsFile->checkImageType<T>()) {
188  throw LSST_FITS_EXCEPT(
189  fits::FitsTypeError, *_fitsFile,
190  str(boost::format("Incompatible type for FITS image: on disk is %s (HDU %d), in-memory is %s. "
191  "Read with allowUnsafe=True to permit conversions that may overflow.") %
192  _fitsFile->getImageDType() % _hdu % makeDTypeString<T>())
193  );
194  }
195  ndarray::Array<T, 2, 2> result = ndarray::allocate(subBBox.getHeight(), subBBox.getWidth());
196  ndarray::Vector<int, 2> offset = ndarray::makeVector(subBBox.getMinY() - fullBBox.getMinY(),
197  subBBox.getMinX() - fullBBox.getMinX());
198  _fitsFile->readImage(result, offset);
199  return result;
200 }
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
void readImage(ndarray::Array< T, N, N > const &array, ndarray::Vector< int, N > const &offset)
Read an array from a FITS image.
Definition: fits.h:541
std::string getImageDType()
Return the numpy dtype equivalent of the image pixel type (e.g.
Definition: fits.cc:1466
bool checkImageType()
Return true if the current HDU is compatible with the given pixel type.
Definition: fits.cc:1435
lsst::geom::Box2I readBBox(ImageOrigin origin=PARENT)
Read the bounding box of the on-disk image.
#define LSST_FITS_EXCEPT(type, fitsObj,...)
A FITS-related replacement for LSST_EXCEPT that takes an additional Fits object and uses makeErrorMes...
Definition: fits.h:105
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174

◆ readBBox()

lsst::geom::Box2I lsst::afw::image::ImageBaseFitsReader::readBBox ( ImageOrigin  origin = PARENT)
inherited

Read the bounding box of the on-disk image.

Parameters
originCoordinate system convention for the returned box. If LOCAL, the returned box will always have a minimum of (0, 0).

Definition at line 86 of file ImageBaseFitsReader.cc.

86  {
87  readMetadata(); // guarantees _bbox is initialized
88  if (origin == LOCAL) {
90  }
91  return _bbox;
92 }
An integer coordinate rectangle.
Definition: Box.h:55
Extent2I const getDimensions() const noexcept
Definition: Box.h:186

◆ readDType()

std::string lsst::afw::image::ImageBaseFitsReader::readDType ( ) const
inherited

Read a string describing the pixel type of the on-disk image.

Returns
A string of the form [u](int|float)<bits> (e.g. "uint16", "float64").

Definition at line 80 of file ImageBaseFitsReader.cc.

80  {
81  checkFitsFile(_fitsFile);
82  fits::HduMoveGuard guard(*_fitsFile, _hdu);
83  return _fitsFile->getImageDType();
84 }

◆ readMetadata()

std::shared_ptr< daf::base::PropertyList > lsst::afw::image::ImageBaseFitsReader::readMetadata ( )
inherited

Read the image's FITS header.

Definition at line 105 of file ImageBaseFitsReader.cc.

105  {
106  checkFitsFile(_fitsFile);
107  if (_metadata == nullptr) {
108  fits::HduMoveGuard guard(*_fitsFile, _hdu);
109  auto metadata = fits::readMetadata(*_fitsFile, /*strip=*/true);
110  // One-use lambda avoids declare-then-initialize; see C++ Core Guidelines, ES28.
111  auto computeShape = [this](){
112  int nAxis = _fitsFile->getImageDim();
113  if (nAxis == 2) {
114  return _fitsFile->getImageShape<2>();
115  }
116  if (nAxis == 3) {
117  ndarray::Vector<ndarray::Size, 3> shape3 = _fitsFile->getImageShape<3>();
118  if (shape3[0] != 1) {
119  throw LSST_EXCEPT(
120  fits::FitsError,
121  str(boost::format("Error reading %s: HDU %d has 3rd dimension %d != 1") %
122  getFileName() % _hdu % shape3[0]));
123  }
124  return shape3.last<2>();
125  }
126  throw LSST_EXCEPT(
127  fits::FitsError,
128  str(boost::format("Error reading %s: HDU %d has %d dimensions") %
129  getFileName() % _hdu % nAxis)
130  );
131  };
132  // Construct _bbox and check dimensions at the same time both to make
133  // one less valid state for this class to be in and to make sure
134  // the appropriate metadata is stripped before we ever return it.
135  ndarray::Vector<ndarray::Size, 2> shape = computeShape();
136  auto xy0 = afw::geom::getImageXY0FromMetadata(*metadata, detail::wcsNameForXY0, /*strip=*/true);
137  _bbox = lsst::geom::Box2I(xy0, lsst::geom::Extent2I(shape[1], shape[0]));
138  _metadata = std::move(metadata);
139  }
140  return _metadata;
141 }
int getImageDim()
Return the number of dimensions in the current HDU.
Definition: fits.cc:1422
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:512
std::string getFileName() const
Return the name of the file this reader targets.
T move(T... args)
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
lsst::geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
Definition: wcsUtils.cc:95
std::string const wcsNameForXY0
Definition: ImageBase.h:70

◆ readXY0()

lsst::geom::Point2I lsst::afw::image::ImageBaseFitsReader::readXY0 ( lsst::geom::Box2I const &  bbox = lsst::geom::Box2I(),
ImageOrigin  origin = PARENT 
)
inherited

Read the image origin from the on-disk image or a subimage thereof.

Parameters
bboxA bounding box used to defined a subimage, or an empty box (default) to use the whole image.
originCoordinate system convention for the given box. Ignored if bbox is empty.

Definition at line 94 of file ImageBaseFitsReader.cc.

94  {
95  if (bbox.isEmpty()) {
96  return readBBox().getMin();
97  } else if (origin == PARENT) {
98  return bbox.getMin();
99  } else {
100  auto full = readBBox();
101  return full.getMin() + lsst::geom::Extent2I(bbox.getMin());
102  }
103 }
Point2I const getMin() const noexcept
Definition: Box.h:156
Extent< int, 2 > Extent2I
Definition: Extent.h:397

The documentation for this class was generated from the following files: