Output.hpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/Observer.hpp>
  24. #include <artis-star/common/observer/View.hpp>
  25. #include <artis-star/utils/DateTime.hpp>
  26. #include <boost/format.hpp>
  27. namespace artis {
  28. namespace observer {
  29. template<typename Time>
  30. class Output {
  31. public:
  32. Output(const Observer<Time>& observer)
  33. :_observer(observer) { }
  34. virtual ~Output() { }
  35. void operator()() const
  36. {
  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. typename View<Time>::Values values = it->second->values();
  42. double begin = it->second->begin();
  43. double end = it->second->end();
  44. o.precision(10);
  45. // write header
  46. o << "time";
  47. for (typename View<Time>::Values::const_iterator
  48. itv = values.begin(); itv != values.end(); ++itv) {
  49. o << ";" << itv->first;
  50. }
  51. o << std::endl;
  52. // write values
  53. for (double t = begin; t <= end; ++t) {
  54. o << utils::DateTime::toJulianDay(t);
  55. // o << t;
  56. for (typename View<Time>::Values::const_iterator itv =
  57. values.begin(); itv != values.end(); ++itv) {
  58. typename View<Time>::Value::const_iterator itp =
  59. itv->second.begin();
  60. while (itp != itv->second.end() and itp->first < t) {
  61. ++itp;
  62. }
  63. o << ";";
  64. if (itp != itv->second.end()) {
  65. o << itp->second;
  66. } else {
  67. o << "NA";
  68. }
  69. }
  70. o << std::endl;
  71. }
  72. }
  73. }
  74. private:
  75. const Observer<Time>& _observer;
  76. };
  77. }
  78. }
  79. #endif