Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

InputDataTable.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 //
00003 //  Thomas Berger (main author), Pepijn Schreinemachers, and Thorsten Arnold
00004 //
00005 //
00006 //  Hohenheim University (490d)
00007 //  Professorship for Land Use Economics in the Tropics and Subtropics
00008 //
00009 //  May not be distributed without permission by main author
00010 //
00011 //  File:       InputDataTable.h (formerly inputTable.h)
00012 //
00013 //  Contents:   Auxiliary class for reading input files
00014 //
00015 //
00016 //
00017 //------------------------------------------------------------------------------
00018 
00019 
00020 #ifndef _InputDataTable_h
00021 #define _InputDataTable_h
00022 
00023 #include <stdio.h>
00024 //#include <stdlib.h>
00025 
00026 //Arnold2006_10
00027 // Libraries C++
00028 #include <iostream>
00029 
00030 #include <fstream>   // new 2005
00031 #include <string>    // new 2005
00032 #include "MatrixDouble.h"
00033 
00034 #define _NUMDATATYPES_ 4
00035 enum DataType {typeEMPTY, typeDouble, typeString, typeInteger, typeValue, typeMAX};
00036 string getStringDataType(DataType tp);
00037 void printDataTypeToScreen();
00038 bool checkDataType(DataType tp, DataType typeExpected, string nameOfFunction);
00039 
00040 #define _NUMCOLS_ 200  // maximum number of columns
00041 #define _MAXROWS_ 1000  // maximum number of columns
00042 #define _NUM_COLINFO_ 2
00043 enum enumTableCol{colType, colCol};
00044 
00045 
00046 
00075 class classInputTable
00076 {
00077 
00078 public:
00079 
00081 
00082 
00083 
00084    // READ functions
00085    virtual void readFromFile(ifstream& s);
00086    virtual void readFromFile(char* s);
00087    virtual void readFromFile(string s);
00088 
00089 
00090    //---------------------------------------------------------------------------
00091 
00093 
00094 
00095    //  info on data types, columns, etc
00096    int rows() {return hasNumberOfRows;}
00097    int cols() {return hasNumberOfCols;}
00098 
00099    void checkDimension(int r, int cAbs, string nameOfFunction);
00100 
00101    
00102    int getStringCols(){return hasNumberOfStringCols;};
00103    int strings(){return getStringCols();};
00104    int getNumCols(DataType dt);
00105    
00106    // Returns data type by column in original data table
00107    DataType getTypeFromAbsCol(int cAbs)
00108       {return (DataType)(int)KEY.getValue(cAbs, colType);}
00109 
00110    // Returns (INTERNAL) column in storage class by column in original data table
00111    // (not to be used, as the use of internall columns might become confusing)
00112    int getInternalColFromAbsCol(int cAbs)
00113       {return (int)KEY.getValue(cAbs, colCol);}
00114 
00115    // Returns column in original data table
00116    virtual int findAbsColumnByName(string varname);
00117    virtual DataType getTypeByVarname(string varname);
00118    virtual string getVariableName(int cAbs);
00120    //---------------------------------------------------------------------------
00121 
00123 
00124    //  All functions require the column in the original table
00125    virtual int getInt(int r, int colAbs);
00126    virtual double getDouble(int r, int colAbs);
00127    virtual string getString(int r, int colAbs);
00128 
00129    virtual int getInt(int r, string varname);
00130    virtual double getDouble(int r, string varname);
00131    virtual string getString(int r, string varname);
00133    //---------------------------------------------------------------------------
00134 
00136 
00137    //  All functions require the column in the original table
00138    virtual void setValue(int r, int c, double value);
00139    virtual void setString(int r, int c, string  str);
00141    //---------------------------------------------------------------------------
00142 
00144 
00145    virtual void writeToFile(ofstream&);
00146    virtual void writeToFile(char* s);
00147    virtual void writeToFile(string s);
00148    void writeToFile_RowsWhereColUnequals(string fn, string nm, double valCol);
00149 
00151    //---------------------------------------------------------------------------
00152 
00154 
00155       
00157    //  info on data types, columns, etc
00158    virtual DataType getCol(string varname, MatrixDouble& matrixTarget);
00159    virtual DataType copyColumnIntoVector(string varname, MatrixDouble& matrixTarget);
00160 
00161    virtual DataType getCol(string varname, string*& stringArrayTarget);   
00162 
00163    // Column in original table
00164    virtual DataType getCol(int col, MatrixDouble& matrixTarget, int colOfMat);
00165    virtual DataType copyColumnIntoMatrix(int col, MatrixDouble& matrixTarget, int colOfMat);
00166    
00167    
00168    virtual DataType getCol(int col, MatrixDouble& matrixTarget);
00169    virtual DataType getCol(int col, string*& names);
00170    
00171    void getMatrixFromTable(int startVal, int endVal, MatrixDouble& indexValueAndColumn, MatrixDouble& monthlyWaterRequirements );
00172    
00174    void getMatrixWithStartString(string startStr, MatrixDouble& indexValueAndColumn, MatrixDouble& matrixReturned);
00175    
00177    //---------------------------------------------------------------------------
00178 
00180 
00181    void printKeyToScreen();
00182    void printAllInfo();
00183    void printStrings();
00184    void printCell(int r, int colAbs);
00185    void printCellToStream(int r, int colAbs, ofstream& out);
00186    void printRowToStream(int r, ofstream& out);
00187    void printRow(int r);
00188    void printCol(int columnAbsolute);
00189    void printCol(string varn);
00190    void printVarNames();
00191    void printVarNamesToStream(ofstream& out);
00192    void printTypesToStream(ofstream& out);
00193    void printTypes();
00195    //---------------------------------------------------------------------------
00196 
00198 
00199    virtual int findAllRowsWithVal(int colAbs, double val, MatrixDouble& foundVals);
00200    virtual int findAllRowsWithVal(string varNm, double val, MatrixDouble& foundVals);
00201 
00202    virtual int findAllRowsWithVal(int colAbs, int val, MatrixDouble& foundVals);
00203    virtual int findAllRowsWithVal(string varNm, int val, MatrixDouble& foundVals);
00204 
00205    
00206 
00207    virtual void shrinkKeyToActualSize();
00209    //---------------------------------------------------------------------------
00210 
00212 
00213 
00214         classInputTable(void)
00215         {       isvirgine=true;
00216       initializeEmpty();
00217         }
00218    void initializeEmpty();
00219 
00220    classInputTable(classInputTable& tab)
00221    { isvirgine=true;
00222      copyToThis(tab);
00223    }
00224 
00225    void allocate(int rows_, int numberOfcolumns_, MatrixDouble& vectorOfColumnTypes, string*& namesOfColumns);
00226    
00227     //DESTRUCTOR
00228     // calls 'deallocate()'
00229         virtual ~classInputTable();
00230 
00231    void copyToThis(classInputTable& tab);
00232 
00233    virtual void deallocate();
00234 
00235    //virtual void joinIntoThis(classInputTable& tab);
00236 
00237    // Operators
00238         virtual classInputTable& operator= (classInputTable& tab);
00240    //---------------------------------------------------------------------------
00241         
00242         
00243    
00244 private:
00245 
00246  virtual void setAllZero();
00247 
00248 protected:     //public
00250    bool isvirgine;
00251    // Number of rows, cols
00252    int hasNumberOfCols;
00253    int hasNumberOfRows;
00254    int hasNumberOfStringCols;
00255    int hasNumberOfDoubles;
00256    int hasNumberOfInts;
00257 
00258    // Storage of variable names
00259    string* varNames;    // a vector of strings
00260 
00261    // Storage of all string-entries 
00262         string** str;
00263    
00264         // Storage of value-entries in a double class "MatrixDouble"
00265         MatrixDouble mat;
00266 
00267    // KEY contains same number of columns as table.
00268    // Thus, each entry represents one column.
00269    //
00270    // KEY has two rows, specifying 
00271    //    (a) the data type of that column
00272    //    (b) the column in internal storage class
00273    // line <enumTableCol> colType : (double)DataType
00274    // line <enumTableCol> colCol  : (double)Column
00275    MatrixDouble KEY;
00276 
00277    // Copy constructors. Carefull: These protected functions do not clear memory,
00278    // thus the function <initializeEmpty> has to be called before!
00279    // Then, the number of cols and rows have to be set
00280    void copyStr(string** s);
00281    void copyVarNames(string* varNames);
00282    void setRows(int r) {hasNumberOfRows=r;}
00283    void setCols(int c) {hasNumberOfCols=c;}
00284 
00285         virtual void copyMat(MatrixDouble& m);
00286    virtual void copyKey(MatrixDouble k);
00287    virtual int getInternalColByVarname(string varname);
00288 
00289    // produces error output if dt is not integer or double
00290    virtual bool testForMatType(DataType dt); 
00291    virtual bool testForMatType(DataType dt, bool doOutput);    
00292 
00293 };
00294 
00295 string makeCapitalLetter(string s);
00296    // Append to closed / open file
00297 void appendToFileBackgroundIrrig(classInputTable& tab, MatrixDouble& foundVals, string outFile);
00298 void appendToFileBackgroundIrrig(classInputTable& tab, MatrixDouble& foundVals, ofstream& out);
00299 void writeToFileBackgroundIrrig(classInputTable& tab, MatrixDouble& foundVals, string outFile);
00300 
00301 string convertTypeAsString(DataType dt);
00302 
00303 // string StringToLower(string strToConvert);
00304 // string StringToUpper(string strToConvert);
00305 //
00306 
00307 
00308 #endif

Generated on Thu Aug 28 12:38:45 2008 for MPMAS by  doxygen 1.3.9.1