16 std::vector<int> cols_to_keep,
18 size_t nestimated_lines) {
21 assert(cols_to_keep.size() > 0 and nskip >= 0 and ncols > 0);
22 for (
auto & i : cols_to_keep)
23 assert(i < ncols and i >= 0);
28 throw std::runtime_error(
"[read_regular_ascii] Failed to open [" +
filename +
"]\n");
32 int ntokeep = int(cols_to_keep.size());
34 result.reserve(nestimated_lines);
41 for (
int i = 0; i < nskip; i++) {
43 std::getline(fp, line);
45 std::cout <<
"Skipping headerline: " << line <<
"\n";
51 std::getline(fp, line);
56 temp[count] = std::stod(line);
59 for (
int i = 0; i < ntokeep; i++)
60 newline[i] = temp[cols_to_keep[i]];
61 result.push_back(newline);
63 throw std::runtime_error(
"Found ncols " + std::to_string(count) +
" which differs from specified " +
64 std::to_string(ncols) +
"\n");
72 for (
int i = 1; i < ncols; i++)
74 for (
int i = 0; i < ntokeep; i++)
75 newline[i] = temp[cols_to_keep[i]];
77 if (result.size() < 10) {
78 std::cout <<
"Line " << result.size() - 1 <<
" : ";
79 for (
auto e : result[result.size() - 1])
80 std::cout << e <<
" ";
81 std::cout << std::endl;
84 result.push_back(newline);
94 std::vector<int> cols_to_keep,
96 size_t nestimated_lines,
97 double fraction_to_read,
98 unsigned int randomSeed) {
100 auto udist = std::uniform_real_distribution<double>(0.0, 1.0);
103 assert(cols_to_keep.size() > 0 and nskip >= 0 and ncols > 0);
104 for (
auto & i : cols_to_keep)
105 assert(i < ncols and i >= 0);
110 throw std::runtime_error(
"[read_regular_ascii] Failed to open [" +
filename +
"]\n");
114 int ntokeep = int(cols_to_keep.size());
116 result.reserve(nestimated_lines);
123 for (
int i = 0; i < nskip; i++) {
125 std::getline(fp, line);
126#ifdef DEBUG_READASCII
127 std::cout <<
"Skipping headerline: " << line <<
"\n";
133 std::getline(fp, line);
134 std::stringstream ss;
138 temp[count] = std::stod(line);
141 for (
int i = 0; i < ntokeep; i++)
142 newline[i] = temp[cols_to_keep[i]];
143 result.push_back(newline);
144 if (count != ncols) {
145 throw std::runtime_error(
"Found ncols " + std::to_string(count) +
" which differs from specified " +
146 std::to_string(ncols) +
"\n");
154 for (
int i = 1; i < ncols; i++)
156 for (
int i = 0; i < ntokeep; i++)
157 newline[i] = temp[cols_to_keep[i]];
158#ifdef DEBUG_READASCII
159 if (result.size() < 10) {
160 std::cout <<
"Line " << result.size() - 1 <<
" : ";
161 for (
auto e : result[result.size() - 1])
162 std::cout << e <<
" ";
163 std::cout << std::endl;
166 if (fraction_to_read >= 1.0)
167 result.push_back(newline);
168 else if (udist(
generator) < fraction_to_read)
169 result.push_back(newline);
179 for (
auto & row :
data) {
180 x.push_back(row[col1]);
181 y.push_back(row[col2]);
194 throw std::runtime_error(
"[loadtxt] Failed to open [" +
filename +
"]\n");
199 result.reserve(nreserve_rows);
206 std::getline(fp, line);
212 if (line[0] ==
'#' or line[0] ==
'!' or line[0] ==
'/')
216 std::stringstream ss;
217 std::vector<double> row;
218 row.reserve(nreserve_cols);
221 row.push_back(std::stod(line));
225 result.push_back(row);
229 size_t size = result.size();
231 size_t ncol1 = result[0].size();
232 size_t ncolmin = ncol1;
234 for (
size_t j = 1; j < size; j++) {
235 ncolmin = std::min(ncolmin, result[j].size());
236 ncolmax = std::max(ncolmax, result[j].size());
238 if (ncolmin != ncolmax) {
239 std::cout <<
"[loadtxt] Warning the file " +
filename +
" is not regular\n";
240 std::cout <<
"The minimum number of columns is " << ncolmin <<
"\n";
241 std::cout <<
"The maximum number of columns is " << ncolmax <<
"\n";
const std::string filename
FML::INTERPOLATION::SPLINE::DVector2D DVector2D
std::pair< DVector, DVector > read_file_and_extract_two_columns(std::string filename, int col1, int col2)
Read a regular file and extract two columns (numbering starting with 0)
DVector2D loadtxt(std::string filename, int nreserve_rows, int nreserve_cols)
Similar to pythons loadtxt function Gives warning if file is not regular Skips lines starting with #,...
FML::INTERPOLATION::SPLINE::DVector DVector
DVector2D read_regular_ascii_subsampled(std::string filename, int ncols, std::vector< int > cols_to_keep, int nskip, size_t nestimated_lines, double fraction_to_read, unsigned int randomSeed)
As above, but include every line read with probabillity fraction_to_read.
DVector2D read_regular_ascii(std::string filename, int ncols, std::vector< int > cols_to_keep, int nskip, size_t nestimated_lines)
Read a regular ascii files with nskip header lines and containing ncol collums nestimated_lines is th...