GPLIB++
TimeSeriesComponent.h
Go to the documentation of this file.
1 #ifndef TIMESERIESCOMPONENT_H_
2 #define TIMESERIESCOMPONENT_H_
3 #include <vector>
4 #include <string>
5 #include <algorithm>
6 #include <boost/date_time/posix_time/posix_time_types.hpp>
7 #include <boost/operators.hpp>
8 #include "VecMat.h"
9 
10 namespace gplib
11  {
12 
13  /** \addtogroup tstools Time series analysis methods */
14  /* @{ */
15 
16  //! TimeSeriesComponent is the base storage class for all types of time series data
17  /*! This is the base class for both MT and Seismic data, all common functionality will
18  * be in this class. Furthermore all functions should be implemented to use this class
19  * unless they need functionality specific to the method.
20  * We inherit from ring_operators to provide a set of useful mathematical operators with minimum implementation
21  */
22  class TimeSeriesComponent: public boost::arithmetic<boost::ring_operators<
23  TimeSeriesComponent>, double>
24  {
25  private:
26  //! The structure holding the data
27  std::vector<double> data;
28  //! Samplerate in Hz
29  double samplerate;
30  //! The name of the data (component or site or both)
31  std::string name;
32  //! The start of the recording
33  boost::posix_time::ptime starttime;
34  public:
35  typedef std::vector<double>::iterator tdatait;
36  //! Access for data vector, for ease of use and efficiency we return a reference
37  std::vector<double> &GetData()
38  {
39  return data;
40  }
41  const std::vector<double> &GetData() const
42  {
43  return data;
44  }
45  //! For some methods we prefer to get the data as a ublas vector, we return a copy
46  /*! This function is quite expensive, as the ublas object is generated on the fly and we return a copy.
47  */
48  gplib::rvec GetUblasData();
49  //! Return samplerate in Hz
50  double GetSamplerate() const
51  {
52  return samplerate;
53  }
54  //! Set sampling rate in Hz
55  void SetSamplerate(const double rate)
56  {
57  samplerate = rate;
58  }
59  //! Set delta t in s
60  void SetDt(const double dt)
61  {
62  samplerate = 1. / dt;
63  }
64  //! Return dt in s
65  double GetDt() const
66  {
67  return 1. / samplerate;
68  }
69  //! Return name of the component
70  std::string GetName() const
71  {
72  return name;
73  }
74  //! Modify name of the component
75  void SetName(const std::string &n)
76  {
77  name = n;
78  }
79 #ifdef HAVEGSL
80  //! resample to a new dt by interpolation, this method only exists when HAVEGSL is defined durign compilation
81  void Resample(const double newdt);
82 #endif
83  //! Shift the start of the recording by npts points
84  /*! if npts < 0 we cut abs(npts) from the beginning, otherwise we add npts 0s at the beginning
85  * this will also adjust the starttime accordingly
86  */
87  void ShiftStart(const int npts);
88  //! Shift the end of the recording by npts points
89  /*! if npts < 0 we cut abs(npts) from the end, otherwise we add npts 0s at the end
90  */
91  void ShiftEnd(const int npts);
92  // we declare the operators necessary for boost::ring_operators to generate the rest
94  //! Multiply each element of the time series by a constant factor
95  TimeSeriesComponent& operator*=(const double factor);
96  //! Devide each element of the time series by a constant number
97  TimeSeriesComponent& operator/=(const double numerator);
98  //! Add a constant shift to each element of the time series
99  TimeSeriesComponent& operator+=(const double shift);
100  //! Substract a constant shift from each element of the time series
101  TimeSeriesComponent& operator-=(const double shift);
102  //! Add two time series point by point and store the result in the current object
104  //! Substract two time series point by point and store the result in the current object
108  virtual ~TimeSeriesComponent();
109  };
110 
111  //! We want to make this inline, so it appears in the header
113  {
114  gplib::rvec ubvec(data.size());
115  std::copy(data.begin(), data.end(), ubvec.begin());
116  return ubvec;
117  }
118  /* @} */
119  }
120 #endif /*TIMESERIESCOMPONENT_H_*/
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
TimeSeriesComponent & operator+=(const double shift)
Add a constant shift to each element of the time series.
void ShiftStart(const int npts)
Shift the start of the recording by npts points.
void ShiftEnd(const int npts)
Shift the end of the recording by npts points.
void SetSamplerate(const double rate)
Set sampling rate in Hz.
TimeSeriesComponent & operator-=(const double shift)
Substract a constant shift from each element of the time series.
TimeSeriesComponent & operator/=(const double numerator)
Devide each element of the time series by a constant number.
TimeSeriesComponent is the base storage class for all types of time series data.
std::vector< double >::iterator tdatait
std::string GetName() const
Return name of the component.
void SetDt(const double dt)
Set delta t in s.
double GetSamplerate() const
Return samplerate in Hz.
gplib::rvec GetUblasData()
For some methods we prefer to get the data as a ublas vector, we return a copy.
void SetName(const std::string &n)
Modify name of the component.
TimeSeriesComponent & operator*=(const double factor)
Multiply each element of the time series by a constant factor.
double GetDt() const
Return dt in s.
const std::vector< double > & GetData() const
TimeSeriesComponent & operator=(const TimeSeriesComponent &source)