Output.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @file common/observer/Output.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2013-2022 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef ARTIS_COMMON_OBSERVER_OUTPUT_HPP
  22. #define ARTIS_COMMON_OBSERVER_OUTPUT_HPP
  23. #include <artis-star/common/observer/Observer.hpp>
  24. #include <artis-star/common/observer/View.hpp>
  25. #include <boost/format.hpp>
  26. #include <fstream>
  27. namespace artis::common::observer {
  28. template<typename Time, typename Iterator>
  29. class Output {
  30. public:
  31. Output(const Observer <Time> &observer)
  32. : _observer(observer) {}
  33. virtual ~Output() {}
  34. void operator()(double /* begin */, double end, const Iterator &iterator) const {
  35. const typename Observer<Time>::Views &views = _observer.views();
  36. for (typename Observer<Time>::Views::const_iterator it =
  37. views.begin(); it != views.end(); ++it) {
  38. std::ofstream o((boost::format("%1%.csv") % it->first).str());
  39. const typename View<Time>::SelectorValues &values = it->second->values();
  40. std::vector<typename Iterator::iterator_type> its;
  41. o.precision(10);
  42. // write header
  43. o << "time";
  44. for (typename View<Time>::SelectorValues::const_iterator itv = values.begin();
  45. itv != values.end(); ++itv) {
  46. const typename View<Time>::VariableValues &vv = itv->second;
  47. for (typename View<Time>::VariableValues::const_iterator itvv = vv.begin();
  48. itvv != vv.end(); ++itvv) {
  49. o << ";" << itvv->first;
  50. its.push_back(iterator.build(itvv->second));
  51. }
  52. }
  53. o << std::endl;
  54. // write values
  55. bool more = true;
  56. double t = iterator.begin(its);
  57. while (more and t <= end) {
  58. more = false;
  59. o << t;
  60. for (auto &dtt: its) {
  61. if (dtt.has_next() and iterator.is_valid(t, dtt)) {
  62. o << ";" << (*dtt).second.to_string();
  63. ++dtt;
  64. more = true;
  65. } else {
  66. o << ";";
  67. }
  68. }
  69. o << std::endl;
  70. t = iterator.next_time(t, its);
  71. }
  72. }
  73. }
  74. private:
  75. const Observer <Time> &_observer;
  76. };
  77. }
  78. #endif