MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
StreamerBase.cpp
Go to the documentation of this file.
1 /***
2  * Filename: StreamerBase.cpp
3  *
4  * Description: Stream table data.
5  *
6  * Version: 0.0.1
7  * Created: 2016-04-26
8 
9  * Revision: none
10  *
11  * Author: Dilawar Singh <dilawars@ncbs.res.in>
12  * Organization: NCBS Bangalore
13  *
14  * License: GNU GPL2
15  */
16 
17 
18 #include "global.h"
19 #include "header.h"
20 #include "StreamerBase.h"
21 
22 #include "../scheduling/Clock.h"
23 #include "../utility/cnpy.hpp"
24 
25 #include <algorithm>
26 #include <sstream>
27 #include <memory>
28 
29 // Class function definitions
31 {
32 }
33 
35 {
36  this->outfilePath_ = st.outfilePath_;
37  return *this;
38 }
39 
40 
42 {
43 
44 }
45 
46 
47 string StreamerBase::getOutFilepath( void ) const
48 {
49  return outfilePath_;
50 }
51 
52 void StreamerBase::setOutFilepath( string filepath )
53 {
54  outfilePath_ = filepath;
55 }
56 
57 
58 void StreamerBase::writeToOutFile( const string& filepath
59  , const string& outputFormat
60  , const string& openmode
61  , const vector<double>& data
62  , const vector<string>& columns
63  )
64 {
65  if( data.size() == 0 )
66  return;
67 
68  if( "npy" == outputFormat )
69  writeToNPYFile( filepath, openmode, data, columns );
70  else if( "csv" == outputFormat or "dat" == outputFormat )
71  writeToCSVFile( filepath, openmode, data, columns );
72  else
73  {
74  LOG( moose::warning, "Unsupported format " << outputFormat
75  << ". Use npy or csv. Falling back to default csv"
76  );
77  writeToCSVFile( filepath, openmode, data, columns );
78  }
79 }
80 
81 
82 /* Write to a csv file. */
83 void StreamerBase::writeToCSVFile( const string& filepath, const string& openmode
84  , const vector<double>& data, const vector<string>& columns )
85 {
86 
87  FILE* fp = fopen( filepath.c_str(), openmode.c_str() );
88  if( NULL == fp )
89  {
90  LOG( moose::warning, "Failed to open " << filepath );
91  return;
92  }
93 
94  // If writing in "w" mode, write the header first.
95  if( openmode == "w" )
96  {
97  string headerText = "";
98  for( vector<string>::const_iterator it = columns.begin();
99  it != columns.end(); it++ )
100  headerText += ( *it + delimiter_ );
101  headerText += eol;
102  fprintf( fp, "%s", headerText.c_str() );
103  }
104 
105  string text = "";
106  for( size_t i = 0; i < data.size(); i+=columns.size() )
107  {
108  // Start of a new row.
109  for( size_t ii = 0; ii < columns.size(); ii++ )
110  text += moose::toString( data[i+ii] ) + delimiter_;
111 
112  // At the end of each row, we remove the delimiter_ and append newline_.
113  *(text.end()-1) = eol;
114  }
115  fprintf( fp, "%s", text.c_str() );
116  fclose( fp );
117 }
118 
119 /* write data to a numpy file */
120 void StreamerBase::writeToNPYFile( const string& filepath, const string& openmode
121  , const vector<double>& data, const vector<string>& columns )
122 {
123  cnpy2::save_numpy<double>( filepath, data, columns, openmode );
124 }
125 
string toString(double x)
Convert a given value to string.
Definition: global.cpp:199
string outfilePath_
Definition: StreamerBase.h:91
static void writeToNPYFile(const string &filepath, const string &openmode, const vector< double > &data, const vector< string > &columns)
Write to NPY format. See the documentation of writeToOutfile for more details.
StreamerBase & operator=(const StreamerBase &st)
const vector< double > & data()
Definition: TableBase.cpp:498
static const char eol
Definition: StreamerBase.h:93
static const char delimiter_
Definition: StreamerBase.h:94
static void writeToCSVFile(const string &filepath, const string &openmode, const vector< double > &data, const vector< string > &columns)
Write data to csv file. See the documentation of writeToOutfile for details.
static void writeToOutFile(const string &filepath, const string &format, const string &openmode, const vector< double > &data, const vector< string > &columns)
Write to a output file in given format.
void setOutFilepath(string stream)
string getOutFilepath() const