DATRW++ library: seismic data I/O with multiple formats
readhpmo.cc
Go to the documentation of this file.
1 /*! \file readhpmo.cc
2  * \brief read data from Walter Grossmann file format (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 31/03/2004
8  *
9  * read data from Walter Grossmann file format (implementation)
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  * - 31/03/2004 V1.0 Thomas Forbriger
31  * - 19/07/2010 Daniel Armbruster header <stdlib.h> added
32  * ============================================================================
33  */
34 #define DATRW_READHPMO_CC_VERSION \
35  "DATRW_READHPMO_CC V1.0 "
36 
37 #include <datrwxx/error.h>
38 #include <datrwxx/readhpmo.h>
39 #include<iostream>
40 #include<sstream>
41 #include <stdlib.h>
42 
43 namespace datrw {
44 
45  namespace hpmo {
46 
47  void Header::readheader(std::istream& is, const bool& verbose)
48  {
49  std::getline(is, this->Mline);
50  if (Mline.length() != 45) { throw NoHeaderException(); }
51  if (verbose) { std::cout << Mline << std::endl; }
52  std::string secstring=Mline.substr(17,2);
53  DATRW_assert((secstring=="57"), "illegal second!");
54  std::string timestring;
55  const std::string sep("/");
56  timestring=Mline.substr(6,4)+sep+Mline.substr(0,2)+sep+
57  Mline.substr(3,2)+sep+Mline.substr(11,8);
58  if (verbose) { std::cout << timestring << std::endl; }
59  Mtime=libtime::TAbsoluteTime(timestring);
60  if (verbose) { std::cout << Mtime.timestring() << std::endl; }
61  std::string timezone=Mline.substr(23,3);
62  if (verbose) { std::cout << timezone << std::endl; }
63  DATRW_assert((timezone == std::string("UTC")),
64  "unexpected time code string!");
65  std::string errorcode=Mline.substr(34,1);
66  if (verbose) { std::cout << errorcode << std::endl; }
67  Merrorflag=atoi(errorcode.c_str());
68  if (verbose) { std::cout << Merrorflag << std::endl; }
69  } // Header::readheader(std::istream& is)
70 
71 /*----------------------------------------------------------------------*/
72 
73  void Header::dump(std::ostream& os) const
74  {
75  os << "Header: " << this->headerline() << std::endl;
76  os << " " << this->time().timestring() << std::endl;
77  os << " Error flag: "
78  << datrw::hpmo::quality(this->Merrorflag) << std::endl;
79  } // void Header::dump(std::ostream& os) const
80 
81 /*======================================================================*/
82 
83  libtime::TRelativeTime dt()
84  {
85  return(libtime::double2time(datrw::hpmo::sampling_interval));
86  } // libtime::TRelativeTime dt()
87 
88 /*----------------------------------------------------------------------*/
89 
90  libtime::TRelativeTime toffset(const int& ichannel)
91  {
92  double delay=(ichannel-1)*0.06;
93  delay += 0.01;
94  if (ichannel==1) { delay += 0.04; }
95  else { delay += 0.41e-3; }
96  return(libtime::double2time(delay));
97  } // libtime::TRelativeTime toffset(const int& ichannel)
98 
99 /*----------------------------------------------------------------------*/
100 
101  void check_channel_no(const int& ichannel)
102  {
103  DATRW_assert(((ichannel > 0) && (ichannel <= nchannels)),
104  "illegal channel number!");
105  } // void check_channel_no(const int& ichannel)
106 
107 /*----------------------------------------------------------------------*/
108 
109  std::string quality(const int& flag)
110  {
111  std::string retval("unknown flag!");
112  if (flag == 0) { retval=std::string("OK"); }
113  if (flag == 1) { retval=std::string("bad DCF reception"); }
114  if (flag == 2) { retval=std::string("dummy block (2.999999 V)"); }
115  if (flag == 3) { retval=std::string("bad DCF reception and ")
116  +std::string("dummy block (2.999999 V)"); }
117  if (flag == 4) { retval=std::string("RS-232 error"); }
118  if (flag == 8) { retval=std::string("possibly error in PC clock"); }
119  if (flag == 16) { retval=std::string("invalid header"); }
120  return(retval);
121  } // std::string quality(const int& flag)
122 
123 /*----------------------------------------------------------------------*/
124 
125  sff::FREE qualityreports(const MinuteBlock* blocks, const int& nblocks)
126  {
127  sff::FREE retval;
128  for (int i=0; i<nblocks; ++i)
129  {
130  const int& qf=blocks[i].Mquality_flag;
131  if (qf != 0)
132  {
133  std::ostringstream oss;
134  oss << qf;
135  retval.append("quality flag @ " + blocks[i].Mtime.timestring()
136  + ": " + oss.str());
137  retval.append(" this means: " + quality(qf));
138  }
139  }
140  return(retval);
141  } // sff::FREE qualityreports(const MinuteBlock* blocks, const int& nblocks)
142 
143 /*======================================================================*/
144 
145  SampleBlock readdata(std::istream& is, const bool& verbose)
146  {
147  SampleBlock retval;
148  int ninsamples=nsamples*nchannels;
149  for (int i=0; i<ninsamples; i++)
150  {
151  is >> retval.Msamples[i];
152  DATRW_assert(is.good(), "ERROR: reading samples!");
153  }
154  std::string dummy;
155  std::getline(is, dummy);
156  return(retval);
157  } // SampleBlock readdata(std::istream& is, const bool& verbose)
158 
159 /*----------------------------------------------------------------------*/
160 
161  void dump(std::ostream& os, const SampleBlock& block)
162  {
163  for (int ic=1; ic<=datrw::hpmo::nchannels; ic++)
164  {
165  os.width(1);
166  os << "K";
167  os.setf(std::ios_base::fixed,std::ios_base::basefield);
168  os.width(2);
169  os.fill('0');
170  os << ic << " ";
171  for (int is=1; is<=datrw::hpmo::nsamples/2; is++)
172  {
173  os.flags(std::ios_base::showpos);
174  os.setf(std::ios_base::fixed,std::ios_base::floatfield);
175  os.width(6);
176  os << block(ic, is) << " ";
177  }
178  os.width(4);
179  os << std::endl << " ";
180  for (int is=datrw::hpmo::nsamples/2+1;
181  is<=datrw::hpmo::nsamples; is++)
182  {
183  os.flags(std::ios_base::showpos);
184  os.setf(std::ios_base::fixed,std::ios_base::floatfield);
185  os.width(6);
186  os << block(ic, is) << " ";
187  }
188  os.unsetf(std::ios_base::showpos);
189  os << std::endl;
190  }
191  } // void dump(std::ostream& os, const SampleBlock& block)
192 
193 /*======================================================================*/
194 
195  void dump(std::ostream& os, const MinuteBlock& block)
196  {
197  os << block.Mtime.timestring() << std::endl;
198  os << "quality: " << block.Mquality_flag << " "
199  << quality(block.Mquality_flag) << std::endl;
200  os << block.Mdata;
201  } // void Samples::dump(std::ostream& os) const
202 
203 /*======================================================================*/
204 
205  std::istream& operator >> (std::istream& is, MinuteBlock& block)
206  {
207  datrw::hpmo::Header header(is);
208  block.Mtime=header.time();
209  block.Mquality_flag=header.errorflag();
210  is >> block.Mdata;
211  return(is);
212  }
213 
214 /*----------------------------------------------------------------------*/
215 
216  Tvecofblocks readfile(std::istream& is, const bool& verbose)
217  {
218  Tvecofblocks retval;
219  bool hot=true;
220  MinuteBlock block;
221  retval.clear();
222  if (verbose) { std::cout << "extracting block # "; }
223  int iblock=0;
224  while (is.good() && (hot))
225  {
228  try { is >> block; }
229  catch ( NoHeaderException )
230  { hot=false; }
231  catch ( Exception& e )
232  {
233  e.report();
234  std::cerr << "ERROR (datrw::hpmo::read): "
235  << "after reading " << retval.size() << " blocks"
236  << std::endl;
237  if (retval.size()>0)
238  {
239  std::cerr << " last successfull block @ "
240  << retval[retval.size()-1].Mtime.timestring() << std::endl;
241  }
242  throw;
243  }
245  if (hot)
246  {
247  retval.push_back(block);
248  if (verbose) { std::cout << ++iblock << " "; }
249  }
250  }
251  if (verbose) { std::cout << std::endl; }
252  return(retval);
253  } // Tvecofblocks readfile(std::istream& is, const bool& verbose)
254 
255  } // namespace hpmo
256 
257 } // namespace datrw
258 
259 /* ----- END OF readhpmo.cc ----- */
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
sff::FREE qualityreports(const MinuteBlock *blocks, const int &nblocks)
prepare a report on unusual quality reports
Definition: readhpmo.cc:125
libtime::TAbsoluteTime time() const
Definition: readhpmo.h:94
const double sampling_interval
sampling interval in seconds
Definition: hpmodata.h:55
const int nchannels
number of channels in HP MO data acquisition system
Definition: hpmodata.h:49
std::vector< MinuteBlock > Tvecofblocks
within the inner reading functions, we use vector to hold minute blocks
Definition: readhpmo.h:67
int errorflag() const
Definition: readhpmo.h:95
libtime::TRelativeTime dt()
return sampling interval of HPMO data acquisition (i.e. 5 sec)
Definition: readhpmo.cc:83
static bool report_on_construct_flag()
return report on construct flag
Definition: exception.h:93
void dump(std::ostream &os) const
Definition: readhpmo.cc:73
static void dont_report_on_construct()
Issue NO screen report on construction of exception.
Definition: exception.cc:90
double Msamples[nchannels *nsamples]
Definition: hpmodata.h:59
const int nsamples
number of samples per minute block and channel
Definition: hpmodata.h:51
exception class declaration for libdatrwxx (prototypes)
Read, hold and handle one minute block header line.
Definition: readhpmo.h:88
Base class for exceptions.
Definition: exception.h:62
hold one minute block
Definition: hpmodata.h:67
std::string headerline() const
Definition: readhpmo.h:97
this excpetion will be thrown by the Header reading function, in case it does not find something that...
Definition: readhpmo.h:77
std::string Mline
Definition: readhpmo.h:102
void readheader(std::istream &is, const bool &verbose=false)
Definition: readhpmo.cc:47
libtime::TRelativeTime toffset(const int &ichannel)
return time offset for channel ichannel (due to multiplexer)
Definition: readhpmo.cc:90
Root namespace of library.
Definition: aalibdatrwxx.cc:16
libtime::TAbsoluteTime Mtime
Definition: hpmodata.h:68
hold samples of one minute-block
Definition: hpmodata.h:58
libtime::TAbsoluteTime Mtime
Definition: readhpmo.h:100
Tvecofblocks readfile(std::istream &is, const bool &verbose)
read a full data file
Definition: readhpmo.cc:216
std::string quality(const int &flag)
return meaning of quality flag
Definition: readhpmo.cc:109
void dump(std::ostream &os, const SampleBlock &block)
dump one block of samples
Definition: readhpmo.cc:161
void check_channel_no(const int &ichannel)
check if channel number is valid
Definition: readhpmo.cc:101
virtual void report() const
Screen report.
Definition: exception.cc:96
std::istream & operator>>(std::istream &is, MinuteBlock &block)
read a full minute block from C++ stream
Definition: readhpmo.cc:205
SampleBlock readdata(std::istream &is, const bool &verbose)
read one minute block of samples
Definition: readhpmo.cc:145