Commit 64179a1e authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

nsignificantdigits() function for WIDX line compilation

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.
now can safely handle zeroes


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 3050
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 2bed486b
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: sffxx.h,v 1.37 2010-04-13 21:32:34 tforb Exp $
* $Id: sffxx.h,v 1.38 2010-05-03 11:07:22 tforb Exp $
* \author Thomas Forbriger
* \date 21/12/2003
*
......@@ -43,6 +43,7 @@
* - 13/04/2010 V1.8
* - start implementing WIDX
* - provide error handling macros
* - 03/05/2010 V1.9 provide debug output for WIDX compilation
*
* ============================================================================
*/
......@@ -51,9 +52,9 @@
#ifndef TF_SFFXX_H_VERSION
#define TF_SFFXX_H_VERSION \
"TF_SFFXX_H V1.8"
"TF_SFFXX_H V1.9"
#define TF_SFFXX_H_CVSID \
"$Id: sffxx.h,v 1.37 2010-04-13 21:32:34 tforb Exp $"
"$Id: sffxx.h,v 1.38 2010-05-03 11:07:22 tforb Exp $"
#include<string>
#include<cmath>
......@@ -579,7 +580,7 @@ namespace sff {
const double& radius=6371.);
//! write WID2 information in extended format
std::string WIDXline(const sff::WID2& wid2);
std::string WIDXline(const sff::WID2& wid2, const bool& debug=false);
//! read WID2 information with extended format
sff::WID2 WIDXline(const std::string& line);
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: widXio.cc,v 1.3 2010-04-13 21:32:34 tforb Exp $
* $Id: widXio.cc,v 1.4 2010-05-03 11:07:22 tforb Exp $
* \author Thomas Forbriger
* \date 13/04/2010
*
......@@ -29,13 +29,17 @@
*
* REVISIONS and CHANGES
* - 13/04/2010 V1.0 Thomas Forbriger
* - 03/05/2010 V1.1
* - provide debug output
* - function nsignificantdigits() now safely takes
* zeroes as input values
*
* ============================================================================
*/
#define TF_WIDXIO_CC_VERSION \
"TF_WIDXIO_CC V1.0"
"TF_WIDXIO_CC V1.1"
#define TF_WIDXIO_CC_CVSID \
"$Id: widXio.cc,v 1.3 2010-04-13 21:32:34 tforb Exp $"
"$Id: widXio.cc,v 1.4 2010-05-03 11:07:22 tforb Exp $"
#include <sffxx.h>
#include <sstream>
......@@ -45,20 +49,43 @@ namespace sff {
namespace helper {
//! return number of significant digits
int nsignificantdigits(double v)
int nsignificantdigits(double v, const bool& debug=false)
{
const double epsilon=1.e-30;
if (debug)
{
std::cerr << "DEBUG (nsignificantdigits): "
<< "entered function for value:" << v << std::endl;
}
if (v<0) { v = -v; }
int n=0;
double basefactor=pow(10.,-floor(log10(v)));
double v1,v2;
do
{
double factor=basefactor*pow(10.,n);
v1=floor(v*factor);
v2=floor(v*factor*10.)/10.;
n++;
if (v<epsilon)
{
n=2;
}
else
{
double basefactor=pow(10.,-floor(log10(v)));
double v1,v2;
do
{
double factor=basefactor*pow(10.,n);
v1=floor(v*factor);
v2=floor(v*factor*10.)/10.;
n++;
if (debug)
{
std::cerr << "DEBUG (nsignificantdigits): "
<< "basefactor: " << basefactor << " "
<< "factor: " << factor << " "
<< "n: " << n << " "
<< "v1: " << v1 << " "
<< "v2: " << v2 << " "
<< std::endl;
}
}
while (v1 != v2);
}
while (v1 != v2);
return (n);
} // int nsignificantdigits(const double& v)
......@@ -88,8 +115,13 @@ namespace sff {
*
* \sa WIDXline(const std::string& line)
*/
std::string WIDXline(const sff::WID2& wid2)
std::string WIDXline(const sff::WID2& wid2, const bool& debug)
{
if (debug)
{
std::cerr << "DEBUG (WIDXline): entered function" <<
std::endl;
}
std::ostringstream os;
os << sff::WIDXID;
os << " " << wid2.date.hierarchicalstring();
......@@ -110,6 +142,11 @@ namespace sff {
os.setf(std::ios_base::left, std::ios_base::adjustfield);
os << wid2.instype.substr(0,6);
os << " " << wid2.nsamples;
if (debug)
{
std::cerr << "DEBUG (WIDXline): partial result:" <<
os.str() << std::endl;
}
double rate=1./wid2.dt;
double value=wid2.dt;
if (sff::helper::nsignificantdigits(rate) < 4)
......@@ -121,23 +158,38 @@ namespace sff {
{
os << " i ";
}
if (debug)
{
std::cerr << "DEBUG (WIDXline): partial result:" <<
os.str() << std::endl;
}
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
int ndigits=sff::helper::nsignificantdigits(value);
int ndigits=sff::helper::nsignificantdigits(value, debug);
if (debug)
{
std::cerr << "DEBUG (WIDXline): ndigits:" <<
ndigits << std::endl;
}
os.precision(ndigits);
os.width(ndigits+6);
os << value;
os << " ";
os.precision(sff::helper::nsignificantdigits(wid2.calib));
os.precision(sff::helper::nsignificantdigits(wid2.calib, debug));
os << wid2.calib;
os << " ";
os.precision(sff::helper::nsignificantdigits(wid2.calper));
os.precision(sff::helper::nsignificantdigits(wid2.calper, debug));
os << wid2.calper;
os << " ";
os.precision(sff::helper::nsignificantdigits(wid2.hang));
os.precision(sff::helper::nsignificantdigits(wid2.hang, debug));
os << wid2.hang;
os << " ";
os.precision(sff::helper::nsignificantdigits(wid2.vang));
os.precision(sff::helper::nsignificantdigits(wid2.vang, debug));
os << wid2.vang;
if (debug)
{
std::cerr << "DEBUG (WIDXline): final result:" <<
os.str() << std::endl;
}
return(os.str());
} // std::string WIDXline(const sff::WID2& wid2)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment