DATRW++ library: seismic data I/O with multiple formats
ihpmostream.cc
Go to the documentation of this file.
1 /*! \file ihpmostream.cc
2  * \brief provide data from HP Mo (BFO data acquisition system) (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 31/03/2004
8  *
9  * provide data from HP-MO (BFO data acquisition system) (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  * - 23/11/2010 V1.1 introduced static members
32  *
33  * ============================================================================
34  */
35 #define DATRW_HPMO_CC_VERSION \
36  "DATRW_HPMO_CC V1.1"
37 
38 #include <datrwxx/datread.h>
39 #include <datrwxx/util.h>
40 #include <datrwxx/hpmo.h>
41 #include <datrwxx/readhpmo.h>
42 #include <sffxx.h>
43 #include <iostream>
44 #include <sstream>
45 
46 namespace datrw {
47 
48  const std::ios_base::openmode ihpmostream::openmode=std::ios_base::in;
49 
50  //@{
51  /*! \brief Format properties
52  * \ingroup group_hpmo
53  */
54  const bool hpmo::isbinary=false;
55  const char* const hpmo::streamID="hpmo";
56  //@}
57 
58  /*----------------------------------------------------------------------*/
59 
60  ihpmostream::ihpmostream(std::istream& is, const bool& verbose):
61  Tbase(is, true, true, false),
62  Mnblocks(0), Mnextblock(0), Mnextchannel(0)
63  { this->read(is, verbose); }
64 
65 /*----------------------------------------------------------------------*/
66 
68  {
69  const int firstblock=Mindex[Mnextblock];
70  const int lastblock=Mindex[Mnextblock+1]-1;
71  const int nblocks=lastblock-firstblock+1;
72  const int nsamp=nblocks*datrw::hpmo::nsamples;
73  Tdseries retval(nsamp);
74  int isample=0;
75  for (int iblock=firstblock; iblock<=lastblock; ++iblock)
76  {
77  for (int i=1; i<=datrw::hpmo::nsamples; ++i)
78  {
79  retval(isample)=Mdatafile[iblock].Mdata(Mnextchannel, i);
80  ++isample;
81  }
82  }
83  this->set_next_header();
84  return(retval);
85  }
86 
87 /*----------------------------------------------------------------------*/
88 
90  { return(datrw::util::convert<Tdseries, Tfseries>(this->dseries())); }
91 
92 /*----------------------------------------------------------------------*/
93 
95  {
97  }
98 
99 /*----------------------------------------------------------------------*/
100 
101  void ihpmostream::read(std::istream& is, const bool& verbose)
102  {
103  // simply read the thing
105  if (verbose)
106  {
107  std::cout << "(ihpmostream::read): read "
108  << blocks.size() << " blocks of data" << std::endl;
109  }
110  int nblocksread=blocks.size();
111  DATRW_assert((nblocksread <= datrw::hpmo::nminutes),
112  "ERROR (ihpmostream::read): "
113  "illegal number of minute blocks in file!" "\n"
114  " Use ihpmostream only together with original HPMO data");
115  // extract the stuff now
116  if (verbose) { std::cout << "(ihpmostream::read): copy block #"; }
117  libtime::TRelativeTime length(datrw::hpmo::dt()*datrw::hpmo::nsamples);
118  for (int iblock=0; iblock<nblocksread; iblock++)
119  {
120  this->Mdatafile[iblock]=blocks[iblock];
121  datrw::hpmo::MinuteBlock& block=this->Mdatafile[iblock];
122  if (iblock > 0)
123  {
124  datrw::hpmo::MinuteBlock& prevblock=this->Mdatafile[iblock-1];
125  if (prevblock.Mtime+length != block.Mtime)
126  {
127  Mindex[Mnblocks]=iblock;
128  ++Mnblocks;
129  if (verbose) {
130  std::cout << std::endl
131  << "(ihpmostream::read): found gap" << std::endl;
132  std::cout << " between block # " << iblock-1 << " @ "
133  << prevblock.Mtime.timestring() << std::endl;
134  std::cout << " and block # " << iblock << " @ "
135  << block.Mtime.timestring() << std::endl;
136  std::cout << "(ihpmostream::read): copy block #";
137  }
138  }
139  }
140  else
141  {
142  Mindex[Mnblocks]=0;
143  ++Mnblocks;
144  }
145  if (verbose) { std::cout << " " << iblock; }
146  }
147  if (verbose)
148  {
149  std::cout << std::endl;
150  std::cout << "(ihpmostream::read): extracted "
151  << Mnblocks << " contiguous data block(s)" << std::endl;
152  }
153  // marks last block
154  Mindex[Mnblocks]=nblocksread;
155  // init reading pointers
156  Mnextblock=0;
157  Mnextchannel=1;
158  // prepare file FREE block
159  std::ostringstream oss;
160  oss << nblocksread;
161  sff::FREE filefree;
162  filefree.append("ihpmostream read " + oss.str() + " blocks from file");
163  oss.str("");
164  oss << Mnblocks;
165  filefree.append("found " + oss.str() + " contiguous sets of blocks");
166  filefree.append(qualityreports(Mdatafile, nblocksread));
167  this->setfilefree(filefree);
168  } // void ihpmostream::read(std::istream& is, const bool& verbose)
169 
170 /*----------------------------------------------------------------------*/
171 
173  {
174  const int firstblock=Mindex[Mnextblock];
175  const int lastblock=Mindex[Mnextblock+1]-1;
176  const int nblocks=lastblock-firstblock+1;
177  const int nsamp=nblocks*datrw::hpmo::nsamples;
178  // DEBUG
179  // std::cout << firstblock << " " << lastblock << " "
180  // << Mnextblock << " " << Mnextchannel << std::endl;
181  this->newtrace();
182  // set WID2 header
183  sff::WID2 wid2line;
184  wid2line.station="BFO";
185  wid2line.auxid="HPMO";
186  wid2line.dt=datrw::hpmo::sampling_interval;
187  std::ostringstream oss;
188  oss << "K";
189  oss.width(2);
190  oss.fill('0');
191  oss << Mnextchannel;
192  wid2line.channel=oss.str();
193  wid2line.nsamples=nsamp;
194  // calculate time of first sample for this channel
195  wid2line.date=Mdatafile[firstblock].Mtime;
196  wid2line.date -= libtime::TRelativeTime(0,0,0,57);
197  wid2line.date += datrw::hpmo::toffset(Mnextchannel);
198  this->setwid2(wid2line);
199  // set FREE block of header
200  oss.str("");
201  oss << "channel " << Mnextchannel << " extracted from "
202  << lastblock-firstblock+1 << " contiguous minute blocks";
203  sff::FREE tracefree;
204  tracefree.append(oss.str());
205  tracefree.append(qualityreports(&Mdatafile[firstblock],
206  lastblock-firstblock+1));
207  this->settracefree(tracefree);
208  // proceed pointers to next trace
209  ++Mnextchannel;
211  {
212  Mnextchannel=1;
213  ++Mnextblock;
214  if (Mnextblock >= Mnblocks)
215  {
216  this->setlast();
217  }
218  }
219  } // void ihpmostream::set_next_header()
220 
221 } // namespace datrw
222 
223 /* ----- END OF ihpmostream.cc ----- */
virtual Tdseries dseries()
Definition: ihpmostream.cc:67
#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
const double sampling_interval
sampling interval in seconds
Definition: hpmodata.h:55
aff::Series< float > Tfseries
Definition: types.h:46
const int nchannels
number of channels in HP MO data acquisition system
Definition: hpmodata.h:49
void setfilefree(const sff::FREE &free)
Definition: datread.cc:97
std::vector< MinuteBlock > Tvecofblocks
within the inner reading functions, we use vector to hold minute blocks
Definition: readhpmo.h:67
libtime::TRelativeTime dt()
return sampling interval of HPMO data acquisition (i.e. 5 sec)
Definition: readhpmo.cc:83
aff::Series< double > Tdseries
Definition: types.h:45
static const std::ios_base::openmode openmode
Definition: hpmo.h:72
const int nsamples
number of samples per minute block and channel
Definition: hpmodata.h:51
const bool isbinary
Format properties.
Definition: ihpmostream.cc:54
void read(std::istream &is, const bool &verbose=false)
read one data file
Definition: ihpmostream.cc:101
hold one minute block
Definition: hpmodata.h:67
void setwid2(const sff::WID2 &wid2)
Definition: datread.cc:113
datrw::hpmo::MinuteBlock Mdatafile[datrw::hpmo::nminutes]
buffer to hold one data file
Definition: hpmo.h:79
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
utilities used by more than one type of data reader (prototypes)
void set_next_header()
set header for next request
Definition: ihpmostream.cc:172
void settracefree(const sff::FREE &free)
Definition: datread.cc:105
int Mnextchannel
Definition: hpmo.h:83
Tvecofblocks readfile(std::istream &is, const bool &verbose)
read a full data file
Definition: readhpmo.cc:216
provide data from HP Mo (BFO data acquisition system) (prototypes)
const char *const streamID
Format properties.
Definition: ihpmostream.cc:55
virtual void skipseries()
Definition: ihpmostream.cc:94
virtual Tfseries fseries()
Definition: ihpmostream.cc:89
int Mindex[datrw::hpmo::nminutes]
Definition: hpmo.h:80
ihpmostream(std::istream &is, const bool &verbose=false)
Definition: ihpmostream.cc:60
int Mnextblock
Definition: hpmo.h:82
const int nminutes
number of minute blocks per file (1h)
Definition: hpmodata.h:53