TimeSeriesData.cpp

Go to the documentation of this file.
00001 #include "TimeSeriesData.h"
00002 #include "BirrpAsciiFormat.h"
00003 #include "MtuFormat.h"
00004 #include "CsvFormat.h"
00005 #include "LemiTsFormat.h"
00006 #include "FatalException.h"
00007 #include "convert.h"
00008 #include "Util.h"
00009 #include <iostream>
00010 
00011 using namespace std;
00012 namespace gplib
00013   {
00014     TimeSeriesData::TimeSeriesData()
00015       {
00016       }
00017 
00018     TimeSeriesData::~TimeSeriesData()
00019       {
00020       }
00021 
00022     void TimeSeriesData::GetData(std::string filename)
00023       {
00024         string ending;
00025         //we identify the type of data by the file extension
00026         ending = GetFileExtension(filename);
00027         //Phoenix MTU data can have different endings depending on the sampling frequency
00028         if (ending == ".ts3" || ending == ".TS3" || ending == ".ts4" || ending
00029             == ".TS4" || ending == ".ts5" || ending == ".TS5")
00030           {
00031             Data = boost::shared_ptr<TimeSeries>(new MtuFormat);
00032             datatype = mtu;
00033           }
00034         //asc is the format used by birrp, basically just columns of numbers
00035         else if (ending == ".asc")
00036           {
00037             Data = boost::shared_ptr<TimeSeries>(new BirrpAsciiFormat);
00038             datatype = birrp;
00039           }
00040         //comma separated values have the ending .csv
00041         else if (ending == ".csv")
00042           {
00043             Data = boost::shared_ptr<TimeSeries>(new CsvFormat);
00044             datatype = csv;
00045           }
00046         //the lemi instruments produce ascii files with additional columns
00047         else if (ending == ".lem")
00048           {
00049             Data = boost::shared_ptr<TimeSeries>(new LemiTsFormat);
00050             datatype = lemi;
00051           }
00052         else
00053           {
00054             datatype = tsunknown;
00055             throw FatalException(
00056                 "Unknown data format or file does not exist : " + filename);
00057           }
00058         name = filename;
00059         unsigned int dotpos = name.find('.', 0);
00060         if (dotpos != string::npos)
00061           name.erase(dotpos);
00062         Data->GetData(filename);
00063       }
00064 
00065     TimeSeriesData& TimeSeriesData::operator=(const TimeSeriesData& source)
00066       {
00067         if (this != &source)
00068           {
00069             this->name = source.name;
00070             this->datatype = source.datatype;
00071             //TODO introduce virtual constructors or redesign read/write mechanism
00072             this->Data = source.Data;
00073           }
00074         return *this;
00075       }
00076 
00077     void TimeSeriesData::WriteAsMtu(std::string filename_base)
00078       {
00079         if (datatype == mtu)
00080           {
00081             switch (int(Data->GetSamplerate()))
00082               {
00083             case 2400:
00084               Data->WriteData(filename_base + ".ts3");
00085               break;
00086             case 150:
00087               Data->WriteData(filename_base + ".ts4");
00088               break;
00089             case 15:
00090               Data->WriteData(filename_base + ".ts5");
00091               break;
00092             default:
00093               throw FatalException(
00094                   "Unknown samplerate ! Cannot write file. Value is: "
00095                       + stringify(Data->GetSamplerate()));
00096               break;
00097               }
00098           }
00099         else
00100           {
00101             throw FatalException("Data conversion not implemented yet ! ");
00102           }
00103       }
00104 
00105     void TimeSeriesData::WriteAsBirrp(std::string filename_base)
00106       {
00107         //return static_cast<CBirrpAsciiFormat*>(Data)->WriteData(filename_base+".asc");
00108         if (datatype == birrp)
00109           Data->WriteData(filename_base + ".asc");
00110         else
00111           {
00112             BirrpAsciiFormat BirrpData;
00113             BirrpData = *Data;
00114             BirrpData.WriteData(filename_base + ".asc");
00115           }
00116       }
00117 
00118     void TimeSeriesData::WriteAsLemi(std::string filename_base)
00119       {
00120         if (datatype == lemi)
00121           Data->WriteData(filename_base + ".lem");
00122         else
00123           {
00124             LemiTsFormat LemiData;
00125             LemiData = *Data;
00126             LemiData.WriteData(filename_base + ".lem");
00127           }
00128       }
00129 
00130     void TimeSeriesData::WriteAsCsv(std::string filename_base)
00131       {
00132         throw FatalException("Csv write not implemented yet ! ");
00133       }
00134 
00135     void TimeSeriesData::WriteBack(std::string filename_base)
00136       {
00137         switch (datatype)
00138           {
00139         case lemi:
00140           WriteAsLemi(filename_base);
00141           break;
00142         case birrp:
00143           WriteAsBirrp(filename_base);
00144           break;
00145         case mtu:
00146           WriteAsMtu(filename_base);
00147           break;
00148         case csv:
00149           WriteAsCsv(filename_base);
00150           break;
00151         default:
00152           throw FatalException("Cannot write back data ! Unknown datatype !");
00153           break;
00154           }
00155       }
00156   }

Generated on Tue May 4 16:52:15 2010 for GPLIB++ by  doxygen 1.5.8