#include "EleFitsUtils/ProgramOptions.h"
#include "ElementsKernel/ProgramHeaders.h"
#include <boost/program_options.hpp>
#include <map>
#include <string>
#include "EleFitsData/TestColumn.h"
#include "EleFitsData/TestRaster.h"
#include "EleFitsData/TestRecord.h"
#include "EleFits/MefFile.h"
using namespace Euclid;
using boost::program_options::options_description;
using boost::program_options::value;
using boost::program_options::variable_value;
static Elements::Logging logger = Elements::Logging::getLogger("EleFitsTutorial");
struct TutoRecords {
};
struct TutoRasters {
};
struct TutoColumns {
};
TutoRecords create_records();
TutoRasters create_rasters();
TutoColumns create_columns();
TutoRecords create_records() {
logger.info(" Creating records...");
auto complex_record = Fits::Test::generate_random_record<std::complex<double>>("COMPLEX");
return {string_record, int_record, float_record, complex_record};
}
TutoRasters create_rasters() {
logger.info(" Creating rasters...");
for (const auto& position : int16_raster2d.domain()) {
int16_raster2d[position] = position[0] + position[1];
}
return {int16_raster2d, int32_raster3d, int64_raster4d};
}
TutoColumns create_columns() {
logger.info(" Creating columns...");
for (long i = 0; i < string_column.row_count(); ++i) {
}
return {string_column, int32_column, float_column};
}
logger.
info(
"Creating a MEF file...");
const auto rasters = create_rasters();
logger.info(" Writing image HDUs...");
const auto& image1 = f.append_image("IMAGE1", {}, rasters.int32_raster3d);
const auto& image2 = f.append_null_image<
std::int16_t>(
"IMAGE2", {}, rasters.int16_raster2d.shape());
image2.raster().write(rasters.int16_raster2d);
const auto columns = create_columns();
logger.info(" Writing binary table HDUs...");
const auto& table1 =
f.append_bintable("TABLE1", {}, columns.string_column, columns.int32_column, columns.float_column);
const auto& table2 = f.append_bintable_header(
"TABLE2",
{},
columns.string_column.info(),
columns.int32_column.info(),
columns.float_column.info());
table2.columns().write(columns.string_column);
table2.columns().write_n(columns.int32_column, columns.float_column);
write_records(f.primary().header());
(void)image1;
(void)table1;
}
const auto records = create_records();
logger.info(" Writing records...");
h.
write(records.string_record);
h.
write_n(records.int_record, records.float_record, records.complex_record);
h.
write_n<Fits::RecordMode::UpdateExisting>(
}
logger.info("Reading the MEF file...");
logger.info(" Accessing HDUs...");
const auto& primary = f.primary();
const auto primary_index = primary.index();
const auto table_index = table1.
index();
logger.info() << " Primary index: " << primary_index;
logger.info() << " Name of the second extension: " << image_name;
logger.info() << " Index of the 'TABLE1' extension: " << table_index;
read_records(primary.header());
read_raster(image2.raster());
read_columns(table1.columns());
}
logger.info(" Reading records...");
const auto int_record = h.
parse<
int>(
"INT");
const int int_value = h.
parse<
int>(
"INT");
const auto some_records = h.
parse_n(
Fits::as<std::string>("STRING"),
Fits::as<int>("INT"),
Fits::as<float>("FLOAT"),
Fits::as<std::complex<double>>("COMPLEX"));
const auto& third_record = std::get<2>(some_records);
const auto variant_records = h.
parse_n<>({
"INT",
"COMPLEX"});
Fits::as<std::string>("STRING"),
Fits::as<int>("INT"),
Fits::as<float>("FLOAT"),
Fits::as<std::complex<double>>("COMPLEX"));
const auto& string_record = tuto_records.string_record;
logger.info() << " " << int_record.keyword << " = " << int_record.value << " " << int_record.unit;
logger.info() << " INT value: " << int_value;
logger.info() << " " << third_record.keyword << " = " << third_record.value << " " << third_record.unit;
logger.info() << " " << complex_record.keyword << " = " << complex_record.value.real() << " + "
<< complex_record.value.imag() << "j " << complex_record.unit;
logger.info() << " " << string_record.keyword << " = " << string_record.value << " " << string_record.unit;
}
logger.info(" Reading a raster...");
const auto& first_pixel = image[{0, 0}];
const auto& last_pixel = image.at({-1, -1});
logger.info() << " First pixel: " << first_pixel;
logger.info() << " Last pixel: " << last_pixel;
}
logger.info(" Reading columns...");
const auto vector_column = du.
read<
double>(
"VECTOR");
const auto by_name = du.
read_n(Fits::as<std::string>(
"STRING"), Fits::as<std::int32_t>(
"INT32"));
const auto& string_column = std::get<0>(by_name);
const auto by_index = du.
read_n(Fits::as<std::string>(0), Fits::as<std::int32_t>(1));
const auto& int_column = std::get<1>(by_index);
const auto& first_string = string_column(0);
const auto& first_int = int_column(0);
const auto& last_float = vector_column.at(-1, -1);
logger.info() << " First string: " << first_string;
logger.info() << " First int: " << first_int;
logger.info() << " Last float: " << last_float;
}
class EleFitsTutorial : public Elements::Program {
public:
auto options = Fits::ProgramOptions::from_aux_file("Tutorial.txt");
options.positional("output", value<std::string>()->default_value("/tmp/tuto.fits"), "Output file");
return options.as_pair();
}
const auto filename = args[
"output"].as<
std::string>();
logger.info() << "---";
logger.info() << "Hello, EleFits " << Fits::version() << "!";
logger.info() << "---";
write_file(filename);
logger.info() << "---";
logger.info() << "---";
logger.info() << "The end!";
logger.info() << "---";
return Elements::ExitCode::OK;
}
};
MAIN_FOR(EleFitsTutorial)
std::tuple< VecColumn< Ts, 1 >... > read_n(const TypedKey< Ts, TKey > &... keys) const
Read a tuple of columns with given names or indices.
VecColumn< T, N > read(ColumnKey key) const
Read the column with given name or index.
Column-wise reader-writer for the binary table data unit.
Definition: BintableColumns.h:77
Binary table HDU reader-writer.
Definition: BintableHdu.h:21
Binary table column data and metadata.
Definition: Column.h:69
const Info & info() const
Get the column metadata.
long index() const
Get the 0-based index of the HDU.
std::string read_name() const
Read the extension name.
Image HDU reader-writer.
Definition: ImageHdu.h:45
VecRaster< T, N > read() const
Read the whole data unit as a new VecRaster.
Reader-writer for the image data unit.
Definition: ImageRaster.h:31
Multi-Extension FITS file reader-writer.
Definition: MefFile.h:84
Data of a n-dimensional image (2D by default).
Definition: Raster.h:149
A random Raster of given type and shape.
Definition: TestRaster.h:54
A small vector column of given type.
Definition: TestColumn.h:172
std::string read_file(const std::string &filename)
Read a text file.
Keyword-value pair with optional unit and comment.
Definition: Record.h:101