pktools 2.6.7
Processing Kernel for geospatial data
Public Member Functions | Protected Attributes | List of all members
FileReaderAscii Class Reference
Collaboration diagram for FileReaderAscii:
Collaboration graph
[legend]

Public Member Functions

 FileReaderAscii (const std::string &filename)
 
 FileReaderAscii (const std::string &filename, const char &fieldseparator)
 
void reset ()
 
void open (const std::string &filename)
 
void close (void)
 
void setFieldSeparator (const char &fieldseparator)
 
void setMinRow (int minRow)
 
void setMaxRow (int maxRow)
 
void setComment (char comment)
 
unsigned int nrOfCol (bool checkCols=false, bool verbose=false)
 
unsigned int nrOfRow (bool checkCols=false, bool verbose=false)
 
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)
 
template<class T >
unsigned int readData (std::vector< T > &dataVector, int col, double scale=1.0, double offset=0, bool verbose=false)
 

Protected Attributes

std::string m_filename
 
std::ifstream m_ifstream
 
char m_fs
 
char m_comment
 
double m_min
 
double m_max
 
int m_minRow
 
int m_maxRow
 

Detailed Description

Definition at line 30 of file FileReaderAscii.h.

Constructor & Destructor Documentation

◆ FileReaderAscii() [1/3]

FileReaderAscii::FileReaderAscii ( void  )

Definition at line 24 of file FileReaderAscii.cc.

25 : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(' '),m_comment('#'){
26}

◆ FileReaderAscii() [2/3]

FileReaderAscii::FileReaderAscii ( const std::string &  filename)

Definition at line 28 of file FileReaderAscii.cc.

29 : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(' '),m_comment('#'){
30 open(filename);
31}

◆ FileReaderAscii() [3/3]

FileReaderAscii::FileReaderAscii ( const std::string &  filename,
const char &  fieldseparator 
)

Definition at line 33 of file FileReaderAscii.cc.

34 : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(fieldseparator),m_comment('#'){
35 open(filename);
36}

◆ ~FileReaderAscii()

FileReaderAscii::~FileReaderAscii ( void  )

Definition at line 38 of file FileReaderAscii.cc.

39{
40}

Member Function Documentation

◆ close()

void FileReaderAscii::close ( void  )

Definition at line 53 of file FileReaderAscii.cc.

53 {
54 m_ifstream.close();
55 // m_ifstream.clear();
56}

◆ nrOfCol()

unsigned int FileReaderAscii::nrOfCol ( bool  checkCols = false,
bool  verbose = false 
)

Definition at line 58 of file FileReaderAscii.cc.

58 {
59 reset();
60 unsigned int totalCol=0;
61 unsigned int nrow=0;
62 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
63 if(verbose)
64 std::cout << "reading csv file " << m_filename << std::endl;
65 std::string csvRecord;
66 while(getline(m_ifstream,csvRecord)){//read a line
67 std::istringstream csvstream(csvRecord);
68 std::string item;
69 unsigned int ncol=0;
70 bool isComment=false;
71 while(getline(csvstream,item,m_fs)){//read a column
72 if(verbose)
73 std::cout << item << " ";
74 size_t pos=item.find(m_comment);
75 if(pos!=std::string::npos){
76 if(pos>0)
77 item=item.substr(0,pos-1);
78 else
79 break;
80 if(verbose)
81 std::cout << "comment found, string is " << item << std::endl;
82 isComment=true;
83 }
84 ++ncol;
85 if(isComment)
86 break;
87 }
88 if(verbose)
89 std::cout << std::endl << "number of columns: " << ncol << std::endl;
90 if(!totalCol)
91 totalCol=ncol;
92 else if(checkCols){
93 if(totalCol!=ncol){
94 std::ostringstream ess;
95 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
96 throw(ess.str());
97 }
98 ++nrow;
99 }
100 else
101 break;
102 }
103 }
104 else{//space or tab delimited fields
105 if(verbose)
106 std::cout << "space or tab delimited fields" << std::endl;
107 std::string spaceRecord;
108 while(!getline(m_ifstream, spaceRecord).eof()){
109 if(verbose>1)
110 std::cout << spaceRecord << std::endl;
111 std::istringstream lineStream(spaceRecord);
112 std::string item;
113 unsigned int ncol=0;
114 bool isComment=false;
115 while(lineStream >> item){
116 if(verbose)
117 std::cout << item << " ";
118 size_t pos=item.find(m_comment);
119 if(pos!=std::string::npos){
120 if(pos>0)
121 item=item.substr(0,pos-1);
122 else
123 break;
124 if(verbose)
125 std::cout << "comment found, string is " << item << std::endl;
126 isComment=true;
127 }
128 ++ncol;
129 if(isComment)
130 break;
131 }
132 if(verbose)
133 std::cout << std::endl << "number of columns: " << ncol << std::endl;
134 if(!totalCol)
135 totalCol=ncol;
136 else if(checkCols){
137 if(totalCol!=ncol){
138 std::ostringstream ess;
139 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
140 throw(ess.str());
141 }
142 }
143 else
144 break;
145 ++nrow;
146 }
147 }
148 return totalCol;
149}

