47 vector<vector<double>> values(names.size(), vector<double>(nrows, 0.));
57 this->insert(names, values);
68 for(table_map::iterator it = m_table.begin(); it != m_table.end(); ++it)
69 keys.push_back(it->first);
96 cbl::data::Table::Table (
const std::string input_dir,
const std::string input_file,
const std::vector<std::string> names,
const std::vector<size_t> use_cols,
const size_t header_lines_to_skip)
98 this->read(input_dir, input_file, names, use_cols, header_lines_to_skip);
107 table_map::iterator it = m_table.find(name);
109 if (it == m_table.end())
110 ErrorCBL(
"Column "+name+
" has not been found, exiting.",
"cbl::data::Table::operator[]",
"Data/Table.cpp");
112 return m_table.at(name);
121 vector<vector<double>> columns(names.size());
123 for (
size_t i=0; i<names.size(); i++)
124 columns[i] = this->
operator[](names[i]);
135 table_map::iterator it = m_table.find(name);
137 if (it == m_table.end())
138 m_table.emplace(name, values);
142 m_table.emplace(name, values);
145 cbl::ErrorCBL(
"Column "+name+
" exists and replace is false. Check your inputs. Exiting.",
"cbl::data::Table::insert",
"Data/Table.cpp");
153 void cbl::data::Table::insert (
const std::vector<std::string> names,
const std::vector<std::vector<double>> values,
const bool replace)
155 for (
size_t i=0; i<names.size(); i++)
156 this->insert(names[i], values[i], replace);
163 void cbl::data::Table::read (
const std::string input_dir,
const std::string input_file,
const std::vector<std::string> names,
const vector<size_t> use_cols,
const size_t header_lines_to_skip)
165 string file = input_dir+input_file;
166 coutCBL <<
"Reading the table " << file << endl;
168 const size_t ncols = names.size();
170 vector<vector<double>> values;
172 if (names.size() != use_cols.size() && use_cols.size()>0)
173 ErrorCBL(
"Wrong number of columns, check your inputs!",
"cbl::data::Table::read",
"Data/Table.cpp");
175 std::function<vector<double>(
const vector<double> params)> assign;
177 if (use_cols.size()==0) {
178 assign = [&] (
const vector<double> params) {
183 checkDim(use_cols, ncols,
"columns");
184 assign = [&] (
const vector<double> params) {
185 vector<double> pp(ncols);
186 for (
size_t i=0; i<ncols; i++)
187 pp[i] = params[use_cols[i]];
192 ifstream fin(file.c_str());
checkIO(fin, file);
195 for (
size_t i=0; i<header_lines_to_skip; i++)
198 while (getline(fin, line)) {
199 stringstream ss(line);
203 while (ss>>NUM) ll.push_back(NUM);
205 values.push_back(assign(ll));
208 fin.clear(); fin.close();
212 coutCBL <<
"Done!" << endl << endl;
219 void cbl::data::Table::write (
const std::string output_dir,
const std::string output_file,
const std::vector<std::string> names)
221 vector<string> keys = (names.size()==0) ? this->m_keys() : names;
222 vector<vector<double>> values =
transpose(this->
operator[](keys));
224 string MK =
"mkdir -p "+output_dir;
225 if (system(MK.c_str())) {}
227 string file = output_dir+output_file;
229 ofstream fout(file.c_str());
checkIO(fout, file);
234 for (
size_t i=0; i<keys.size(); i++)
235 fout <<
" " << keys[i];
238 for (
size_t i=0; i<values.size(); i++) {
239 for (
size_t j=0; j<values[i].size(); j++)
240 cbl::Print(values[i][j], 5, 14,
"",
" ",
false, fout);
244 fout.clear(); fout.close();
246 coutCBL <<
"I wrote the file: " << file << endl;
#define coutCBL
CBL print message.
std::vector< std::string > m_keys()
get the map keys
void m_set(const std::vector< std::string > names, const size_t nrows)
function to set internal members
std::vector< double > & operator[](const std::string name)
function to get a column
void write(const std::string output_dir, const std::string output_file, const std::vector< std::string > names={})
function to write columns
void insert(const std::string name, const std::vector< double > values, const bool replace=false)
function to insert a column
Table()=default
default constructor
void read(const std::string input_dir, const std::string input_file, const std::vector< std::string > names, const std::vector< size_t > use_cols={}, const size_t header_lines_to_skip=1)
function to read from an ascii file
The global namespace of the CosmoBolognaLib
void Print(const T value, const int prec, const int ww, const std::string header="", const std::string end="\n", const bool use_coutCBL=true, std::ostream &stream=std::cout, const std::string colour=cbl::par::col_default)
function to print values with a proper homegenised format
void checkDim(const std::vector< T > vect, const int val, const std::string vector, bool equal=true)
check if the dimension of a std::vector is equal/lower than an input value
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
std::vector< std::vector< T > > transpose(std::vector< std::vector< T >> matrix)
transpose a matrix