sfftimeseries.h 14.5 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
/*! \file sffheaders.h
 * \brief provide full sffheaders (prototypes)
3
 * \ingroup group_sffheaders
thomas.forbriger's avatar
thomas.forbriger committed
4
5
6
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
7
 * $Id$
thomas.forbriger's avatar
thomas.forbriger committed
8
9
10
11
12
13
 * \author Thomas Forbriger
 * \date 18/07/2005
 * 
 * provide full sffheaders (prototypes)
 * 
 * Copyright (c) 2005 by Thomas Forbriger (BFO Schiltach) 
thomas.forbriger's avatar
thomas.forbriger committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29
 *
thomas.forbriger's avatar
thomas.forbriger committed
30
 * ----
thomas.forbriger's avatar
thomas.forbriger committed
31
32
33
 * 
 * REVISIONS and CHANGES 
 *  - 18/07/2005   V1.0   Thomas Forbriger
thomas.forbriger's avatar
thomas.forbriger committed
34
 *  - 29/06/2006   V1.1   added constructors
thomas.forbriger's avatar
thomas.forbriger committed
35
 *  - 31/10/2007   V1.2   support trace index
36
 *  - 13/11/2012   V1.3   support libdatrwxx output functions
37
38
 *  - 29/01/2014          (thof) move file from tsxx to tsioxx;
 *                        adjust namespaces and documentation
thomas.forbriger's avatar
thomas.forbriger committed
39
40
41
42
43
 * 
 * ============================================================================
 */

// include guard
44
#ifndef TSIOXX_SFFHEADERS_H_VERSION
thomas.forbriger's avatar
thomas.forbriger committed
45

46
#define TSIOXX_SFFHEADERS_H_VERSION \
47
  "TSIOXX_SFFHEADERS_H   2014-01-29"
48
#define TSIOXX_SFFHEADERS_H_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
49
  "$Id$"
thomas.forbriger's avatar
thomas.forbriger committed
50
51
52

#include<sffxx.h>
#include<tsxx/tsxx.h>
53
#include<datrwxx/datread.h>
54
#include<datrwxx/datwrite.h>
55
#include<tfxx/rangelist.h>
thomas.forbriger's avatar
thomas.forbriger committed
56
57
58
#include<sffostream.h>
#include<vector>

59
60
/*! \brief SFF Timeseries I/O.
 *
61
 * \defgroup group_sffheaders SFF Timeseries I/O
62
 *
63
 * This interface is presented in sffheaders.h
64
 *
65
66
67
68
 * This module provides three submodules:
 *   - \ref subgroup_sffheaders_classes
 *   - \ref subgroup_sffheaders_datrwxx
 *   - \ref subgroup_sffheaders_sffxx
69
70
 */

71
namespace tsio {
thomas.forbriger's avatar
thomas.forbriger committed
72
73
74
75
76

