Skip to content

Commit

Permalink
cleanup FITS code
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Wittgen committed Jul 29, 2022
1 parent c751973 commit b342c12
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 47 deletions.
4 changes: 2 additions & 2 deletions include/lsst/afw/fits.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HeaderIterationFunctor {
public:
virtual void operator()(std::string const& key, std::string const& value, std::string const& comment) = 0;

virtual ~HeaderIterationFunctor() {}
virtual ~HeaderIterationFunctor() = default;
};

/**
Expand Down Expand Up @@ -312,7 +312,7 @@ class Fits {
std::string getFileName() const;

/// Return the current HDU (0-indexed; 0 is the Primary HDU).
int getHdu();
int getHdu() const;

/**
* Set the current HDU.
Expand Down
75 changes: 38 additions & 37 deletions src/fits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::string makeLimitedFitsHeaderImpl(std::vector<std::string> const &paramNames
auto name = (lastPeriod == std::string::npos) ? fullName : fullName.substr(lastPeriod + 1);
std::type_info const &type = metadata.typeOf(name);

std::string out = "";
std::string out;
out.reserve(80);
if (name.size() > 8) {
continue; // The name is too long for a FITS keyword; skip this item
Expand All @@ -67,7 +67,7 @@ std::string makeLimitedFitsHeaderImpl(std::vector<std::string> const &paramNames
} else if (type == typeid(int)) {
out += (boost::format("%20d") % metadata.get<int>(name)).str();
} else if (type == typeid(double)) {
double value = metadata.get<double>(name);
auto value = metadata.get<double>(name);
if (!std::isnan(value)) {
// use G because FITS wants uppercase E for exponents
out += (boost::format("%#20.17G") % value).str();
Expand All @@ -79,7 +79,7 @@ std::string makeLimitedFitsHeaderImpl(std::vector<std::string> const &paramNames
out += " ";
}
} else if (type == typeid(float)) {
float value = metadata.get<float>(name);
auto value = metadata.get<float>(name);
if (!std::isnan(value)) {
out += (boost::format("%#20.15G") % value).str();
} else {
Expand Down Expand Up @@ -163,7 +163,7 @@ class StringStartSet {
/// If we write any of these keys ourselves, it may corrupt the FITS file.
/// Also, the user has no business reading them, since the use of FITS is
/// an implementation detail that should be opaque to the user.
static std::unordered_set<std::string> const ignoreKeys = {
std::unordered_set<std::string> const ignoreKeys = {
// FITS core keywords
"SIMPLE", "BITPIX", "NAXIS", "EXTEND", "GCOUNT", "PCOUNT", "XTENSION", "TFIELDS", "BSCALE", "BZERO",
// FITS compression keywords
Expand Down Expand Up @@ -364,7 +364,7 @@ bool isFitsImageTypeSigned(int constant) {
throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Invalid constant.");
}

static bool allowImageCompression = true;
bool allowImageCompression = true;

int fitsTypeForBitpix(int bitpix) {
switch (bitpix) {
Expand Down Expand Up @@ -425,32 +425,33 @@ ItemInfo isCommentIsValid(daf::base::PropertyList const &pl, std::string const &
std::string makeErrorMessage(std::string const &fileName, int status, std::string const &msg) {
std::ostringstream os;
os << "cfitsio error";
if (fileName != "") {
if (!fileName.empty()) {
os << " (" << fileName << ")";
}
if (status != 0) {
char fitsErrMsg[FLEN_ERRMSG];
fits_get_errstatus(status, fitsErrMsg);
os << ": " << fitsErrMsg << " (" << status << ")";
}
if (msg != "") {
if (!msg.empty()) {
os << " : " << msg;
}
os << "\ncfitsio error stack:\n";
char cfitsioMsg[FLEN_ERRMSG];
// fits_read_errmsg can return a junk string with control characters
while (fits_read_errmsg(cfitsioMsg) != 0) {
cfitsioMsg[FLEN_ERRMSG-1] = char(0); // ensure termination
std::size_t len=strlen(cfitsioMsg);
if(len-1 >= FLEN_ERRMSG) len = FLEN_ERRMSG - 1;
for(int i=0;i<len;i++)
if(cfitsioMsg[i] < char(32)) cfitsioMsg[i] = ' ';
for(std::size_t i=0;i<len;i++)
if(cfitsioMsg[i] < char(32)) cfitsioMsg[i] = '.';
os << " " << cfitsioMsg << "\n";
}
return os.str();
}

std::string makeErrorMessage(void *fptr, int status, std::string const &msg) {
std::string fileName = "";
fitsfile *fd = reinterpret_cast<fitsfile *>(fptr);
std::string fileName;
auto *fd = reinterpret_cast<fitsfile *>(fptr);
if (fd != nullptr && fd->Fptr != nullptr && fd->Fptr->filename != nullptr) {
fileName = fd->Fptr->filename;
}
Expand All @@ -459,7 +460,7 @@ std::string makeErrorMessage(void *fptr, int status, std::string const &msg) {

std::string makeLimitedFitsHeader(daf::base::PropertySet const &metadata,
std::set<std::string> const &excludeNames) {
daf::base::PropertyList const *pl = dynamic_cast<daf::base::PropertyList const *>(&metadata);
auto const *pl = dynamic_cast<daf::base::PropertyList const *>(&metadata);
std::vector<std::string> allParamNames;
if (pl) {
allParamNames = pl->getOrderedNames();
Expand Down Expand Up @@ -500,14 +501,14 @@ int getBitPix() {

std::string Fits::getFileName() const {
std::string fileName = "<unknown>";
fitsfile *fd = reinterpret_cast<fitsfile *>(fptr);
auto *fd = reinterpret_cast<fitsfile *>(fptr);
if (fd != nullptr && fd->Fptr != nullptr && fd->Fptr->filename != nullptr) {
fileName = fd->Fptr->filename;
}
return fileName;
}

int Fits::getHdu() {
int Fits::getHdu() const {
int n = 1;
fits_get_hdu_num(reinterpret_cast<fitsfile *>(fptr), &n);
return n - 1;
Expand Down Expand Up @@ -797,7 +798,7 @@ void Fits::forEachKey(HeaderIterationFunctor &functor) {
// We uppercase to try to be more consistent.
std::string upperKey(key);
boost::to_upper(upperKey);
if (upperKey.compare(key) != 0){
if (upperKey != key){
LOGLS_DEBUG("lsst.afw.fits",
boost::format("In %s, standardizing key '%s' to uppercase '%s' on read.") %
BOOST_CURRENT_FUNCTION % key % upperKey);
Expand Down Expand Up @@ -884,7 +885,7 @@ class MetadataIterationFunctor : public HeaderIterationFunctor {
}
}

void add(std::string const &key, std::string const &comment) {
void add(std::string const &key, std::string const &comment) const {
// If this undefined value is adding to a pre-existing key that has
// a defined value we must skip the add so as not to break
// PropertyList/Set.
Expand All @@ -911,9 +912,9 @@ class MetadataIterationFunctor : public HeaderIterationFunctor {
}
}

bool strip;
daf::base::PropertySet *set;
daf::base::PropertyList *list;
bool strip{};
daf::base::PropertySet *set{};
daf::base::PropertyList *list{};
};

void MetadataIterationFunctor::operator()(std::string const &key, std::string const &value,
Expand Down Expand Up @@ -994,71 +995,71 @@ void writeKeyFromProperty(Fits &fits, daf::base::PropertySet const &metadata, st
if (metadata.isArray(key)) {
std::vector<bool> tmp = metadata.getArray<bool>(key);
// work around unfortunate specialness of std::vector<bool>
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), static_cast<bool>(tmp[i]), comment);
for (auto && i : tmp) {
writeKeyImpl(fits, key.c_str(), static_cast<bool>(i), comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<bool>(key), comment);
}
} else if (valueType == typeid(std::uint8_t)) {
if (metadata.isArray(key)) {
std::vector<std::uint8_t> tmp = metadata.getArray<std::uint8_t>(key);
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), tmp[i], comment);
for (unsigned char & i : tmp) {
writeKeyImpl(fits, key.c_str(), i, comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<std::uint8_t>(key), comment);
}
} else if (valueType == typeid(int)) {
if (metadata.isArray(key)) {
std::vector<int> tmp = metadata.getArray<int>(key);
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), tmp[i], comment);
for (int & i : tmp) {
writeKeyImpl(fits, key.c_str(), i, comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<int>(key), comment);
}
} else if (valueType == typeid(long)) {
if (metadata.isArray(key)) {
std::vector<long> tmp = metadata.getArray<long>(key);
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), tmp[i], comment);
for (long & i : tmp) {
writeKeyImpl(fits, key.c_str(), i, comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<long>(key), comment);
}
} else if (valueType == typeid(long long)) {
if (metadata.isArray(key)) {
std::vector<long long> tmp = metadata.getArray<long long>(key);
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), tmp[i], comment);
for (long long & i : tmp) {
writeKeyImpl(fits, key.c_str(), i, comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<long long>(key), comment);
}
} else if (valueType == typeid(std::int64_t)) {
if (metadata.isArray(key)) {
std::vector<std::int64_t> tmp = metadata.getArray<std::int64_t>(key);
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), tmp[i], comment);
for (long & i : tmp) {
writeKeyImpl(fits, key.c_str(), i, comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<std::int64_t>(key), comment);
}
} else if (valueType == typeid(double)) {
if (metadata.isArray(key)) {
std::vector<double> tmp = metadata.getArray<double>(key);
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), tmp[i], comment);
for (double & i : tmp) {
writeKeyImpl(fits, key.c_str(), i, comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<double>(key), comment);
}
} else if (valueType == typeid(std::string)) {
if (metadata.isArray(key)) {
std::vector<std::string> tmp = metadata.getArray<std::string>(key);
for (std::size_t i = 0; i != tmp.size(); ++i) {
writeKeyImpl(fits, key.c_str(), tmp[i], comment);
for (auto & i : tmp) {
writeKeyImpl(fits, key.c_str(), i, comment);
}
} else {
writeKeyImpl(fits, key.c_str(), metadata.get<std::string>(key), comment);
Expand Down Expand Up @@ -1097,7 +1098,7 @@ void Fits::readMetadata(daf::base::PropertySet &metadata, bool strip) {

void Fits::writeMetadata(daf::base::PropertySet const &metadata) {
using NameList = std::vector<std::string>;
daf::base::PropertyList const *pl = dynamic_cast<daf::base::PropertyList const *>(&metadata);
auto const *pl = dynamic_cast<daf::base::PropertyList const *>(&metadata);
NameList paramNames;
if (pl) {
paramNames = pl->getOrderedNames();
Expand Down
9 changes: 1 addition & 8 deletions src/image/ExposureFitsReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,7 @@ Exposure<ImagePixelT, MaskPixelT, VariancePixelT> ExposureFitsReader::read(lsst:
bool allowUnsafe) {
auto mi =
readMaskedImage<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, allowUnsafe);
std::shared_ptr<ExposureInfo> exposureInfo;
try {
exposureInfo = readExposureInfo();
} catch(fits::FitsError const &e) {
throw LSST_EXCEPT(pexExcept::IoError,
boost::str(boost::format("Invalid or corrupt ExposureInfo in FITS file")));
}
return Exposure<ImagePixelT, MaskPixelT, VariancePixelT>(mi, std::move(exposureInfo));
return Exposure<ImagePixelT, MaskPixelT, VariancePixelT>(mi, readExposureInfo());
}

void ExposureFitsReader::_ensureReaders() {
Expand Down

0 comments on commit b342c12

Please sign in to comment.