DATRW++ library: seismic data I/O with multiple formats
tracereader.cc
Go to the documentation of this file.
1 /*! \file tracereader.cc
2  * \brief provide more efficient reading of sequential traces (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 08/07/2008
8  *
9  * provide more efficient reading of sequential traces (implementation)
10  *
11  * Copyright (c) 2008 by Thomas Forbriger (BFO Schiltach)
12  *
13  * ----
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27  * ----
28  *
29  * REVISIONS and CHANGES
30  * - 08/07/2008 V1.0 Thomas Forbriger
31  * - 25/11/2010 V1.1
32  * - use correct open mode upon ifstream open
33  * - 07/09/2011 V1.2 support format modifier strings
34  * - 29/11/2011 V1.3 introduced assertopen; query functions must not be
35  * called with no file being open
36  *
37  * ============================================================================
38  */
39 #define DATRW_TRACEREADER_CC_VERSION \
40  "DATRW_TRACEREADER_CC V1.3"
41 
42 #include <datrwxx/tracereader.h>
43 #include <datrwxx/error.h>
44 
45 namespace datrw {
46 
48  {
49  if (this->Mopen)
50  {
51  delete MPis;
52  delete MPias;
53  }
54  } // sequentialtracereader::~sequentialtracereader()
55 
56  /*----------------------------------------------------------------------*/
57 
59  {
60  DATRW_assert(this->Mopen,
61  "ERROR (sequentialtracereader): "
62  "a query function was called with no file being open");
63  } // void sequentialtracereader::assertopen() const
64 
65  /*----------------------------------------------------------------------*/
66 
67  bool sequentialtracereader::select(const std::string& filename,
68  const int& itrace,
69  const std::string& format)
70  {
71  if (Mdebug)
72  {
73  std::cerr << "DEBUG: sequentialtracereader: "
74  << "entered select with parameters:" << std::endl
75  << " filename: " << filename << std::endl
76  << " itrace: " << itrace << std::endl
77  << " format: " << format << std::endl;
78  if (Mopen)
79  {
80  std::cerr << " current filename: " << Mfilename << std::endl
81  << " current trace: " << Mindex << std::endl;
82  }
83  }
84  bool doopen=false;
85  // check: must a new file be opened?
86  if (!Mopen)
87  {
88  doopen=true;
89  if (Mdebug)
90  {
91  std::cerr << "DEBUG: sequentialtracereader: "
92  << "open file, because no file is open" << std::endl;
93  }
94  }
95  else
96  {
97  if (filename != Mfilename)
98  {
99  doopen=true;
100  if (Mdebug)
101  {
102  std::cerr << "DEBUG: sequentialtracereader: "
103  << "open file, because file name differs" << std::endl;
104  }
105  }
106  else if (itrace < Mindex)
107  {
108  doopen=true;
109  if (Mdebug)
110  {
111  std::cerr << "DEBUG: sequentialtracereader: "
112  << "open file, because trace index (" << itrace << ") "
113  << "is less than current index (" << Mindex << ")" << std::endl;
114  }
115  }
116  }
117  // if yes: open new file
118  if (doopen)
119  {
120  if (this->Mopen)
121  {
122  if (Mdebug)
123  {
124  std::cerr << "DEBUG: sequentialtracereader: "
125  << "close previous file" << std::endl;
126  }
127  delete MPis;
128  delete MPias;
129  }
130  Mindex=1;
131  Mfilename=filename;
132  MPis=new std::ifstream(Mfilename.c_str(),
133  datrw::ianystream::openmode(format));
134  MPias=new datrw::ianystream(*MPis, format, Mdebug);
135  Mopen=true;
136  if (Mdebug)
137  {
138  std::cerr << "DEBUG: sequentialtracereader: "
139  << "file " << Mfilename << " is opened" << std::endl;
140  }
141  }
142  // skip to selected trace
143  int nskip=itrace-Mindex;
144  DATRW_assert(nskip>=0, "sequentialtracereader::select: "
145  "cannot skip backwards!");
146  if (Mdebug)
147  {
148  std::cerr << "DEBUG: sequentialtracereader: "
149  << nskip << " skips: " << std::endl;
150  }
151  for (int iskip=0; iskip<nskip; ++iskip)
152  {
153  DATRW_assert(MPias->good(), "sequentialtracereader::select: "
154  "input is not good!");
155  MPias->skipseries();
156  ++Mindex;
157  }
158  return(MPias->good());
159  } // sequentialtracereader::select
160 
161  /*----------------------------------------------------------------------*/
162 
163  bool sequentialtracereader::select(const std::string& filename,
164  const int& itrace,
165  const Eformat& format)
166  {
167  return(this->select(filename, itrace, anyID(format)));
168  } // sequentialtracereader::select
169 
170 } // namespace datrw
171 
172 /* ----- END OF tracereader.cc ----- */
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
Eformat anyID(std::string formatstring)
convert identifier from and to string representation
Definition: formats.cc:68
exception class declaration for libdatrwxx (prototypes)
~sequentialtracereader()
destructor
Definition: tracereader.cc:47
Class to read any type of data file.
Definition: readany.h:82
Root namespace of library.
Definition: aalibdatrwxx.cc:16
datrw::ianystream * MPias
Definition: tracereader.h:117
bool select(const std::string &filename, const int &itrace, const Eformat &format)
Definition: tracereader.cc:163
provide more efficient reading of sequential traces (prototypes)
Eformat
Definition: formats.h:54