outputmodule.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file outputmodule.cpp
  3. /// \brief Implementation of OutputModule and its container class
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date: 2013-07-17 09:22:52 +0200 (Wed, 17 Jul 2013) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #include "config.h"
  10. #include "outputmodule.h"
  11. #include "parameters.h"
  12. #include "guess.h"
  13. namespace GuessOutput {
  14. OutputChannel* output_channel;
  15. ///////////////////////////////////////////////////////////////////////////////////////
  16. /// OutputModule
  17. ///
  18. void OutputModule::create_output_table(Table& table, const char* file, const ColumnDescriptors& columns) {
  19. table = output_channel->create_table(TableDescriptor(file, columns));
  20. }
  21. void OutputModule::close_output_table(Table& table) {
  22. output_channel->close_table(table);
  23. table = Table();
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////////////
  26. /// OutputModuleContainer
  27. ///
  28. OutputModuleContainer::OutputModuleContainer()
  29. : coordinates_precision(2) {
  30. declare_parameter("outputdirectory", &outputdirectory, 300, "Directory for the output files");
  31. declare_parameter("coordinates_precision", &coordinates_precision, 0, 10, "Digits after decimal point in coordinates in output");
  32. }
  33. OutputModuleContainer::~OutputModuleContainer() {
  34. for (size_t i = 0; i < modules.size(); ++i) {
  35. delete modules[i];
  36. }
  37. delete output_channel;
  38. }
  39. void OutputModuleContainer::add(OutputModule* output_module) {
  40. modules.push_back(output_module);
  41. }
  42. void OutputModuleContainer::init() {
  43. // We MUST have an output directory
  44. if (outputdirectory=="") {
  45. fail("No output directory given in the .ins file!");
  46. }
  47. // Create the output channel
  48. #ifdef COMPRESS_OUTPUT
  49. output_channel = new GZFileOutputChannel(outputdirectory.c_str(),
  50. coordinates_precision);
  51. #else
  52. output_channel = new FileOutputChannel(outputdirectory.c_str(),
  53. coordinates_precision);
  54. #endif
  55. for (size_t i = 0; i < modules.size(); ++i) {
  56. modules[i]->init();
  57. }
  58. }
  59. void OutputModuleContainer::outannual(Gridcell& gridcell) {
  60. for (size_t i = 0; i < modules.size(); ++i) {
  61. modules[i]->outannual(gridcell);
  62. }
  63. }
  64. void OutputModuleContainer::outdaily(Gridcell& gridcell) {
  65. for (size_t i = 0; i < modules.size(); ++i) {
  66. modules[i]->outdaily(gridcell);
  67. }
  68. }
  69. ///////////////////////////////////////////////////////////////////////////////////////
  70. /// OutputModuleRegistry
  71. ///
  72. OutputModuleRegistry& OutputModuleRegistry::get_instance() {
  73. static OutputModuleRegistry instance;
  74. return instance;
  75. }
  76. void OutputModuleRegistry::register_output_module(const char* name,
  77. OutputModuleCreator omc) {
  78. modules.insert(make_pair(std::string(name), omc));
  79. }
  80. void OutputModuleRegistry::create_all_modules(OutputModuleContainer& container) const {
  81. for (std::map<std::string, OutputModuleCreator>::const_iterator itr = modules.begin();
  82. itr != modules.end(); ++itr) {
  83. container.add((itr->second)());
  84. }
  85. }
  86. } // namespace