DATRW++ library: seismic data I/O with multiple formats
sactest.cc
Go to the documentation of this file.
1 /*! \file sactest.cc
2  * \brief test SAC reading functions
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 21/12/2004
8  *
9  * test SAC reading functions
10  *
11  * ----
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25  * ----
26  *
27  * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach)
28  *
29  * REVISIONS and CHANGES
30  * - 21/12/2004 V1.0 Thomas Forbriger
31  *
32  * ============================================================================
33  */
34 #define SACTEST_VERSION \
35  "SACTEST V1.0 test SAC reading functions"
36 
37 #include <iostream>
38 #include <string>
39 #include <fstream>
40 #include <tfxx/commandline.h>
41 #include <tfxx/error.h>
42 #include <datrwxx/sacread.h>
43 #include <datrwxx/bytesex.h>
44 
45 using std::cout;
46 using std::cerr;
47 using std::endl;
48 
49 struct Options {
51 }; // struct Options
52 
53 int main(int iargc, char* argv[])
54 {
55 
56  // define usage information
57  char usage_text[]=
58  {
59  SACTEST_VERSION "\n"
60  "usage: sactest file [-h] [-d]" "\n"
61  " or: sactest --help|-h" "\n"
62  };
63 
64  // define full help text
65  char help_text[]=
66  {
67  "file name of SAC file to read" "\n"
68  "\n"
69  "-h read and dump header only" "\n"
70  "-d read samples (implies -h)" "\n"
71  "\n"
72  };
73 
74  // define commandline options
75  using namespace tfxx::cmdline;
76  static Declare options[]=
77  {
78  // 0: print help
79  {"help",arg_no,"-"},
80  // 1: verbose mode
81  {"v",arg_no,"-"},
82  // 2: dump header
83  {"h",arg_no,"-"},
84  // 3: read data
85  {"d",arg_no,"-"},
86  {NULL}
87  };
88 
89  // no arguments? print usage...
90  if (iargc<2)
91  {
92  cerr << usage_text << endl;
93  exit(0);
94  }
95 
96  // collect options from commandline
97  Commandline cmdline(iargc, argv, options);
98 
99  // help requested? print full help text...
100  if (cmdline.optset(0))
101  {
102  cerr << usage_text << endl;
103  cerr << help_text << endl;
104  exit(0);
105  }
106 
107  Options opt;
108  opt.verbose=cmdline.optset(1);
109  opt.dumpheader=cmdline.optset(2);
110  opt.readdata=cmdline.optset(3);
111 
112  TFXX_assert(cmdline.extra(), "missing SAC file name");
113  std::string filename=cmdline.next();
114 
115  if (opt.verbose)
116  {
117  cout << "file to process: " << filename << endl;
118  }
119 
120  if (opt.dumpheader) {
121  if (opt.verbose)
122  {
123  cout << "dump header" << endl;
124  cout << "-----------" << endl;
125  }
126  std::ifstream ifs(filename.c_str());
128  cout << " sampling interval: " << hd.delta << " s" << endl;
129  cout << " initial value: " << hd.b << endl;
130  cout << " final value: " << hd.e << endl;
131  cout << " station latitude: " << hd.stla << " °N" << endl;
132  cout << " station longitude: " << hd.stlo << " °E" << endl;
133  cout << " station elevation: " << hd.stel << " m" << endl;
134  cout << " station depth: " << hd.stdp << " m" << endl;
135  cout << " component azimuth: " << hd.cmpaz << " °" << endl;
136  cout << " component inclination: " << hd.cmpinc << " °" << endl;
137  cout << " zero time of file, year: " << hd.nzyear << endl;
138  cout << " zero time of file, doy: " << hd.nzjday << endl;
139  cout << " zero time of file, hour: " << hd.nzhour << endl;
140  cout << " zero time of file, minute: " << hd.nzmin << endl;
141  cout << " zero time of file, second: " << hd.nzsec << endl;
142  cout << " zero time of file, millisecond: " << hd.nzmsec << endl;
143  cout << " number of samples: " << hd.npts << endl;
144  cout << " station name: " << std::string(hd.kstnm,8) << endl;
145  cout << " component name: " << std::string(hd.kcmpnm,8) << endl;
146  cout << " network name: " << std::string(hd.knetwk,8) << endl;
147  cout << " man-made event name: " << std::string(hd.khole,8) << endl;
148  cout << " type of file: " << hd.iftype << endl;
149  cout << " even sampling flag: " << hd.leven << endl;
150 
151  if (opt.readdata)
152  {
153  datrw::sac::Tseries series(hd.npts);
154  typedef char* Pchar;
155  ifs.read(Pchar(series.pointer()), hd.npts*sizeof(datrw::sac::Tvalue));
156  if (ifs.good())
157  { cout << "input stream is good after reading samples" << endl; }
158  else
159  { cout << "input stream is NOT good after reading samples!" << endl; }
160  if (opt.verbose)
161  {
162  cout << "read " << series.size() << " samples" << endl;
163  int ifi=10 > series.last() ? series.last() : 10;
164  int il=series.last()-10;
165  il=series.first() > il ? series.first() : il;
166  for (int i=series.f(); i<=ifi; ++i)
167  { cout << i << " " << series(i) << endl; }
168  cout << "." << endl << "." << endl << "." << endl;
169  for (int i=il; i<=series.last(); ++i)
170  { cout << i << " " << series(i) << endl; }
171  }
172  }
173  }
174 }
175 
176 /* ----- END OF sactest.cc ----- */
SACheader read_sac_header(std::istream &is)
read SAC header from stream
Definition: sacread.cc:45
int main(int iargc, char *argv[])
Definition: sactest.cc:53
bool readdata
Definition: sactest.cc:50
bool dumpheader
Definition: sactest.cc:50
A copy of bytesex.h from libtfxx (prototypes)
header structure for SAC binary data
Definition: sacread.h:114
bool verbose
Definition: asciitest.cc:51
aff::Series< Tvalue > Tseries
Definition: sacread.h:264
#define SACTEST_VERSION
Definition: sactest.cc:34
float Tvalue
binary SAC sample type
Definition: sacread.h:258