ParametersFileStream.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*!
  2. * \file ParametersFileStream.h
  3. * \brief Flux texte sur le fichier de paramètres du programme et du bassin versant.
  4. * \author The VLE Development Team
  5. * See the AUTHORS or Authors.txt file
  6. * \version 2.0
  7. * \date 12 juin 2013
  8. */
  9. /*
  10. * Copyright (C) 2012-2013 ULCO http://www.univ-littoral.fr
  11. * Copyright (C) 2012-2013 INRA http://www.inra.fr
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #ifndef PARAMETERSFILESTREAM_H_
  27. #define PARAMETERSFILESTREAM_H_
  28. #include "FileStream.h"
  29. #include "Parameters.h"
  30. #include <boost/lexical_cast.hpp>
  31. #include <boost/shared_ptr.hpp>
  32. /*! \class ParametersFileStream
  33. *
  34. * \brief gère un flux texte en lecture et écriture sur le fichier de paramètres.
  35. */
  36. class ParametersFileStream : public FileStream<corsenContainers::CType, Parameters>
  37. {
  38. public:
  39. std::string _modeFilePath;
  40. std::string _modeFileFormat;
  41. ParametersFileStream(std::string & parametersFilePath, std::string & modeFilePath) : FileStream<corsenContainers::CType, Parameters>(parametersFilePath)
  42. {
  43. _modeFilePath = modeFilePath;
  44. _modeFileFormat = findFormat(modeFilePath);
  45. }
  46. virtual ~ParametersFileStream(){}
  47. returnType * read() const throw (FileNotFound, InvalidFileFormat)
  48. {
  49. std::ifstream parFile(_filePath.c_str(),std::ios::in);
  50. std::ifstream modeFile(_modeFilePath.c_str(),std::ios::in);
  51. Parameters * p = NULL;
  52. if(not parFile)
  53. throw FileNotFound("Exception opening file : "+ _filePath);
  54. if(not modeFile)
  55. throw FileNotFound("Exception opening file : "+ _modeFilePath);
  56. if(_format.compare(".txt") == 0)
  57. p = readStandardFormat(parFile);
  58. else if(_format.compare(".def") == 0)
  59. p = readDefaultValues(parFile);
  60. else
  61. throw InvalidFileFormat("Exception Unknown file format "+_format+"\n");
  62. if(_modeFileFormat.compare("") == 0)
  63. readStandardModeFormat(modeFile, p);
  64. else
  65. {
  66. if(p != NULL)
  67. delete p;
  68. throw InvalidFileFormat("Exception Unknown file format "+_modeFileFormat+"\n");
  69. }
  70. return p;
  71. }
  72. void write(const returnType & object, const std::string & /* extension */) const throw (FileNotFound, InvalidFileFormat)
  73. {
  74. std::ofstream parFile(_filePath.c_str(), std::ios::trunc) ;
  75. if(not parFile)
  76. throw FileNotFound("Exception opening file: "+ _filePath);
  77. if(_format.compare(".txt") == 0)
  78. return writeStandardFormat(parFile, object);
  79. throw InvalidFileFormat("Exception Unknown file format "+_filePath+"\n");
  80. }
  81. private :
  82. void readStandardModeFormat(std::ifstream & file, Parameters * p) const throw (FileNotFound, boost::bad_lexical_cast)
  83. {
  84. using boost::lexical_cast;
  85. std::string line;
  86. std::vector<std::string> parameters;
  87. std::getline(file, line);
  88. boost::split(parameters,line,boost::is_any_of(" "));
  89. p->_meshMode = lexical_cast<bool>(parameters.at(0));
  90. p->_gap = lexical_cast<int>(parameters.at(1));
  91. file.close();
  92. }
  93. returnType * readDefaultValues(std::ifstream & file) const throw (ReadError)
  94. {
  95. Parameters * param = new Parameters();
  96. file.read(reinterpret_cast<char *>(&(param->_riverLevel)), sizeof(int));
  97. file.read(reinterpret_cast<char *>(&(param->_displaySea)), sizeof(int));
  98. if(file.bad() or not file.eof())
  99. {
  100. file.close();
  101. throw ReadError("Exception reading file "+ _filePath);
  102. }
  103. #ifdef DEBUG
  104. std::cout<<"Seuil de rivière: "<<param->_riverLevel<<" ha"<<std::endl;
  105. std::cout<<"Afficher la mer: "<<param->_displaySea<<std::endl
  106. #endif
  107. file.close();
  108. return param;
  109. }
  110. returnType * readStandardFormat(std::ifstream & file) const
  111. {
  112. using boost::lexical_cast;
  113. using boost::bad_lexical_cast ;
  114. Parameters * param = new Parameters();
  115. std::string line;
  116. std::vector<std::string> parameters;
  117. std::getline(file,line);
  118. boost::split(parameters, line, boost::is_any_of(" "));
  119. try
  120. {
  121. param->_nodesPerLine = lexical_cast<int>(parameters.at(0));
  122. param->_nodesPerColumn = lexical_cast <int>(parameters.at(1));
  123. if(param->_meshMode)
  124. {
  125. param->_linesNumber = param->_nodesPerLine;
  126. param->_columnsNumber = param->_nodesPerColumn;
  127. }
  128. else
  129. {
  130. param->_linesNumber = param->_nodesPerLine - 1 ;
  131. param->_columnsNumber = param->_nodesPerColumn - 1 ;
  132. }
  133. #ifdef DEBUG
  134. std::cout<<"Matrice Altitude : "<<param->_linesNumber<<" lignes et "<<param->_columnsNumber<<" colonnes \n"<<"mnt "<<param->_nodesPerLine<<" X "<<param->_nodesPerColumn<<std::endl;
  135. #endif
  136. }
  137. catch (const bad_lexical_cast & e)
  138. {
  139. std::cerr<<e.what();
  140. }
  141. if(parameters.size() >= 3)
  142. {
  143. param->_horizontalMeshStep = lexical_cast<double>(parameters.at(2));
  144. if (parameters.size() >=4 )
  145. {
  146. param->_verticalMeshStep = lexical_cast<double>(parameters.at(3));
  147. param->_lambertCadre = false ;
  148. if (parameters.size() == 12)
  149. {
  150. param->_lambertCadre = true ;
  151. param->lx0 = lexical_cast<double>(parameters.at(4));
  152. param->ly0 = lexical_cast<double>(parameters.at(5));
  153. param->lx1 = lexical_cast<double>(parameters.at(6));
  154. param->ly1 = lexical_cast<double>(parameters.at(7));
  155. param->lx2 = lexical_cast<double>(parameters.at(8));
  156. param->ly2 = lexical_cast<double>(parameters.at(9));
  157. param->lx3 = lexical_cast<double>(parameters.at(10));
  158. param->ly3 = lexical_cast<double>(parameters.at(11));
  159. }
  160. }
  161. else
  162. param->_verticalMeshStep = param->_horizontalMeshStep;
  163. param->_meshSize = param->_horizontalMeshStep ;
  164. }
  165. else
  166. {
  167. param->_horizontalMeshStep = 50. ;
  168. param->_verticalMeshStep = 50.;
  169. param->_meshSize = 50.;
  170. }
  171. param->_meshArea = param->_horizontalMeshStep * param->_verticalMeshStep ;
  172. param->_colorColumnsNumber = (( param->_columnsNumber / 32 ) + 1 ) * 32 ;
  173. param->_charColumnsNumber = (int) ceil ( (double) param->_columnsNumber / 8. ) ;
  174. #ifdef DEBUG
  175. std::cout<<"Pas de la maille en x : "<<param->_horizontalMeshStep<<" mètres \n" ;
  176. std::cout<<"Pas de la maille en y : "<<param->_verticalMeshStep<<" mètres \n\n";
  177. std::cout<<param->_colorColumnsNumber<<" colonnes couleur\n\n";
  178. std::cout<<param->_charColumnsNumber<<" colonnes caractère (codage binaire)\n\n";
  179. #endif
  180. if(not param->_lambertCadre)
  181. {
  182. param->lx0 = 0. ;
  183. param->ly0 = 0. ;
  184. param->lx1 = 0. ;
  185. param->ly1 = param->_verticalMeshStep * param->_nodesPerColumn / 1000. ;
  186. param->lx2 = param->_verticalMeshStep * param->_nodesPerLine / 1000. ;
  187. param->ly2 = 0. ;
  188. param->lx3 = param->_verticalMeshStep * param->_nodesPerLine / 1000. ;
  189. param->ly3 = param->_verticalMeshStep * param->_nodesPerColumn / 1000. ;
  190. param->_lambertCadre = true ;
  191. }
  192. file.close();
  193. return param;
  194. }
  195. void writeStandardFormat(std::ofstream & file, const returnType & object) const throw (FileNotFound)
  196. {
  197. std::ofstream modeFile(_modeFilePath, std::ios::trunc);
  198. if(not modeFile)
  199. throw FileNotFound("Exception opening file: "+ _modeFilePath);
  200. modeFile<<object._meshMode<<" "<<object._gap;;
  201. modeFile.close();
  202. file<<object._linesNumber<<" ";
  203. file<<object._columnsNumber<<" ";
  204. file<<object._horizontalMeshStep<<" ";
  205. file<<object._verticalMeshStep<<" ";
  206. file<<object.lx0<<" ";
  207. file<<object.ly0<<" ";
  208. file<<object.lx1<<" ";
  209. file<<object.ly1<<" ";
  210. file<<object.lx2<<" ";
  211. file<<object._charColumnsNumber<<" ";
  212. file<<object.lx3<<" ";
  213. file<<object.ly3<<"\n";
  214. file.close();
  215. }
  216. };
  217. #endif /* PARAMETERSFILESTREAM_H_ */