  /*! \brief some really SFF specific compounds and functions
   */
  namespace sff {

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*! \brief A set of classes to handle complete files
 *
 * \defgroup subgroup_sffheaders_classes A set of classes to handle complete files
 * \ingroup group_sffheaders
 *
 * As a basis for time series processing, it provides the class template
 * SFFTimeSeries which inherits from ts::TimeSeries and this way is a fully
 * qualified TimeSeries class in terms of libtsxx.
 * SFFTimeSeries can be passed directly for filter modules and other modules
 * in libtsxx.
 * In contrast to the basic TimeSeries class definitions in libtsxx,
 * SFFTimeSeries is equipped with an SFF TraceHeader.
 *
 * By combining several SFFTimeSeries objects to a TraceVector, a set of
 * traces can be stored and passed along.
 * A TraceVector together with a FileHeader builds a data File.
 *
 * @{
 */

    /*! \brief hold information for a complete SFF file header
     * \ingroup group_sffheaders_classes
99
     */
thomas.forbriger's avatar
thomas.forbriger committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    class FileHeader {
      public:
        FileHeader(): Mhasfree(false), Mhassrce(false) { }
        void srce(const ::sff::SRCE& s);
        void free(const ::sff::FREE& f);
        void append(const ::sff::FREE& f);
        ::sff::FREE free() const { return Mfree; }
        ::sff::SRCE srce() const { return Msrce; }
        bool hasfree() const { return Mhasfree; }
        bool hassrce() const { return Mhassrce; }
        void read(std::istream& is, const bool& verbose=false);
      private:
        ::sff::FREE Mfree;
        ::sff::SRCE Msrce;
        bool Mhasfree;
        bool Mhassrce;
    }; // struct FileHeader

    /*----------------------------------------------------------------------*/

120
121
    /*! \brief hold information for a complete SFF trace header
     * \ingroup group_sffheaders_classes
122
     */
thomas.forbriger's avatar
thomas.forbriger committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    class TraceHeader {
      public:
        TraceHeader(): Mhasfree(false), Mhasinfo(false) { }
        void info(const ::sff::INFO& s);
        void free(const ::sff::FREE& f);
        void wid2(const ::sff::WID2& w) { Mwid2=w; }
        void append(const ::sff::FREE& f);
        ::sff::FREE free() const { return Mfree; }
        ::sff::INFO info() const { return Minfo; }
        ::sff::WID2 wid2() const { return Mwid2; }
        bool hasfree() const { return Mhasfree; }
        bool hasinfo() const { return Mhasinfo; }
        void read(std::istream& is, const bool& verbose=false);
      private:
        ::sff::FREE Mfree;
        ::sff::INFO Minfo;
        ::sff::WID2 Mwid2;
        bool Mhasfree;
        bool Mhasinfo;
    }; // class TraceHeader

    /*----------------------------------------------------------------------*/

146
147
    /*! \brief hold a full SFF trace and provide conversion
     * \ingroup group_sffheaders_classes
148
     */
thomas.forbriger's avatar
thomas.forbriger committed
149
150
151
152
153
154
155
156
157
158
159
160
161
    template<class C>
      class SFFTimeSeries: 
      public ts::TimeSeries<C,ts::sff::TraceHeader> {
        public:
          typedef ts::TimeSeries<C,ts::sff::TraceHeader> Tbase;
          typedef typename Tbase::Tvalue Tvalue;
          typedef ts::TimeSeries<C,::sff::WID2> Twid2timeseries;
          typedef ts::TimeSeries<C,ts::DTHeader<Tvalue> > Tdttimeseries;
          typedef typename Tbase::Tseries Tseries;
          typedef typename Tbase::Theader Theader;
          typedef typename Tbase::Tcoc Tcoc;
          typedef typename Tbase::Ttimeseries Ttimeseries;
          typedef typename Tbase::Tconsttimeseries Tconsttimeseries;
thomas.forbriger's avatar
thomas.forbriger committed
162
163
          SFFTimeSeries(): Tbase(), Mtraceindex(-1) { }
          SFFTimeSeries(const Tseries& s): Tbase(s), Mtraceindex(-1) { }
thomas.forbriger's avatar
thomas.forbriger committed
164
          void read(std::istream& is, const bool& verbose=false);
thomas.forbriger's avatar
thomas.forbriger committed
165
166
          int traceindex() const { return Mtraceindex; }
          void settraceindex(int ti) { Mtraceindex=ti; }
thomas.forbriger's avatar
thomas.forbriger committed
167
          operator Twid2timeseries() const
thomas.forbriger's avatar
thomas.forbriger committed
168
          { return Twid2timeseries(this->series(), this->header.wid2()); }
thomas.forbriger's avatar
thomas.forbriger committed
169
          operator Tdttimeseries() const
thomas.forbriger's avatar
thomas.forbriger committed
170
          { return Tdttimeseries(this->series(), this->header.wid2().dt); }
thomas.forbriger's avatar
thomas.forbriger committed
171
          int Mtraceindex; //!< trace index in data file
thomas.forbriger's avatar
thomas.forbriger committed
172
173
174
175
      }; // class SFFTimeSeries

    /*----------------------------------------------------------------------*/

176
177
    /*! \brief hold SFF traces with full header information
     * \ingroup group_sffheaders_classes
178
     */
thomas.forbriger's avatar
thomas.forbriger committed
179
180
181
182
183
184
185
186
187
    template<class C>
      class TraceVector: public std::vector<SFFTimeSeries<C> > {
        public:
          typedef std::vector<SFFTimeSeries<C> > Tbase;
          typedef SFFTimeSeries<C> Ttimeseries;
      }; // class TraceVector

    /*----------------------------------------------------------------------*/

188
189
190
191
    /*! \brief hold all information contained in an SFF data file
     * \ingroup group_sffheaders_classes
     *
     * Store the complete information contained in a data file
192
     */
thomas.forbriger's avatar
thomas.forbriger committed
193
194
195
196
197
198
    template<class C>
      struct File: public TraceVector<C> {
        public:
          typedef TraceVector<C> Tbase;
          typedef Tbase Ttracevector;
          typedef typename Tbase::Ttimeseries Ttimeseries;
199
          typedef ts::RangeList<int> Trangelist;
200
          void read(datrw::idatstream& is, 
thomas.forbriger's avatar
thomas.forbriger committed
201
                    const bool& verbose=false);
202
          void read(datrw::idatstream& is, 
thomas.forbriger's avatar
thomas.forbriger committed
203
204
205
206
207
                    const Trangelist& rl,
                    const bool& verbose=false);
          FileHeader fileheader;
      }; // struct File

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*!
 * @}
 */
    
/*======================================================================*/

/*! \brief Interface to libdatrwxx
 *
 * \defgroup subgroup_sffheaders_datrwxx Interface to libdatrwxx
 * \ingroup group_sffheaders
 *
 * Input/output operators for structures in \ref group_sffheaders_classes
 * to libdatrwxx input/output streams.
 * @{
 */

thomas.forbriger's avatar
thomas.forbriger committed
224
    /*======================================================================*/
225
    // libdatrwxx input operators 
thomas.forbriger's avatar
thomas.forbriger committed
226

227
228
    datrw::idatstream& operator>>(datrw::idatstream& is, FileHeader& fh);
    datrw::idatstream& operator>>(datrw::idatstream& is, TraceHeader& th);
thomas.forbriger's avatar
thomas.forbriger committed
229
230
231

