VariableFormatterODIM.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef VARIABLE_FORMATTER_ODIM
32 #define VARIABLE_FORMATTER_ODIM
33 
34 #include <ostream>
35 #include <cmath>
36 #include <string>
37 #include <set>
38 #include <algorithm>
39 //
40 #include <drain/util/VariableFormatter.h> // for VariableHandler
41 #include <drain/util/Time.h>
42 #include "ODIM.h"
43 
44 //#include "EncodingODIM.h"
45 
46 namespace rack {
47 
49 
64 template <class T>
66 
67 public:
68 
69  typedef typename drain::VariableFormatter<T>::map_t map_t;
70 
71  /*
72  typedef std::set<std::string> nameSet;
73 
74  static
75  const nameSet timeKeys; // = {"time", "starttime", "endtime"};
76 
77  static
78  const nameSet dateKeys; // = {"date", "startdate", "enddate"};
79 
80  static
81  const nameSet locationKeys; // = {"site", "src", "lat", "lon", "PLC", "NOD", "WMO"};
82  */
83 
84  virtual inline
86 
88  static
89  bool formatDate(std::ostream & ostr, const std::string & key, const T & value, const std::string & format = "%Y/%m/%d"){
90 
91  /*
92  const size_t i = key.find(':');
93  if (i != std::string::npos){
94  return formatDate(ostr, key.substr(i+1), value, format);
95  }
96  */
97 
98  if (ODIM::dateKeys.count(key) > 0){
99  // if (drain::StringTools::endsWith(key, "date")){
100  // ostr << "DATE:" << key << "=" << value << " {" << format << "} -> ";
101  ostr << drain::Time(value, "%Y%m%d").str(format);
102  return true;
103  }
104  else {
105  return false;
106  }
107  }
108 
110  static
111  bool formatTime(std::ostream & ostr, const std::string & key, const T & value, const std::string & format = "%H:%M"){// add UTC
112 
113  /*
114  const size_t i = key.find(':');
115  if (i != std::string::npos){
116  return formatTime(ostr, key.substr(i+1), value, format);
117  }
118  */
119 
120  if (ODIM::timeKeys.count(key) > 0){
121  // if (drain::StringTools::endsWith(key, "time")){
122  // ostr << "TIME:" << key << "=" << value << " {" << format << "} -> ";
123  ostr << drain::Time(value, "%H%M%S").str(format);
124  return true;
125  }
126  else {
127  return false;
128  }
129  }
130 
132  static
133  bool formatPlace(std::ostream & ostr, const std::string & key, const T & value, const std::string & format = ""){
134 
135  if (ODIM::locationKeys.count(key) > 0){
136  // ostr << drain::Time(value, "%Y%m%d").str(format);
137  ostr << value;
138  return true;
139  }
140  else {
141  return false;
142  }
143  }
144 
146  virtual
147  bool formatVariable(const std::string & key, const T & value, const std::string & format, std::ostream & ostr) const override {
148 
149  drain::Logger mout(__FILE__, __FUNCTION__);
150  // mout.warn("trying time format: ", key, " + ", format);
151 
152  if (format.find('%') != std::string::npos){
153  // Time formatting (instead of C-stype printf formatting)td::ostream & ostr
154  if (formatDate(ostr, key, value, format)){
155  // mout.attention("formatting DATE");
156  return true;
157  }
158  else if (formatTime(ostr, key, value, format)){
159  // mout.attention("formatting TIME");
160  return true;
161  }
162  if (formatPlace(ostr, key, value, format)){
163  // mout.attention("formatting PLACE");
164  return true;
165  }
166  }
167 
168  // Else, use default formatting:
169  return drain::VariableFormatter<T>::formatVariable(key, value, format, ostr); // basic/trad.
170  }
171 
172  /*
173  static
174  bool formatValue(const T & value, const std::string & format, std::ostream & ostr) override {
175  d
176  rain::Logger mout(__FILE__, __FUNCTION__);
177  mout.warn("trying time format: ", key, " + ", format);
178 
179  if (format.find('%') != std::string::npos){
180  // Time formatting (instead of C-stype printf formatting)
181  if (drain::StringTools::endsWith(key, "date")){
182  std::string s;
183  drain::MapTools::get(variables, key, s);
184  // mout.warn("time format: ", key, " -> ", s, '+', format); // " -> ", t.str(), " => ", t.str(key));
185  ostr << drain::Time(s, "%Y%m%d").str(format);
186  return true;
187  }
188  else if (drain::StringTools::endsWith(key, "time")){
189  std::string s;
190  drain::MapTools::get(variables, key, s);
191  // mout.warn("time format: ", key, " -> ", s, '+', format); // , " -> ", t.str(), " => ", t.str(key));
192  ostr << drain::Time(s, "%H%M%S").str(format);
193  return true;
194  }
195  }
196 
197  return drain::VariableFormatter<T>::formatValue(value, format, ostr); // basic/trad.
198  //return false;
199  }
200  */
201 
202 };
203 
204 /*
205 template <class T>
206 const typename VariableFormatterODIM<T>::nameSet VariableFormatterODIM<T>::timeKeys = {"time", "starttime", "endtime"};
207 
208 template <class T>
209 const typename VariableFormatterODIM<T>::nameSet VariableFormatterODIM<T>::dateKeys = {"date", "startdate", "enddate"};
210 
211 template <class T>
212 const typename VariableFormatterODIM<T>::nameSet VariableFormatterODIM<T>::locationKeys = {"site", "src", "lat", "lon", "PLC", "NOD", "WMO"};
213 */
214 
215 } // namespace rack
216 
217 
218 #endif
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Utility for handling time. Internally, uses tm (C time structure).
Definition: Time.h:54
const std::string & str(const std::string &format="") const
Returns the std::string using formatting as defined by strftime()
Definition: Time.cpp:76
Formats variables to output stream.
Definition: VariableFormatter.h:71
virtual bool formatVariable(const std::string &key, const T &value, const std::string &format, std::ostream &ostr) const
Given a key, retrieve an associated value from the map and print the value formatted into a stream.
Definition: VariableFormatter.h:128
Formats variables to output stream.
Definition: VariableFormatterODIM.h:65
static bool formatDate(std::ostream &ostr, const std::string &key, const T &value, const std::string &format="%Y/%m/%d")
Recognizes and format a date. Assumes that the variable name (key ) ends with "date".
Definition: VariableFormatterODIM.h:89
virtual bool formatVariable(const std::string &key, const T &value, const std::string &format, std::ostream &ostr) const override
Checks if variables have ODIM names (keys), and apply special formatting (currently with time stamps)
Definition: VariableFormatterODIM.h:147
static bool formatPlace(std::ostream &ostr, const std::string &key, const T &value, const std::string &format="")
Currently, only recognizes a place, and writes it directly in stream.
Definition: VariableFormatterODIM.h:133
static bool formatTime(std::ostream &ostr, const std::string &key, const T &value, const std::string &format="%H:%M")
Reformat a time. Assumes that the variable name (key ) ends with "time".
Definition: VariableFormatterODIM.h:111
Definition: DataSelector.cpp:44