00001 #ifndef TIMESERIESCOMPONENT_H_ 00002 #define TIMESERIESCOMPONENT_H_ 00003 #include <vector> 00004 #include <string> 00005 #include <algorithm> 00006 #include <boost/date_time/posix_time/posix_time_types.hpp> 00007 #include <boost/operators.hpp> 00008 #include "VecMat.h" 00009 00010 namespace gplib 00011 { 00012 00013 /** \addtogroup tstools Time series analysis methods */ 00014 /* @{ */ 00015 00016 //! TimeSeriesComponent is the base storage class for all types of time series data 00017 /*! This is the base class for both MT and Seismic data, all common functionality will 00018 * be in this class. Furthermore all functions should be implemented to use this class 00019 * unless they need functionality specific to the method. 00020 * We inherit from ring_operators to provide a set of useful mathematical operators with minimum implementation 00021 */ 00022 class TimeSeriesComponent: public boost::arithmetic<boost::ring_operators< 00023 TimeSeriesComponent>, double> 00024 { 00025 private: 00026 //! The structure holding the data 00027 std::vector<double> data; 00028 //! Samplerate in Hz 00029 double samplerate; 00030 //! The name of the data (component or site or both) 00031 std::string name; 00032 //! The start of the recording 00033 boost::posix_time::ptime starttime; 00034 public: 00035 typedef std::vector<double>::iterator tdatait; 00036 //! Access for data vector, for ease of use and efficiency we return a reference 00037 std::vector<double> &GetData() 00038 { 00039 return data; 00040 } 00041 const std::vector<double> &GetData() const 00042 { 00043 return data; 00044 } 00045 //! For some methods we prefer to get the data as a ublas vector, we return a copy 00046 /*! This function is quite expensive, as the ublas object is generated on the fly and we return a copy. 00047 */ 00048 gplib::rvec GetUblasData(); 00049 //! Return samplerate in Hz 00050 double GetSamplerate() const 00051 { 00052 return samplerate; 00053 } 00054 //! Set sampling rate in Hz 00055 void SetSamplerate(const double rate) 00056 { 00057 samplerate = rate; 00058 } 00059 //! Set delta t in s 00060 void SetDt(const double dt) 00061 { 00062 samplerate = 1. / dt; 00063 } 00064 //! Return dt in s 00065 double GetDt() const 00066 { 00067 return 1. / samplerate; 00068 } 00069 //! Return name of the component 00070 std::string GetName() const 00071 { 00072 return name; 00073 } 00074 //! Modify name of the component 00075 void SetName(const std::string &n) 00076 { 00077 name = n; 00078 } 00079 #ifdef HAVEGSL 00080 //! resample to a new dt by interpolation, this method only exists when HAVEGSL is defined durign compilation 00081 void Resample(const double newdt); 00082 #endif 00083 //! Shift the start of the recording by npts points 00084 /*! if npts < 0 we cut abs(npts) from the beginning, otherwise we add npts 0s at the beginning 00085 * this will also adjust the starttime accordingly 00086 */ 00087 void ShiftStart(const int npts); 00088 //! Shift the end of the recording by npts points 00089 /*! if npts < 0 we cut abs(npts) from the end, otherwise we add npts 0s at the end 00090 */ 00091 void ShiftEnd(const int npts); 00092 // we declare the operators necessary for boost::ring_operators to generate the rest 00093 TimeSeriesComponent& operator=(const TimeSeriesComponent& source); 00094 //! Multiply each element of the time series by a constant factor 00095 TimeSeriesComponent& operator*=(const double factor); 00096 //! Devide each element of the time series by a constant number 00097 TimeSeriesComponent& operator/=(const double numerator); 00098 //! Add a constant shift to each element of the time series 00099 TimeSeriesComponent& operator+=(const double shift); 00100 //! Substract a constant shift from each element of the time series 00101 TimeSeriesComponent& operator-=(const double shift); 00102 //! Add two time series point by point and store the result in the current object 00103 TimeSeriesComponent& operator+=(const TimeSeriesComponent &other); 00104 //! Substract two time series point by point and store the result in the current object 00105 TimeSeriesComponent& operator-=(const TimeSeriesComponent &other); 00106 TimeSeriesComponent(const TimeSeriesComponent& source); 00107 TimeSeriesComponent(); 00108 virtual ~TimeSeriesComponent(); 00109 }; 00110 00111 //! We want to make this inline, so it appears in the header 00112 inline gplib::rvec TimeSeriesComponent::GetUblasData() 00113 { 00114 gplib::rvec ubvec(data.size()); 00115 std::copy(data.begin(), data.end(), ubvec.begin()); 00116 return ubvec; 00117 } 00118 /* @} */ 00119 } 00120 #endif /*TIMESERIESCOMPONENT_H_*/
1.5.8