Output.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @file artis/observer/Output.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-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_OBSERVER_OUTPUT_HPP
  22. #define ARTIS_OBSERVER_OUTPUT_HPP
  23. #include <artis/observer/Observer.hpp>
  24. #include <artis/observer/View.hpp>
  25. #include <artis/utils/DateTime.hpp>
  26. #include <boost/format.hpp>
  27. namespace artis {
  28. namespace observer {
  29. template<typename U, typename V>
  30. class Output {
  31. public:
  32. Output(const Observer<U, V>& observer)
  33. :_observer(observer) { }
  34. virtual ~Output() { }
  35. void operator()(const std::string& path = "") const
  36. {
  37. const typename Observer<U, V>::Views& views = _observer.views();
  38. for (typename Observer<U, V>::Views::const_iterator it =
  39. views.begin(); it != views.end(); ++it) {
  40. std::ofstream o((boost::format("%1%/%2%.csv") % path %
  41. it->first).str());
  42. typename View<U, V>::Values values = it->second->values();
  43. double begin = it->second->begin();
  44. double end = it->second->end();
  45. o.precision(10);
  46. // write header
  47. o << "time";
  48. for (typename View<U, V>::Values::const_iterator
  49. itv = values.begin(); itv != values.end(); ++itv) {
  50. o << ";" << itv->first;
  51. }
  52. o << std::endl;
  53. // write values
  54. for (double t = begin; t <= end; ++t) {
  55. o << utils::DateTime::toJulianDay(t);
  56. // o << t;
  57. for (typename View<U, V>::Values::const_iterator itv =
  58. values.begin(); itv != values.end(); ++itv) {
  59. typename View<U, V>::Value::const_iterator itp =
  60. itv->second.begin();
  61. while (itp != itv->second.end() and itp->first < t) {
  62. ++itp;
  63. }
  64. o << ";";
  65. if (itp != itv->second.end()) {
  66. o << itp->second;
  67. } else {
  68. o << "NA";
  69. }
  70. }
  71. o << std::endl;
  72. }
  73. }
  74. }
  75. private:
  76. const Observer<U, V>& _observer;
  77. };
  78. }
  79. }
  80. #endif