OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
CSVWriter.hpp
1 /*
2  * CSVWriter.hpp
3  *
4  * Created on: Dec 15, 2014
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef CSVWRITER_HPP_
9 #define CSVWRITER_HPP_
10 
11 #include <iostream>
12 #include <boost/fusion/include/mpl.hpp>
13 #include <boost/fusion/include/for_each.hpp>
14 #include <fstream>
15 #include "util/common.hpp"
16 #include <boost/mpl/range_c.hpp>
17 #include "util/for_each_ref_host.hpp"
18 #include "csv_multiarray.hpp"
19 #include "util/util.hpp"
20 #include "is_csv_writable.hpp"
21 
22 #define CSV_WRITER 0x30000
23 
33 template<typename Tobj>
34 struct csv_prp
35 {
37  std::stringstream & str;
38 
40  Tobj & obj;
41 
50  csv_prp(std::stringstream & str, Tobj & obj)
51  :str(str),obj(obj)
52  {
53  };
54 
56  template<typename T>
57  void operator()(T& t)
58  {
59  // This is the type of the csv column
60  typedef typename boost::mpl::at<typename Tobj::type,T>::type col_type;
61 
62  // Remove the reference from the column type
63  typedef typename boost::remove_reference<col_type>::type col_rtype;
64  typedef typename std::remove_all_extents<col_rtype>::type base_col_rtype;
65 
67  }
68 };
69 
79 template<typename Tobj, bool attr>
80 struct csv_col
81 {
83  std::stringstream & str;
84 
90  csv_col(std::stringstream & str)
91  :str(str)
92  {
93  };
94 
96  template<typename T>
97  inline void operator()(T& t)
98  {
99  // This is the type of the csv column
100  typedef typename boost::mpl::at<typename Tobj::type,boost::mpl::int_<T::value>>::type col_type;
101 
102  csv_col_str<col_type>(std::string(Tobj::attributes::name[T::value]),str);
103  }
104 };
105 
119 template<typename Tobj>
120 struct csv_col<Tobj,false>
121 {
123  std::stringstream & str;
124 
130  csv_col(std::stringstream & str)
131  :str(str)
132  {
133  };
134 
136  template<typename T>
137  void operator()(T& t)
138  {
139  // This is the type of the csv column
140  typedef typename boost::fusion::result_of::at_c<typename Tobj::type,T::value>::type col_type;
141 
142  // Remove the reference from the column type
143  typedef typename boost::remove_reference<col_type>::type col_rtype;
144 
145  std::stringstream str2;
146  str2 << "column_" << T::value;
147 
148  csv_col_str<col_rtype>(str2.str(),str);
149  }
150 };
151 
152 //#define VECTOR 1
153 
162 template <typename v_pos, typename v_prp, unsigned int impl = 1>
164 {
168  std::string get_csv_colums()
169  {
170  std::stringstream str;
171 
172  // write positional columns
173  for (size_t i = 0 ; i < v_pos::value_type::dims ; i++)
174  {
175  if (i == 0)
176  str << "x[" << i << "]";
177  else
178  str << "," << "x[" << i << "]";
179  }
180 
181  // write positional information
182 
184 
185  // Iterate through all the vertex and create the vertex list
186  boost::mpl::for_each_ref_host< boost::mpl::range_c<int,0,v_prp::value_type::max_prop> >(col);
187 
188  str << "\n";
189 
190  return str.str();
191  }
192 
200  std::string get_csv_data(v_pos & vp, v_prp & vpr, size_t offset)
201  {
202  std::stringstream str;
203 
204  // The position and property vector size must match
205  if (vp.size() != vpr.size())
206  {
207  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " position vector and property vector must have the same size \n";
208  return std::string("");
209  }
210 
211  // Write the data
212  for (size_t i = offset ; i < vp.size() ; i++)
213  {
214  for (size_t j = 0 ; j < v_pos::value_type::dims ; j++)
215  {
216  if (j == 0)
217  str << vp.template get<0>(i)[j];
218  else
219  str << "," << vp.template get<0>(i)[j];
220  }
221 
222  // Object to write
223  auto obj = vpr.get(i);
224 
225  csv_prp<decltype(obj)> c_prp(str,obj);
226 
227  // write the properties to the stream string
228  boost::mpl::for_each_ref_host< boost::mpl::range_c<int,0,v_prp::value_type::max_prop> >(c_prp);
229 
230  str << "\n";
231  }
232 
233  return str.str();
234  }
235 
236 public:
237 
248  bool write(std::string file, v_pos & v , v_prp & prp, size_t offset=0)
249  {
250  // Header for csv (colums name)
251  std::string csv_header;
252  // Data point
253  std::string point_data;
254 
255  // Get csv columns
256  csv_header = get_csv_colums();
257 
258  // For each property in the vertex type produce a point data
259  point_data = get_csv_data(v,prp,offset);
260 
261  // write the file
262  std::ofstream ofs(file);
263 
264  // Check if the file is open
265  if (ofs.is_open() == false)
266  {std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " cannot create the CSV file: " << file << std::endl;}
267 
268  ofs << csv_header << point_data;
269 
270  // Close the file
271 
272  ofs.close();
273 
274  // Completed succefully
275  return true;
276  }
277 };
278 
279 
280 #endif /* CSVWRITER_HPP_ */
281 
std::stringstream & str
String containing the colums list as string.
Definition: CSVWriter.hpp:83
std::stringstream & str
String containing the csv line constructed from an object.
Definition: CSVWriter.hpp:37
CSV Writer.
Definition: CSVWriter.hpp:163
csv_col(std::stringstream &str)
Constructor.
Definition: CSVWriter.hpp:90
Tobj & obj
Object to write.
Definition: CSVWriter.hpp:40
void operator()(T &t)
It call the functor for each member.
Definition: CSVWriter.hpp:57
bool write(std::string file, v_pos &v, v_prp &prp, size_t offset=0)
It write a CSV file.
Definition: CSVWriter.hpp:248
This class is an helper to produce csv data from multi-array.
void operator()(T &t)
It call the functor for each member.
Definition: CSVWriter.hpp:97
This class is an helper to produce csv headers from multi-array.
csv_prp(std::stringstream &str, Tobj &obj)
Constructor.
Definition: CSVWriter.hpp:50
csv_col(std::stringstream &str)
Constructor.
Definition: CSVWriter.hpp:130
void operator()(T &t)
It call the functor for each member.
Definition: CSVWriter.hpp:137
this class is a functor for "for_each" algorithm
Definition: CSVWriter.hpp:34
std::string get_csv_colums()
Get the colums name (also the positional name)
Definition: CSVWriter.hpp:168
this class is a functor for "for_each" algorithm
Definition: CSVWriter.hpp:80
std::string get_csv_data(v_pos &vp, v_prp &vpr, size_t offset)
Get the csv data section.
Definition: CSVWriter.hpp:200
std::stringstream & str
String containing the colums list as string.
Definition: CSVWriter.hpp:123