GSE++ library: reading and writing GSE waveforms
gsexx_tests.cc
Go to the documentation of this file.
1 
33 #define TF_GSEXX_TESTS_CC_VERSION \
34  "TF_GSEXX_TESTS_CC V1.0 "
35 #define TF_GSEXX_TESTS_CC_CVSID \
36  "$Id$"
37 
38 #include<fstream>
39 #include<cmath>
40 #include<cstdlib>
41 #include<iostream>
42 #include<gsexx.h>
43 
45 
51 #define TESTCODE( ARG ) cout << #ARG << endl; ARG
52 
53 namespace GSE2 {
54 
56 
61 namespace tests {
62 
63 /*======================================================================*/
64 /*
65  * Test first and second differences
66  * ---------------------------------
67  */
68 
70 
77 void test_diff()
78 {
79  std::cout << std::endl;
80  std::cout
81  << "Test the template metaprograms that apply and remove differences."
82  << std::endl;
83 
84  // test values
85  int values[] = { 1,3,5,7,3,-2,-4,6 };
86  // number of test values
87  const int nval=8;
88  // declare differencers
91  // declare inverse operators
94  for (int i=0; i<nval; i++)
95  {
96  // apply 1st differences
97  int v1st=diff1st(values[i]);
98  // apply 2nd differences
99  int v2nd=diff2nd(values[i]);
100  // remove 1st differences
101  int v1strem=rem1st(v1st);
102  // remove 2nd differences
103  int v2ndrem=rem2nd(v2nd);
104  // print results
105  std::cout.width(3); std::cout << i << " " << "value=";
106  std::cout.width(3); std::cout << values[i] << " " << "1st diff=";
107  std::cout.width(3); std::cout << v1st << " " << "1st rem=";
108  std::cout.width(3); std::cout << v1strem << " " << "2nd diff=";
109  std::cout.width(3); std::cout << v2nd << " " << "2nd rem=";
110  std::cout.width(3); std::cout << v2ndrem << " ";
111  std::cout << std::endl;
112  }
113 } // function test_diff
114 
115 /*======================================================================*/
116 /*
117  * Test checksum class
118  * -------------------
119  */
120 
122 
126 void test_chk2()
127 {
128  std::cout << std::endl;
129  std::cout << "Test the checksum algorithm."
130  << std::endl;
131 
132  // number of test values
133  const int nval=30;
134  // declare checksum object
135  waveform::TCHK2 checksum;
136 
137  for (int j=0; j<3; j++)
138  {
139  waveform::intT val=1;
140  for (int i=0; i<nval; i++)
141  {
142  checksum.add(val);
143  // print results
144  std::cout.width(3); std::cout << j << ",";
145  std::cout.width(2);
146  std::cout << i << " " << "value=";
147  std::cout.width(11);
148  std::cout << val << " " << "checksum=";
149  std::cout.width(10); std::cout << checksum.value() << std::endl;
150  // increase sample value in powers of two
151  val*=2;
152  }
153  }
154 
155  std::cout << checksum.write();
156 } // function test_chk2
157 
158 /*======================================================================*/
159 /*
160  * Test DAT2 class CM6 subformat
161  * -----------------------------
162  */
164 {
165  std::cout << std::endl;
166  std::cout << "Test the DAT2 class with CM6 subformat."
167  << std::endl;
168  std::cout << "1. write waveform"
169  << std::endl;
170 
171  const int msamples=2000;
172  int data[msamples];
173  int indata[msamples];
174  for (int i=0; i<msamples; i++)
175  {
176  data[i]=int(1.e5*std::sin(i*2.*3.141592653*5./(msamples-35)));
177  indata[i]=0;
178  }
179 
180  GSE2::waveform::TWID2 wid2line;
181  wid2line.Fsamps=msamples;
182  wid2line.Fsamprate=1.;
183  wid2line.Fstation="BFO";
184  wid2line.Finstype="STS-2";
185  wid2line.Fchannel="BHZ";
186  std::cout << wid2line.line();
187 
188  GSE2::waveform::TDAT2writeCM6 writer(msamples);
189  int i=0;
190  while (writer.hot())
191  {
192  if (i >= msamples)
193  {
194  std::cerr << "ERROR: missed last sample!" << std::endl;
195  abort();
196  }
197  std::cout << writer(data[i]);
198  i++;
199  }
200 
201  // write to file
202  {
203  std::ofstream os("junk.dat");
204  os << wid2line.line();
205  GSE2::waveform::TDAT2writeCM6 fwriter(msamples);
206  int i=0;
207  while (fwriter.hot())
208  {
209  if (i >= msamples)
210  {
211  std::cerr << "ERROR: missed last sample!" << std::endl;
212  abort();
213  }
214  os << fwriter(data[i]);
215  i++;
216  }
217  }
218 
219  {
220  char c;
221  std::cerr << "manipulate junk.dat - if you like to..." << std::endl;
222  std::cin.get(c);
223  }
224 
225  GSE2::waveform::TWID2 newwid2line;
226  newwid2line.Fstation="STA";
227  newwid2line.Fchannel="CHA";
228  newwid2line.Fauxid="AUX";
229  newwid2line.Fyear=1;
230  newwid2line.Fmonth=2;
231  newwid2line.Fday=3;
232 
233  // read from file
234  {
235  std::ifstream is("junk.dat");
236  GSE2::waveform::TDAT2readCM6 freader(msamples);
237  newwid2line.read(is);
238  int i=0;
239  while (freader.hot())
240  {
241  if (i >= msamples)
242  {
243  std::cerr << "ERROR: missed last sample!" << std::endl;
244  abort();
245  }
246  indata[i] =freader(is);
247  i++;
248  }
249  }
250 
251  // write to second file
252  {
253  std::ofstream os("junk2.dat");
254  os << newwid2line.line();
255  GSE2::waveform::TDAT2writeCM6 fwriter(msamples);
256  int i=0;
257  while (fwriter.hot())
258  {
259  if (i >= msamples)
260  {
261  std::cerr << "ERROR: missed last sample!" << std::endl;
262  abort();
263  }
264  os << fwriter(indata[i]);
265  i++;
266  }
267  }
268 
269 } // test_dat2cm6
270 
271 } // namespace tests
272 } // namespace GSE2
273 
274 /*======================================================================*/
275 
277 
280 int main()
281 {
282  std::cout << "This is a program to test the GSE2-module components." <<
283  std::endl;
284 
286  try {
287  // test differences
289  // test checksums
291  // test dat2 subformat cm6
293  } catch (GSE2::Terror& e) {
294  std::cerr << e.message() << std::endl;
295  }
296 }
297 
298 /* ----- END OF gsexx_tests.cc ----- */
GSE++ library: read and write GSE waveform data (prototypes).
std::string Finstype
instrument type
Definition: gsexx.h:146
std::string Fchannel
FDSN channel code.
Definition: gsexx.h:139
void test_diff()
Test the template metaprograms that apply and remove differences.
Definition: gsexx_tests.cc:77
Derived class for writing CM6 subformat data.
Definition: gsexx.h:394
const std::string & message() const
return error message
Definition: gsexx.h:75
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
std::string write() const
write CHK2 line to string
void test_chk2()
Test the TCHK2 class.
Definition: gsexx_tests.cc:126
int Fyear
year of date
Definition: gsexx.h:132
All stuff defined by the GSE2 standard.
int main()
Main program calling all test functions.
Definition: gsexx_tests.cc:280
int Fmonth
month of date
Definition: gsexx.h:133
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
bool hot() const
return true if not all samples are processed
Definition: gsexx.h:304
void add(const intT &value)
Add a value to the checksum.
Definition: gsexx_TCHK2.cc:77
Base class for all exceptions in this module.
Definition: gsexx.h:68
void test_dat2cm6()
Definition: gsexx_tests.cc:163
std::string Fauxid
Auxiliary identification code.
Definition: gsexx.h:140
int Fsamps
number of samples
Definition: gsexx.h:142
std::string line() const
write the WID2 line
Definition: gsexx_TWID2.cc:74
int Fday
day of date
Definition: gsexx.h:134
Derived class for reading CM6 subformat data.
Definition: gsexx.h:375
std::string Fstation
Station code.
Definition: gsexx.h:138
void read(std::istream &is)
read a WID2 line from a stream
Definition: gsexx_TWID2.cc:99
intT value() const
Return the checksum value.
Definition: gsexx.h:181
A class for the cumulative calculation of checksums.
Definition: gsexx.h:173
static bool silent
be silent?
Definition: gsexx.h:77