OpenFPM_io  0.2.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Functions Variables Typedefs
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 <boost/mpl/for_each.hpp>
18 #include "csv_multiarray.hpp"
19 #include "util.hpp"
20 
21 #define CSV_WRITER 0x30000
22 
32 template<typename Tobj>
33 struct csv_prp
34 {
35  // String
36  std::stringstream & str;
37 
38  // Object to write
39  Tobj & obj;
40 
49  csv_prp(std::stringstream & str, Tobj & obj)
50  :str(str),obj(obj)
51  {
52  };
53 
55  template<typename T>
56  void operator()(T& t)
57  {
58  // This is the type of the csv column
59  typedef decltype(obj.template get<T::value>()) col_type;
60 
61  // Remove the reference from the column type
62  typedef typename boost::remove_reference<col_type>::type col_rtype;
63 
64  csv_value_str<col_rtype>(obj.template get<T::value>(),str);
65  }
66 };
67 
77 template<typename Tobj, bool attr>
78 struct csv_col
79 {
80  std::stringstream & str;
81 
82  csv_col(std::stringstream & str)
83  :str(str)
84  {
85  };
86 
88  template<typename T>
89  inline void operator()(T& t)
90  {
91  // This is the type of the csv column
92  typedef typename boost::mpl::at<typename Tobj::type,boost::mpl::int_<T::value>>::type col_type;
93 
94  csv_col_str<col_type>(std::string(Tobj::attributes::name[T::value]),str);
95  }
96 };
97 
111 template<typename Tobj>
112 struct csv_col<Tobj,false>
113 {
114  std::stringstream & str;
115 
116  csv_col(std::stringstream & str)
117  :str(str)
118  {
119  };
120 
122  template<typename T>
123  void operator()(T& t)
124  {
125  // This is the type of the csv column
126  typedef typename boost::fusion::result_of::at_c<typename Tobj::type,T::value>::type col_type;
127 
128  // Remove the reference from the column type
129  typedef typename boost::remove_reference<col_type>::type col_rtype;
130 
131  std::stringstream str2;
132  str2 << "column_" << T::value;
133 
134  csv_col_str<col_rtype>(str2.str(),str);
135  }
136 };
137 
138 #define VECTOR 1
139 
148 template <typename v_pos, typename v_prp, unsigned int impl = VECTOR>
150 {
154  std::string get_csv_colums()
155  {
156  std::stringstream str;
157 
158  // write positional columns
159  for (size_t i = 0 ; i < v_pos::value_type::dims ; i++)
160  {
161  if (i == 0)
162  str << "x[" << i << "]";
163  else
164  str << "," << "x[" << i << "]";
165  }
166 
167  // write positional information
168 
170 
171  // Iterate through all the vertex and create the vertex list
172  boost::mpl::for_each< boost::mpl::range_c<int,0,v_prp::value_type::max_prop> >(col);
173 
174  str << "\n";
175 
176  return str.str();
177  }
178 
186  std::string get_csv_data(v_pos & vp, v_prp & vpr, size_t offset)
187  {
188  std::stringstream str;
189 
190  // The position and property vector size must match
191  if (vp.size() != vpr.size())
192  {
193  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " position vector and property vector must have the same size \n";
194  return std::string("");
195  }
196 
197  // Write the data
198  for (size_t i = offset ; i < vp.size() ; i++)
199  {
200  for (size_t j = 0 ; j < v_pos::value_type::dims ; j++)
201  {
202  if (j == 0)
203  str << vp.template get<0>(i)[j];
204  else
205  str << "," << vp.template get<0>(i)[j];
206  }
207 
208  // Object to write
209  auto obj = vpr.get(i);
210 
211  csv_prp<decltype(obj)> c_prp(str,obj);
212 
213  // write the properties to the stream string
214  boost::mpl::for_each< boost::mpl::range_c<int,0,v_prp::value_type::max_prop> >(c_prp);
215 
216  str << "\n";
217  }
218 
219  return str.str();
220  }
221 
222 public:
223 
235  bool write(std::string file, v_pos & v , v_prp & prp, size_t offset=0)
236  {
237  // Header for csv (colums name)
238  std::string csv_header;
239  // Data point
240  std::string point_data;
241 
242  // Get csv columns
243  csv_header = get_csv_colums();
244 
245  // For each property in the vertex type produce a point data
246  point_data = get_csv_data(v,prp,offset);
247 
248  // write the file
249  std::ofstream ofs(file);
250 
251  // Check if the file is open
252  if (ofs.is_open() == false)
253  {std::cerr << "Error cannot create the CSV file: " + file;}
254 
255  ofs << csv_header << point_data;
256 
257  // Close the file
258 
259  ofs.close();
260 
261  // Completed succefully
262  return true;
263  }
264 };
265 
266 
267 #endif /* CSVWRITER_HPP_ */
268 
CSV Writer.
Definition: CSVWriter.hpp:149
void operator()(T &t)
It call the functor for each member.
Definition: CSVWriter.hpp:56
bool write(std::string file, v_pos &v, v_prp &prp, size_t offset=0)
It write a CSV file.
Definition: CSVWriter.hpp:235
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:89
This class is an helper to produce csv headers from multi-array.
csv_prp(std::stringstream &str, Tobj &obj)
Constructor.
Definition: CSVWriter.hpp:49
void operator()(T &t)
It call the functor for each member.
Definition: CSVWriter.hpp:123
this class is a functor for "for_each" algorithm
Definition: CSVWriter.hpp:33
std::string get_csv_colums()
Get the colums name (also the positional name)
Definition: CSVWriter.hpp:154
this class is a functor for "for_each" algorithm
Definition: CSVWriter.hpp:78
std::string get_csv_data(v_pos &vp, v_prp &vpr, size_t offset)
Get the csv data section.
Definition: CSVWriter.hpp:186