DATRW++ library: seismic data I/O with multiple formats
tsoftreader.cc
Go to the documentation of this file.
1 /*! \file tsoftreader.cc
2  * \brief module to read TSOFT data (implementation)
3  * \ingroup group_tsoft
4  *
5  * ----------------------------------------------------------------------------
6  *
7  * \author Thomas Forbriger
8  * \date 16/09/2009
9  *
10  * module to read TSOFT data (implementation)
11  *
12  * Copyright (c) 2009 by Thomas Forbriger (BFO Schiltach)
13  *
14  * ----
15  * This program 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 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program 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 this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28  * ----
29  *
30  * REVISIONS and CHANGES
31  * - 16/09/2009 V1.0 Thomas Forbriger
32  * - 26/12/2012 V1.1 skip empty data lines
33  *
34  * ============================================================================
35  */
36 #define DATRW_TSOFTREADER_CC_VERSION \
37  "DATRW_TSOFTREADER_CC V1.1"
38 
39 #include <iostream>
40 #include <sstream>
41 #include <datrwxx/error.h>
42 #include <datrwxx/tsoftreader.h>
43 
44 using std::cout;
45 using std::endl;
46 
47 namespace datrw {
48 
49  namespace tsoft {
50 
51  const char* TSOFTID="v01.0";
52 
53  /*======================================================================*/
54  // struct Checklist
55 
57  fileid(false), timeformat(false), increment(false), channels(false),
58  units(false), undetval(false), data(false)
59  {} // Checklist::Checklist()
60 
61  /*----------------------------------------------------------------------*/
62 
63  void Checklist::report(std::ostream& os) const
64  {
65  this->reportitem(os, datrw::tsoft::tagfileid, this->fileid);
69  this->reportitem(os, datrw::tsoft::tagunits, this->units);
71  this->reportitem(os, datrw::tsoft::tagdata, this->data);
72  } // void Checklist::report(std::ostream& os) const
73 
74  /*----------------------------------------------------------------------*/
75 
76  void Checklist::reportitem(std::ostream& os,
77  const char* tag,
78  const bool& flag)
79  {
80  os << "tag " << tag << " is";
81  if (!flag) { os << " NOT"; }
82  os << " present" << std::endl;
83  } // static void Checklist::reportitem
84 
85  /*----------------------------------------------------------------------*/
86 
87  bool Checklist::allchecked() const
88  {
89  return(fileid && timeformat && increment && channels && units
90  && undetval && data);
91  } // bool Checklist::allchecked() const
92 
93  /*======================================================================*/
94  // class TSOFTfile
95 
96  void TSOFTfile::read(std::istream& is, const bool& debug)
97  {
98  int count=0;
99  bool indata=false;
100  bool inchannels=false;
101  bool inunits=false;
102  std::string inputline;
103  inputline=getDOSline(is);
104  if (debug) { std::cout << "<" << inputline << ">" << endl; }
105  while (is.good())
106  {
107  datrw::tsoft::Line theline(inputline);
108  int linelength=theline.theline().length();
109  if ((!indata) && (linelength > 0))
110  { Mfree.push_back(theline.theline()); }
111  if (theline.hastag() && (linelength > 0))
112  {
113  // reset special block processing
114  indata=false;
115  inchannels=false;
116  inunits=false;
117  count=0;
118  if (theline.thetag() == datrw::tsoft::tagdata)
119  {
120  indata=true;
121  Mchecklist.data=true;
122  if (debug)
123  { cout << "found data tag " << theline.thetag() << endl; }
124  }
125  else if (theline.thetag() == datrw::tsoft::tagchannels)
126  {
127  inchannels=true;
128  Mchecklist.channels=true;
129  if (debug)
130  { cout << "found channels tag " << theline.thetag() << endl; }
131  }
132  else if (theline.thetag() == datrw::tsoft::tagunits)
133  {
134  inunits=true;
135  Mchecklist.units=true;
136  if (debug)
137  { cout << "found units tag " << theline.thetag() << endl; }
138  }
139  else if (theline.thetag() == datrw::tsoft::tagfileid)
140  {
141  Mchecklist.fileid=true;
142  DATRW_assert(theline.hascontent(),
143  "missing content in tagged line");
145  "file ID is not TSOFT or not supported");
146  if (debug)
147  { cout << "found fileid tag " << theline.thetag() << endl; }
148  }
149  else if (theline.thetag() == datrw::tsoft::tagtimeformat)
150  {
151  Mchecklist.timeformat=true;
152  DATRW_assert(theline.hascontent(),
153  "missing content in tagged line");
154  DATRW_assert(theline.thecontent() == "DATETIME",
155  "time format is not supported");
156  if (debug)
157  { cout << "found timeformat tag " << theline.thetag() << endl; }
158  }
159  else if (theline.thetag() == datrw::tsoft::tagundetval)
160  {
161  Mchecklist.undetval=true;
162  DATRW_assert(theline.hascontent(),
163  "missing content in tagged line");
164  std::istringstream iss(theline.thecontent());
165  iss >> Mundetval;
166  if (debug)
167  { cout << "found undetval tag " << theline.thetag() << endl; }
168  }
169  else if (theline.thetag() == datrw::tsoft::tagincrement)
170  {
171  Mchecklist.increment=true;
172  DATRW_assert(theline.hascontent(),
173  "missing content in tagged line");
174  // interval is provided in seconds
175  std::istringstream iss(theline.thecontent());
176  double dt;
177  iss >> dt;
178  Mincrement=libtime::double2time(dt);
179  if (debug)
180  { cout << "found increment tag " << theline.thetag() << endl; }
181  }
182  }
183  else if (indata && (linelength>0))
184  {
185  if (debug)
186  cout << "go for data line #" << count+1 << endl;
188  debug);
189  ++count;
190  }
191  else if (inchannels)
192  {
193  if (debug)
194  cout << "go for info for channel #" << count+1 << endl;
195  Channelinfo& ci=Mdatacontainer[count].chinfo();
196  if (debug)
197  {
198  cout << "have access to channelinfo for channel " << count+1 << endl;
199  cout.flush();
200  }
201  ci.setchannelinfo(inputline);
202  ++count;
203  if (debug)
204  cout << "read info for channel #" << count << endl;
205  }
206  else if (inunits)
207  {
208  Channelinfo& ci=Mdatacontainer[count].chinfo();
209  ci.setunits(inputline);
210  ++count;
211  if (debug)
212  cout << "read units for channel #" << count << endl;
213  }
214  // read next line
215  inputline=getDOSline(is);
216  if (debug) { std::cout << "<" << inputline << ">" << endl; }
217  } // while (is.good())
218  // file is read
220  // check if input is complete
221  if (!Mchecklist.allchecked())
222  {
223  std::cerr << "mandatory fields in input file are missing!"
224  << std::endl;
225  Mchecklist.report(std::cerr);
226  }
228  "mandatory fields are missed");
229  } // void TSOFTfile::read(std::istream& is)
230 
231  } // namespace tsoft
232 
233 } // namespace datrw
234 
235 /* ----- END OF tsoftreader.cc ----- */
contains channel info.
Definition: tsoftdata.h:173
std::string thetag() const
return tag
Definition: tsoftdata.h:148
bool allchecked() const
check whether all are checked
Definition: tsoftreader.cc:87
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
libtime::TRelativeTime Mincrement
sampling interval
Definition: tsoftreader.h:119
const char * tagdata
TSOFT data tag.
Definition: tsoftdata.cc:54
Checklist Mchecklist
my checklist
Definition: tsoftreader.h:123
Datacontainer Mdatacontainer
store channel info
Definition: tsoftreader.h:117
contains takes one line and splits it into tag and information.
Definition: tsoftdata.h:141
void setunits(const std::string &line)
set unit info
Definition: tsoftdata.h:180
double Mundetval
store undetval
Definition: tsoftreader.h:121
libtime::TRelativeTime dt()
return sampling interval of HPMO data acquisition (i.e. 5 sec)
Definition: readhpmo.cc:83
const char * tagtimeformat
TSOFT timeformat tag.
Definition: tsoftdata.cc:58
exception class declaration for libdatrwxx (prototypes)
static void reportitem(std::ostream &os, const char *tag, const bool &flag)
Definition: tsoftreader.cc:76
void read(std::istream &is, const bool &debug=false)
read from stream
Definition: tsoftreader.cc:96
const char *const data
keywords for consistency checks
std::string thecontent() const
return content
Definition: tsoftdata.h:150
const char * tagunits
TSOFT units tag.
Definition: tsoftdata.cc:56
Tlos Mfree
store header
Definition: tsoftreader.h:115
const char * tagincrement
TSOFT increment tag.
Definition: tsoftdata.cc:59
Root namespace of library.
Definition: aalibdatrwxx.cc:16
void report(std::ostream &os) const
report check status
Definition: tsoftreader.cc:63
void setchannelinfo(const std::string &line)
set channel info
Definition: tsoftdata.cc:108
const char * tagfileid
TSOFT fileid tag.
Definition: tsoftdata.cc:57
void push_data(const std::string &line, const double &undetval, const libtime::TRelativeTime &dt, const bool &debug=false)
push a data line
Definition: tsoftdata.cc:297
void flushchannels()
flush all channels
Definition: tsoftdata.cc:329
const char * tagchannels
TSOFT channels tag.
Definition: tsoftdata.cc:55
Checklist()
default constructor
Definition: tsoftreader.cc:56
bool hascontent() const
check whether this line has a content
Definition: tsoftdata.h:154
bool hastag() const
check whether this line has a tag
Definition: tsoftdata.h:152
const char * TSOFTID
string constant to check TSOFT file id
Definition: tsoftreader.cc:51
std::string getDOSline(std::istream &is)
helper function getDOSline
Definition: tsoftdata.cc:423
const char * tagundetval
TSOFT undetval tag.
Definition: tsoftdata.cc:60
std::string theline() const
return line
Definition: tsoftdata.h:146