    /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
232
    template<class C>
233
    datrw::idatstream& operator>>(datrw::idatstream& is, 
thomas.forbriger's avatar
thomas.forbriger committed
234
235
236
                                    SFFTimeSeries<C>& s)
    { return(is >> s.series() >> s.header); }

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    /*======================================================================*/
    // libdatrwxx output operators 

    inline
    datrw::odatstream& operator<<(datrw::odatstream& os, 
                                     const FileHeader& fh)
    { 
      if (fh.hasfree()) { os << fh.free(); }
      if (fh.hassrce()) { os << fh.srce(); }
      return os;
    }

    /*----------------------------------------------------------------------*/

    inline
    datrw::odatstream& operator<<(datrw::odatstream& os, 
                                  const TraceHeader& th)
    { 
      os << th.wid2();
      if (th.hasfree()) { os << th.free(); }
      if (th.hasinfo()) { os << th.info(); }
      return os;
    }

    /*----------------------------------------------------------------------*/

    /*! \note
     * The compiler cannot distinguish between this definition and the
     * next one.
     */
    template<class C>
    datrw::odatstream& operator<<(datrw::odatstream& os, 
                       const typename SFFTimeSeries<C>::Tconsttimeseries& s)
    { 
      typedef typename SFFTimeSeries<C>::Tconsttimeseries::Tseries Tseries;
      return(os << s.header << Tseries(s)); 
    }

    /*----------------------------------------------------------------------*/

