#include "EleFits/MefFile.h"
#include "EleFitsData/TestUtils.h"
#include "EleFitsUtils/ProgramOptions.h"
#include "ElementsKernel/ProgramHeaders.h"
using boost::program_options::value;
using namespace Euclid;
const Fits::BintableHdu& writeBintable(Fits::MefFile& f,
const std::string& extName,
long rows) {
auto stringInfo = Fits::makeColumnInfo<std::string>("STRING", "", 6);
auto stringData = Fits::Test::generateRandomVector<std::string>(rows);
auto scalarInfo = Fits::makeColumnInfo<std::complex<float>>("SCALAR");
auto scalarData = Fits::Test::generateRandomVector<std::complex<float>>(rows);
auto vectorInfo = Fits::makeColumnInfo<std::int16_t>("VECTOR", "", 3);
auto vectorData = Fits::Test::generateRandomVector<std::int16_t>(rows * vectorInfo.repeatCount());
auto multidimInfo = Fits::makeColumnInfo<std::uint16_t>("MULTIDIM", "", 6, 4);
auto multidimData = Fits::Test::generateRandomVector<std::uint16_t>(rows * multidimInfo.repeatCount());
return f.assignBintableExt(extName, stringCol, scalarCol, vectorCol, multidimCol);
}
template <typename TInfo, typename T>
void appendColumn(const Fits::BintableColumns& du, const TInfo& info, const T* data) {
const auto rows = du.readRowCount();
du.init(info);
}
double readColumns(const Fits::BintableColumns& du) {
const auto cols = du.readSeq(Fits::as<double>("VECTOR"), Fits::as<float>("MULTIDIM"));
double result = 0;
for (const auto& a : std::get<0>(cols)) {
result += a;
}
for (const auto& b : std::get<1>(cols)) {
result -= b;
}
return result;
}
void viewAsRaster(const Fits::BintableColumns& du) {
for (long i = 0; i < col.rowCount(); ++i) {
auto raster = col.entry(i);
for (const auto& p : raster.domain()) {
if ((p[0] + p[1]) % 2 == 1) {
raster[p] = 0;
}
}
}
du.write(col);
}
class EleFitsBintableExample : public Elements::Program {
public:
Fits::ProgramOptions options("Generate, write and read a binary table.");
options.positional("output", value<std::string>()->default_value("/tmp/bintable.fits"), "Output file");
options.named("rows", value<long>()->default_value(42), "Number of rows");
return options.asPair();
}
Logging logger = Logging::getLogger("EleFitsBintableExample");
const auto filename = args[
"output"].as<
std::string>();
const auto rows = args["rows"].as<long>();
logger.info("Opening or creating the file...");
Fits::MefFile f(filename, Fits::FileMode::Write);
logger.info("Creating a binary table HDU...");
const auto& bintable = writeBintable(f, extName, rows);
logger.info("Appending a column...");
Fits::ColumnInfo<double> info("BACK", "unit");
const auto vec = Fits::Test::generateRandomVector<double>(rows, -1, 1);
appendColumn(bintable.columns(), info, vec.data());
logger.info("Reading columns...");
auto result = readColumns(bintable.columns());
logger.info() << " Result = " << result;
logger.info("Modifying a multidimensional column...");
viewAsRaster(bintable.columns());
logger.info("Done.");
return ExitCode::OK;
}
};
MAIN_FOR(EleFitsBintableExample)
Column< typename TContainer::value_type, std::decay_t< TInfo >::Dim, TContainer > makeColumn(TInfo info, TContainer &&data)
Shortcut to create a column from a column info and data without specifying the template parameters.
Definition: Column.h:250