CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
fits.cpp

This example shows how to read/write a fits file

// ===========================================
// Example code: how to read/write a fits file
// ===========================================
#include "FITSwrapper.h"
int main () {
try {
// input file to read
const std::string input_file = "catalogue.fits";
// number of the table extension in the FITS file; a FITS can
// contain more the one extension; the extension 0 is the primary
// header
const int next = 1;
// the value used to fill non-existing columns, if
// fill_value==par::defaultDouble, an error is raised when a
// column is not found
const double fill_value = 1.;
// name of the columns in the Table
const std::vector<std::string> column_names = {"XX", "YY", "ZZ", "Weight"};
// read the columns from the table searching by names
std::vector<std::vector<double>> table = cbl::wrapper::ccfits::read_table_fits(input_file, column_names, next, fill_value);
std::cout << table[0][0] << " " <<table[1][0] << " " << table[2][0] << " " << table[3][0] << std::endl;
// the unit of the columns, optional
const std::vector<std::string> column_units = {"Mpc/h", "Mpc/h", "Mpc/h", ""};
// write the output on a new fits file, adding the weight column
cbl::wrapper::ccfits::write_table_fits("./", "catalogue_with_weights.fits", column_names, table, column_units);
}
catch(cbl::glob::Exception &exc) { std::cerr << exc.what() << std::endl; exit(1); }
return 0;
}
class FITSwrapper that wrap CCfits routines to manage FITS files
int main()
main function to create the logo of the CosmoBolognaLib
Definition: Logo.cpp:41
The class Exception.
Definition: Exception.h:111
const char * what() const noexcept override
the error description
Definition: Exception.h:203
std::vector< std::vector< double > > read_table_fits(const std::string input_fits, const std::vector< std::string > column_names, const int next=1, const double fill_value=cbl::par::defaultDouble)
function to read a table from a fits file
Definition: FITSwrapper.cpp:45
void write_table_fits(const std::string output_dir, const std::string file_fits, const std::vector< std::string > column_names, const std::vector< std::vector< double >> table, const std::vector< std::string > column_units={})
function that writes a table on a fits file
Definition: FITSwrapper.cpp:88