    template<class C>
    datrw::odatstream& operator<<(datrw::odatstream& os, 
                                   const SFFTimeSeries<C>& s)
    { 
      typedef typename SFFTimeSeries<C>::Tcoc Tcoc;
      return(os << s.header << Tcoc(s)); 
    }

thomas.forbriger's avatar
thomas.forbriger committed
285
286
    /*----------------------------------------------------------------------*/

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
    template<class C>
    datrw::odatstream& operator<<(datrw::odatstream& os, 
                                   const TraceVector<C>& tv)
    { 
      typedef TraceVector<C> Ttracevector;
      for(typename Ttracevector::const_iterator i=tv.begin();
          i != tv.end(); ++i)
      { os << *i; }
      return os;
    }

    /*----------------------------------------------------------------------*/

    template<class C>
    datrw::odatstream& operator<<(datrw::odatstream& os, 
                                     const File<C>& f)
    { return(os << f.fileheader << TraceVector<C>(f)); }

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*!
 * @}
 */

/*======================================================================*/

/*! \brief Interface to libsffxx
 *
 * \defgroup subgroup_sffheaders_sffxx Interface to libsffxx
 * \ingroup group_sffheaders
 *
 * Output operators for classes in module \ref subgroup_sffheaders_classes
 * to SFFostream from libsff
 *
 * \deprecated
 *   Direct output to libsffxx through SFFostream is deprecated.
 *   Use output through libdatrwxx streams instead.
 *   Appropriate operators are presented in module 
 *   \ref subgroup_sffheaders_datrwxx
 * @{
 */
326
327
328
    /*======================================================================*/
    // sff::SFFostream output operators 

thomas.forbriger's avatar
thomas.forbriger committed
329
    template<class C>
thomas.forbriger's avatar
thomas.forbriger committed
330
331
    ::sff::SFFostream<C>& operator<<(::sff::SFFostream<C>& os, 
                                     const FileHeader& fh)
thomas.forbriger's avatar
thomas.forbriger committed
332
333
334
335
336
337
338
339
340
    { 
      if (fh.hasfree()) { os << fh.free(); }
      if (fh.hassrce()) { os << fh.srce(); }
      return os;
    }

    /*----------------------------------------------------------------------*/

    template<class C>
thomas.forbriger's avatar
thomas.forbriger committed
341
342
    ::sff::SFFostream<C>& operator<<(::sff::SFFostream<C>& os, 
                                     const TraceHeader& th)
thomas.forbriger's avatar
thomas.forbriger committed
343
344
345
346
347
348
349
350
351
    { 
      os << th.wid2();
      if (th.hasfree()) { os << th.free(); }
      if (th.hasinfo()) { os << th.info(); }
      return os;
    }

    /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
comemnt    
thomas.forbriger committed
352
353
354
355
    /*! \note
     * The compiler cannot distinguish between this definition and the
     * next one.
     */
thomas.forbriger's avatar
thomas.forbriger committed
356
357
    template<class C>
    ::sff::SFFostream<C>& operator<<(::sff::SFFostream<C>& os, 
thomas.forbriger's avatar
comemnt    
thomas.forbriger committed
358
                       const typename SFFTimeSeries<C>::Tconsttimeseries& s)
thomas.forbriger's avatar
thomas.forbriger committed
359
    { 
thomas.forbriger's avatar
comemnt    
thomas.forbriger committed
360
361
      typedef typename SFFTimeSeries<C>::Tconsttimeseries::Tseries Tseries;
      return(os << Tseries(s) << s.header); 
thomas.forbriger's avatar
thomas.forbriger committed
362
    }
thomas.forbriger's avatar
thomas.forbriger committed
363
364
365

    /*----------------------------------------------------------------------*/

366
367
    template<class C>
    ::sff::SFFostream<C>& operator<<(::sff::SFFostream<C>& os, 
thomas.forbriger's avatar
comemnt    
thomas.forbriger committed
368
                                   const SFFTimeSeries<C>& s)
369
    { 
thomas.forbriger's avatar
comemnt    
thomas.forbriger committed
370
371
      typedef typename SFFTimeSeries<C>::Tcoc Tcoc;
      return(os << Tcoc(s) << s.header); 
372
373
374
375
    }

