GSE++ library: reading and writing GSE waveforms
gsexx.h
Go to the documentation of this file.
1 
46 // include guard
47 #ifndef TF_GSEXX_H_VERSION
48 
49 #define TF_GSEXX_H_VERSION \
50  "TF_GSEXX_H V1.5"
51 #define TF_GSEXX_H_CVSID \
52  "$Id$"
53 
54 // #include <libtime++.h>
55 #include<string>
56 #include<iostream>
57 
59 namespace GSE2 {
60 
61 /*----------------------------------------------------------------------*/
62 /*
63  * exception base class
64  * --------------------
65  */
66 
68 class Terror {
69  public:
71  Terror(const std::string& message): Mmessage(message) {
72  if (!silent) { std::cerr << Mmessage << std::endl; }
73  }
75  const std::string& message() const { return(Mmessage); }
77  static bool silent;
78  private:
80  std::string Mmessage;
81 }; // Terror
82 
83 /*======================================================================*/
84 
86 namespace waveform {
87 
89 typedef int intT;
90 
93 {
100 };
101 
102 /*======================================================================*/
103 /*
104  * all stuff for WID2 lines
105  * ------------------------
106  */
107 
111 struct TWID2 {
112  public:
114  static const char* const GSEID;
116  TWID2() { this->defaults(); }
118  std::string subformat() const;
120  std::string line() const;
122  void read(std::istream& is);
124  void defaults();
126  void setsubformat(const std::string& key);
128  double seconds() const;
130  void seconds(const double& s);
131  public:
132  int Fyear;
133  int Fmonth;
134  int Fday;
135  int Fhour;
136  int Fminute;
137  // double Fseconds; //!< seconds of time
138  std::string Fstation;
139  std::string Fchannel;
140  std::string Fauxid;
142  int Fsamps;
143  double Fsamprate;
144  double Fcalib;
145  double Fcalper;
146  std::string Finstype;
147  double Fhang;
148  double Fvang;
149  int Fmilsec;
151 }; // struct TWID2
152 
153 /*======================================================================*/
154 /*
155  * all stuff for the GSE2.1 STA2 line
156  * ----------------------------------
157  */
158 
160 class TSTA2 {
161  public:
163  static const char* const GSEID;
164 }; // class TSTA2
165 
166 /*======================================================================*/
167 /*
168  * all stuff for checksums
169  * -----------------------
170  */
171 
173 class TCHK2 {
174  friend std::istream& operator>>(std::istream&, TCHK2& chk2);
175  public:
177  TCHK2(): Msum(0) { }
179  void add(const intT& value);
181  intT value() const { return(Msum > 0 ? Msum : -Msum); }
183  std::string write() const;
185  void read(std::istream& is);
187  static const char* const GSEID;
188  private:
190  void set(const intT& value) { Msum=value; }
192 }; // class TCHK2
193 
194 /*======================================================================*/
195 /*
196  * modules to apply and remove differnces must be declared here
197  * ------------------------------------------------------------
198  * the full stuff must be provided here, since we want to use the classes
199  * as member types in the TDAT2 compression classes.
200  */
201 
203 namespace differences {
204 
206 class Tapply_diff {
207  public:
209  Tapply_diff(): Mxold(0) { }
211  int operator()(const intT& x)
212  {
213  intT xold=Mxold;
214  Mxold=x;
215  return(x-xold);
216  }
218  intT previous_value() const { return(Mxold); }
219  private:
222 }; // Tapply_diff
223 
224 
226 class Tremove_diff {
227  public:
229  Tremove_diff(): Mxold(0) { }
231  intT operator()(const intT& x)
232  { return(Mxold=Mxold+x); }
234  intT previous_value() const { return(Mxold); }
235  private:
238 }; // Tremove_diff
239 
241 template<int n, class OP>
243  public:
245  intT operator()(const intT& val) { return(Mop(Mdiff(val))); }
247  void report_status(std::ostream& os) const
248  {
249  Mdiff.report_status(os);
250  os << "operator of order " << n
251  << " holds value: " << Mop.previous_value() << std:: endl;
252  }
253  private:
255  OP Mop;
258 }; // Tdiff_operator<n, OP>
259 
261 template<class OP>
262 class Tdiff_operator<0, OP> {
263  public:
265  intT operator()(const intT& val) { return(val); }
267  void report_status(std::ostream& os) const
268  {
269  os << "operator of order 0 just returns values" << std::endl;
270  }
271 }; // Tdiff_operator<0, OP>
272 
273 } // namespace differences
274 
276 typedef differences::Tdiff_operator<1, differences::Tapply_diff>
287 
288 /*======================================================================*/
289 /*
290  * All stuff for DAT2 reading and writing
291  * --------------------------------------
292  */
293 
295 class TDAT2sum {
296  public:
298  const TCHK2& checksum() const { return(Mchecksum); }
300  intT nread() const { return(Mnsamp); }
302  intT msamp() const { return(Mmsamp); }
304  bool hot() const { return((Mnsamp<Mmsamp)); }
306  static const char* const GSEID;
307  protected:
309  TDAT2sum(const intT& msamp): Mnsamp(0), Mmsamp(msamp) { }
311  void add(const intT& value);
312  private:
319 }; // TDAT2sum
320 
321 // inline add function
322 inline void TDAT2sum::add(const intT& value)
323 {
324  Mchecksum.add(value);
325  ++Mnsamp;
326 }
327 
328 /*----------------------------------------------------------------------*/
329 
331 class TDAT2read: public TDAT2sum
332 {
333  public:
335  virtual ~TDAT2read() { }
337  intT operator()(std::istream& is);
338  protected:
341  TDAT2sum(msamp) { }
342  private:
344  virtual intT convert(std::istream& is) = 0;
345  protected:
346 }; // TDAT2read
347 
348 /*----------------------------------------------------------------------*/
349 
351 class TDAT2write: public TDAT2sum
352 {
353  public:
355  virtual ~TDAT2write() { }
357  std::string operator()(const intT& value);
358  protected:
360  TDAT2write(const intT& msamp, const intT& linelength):
361  TDAT2sum(msamp), Mlinelength(linelength), Mcpos(0) { }
362  private:
364  virtual std::string convert(const intT& value) = 0;
365  protected:
370 }; // TDAT2write
371 
372 /*----------------------------------------------------------------------*/
373 
375 class TDAT2readCM6: public TDAT2read
376 {
377  public:
379  virtual ~TDAT2readCM6() { }
382  TDAT2read(msamp) { }
383  private:
385  intT convert(std::istream& is);
386  private:
389 }; // TDAT2readCM6
390 
391 /*----------------------------------------------------------------------*/
392 
395 {
396  public:
398  virtual ~TDAT2writeCM6() { }
400  TDAT2writeCM6(const intT& msamp, const intT& linelength=80):
401  TDAT2write(msamp, linelength) { }
402  private:
404  std::string convert(const intT& value);
405  private:
408 }; // TDAT2writeCM6
409 
410 /*======================================================================*/
411 
412 } // namespace waveform
413 
414 /*======================================================================*/
415 /*
416  * GSE2 helper functions
417  * ---------------------
418  */
419 
421 template<class C>
422 bool GSEIDmatch(const std::string& line)
423 { return(line.substr(0,4)==std::string(C::GSEID)); }
424 
425 } // namespace GSE2
426 
427 #endif // TF_GSEXX_H_VERSION (includeguard)
428 
429 /* ----- END OF gsexx.h ----- */
static const char *const GSEID
GSE line idetifier.
Definition: gsexx.h:187
std::string Finstype
instrument type
Definition: gsexx.h:146
std::string Fchannel
FDSN channel code.
Definition: gsexx.h:139
virtual intT convert(std::istream &is)=0
get from stream: user must define
TCHK2()
Default constructor.
Definition: gsexx.h:177
double seconds() const
provide seconds in GSE specific floating point format
Definition: gsexx_TWID2.cc:144
Template to apply or remove differences of any order n.
Definition: gsexx.h:242
intT operator()(const intT &x)
integrate by adding this value to the previous returned
Definition: gsexx.h:231
virtual ~TDAT2write()
provide explicit virtual destructor
Definition: gsexx.h:355
intT Mmsamp
total number of samples to read
Definition: gsexx.h:318
Derived class for writing CM6 subformat data.
Definition: gsexx.h:394
intT convert(std::istream &is)
get from stream: user must define
Definition: gsexx_TDAT2.cc:266
double Fcalib
calibration factor
Definition: gsexx.h:144
Elementary operator to calculate differences.
Definition: gsexx.h:206
const std::string & message() const
return error message
Definition: gsexx.h:75
TDAT2readCM6(const intT &msamp)
constructor of implementation class
Definition: gsexx.h:381
int intT
All GSE2 waveform data is based on 4 byte integers.
Definition: gsexx.h:89
double Fsamprate
sampling rate (Hz)
Definition: gsexx.h:143
differences::Tdiff_operator< 2, differences::Tremove_diff > remove2nddiffT
Operator to remove second differences.
Definition: gsexx.h:286
int Fhour
hour of time
Definition: gsexx.h:135
static const char *const GSEID
GSE line idetifier.
Definition: gsexx.h:306
std::string convert(const intT &value)
put to stream: user must define
Definition: gsexx_TDAT2.cc:308
std::string write() const
write CHK2 line to string
TDAT2writeCM6(const intT &msamp, const intT &linelength=80)
constructor of implementation class
Definition: gsexx.h:400
static const char *const GSEID
GSE line idetifier.
Definition: gsexx.h:114
int operator()(const intT &x)
return difference to previous value and remember this
Definition: gsexx.h:211
Esubformat
Possible subformats of waveform data.
Definition: gsexx.h:92
int Fminute
minute of time
Definition: gsexx.h:136
void setsubformat(const std::string &key)
set subformat from key string
Definition: gsexx_TWID2.cc:66
virtual ~TDAT2writeCM6()
provide explicit virtual destructor
Definition: gsexx.h:398
Handle checksum and sample count.
Definition: gsexx.h:295
intT Mnsamp
number of samples read
Definition: gsexx.h:316
TDAT2write(const intT &msamp, const intT &linelength)
constructor to be called from implementation class
Definition: gsexx.h:360
void read(std::istream &is)
read CHK2 line from istream
virtual ~TDAT2readCM6()
provide explicit virtual destructor
Definition: gsexx.h:379
int Fyear
year of date
Definition: gsexx.h:132
Terror(const std::string &message)
pass error message
Definition: gsexx.h:71
bool GSEIDmatch(const std::string &line)
Check GSE identifier at beginning of line.
Definition: gsexx.h:422
All stuff defined by the GSE2 standard.
intT previous_value() const
return diff value on request (for debugging)
Definition: gsexx.h:234
TDAT2sum(const intT &msamp)
only derived class should create an instance
Definition: gsexx.h:309
friend std::istream & operator>>(std::istream &, TCHK2 &chk2)
int Fmonth
month of date
Definition: gsexx.h:133
void defaults()
set the values to defaults
Definition: gsexx_TWID2.cc:156
differences::Tdiff_operator< 2, differences::Tapply_diff > apply2nddiffT
Operator to apply second differences.
Definition: gsexx.h:280
intT operator()(std::istream &is)
get another value from the stream
Definition: gsexx_TDAT2.cc:122
A class to hold and manage the WID2-line. This is a struct - because it is a simple collection of fie...
Definition: gsexx.h:111
intT Mxold
previous returned value
Definition: gsexx.h:237
differences::Tdiff_operator< 1, differences::Tapply_diff > apply1stdiffT
Operator to apply first differences.
Definition: gsexx.h:277
void add(const intT &value)
count a sample
Definition: gsexx.h:322
intT msamp() const
return total number of samples to read
Definition: gsexx.h:302
static const char *const GSEID
GSE line idetifier.
Definition: gsexx.h:163
differences::Tdiff_operator< 1, differences::Tremove_diff > remove1stdiffT
Operator to remove first differences.
Definition: gsexx.h:283
bool hot() const
return true if not all samples are processed
Definition: gsexx.h:304
Abstract base class for writing GSE2 waveform data.
Definition: gsexx.h:351
virtual ~TDAT2read()
provide explicit virtual destructor
Definition: gsexx.h:335
void add(const intT &value)
Add a value to the checksum.
Definition: gsexx_TCHK2.cc:77
intT Mcpos
character position in line
Definition: gsexx.h:369
Tapply_diff()
initialize (set previous value to zero)
Definition: gsexx.h:209
intT operator()(const intT &val)
apply elementary operator to result of operator of next lower order
Definition: gsexx.h:245
Base class for all exceptions in this module.
Definition: gsexx.h:68
TDAT2read(const intT &msamp)
constructor to be called from implementation class
Definition: gsexx.h:340
Esubformat Fsubformat
GSE2 waveform subformat.
Definition: gsexx.h:141
std::string Fauxid
Auxiliary identification code.
Definition: gsexx.h:140
virtual std::string convert(const intT &value)=0
put to stream: user must define
int Fsamps
number of samples
Definition: gsexx.h:142
void report_status(std::ostream &os) const
report status (for debugging)
Definition: gsexx.h:267
std::string line() const
write the WID2 line
Definition: gsexx_TWID2.cc:74
std::string Mmessage
error message
Definition: gsexx.h:80
int Fday
day of date
Definition: gsexx.h:134
double Fhang
horizontal orientation
Definition: gsexx.h:147
void report_status(std::ostream &os) const
report status (for debugging)
Definition: gsexx.h:247
Derived class for reading CM6 subformat data.
Definition: gsexx.h:375
Tremove_diff()
initialize previous value to zero
Definition: gsexx.h:229
std::string Fstation
Station code.
Definition: gsexx.h:138
intT Mlinelength
linelength in output file
Definition: gsexx.h:367
const TCHK2 & checksum() const
return the checksum
Definition: gsexx.h:298
intT Mxold
previous value in stream
Definition: gsexx.h:221
TWID2()
default constructor sets default values
Definition: gsexx.h:116
intT Msum
checksum value
Definition: gsexx.h:191
std::string subformat() const
return subformat ID string
Definition: gsexx_TWID2.cc:58
intT nread() const
return the number of samples read
Definition: gsexx.h:300
Elementary operator to calculate the sum, which removes differences.
Definition: gsexx.h:226
TCHK2 Mchecksum
The checksum is handled by the base class.
Definition: gsexx.h:314
apply2nddiffT Mapplydiff
only compression formats apply differences.
Definition: gsexx.h:407
intT previous_value() const
return diff value on request (for debugging)
Definition: gsexx.h:218
void read(std::istream &is)
read a WID2 line from a stream
Definition: gsexx_TWID2.cc:99
intT operator()(const intT &val)
least order operator must copy value (for any elementary operation)
Definition: gsexx.h:265
intT value() const
Return the checksum value.
Definition: gsexx.h:181
Tdiff_operator< n-1, OP > Mdiff
next lower order operator
Definition: gsexx.h:257
A class for the cumulative calculation of checksums.
Definition: gsexx.h:173
std::string operator()(const intT &value)
write another value to the stream
Definition: gsexx_TDAT2.cc:203
double Fcalper
calibration reference period
Definition: gsexx.h:145
A class to hold and manage the STA2-line.
Definition: gsexx.h:160
double Fvang
veritcal orientation This field is required to obtain millisecond precision
Definition: gsexx.h:148
static bool silent
be silent?
Definition: gsexx.h:77
Abstract base class for reading GSE2 waveform data.
Definition: gsexx.h:331
remove2nddiffT Mremovediff
only compression formats apply differences.
Definition: gsexx.h:388