GPLIB++
TimeSeries.cpp
Go to the documentation of this file.
1 #include "TimeSeries.h"
2 #include <algorithm>
3 #include <numeric>
4 #include "FatalException.h"
5 #include <boost/cast.hpp>
6 #include <iostream>
7 #include <boost/date_time/posix_time/posix_time.hpp>
8 
9 using namespace std;
10 namespace gplib
11  {
12  TimeSeries::TimeSeries()
13  {
14  Ex.SetName("ex");
15  Ey.SetName("ey");
16  Hx.SetName("hx");
17  Hy.SetName("hy");
18  Hz.SetName("hz");
19  }
20 
21  TimeSeries::~TimeSeries()
22  {
23  }
24 
25  TimeSeries& TimeSeries::operator=(const TimeSeries &source)
26  {
27  if (this != &source)
28  {
29  this->Ex = source.Ex;
30  this->Ey = source.Ey;
31  this->Hx = source.Hx;
32  this->Hy = source.Hy;
33  this->Hz = source.Hz;
34  copy(source.t.begin(), source.t.end(), back_inserter(t));
35  }
36  return *this;
37  }
38 
39  TimeSeries& TimeSeries::operator*=(const double &factor)
40  {
41  this->Ex *= factor;
42  this->Ey *= factor;
43  this->Hx *= factor;
44  this->Hy *= factor;
45  this->Hz *= factor;
46  return *this;
47  }
48 
49  TimeSeries& TimeSeries::operator+=(const double &shift)
50  {
51  this->Ex += shift;
52  this->Ey += shift;
53  this->Hx += shift;
54  this->Hy += shift;
55  this->Hz += shift;
56  return *this;
57  }
58 
59  void TimeSeries::erase(const int startindex, const int endindex)
60  {
61  Ex.GetData().erase(Ex.GetData().begin() + startindex,
62  Ex.GetData().begin() + endindex);
63  Ey.GetData().erase(Ey.GetData().begin() + startindex,
64  Ey.GetData().begin() + endindex);
65  Hx.GetData().erase(Hx.GetData().begin() + startindex,
66  Hx.GetData().begin() + endindex);
67  Hy.GetData().erase(Hy.GetData().begin() + startindex,
68  Hy.GetData().begin() + endindex);
69  Hz.GetData().erase(Hz.GetData().begin() + startindex,
70  Hz.GetData().begin() + endindex);
71  t.erase(t.begin() + startindex, t.begin() + endindex);
72  }
73 
74  size_t TimeSeries::Size()
75  {
76  size_t exlength = Ex.GetData().size();
77  if (exlength != Ey.GetData().size() || exlength != Hx.GetData().size()
78  || exlength != Hy.GetData().size() || exlength
79  != Hz.GetData().size())
80  throw FatalException(
81  "Component sizes differ, unable to determine size !");
82  else
83  return exlength;
84  }
85 
86  //Here start some non-member funciton implementations
87  //! Synchronize only works for continuous data at this point
88  void Synchronize(TimeSeries &Data1, TimeSeries &Data2)
89  {
90  if (Data1.GetTime().empty() || Data2.GetTime().empty())
91  throw FatalException("No time information available !");
92  boost::posix_time::time_duration startdiff(Data1.GetTime().at(0)
93  - Data2.GetTime().at(0));
94  int offset = boost::numeric_cast<int>(startdiff.total_microseconds()
95  * Data1.GetSamplerate() / 1000000); //calculate the offset in samples
96  if (boost::numeric_cast<unsigned int>(abs(offset)) > min(
97  Data1.GetTime().size(), Data2.GetTime().size()))
98  throw FatalException("Offset too large. Cannot synchronize !");
99  if (offset > 0) //Data1 starts later
100  Data2.erase(0, offset);
101  if (offset < 0)
102  Data1.erase(0, -offset);
103  }
104  }
ttimedata t
The time associated with each data point.
Definition: TimeSeries.h:32
TimeSeriesComponent Ey
The data for the east-west electric field.
Definition: TimeSeries.h:30
virtual void GetData(const std::string filename)=0
void erase(const int startindex, const int endindex)
Erase data between startindex and endindex.
Definition: TimeSeries.cpp:59
double GetSamplerate()
The samplerate is stored in each component, we just return the samplerate of Hx assuming they are all...
Definition: TimeSeries.h:71
This class is the base class for all classes dealing with MT time series.
Definition: TimeSeries.h:14
TimeSeriesComponent Ex
The data for the north-south electric field.
Definition: TimeSeries.h:28
void Synchronize(TimeSeries &Data1, TimeSeries &Data2)
Synchronize only works for continuous data at this point.
Definition: TimeSeries.cpp:88
ttimedata & GetTime()
Definition: TimeSeries.h:55
TimeSeriesComponent Hz
The data for the vertical magnetic field.
Definition: TimeSeries.h:26
TimeSeriesComponent Hy
The data for the east-west magnetic field.
Definition: TimeSeries.h:24
TimeSeriesComponent Hx
The data for the north-south magnetic field.
Definition: TimeSeries.h:22
The basic exception class for all errors that arise in gplib.