DATRW++ library: seismic data I/O with multiple formats
seifeio.cc
Go to the documentation of this file.
1 /*! \file seifeio.cc
2  * \brief seife input/output (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 14/02/2011
8  *
9  * seife input/output (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  * - 14/02/2011 V1.0 Thomas Forbriger
32  *
33  * ============================================================================
34  */
35 #define DATRW_SEIFEIO_CC_VERSION \
36  "DATRW_SEIFEIO_CC V1.0"
37 
38 #include <sstream>
39 #include <iomanip>
40 #include <datrwxx/seifeio.h>
41 #include <datrwxx/error.h>
42 #include <datrwxx/debug.h>
43 #include <datrwxx/util.h>
44 
45 namespace datrw {
46 
47  const char* const seife::seife_standard_format="(6(g12.5,1x))";
48  const unsigned int seife::seife_standard_precision=5;
49  const unsigned int seife::seife_standard_width=12;
50  const unsigned int seife::seife_standard_columns=6;
51 
52  namespace seife {
53 
54  /*----------------------------------------------------------------------*/
55 
56  void ParameterLine::set(const std::string& line)
57  {
58  DATRW_debug(Mdebug, "ParameterLine::set",
59  "read values from line:\n" << line);
60  std::istringstream iss(line.substr(0,10));
61  DATRW_assert(iss.good(), "ERROR: iss is not good");
62  DATRW_debug(Mdebug, "ParameterLine::set",
63  DATRW_value(line.substr(0,10)));
64  int nsamples;
65  iss >> nsamples;
66  DATRW_assert(nsamples>0, "ERROR: number of samples is not positive");
67  Mnsamples=static_cast<unsigned int>(nsamples);
68  DATRW_debug(Mdebug, "ParameterLine::set",
70  //DATRW_assert(iss.good(), "ERROR: reading number of samples");
71  iss.clear();
72  iss.str(line.substr(10,20));
73  DATRW_debug(Mdebug, "ParameterLine::set",
74  DATRW_value(iss.str()));
75  iss >> Mformat;
76  DATRW_debug(Mdebug, "ParameterLine::set",
77  DATRW_value(line.substr(10,20)) << "\n"
79  //DATRW_assert(iss.good(), "ERROR: reading Fortran data format");
80  iss.clear();
81  iss.str(line.substr(30,10));
82  DATRW_debug(Mdebug, "ParameterLine::set",
83  DATRW_value(iss.str()));
84  iss >> Mdt;
85  DATRW_assert(Mdt>0., "ERROR: sampling interval is not positive");
86  DATRW_debug(Mdebug, "ParameterLine::set",
87  DATRW_value(line.substr(30,10)) << "\n"
88  DATRW_value(Mdt));
89  //DATRW_assert(iss.good(), "ERROR: reading sampling interval");
90  double tmin, tsec;
91  iss.clear();
92  iss.str(line.substr(40,10));
93  iss >> tmin;
94  DATRW_debug(Mdebug, "ParameterLine::set",
95  DATRW_value(line.substr(40,10)) << "\n"
96  DATRW_value(tmin));
97  //DATRW_assert(iss.good(), "ERROR: reading number of minutes");
98  iss.clear();
99  iss.str(line.substr(50,10));
100  iss >> tsec;
101  DATRW_debug(Mdebug, "ParameterLine::set",
102  DATRW_value(line.substr(50,10)) << "\n"
103  DATRW_value(tsec));
104  //DATRW_assert(iss.good(), "ERROR: reading number of seconds");
105  Mtime=libtime::double2time(tsec)+libtime::double2time(tmin*60.);
106  DATRW_debug(Mdebug, "ParameterLine::set",
107  DATRW_value(Mtime.timestring()));
108  } // void ParameterLine::set(const std::string& line)
109 
110  /*----------------------------------------------------------------------*/
111 
112  std::string ParameterLine::line() const
113  {
114  libtime::TRelativeTime
115  ttmin(Mtime.days(),Mtime.hour(),Mtime.minute());
116  libtime::TRelativeTime ttsec(Mtime);
117  ttsec -= ttmin;
118  double tmin, tsec;
119  tmin=1440.*ttmin.days()+60.*ttmin.hour()+ttmin.minute();
120  tsec=libtime::time2double(ttsec);
121  std::ostringstream oss;
122  oss << std::setw(10) << std::left << Mnsamples;
123  oss << std::setw(20) << std::left << Mformat;
124  oss << std::setw(10) << std::left << std::showpoint <<
125  std::setprecision(3);
127  { oss << std::scientific; }
128  else
129  { oss << std::fixed; }
130  oss << Mdt;
131  oss << std::setw(10) << std::left << std::setprecision(1)
132  << std::fixed << tmin;
133  oss << std::setw(10) << std::left << std::setprecision(6) << tsec;
134  return(oss.str());
135  } // void ParameterLine::set(const std::string& line)
136 
137  /*======================================================================*/
138  // Header
139  // ------
140 
141  void Header::read(std::istream& is)
142  {
143  std::string line;
144  DATRW_assert(is.good(),
145  "ERROR (seife::Header::read): input stream is not good");
146  getline(is, line);
147  Mfree.append(line);
148  DATRW_assert(is.good(),
149  "ERROR (seife::Header::read): input stream is not good");
150  getline(is, line);
151  while (line.substr(0,1) == "%")
152  {
153  Mfree.append(line.substr(1));
154  DATRW_assert(is.good(),
155  "ERROR (seife::Header::read): input stream is not good");
156  getline(is, line);
157  }
158  Mparameters.set(line);
159  } // void Header::read(std::istream& is)
160 
161  /*----------------------------------------------------------------------*/
162 
163  void Header::write(std::ostream& os) const
164  {
165  typedef ::sff::FREE::Tlines Tlines;
166  const Tlines& lines=Mfree.lines;
167  if (lines.empty())
168  {
169  os << DATRW_SEIFEIO_CC_VERSION << std::endl;
170  }
171  else
172  {
173  Tlines::const_iterator I=lines.begin();
174  os << *I << std::endl;
175  ++I;
176  unsigned int nlines=0;
177  while ((I != lines.end()) && (nlines<48))
178  {
179  os << "%" << *I << std::endl;
180  ++I;
181  ++nlines;
182  }
183  }
184  os << Mparameters.line() << std::endl;
185  } // void Header::write(std::ostream& os) const
186 
187  /*======================================================================*/
188 
189  void write_series(std::ostream& os, const Tseries::Tcoc& s)
190  {
191  unsigned int j=1;
192  for (int i=s.f();i<=s.l();++i)
193  {
194  os << std::scientific;
195  os << std::setprecision(seife_standard_precision);
196  os << std::setw(seife_standard_width);
197  os << s(i) << " ";
198  ++j;
200  {
201  j=1;
202  os << std::endl;
203  }
204  }
205  if (j != 1) { os << std::endl; }
206  } // void write_series(std::ostream& os, const Tseries::Tcoc& s)
207 
208  } // namespace seife
209 
210 } // namespace datrw
211 
212 /* ----- END OF seifeio.cc ----- */
unsigned int Mnsamples
number of samples
Definition: seifeio.h:117
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
const unsigned int seife_standard_columns
Definition: seifeio.cc:50
unsigned int nsamples() const
return number of samples
Definition: seifeio.h:92
void read(std::istream &is)
read header from input
Definition: seifeio.cc:141
const char *const seife_standard_format
Definition: seifeio.cc:47
macro function for debugging output (prototypes)
ParameterLine Mparameters
numerical header parameters
Definition: seifeio.h:158
::sff::FREE Mfree
comments
Definition: seifeio.h:160
void set(const std::string &line)
set values from line
Definition: seifeio.cc:56
const unsigned int seife_standard_width
Definition: seifeio.cc:49
exception class declaration for libdatrwxx (prototypes)
bool Mdebug
request debug output
Definition: seifeio.h:115
#define DATRW_SEIFEIO_CC_VERSION
Definition: seifeio.cc:35
const unsigned int seife_standard_precision
Definition: seifeio.cc:48
Root namespace of library.
Definition: aalibdatrwxx.cc:16
utilities used by more than one type of data reader (prototypes)
void write_series(std::ostream &os, const Tseries::Tcoc &s)
write samples to file
Definition: seifeio.cc:189
int ntrailingdigits(double v, const bool &debug)
return number of trailing digits (after decimal point)
Definition: util.cc:97
void write(std::ostream &os) const
! write header to stream
Definition: seifeio.cc:163
#define DATRW_debug(C, N, M)
produce debug output
Definition: debug.h:50
std::string Mformat
Fortran data format.
Definition: seifeio.h:119
double Mdt
sampling interval
Definition: seifeio.h:121
libtime::TRelativeTime Mtime
time of first sample
Definition: seifeio.h:123
#define DATRW_value(V)
report value
Definition: debug.h:65
std::string line() const
create line for output from values
Definition: seifeio.cc:112