TimeSeries.cpp

Go to the documentation of this file.
00001 #include "TimeSeries.h"
00002 #include <algorithm>
00003 #include <numeric>
00004 #include "FatalException.h"
00005 #include <boost/cast.hpp>
00006 #include <iostream>
00007 #include <boost/date_time/posix_time/posix_time.hpp>
00008 
00009 using namespace std;
00010 namespace gplib
00011   {
00012     TimeSeries::TimeSeries()
00013       {
00014         Ex.SetName("ex");
00015         Ey.SetName("ey");
00016         Hx.SetName("hx");
00017         Hy.SetName("hy");
00018         Hz.SetName("hz");
00019       }
00020 
00021     TimeSeries::~TimeSeries()
00022       {
00023       }
00024 
00025     TimeSeries& TimeSeries::operator=(const TimeSeries &source)
00026       {
00027         if (this != &source)
00028           {
00029             this->Ex = source.Ex;
00030             this->Ey = source.Ey;
00031             this->Hx = source.Hx;
00032             this->Hy = source.Hy;
00033             this->Hz = source.Hz;
00034             copy(source.t.begin(), source.t.end(), back_inserter(t));
00035           }
00036         return *this;
00037       }
00038 
00039     TimeSeries& TimeSeries::operator*=(const double &factor)
00040       {
00041         this->Ex *= factor;
00042         this->Ey *= factor;
00043         this->Hx *= factor;
00044         this->Hy *= factor;
00045         this->Hz *= factor;
00046         return *this;
00047       }
00048 
00049     TimeSeries& TimeSeries::operator+=(const double &shift)
00050       {
00051         this->Ex += shift;
00052         this->Ey += shift;
00053         this->Hx += shift;
00054         this->Hy += shift;
00055         this->Hz += shift;
00056         return *this;
00057       }
00058 
00059     void TimeSeries::erase(const int startindex, const int endindex)
00060       {
00061         Ex.GetData().erase(Ex.GetData().begin() + startindex,
00062             Ex.GetData().begin() + endindex);
00063         Ey.GetData().erase(Ey.GetData().begin() + startindex,
00064             Ey.GetData().begin() + endindex);
00065         Hx.GetData().erase(Hx.GetData().begin() + startindex,
00066             Hx.GetData().begin() + endindex);
00067         Hy.GetData().erase(Hy.GetData().begin() + startindex,
00068             Hy.GetData().begin() + endindex);
00069         Hz.GetData().erase(Hz.GetData().begin() + startindex,
00070             Hz.GetData().begin() + endindex);
00071         t.erase(t.begin() + startindex, t.begin() + endindex);
00072       }
00073 
00074     size_t TimeSeries::Size()
00075       {
00076         size_t exlength = Ex.GetData().size();
00077         if (exlength != Ey.GetData().size() || exlength != Hx.GetData().size()
00078             || exlength != Hy.GetData().size() || exlength
00079             != Hz.GetData().size())
00080           throw FatalException(
00081               "Component sizes differ, unable to determine size !");
00082         else
00083           return exlength;
00084       }
00085 
00086     //Here start some non-member funciton implementations
00087     //! Synchronize only works for continuous data at this point
00088     void Synchronize(TimeSeries &Data1, TimeSeries &Data2)
00089       {
00090         if (Data1.GetTime().empty() || Data2.GetTime().empty())
00091           throw FatalException("No time information available !");
00092         boost::posix_time::time_duration startdiff(Data1.GetTime().at(0)
00093             - Data2.GetTime().at(0));
00094         int offset = boost::numeric_cast<int>(startdiff.total_microseconds()
00095             * Data1.GetSamplerate() / 1000000); //calculate the offset in samples
00096         if (boost::numeric_cast<unsigned int>(abs(offset)) > min(
00097             Data1.GetTime().size(), Data2.GetTime().size()))
00098           throw FatalException("Offset too large. Cannot synchronize !");
00099         if (offset > 0) //Data1 starts later
00100           Data2.erase(0, offset);
00101         if (offset < 0)
00102           Data1.erase(0, -offset);
00103       }
00104   }

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