DATRW++ library: seismic data I/O with multiple formats
osffstream.cc
Go to the documentation of this file.
1 /*! \file osffstream.cc
2  * \brief wrapper around SFF output stream (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 11/04/2006
8  *
9  * wrapper around SFF output stream (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  * - 17.12.2010 V1.1
33  * - bug fix: INFO data was not passed
34  * - declare SFF data type as single precision
35  * - declare GSE data type as integer
36  * - 07/06/2011 V1.2 promise constness of series samples
37  * - 21/11/2011 V1.3 introduce format modifiers
38  * - 18/11/2016 V1.4 use debug flag in base class
39  *
40  * ============================================================================
41  */
42 #define DATRW_OSFF_CC_VERSION \
43  "DATRW_OSFF_CC V1.4"
44 
45 #include <datrwxx/sff.h>
46 #include <datrwxx/debug.h>
47 #include <datrwxx/sffwriters.h>
48 #include <datrwxx/formatmodifier.h>
49 
50 namespace datrw {
51 
52  const std::ios_base::openmode osffstream::openmode=std::ios_base::out;
53 
54  /*----------------------------------------------------------------------*/
55 
56  osffstream::osffstream(std::ostream& os,
57  const std::string& modifier,
58  const bool& debug):
59  Tbase(os, Ffloat, true, true, true, true, debug),
60  Mwid2iswaiting(false),
61  Mfreeiswaiting(false),
62  Minfoiswaiting(false),
63  Mnormmode(::sff::NM_maxdyn)
64  {
65  Mdebug=debug;
66  datrw::Subformat subformat(modifier);
67  if (subformat.isset("dontscale"))
68  { Mnormmode=::sff::NM_one; }
69  else if (subformat.isset("scaleifneeded"))
70  { Mnormmode=::sff::NM_ifneeded; }
72  "osffstream");
73  DATRW_debug(Mdebug, "osffstream::osffstream",
74  "new instance established");
75  } // osffstream::osffstream(std::ostream& os, const bool& debug)
76 
77  /*----------------------------------------------------------------------*/
78 
79  void osffstream::help(std::ostream& os)
80  {
81  os <<
82  std::endl <<
83  "SFF writing functions" << std::endl <<
84  "---------------------" << std::endl <<
85  DATRW_OSFF_CC_VERSION << std::endl <<
86  std::endl;
87  os <<
88  "This module writes SFF data. SFF (Stuttgart File Format) is\n"
89  "based on GSE subformat CM6. GSE is an integer format with\n"
90  "a dynamic range of little more than 27 bits. Second differences\n"
91  "are saved such that in the worst case two bits are consumed by the\n"
92  "second differences and one additional bit for the sign. The\n"
93  "underlying libgsexx aborts if numbers larger than 2**26-1 are\n"
94  "to be written. For this reason floating point data must be\n"
95  "scaled appropriately before being written to SFF data. The\n"
96  "default is to scale to maximum dynamic range (i.e. 2**26-1).\n"
97  "Two other scaling options are available through format modifiers:"
98  << std::endl;
99  os <<
100  "dontscale Write data as is. This is appropriate for integer\n"
101  " data in particlular.\n"
102  "scaleifneeded Scale only if largest value in time series is\n"
103  " larger than 2**26-1. Sample values smaller than\n"
104  " will be truncated to zero silently."
105  << std::endl;
106  } // void osffstream::help(std::ostream& os=std::cout)
107 
108  /*----------------------------------------------------------------------*/
109 
111  {
112  DATRW_debug(Mdebug, "osffstream::writefileheader",
113  "write file header to file");
114  ::sff::FileHeader fileheader;
115  if (this->hassrce()) { fileheader.setsrce(this->srce()); }
116  if (this->hasfree()) { fileheader.setfree(this->free()); }
117  Mos << fileheader;
118  } // void osffstream::writefileheader()
119 
120  /*----------------------------------------------------------------------*/
121 
122  void osffstream::writetrace(const Tdseries::Tcoc& series)
123  {
124  DATRW_debug(Mdebug, "osffstream::writetrace",
125  "write series of type double to file");
126  this->flushwaitingtrace();
128  } // void osffstream::writetrace(const Tdseries& series)
129 
130  /*----------------------------------------------------------------------*/
131 
132  void osffstream::writetrace(const Tfseries::Tcoc& series)
133  {
134  DATRW_debug(Mdebug, "osffstream::writetrace",
135  "write series of type float to file");
136  this->flushwaitingtrace();
138  } // void osffstream::writetrace(const Tfseries& series)
139 
140  /*----------------------------------------------------------------------*/
141 
142  void osffstream::writetrace(const Tiseries::Tcoc& series)
143  {
144  DATRW_debug(Mdebug, "osffstream::writetrace",
145  "write series of type integer to file");
146  this->flushwaitingtrace();
148  } // void osffstream::writetrace(const Tiseries& series)
149 
150  /*----------------------------------------------------------------------*/
151 
152  void osffstream::flushwaitingtrace(const bool& last)
153  {
154  DATRW_debug(Mdebug, "osffstream::flushwaitingtrace",
155  "flush previous trace data to file");
156  // flush previous trace
157  if (Mwid2iswaiting)
158  {
159  // prepare trace header
160  ::sff::TraceHeader traceheader(Mwid2waiting, last);
161  if (Mfreeiswaiting) { traceheader.setfree(Mfreewaiting); }
162  if (Minfoiswaiting) { traceheader.setinfo(Minfowaiting); }
164  traceheader,
166  Mnormmode);
167  }
168  else
169  {
170  DATRW_assert((!last),
171  "file is to be closed and no data was written");
172  }
173  // check for current trace headers and store them
174  if (!last)
175  {
176  DATRW_assert(this->haswid2(), "missing trace header data");
177  Mwid2iswaiting=true;
178  Mwid2waiting=this->wid2();
179  Mfreeiswaiting=false;
180  if (this->hasfree()) { Mfreeiswaiting=true; Mfreewaiting=this->free(); }
181  Minfoiswaiting=false;
182  if (this->hasinfo()) { Minfoiswaiting=true; Minfowaiting=this->info(); }
183  }
184  }
185 
186  /*======================================================================*/
187 
188  ogsestream::ogsestream(std::ostream& os, const bool& debug):
189  Tbase(os, "", debug)
190  {
191  Mnormmode=::sff::NM_one;
192  DATRW_debug(Mdebug, "ogsestream::ogsestream",
193  "new instance established");
194  this->setdatatype(Fint);
195  } // ogsestream::ogsestream(std::ostream& os, const bool& debug)
196 
197  /*----------------------------------------------------------------------*/
198 
199  void ogsestream::help(std::ostream& os)
200  {
201  os << "This module writes standard GSE data." << std::endl;
202  os << "Essentially SFF data is written but without normalising"
203  << std::endl;
204  os << "them. Notice that floating point data will by truncated to"
205  << std::endl;
206  os << "integers." << std::endl;
207  } // void ogsestream::help(std::ostream& os=std::cout)
208 
209 } // namespace datrw
210 
211 /* ----- END OF osffstream.cc ----- */
bool Minfoiswaiting
Definition: sff.h:120
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
bool hasinfo() const
info is available
Definition: datwrite.h:182
provide format modifiers (prototypes)
macro function for debugging output (prototypes)
void setdatatype(const Edatatype &daty)
Definition: datwrite.h:191
bool hassrce() const
srce is available
Definition: datwrite.h:180
sff::WID2 wid2() const
return WID2 data
Definition: datwrite.h:169
::sff::WID2 Mwid2waiting
Definition: sff.h:121
bool Mfreeiswaiting
Definition: sff.h:120
std::ostream & Mos
output stream to be used by this class
Definition: datwrite.h:194
bool haswid2() const
wid2 is available
Definition: datwrite.h:178
#define DATRW_OSFF_CC_VERSION
Definition: osffstream.cc:42
virtual void writefileheader()
actually write the file header
Definition: osffstream.cc:110
read sff data (prototypes)
virtual void writetrace(const Tdseries::Tcoc &series)
write double data
Definition: osffstream.cc:122
static void help(std::ostream &os=std::cout)
Definition: osffstream.cc:79
::sff::FREE Mfreewaiting
Definition: sff.h:122
::sff::INFO Minfowaiting
Definition: sff.h:123
sff::INFO info() const
return SRCE data
Definition: datwrite.h:173
#define DATRW_assert_modifiers_are_recognized(S, F)
abort if user passed unused modifiers
bool debug()
indicate debug mode
Definition: datwrite.h:145
Root namespace of library.
Definition: aalibdatrwxx.cc:16
osffstream(std::ostream &os, const std::string &modifier="", const bool &debug=false)
Definition: osffstream.cc:56
sff::SRCE srce() const
return SRCE data
Definition: datwrite.h:171
sff::FREE free() const
return FREE data
Definition: datwrite.h:175
void flushwaitingtrace(const bool &last=false)
Definition: osffstream.cc:152
static const std::ios_base::openmode openmode
Definition: sff.h:106
void writesfftrace(std::ostream &os, const ::sff::TraceHeader &traceheader, const datrw::util::seriesreservoir &series, const ::sff::Enormmode &normmode)
function to write an SFF trace from a series reservoir
Definition: sffwriters.cc:45
#define DATRW_debug(C, N, M)
produce debug output
Definition: debug.h:50
::sff::Enormmode Mnormmode
Definition: sff.h:127
Class to handle format modifiersThis class is used to parse a format modifier string. Detailed instructions will be given upon request. For some hints have a look at tests/libdatrwxxtests.c.
bool Mdebug
global debug flag
Definition: datwrite.h:197
bool isset(const std::string &k) const
check if user provided this key
static void help(std::ostream &os=std::cout)
Definition: osffstream.cc:199
datrw::util::seriesreservoir Mserieswaiting
Definition: sff.h:124
bool Mwid2iswaiting
Definition: sff.h:120
bool hasfree() const
free is available
Definition: datwrite.h:184
ogsestream(std::ostream &os, const bool &debug=false)
Definition: osffstream.cc:188