    /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
376
377
    template<class C>
    ::sff::SFFostream<C>& operator<<(::sff::SFFostream<C>& os, 
thomas.forbriger's avatar
thomas.forbriger committed
378
                                   const TraceVector<C>& tv)
thomas.forbriger's avatar
thomas.forbriger committed
379
380
381
382
383
    { 
      typedef TraceVector<C> Ttracevector;
      for(typename Ttracevector::const_iterator i=tv.begin();
          i != tv.end(); ++i)
      { os << *i; }
thomas.forbriger's avatar
thomas.forbriger committed
384
      return os;
thomas.forbriger's avatar
thomas.forbriger committed
385
386
387
388
389
    }

    /*----------------------------------------------------------------------*/

    template<class C>
thomas.forbriger's avatar
thomas.forbriger committed
390
391
    ::sff::SFFostream<C>& operator<<(::sff::SFFostream<C>& os, 
                                     const File<C>& f)
thomas.forbriger's avatar
thomas.forbriger committed
392
393
    { return(os << f.fileheader << TraceVector<C>(f)); }

394
395
396
/*!
* @}
*/
397

thomas.forbriger's avatar
thomas.forbriger committed
398
399
400
    /*======================================================================*/
    // member functions
    
401
402
    /*! \brief Read a complete file with all traces.
     */
thomas.forbriger's avatar
thomas.forbriger committed
403
    template<class C>
404
    inline void File<C>::read(datrw::idatstream& is, const bool& verbose)
thomas.forbriger's avatar
thomas.forbriger committed
405
406
407
408
409
410
411
    {
      Trangelist rl;
      this->read(is, rl, verbose);
    } // TraceVector<C>::read
    
    /*----------------------------------------------------------------------*/

412
413
414
    
    /*! \brief Read a data file an extract selected traces.
     */
thomas.forbriger's avatar
thomas.forbriger committed
415
    template<class C>
416
      inline void File<C>::read(datrw::idatstream& is, 
thomas.forbriger's avatar
thomas.forbriger committed
417
418
419
420
421
422
423
424
                                const Trangelist& rl,
                                const bool& verbose)
    {
      is >> this->fileheader;
      int itrace=0;
      while (is.good())
      {
        ++itrace;
thomas.forbriger's avatar
thomas.forbriger committed
425
        if ((rl.size()==0) || rl.contains(itrace))
thomas.forbriger's avatar
thomas.forbriger committed
426
        {
thomas.forbriger's avatar
thomas.forbriger committed
427
          if (verbose)
thomas.forbriger's avatar
thomas.forbriger committed
428
          { std::cout << "  * read trace #" << itrace << std::endl; }
thomas.forbriger's avatar
thomas.forbriger committed
429
430
          Ttimeseries timeseries;
          is >> timeseries;
thomas.forbriger's avatar
thomas.forbriger committed
431
          timeseries.settraceindex(itrace);
thomas.forbriger's avatar
thomas.forbriger committed
432
433
434
435
          this->push_back(timeseries);
        }
        else
        {
thomas.forbriger's avatar
thomas.forbriger committed
436
          if (verbose)
thomas.forbriger's avatar
thomas.forbriger committed
437
438
          { std::cout << "    skip trace #" << itrace << std::endl; }
          is.skipseries();
thomas.forbriger's avatar
thomas.forbriger committed
439
440
441
442
443
444
        }
      }
    } // TraceVector<C>::read
    
  } // namespace sff

445
} // namespace tsio
thomas.forbriger's avatar
thomas.forbriger committed
446

447
#endif // TSIOXX_SFFHEADERS_H_VERSION (includeguard)
thomas.forbriger's avatar
thomas.forbriger committed
448
449

/* ----- END OF sffheaders.h ----- */