TS++ library: time series library
tscollection.h
Go to the documentation of this file.
1 
36 // include guard
37 #ifndef TS_TSCOLLECTION_H_VERSION
38 
39 #define TS_TSCOLLECTION_H_VERSION \
40  "TS_TSCOLLECTION_H V1.0"
41 
42 #include<vector>
43 #include<tsxx/error.h>
44 #include<tsxx/wid2timeseries.h>
45 
46 namespace ts {
47 
55  template<typename T>
57  public std::vector<TimeSeries<aff::Series<T>, ::sff::WID2> >
58  {
59  public:
64  typedef T Tvalue;
66  typedef aff::Series<Tvalue> Tseries;
67  typedef ::sff::WID2 Theader;
70  typedef std::vector<Ttimeseries> Tbase;
72 
74  bool are_consistent(const ::sff::WID2compare comparer) const;
76  bool overlap() const;
78  void synchronize_nsamples();
80  void trim_to_date();
82  void trim_to_nsamples();
83 
84  }; // class TimeSeriesCollection
85 
86  /* ---------------------------------------------------------------------- */
87 
98  template<typename T>
99  bool TimeSeriesCollection<T>::are_consistent(const ::sff::WID2compare comparer) const
100  {
101  bool retval=true;
102  if (this->size() > 1)
103  {
104  typename Tbase::const_iterator i_series=this->begin();
105  Theader refheader=i_series->header;
106  ++i_series;
107  while ((i_series != this->end()) && retval)
108  {
109  retval=comparer(refheader, i_series->header);
110  ++i_series;
111  }
112  }
113  return(retval);
114  } // bool TimeSeriesCollection<T>::are_consistent(const ::sff::WID2compare comparer) const
115 
116  /* ---------------------------------------------------------------------- */
117 
125  template<typename T>
127  {
128  bool retval=true;
129  if (this->size() > 1)
130  {
131  typename Tbase::const_iterator i_series=this->begin();
132  Theader header=i_series->header;
133  libtime::TAbsoluteTime begin=header.date;
134  libtime::TAbsoluteTime end=::sff::wid2lastsample(header);
135  ++i_series;
136  while ((i_series != this->end()) && retval)
137  {
138  header=i_series->header;
139  libtime::TAbsoluteTime thisbegin=header.date;
140  libtime::TAbsoluteTime thisend=::sff::wid2lastsample(header);
141  begin = thisbegin > begin ? thisbegin : begin;
142  end = thisend < end ? thisend : end;
143  ++i_series;
144  }
145  retval=(end >= begin);
146  }
147  return(retval);
148  } // bool TimeSeriesCollection<T>::overlap() const
149 
150  /* ---------------------------------------------------------------------- */
151 
157  template<typename T>
159  {
160  typename Tbase::iterator i_series=this->begin();
161  while (i_series != this->end())
162  {
163  const unsigned int& header_nsamples=i_series->header.nsamples;
164  unsigned int series_nsamples=i_series->size();
165  unsigned int nsamples=
166  series_nsamples < header_nsamples
167  ? series_nsamples : header_nsamples;
168  i_series->header.nsamples=nsamples;
169  i_series->setlastindex(i_series->f()+nsamples-1);
170  ++i_series;
171  }
172  } // void TimeSeriesCollection<T>::synchronize_nsamples()
173 
174  /* ---------------------------------------------------------------------- */
175 
180  template<typename T>
182  {
183  if (this->size() > 1)
184  {
185  // find lastest start date
186  typename Tbase::iterator i_series=this->begin();
187  Theader header=i_series->header;
188  libtime::TAbsoluteTime begin=header.date;
189  ++i_series;
190  while (i_series != this->end())
191  {
192  header=i_series->header;
193  libtime::TAbsoluteTime thisbegin=header.date;
194  begin = thisbegin > begin ? thisbegin : begin;
195  ++i_series;
196  }
197 
198  // adjust series and header
199  i_series=this->begin();
200  while (i_series != this->end())
201  {
202  header=i_series->header;
203  long int index_offset=wid2isample(header, begin);
204  TSXX_assert(index_offset >= 0,
205  "inconsistent header data");
206  TSXX_assert(index_offset < header.nsamples,
207  "time series does not overlap with others");
208  i_series->header.date=wid2isample(header, index_offset);
209  i_series->header.nsamples -= index_offset;
210  i_series->setfirstindex(i_series->f()+index_offset);
211  ++i_series;
212  }
213  }
214  } // void TimeSeriesCollection<T>::trim_to_date()
215 
216  /* ---------------------------------------------------------------------- */
217 
222  template<typename T>
224  {
225  this->synchronize_nsamples();
226  if (this->size() > 1)
227  {
228  // find smallest number of samples in collection
229  typename Tbase::iterator i_series=this->begin();
230  Theader header=i_series->header;
231  unsigned int nsamples=header.nsamples;
232  ++i_series;
233  while (i_series != this->end())
234  {
235  header=i_series->header;
236  unsigned int thisnsamples=header.nsamples;
237  nsamples = thisnsamples < nsamples ? thisnsamples : nsamples;
238  ++i_series;
239  }
240 
241  // adjust number of samples
242  i_series=this->begin();
243  while (i_series != this->end())
244  {
245  i_series->header.nsamples = nsamples;
246  i_series->setlastindex(i_series->f()+nsamples-1);
247  ++i_series;
248  }
249  }
250  } // void TimeSeriesCollection<T>::trim_to_nsamples()
251 
252  /* ---------------------------------------------------------------------- */
253 
254 } // namespace ts
255 
256 #endif // TS_TSCOLLECTION_H_VERSION (includeguard)
257 
258 /* ----- END OF tscollection.h ----- */
bool overlap() const
Check whether all time series have a common time window.
Definition: tscollection.h:126
bool are_consistent(const ::sff::WID2compare comparer) const
Check consistency.
Definition: tscollection.h:99
ts::TimeSeries< typename Tseries::Tcoc, Theader > Tconsttimeseries
Definition: tscollection.h:69
error handling for libtsxx (prototypes)
A class template for a list of time series with SFF header.
Definition: tscollection.h:56
Structure to hold the data samples of a series together with header information to form a time series...
Definition: tsxx.h:83
std::vector< Ttimeseries > Tbase
Definition: tscollection.h:70
All stuff in this library will be placed within namespace ts.
Definition: anyfilter.cc:43
ts::TimeSeries< Tseries, Theader > Ttimeseries
Definition: tscollection.h:68
aff::Series< Tvalue > Tseries
Definition: tscollection.h:66
#define TSXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:127
time series with WID2 header (prototypes)
void synchronize_nsamples()
Synchronize header with series.
Definition: tscollection.h:158
void trim_to_nsamples()
Trim to same number of samples.
Definition: tscollection.h:223
void trim_to_date()
Trim to same start time.
Definition: tscollection.h:181