offer multiple csv-like output formats

This commit is contained in:
Mario Fink 2020-07-24 12:33:56 +02:00
parent 4ef68695b3
commit 31e2d7a82e
6 changed files with 27 additions and 32 deletions

View File

@ -214,12 +214,11 @@ public:
totalmarksize += datasec_[mrk.first].size(); totalmarksize += datasec_[mrk.first].size();
} }
// std::cout<<"totalmarksize "<<totalmarksize<<"\n"; // std::cout<<"totalmarksize "<<totalmarksize<<"\n";
// std::cout<<"\n";
// check validity of format // check validity of format
// assert ( totalmarksize > 0 && "didn't find any predefined marker => probably not a valid .raw-file" ); // assert ( totalmarksize > 0 && "didn't find any predefined marker => probably not a valid .raw-file" );
if ( totalmarksize < 100 ) valid_ = false; if ( totalmarksize < 100 ) valid_ = false;
std::cout<<"\n";
} }
// get all predefined markers // get all predefined markers
@ -573,38 +572,29 @@ public:
return segments_[marker]; return segments_[marker];
} }
// write data to csv-like file // write csv file
void write_data(std::string filename, int precision = 9, int width = 25) void write_table(std::string filename, char delimiter = ',', int precision = 6, int width = 25)
{ {
// assert ( segments_.size() > 0 );
// assert ( datmes_.size() > 0 );
if ( valid_ ) if ( valid_ )
{ {
// open file // open file
std::ofstream fout(filename.c_str()); std::ofstream fout(filename.c_str());
// write header // define column names (including units)
// fout<<"# ";
std::string colA = std::string("Time [") + get_temp_unit() + std::string("]"); std::string colA = std::string("Time [") + get_temp_unit() + std::string("]");
std::string colB = get_name() + std::string(" [") + get_unit() + std::string("]"); std::string colB = get_name() + std::string(" [") + get_unit() + std::string("]");
if ( width > 0 )
// write header
if ( delimiter != ' ' )
{ {
// fout<<std::setw(width)<<std::left<<colA; fout<<colA<<delimiter<<colB<<"\n";
// fout<<std::setw(width)<<std::left<<colB;
fout<<std::setw(width)<<std::right<<"Time";
fout<<std::setw(width)<<std::right<<get_name();
fout<<"\n";
fout<<std::setw(width)<<std::right<<get_temp_unit();
fout<<std::setw(width)<<std::right<<get_unit();
} }
else else
{ {
// fout<<colA<<","<<colB; fout<<std::setw(width)<<std::right<<colA;
fout<<"Time"<<","<<get_name()<<"\n"; fout<<std::setw(width)<<std::right<<colB;
fout<<get_temp_unit()<<";"<<get_unit(); fout<<"\n";
} }
fout<<"\n";
// get time step and offset // get time step and offset
double dt = get_dt(); double dt = get_dt();
@ -617,16 +607,19 @@ public:
// get time // get time
double tim = tidx*dt + timoff; double tim = tidx*dt + timoff;
if ( width > 0 ) if ( delimiter != ' ' )
{ {
fout<<std::fixed<<std::dec<<std::setprecision(precision)<<std::setw(width)<<std::right<<tim; fout<<std::fixed<<std::dec<<std::setprecision(precision)
fout<<std::fixed<<std::dec<<std::setprecision(precision)<<std::setw(width)<<std::right<<el; <<tim<<delimiter<<el<<"\n";
} }
else else
{ {
fout<<std::fixed<<std::dec<<std::setprecision(precision)<<tim<<","<<el; fout<<std::fixed<<std::dec<<std::setprecision(precision)
<<std::setw(width)<<std::right<<tim;
fout<<std::fixed<<std::dec<<std::setprecision(precision)
<<std::setw(width)<<std::right<<el;
fout<<"\n";
} }
fout<<"\n";
// keep track of timestep // keep track of timestep
tidx++; tidx++;

View File

@ -25,6 +25,7 @@ eatall : $(SRC)eatall.cpp $(LIB)raweat.hpp
clean : clean :
rm -f $(EXE) rm -f $(EXE)
rm -f eatall rm -f eatall
rm -f eatdev
# check existence of name of executable globally # check existence of name of executable globally
chexe:=$(shell command -v $(EXE)) chexe:=$(shell command -v $(EXE))

View File

@ -12,4 +12,4 @@ print(eatraw.unit())
print(eatraw.get_time()) print(eatraw.get_time())
print(eatraw.get_channel()) print(eatraw.get_channel())
eatraw.print(b"mycsv.csv") eatraw.write_table(b"mycsv.csv",ord(' '))

View File

@ -26,4 +26,4 @@ cdef extern from "lib/raweat.hpp":
vector[double] get_time() vector[double] get_time()
vector[double] get_data() vector[double] get_data()
# dump all data to .csv # dump all data to .csv
void write_data(const char*) void write_table(const char*,char delimiter)

View File

@ -35,5 +35,5 @@ cdef class raweater:
def get_channel(self): def get_channel(self):
return self.rawit.get_data() return self.rawit.get_data()
def print(self, const char* csvfile): def write_table(self, const char* csvfile, char delimiter):
return self.rawit.write_data(csvfile) return self.rawit.write_table(csvfile,delimiter)

View File

@ -51,13 +51,14 @@ int main(int argc, char* argv[])
// //for ( auto el: segvec ) std::cout<<el<<"\n"; // //for ( auto el: segvec ) std::cout<<el<<"\n";
// } // }
// get array of encoded data // get array of encoded data
// std::vector<double> maindata = eatraw.get_data(); // std::vector<double> maindata = eatraw.get_data();
// std::cout<<"\nsize of data array: "<<maindata.size()<<"\n\n"; // std::cout<<"\nsize of data array: "<<maindata.size()<<"\n\n";
// for ( unsigned long int i = 0; i < 10; i++ ) std::cout<<mydata[i]<<"\n"; // for ( unsigned long int i = 0; i < 10; i++ ) std::cout<<mydata[i]<<"\n";
// write data in csv-file // write data in csv-file
eatraw.write_data(std::string(argv[2])); // eatraw.write_data(std::string(argv[2]));
eatraw.write_table(std::string(argv[2]),' ');
return 0; return 0;
} }