[pktools] 63/375: introduced FileReaderAscii.h and updated pkstat.cc

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:53:58 UTC 2014


This is an automated email from the git hooks/post-receive script.

sebastic-guest pushed a commit to branch upstream-master
in repository pktools.

commit 143c64cf01bdea87fdf410ca3cd18645f1ed69a4
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Mon Feb 18 17:27:43 2013 +0100

    introduced FileReaderAscii.h and updated pkstat.cc
---
 src/apps/Makefile.am              |   2 +-
 src/apps/pkstat.cc                | 103 ++++----------------
 src/fileclasses/FileReaderAscii.h | 192 ++++++++++++++++++++++++++++++++++++++
 src/fileclasses/Makefile.am       |   2 +-
 4 files changed, 211 insertions(+), 88 deletions(-)

diff --git a/src/apps/Makefile.am b/src/apps/Makefile.am
index 92d0a68..bff0785 100644
--- a/src/apps/Makefile.am
+++ b/src/apps/Makefile.am
@@ -44,7 +44,7 @@ pkdumpimg_SOURCES = pkdumpimg.cc
 pkdumpogr_SOURCES = pkdumpogr.h pkdumpogr.cc
 pksieve_SOURCES = pksieve.cc
 pkstat_SOURCES = $(top_srcdir)/src/algorithms/Histogram.h pkstat.cc
-pkstat_LDADD = $(GSL_LIBS) $(AM_LDFLAGS)
+pkstat_LDADD = -lfileClasses $(GSL_LIBS) $(AM_LDFLAGS)
 pkstatogr_SOURCES = pkstatogr.cc
 pkegcs_SOURCES = pkegcs.cc
 pkegcs_LDADD = -lgdal $(AM_LDFLAGS) -lgdal
diff --git a/src/apps/pkstat.cc b/src/apps/pkstat.cc
index f6940ff..1467dac 100644
--- a/src/apps/pkstat.cc
+++ b/src/apps/pkstat.cc
@@ -22,6 +22,7 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include <vector>
 #include <math.h>
 #include "base/Optionpk.h"
+#include "fileclasses/FileReaderAscii.h"
 #include "algorithms/Histogram.h"
 
 using namespace std;