◆ nrOfRow()

unsigned int FileReaderAscii::nrOfRow ( bool  checkCols = false,
bool  verbose = false 
)

Definition at line 151 of file FileReaderAscii.cc.

151 {
152 reset();
153 unsigned int totalCol=0;
154 unsigned int nrow=0;
155 unsigned int ncomment=0;
156 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
157 if(verbose)
158 std::cout << "reading csv file " << m_filename << std::endl;
159 std::string csvRecord;
160 while(getline(m_ifstream,csvRecord)){//read a line
161 std::istringstream csvstream(csvRecord);
162 std::string item;
163 unsigned int ncol=0;
164 bool isComment=false;
165 while(getline(csvstream,item,m_fs)){//read a column
166 if(verbose)
167 std::cout << item << " ";
168 size_t pos=item.find(m_comment);
169 if(pos!=std::string::npos){
170 if(pos>0){
171 if(verbose)
172 std::cout << "comment found, string is " << item << std::endl;
173 isComment=true;
174 }
175 else{
176 ++ncomment;
177 break;
178 }
179 }
180 ++ncol;
181 if(isComment)
182 break;
183 }
184 if(verbose)
185 std::cout << std::endl;
186 if(checkCols){
187 if(totalCol!=ncol){
188 std::ostringstream ess;
189 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
190 throw(ess.str());
191 }
192 }
193 ++nrow;
194 }
195 }
196 else{//space or tab delimited fields
197 if(verbose)
198 std::cout << "space or tab delimited fields" << std::endl;
199 std::string spaceRecord;
200 int totalCol=0;
201 while(!getline(m_ifstream, spaceRecord).eof()){
202 if(verbose>1)
203 std::cout << spaceRecord << std::endl;
204 std::istringstream lineStream(spaceRecord);
205 std::string item;
206 unsigned int ncol=0;
207 bool isComment=false;
208 while(lineStream >> item){
209 if(verbose)
210 std::cout << item << " ";
211 size_t pos=item.find(m_comment);
212 if(pos!=std::string::npos){
213 if(pos>0){
214 if(verbose)
215 std::cout << "comment found, string is " << item << std::endl;
216 isComment=true;
217 }
218 else{
219 ++ncomment;
220 break;
221 }
222 }
223 ++ncol;
224 if(isComment)
225 break;
226 }
227 if(verbose)
228 std::cout << std::endl << "number of columns: " << ncol << std::endl;
229 if(checkCols){
230 if(totalCol!=ncol){
231 std::ostringstream ess;
232 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
233 throw(ess.str());
234 }
235 }
236 ++nrow;
237 }
238 }
239 return nrow;
240}

◆ open()

void FileReaderAscii::open ( const std::string &  filename)

Definition at line 42 of file FileReaderAscii.cc.

42 {
43 m_filename=filename;
44 m_ifstream.open(filename.c_str(),std::ios_base::in);
45 if(!(m_ifstream)){
46 std::string errorString;
47 errorString="Error: could not open file ";
48 errorString+=filename;
49 throw(errorString);
50 }
51}

◆ readData() [1/2]

template<class T >
unsigned int FileReaderAscii::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 
)

Definition at line 173 of file FileReaderAscii.h.

