Output.hpp 2.9 KB

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