@@ -30,8 +31,9 @@ int main(int argc, char *argv[])
 {
   Optionpk<string> input_opt("i","input","name of the input text file","");
   Optionpk<char> fs_opt("fs","fs","field separator.",' ');
+  Optionpk<char> comment_opt("comment","comment","comment character",'#');
   Optionpk<bool> output_opt("o","output","output the selected columns",false);
-  Optionpk<short> col_opt("c", "column", "column nr, starting from 0", 0);
+  Optionpk<int> col_opt("c", "column", "column nr, starting from 0", 0);
   Optionpk<int> range_opt("r", "range", "rows to start/end reading. Use -r 1 -r 10 to read first 10 rows where first row is header. Use 0 to read all rows with no header.", 0);
   Optionpk<bool> size_opt("size","size","sample size",false);
   Optionpk<bool> mean_opt("m","mean","calculate mean value",false);
@@ -42,8 +44,8 @@ int main(int argc, char *argv[])
   Optionpk<bool> stdev_opt("stdev","stdev","calculate standard deviation",false);
   Optionpk<bool> sum_opt("s","sum","calculate sum of column",false);
   Optionpk<bool> minmax_opt("mm","minmax","calculate minimum and maximum value",false);
-  Optionpk<double> min_opt("min","min","calculate minimum value",0);
-  Optionpk<double> max_opt("max","max","calculate maximum value",0);
+  Optionpk<double> min_opt("min","min","set minimum value",0);
+  Optionpk<double> max_opt("max","max","set maximum value",0);
   Optionpk<bool> histogram_opt("hist","hist","calculate histogram",false);
   Optionpk<short> nbin_opt("bin","bin","number of bins to calculate histogram",10);
   Optionpk<bool> relative_opt("rel","relative","use percentiles for histogram to calculate histogram",false);
@@ -56,6 +58,7 @@ int main(int argc, char *argv[])
   try{
     doProcess=input_opt.retrieveOption(argc,argv);
     fs_opt.retrieveOption(argc,argv);
+    comment_opt.retrieveOption(argc,argv);
     output_opt.retrieveOption(argc,argv);
     col_opt.retrieveOption(argc,argv);
     range_opt.retrieveOption(argc,argv);
@@ -89,95 +92,23 @@ int main(int argc, char *argv[])
 
   vector< vector<double> > dataVector(col_opt.size());
   vector< vector<int> > histVector(col_opt.size());
-  ifstream dataFile;
-  if(verbose_opt[0])
-    cout << "opening file " << input_opt[0] << endl;
-  dataFile.open(input_opt[0].c_str());
 
-  int nrow=0;
-  bool withinRange=true;
-  
-  if(fs_opt[0]>' '&&fs_opt[0]<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
-  // if(input_opt[0].find(".csv")!=string::npos){
-    if(verbose_opt[0])
-      cout << "reading csv file " << input_opt[0] << endl;
-    string csvRecord;
-    while(getline(dataFile,csvRecord)){//read a line
-      withinRange=true;
-      if(nrow<range_opt[0])
-        withinRange=false;
-      if(range_opt.size()>1)
-        if(nrow>range_opt[1])
-          withinRange=false;
-      if(withinRange){
-        istringstream csvstream(csvRecord);
-        string item;
-        int ncol=0;
-        while(getline(csvstream,item,fs_opt[0])){//read a column
-          if(verbose_opt[0])
-            cout << item << " ";
-          for(int icol=0;icol<col_opt.size();++icol){
-            if(ncol==col_opt[icol]){
-              double value=atof(item.c_str());
-              if((value>=min_opt[0]&&value<=max_opt[0])||max_opt[0]<=min_opt[0])
-                dataVector[icol].push_back(value);
-            }
-          }
-          ++ncol;
-        }
-        if(verbose_opt[0])
-          cout << endl;
-        assert(ncol>=col_opt[0]);
-      }
-      ++nrow;
-    }
-    assert(dataVector.size());
-  }
-  else{//space or tab delimited fields
-    string spaceRecord;
-    while(!getline(dataFile, spaceRecord).eof()){
-      withinRange=true;
-      if(nrow<range_opt[0])
-        withinRange=false;
-      if(range_opt.size()>1)
-        if(nrow>range_opt[1])
-          withinRange=false;
-      if(withinRange){
-        if(verbose_opt[0]>1)
-          cout << spaceRecord << endl;
-        istringstream lineStream(spaceRecord);
-        string item;
-        int ncol=0;
-        while(lineStream >> item){
-          if(verbose_opt[0]>1)
-            cout << item << " ";
-          istringstream itemStream(item);
-          double value;
-          itemStream >> value;
-          for(int icol=0;icol<col_opt.size();++icol){
-            if(ncol==col_opt[icol]){
-              if((value>=min_opt[0]&&value<=max_opt[0])||max_opt[0]<=min_opt[0])
-                dataVector[icol].push_back(value);
-            }
-          }
-          ++ncol;
-        }
-        if(verbose_opt[0]>1)
-          cout << endl;
-        if(verbose_opt[0])
-          cout << "number of columns: " << ncol << endl;
-        assert(ncol>=col_opt[0]);
-      }
-      ++nrow;
-    }
-  }
+  FileReaderAscii asciiReader(input_opt[0]);
+  asciiReader.setFieldSeparator(fs_opt[0]);
+  asciiReader.setComment(comment_opt[0]);
+  asciiReader.setMinRow(range_opt[0]);
+  if(range_opt.size()>1)
+    asciiReader.setMaxRow(range_opt[1]);
+  asciiReader.readData(dataVector,col_opt);
   assert(dataVector.size());
-  dataFile.close();
   double minValue=min_opt[0];
   double maxValue=max_opt[0];
   Histogram hist;
   for(int icol=0;icol<col_opt.size();++icol){
-    assert(dataVector[icol].size());
+    if(!dataVector[icol].size()){
+      std::cerr << "Warning: dataVector[" << icol << "] is empty" << std::endl;
+      continue;
+    }
     if(size_opt[0])
       cout << "sample size column " << col_opt[icol] << ": " << dataVector[icol].size() << endl;
     if(mean_opt[0])
diff --git a/src/fileclasses/FileReaderAscii.h b/src/fileclasses/FileReaderAscii.h
new file mode 100644
index 0000000..384f558
--- /dev/null
+++ b/src/fileclasses/FileReaderAscii.h
@@ -0,0 +1,192 @@
+/**********************************************************************
+FileReaderAscii.h: class to read ASCII files using (colum based)
+Copyright (C) 2008-2013 Pieter Kempeneers
+
+This file is part of pktools
+
+pktools is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+pktools is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with pktools.  If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+#ifndef _IMGREADERASCII_H_
+#define _IMGREADERASCII_H_
+
+#include <string>
+#include <vector>
+#include <fstream>
+
+//--------------------------------------------------------------------------
+class FileReaderAscii
+{
+public:
+  FileReaderAscii(void);
+  FileReaderAscii(const std::string& filename);
+  FileReaderAscii(const std::string& filename, const char& fieldseparator);
+  ~FileReaderAscii(void);
+  void open(const std::string& filename);
+  void close(void);
+  void setFieldSeparator(const char& fieldseparator){m_fs=fieldseparator;};
+  void setMinRow(int minRow){m_minRow=minRow;};
+  void setMaxRow(int maxRow){m_maxRow=maxRow;};
+  void setComment(char comment){m_comment=comment;};
+  template<class T> unsigned int readData(vector<vector<T> > &dataVector, const vector<int> &cols);
+  protected:
+  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;
+};
+
+FileReaderAscii::FileReaderAscii(void)
+  : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(' '),m_comment('#'){
+}
+
+FileReaderAscii::FileReaderAscii(const std::string& filename)
+  : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(' '),m_comment('#'){
+  open(filename);
+}
+
+FileReaderAscii::~FileReaderAscii(void)
+{
+}
+
+void FileReaderAscii::open(const std::string& filename){
+  m_filename=filename;
+  m_ifstream.open(filename.c_str(),ios_base::in);
+  if(!(m_ifstream)){
+    string errorString;
+    errorString="Error: could not open file ";
+    errorString+=filename;
+    throw(errorString);
+  }
+}
+
+void FileReaderAscii::close(){
+  m_ifstream.close();
+  //  m_ifstream.clear();
+}
+
+template<class T> unsigned int FileReaderAscii::readData(vector<vector<T> > &dataVector, const vector<int> &cols){
+  bool verbose=false;
+  dataVector.clear();
+  dataVector.resize(cols.size());
+  int nrow=0;
+  bool withinRange=true;
+  if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
+    if(verbose)
+      cout << "reading csv file " << m_filename << endl;
+    string csvRecord;
+    while(getline(m_ifstream,csvRecord)){//read a line
+      withinRange=true;
+      if(nrow<m_minRow)
+        withinRange=false;
+      if(m_maxRow>m_minRow)
+        if(nrow>m_maxRow)
+          withinRange=false;
+      if(withinRange){
+        istringstream csvstream(csvRecord);
+        string item;
+        int ncol=0;
+        bool isComment=false;
+        while(getline(csvstream,item,m_fs)){//read a column
+          if(verbose)
+            cout << item << " ";
+          unsigned pos=item.find(m_comment);
+          if(pos!=std::string::npos){
+            if(pos>0)
+              item=item.substr(0,pos-1);
+            else
+              break;
+            if(verbose)
+              std::cout << "comment found, string is " << item << std::endl;
+            isComment=true;
+          }
+          for(int icol=0;icol<cols.size();++icol){
+            if(ncol==cols[icol]){
+              T value=string2type<T>(item);
+              if((value>=m_min&&value<=m_max)||m_max<=m_min)
+                dataVector[icol].push_back(value);
+            }
+          }
+          ++ncol;
+          if(isComment)
+            break;
+        }
+        if(verbose)
+          cout << endl;
+        if(dataVector.back().size())
+          assert(ncol>=cols[0]);
+      }
+      ++nrow;
+    }
+    assert(dataVector.size());
+  }
+  else{//space or tab delimited fields
+    if(verbose)
+      std::cout << "space or tab delimited fields" << std::endl;
+    string spaceRecord;
+    while(!getline(m_ifstream, spaceRecord).eof()){
+      withinRange=true;
+      if(nrow<m_minRow)
+        withinRange=false;
+      if(m_maxRow>m_minRow)
+        if(nrow>m_maxRow)
+          withinRange=false;
+      if(withinRange){
+        if(verbose>1)
+          cout << spaceRecord << endl;
+        istringstream lineStream(spaceRecord);
+        string item;
+        int ncol=0;
+        bool isComment=false;
+        while(lineStream >> item){
+          if(verbose)
+            cout << item << " ";
+          // istringstream itemStream(item);
+          unsigned pos=item.find(m_comment);
+          if(pos!=std::string::npos){
+            if(pos>0)
+              item=item.substr(0,pos-1);
+            else
+              break;
+            if(verbose)
+              std::cout << "comment found, string is " << item << std::endl;
+            isComment=true;
+          }
+          T value=string2type<T>(item);
+          for(int icol=0;icol<cols.size();++icol){
+            if(ncol==cols[icol]){
+              if((value>=m_min&&value<=m_max)||m_max<=m_min)
+                dataVector[icol].push_back(value);
+            }
+          }
+          ++ncol;
+          if(isComment)
+            break;
+        }
+        if(verbose>1)
+          cout << endl;
+        if(verbose)
+          cout << "number of columns: " << ncol << endl;
+        if(dataVector.back().size())
+          assert(ncol>=cols[0]);
+      }
+      ++nrow;
+    }
+  }
+  return dataVector.size();
+}
+#endif // _IMGREADERASCII_H_
diff --git a/src/fileclasses/Makefile.am b/src/fileclasses/Makefile.am
index 2c18c66..e29e5df 100644
--- a/src/fileclasses/Makefile.am
+++ b/src/fileclasses/Makefile.am
@@ -11,7 +11,7 @@ noinst_LIBRARIES = libfileClasses.a
 # where to install the headers on the system
 libfileClasses_adir = $(includedir)/fileclasses
 # the list of header files that belong to the library (to be installed later)
-libfileClasses_a_HEADERS = FileReaderLas.h
+libfileClasses_a_HEADERS = FileReaderLas.h FileReaderAscii.h
 # the sources to add to the library and to add to the source distribution
 libfileClasses_a_SOURCES = $(libfileClasses_a_HEADERS) FileReaderLas.cc
 endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/pktools.git



More information about the Pkg-grass-devel mailing list