173 {
174 reset();
175 dataVector.clear();
176 if(!transpose)
177 dataVector.resize(cols.size());
178 int nrow=0;
179 bool withinRange=true;
180 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
181 if(verbose)
182 std::cout << "reading csv file " << m_filename << std::endl;
183 std::string csvRecord;
184 while(getline(m_ifstream,csvRecord)){//read a line
185 std::vector<T> sampleVector;
186 withinRange=true;
187 if(nrow<m_minRow)
188 withinRange=false;
189 if(m_maxRow>m_minRow)
190 if(nrow>m_maxRow)
191 withinRange=false;
192 if(withinRange){
193 std::istringstream csvstream(csvRecord);
194 std::string item;
195 int ncol=0;
196 bool isComment=false;
197 while(getline(csvstream,item,m_fs)){//read a column
198 if(verbose)
199 std::cout << item << " ";
200 size_t pos=item.find(m_comment);
201 if(pos!=std::string::npos){
202 isComment=true;
203 if(pos>0)
204 item=item.substr(0,pos-1);
205 else
206 break;
207 if(verbose)
208 std::cout << "comment found, string is " << item << std::endl;
209 }
210 for(int icol=0;icol<cols.size();++icol){
211 if(ncol==cols[icol]){
212 T value=scale*string2type<T>(item)+offset;
213 // T value=string2type<T>(item);
214 if((value>=m_min&&value<=m_max)||m_max<=m_min){
215 if(transpose)
216 sampleVector.push_back(value);
217 else
218 dataVector[icol].push_back(value);
219 }
220 }
221 }
222 ++ncol;
223 if(isComment)
224 break;
225 }
226 if(verbose)
227 std::cout << std::endl;
228 // if(dataVector.back().size())
229 // assert(ncol>=cols[0]);
230 }
231 if(sampleVector.size()&&transpose)
232 dataVector.push_back(sampleVector);
233 ++nrow;
234 }
235 assert(dataVector.size());
236 }
237 else{//space or tab delimited fields
238 if(verbose)
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;
243 withinRange=true;
244 if(nrow<m_minRow)
245 withinRange=false;
246 if(m_maxRow>m_minRow)
247 if(nrow>m_maxRow)
248 withinRange=false;
249 if(withinRange){
250 if(verbose>1)
251 std::cout << spaceRecord << std::endl;
252 std::istringstream lineStream(spaceRecord);
253 std::string item;
254 int ncol=0;
255 bool isComment=false;
256 while(lineStream >> item){
257 if(verbose)
258 std::cout << item << " ";
259 // std::istringstream itemStream(item);
260 size_t pos=item.find(m_comment);
261 if(pos!=std::string::npos){
262 isComment=true;
263 if(pos>0)
264 item=item.substr(0,pos-1);
265 else
266 break;
267 if(verbose)
268 std::cout << "comment found, string is " << item << std::endl;
269 }
270 T value=scale*string2type<T>(item)+offset;
271 // T value=string2type<T>(item);
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){
275 if(transpose)
276 sampleVector.push_back(value);
277 else
278 dataVector[icol].push_back(value);
279 }
280 }
281 }
282 ++ncol;
283 if(isComment)
284 break;
285 }
286 if(verbose>1)
287 std::cout << std::endl;
288 if(verbose)
289 std::cout << "number of columns: " << ncol << std::endl;
290 // if(dataVector.back().size())
291 // assert(ncol>=cols[0]);
292 }
293 if(sampleVector.size()&&transpose)
294 dataVector.push_back(sampleVector);
295 ++nrow;
296 }
297 }
298 return dataVector.size();
299}

◆ readData() [2/2]

template<class T >
unsigned int FileReaderAscii::readData ( std::vector< T > &  dataVector,
int  col,
double  scale = 1.0,
double  offset = 0,
bool  verbose = false 
)

Definition at line 60 of file FileReaderAscii.h.

