DATRW++ library: seismic data I/O with multiple formats
readtfascii.cc
Go to the documentation of this file.
1 /*! \file readtfascii.cc
2  * \brief read data obtained in ASCII (any2ascii) from T. Forbriger
3  * \brief (implementation)
4  *
5  * ----------------------------------------------------------------------------
6  *
7  * \author Daniel Armbruster
8  * \date 05/10/2010
9  *
10  * Purpose: read data obtained in ASCII (any2ascii) from T. Forbriger
11  * (implementation)
12  * ----
13  * This file is part of libdatrwxx.
14  *
15  * libdatrwxx is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * libdatrwxx is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with libdatrwxx. If not, see <http://www.gnu.org/licenses/>.
27  * ----
28  *
29  * Copyright (c) 2010 by Daniel Armbruster
30  *
31  * REVISIONS and CHANGES
32  * 05/10/2010 V0.1 Daniel Armbruster
33  *
34  * ============================================================================
35  */
36 
37 #define DATRW_READTFASCII_CC_VERSION \
38  "DATRW_READTFASCII_CC V1.0 "
39 
40 #include<cstdlib>
41 #include<sstream>
42 #include<libtime++.h>
43 #include<datrwxx/readtfascii.h>
44 #include<datrwxx/error.h>
45 
46 namespace datrw {
47 
48  namespace tfascii {
49 
50  namespace helper {
51 
52  /*---------------------------------------------------------------------*/
53  void extractvalue(std::string& line, bool unit)
54  {
55  if (unit)
56  {
57  std::string tmp = line.substr(0,line.rfind(" "));
58  line = tmp.substr(tmp.rfind(" ")+1);
59  }
60  else
61  {
62  line = line.substr(line.rfind(" ")+1);
63  }
64  } // function extractvalue
65 
66  /*----------------------------------------------------------------------*/
67  void extract(std::string& line)
68  {
69  line = line.substr(line.find(":")+1);
70  } // function extract
71 
72  } // namespace helper
73 
74  /*----------------------------------------------------------------------*/
75  const char* const streamname = "itfasciistream";
76 
77  /*----------------------------------------------------------------------*/
78  FileHeader readfileheader(std::istream& is, const bool& verbose)
79  {
80  std::string line;
81  bool hot = true;
82  FileHeader fileheader;
83  fileheader.hasfilefree = false;
84  fileheader.hassrce = false;
85 
86  while (hot && is.good())
87  {
88  getline(is, line);
89  // get SRCE line if available
90  if (line == "contents of SFF SRCE line:")
91  {
92  fileheader.hassrce = true;
93  if (verbose)
94  {
95  std::cout << "SFF SRCE line detected." << std::endl;
96  }
97  DATRW_assert(getline(is, line), "ERROR: reading file");
98  helper::extract(line);
99  fileheader.srce.type = line;
100  DATRW_assert(getline(is, line), "ERROR: reading file");
101  helper::extractvalue(line);
102  if (line == "cartesian")
103  {
104  fileheader.srce.cs = sff::CS_cartesian;
105  }
106  else
107  {
108  fileheader.srce.cs = sff::CS_spherical;
109  }
110  DATRW_assert(getline(is, line), "ERROR: reading file");
111  helper::extractvalue(line, true);
112  fileheader.srce.cx = std::atof(line.c_str());
113  DATRW_assert(getline(is, line), "ERROR: reading file");
114  helper::extractvalue(line, true);
115  fileheader.srce.cy = std::atof(line.c_str());
116  DATRW_assert(getline(is, line), "ERROR: reading file");
117  helper::extractvalue(line, true);
118  fileheader.srce.cz = std::atof(line.c_str());
119  DATRW_assert(getline(is, line), "ERROR: reading file");
120  helper::extract(line);
121  std::istringstream iss(line);
122  char c;
123  int doy, day, month, year, hour, minute, secs, micsecs;
124  iss >> doy;
125  iss >> day >> c >> month >> c >> year;
126  iss >> hour >> c >> minute >> c >> secs >> c >> micsecs;
127  fileheader.srce.date = libtime::TAbsoluteTime(year,
128  month,
129  day,
130  hour,
131  minute,
132  secs,
133  0,
134  micsecs);
135  DATRW_assert(doy == fileheader.srce.date.doy(),
136  "ERROR: inconsistent date values");
137  }
138  // get fileFREE if available
139  if (line == "contents of SFF FREE block:")
140  {
141  fileheader.hasfilefree = true;
142  if (verbose)
143  {
144  std::cout << "file FREE block detected." << std::endl;
145  }
146  while (getline(is, line))
147  {
148  // break if stream reaches a empty line
149  if (line.empty())
150  {
151  hot = false;
152  break;
153  }
154  fileheader.filefree.append(line);
155  }
156  } else
157  if (line == "file header contains no FREE block")
158  { hot = false; }
159  }
160 
161  return fileheader;
162  } // function readfileheader
163 
164  /*----------------------------------------------------------------------*/
165  TraceHeader readtraceheader(std::istream& is, const bool& verbose)
166  {
167  std::string line;
168  TraceHeader traceheader;
169  traceheader.hastracefree = false;
170  traceheader.hasinfo = false;
171  traceheader.haswid2 = false;
172 
173  while (is.good())
174  {
175  getline(is, line);
176  // get WID2 lines if available
177  if (line == "contents of SFF WID2 line:")
178  {
179  traceheader.haswid2 = true;
180  if (verbose)
181  {
182  std::cout << "SFF WID2 line detected." << std::endl;
183  }
184  DATRW_assert(getline(is, line), "ERROR: reading file");
185  helper::extract(line);
186  std::istringstream iss(line);
187  char c;
188  int doy, day, month, year, hour, minute, secs, micsecs;
189  iss >> doy;
190  iss >> day >> c >> month >> c >> year;
191  iss >> hour >> c >> minute >> c >> secs >> c >> micsecs;
192  traceheader.wid2.date = libtime::TAbsoluteTime(year,
193  month,
194  day,
195  hour,
196  minute,
197  secs,
198  0,
199  micsecs);
200  DATRW_assert(doy == traceheader.wid2.date.doy(),
201  "ERROR: inconsistent date values");
202  DATRW_assert(getline(is, line), "ERROR: reading file");
203  helper::extractvalue(line);
204  traceheader.wid2.station = line;
205  DATRW_assert(getline(is, line), "ERROR: reading file");
206  helper::extractvalue(line);
207  traceheader.wid2.channel = line;
208  DATRW_assert(getline(is, line), "ERROR: reading file");
209  helper::extractvalue(line);
210  traceheader.wid2.auxid = line;
211  DATRW_assert(getline(is, line), "ERROR: reading file");
212  helper::extractvalue(line);
213  traceheader.wid2.instype = line;
214  DATRW_assert(getline(is, line), "ERROR: reading file");
215  helper::extractvalue(line);
216  traceheader.wid2.nsamples = std::atoi(line.c_str());
217  DATRW_assert(getline(is, line), "ERROR: reading file");
218  helper::extractvalue(line, true);
219  traceheader.wid2.dt = std::atof(line.c_str());
220  DATRW_assert(getline(is, line), "ERROR: reading file");
221  helper::extractvalue(line);
222  traceheader.wid2.calib = std::atof(line.c_str());
223  DATRW_assert(getline(is, line), "ERROR: reading file");
224  helper::extractvalue(line);
225  traceheader.wid2.calper = std::atof(line.c_str());
226  DATRW_assert(getline(is, line), "ERROR: reading file");
227  helper::extractvalue(line, true);
228  traceheader.wid2.hang = std::atof(line.c_str());
229  DATRW_assert(getline(is, line), "ERROR: reading file");
230  helper::extractvalue(line, true);
231  traceheader.wid2.vang = std::atof(line.c_str());
232  }
233  // get INFO lines if available
234  if (line == "contents of SFF INFO line:")
235  {
236  traceheader.hasinfo = true;
237  if (verbose)
238  {
239  std::cout << "SFF INFO line detected." << std::endl;
240  }
241  DATRW_assert(getline(is, line), "ERROR: reading file");
242  helper::extractvalue(line);
243  if (line == "cartesian")
244  {
245  traceheader.info.cs = sff::CS_cartesian;
246  }
247  else
248  {
249  traceheader.info.cs = sff::CS_spherical;
250  }
251  DATRW_assert(getline(is, line), "ERROR: reading file");
252  helper::extractvalue(line, true);
253  traceheader.info.cx = std::atof(line.c_str());
254  DATRW_assert(getline(is, line), "ERROR: reading file");
255  helper::extractvalue(line, true);
256  traceheader.info.cy = std::atof(line.c_str());
257  DATRW_assert(getline(is, line), "ERROR: reading file");
258  helper::extractvalue(line, true);
259  traceheader.info.cz = std::atof(line.c_str());
260  DATRW_assert(getline(is, line), "ERROR: reading file");
261  helper::extractvalue(line);
262  traceheader.info.nstacks = std::atoi(line.c_str());
263  }
264  // get traceFREE block if available
265  if (line == "contents of SFF FREE block:")
266  {
267  traceheader.hastracefree = true;
268  if (verbose)
269  {
270  std::cout << "trace FREE block detected." << std::endl;
271  }
272  while (getline(is, line))
273  {
274  // break if stream reaches a empty line
275  if (line.empty()) { break; }
276  traceheader.tracefree.append(line);
277  }
278  }
279  // break if stream reaches data
280  if (line == "data:") { break; }
281  }
282 
283  return traceheader;
284  } // function readtraceheader
285 
286  /*----------------------------------------------------------------------*/
287  void help(std::ostream& os)
288  {
289  using std::endl;
290  os << endl;
291  os << "TFASCII data reading functions" << endl;
292  os << "------------------------------" << endl;
293  os << endl;
294  os << "This data reading module implements reading functions" << endl
295  << "for the output data of Thomas Forbriger's any2ascii.cc" << endl
296  << "program. As the name of this program already says its" << endl
297  << "output data are human readable ASCII files."<< endl
298  << "If you wish to read an output file of any2ascii.cc just" << endl
299  << "remember that files can be read that were generated " << endl
300  << "without the [-tcol] option, because the reading module can" << endl
301  << "not handle with an additional time column in the input" << endl
302  << "data." << endl
303  << endl
304  << "IMPORTANT NOTE: The tfascii reading module just relies as" << endl
305  << "in many cases on the input following the tfascii" << endl
306  << "format. The code does not contain any plausibility" << endl
307  << "checks." << endl;
308  os << endl;
309  } // function help
310 
311  } // namespace tfascii
312 
313 } // namespace datrw
314 
315 /* ----- END OF readtfascii.cc ----- */
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
FileHeader readfileheader(std::istream &is, const bool &verbose)
read the complete fileheader
Definition: readtfascii.cc:78
holds the traceheader data
Definition: readtfascii.h:106
const char *const streamname
name of the stream
Definition: readtfascii.cc:75
void extract(std::string &line)
splits a given string after the colon
Definition: readtfascii.cc:67
TraceHeader readtraceheader(std::istream &is, const bool &verbose)
read the complete traceheader
Definition: readtfascii.cc:165
exception class declaration for libdatrwxx (prototypes)
Root namespace of library.
Definition: aalibdatrwxx.cc:16
holds the fileheader data
Definition: readtfascii.h:98
void help(std::ostream &os)
function to print online help
Definition: readtfascii.cc:287
void extractvalue(std::string &line, bool unit)
split given string
Definition: readtfascii.cc:53