00001 #include "DataFile2D.h"
00002 #include "iostream"
00003
00004 namespace ICLIB {
00005
00006 DataFile2D::DataFile2D() {
00007 mFilename = "<unknown filename>";
00008 }
00009
00010 DataFile2D::~DataFile2D() {
00011 }
00012
00013 void DataFile2D::load(const std::string& filename) {
00014 mFilename = filename;
00015 clear();
00016 std::ifstream file(filename.c_str());
00017 if(!file) {
00018 std::ostringstream err;
00019 err << "could not open file " << filename << " for reading";
00020 throw FileNotFoundException(filename,
00021 "DataFile2D::load",
00022 __FILE__,
00023 __LINE__);
00024
00025
00026
00027
00028 }
00029 std::string line;
00030 std::vector< std::string > keys;
00031 if(std::getline(file,line)) {
00032 std::istringstream iline(line);
00033 std::string key;
00034 while(std::getline(iline,key,','))
00035 keys.push_back(key);
00036 }
00037 if(keys.size()!=3) {
00038 std::ostringstream err;
00039 err << "error while parsing file " << filename << " : invalid header";
00040 throw ParseException ( filename,
00041 "DataFile2D::load",
00042 __FILE__,
00043 __LINE__);
00044
00045
00046
00047
00048 }
00049
00050 if(std::getline(file,line)) {
00051
00052 std::istringstream iline(line);
00053
00054 double tmp;
00055 Values xdata;
00056 if(iline >> tmp)
00057 {
00058
00059 xdata.push_back(tmp);
00060 while(iline >> tmp)
00061 {
00062
00063 xdata.push_back(tmp);
00064 }
00065 }
00066 Values ydata;
00067 Values zdata;
00068
00069 while(std::getline(file,line)) {
00070
00071 std::istringstream iline(line);
00072
00073 double tmp;
00074 if(iline >> tmp)
00075 {
00076
00077 ydata.push_back(tmp);
00078 while(iline >> tmp)
00079 {
00080
00081 zdata.push_back(tmp);
00082 }
00083 }
00084 }
00085 setValues(keys[0],xdata);
00086 setValues(keys[1],ydata);
00087 setValues(keys[2],zdata);
00088 }
00089 }
00090
00091 void DataFile2D::save(const std::string& filename) {
00092 std::ofstream file(filename.c_str(),ios::out);
00093 if(!file) {
00094 std::ostringstream err;
00095 err << "could not open file " << filename << " for writing";
00096 throw FileNotFoundException(filename,
00097 "DataFile2D::save",
00098 __FILE__,
00099 __LINE__);
00100
00101
00102
00103
00104 }
00105 if(mValues.size()==3) {
00106
00107 ValuesMap::iterator it = mValues.begin();
00108 file << it->first;
00109 std::vector< double >& xdata = it->second;
00110 ++it;
00111 file << "," << it->first;
00112 std::vector< double >& ydata = it->second;
00113 ++it;
00114 file << "," << it->first;
00115 std::vector< double >& zdata = it->second;
00116 file << std::endl;
00117
00118
00119 file << "0.0\t";
00120 long x=0;
00121 while(x<xdata.size()) {
00122 file << xdata[x];
00123 if(++x<xdata.size())
00124 file << "\t";
00125 else
00126 file << std::endl;
00127 }
00128
00129
00130 for(long y=0; y<ydata.size(); ++y) {
00131 file << ydata[y] << "\t";
00132 x=0;
00133 while(x<xdata.size() && y*xdata.size()+x<zdata.size()) {
00134 file << zdata[y*xdata.size()+x];
00135 if(++x<xdata.size())
00136 file << "\t";
00137 else
00138 file << std::endl;
00139 }
00140 }
00141 }
00142 }
00143
00144 void DataFile2D::getArrayValues(vector<double>& aray,int& xdim,int& ydim) {
00145 }
00146
00147 void DataFile2D::setArrayValues(const vector<double>& aray,int xdim,int ydim) {
00148 }
00149
00150 }