00001 #include "DataFile1D.h"
00002
00003
00004 namespace ICLIB {
00005
00006 DataFile1D::DataFile1D() {
00007 mFilename = "<unknown filename>";
00008 }
00009
00010 DataFile1D::~DataFile1D() {
00011 }
00012
00013 void DataFile1D::load(const std::string& filename) {
00014 mFilename = filename;
00015 clear();
00016
00017 std::ifstream file(filename.c_str());
00018 if(!file) {
00019 std::ostringstream err;
00020 err << "could not open file " << filename << " for reading";
00021 throw FileNotFoundException(filename,
00022 "DataFile1D::load",
00023 __FILE__,
00024 __LINE__);
00025
00026
00027
00028
00029 }
00030 std::string line;
00031 std::vector< std::string > keys;
00032 if(std::getline(file,line)) {
00033 std::istringstream iline(line);
00034 std::string key;
00035 while(std::getline(iline,key,','))
00036 keys.push_back(key);
00037 }
00038 if(keys.size()<2) {
00039 std::ostringstream err;
00040 err << "error while parsing file " << filename << " : invalid header";
00041 throw ParseException ( filename,
00042 "DataFile1D::load",
00043 __FILE__,
00044 __LINE__);
00045
00046
00047
00048
00049 }
00050 int numkey = 0;
00051 while(std::getline(file,line) && numkey<keys.size()) {
00052 std::vector< double > data;
00053 std::istringstream iline(line);
00054 double tmp;
00055 while(iline >> tmp)
00056 data.push_back(tmp);
00057 setValues(keys[numkey++],data);
00058 }
00059 }
00060
00061 void DataFile1D::save(const std::string& filename) {
00062 std::ofstream file(filename.c_str(),ios::out);
00063 if(!file) {
00064 std::ostringstream err;
00065 err << "could not open file " << filename << " for writing";
00066 throw FileNotFoundException(filename,
00067 "DataFile1D::save",
00068 __FILE__,
00069 __LINE__);
00070
00071
00072
00073
00074 }
00075 ValuesMap::iterator it=mValues.begin();
00076 while(it!=mValues.end()) {
00077 file << it->first;
00078 if(++it!=mValues.end())
00079 file << ",";
00080 else
00081 file << std::endl;
00082 }
00083 for(it = mValues.begin(); it!=mValues.end(); ++it) {
00084 std::vector< double >& data = it->second;
00085 long i=0;
00086 while(i<data.size()) {
00087 file << data[i];
00088 if(++i<data.size())
00089 file << "\t";
00090 else
00091 file << std::endl;
00092 }
00093 }
00094 }
00095
00096 void DataFile1D::getArrayValues(vector<double>& aray,int& xdim,int& ydim) {
00097 xdim = 0;
00098 ydim = 0;
00099 if(mValues.size()>0) {
00100 xdim = mValues[0].second.size();
00101 ydim = mValues.size();
00102 aray.resize(xdim*ydim);
00103 for(int j=0; j<ydim; ++j) {
00104 for(int i=0; i<xdim; ++i) {
00105 aray[xdim*j+i] = mValues[j].second[i];
00106 }
00107 }
00108 }
00109 }
00110
00111 void DataFile1D::setArrayValues(const vector<double>& aray,int xdim,int ydim) {
00112 }
00113
00114 }