SFF++ library: reading and writing SFF from C++
widXio.cc
Go to the documentation of this file.
1 
38 #define TF_WIDXIO_CC_VERSION \
39  "TF_WIDXIO_CC V1.1"
40 
41 #include <sffxx.h>
42 #include <sstream>
43 
44 namespace sff {
45 
46  namespace helper {
47 
49  int nsignificantdigits(double v, const bool& debug=false)
50  {
51  const double epsilon=1.e-30;
52  if (debug)
53  {
54  std::cerr << "DEBUG (nsignificantdigits): "
55  << "entered function for value:" << v << std::endl;
56  }
57  if (v<0) { v = -v; }
58  int n=0;
59  if (v<epsilon)
60  {
61  n=2;
62  }
63  else
64  {
65  double basefactor=pow(10.,-floor(log10(v)));
66  double v1,v2;
67  do
68  {
69  double factor=basefactor*pow(10.,n);
70  v1=v*factor;
71  v2=floor(v*factor);
72  n++;
73  if (debug)
74  {
75  std::cerr << "DEBUG (nsignificantdigits): "
76  << "basefactor: " << basefactor << " "
77  << "factor: " << factor << " "
78  << "n: " << n << " "
79  << "v1: " << v1 << " "
80  << "v2: " << v2 << " "
81  << std::endl;
82  }
83  }
84  while (v1 != v2);
85  }
86  return (n);
87  } // int nsignificantdigits(const double& v)
88 
89  } // namespace helper
90 
91  /*----------------------------------------------------------------------*/
92 
115  std::string WIDXline(const sff::WID2& wid2, const bool& debug)
116  {
117  if (debug)
118  {
119  std::cerr << "DEBUG (WIDXline): entered function" <<
120  std::endl;
121  }
122  std::ostringstream os;
123  os << sff::WIDXID;
124  os << " " << wid2.date.hierarchicalstring();
125  os << " ";
126  os.width(5);
127  os.setf(std::ios_base::left, std::ios_base::adjustfield);
128  os << wid2.station.substr(0,5);
129  os << " ";
130  os.width(3);
131  os.setf(std::ios_base::left, std::ios_base::adjustfield);
132  os << wid2.channel.substr(0,3);
133  os << " ";
134  os.width(4);
135  os.setf(std::ios_base::left, std::ios_base::adjustfield);
136  os << wid2.auxid.substr(0,4);
137  os << " ";
138  os.width(6);
139  os.setf(std::ios_base::left, std::ios_base::adjustfield);
140  os << wid2.instype.substr(0,6);
141  os << " " << wid2.nsamples;
142  if (debug)
143  {
144  std::cerr << "DEBUG (WIDXline): partial result:" <<
145  os.str() << std::endl;
146  }
147  double rate=1./wid2.dt;
148  double value=wid2.dt;
149  if (sff::helper::nsignificantdigits(rate) < 4)
150  {
151  os << " r ";
152  value=rate;
153  }
154  else
155  {
156  os << " i ";
157  }
158  if (debug)
159  {
160  std::cerr << "DEBUG (WIDXline): partial result:" <<
161  os.str() << std::endl;
162  }
163  os.setf(std::ios_base::scientific, std::ios_base::floatfield);
164  int ndigits=sff::helper::nsignificantdigits(value, debug);
165  if (debug)
166  {
167  std::cerr << "DEBUG (WIDXline): ndigits:" <<
168  ndigits << std::endl;
169  }
170  os.precision(ndigits);
171  os.width(ndigits+6);
172  os << value;
173  os << " ";
174  os.precision(sff::helper::nsignificantdigits(wid2.calib, debug));
175  os << wid2.calib;
176  os << " ";
177  os.precision(sff::helper::nsignificantdigits(wid2.calper, debug));
178  os << wid2.calper;
179  os << " ";
180  os.precision(sff::helper::nsignificantdigits(wid2.hang, debug));
181  os << wid2.hang;
182  os << " ";
183  os.precision(sff::helper::nsignificantdigits(wid2.vang, debug));
184  os << wid2.vang;
185  if (debug)
186  {
187  std::cerr << "DEBUG (WIDXline): final result:" <<
188  os.str() << std::endl;
189  }
190  return(os.str());
191  } // std::string WIDXline(const sff::WID2& wid2)
192 
193  /*----------------------------------------------------------------------*/
194 
199  sff::WID2 WIDXline(const std::string& line)
200  {
201  sff::WID2 retval;
202  SFF_assert(line.substr(0,4) == WIDXID,
203  "wrong line ID");
204  retval.date=libtime::TAbsoluteTime(line.substr(5,26));
205  retval.station=line.substr(32,5);
206  retval.channel=line.substr(38,3);
207  retval.auxid=line.substr(42,4);
208  retval.instype=line.substr(47,6);
209  std::istringstream is(line.substr(54));
210  is >> retval.nsamples;
211  std::string type;
212  is >> type;
213  is >> retval.dt;
214  if (type == "r") { retval.dt = 1./retval.dt; }
215  is >> retval.calib;
216  is >> retval.calper;
217  is >> retval.hang;
218  is >> retval.vang;
219  return(retval);
220  } // sff::WID2 WIDXline(const std::string& line)
221 
222 } // namespace sff
223 
224 /* ----- END OF widXio.cc ----- */
all SFF modules
Definition: offset.cc:42
std::string auxid
Auxiliary identification code.
Definition: sffxx.h:218
double calper
calibration reference period
Definition: sffxx.h:222
std::string station
Station code.
Definition: sffxx.h:216
std::string instype
instrument type
Definition: sffxx.h:223
std::string channel
FDSN channel code.
Definition: sffxx.h:217
int nsamples
number of samples
Definition: sffxx.h:219
libtime::TAbsoluteTime date
time of first sample
Definition: sffxx.h:215
#define SFF_assert(C, M)
check condition
Definition: sffxx.h:82
std::string WIDXline(const sff::WID2 &wid2, const bool &debug=false)
write WID2 information in extended format
Definition: widXio.cc:115
double calib
calibration factor
Definition: sffxx.h:221
const char *const WIDXID
ID for extended WID2 format.
Definition: sffxx.h:132
Waveform Header.
Definition: sffxx.h:209
double vang
veritcal orientation
Definition: sffxx.h:225
int nsignificantdigits(double v, const bool &debug=false)
return number of significant digits
Definition: widXio.cc:49
double dt
sampling interval (sec)
Definition: sffxx.h:220
double hang
horizontal orientation
Definition: sffxx.h:224
SFF library (prototypes)