44LOG_LOGGER _log =
LOG_GET(
"lsst.afw.image.fits.ExposureFitsReader");
46template <
typename T, std::
size_t N>
47bool _contains(std::array<T, N>
const& array, T
const& value) {
48 for (T
const& element : array) {
49 if (element == value) {
57std::map<std::string, FilterLabel>
const _AFW_NAMES = {
68bool _isBand(std::string
const& name) {
69 static std::set<std::string>
const BANDS = {
"u",
"g",
"r",
"i",
"z",
"y",
"SH",
"PH",
"VR",
"white"};
71 if (BANDS.
count(name) > 0) {
98 if (name == DEFAULT) {
118 if (_AFW_NAMES.
count(name) > 0) {
132 if (primaryMetadata->exists(versionName)) {
133 version = primaryMetadata->getAsInt(versionName);
134 primaryMetadata->remove(versionName);
140 str(boost::format(
"Cannot read Exposure FITS version > %i") %
148 LOGLS_DEBUG(_log,
"No WCS found in FITS metadata");
155 imageMetadata->remove(
"LTV1");
156 imageMetadata->remove(
"LTV2");
158 if (!imageMetadata->exists(
"INHERIT")) {
232 auto popInt = [&metadata](
std::string const& name) {
237 if (metadata.
exists(name)) {
238 r = metadata.
get<
int>(name);
246 _hdu = popInt(
"AR_HDU");
248 _state = ArchiveState::MISSING;
251 _state = ArchiveState::PRESENT;
254 _ids[
PSF] = popInt(
"PSF_ID");
255 _ids[
WCS] = popInt(
"SKYWCS_ID");
260 _ids[
DETECTOR] = popInt(
"DETECTOR_ID");
267 if (headerKey.substr(0, PREFIX.
size()) == PREFIX) {
269 int archiveId = metadata.
get<
int>(headerKey);
270 _genericIds.emplace(componentName, archiveId);
271 if (!_contains(_ids, archiveId)) {
272 _extraIds.emplace(componentName);
275 toStrip.
push_back(componentName +
"_ID");
292 template <
typename T>
294 if (!_ensureLoaded(fitsFile)) {
297 return _archive.get<T>(_ids[c]);
319 template <
typename T>
321 if (!_ensureLoaded(fitsFile)) {
325 if (_genericIds.count(c) > 0) {
326 int archiveId = _genericIds.at(c);
327 return _archive.get<T>(archiveId);
346 if (!_ensureLoaded(fitsFile)) {
352 for (
std::string const& componentName : _extraIds) {
357 "Could not read component " << componentName <<
"; skipping: " << err.
what());
365 if (_state == ArchiveState::MISSING) {
368 if (_state == ArchiveState::PRESENT) {
371 _state = ArchiveState::LOADED;
373 assert(_state == ArchiveState::LOADED);
377 enum class ArchiveState { UNKNOWN, MISSING, PRESENT, LOADED };
380 ArchiveState _state = ArchiveState::UNKNOWN;
381 table::io::InputArchive _archive;
383 std::map<std::string, int> _genericIds;
384 std::set<std::string> _extraIds;
396 return _maskedImageReader.readBBox(origin);
400 return _maskedImageReader.readXY0(bbox, origin);
405 return _metadataReader->version;
416 return _metadataReader->metadata;
421 return _metadataReader->exposureId;
428 r = _metadataReader->wcs;
435 if (_metadataReader->version < 2) {
436 return _metadataReader->filterLabel;
444 if (_metadataReader->version == 0) {
445 return _metadataReader->photoCalib;
474 return _metadataReader->visitInfo;
495 return _archiveReader->readExtraComponents(_getFitsFile());
506 result->setId(*exposureId);
515 LOGLS_WARN(_log,
"Could not read PSF; setting to null: " << err.
what());
520 LOGLS_WARN(_log,
"Could not read CoaddInputs; setting to null: " << err.
what());
525 LOGLS_WARN(_log,
"Could not read ApCorrMap; setting to null: " << err.
what());
530 LOGLS_WARN(_log,
"Could not read ValidPolygon; setting to null: " << err.
what());
535 LOGLS_WARN(_log,
"Could not read TransmissionCurve; setting to null: " << err.
what());
540 LOGLS_WARN(_log,
"Could not read Detector; setting to null: " << err.
what());
545 result->setWcs(_metadataReader->wcs);
554 auto msg = str(boost::format(
"Could not read WCS extension; setting to null: %s") % err.
what());
555 if (result->hasWcs()) {
556 msg +=
" ; using WCS from FITS header";
565 if (
object.use_count() > 0) {
568 LOGLS_WARN(_log,
"Data corruption: generic component " << key <<
" is not a Storable; skipping.");
573 if (_metadataReader->version < 2 && !result->hasFilter()) {
579template <
typename ImagePixelT>
582 return _maskedImageReader.readImage<ImagePixelT>(bbox, origin, allowUnsafe);
585template <
typename ImagePixelT>
588 return _maskedImageReader.readImageArray<ImagePixelT>(bbox, origin, allowUnsafe);
591template <
typename MaskPixelT>
593 bool conformMasks,
bool allowUnsafe) {
594 return _maskedImageReader.readMask<MaskPixelT>(bbox, origin, conformMasks, allowUnsafe);
597template <
typename MaskPixelT>
600 return _maskedImageReader.readMaskArray<MaskPixelT>(bbox, origin, allowUnsafe);
603template <
typename VariancePixelT>
606 return _maskedImageReader.readVariance<VariancePixelT>(bbox, origin, allowUnsafe);
609template <
typename VariancePixelT>
613 return _maskedImageReader.readVarianceArray<VariancePixelT>(bbox, origin, allowUnsafe);
616template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
619 return _maskedImageReader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks,
624template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
634void ExposureFitsReader::_ensureReaders() {
635 if (!_metadataReader) {
636 auto metadataReader = std::make_unique<MetadataReader>(_maskedImageReader.
readPrimaryMetadata(),
639 _archiveReader = std::make_unique<ArchiveReader>(*metadataReader->metadata);
640 _metadataReader =
std::move(metadataReader);
642 assert(_archiveReader);
645#define INSTANTIATE(ImagePixelT) \
646 template Exposure<ImagePixelT, MaskPixel, VariancePixel> ExposureFitsReader::read( \
647 lsst::geom::Box2I const&, ImageOrigin, bool, bool); \
648 template Image<ImagePixelT> ExposureFitsReader::readImage(lsst::geom::Box2I const&, ImageOrigin, bool); \
649 template ndarray::Array<ImagePixelT, 2, 2> ExposureFitsReader::readImageArray(lsst::geom::Box2I const&, \
650 ImageOrigin, bool); \
651 template MaskedImage<ImagePixelT, MaskPixel, VariancePixel> ExposureFitsReader::readMaskedImage( \
652 lsst::geom::Box2I const&, ImageOrigin, bool, bool)
#define INSTANTIATE(FROMSYS, TOSYS)
#define LSST_EXCEPT(type,...)
#define LOGLS_WARN(logger, message)
#define LOGLS_DEBUG(logger, message)
Implementation of the Photometric Calibration class.
A representation of a detector in a mosaic camera.
A polymorphic base class for representing an image's Point Spread Function.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
RAII scoped guard for moving the HDU in a Fits object.
Lifetime-management for memory that goes into FITS memory files.
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
A thin wrapper around std::map to allow aperture corrections to be attached to Exposures.
std::shared_ptr< T > readComponent(afw::fits::Fits *fitsFile, std::string c)
Read an arbitrary component, if available.
std::map< std::string, std::shared_ptr< table::io::Persistable > > readExtraComponents(afw::fits::Fits *fitsFile)
Read the components that are stored using arbitrary-component support.
ArchiveReader(daf::base::PropertyList &metadata)
std::shared_ptr< T > readComponent(afw::fits::Fits *fitsFile, Component c)
Read a known component, if available.
std::shared_ptr< TransmissionCurve > readTransmissionCurve()
Read the Exposure's transmission curve.
std::string readMaskDType() const
Read a string describing the pixel type of the on-disk image plane.
Exposure< ImagePixelT, MaskPixelT, VariancePixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Read the full Exposure.
std::shared_ptr< typehandling::Storable > readComponent(std::string const &componentName)
Read an arbitrary non-standard component by name.
std::shared_ptr< afw::geom::SkyWcs > readWcs()
Read the Exposure's world coordinate system.
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > readMaskedImage(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Read the MaskedImage.
std::map< std::string, std::shared_ptr< table::io::Persistable > > readExtraComponents()
Read the Exposure's non-standard components.
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.
ndarray::Array< VariancePixelT, 2, 2 > readVarianceArray(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the variance plane.
int readSerializationVersion()
Read the serialization version number from the header.
ExposureFitsReader(std::string const &fileName)
Construct a FITS reader object.
std::shared_ptr< detection::Psf > readPsf()
Read the Exposure's point-spread function.
std::shared_ptr< afw::geom::polygon::Polygon > readValidPolygon()
Read the polygon describing the region of validity for the Exposure.
Image< ImagePixelT > readImage(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the image plane.
std::string readVarianceDType() const
Read a string describing the pixel type of the on-disk image plane.
std::shared_ptr< FilterLabel > readFilter()
Read the Exposure's filter information.
ndarray::Array< ImagePixelT, 2, 2 > readImageArray(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the image plane.
lsst::geom::Box2I readBBox(ImageOrigin origin=PARENT)
Read the bounding box of the on-disk image.
Image< VariancePixelT > readVariance(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the variance plane.
std::shared_ptr< daf::base::PropertyList > readMetadata()
Read the flexible metadata associated with the Exposure.
std::shared_ptr< PhotoCalib > readPhotoCalib()
Read the Exposure's photometric calibration.
std::optional< table::RecordId > readExposureId()
Read the Exposure's exposure ID, if it exists.
std::shared_ptr< ApCorrMap > readApCorrMap()
Read the Exposure's aperture correction map.
~ExposureFitsReader() noexcept
ndarray::Array< MaskPixelT, 2, 2 > readMaskArray(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the mask plane.
std::string readImageDType() const
Read a string describing the pixel type of the on-disk image plane.
std::shared_ptr< CoaddInputs > readCoaddInputs()
Read the Exposure's coadd input catalogs.
std::shared_ptr< cameraGeom::Detector > readDetector()
Read the Exposure's detector.
std::shared_ptr< ExposureInfo > readExposureInfo()
Read the ExposureInfo containing all non-image components.
std::shared_ptr< VisitInfo > readVisitInfo()
Read the Exposure's visit metadata.
Mask< MaskPixelT > readMask(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Read the mask plane.
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Exposure(unsigned int width, unsigned int height, std::shared_ptr< geom::SkyWcs const > wcs=std::shared_ptr< geom::SkyWcs const >())
Construct an Exposure with a blank MaskedImage of specified size (default 0x0) and a SkyWcs (which ma...
static int getFitsSerializationVersion()
Get the version of FITS serialization that this ExposureInfo understands.
static std::string const & getFitsSerializationVersionName()
Get the version of FITS serialization version info name.
static typehandling::Key< std::string, std::shared_ptr< FilterLabel const > > const KEY_FILTER
Standard key for looking up filter information.
A group of labels for a filter in an exposure or coadd.
static FilterLabel fromBand(std::string const &band)
Construct a FilterLabel from specific inputs.
static FilterLabel fromBandPhysical(std::string const &band, std::string const &physical)
Construct a FilterLabel from specific inputs.
static FilterLabel fromPhysical(std::string const &physical)
Construct a FilterLabel from specific inputs.
A class to represent a 2-dimensional array of pixels.
Represent a 2-dimensional array of bitmask pixels.
std::shared_ptr< daf::base::PropertyList > readImageMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readPrimaryMetadata()
Read the FITS header of one of the HDUs.
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.
A class to manipulate images, masks, and variance as a single object.
The photometric calibration of an exposure.
A spatially-varying transmission curve as a function of wavelength.
Interface supporting iteration over heterogenous containers.
T get(std::string const &name) const
virtual void remove(std::string const &name)
bool exists(std::string const &name) const
CoordinateExpr< N > ne(Point< T, N > const &other) const noexcept
virtual char const * what(void) const noexcept
T find_last_not_of(T... args)
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
int stripVisitInfoKeywords(daf::base::PropertySet &metadata)
Remove VisitInfo-related keywords from the metadata.
std::shared_ptr< PhotoCalib > makePhotoCalibFromMetadata(daf::base::PropertySet &metadata, bool strip=false)
Construct a PhotoCalib from FITS FLUXMAG0/FLUXMAG0ERR keywords.
std::shared_ptr< FilterLabel > makeFilterLabel(std::string const &name)
Convert an old-style single Filter name to a FilterLabel, using available information.
std::shared_ptr< FilterLabel > makeFilterLabelDirect(std::string const &name)
Convert an old-style filter name to a FilterLabel without external information.
constexpr Key< K, V > makeKey(K const &id)
Factory function for Key, to enable type parameter inference.
Extent< double, 2 > Extent2D
T dynamic_pointer_cast(T... args)