11 #include "boost/regex.hpp" 12 #include "boost/multi_index_container.hpp" 13 #include "boost/multi_index/sequenced_index.hpp" 14 #include "boost/multi_index/ordered_index.hpp" 15 #include "boost/multi_index/hashed_index.hpp" 16 #include "boost/multi_index/member.hpp" 35 template <std::
string FitsSchemaItem::*Member>
36 struct SetFitsSchemaString {
37 void operator()(FitsSchemaItem &item) { item.*Member = _v; }
38 explicit SetFitsSchemaString(
std::string const &v) : _v(v) {}
52 typedef boost::multi_index_container<
54 boost::multi_index::indexed_by<
55 boost::multi_index::ordered_non_unique<
56 boost::multi_index::member<FitsSchemaItem, int, &FitsSchemaItem::column>>,
57 boost::multi_index::ordered_non_unique<
58 boost::multi_index::member<FitsSchemaItem, int, &FitsSchemaItem::bit>>,
59 boost::multi_index::hashed_unique<
60 boost::multi_index::member<FitsSchemaItem, std::string, &FitsSchemaItem::ttype>>,
61 boost::multi_index::sequenced<>>>
65 typedef SetFitsSchemaString<&FitsSchemaItem::ttype>
SetTTYPE;
66 typedef SetFitsSchemaString<&FitsSchemaItem::tform>
SetTFORM;
67 typedef SetFitsSchemaString<&FitsSchemaItem::tccls>
SetTCCLS;
68 typedef SetFitsSchemaString<&FitsSchemaItem::tunit>
SetTUNIT;
69 typedef SetFitsSchemaString<&FitsSchemaItem::doc>
SetDoc;
78 ByColumn &
byColumn() {
return inputs.get<0>(); }
79 ByBit &
byBit() {
return inputs.get<1>(); }
80 ByName &
byName() {
return inputs.get<2>(); }
81 AsList &
asList() {
return inputs.get<3>(); }
98 : _impl(
std::make_shared<
Impl>()) {
101 if (!metadata.
exists(
"AFW_TYPE")) {
104 _impl->version = metadata.
get(
"AFW_TABLE_VERSION", _impl->version);
105 _impl->type = metadata.
get(
"AFW_TYPE", _impl->type);
107 metadata.
remove(
"AFW_TABLE_VERSION");
110 metadata.
remove(
"AFW_TYPE");
114 _impl->archiveHdu = metadata.
get(
"AR_HDU", -1);
115 if (_impl->archiveHdu > 0) {
118 metadata.
remove(
"AR_HDU");
127 if (pos == std::string::npos) {
129 (boost::format(
"Malformed alias definition: '%s'") % (*i)).str());
131 _impl->schema.getAliasMap()->set(i->substr(0, pos), i->substr(pos + 1, std::string::npos));
140 if (_impl->version == 0) {
151 if (!target.
empty()) {
152 _impl->schema.getAliasMap()->set(oldSlotKeys[i].
second, target);
155 metadata.
remove(oldSlotKeys[i].first +
"_ERR_SLOT");
156 metadata.
remove(oldSlotKeys[i].first +
"_FLAG_SLOT");
165 if (
key->compare(0, 5,
"TTYPE") == 0) {
167 auto iter = _impl->byColumn().lower_bound(column);
168 if (iter == _impl->byColumn().end() || iter->column != column) {
169 iter = _impl->byColumn().insert(iter,
FitsSchemaItem(column, -1));
173 if (iter->doc.empty()) {
179 }
else if (
key->compare(0, 5,
"TFLAG") == 0) {
181 auto iter = _impl->byBit().lower_bound(bit);
182 if (iter == _impl->byBit().end() || iter->bit != bit) {
187 if (iter->doc.empty()) {
193 }
else if (
key->compare(0, 4,
"TDOC") == 0) {
195 auto iter = _impl->byColumn().lower_bound(column);
196 if (iter == _impl->byColumn().end() || iter->column != column) {
197 iter = _impl->byColumn().insert(iter,
FitsSchemaItem(column, -1));
203 }
else if (
key->compare(0, 5,
"TFDOC") == 0) {
205 auto iter = _impl->byBit().lower_bound(bit);
206 if (iter == _impl->byBit().end() || iter->bit != bit) {
213 }
else if (
key->compare(0, 5,
"TUNIT") == 0) {
215 auto iter = _impl->byColumn().lower_bound(column);
216 if (iter == _impl->byColumn().end() || iter->column != column) {
217 iter = _impl->byColumn().insert(iter,
FitsSchemaItem(column, -1));
223 }
else if (
key->compare(0, 5,
"TCCLS") == 0) {
225 auto iter = _impl->byColumn().lower_bound(column);
226 if (iter == _impl->byColumn().end() || iter->column != column) {
227 iter = _impl->byColumn().insert(iter,
FitsSchemaItem(column, -1));
233 }
else if (
key->compare(0, 5,
"TFORM") == 0) {
235 auto iter = _impl->byColumn().lower_bound(column);
236 if (iter == _impl->byColumn().end() || iter->column != column) {
237 iter = _impl->byColumn().insert(iter,
FitsSchemaItem(column, -1));
243 }
else if (
key->compare(0, 5,
"TZERO") == 0) {
247 }
else if (
key->compare(0, 5,
"TSCAL") == 0) {
251 }
else if (
key->compare(0, 5,
"TNULL") == 0) {
255 }
else if (
key->compare(0, 5,
"TDISP") == 0) {
263 _impl->flagColumn = metadata.
get(
"FLAGCOL", 0);
264 if (_impl->flagColumn > 0) {
266 metadata.
remove(
"FLAGCOL");
269 auto iter = _impl->byColumn().find(_impl->flagColumn);
270 if (iter == _impl->byColumn().end()) {
273 (boost::format(
"Column for flag data not found; FLAGCOL=%d") % _impl->flagColumn).str());
278 static boost::regex
const regex(
"(\\d+)?X\\(?(\\d)*\\)?", boost::regex::perl);
280 if (!boost::regex_match(iter->tform, m, regex)) {
283 (boost::format(
"Invalid TFORM key for flags column: '%s'") % iter->tform).str());
289 _impl->flagKeys.resize(nFlags);
290 _impl->flagWorkspace.reset(
new bool[nFlags]);
293 _impl->byColumn().erase(iter);
306 int oldHdu = fits.
getHdu();
307 if (_impl->archiveHdu < 0) _impl->archiveHdu = oldHdu + 1;
309 fits.
setHdu(_impl->archiveHdu);
316 _impl->archiveHdu = -1;
324 auto iter = _impl->byName().find(ttype);
325 if (iter == _impl->byName().end()) {
332 auto iter = _impl->byColumn().lower_bound(column);
333 if (iter == _impl->byColumn().end() || iter->column != column) {
340 auto iter = _impl->byColumn().lower_bound(item->
column);
341 assert(iter != _impl->byColumn().end() && iter->column == item->
column);
342 _impl->byColumn().erase(iter);
346 auto iter = _impl->byName().find(ttype);
347 if (iter != _impl->byName().end() && iter->ttype == ttype) {
348 _impl->byName().erase(iter);
353 auto iter = _impl->byColumn().lower_bound(column);
354 if (iter != _impl->byColumn().end() && iter->column == column) {
355 _impl->byColumn().erase(iter);
359 void erase(
int column);
362 _impl->readers.push_back(
std::move(reader));
367 template <
typename T>
404 "Angle fields must be persisted in radians (TUNIT='rad').");
429 _isVariableLength(size == 0) {}
441 bool _isVariableLength;
444 template <
typename T>
457 ndarray::Array<T, 1, 1> array = ndarray::allocate(size);
459 record.
set(_key, array);
469 template <
typename T>
541 template <
typename T,
int N>
545 static boost::regex
const regex(
"(.*)(\\^(\\d+))?", boost::regex::perl);
547 if (!boost::regex_match(oldUnits, m, regex)) {
570 for (
int i = 0; i < _size; ++i) {
571 for (
int j = i; j < _size; ++j) {
588 static boost::regex
const regex(
"(\\d+)?([PQ])?(\\u)\\(?(\\d)*\\)?", boost::regex::perl);
591 if (!boost::regex_match(item.
tform, m, regex)) {
598 char code = m[3].str()[0];
608 if (item.
tccls ==
"Array") {
609 return StandardReader<Array<std::uint8_t>>::make(schema, item, size);
611 return StandardReader<std::uint8_t>::make(schema, item);
614 return VariableLengthArrayReader<std::uint8_t>::make(schema, item);
616 return StandardReader<Array<std::uint8_t>>::make(schema, item, size);
621 if (item.
tccls ==
"Array") {
622 return StandardReader<Array<std::uint16_t>>::make(schema, item, size);
624 return StandardReader<std::uint16_t>::make(schema, item);
627 return VariableLengthArrayReader<std::uint16_t>::make(schema, item);
629 return StandardReader<Array<std::uint16_t>>::make(schema, item, size);
632 return VariableLengthArrayReader<std::int32_t>::make(schema, item);
634 if (item.
tccls ==
"Point") {
635 return PointConversionReader<std::int32_t>::make(schema, item);
637 if (size > 1 || item.
tccls ==
"Array") {
638 return StandardReader<Array<std::int32_t>>::make(schema, item, size);
640 return StandardReader<std::int32_t>::make(schema, item);
643 return StandardReader<std::int64_t>::make(schema, item);
647 return VariableLengthArrayReader<float>::make(schema, item);
650 if (item.
tccls ==
"Array") {
651 return StandardReader<Array<float>>::make(schema, item, 1);
655 return StandardReader<float>::make(schema, item);
657 if (size == 3 && item.
tccls ==
"Covariance(Point)") {
659 return CovarianceConversionReader<float, 2>::make(schema, item, names);
661 if (size == 6 && item.
tccls ==
"Covariance(Moments)") {
663 return CovarianceConversionReader<float, 3>::make(schema, item, names);
665 if (item.
tccls ==
"Covariance") {
666 double v = 0.5 * (
std::sqrt(1 + 8 * size) - 1);
668 if (n * (n + 1) != size * 2) {
672 for (
int i = 0; i < n; ++i) {
675 return CovarianceConversionReader<float, Eigen::Dynamic>::make(schema, item, names);
677 return StandardReader<Array<float>>::make(schema, item, size);
680 return VariableLengthArrayReader<double>::make(schema, item);
683 if (item.
tccls ==
"Angle") {
684 return AngleReader::make(schema, item);
686 if (item.
tccls ==
"Array") {
687 return StandardReader<Array<double>>::make(schema, item, 1);
689 return StandardReader<double>::make(schema, item);
692 if (item.
tccls ==
"Point") {
693 return PointConversionReader<double>::make(schema, item);
695 if (item.
tccls ==
"Coord") {
696 return CoordConversionReader::make(schema, item);
699 if (size == 3 && item.
tccls ==
"Moments") {
700 return MomentsConversionReader::make(schema, item);
702 return StandardReader<Array<double>>::make(schema, item, size);
705 return StringReader::make(schema, item, size);
720 if (!includes(item.
ttype,
"flux"))
return false;
729 return includes(units,
"count") || includes(units,
"dn") || includes (units,
"adu");
740 if (_impl->version == 0) {
741 AliasMap &aliases = *_impl->schema.getAliasMap();
742 for (
auto iter = _impl->asList().begin(); iter != _impl->asList().end(); ++iter) {
744 if (flagPos != std::string::npos) {
753 ttype.
replace(flagPos, 5,
"flag");
759 aliases.
set(prefix +
"flags", prefix +
"flag");
760 }
else if (isInstFlux(*iter)) {
762 if (endswith(iter->ttype,
"_err")) {
763 aliases.
set(replace(iter->ttype,
"_err",
"_instFluxErr"), iter->ttype);
765 aliases.
set(iter->ttype +
"_instFlux", iter->ttype);
767 }
else if (endswith(iter->ttype,
"_err")) {
773 if (iter->tccls ==
"Covariance(Point)") {
774 aliases.
set(replace(iter->ttype,
"_err",
"_yErr"), iter->ttype +
"_yErr");
775 aliases.
set(replace(iter->ttype,
"_err",
"_xErr"), iter->ttype +
"_xErr");
776 aliases.
set(replace(iter->ttype,
"_err",
"_x_y_Cov"), iter->ttype +
"_x_y_Cov");
777 }
else if (iter->tccls ==
"Covariance(Moments)") {
778 aliases.
set(replace(iter->ttype,
"_err",
"_xxErr"), iter->ttype +
"_xxErr");
779 aliases.
set(replace(iter->ttype,
"_err",
"_yyErr"), iter->ttype +
"_yyErr");
780 aliases.
set(replace(iter->ttype,
"_err",
"_xyErr"), iter->ttype +
"_xyErr");
781 aliases.
set(replace(iter->ttype,
"_err",
"_xx_yy_Cov"), iter->ttype +
"_xx_yy_Cov");
782 aliases.
set(replace(iter->ttype,
"_err",
"_xx_xy_Cov"), iter->ttype +
"_xx_xy_Cov");
783 aliases.
set(replace(iter->ttype,
"_err",
"_yy_xy_Cov"), iter->ttype +
"_yy_xy_Cov");
787 }
else if (_impl->version < 3) {
791 AliasMap &aliases = *_impl->schema.getAliasMap();
792 for (
auto iter = _impl->asList().begin(); iter != _impl->asList().end(); ++iter) {
794 if (_impl->version < 2 && endswith(name,
"Sigma")) {
795 name = replace(
std::move(name),
"Sigma",
"Err");
797 if (_impl->version < 3 && isInstFlux(*iter)) {
798 name = replace(
std::move(name),
"flux",
"instFlux");
800 if (name != iter->ttype) {
801 aliases.
set(name, iter->ttype);
805 for (
auto iter = _impl->asList().begin(); iter != _impl->asList().end(); ++iter) {
809 _impl->readers.push_back(
std::move(reader));
811 LOGLS_WARN(
"afw.FitsSchemaInputMapper",
"Format " << iter->tform <<
" for column " 813 <<
" not supported; skipping.");
816 if (static_cast<std::size_t>(iter->bit) >= _impl->flagKeys.size()) {
818 (boost::format(
"Flag field '%s' is is in bit %d (0-indexed) of only %d") %
819 iter->ttype % iter->bit % _impl->flagKeys.size())
822 _impl->flagKeys[iter->bit] = _impl->schema.addField<Flag>(iter->ttype, iter->doc);
825 _impl->asList().clear();
826 return _impl->schema;
830 if (!_impl->flagKeys.empty()) {
831 fits.
readTableArray<
bool>(row, _impl->flagColumn, _impl->flagKeys.size(), _impl->flagWorkspace.get());
832 for (
std::size_t bit = 0; bit < _impl->flagKeys.size(); ++bit) {
833 record.
set(_impl->flagKeys[bit], _impl->flagWorkspace[bit]);
836 for (
auto iter = _impl->readers.begin(); iter != _impl->readers.end(); ++iter) {
837 (**iter).readCell(record, row, fits, _impl->archive);
#define LOGLS_WARN(logger, message)
An ellipse core with quadrupole moments as parameters.
Defines the fields and offsets for a table.
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Key< Flag > const & target
std::string const & getComment(std::string const &name) const
std::vector< T > getArray(std::string const &name) const
void set(std::string const &alias, std::string const &target)
Add an alias to the schema or replace an existing one.
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
Mapping class that holds aliases for a Schema.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
int computeCovariancePackedSize(int size)
Defines the packed size of a covariance matrices.
long getTableArraySize(int col)
Return the size of an array column.
AngleUnit constexpr radians
A structure that describes a field as a collection of related strings read from the FITS header...
virtual void remove(std::string const &name)
An exception thrown when problems are found when reading or writing FITS files.
A base class for image defects.
A FunctorKey used to get or set a lsst::geom::Point from an (x,y) pair of int or double Keys...
static QuadrupoleKey addFields(Schema &schema, std::string const &name, std::string const &doc, CoordinateType coordType=CoordinateType::PIXEL)
Add a set of quadrupole subfields to a schema and return a QuadrupoleKey that points to them...
int indexCovariance(int i, int j)
Defines the ordering of packed covariance matrices.
bool exists(std::string const &name) const
Tag types used to declare specialized field types.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Field< T >::Element * getElement(Key< T > const &key)
Return a pointer to the underlying elements of a field (non-const).
#define LSST_EXCEPT(type,...)
T get(std::string const &name) const
Base class for all records.
A class used as a handle to a particular field in a table.
static CoordKey addFields(afw::table::Schema &schema, std::string const &name, std::string const &doc)
Add a pair of _ra, _dec fields to a Schema, and return a CoordKey that points to them.
void readTableArray(std::size_t row, int col, int nElements, T *value)
Read an array value from a binary table.
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
std::vector< std::string > getOrderedNames() const
A FunctorKey used to get or set a geom::ellipses::Quadrupole from a tuple of constituent Keys...
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
A FunctorKey used to get or set celestial coordinates from a pair of lsst::geom::Angle keys...