DATRW++ library: seismic data I/O with multiple formats
oasciistream.cc
Go to the documentation of this file.
1 /*! \file oasciistream.cc
2  * \brief output raw ASCII data (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 18/10/2011
8  *
9  * output raw ASCII data (implementation)
10  *
11  * Copyright (c) 2011 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  * - 18/10/2011 V1.0 Thomas Forbriger
32  * - 23/08/2012 V1.1 support format modifiers
33  * - 18/11/2016 V1.2 use debug flag in base class
34  *
35  * ============================================================================
36  */
37 #define DATRW_OASCIISTREAM_CC_VERSION \
38  "DATRW_OASCIISTREAM_CC V1.2"
39 
40 #include<datrwxx/ascii.h>
41 #include<datrwxx/debug.h>
42 #include<datrwxx/error.h>
43 #include<datrwxx/formats.h>
45 #include<datrwxx/asciiheaderkeys.h>
46 
47 #define POUT( K, V ) Mos << "# " << K << ": " << V << std::endl;
48 #define FOUT( V ) Mos << "## " << V << std::endl;
49 
50 namespace datrw {
51 
52  const std::ios_base::openmode
53  oasciistream::openmode=std::ios_base::out;
54 
55  /*----------------------------------------------------------------------*/
56 
57  oasciistream::oasciistream(std::ostream& os,
58  const std::string& modifier,
59  const bool& debug):
60  Tbase(os, Fall, true, true, true, true, debug)
61  {
62  DATRW_debug(Mdebug, "oasciistream::oasciistream",
63  "new instance established");
64  // format modifiers must be used here to set defaults
65  datrw::Subformat subformat(modifier);
66  subformat("precision", "7") >> this->Mprecision;
68  "iasciistream");
69  } // oasciistream::oasciistream
70 
71  /*----------------------------------------------------------------------*/
72 
73  void oasciistream::help(std::ostream& os)
74  {
75  os <<
76  std::endl <<
77  "ASCII writing functions" << std::endl <<
78  "-----------------------" << std::endl <<
79  DATRW_OASCIISTREAM_CC_VERSION << std::endl <<
80  std::endl;
81  os <<
82  "Valid format modifiers are:\n";
84  mh("precision", "n") <<
85  "Set number of significant digits to \"n\" for\n";
86  mh() << "float and double format output.\n";
87  } // void oasciistream::help(std::ostream& os=std::cout)
88 
89  /*----------------------------------------------------------------------*/
90 
92  {
93  DATRW_debug(Mdebug, "oasciistream::writefileheader",
94  "entered function");
95  if (this->hasfree())
96  {
97  const ::sff::FREE& free=this->free();
98  ::sff::FREE::Tlines::const_iterator I=free.lines.begin();
99  while (I!=free.lines.end())
100  {
101  DATRW_debug(Mdebug, "oasciistream::writefileheader",
102  "line: " << *I);
103  FOUT( *I );
104  ++I;
105  }
106  }
107  DATRW_debug(Mdebug, "oasciistream::writefileheader",
108  "file FREE written if present");
109  if (this->hassrce())
110  {
111  ::sff::SRCE srce=this->srce();
112  POUT( ascii::keySRCEtype, srce.type );
113  POUT( ascii::keySRCEdate, srce.date.hierarchicalstring() );
114  POUT( ascii::keySRCEX, srce.cx );
115  POUT( ascii::keySRCEY, srce.cy );
116  POUT( ascii::keySRCEZ, srce.cz );
117  POUT( ascii::keySRCECS, ::sff::coosysID(srce.cs) );
118  }
119  DATRW_debug(Mdebug, "oasciistream::writefileheader",
120  "SRCE written if present");
121  } // void oasciistream::writefileheader()
122 
123  /*----------------------------------------------------------------------*/
124 
125  void oasciistream::writetrace(const Tdseries::Tcoc& series)
126  {
127  DATRW_debug(Mdebug, "oasciistream::writetrace (double)",
128  DATRW_value(series.f()) << ", "
129  DATRW_value(series.l()) << ", "
131  this->writetraceheader(series.size(),ascii::keydouble);
132  for (int i=series.first(); i<=series.last(); ++i)
133  {
134  Mos.precision(Mprecision);
135  Mos << series(i) << "\n";
136  }
137  Mos.flush();
138  } // void oasciistream::writefileheader()
139 
140  /*----------------------------------------------------------------------*/
141 
142  void oasciistream::writetrace(const Tfseries::Tcoc& series)
143  {
144  DATRW_debug(Mdebug, "oasciistream::writetrace (float)",
145  DATRW_value(series.f()) << ", "
146  DATRW_value(series.l()) << ", "
148  this->writetraceheader(series.size(),ascii::keyfloat);
149  for (int i=series.first(); i<=series.last(); ++i)
150  {
151  Mos.precision(Mprecision);
152  Mos << series(i) << "\n";
153  }
154  Mos.flush();
155  } // void oasciistream::writetrace(const Tfseries::Tcoc& series)
156 
157  /*----------------------------------------------------------------------*/
158 
159  void oasciistream::writetrace(const Tiseries::Tcoc& series)
160  {
161  DATRW_debug(Mdebug, "oasciistream::writetrace (int)",
162  DATRW_value(series.f()) << ", "
163  DATRW_value(series.l()));
164  this->writetraceheader(series.size(),ascii::keyint);
165  for (int i=series.first(); i<=series.last(); ++i)
166  {
167  Mos << series(i) << "\n";
168  }
169  Mos.flush();
170  } // void oasciistream::writetrace(const Tiseries::Tcoc& series)
171 
172  /*----------------------------------------------------------------------*/
173 
174  void oasciistream::writetraceheader(const unsigned int& n,
175  const char* type)
176  {
177  DATRW_debug(Mdebug, "oasciistream::writetraceheader",
178  "entered function");
179  ::sff::WID2 wid2=this->wid2();
180  wid2.nsamples=n;
181  POUT( ascii::keydate, wid2.date.hierarchicalstring() );
182  POUT( ascii::keydt, wid2.dt );
183  POUT( ascii::keynsamples, wid2.nsamples );
184  POUT( ascii::keystation, wid2.station );
185  POUT( ascii::keychannel, wid2.channel );
186  POUT( ascii::keyauxid, wid2.auxid );
187  POUT( ascii::keyinstype, wid2.instype );
188  POUT( ascii::keycalib, wid2.calib );
189  POUT( ascii::keycalper, wid2.calper );
190  POUT( ascii::keyhang, wid2.hang );
191  POUT( ascii::keyvang, wid2.vang );
192  DATRW_debug(Mdebug, "oasciistream::writetraceheader",
193  "WID2 written");
194  if (this->hasinfo())
195  {
196  ::sff::INFO info=this->info();
197  POUT( ascii::keynstacks, info.nstacks );
198  POUT( ascii::keyRECVX, info.cx );
199  POUT( ascii::keyRECVY, info.cy );
200  POUT( ascii::keyRECVZ, info.cz );
201  POUT( ascii::keyRECVCS, ::sff::coosysID(info.cs) );
202  }
203  DATRW_debug(Mdebug, "oasciistream::writetraceheader",
204  "INFO written if present");
205  if (this->hasfree())
206  {
207  const ::sff::FREE& free=this->free();
208  ::sff::FREE::Tlines::const_iterator I=free.lines.begin();
209  while (I!=free.lines.end())
210  {
211  FOUT( *I );
212  ++I;
213  }
214  }
215  DATRW_debug(Mdebug, "oasciistream::writetraceheader",
216  "trace FREE written if present");
217  POUT( ascii::keydata, type);
218  DATRW_debug(Mdebug, "oasciistream::writetraceheader",
219  "finished");
220  } // void oasciistream::writetraceheader(const unsigned int& n)
221 
222 } // namespace datrw
223 
224 /* ----- END OF oasciistream.cc ----- */
const char *const keyinstype
key for WID2 line header field
Definition: ascii.cc:51
const char *const keySRCEtype
key for SRCE line header field
Definition: ascii.cc:58
unsigned int Mprecision
floating point format precision
Definition: ascii.h:121
bool hasinfo() const
info is available
Definition: datwrite.h:182
const char *const keydouble
key for DATA type header field
Definition: ascii.cc:73
const char *const keySRCEY
key for SRCE line header field
Definition: ascii.cc:60
provide format modifiers (prototypes)
#define DATRW_OASCIISTREAM_CC_VERSION
Definition: oasciistream.cc:37
macro function for debugging output (prototypes)
bool hassrce() const
srce is available
Definition: datwrite.h:180
const char *const keyRECVY
key for INFO line header field
Definition: ascii.cc:65
sff::WID2 wid2() const
return WID2 data
Definition: datwrite.h:169
const char *const keydt
key for WID2 line header field
Definition: ascii.cc:46
const char *const keyRECVCS
key for INFO line header field
Definition: ascii.cc:67
const char *const keyhang
key for WID2 line header field
Definition: ascii.cc:54
std::ostream & Mos
output stream to be used by this class
Definition: datwrite.h:194
virtual void writetrace(const Tdseries::Tcoc &series)
write double data
static const std::ios_base::openmode openmode
Definition: ascii.h:113
static void help(std::ostream &os=std::cout)
Definition: oasciistream.cc:73
Help formatting modifier online help.
const char *const keyint
key for DATA type header field
Definition: ascii.cc:71
exception class declaration for libdatrwxx (prototypes)
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
const char *const keycalib
key for WID2 line header field
Definition: ascii.cc:52
const char *const keyauxid
key for WID2 line header field
Definition: ascii.cc:50
const char *const keychannel
key for WID2 line header field
Definition: ascii.cc:49
Root namespace of library.
Definition: aalibdatrwxx.cc:16
sff::SRCE srce() const
return SRCE data
Definition: datwrite.h:171
const char *const keydate
key for WID2 line header field
Definition: ascii.cc:45
virtual void writefileheader()
actually write the file header
Definition: oasciistream.cc:91
const char *const keynstacks
key for INFO line header field
Definition: ascii.cc:68
oasciistream(std::ostream &os, const std::string &modifier="", const bool &debug=false)
Definition: oasciistream.cc:57
const char *const keySRCEdate
key for SRCE line header field
Definition: ascii.cc:57
#define POUT(K, V)
Definition: oasciistream.cc:47
sff::FREE free() const
return FREE data
Definition: datwrite.h:175
const char *const keyRECVX
key for INFO line header field
Definition: ascii.cc:64
const char *const keycalper
key for WID2 line header field
Definition: ascii.cc:53
#define DATRW_debug(C, N, M)
produce debug output
Definition: debug.h:50
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
const char *const keynsamples
key for WID2 line header field
Definition: ascii.cc:47
void writetraceheader(const unsigned int &n, const char *type)
const char *const keySRCEX
key for SRCE line header field
Definition: ascii.cc:59
const char *const keyRECVZ
key for INFO line header field
Definition: ascii.cc:66
const char *const keySRCECS
key for SRCE line header field
Definition: ascii.cc:62
const char *const keyfloat
key for DATA type header field
Definition: ascii.cc:72
const char *const keyvang
key for WID2 line header field
Definition: ascii.cc:55
#define DATRW_value(V)
report value
Definition: debug.h:65
#define FOUT(V)
Definition: oasciistream.cc:48
bool hasfree() const
free is available
Definition: datwrite.h:184
const char *const keystation
key for WID2 line header field
Definition: ascii.cc:48
common description of formats (prototypes)
const char *const keydata
key for DATA type header field
Definition: ascii.cc:70
const char *const keySRCEZ
key for SRCE line header field
Definition: ascii.cc:61
interface to write ASCII data (prototypes)