Output.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. {
  33. public:
  34. Output(const Observer<Time> &observer)
  35. : _observer(observer)
  36. {}
  37. virtual ~Output()
  38. {}
  39. void operator()(double begin, double end, double step) const
  40. {
  41. const typename Observer<Time>::Views &views = _observer.views();
  42. for (typename Observer<Time>::Views::const_iterator it =
  43. views.begin(); it != views.end(); ++it) {
  44. std::ofstream o((boost::format("%1%.csv") % it->first).str());
  45. const typename View<Time>::SelectorValues &values = it->second->values();
  46. std::vector<artis::observer::DiscreteTimeIterator<artis::common::DoubleTime>> its;
  47. o.precision(10);
  48. // write header
  49. o << "time";
  50. for (typename View<Time>::SelectorValues::const_iterator itv = values.begin();
  51. itv != values.end(); ++itv) {
  52. const typename View<Time>::VariableValues &vv = itv->second;
  53. for (typename View<Time>::VariableValues::const_iterator itvv = vv.begin();
  54. itvv != vv.end(); ++itvv) {
  55. o << ";" << itvv->first;
  56. its.push_back(
  57. artis::observer::DiscreteTimeIterator<artis::common::DoubleTime>(
  58. itvv->second, begin, step));
  59. }
  60. }
  61. o << std::endl;
  62. // write values
  63. bool more = true;
  64. double t = begin;
  65. while (more and t <= end) {
  66. more = false;
  67. o << t;
  68. for (auto &dtt: its) {
  69. if (dtt.has_next()) {
  70. o << ";" << (*dtt).second.to_string();
  71. ++dtt;
  72. more = true;
  73. } else {
  74. o << ";";
  75. }
  76. }
  77. o << std::endl;
  78. t += step;
  79. }
  80. }
  81. }
  82. private:
  83. const Observer<Time> &_observer;
  84. };
  85. }
  86. }
  87. #endif