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

View File

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

View File

@ -12,4 +12,4 @@ print(eatraw.unit())
print(eatraw.get_time())
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_data()
# 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):
return self.rawit.get_data()
def print(self, const char* csvfile):
return self.rawit.write_data(csvfile)
def write_table(self, const char* csvfile, char delimiter):
return self.rawit.write_table(csvfile,delimiter)

View File

@ -57,7 +57,8 @@ int main(int argc, char* argv[])
// for ( unsigned long int i = 0; i < 10; i++ ) std::cout<<mydata[i]<<"\n";
// 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;
}