Documentation for OpenFLUID 2.2.0
DistributionBindings.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file DistributionBindings.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  @author Armel THÖNI <armel.thoni@inrae.fr>
38  */
39 
40 
41 #ifndef __OPENFLUID_TOOLS_DISTRIBUTIONBINDINGS_HPP__
42 #define __OPENFLUID_TOOLS_DISTRIBUTIONBINDINGS_HPP__
43 
44 
46 #include <openfluid/dllexport.hpp>
51 #include <openfluid/config.hpp>
52 
53 
54 namespace openfluid { namespace tools {
55 
56 
57 inline std::vector<openfluid::tools::ClassIDVar>
58 stringArrayToClassIDVarList(const std::vector<std::string>& StringArray, bool RemoveFirst=false)
59 {
60  std::vector<openfluid::tools::ClassIDVar> CSVTriplets;
61  int Begin = RemoveFirst ? 1 : 0;
62  for (std::size_t i=Begin;i<StringArray.size();i++)
63  {
64  const std::string& Column = StringArray[i];
65  // parse and create CSVTriplet
66  std::size_t HashPosition = Column.find(openfluid::config::CHAR_SEPARATOR_UNIT);
67  std::size_t ColonPosition = Column.find(openfluid::config::CHAR_SEPARATOR_VAR);
68 
69  if(HashPosition == std::string::npos || ColonPosition == std::string::npos)
70  {
71  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION, "wrong format for multicolumn csv file");
72  }
73 
74  openfluid::tools::ClassIDVar CurrentCSVTriplet;
75 
76  CurrentCSVTriplet.UnitsClass = Column.substr(0,HashPosition);
77  CurrentCSVTriplet.UnitsIDsStr = Column.substr(HashPosition+1, ColonPosition-HashPosition-1);
78  CurrentCSVTriplet.VariableName = Column.substr(ColonPosition+1);
79 
80  CSVTriplets.push_back(CurrentCSVTriplet);
81  }
82  return CSVTriplets;
83 };
84 
85 
86 // =====================================================================
87 // =====================================================================
88 
89 
90 inline std::vector<openfluid::tools::ClassIDVar> stringSelectionToClassIDVarList(const std::string& SelectionStr,
91  bool RemoveFirst=false)
92 {
93  std::vector<std::string> Columns = openfluid::tools::split(SelectionStr, ";");
94  //TODO Extract and make split char consistent between ops
95 
96  return stringArrayToClassIDVarList(Columns, RemoveFirst);
97 };
98 
99 
100 template<typename DataType=double>
102 {
103  public:
104 
106 
108 
110 
111  ReaderNextValue(): Reader(nullptr), isAvailable(false)
112  { }
113 };
114 
115 
116 // =====================================================================
117 // =====================================================================
118 
119 
120 template<typename DataType=double>
122 {
123  public:
124 
125  typedef std::list<ReaderNextValue<DataType>> ReadersNextValues_t;
126 
127 
128  protected:
129 
131 
132 
133  public:
134 
136 
138  {
139 
140  // delete readers
141 
143  {
144  if (RNV.Reader)
145  {
146  delete RNV.Reader;
147  }
148  }
149  }
150 
152  {
154  {
155  bool DataFound = true;
157 
158  if ((RNV.isAvailable && RNV.NextValue.first < DT) ||
159  RNV.isAvailable == false)
160  {
161  RNV.isAvailable = false;
162 
163  while (DataFound && !RNV.isAvailable)
164  {
165  DataFound = RNV.Reader->getNextValue(CI);
166  if (DataFound && CI.first >= DT)
167  {
168  RNV.isAvailable = true;
169  RNV.NextValue = CI;
170  }
171  }
172  }
173  }
174  }
175 
177  {
178  openfluid::core::DateTime DTPlusOne(DT);
179  DTPlusOne.addSeconds(1);
180  advanceToTime(DTPlusOne);
181 
183 
184  bool AvailableFound = false;
185 
187  {
188 
189  if (!AvailableFound && RNV.isAvailable)
190  {
191  NDT = RNV.NextValue.first;
192  AvailableFound = true;
193  }
194  }
195 
196  if (!AvailableFound)
197  {
198  return false;
199  }
200 
202  {
203  if (RNV.isAvailable && RNV.NextValue.first < NDT)
204  {
205  NDT = RNV.NextValue.first;
206  }
207  }
208 
209  NextDT = NDT;
210 
211  return true;
212  }
213 
214 };
215 
216 
217 // =====================================================================
218 // =====================================================================
219 
220 
222 {
223  public:
224 
225  typedef std::map<openfluid::core::UnitID_t,ReaderNextValue<double>*> UnitIDReader_t;
226 
227 
228  protected:
229 
231 
232  public:
233 
235 
237  const openfluid::core::DateTime& DT,
239 
241 
242 };
243 
244 
245 // =====================================================================
246 // =====================================================================
247 
248 
250 {
251  public:
252 
253  typedef std::map<std::vector<std::string>, unsigned int> TripletLocation_t;
254 
255  private:
256 
257  TripletLocation_t m_ColBySelectionTriplets;
258 
259  inline static const std::string s_MissingValueString = "NA"; // TODO set as parameter transmitted by calling object
260 
261 
262  public:
263 
264  MulticolDistributionBindings(const std::string& DataFile,
265  const std::string& DateFormat = "%Y%m%dT%H%M%S",
266  const std::string& ColSeparators = ";");
267 
268 
269  /**
270  Apply to Value the wanted value from distribution table for given variable at given time
271  @return the status as int: 1 for success, 0 for expected NA, -1 for failed conversion from string to double
272  @throw openfluid::base::FrameworkException when wrong number of columns
273  */
274  int getValue(const openfluid::core::UnitsClass_t& UnitsClass,
275  const openfluid::core::UnitID_t& UnitID,
276  const openfluid::core::VariableName_t& VariableName,
277  const openfluid::core::DateTime& DT,
279 
281  {
282  return m_ColBySelectionTriplets;
283  }
284 };
285 
286 
287 } }
288 
289 
290 #endif /* __OPENFLUID_TOOLS_DISTRIBUTIONBINDINGS_HPP__ */
Definition: FrameworkException.hpp:51
Class for management of date and time information.
Definition: DateTime.hpp:88
void addSeconds(const RawTime_t &Seconds)
Definition: DoubleValue.hpp:81
Definition: DistributionBindings.hpp:222
UnitIDReader_t m_UnitIDReaders
Definition: DistributionBindings.hpp:230
bool getValue(const openfluid::core::UnitID_t &UnitID, const openfluid::core::DateTime &DT, openfluid::core::DoubleValue &Value)
std::map< openfluid::core::UnitID_t, ReaderNextValue< double > * > UnitIDReader_t
Definition: DistributionBindings.hpp:225
DistributionBindings(const DistributionTables &DistriTables)
Definition: DistributionTables.hpp:55
Definition: DistributionBindings.hpp:122
void advanceToTime(const openfluid::core::DateTime &DT)
Definition: DistributionBindings.hpp:151
ReadersNextValues_t m_ReadersNextValues
Definition: DistributionBindings.hpp:130
~GenericDistributionBindings()
Definition: DistributionBindings.hpp:137
bool advanceToNextTimeAfter(const openfluid::core::DateTime &DT, openfluid::core::DateTime &NextDT)
Definition: DistributionBindings.hpp:176
std::list< ReaderNextValue< DataType > > ReadersNextValues_t
Definition: DistributionBindings.hpp:125
Definition: DistributionBindings.hpp:250
MulticolDistributionBindings(const std::string &DataFile, const std::string &DateFormat="%Y%m%dT%H%M%S", const std::string &ColSeparators=";")
TripletLocation_t getColBySelectionTriplet()
Definition: DistributionBindings.hpp:280
int getValue(const openfluid::core::UnitsClass_t &UnitsClass, const openfluid::core::UnitID_t &UnitID, const openfluid::core::VariableName_t &VariableName, const openfluid::core::DateTime &DT, openfluid::core::DoubleValue &Value)
std::map< std::vector< std::string >, unsigned int > TripletLocation_t
Definition: DistributionBindings.hpp:253
Definition: ProgressiveChronFileReader.hpp:56
Definition: DistributionBindings.hpp:102
ChronItem_t< DataType > NextValue
Definition: DistributionBindings.hpp:107
bool isAvailable
Definition: DistributionBindings.hpp:109
ReaderNextValue()
Definition: DistributionBindings.hpp:111
ProgressiveChronFileReader< DataType > * Reader
Definition: DistributionBindings.hpp:105
#define OPENFLUID_API
Definition: dllexport.hpp:86
unsigned int UnitID_t
Definition: TypeDefs.hpp:70
std::string VariableName_t
Definition: TypeDefs.hpp:131
std::string UnitsClass_t
Definition: TypeDefs.hpp:98
std::vector< openfluid::tools::ClassIDVar > stringArrayToClassIDVarList(const std::vector< std::string > &StringArray, bool RemoveFirst=false)
Definition: DistributionBindings.hpp:58
std::vector< openfluid::tools::ClassIDVar > stringSelectionToClassIDVarList(const std::string &SelectionStr, bool RemoveFirst=false)
Definition: DistributionBindings.hpp:90
std::vector< std::string > OPENFLUID_API split(const std::string &Str, const char Sep, bool KeepEmpty=false)
std::pair< openfluid::core::DateTime, DataType > ChronItem_t
Definition: ChronologicalSerie.hpp:55
Definition: ApplicationException.hpp:47
Definition: VarHelpers.hpp:98
std::string UnitsIDsStr
Definition: VarHelpers.hpp:100
openfluid::core::VariableName_t VariableName
Definition: VarHelpers.hpp:60
openfluid::core::UnitsClass_t UnitsClass
Definition: VarHelpers.hpp:58