60 {
61 reset();
62 dataVector.clear();
63 int nrow=0;
64 bool withinRange=true;
65 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
66 if(verbose)
67 std::cout << "reading csv file " << m_filename << std::endl;
68 std::string csvRecord;
69 while(getline(m_ifstream,csvRecord)){//read a line
70 withinRange=true;
71 if(nrow<m_minRow)
72 withinRange=false;
73 if(m_maxRow>m_minRow)
74 if(nrow>m_maxRow)
75 withinRange=false;
76 if(withinRange){
77 std::istringstream csvstream(csvRecord);
78 std::string item;
79 int ncol=0;
80 bool isComment=false;
81 while(getline(csvstream,item,m_fs)){//read a column
82 if(verbose)
83 std::cout << item << " ";
84 size_t pos=item.find(m_comment);
85 if(pos!=std::string::npos){
86 isComment=true;
87 if(pos>0)
88 item=item.substr(0,pos-1);
89 else
90 break;
91 if(verbose)
92 std::cout << "comment found, string is " << item << std::endl;
93 }
94 if(ncol==col){
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);
98 }
99 ++ncol;
100 if(isComment)
101 break;
102 }
103 if(verbose)
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;
108 throw(ess.str());
109 }
110 }
111 ++nrow;
112 }
113 assert(dataVector.size());
114 }
115 else{//space or tab delimited fields
116 if(verbose)
117 std::cout << "space or tab delimited fields" << std::endl;
118 std::string spaceRecord;
119 while(!getline(m_ifstream, spaceRecord).eof()){
120 withinRange=true;
121 if(nrow<m_minRow)
122 withinRange=false;
123 if(m_maxRow>m_minRow)
124 if(nrow>m_maxRow)
125 withinRange=false;
126 if(withinRange){
127 if(verbose>1)
128 std::cout << spaceRecord << std::endl;
129 std::istringstream lineStream(spaceRecord);
130 std::string item;
131 int ncol=0;
132 bool isComment=false;
133 while(lineStream >> item){
134 if(verbose)
135 std::cout << item << " ";
136 // std::istringstream itemStream(item);
137 size_t pos=item.find(m_comment);
138 if(pos!=std::string::npos){
139 isComment=true;
140 if(pos>0)
141 item=item.substr(0,pos-1);
142 else
143 break;
144 if(verbose)
145 std::cout << "comment found, string is " << item << std::endl;
146 }
147 T value=scale*string2type<T>(item)+offset;
148 // T value=string2type<T>(item);
149 if(ncol==col){
150 if((value>=m_min&&value<=m_max)||m_max<=m_min)
151 dataVector.push_back(value);
152 }
153 ++ncol;
154 if(isComment)
155 break;
156 }
157 if(verbose>1)
158 std::cout << std::endl;
159 if(verbose)
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;
164 throw(ess.str());
165 }
166 }
167 ++nrow;
168 }
169 }
170 return dataVector.size();
171}

◆ reset()

void FileReaderAscii::reset ( )
inline

Definition at line 37 of file FileReaderAscii.h.

37{m_ifstream.clear();m_ifstream.seekg(0,std::ios::beg);};

◆ setComment()

void FileReaderAscii::setComment ( char  comment)
inline

Definition at line 43 of file FileReaderAscii.h.

43{m_comment=comment;};

◆ setFieldSeparator()

void FileReaderAscii::setFieldSeparator ( const char &  fieldseparator)
inline

Definition at line 40 of file FileReaderAscii.h.

40{m_fs=fieldseparator;};

◆ setMaxRow()

void FileReaderAscii::setMaxRow ( int  maxRow)
inline

Definition at line 42 of file FileReaderAscii.h.

42{m_maxRow=maxRow;};

◆ setMinRow()

void FileReaderAscii::setMinRow ( int  minRow)
inline

Definition at line 41 of file FileReaderAscii.h.

41{m_minRow=minRow;};

Member Data Documentation

◆ m_comment

char FileReaderAscii::m_comment
protected

Definition at line 53 of file FileReaderAscii.h.

◆ m_filename

std::string FileReaderAscii::m_filename
protected

Definition at line 50 of file FileReaderAscii.h.

◆ m_fs

char FileReaderAscii::m_fs
protected

Definition at line 52 of file FileReaderAscii.h.

◆ m_ifstream

std::ifstream FileReaderAscii::m_ifstream
protected

Definition at line 51 of file FileReaderAscii.h.

◆ m_max

double FileReaderAscii::m_max
protected

Definition at line 55 of file FileReaderAscii.h.

◆ m_maxRow

int FileReaderAscii::m_maxRow
protected

Definition at line 57 of file FileReaderAscii.h.

◆ m_min

double FileReaderAscii::m_min
protected

Definition at line 54 of file FileReaderAscii.h.

◆ m_minRow

int FileReaderAscii::m_minRow
protected

Definition at line 56 of file FileReaderAscii.h.


The documentation for this class was generated from the following files: