Output.hpp 3.5 KB

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