20 #ifndef _IMGREADERASCII_H_ 21 #define _IMGREADERASCII_H_ 26 #include "base/Optionpk.h" 35 FileReaderAscii(
const std::string& filename,
const char& fieldseparator);
37 void reset(){m_ifstream.clear();m_ifstream.seekg(0,std::ios::beg);};
38 void open(
const std::string& filename);
40 void setFieldSeparator(
const char& fieldseparator){m_fs=fieldseparator;};
41 void setMinRow(
int minRow){m_minRow=minRow;};
42 void setMaxRow(
int maxRow){m_maxRow=maxRow;};
43 void setComment(
char comment){m_comment=comment;};
44 unsigned int nrOfCol(
bool checkCols=
false,
bool verbose=
false);
45 unsigned int nrOfRow(
bool checkCols=
false,
bool verbose=
false);
46 template<
class T>
unsigned int readData(std::vector<std::vector<T> > &dataVector,
const std::vector<int> &cols,
double scale=1.0,
double offset=0.0,
bool transpose=
false,
bool verbose=
false);
47 template<
class T>
unsigned int readData(std::vector<T> &dataVector,
int col,
double scale=1.0,
double offset=0,
bool verbose=
false);
50 std::string m_filename;
51 std::ifstream m_ifstream;
60 template<
class T>
unsigned int FileReaderAscii::readData(std::vector<T> &dataVector,
int col,
double scale,
double offset,
bool verbose){
64 bool withinRange=
true;
65 if(m_fs>
' '&&m_fs<=
'~'){
67 std::cout <<
"reading csv file " << m_filename << std::endl;
68 std::string csvRecord;
69 while(getline(m_ifstream,csvRecord)){
77 std::istringstream csvstream(csvRecord);
81 while(getline(csvstream,item,m_fs)){
83 std::cout << item <<
" ";
84 size_t pos=item.find(m_comment);
85 if(pos!=std::string::npos){
88 item=item.substr(0,pos-1);
92 std::cout <<
"comment found, string is " << item << std::endl;
95 T value=scale*string2type<T>(item)+offset;
96 if((value>=m_min&&value<=m_max)||m_max<=m_min)
97 dataVector.push_back(value);
104 std::cout << std::endl;
105 if(dataVector.size()&&ncol<=col){
106 std::ostringstream ess;
107 ess <<
"Error: different number of cols found in line " << nrow <<
" (" << ncol <<
")" << std::endl;
113 assert(dataVector.size());
117 std::cout <<
"space or tab delimited fields" << std::endl;
118 std::string spaceRecord;
119 while(!getline(m_ifstream, spaceRecord).eof()){
123 if(m_maxRow>m_minRow)
128 std::cout << spaceRecord << std::endl;
129 std::istringstream lineStream(spaceRecord);
132 bool isComment=
false;
133 while(lineStream >> item){
135 std::cout << item <<
" ";
137 size_t pos=item.find(m_comment);
138 if(pos!=std::string::npos){
141 item=item.substr(0,pos-1);
145 std::cout <<
"comment found, string is " << item << std::endl;
147 T value=scale*string2type<T>(item)+offset;
150 if((value>=m_min&&value<=m_max)||m_max<=m_min)
151 dataVector.push_back(value);
158 std::cout << std::endl;
160 std::cout <<
"number of columns: " << ncol << std::endl;
161 if(dataVector.size()&&ncol<=col){
162 std::ostringstream ess;
163 ess <<
"Error: different number of cols found in line " << nrow <<
" (" << ncol <<
")" << std::endl;
170 return dataVector.size();
173 template<
class T>
unsigned int FileReaderAscii::readData(std::vector<std::vector<T> > &dataVector,
const std::vector<int> &cols,
double scale,
double offset,
bool transpose,
bool verbose){
177 dataVector.resize(cols.size());
179 bool withinRange=
true;
180 if(m_fs>
' '&&m_fs<=
'~'){
182 std::cout <<
"reading csv file " << m_filename << std::endl;
183 std::string csvRecord;
184 while(getline(m_ifstream,csvRecord)){
185 std::vector<T> sampleVector;
189 if(m_maxRow>m_minRow)
193 std::istringstream csvstream(csvRecord);
196 bool isComment=
false;
197 while(getline(csvstream,item,m_fs)){
199 std::cout << item <<
" ";
200 size_t pos=item.find(m_comment);
201 if(pos!=std::string::npos){
204 item=item.substr(0,pos-1);
208 std::cout <<
"comment found, string is " << item << std::endl;
210 for(
int icol=0;icol<cols.size();++icol){
211 if(ncol==cols[icol]){
212 T value=scale*string2type<T>(item)+offset;
214 if((value>=m_min&&value<=m_max)||m_max<=m_min){
216 sampleVector.push_back(value);
218 dataVector[icol].push_back(value);
227 std::cout << std::endl;
231 if(sampleVector.size()&&transpose)
232 dataVector.push_back(sampleVector);
235 assert(dataVector.size());
239 std::cout <<
"space or tab delimited fields" << std::endl;
240 std::string spaceRecord;
241 while(!getline(m_ifstream, spaceRecord).eof()){
242 std::vector<T> sampleVector;
246 if(m_maxRow>m_minRow)
251 std::cout << spaceRecord << std::endl;
252 std::istringstream lineStream(spaceRecord);
255 bool isComment=
false;
256 while(lineStream >> item){
258 std::cout << item <<
" ";
260 size_t pos=item.find(m_comment);
261 if(pos!=std::string::npos){
264 item=item.substr(0,pos-1);
268 std::cout <<
"comment found, string is " << item << std::endl;
270 T value=scale*string2type<T>(item)+offset;
272 for(
int icol=0;icol<cols.size();++icol){
273 if(ncol==cols[icol]){
274 if((value>=m_min&&value<=m_max)||m_max<=m_min){
276 sampleVector.push_back(value);
278 dataVector[icol].push_back(value);
287 std::cout << std::endl;
289 std::cout <<
"number of columns: " << ncol << std::endl;
293 if(sampleVector.size()&&transpose)
294 dataVector.push_back(sampleVector);
298 return dataVector.size();
301 #endif // _IMGREADERASCII_H_