DataFile1D.cpp

Go to the documentation of this file.
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) /*throw(Tango::DevFailed)*/ {
00014         mFilename = filename;
00015         clear();
00016         // read file
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                 /*Tango::Except::throw_exception( 
00026                         static_cast<const char*>("TANGO_DEVICE_ERROR"), 
00027                         static_cast<const char*>(err.str().c_str()),
00028                         static_cast<const char*>("DataFile1D::load"));*/
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                 /*Tango::Except::throw_exception( 
00046                         static_cast<const char*>("TANGO_DEVICE_ERROR"), 
00047                         static_cast<const char*>(err.str().c_str()),
00048                         static_cast<const char*>("DataFile2D::load"));*/
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) /*throw(Tango::DevFailed)*/ {
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                 /*Tango::Except::throw_exception( 
00071                         static_cast<const char*>("TANGO_DEVICE_ERROR"), 
00072                         static_cast<const char*>(err.str().c_str()),
00073                         static_cast<const char*>("DataFile1D::save"));*/
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 } // namespace

Generated on Tue Apr 14 09:50:27 2009 for Interpolator Library by  doxygen 1.4.5