* rawmerge.hpp: generalize/fix condition for consistent timeseries

* introduce propagation of C++ exceptions to Cython/Python
* convert all cout/cerr output to exceptions
* Cython: introduce separate 'do_conversion' => avoid constructor to
trigger conversion
This commit is contained in:
Mario Fink
2020-09-18 18:43:25 +02:00
parent 3611e43276
commit 043784d278
9 changed files with 135 additions and 128 deletions

View File

@@ -80,7 +80,7 @@ public:
raw_eater(std::string rawfile, bool showlog = false) : rawfile_(rawfile)
{
// trigger setup and conversion
if ( ! rawfile.empty() ) this->setup_and_conversion(showlog);
//if ( ! rawfile.empty() ) this->setup_and_conversion(showlog);
}
raw_eater()
@@ -88,6 +88,12 @@ public:
}
// destructor
~raw_eater()
{
}
// provide/set new raw file
void set_file(std::string rawfile, bool showlog = false)
{
@@ -100,11 +106,11 @@ public:
datmes_.clear();
// do setup and conversion
setup_and_conversion(showlog);
//setup_and_conversion(showlog);
}
// set up and conversion
void setup_and_conversion(bool showlog)
void setup_and_conversion(bool showlog = false)
{
// make sure 'raw_file' is already set
assert ( !rawfile_.empty() );
@@ -123,10 +129,8 @@ public:
if ( showlog )
{
// show raw data
// show raw data and display predefined markers
this->show_buffer();
// display predefined markers
this->show_markers();
}
@@ -150,12 +154,6 @@ public:
}
}
// destructor
~raw_eater()
{
}
// display buffer/data properties
void show_buffer(int numel = 32)
{
@@ -239,10 +237,16 @@ public:
unsigned long int totalmarksize = 0;
for (std::pair<std::string,std::vector<unsigned char>> mrk : markers_ )
{
//assert ( datasec_[mrk.first].size() > 0 && "marker segment of length zero" );
if ( datasec_[mrk.first].size() == 0 )
{
std::cout<<"warning: "<<mrk.first<<" not found in buffer\n";
std::string errmess = mrk.first + std::string(" not found in buffer");
// std::cerr<<errmess;
try {
throw std::runtime_error(errmess);
} catch( const std::exception& e ) {
throw;
//std::cout<<e.what()<<"\n";
}
}
totalmarksize += datasec_[mrk.first].size();
}
@@ -250,7 +254,6 @@ public:
// std::cout<<"\n";
// check validity of format
// assert ( totalmarksize > 0 && "didn't find any predefined marker => probably not a valid .raw-file" );
valid_ = ( totalmarksize < 100 ) ? false : true;
}
@@ -389,7 +392,6 @@ public:
}
else
{
// switch for datatypes
switch ( dattype )
{
@@ -414,9 +416,9 @@ public:
convert_data_as_type<unsigned long int>(datbuf,factor,offset);
break;
case 6 :
std::cout<<"warning: 'signed long int' datatype with experimental support\n";
// assert ( sizeof(signed long int)*8 == typesize );
// convert_data_as_type<signed long int>(datbuf,factor,offset);
// throw std::runtime_error("warning: 'signed long int' datatype with experimental support");
assert ( sizeof(int)*8 == typesize );
convert_data_as_type<int>(datbuf,factor,offset);
break;
@@ -429,13 +431,13 @@ public:
convert_data_as_type<double>(datbuf,factor,offset);
break;
case 9 :
std::cerr<<"'imc Devices Transitional Recording' datatype not supported\n";
throw std::runtime_error("'imc Devices Transitional Recording' datatype not supported");
break;
case 10 :
std::cerr<<"'Timestamp Ascii' datatype not supported\n";
throw std::runtime_error("'Timestamp Ascii' datatype not supported");
break;
case 11 :
std::cout<<"warning: '2-Byte-Word digital' datatype with experimental support\n";
// throw std::runtime_error("warning: '2-Byte-Word digital' datatype with experimental support");
assert ( sizeof(short int)*8 == typesize );
convert_data_as_type<short int>(datbuf,factor,offset);
break;

View File

@@ -83,13 +83,19 @@ public:
{
// set raw file and perform conversion
this->set_file(rawfile,showlog);
// try {
this->setup_and_conversion();
// } catch (const std::exception& e) {
// // throw;
// std::cout<<e.what();
// }
// show channel name, unit, timestep, time unit, etc.
if ( showlog && this->get_valid() )
{
std::cout<<this->get_name()<<" ["<<this->get_unit()<<"]"<<"\n";
std::cout<<"Time ["<<this->get_temp_unit()<<"]"<<"\n";
for ( unsigned long int i = 0; i < 5; i++ ) std::cout<<this->get_time()[i]<<"\n";
std::cout<<this->get_name()<<" ["<<this->get_unit()<<"]"<<"\n";
for ( unsigned long int i = 0; i < 5; i++ ) std::cout<<this->get_data()[i]<<"\n";
std::cout<<"lenght of channel "<<this->get_time().size()<<"\n";
std::cout<<"\n";
@@ -126,12 +132,9 @@ public:
std::vector<double> td = this->get_data();
// compare start/end of timeseries (define tolerance)
double deltat = 7*fmax(this->get_dt(),this->dt_);
if ( ( this->timeseries_[0] - ts[0] < deltat )
&& ( this->timeseries_.back() - ts.back() < deltat ) )
// double tol = 0.001;
// if ( ( (this->timeseries_[0]-ts[0])/ts[0] < tol )
// && ( (this->timeseries_.back()-ts.back())/ts.back() < tol ) )
double deltat = 9*fmax(this->get_dt(),this->dt_);
if ( ( fabs( this->timeseries_[0] - ts[0] ) < deltat )
&& ( fabs( this->timeseries_.back() - ts.back() ) < deltat ) )
{
// resulting new time series
std::vector<double> newts;
@@ -155,9 +158,14 @@ public:
else
{
// refuse to merge due to inconsistent start of timeseries
std::cerr<<"rawmerge: add_channel '"<<rawfile
<<"' : inconsistent start of time series, i.e. "
<<timeseries_[0]<<" vs. "<<ts[0]<<"\n";
std::string errmess = std::string("rawmerge: add_channel '") + rawfile
+ std::string("' : inconsistent start of time series, i.e. ")
+ std::to_string(timeseries_[0]) + std::string(" vs. ")
+ std::to_string(ts[0])
+ std::string("( timesteps: ") + std::to_string(this->get_dt())
+ std::string(" and ") + std::to_string(this->dt_) + std::string(")");
// std::cerr<<errmess<<"\n";
throw std::runtime_error(errmess);
return false;
}
@@ -165,9 +173,12 @@ public:
else
{
// refuse to merge due to different temporal units
std::cerr<<"rawmerge: add_channel '"<<rawfile
<<"' : inconsistent time units: '"
<<this->get_temp_unit()<<"' versus '"<<this->temp_unit_<<"'\n";
std::string errmess = std::string("rawmerge: add_channel '") + rawfile
+ std::string("' : inconsistent time units: ', i.e. '")
+ this->get_temp_unit() + std::string("' vs. '")
+ this->temp_unit_ + std::string("'");
// std::cerr<<errmess<<"\n";
throw std::runtime_error(errmess);
return false;
}
@@ -175,7 +186,10 @@ public:
else
{
// provided file does not feature a valid .raw format
std::cerr<<"rawmerge: add_channel '"<<rawfile<<"' : invalid .raw file\n";
std::string errmess = std::string("rawmerge: add_channel '") + rawfile
+ std::string("' : invalid .raw file");
// std::cerr<<errmess<<"\n";
throw std::runtime_error(errmess);
return false;
}