DATRW++ library: seismic data I/O with multiple formats
datwrite.cc
Go to the documentation of this file.
1 /*! \file datrw.cc
2  * \brief generic interface definition (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 11/04/2006
8  *
9  * generic interface definition (implementation)
10  *
11  * Copyright (c) 2006 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  *
30  * REVISIONS and CHANGES
31  * - 11/04/2006 V1.0 Thomas Forbriger
32  * - 01/04/2011 V1.1 do not clear Mhassrce upon writing the file header
33  * - 07/06/2011 V1.2 promise constness of series samples
34  * - 08/07/2016 V1.3 make correct use of new DATRW_report_assert
35  * - 18/11/2016 V1.4 provide debug flag in base class
36  * and produce debug output
37  *
38  * ============================================================================
39  */
40 #define DATRW_DATWRITE_CC_VERSION \
41  "DATRW_DATWRITE_CC V1.4"
42 
43 #include<fstream>
44 #include <datrwxx/datwrite.h>
45 #include <datrwxx/debug.h>
46 
47 namespace datrw {
48 
49  void abort_if_exists(const std::string& filename)
50  {
51  std::ifstream file(filename.c_str(),std::ios_base::in);
52  DATRW_assert((!file.good()),"ERROR: file exists!");
53  }
54 
55  /*----------------------------------------------------------------------*/
56 
57  /*! \brief output stream base class constructor, has to be called from
58  * derived class.
59  *
60  * \param os C++ output file stream; all data will be written to this stream
61  * \param datatype indicates the value type used for storing sample values
62  * in the file; if the file format uses integer values to
63  * store the data, round-off errors will happen, if float
64  * data is stored in this format; this parameter is stored
65  * and returned to the user upon request through
66  * datrw::odatstream::seriestype()
67  * \param handlesfilefree \c true if the format can store the data in a FREE
68  * block for a data file (i.e. a free format string file
69  * header); this parameter is stored and returned to the
70  * user upon request through
71  * datrw::odatstream::handlesfilefree()
72  * \param handlestracefree \c true if the format can store the data in a FREE
73  * block for a data trace (i.e. a free format string trace
74  * header); this parameter is stored and returned to the
75  * user upon request through
76  * datrw::odatstream::handlestracefree()
77  * \param handlessrce \c true if the format can store SRCE data which
78  * defines a seismic source; this parameter is stored and
79  * returned to the user upon request through
80  * datrw::odatstream::handlessrce()
81  * \param handlesinfo \c true if the format can store INFO data which
82  * defines receiver properties; this parameter is stored and
83  * returned to the user upon request through
84  * datrw::odatstream::handlesinfo()
85  * \param debug set debug mode on a base class level
86  */
87  odatstream::odatstream(std::ostream& os,
88  const Edatatype& datatype,
89  const bool& handlesfilefree,
90  const bool& handlestracefree,
91  const bool& handlessrce,
92  const bool& handlesinfo,
93  const bool& debug)
94  : Mos(os), Mdebug(debug), Mwid2set(false), Msrceset(false),
95  Minfoset(false), Mfreeset(false), Mheaderflushed(false),
96  Mhandlestracefree(handlestracefree),
97  Mhandlesfilefree(handlesfilefree),
98  Mhandlesinfo(handlesinfo),
99  Mhandlessrce(handlessrce),
100  Mdatatype(datatype)
101  {
102  DATRW_debug(this->Mdebug,
103  "odatstream::odatstream",
104  "create new output stream");
105  DATRW_assert(os.good(), "output stream is not good!");
106  }
107 
108  /*----------------------------------------------------------------------*/
109 
110  void odatstream::setfree(const sff::FREE& free)
111  {
112  DATRW_debug(this->Mdebug, "odatstream::setfree",
113  DATRW_value(free.lines.size()));
114  if (Mheaderflushed)
115  {
117  "file format cannot handle trace FREE data\n" <<
118  "FREE data will be dropped silently");
119  } else {
121  "file format cannot handle file FREE data\n" <<
122  "FREE data will be dropped silently");
123  }
124  Mfreeset=true;
125  Mfree=free;
126  }
127 
128  /*----------------------------------------------------------------------*/
129 
130  void odatstream::setwid2(const sff::WID2& wid2)
131  {
132  DATRW_debug(this->Mdebug, "odatstream::setwid2",
133  DATRW_value(wid2.line().substr(0,50)));
134  Mwid2set=true;
135  Mwid2=wid2;
136  this->flushfileheader();
137  }
138 
139  /*----------------------------------------------------------------------*/
140 
141  void odatstream::setinfo(const sff::INFO& info)
142  {
143  DATRW_debug(this->Mdebug, "odatstream::setinfo",
144  DATRW_value(info.line().substr(0,50)));
146  "file format cannot handle INFO data\n"
147  "INFO data will be dropped silently");
148  Minfoset=true;
149  Minfo=info;
150  }
151 
152  /*----------------------------------------------------------------------*/
153 
154  void odatstream::setsrce(const sff::SRCE& srce)
155  {
156  DATRW_debug(this->Mdebug, "odatstream::setsrce",
157  DATRW_value(srce.line().substr(0,50)));
159  "file format cannot handle SRCE data\n"
160  "SRCE data will be dropped silently");
161  Msrceset=true;
162  Msrce=srce;
163  }
164 
165  /*----------------------------------------------------------------------*/
166 
168  {
169  if (!Mheaderflushed)
170  {
171  writefileheader();
172  }
173  Mheaderflushed=true;
174  /*
175  * 1.4.2011: I see no reason to clear Msrceset here. Keeping this flag
176  * provides the trace writing functions with information on the source
177  * which relates to the trace. This is of particular value for writer like
178  * osustream, which have to calculate delay times.
179  */
180  // Msrceset=false;
181  Mfreeset=false;
182  }
183 
184  /*----------------------------------------------------------------------*/
185 
187  {
188  Mwid2set=false;
189  Mfreeset=false;
190  Minfoset=false;
191  }
192 
193  /*----------------------------------------------------------------------*/
194 
195  void odatstream::writeseries(const Tdseries::Tcoc& series)
196  {
197  DATRW_debug(this->Mdebug, "odatstream::writeseries (double)",
198  DATRW_value(series.f()) << ", " << DATRW_value(series.l()));
199  DATRW_assert(this-Mwid2set,"missing WID2 header");
200  this->writetrace(series);
201  this->cleartraceheader();
202  }
203 
204  /*----------------------------------------------------------------------*/
205 
206  void odatstream::writeseries(const Tfseries::Tcoc& series)
207  {
208  DATRW_debug(this->Mdebug, "odatstream::writeseries (float)",
209  DATRW_value(series.f()) << ", " << DATRW_value(series.l()));
210  DATRW_assert(this-Mwid2set,"missing WID2 header");
211  this->writetrace(series);
212  this->cleartraceheader();
213  }
214 
215  /*----------------------------------------------------------------------*/
216 
217  void odatstream::writeseries(const Tiseries::Tcoc& series)
218  {
219  DATRW_debug(this->Mdebug, "odatstream::writeseries (int)",
220  DATRW_value(series.f()) << ", " << DATRW_value(series.l()));
221  DATRW_assert(this-Mwid2set,"missing WID2 header");
222  this->writetrace(series);
223  this->cleartraceheader();
224  }
225 
226  /*----------------------------------------------------------------------*/
227 
228  void odatstream::help(std::ostream& os, const char* name)
229  {
230  os << "Class " << name << " provides no help text." << std::endl;
231  }
232 
233 
234 } // namespace datrw
235 
236 /* ----- END OF datrw.cc ----- */
odatstream(std::ostream &os, const Edatatype &datatype, const bool &handlesfilefree=false, const bool &handlestracefree=false, const bool &handlessrce=false, const bool &handlesinfo=false, const bool &debug=false)
constructor is protected: do not create an instance of this class
Definition: datwrite.cc:87
void abort_if_exists(const std::string &filename)
Definition: datwrite.cc:49
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
void writeseries(const Tdseries::Tcoc &series)
write double data
Definition: datwrite.cc:195
void cleartraceheader()
clear trace header flags
Definition: datwrite.cc:186
void setwid2(const sff::WID2 &wid2)
Definition: datwrite.cc:130
macro function for debugging output (prototypes)
Edatatype
Definition: formats.h:84
static void help(std::ostream &os=std::cout, const char *name="idatsream")
print some info about data conversion.
Definition: datwrite.cc:228
void setfree(const sff::FREE &free)
Definition: datwrite.cc:110
sff::WID2 wid2() const
return WID2 data
Definition: datwrite.h:169
void setinfo(const sff::INFO &info)
Definition: datwrite.cc:141
sff::SRCE Msrce
Definition: datwrite.h:210
sff::INFO info() const
return SRCE data
Definition: datwrite.h:173
sff::INFO Minfo
Definition: datwrite.h:211
void flushfileheader()
flush file header to file
Definition: datwrite.cc:167
bool handlesfilefree() const
true if file FREE block can be handled
Definition: datwrite.h:123
Root namespace of library.
Definition: aalibdatrwxx.cc:16
sff::SRCE srce() const
return SRCE data
Definition: datwrite.h:171
void setsrce(const sff::SRCE &srce)
Definition: datwrite.cc:154
bool handlesinfo() const
true if INFO data can be handled
Definition: datwrite.h:129
sff::FREE free() const
return FREE data
Definition: datwrite.h:175
virtual void writetrace(const Tdseries::Tcoc &series)
write double data
Definition: datwrite.h:158
sff::FREE Mfree
Definition: datwrite.h:212
virtual void writefileheader()
actually write the file header
Definition: datwrite.h:164
#define DATRW_debug(C, N, M)
produce debug output
Definition: debug.h:50
bool Mdebug
global debug flag
Definition: datwrite.h:197
bool handlessrce() const
true if SRCE data can be handled
Definition: datwrite.h:125
#define DATRW_value(V)
report value
Definition: debug.h:65
sff::WID2 Mwid2
Definition: datwrite.h:209
bool handlestracefree() const
true if trace FREE block can be handled
Definition: datwrite.h:127
#define DATRW_report_assert(C, M)
Check an assertion and report only.
Definition: error.h:120
generic interface definition (prototypes)