Output.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @file observer/Output.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2013-2019 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/Iterator.hpp>
  24. #include <artis-star/common/observer/Observer.hpp>
  25. #include <artis-star/common/observer/View.hpp>
  26. #include <boost/format.hpp>
  27. #include <fstream>
  28. namespace artis {
  29. namespace observer {
  30. template<typename Time>
  31. class Output {
  32. public:
  33. Output(const Observer<Time> &observer)
  34. : _observer(observer) {}
  35. virtual ~Output() {}
  36. void operator()(double begin, double end, double step) const {
  37. const typename Observer<Time>::Views &views = _observer.views();
  38. for (typename Observer<Time>::Views::const_iterator it =
  39. views.begin(); it != views.end(); ++it) {
  40. std::ofstream o((boost::format("%1%.csv") % it->first).str());
  41. const typename View<Time>::SelectorValues &values = it->second->values();
  42. std::vector<artis::observer::DiscreteTimeIterator<artis::common::DoubleTime>> its;
  43. o.precision(10);
  44. // write header
  45. o << "time";
  46. for (typename View<Time>::SelectorValues::const_iterator itv = values.begin();
  47. itv != values.end(); ++itv) {
  48. const typename View<Time>::VariableValues &vv = itv->second;
  49. for (typename View<Time>::VariableValues::const_iterator itvv = vv.begin();
  50. itvv != vv.end(); ++itvv) {
  51. o << ";" << itvv->first;
  52. its.push_back(
  53. artis::observer::DiscreteTimeIterator<artis::common::DoubleTime>(
  54. itvv->second, begin, step));
  55. }
  56. }
  57. o << std::endl;
  58. // write values
  59. bool more = true;
  60. double t = begin;
  61. while (more and t <= end) {
  62. more = false;
  63. o << t;
  64. for (auto &dtt: its) {
  65. if (dtt.has_next()) {
  66. o << ";" << (*dtt).second.to_string();
  67. ++dtt;
  68. more = true;
  69. } else {
  70. o << ";";
  71. }
  72. }
  73. o << std::endl;
  74. t += step;
  75. }
  76. }
  77. }
  78. private:
  79. const Observer<Time> &_observer;
  80. };
  81. }
  82. }
  83. #endif