MDF Lib 2.2
Interface against MDF 3/4 files
Loading...
Searching...
No Matches
ichannelconversion.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
5
10#pragma once
11#include <optional>
12#include <sstream>
13#include <string>
14#include <vector>
15#include <variant>
16
17#include "mdf/iblock.h"
18#include "mdf/imetadata.h"
19#include "mdf/mdfhelper.h"
20
21namespace mdf {
22
29enum class ConversionType : uint8_t {
31 NoConversion = 0,
32
36 Linear = 1,
37
42 Rational = 2,
43 Algebraic = 3,
44
50
55 ValueToValue = 5,
56
63
70 ValueToText = 7,
71
79
84 TextToValue = 9,
85
92
96 BitfieldToText = 11,
97 // MDF 3 types
98 Polynomial = 30,
99 Exponential = 31,
100 Logarithmic = 32,
101 DateConversion = 33,
102 TimeConversion = 34
103};
104
109 double value = 0;
110 std::string text;
111};
112
117 double lower = 0;
118 double upper = 0;
119 uint32_t link_text = 0;
120 std::string text;
121};
122
126namespace CcFlag {
127constexpr uint16_t PrecisionValid = 0x0001;
128constexpr uint16_t RangeValid = 0x0002;
129constexpr uint16_t StatusString = 0x0004;
130} // namespace CcFlag
131
143
144 public:
145 ~IChannelConversion() override = default;
146
147 virtual void Name(const std::string& name);
148 [[nodiscard]] virtual std::string Name() const;
149
150 virtual void Description(const std::string& desc);
151 [[nodiscard]] virtual std::string Description() const;
152
153 virtual void Unit(const std::string& unit) = 0;
154 [[nodiscard]] virtual std::string Unit() const = 0;
155 [[nodiscard]] virtual bool IsUnitValid() const = 0;
156
157 virtual void Type(ConversionType type) = 0;
158 [[nodiscard]] virtual ConversionType Type() const = 0;
159
160 [[nodiscard]] virtual bool IsDecimalUsed()
161 const = 0;
162 virtual void Decimals(uint8_t decimals);
163 [[nodiscard]] virtual uint8_t Decimals() const = 0;
164
166 [[nodiscard]] virtual IChannelConversion* CreateInverse();
167
169 [[nodiscard]] virtual IChannelConversion* Inverse() const;
170
171 virtual void Range(double min, double max);
172 [[nodiscard]] virtual std::optional<std::pair<double, double>> Range()
173 const;
174
175 virtual void Flags(uint16_t flags);
176 [[nodiscard]] virtual uint16_t Flags() const;
177
179 [[nodiscard]] virtual IMetaData* CreateMetaData();
180
182 [[nodiscard]] virtual IMetaData* MetaData() const;
183
185 virtual void Formula( const std::string& formula);
187 [[nodiscard]] virtual const std::string& Formula() const;
188
190 [[nodiscard]] uint16_t NofParameters() const;
191
197 void Parameter(uint16_t index, double parameter);
198
204 [[nodiscard]] double Parameter(uint16_t index) const;
205
211 [[nodiscard]] uint64_t ParameterUint(uint16_t index) const;
217 void ParameterUint(uint16_t index, uint64_t parameter);
218
220 [[nodiscard]] virtual uint16_t NofReferences() const;
221
227 virtual void Reference(uint16_t index, const std::string& text);
228
230 [[nodiscard]] virtual std::string Reference(uint16_t index) const;
231
237 void ChannelDataType(uint8_t channel_data_type);
238
248 template <typename T, typename V>
249 bool Convert(const T& channel_value, V& eng_value) const;
250
261 template <typename T, typename V = std::string>
262 bool Convert(const T& channel_value, std::string& eng_value) const;
263
265 template <typename T = std::string, typename V = double>
266 bool Convert(const std::string& channel_value, double& eng_value) const;
267
269 template <typename T = std::string, typename V = std::string>
270 bool Convert(const std::string& channel_value, std::string& eng_value) const {
272 ConvertTextToTranslation(channel_value, eng_value);
273 } else if (Type() == ConversionType::NoConversion) {
274 eng_value = channel_value;
275 } else {
276 return false;
277 }
278 return true;
279 }
280
281 protected:
282 uint16_t nof_values_ = 0;
283
285 using ParameterList = std::vector<std::variant<uint64_t, double>>;
286
289 0;
290
291 // The formula and conversion list is used in MDF 3 while MDF4
292 // uses the reference list.
293 mutable std::string formula_;
294 std::vector<TextConversion> text_conversion_list_;
295 std::vector<TextRangeConversion> text_range_conversion_list_;
296
297 [[nodiscard]] bool IsChannelInteger()
298 const;
299 [[nodiscard]] bool IsChannelFloat()
300 const;
301
302 virtual bool ConvertLinear(double channel_value, double& eng_value)
303 const;
304 virtual bool ConvertRational(double channel_value, double& eng_value)
305 const;
306 virtual bool ConvertAlgebraic(double channel_value, double& eng_value)
307 const;
308 virtual bool ConvertValueToValueInterpolate(double channel_value,
309 double& eng_value) const;
310 virtual bool ConvertValueToValue(double channel_value,
311 double& eng_value) const;
312 virtual bool ConvertValueRangeToValue(double channel_value,
313 double& eng_value) const;
314
315 virtual bool ConvertValueToText(double channel_value,
316 std::string& eng_value) const;
317 virtual bool ConvertValueRangeToText(double channel_value,
318 std::string& eng_value) const;
319 virtual bool ConvertTextToValue(const std::string& channel_value,
320 double& eng_value) const;
321 virtual bool ConvertTextToTranslation(const std::string& channel_value,
322 std::string& eng_value) const;
323 virtual bool ConvertPolynomial(double channel_value, double& eng_value)
324 const;
325 virtual bool ConvertLogarithmic(double channel_value,
326 double& eng_value) const;
327 virtual bool ConvertExponential(double channel_value,
328 double& eng_value) const;
329};
330
331template <typename T, typename V>
332inline bool IChannelConversion::Convert(const T& channel_value,
333 V& eng_value) const {
334 bool valid = false;
335 double value = 0.0;
336 switch (Type()) {
338 valid = ConvertLinear(static_cast<double>(channel_value), value);
339 eng_value = static_cast<V>(value);
340 break;
341 }
342
344 valid = ConvertRational(static_cast<double>(channel_value), value);
345 eng_value = static_cast<V>(value);
346 break;
347 }
348
350 valid = ConvertAlgebraic(static_cast<double>(channel_value), value);
351 eng_value = static_cast<V>(value);
352 break;
353 }
354
357 static_cast<double>(channel_value), value);
358 eng_value = static_cast<V>(value);
359 break;
360 }
361
363 valid = ConvertValueToValue(static_cast<double>(channel_value), value);
364 eng_value = static_cast<V>(value);
365 break;
366 }
367
369 valid =
370 ConvertValueRangeToValue(static_cast<double>(channel_value), value);
371 eng_value = static_cast<V>(value);
372 break;
373 }
374
376 std::string text;
377 valid = ConvertValueToText(static_cast<double>(channel_value), text);
378 std::istringstream s(text);
379 s >> eng_value;
380 break;
381 }
382
384 std::string text;
385 valid =
386 ConvertValueRangeToText(static_cast<double>(channel_value), text);
387 std::istringstream s(text);
388 s >> eng_value;
389 break;
390 }
391
393 valid = ConvertPolynomial(static_cast<double>(channel_value), value);
394 eng_value = static_cast<V>(value);
395 break;
396 }
397
399 valid = ConvertExponential(static_cast<double>(channel_value), value);
400 eng_value = static_cast<V>(value);
401 break;
402 }
403
405 valid = ConvertLogarithmic(static_cast<double>(channel_value), value);
406 eng_value = static_cast<V>(value);
407 break;
408 }
410 default: {
411 eng_value = static_cast<V>(channel_value);
412 valid = true;
413 break;
414 }
415 }
416 return valid;
417}
418
419template <typename T, typename V>
420inline bool IChannelConversion::Convert(const T& channel_value,
421 std::string& eng_value) const {
422 bool valid = false;
423 double value = 0.0;
424 switch (Type()) {
426 valid = ConvertLinear(static_cast<double>(channel_value), value);
427 eng_value =
429 break;
430 }
431
433 valid = ConvertRational(static_cast<double>(channel_value), value);
434 eng_value =
436 break;
437 }
438
440 valid = ConvertAlgebraic(static_cast<double>(channel_value), value);
441 eng_value =
443 break;
444 }
445
448 static_cast<double>(channel_value), value);
449 eng_value =
451 break;
452 }
453
455 valid = ConvertValueToValue(static_cast<double>(channel_value), value);
456 eng_value =
458 break;
459 }
460
462 valid =
463 ConvertValueRangeToValue(static_cast<double>(channel_value), value);
464 eng_value =
466 break;
467 }
468
470 valid =
471 ConvertValueToText(static_cast<double>(channel_value), eng_value);
472 break;
473 }
474
476 valid = ConvertValueRangeToText(static_cast<double>(channel_value),
477 eng_value);
478 break;
479 }
480
482 valid = ConvertPolynomial(static_cast<double>(channel_value), value);
483 eng_value =
485 break;
486 }
487
489 valid = ConvertExponential(static_cast<double>(channel_value), value);
490 eng_value =
492 break;
493 }
494
496 valid = ConvertLogarithmic(static_cast<double>(channel_value), value);
497 eng_value =
499 break;
500 }
501
503 default: {
504 eng_value = MdfHelper::FormatDouble(static_cast<double>(channel_value),
505 IsDecimalUsed() ? Decimals() : 6);
506 valid = true;
507 break;
508 }
509 }
510 return valid;
511}
512
513template <typename T, typename V>
514bool IChannelConversion::Convert(const std::string& channel_value,
515 double& eng_value) const {
517 ConvertTextToValue(channel_value, eng_value);
518 } else if (Type() == ConversionType::NoConversion) {
519 eng_value = std::stod(channel_value);
520 } else {
521 return false;
522 }
523 return true;
524}
525
526} // namespace mdf
Base class for all MDF blocks.
Definition iblock.h:19
Defines a channel conversion (CC) block.
Definition ichannelconversion.h:142
virtual uint8_t Decimals() const =0
Number of decimals.
virtual std::optional< std::pair< double, double > > Range() const
Returns the range if exist.
virtual bool ConvertValueToValueInterpolate(double channel_value, double &eng_value) const
Value to value interpolation conversion.
virtual std::string Name() const
Name.
bool IsChannelInteger() const
Returns true if channel is an integer.
virtual bool ConvertRational(double channel_value, double &eng_value) const
Rational conversion.
virtual void Range(double min, double max)
Sets the range.
virtual bool IsDecimalUsed() const =0
True if decimal is used.
virtual bool ConvertExponential(double channel_value, double &eng_value) const
Exponential conversion (MDF3).
virtual void Flags(uint16_t flags)
Sets the CcFlag.
virtual bool ConvertTextToValue(const std::string &channel_value, double &eng_value) const
Text to value conversion.
virtual bool ConvertValueToText(double channel_value, std::string &eng_value) const
Value to text conversion.
virtual bool ConvertLogarithmic(double channel_value, double &eng_value) const
Logarithmic conversion (MDF3).
uint16_t nof_values_
Number of parameter values (MDF3).
Definition ichannelconversion.h:282
virtual std::string Unit() const =0
Unit of measure.
virtual void Unit(const std::string &unit)=0
Sets the unit.
double Parameter(uint16_t index) const
Returns the parameter (double)
std::vector< TextConversion > text_conversion_list_
MDF3.
Definition ichannelconversion.h:294
virtual bool IsUnitValid() const =0
True if unit exist.
std::string formula_
Formula string (MDF3).
Definition ichannelconversion.h:293
virtual void Reference(uint16_t index, const std::string &text)
Sets text reference (TX) block.
virtual bool ConvertTextToTranslation(const std::string &channel_value, std::string &eng_value) const
Text to text conversion.
bool Convert(const T &channel_value, V &eng_value) const
Converts a channel value to an engineering (scaled) value.
Definition ichannelconversion.h:332
virtual ConversionType Type() const =0
Conversion type.
uint64_t ParameterUint(uint16_t index) const
Returns the parameter as a bit field (uint64_t)
virtual void Formula(const std::string &formula)
Sets the formula string.
virtual bool ConvertAlgebraic(double channel_value, double &eng_value) const
Algebraic conversion.
virtual bool ConvertValueRangeToText(double channel_value, std::string &eng_value) const
Value range to text value.
virtual bool ConvertValueToValue(double channel_value, double &eng_value) const
Value to value conversion.
virtual uint16_t NofReferences() const
Number of references in the block.
virtual void Decimals(uint8_t decimals)
Sets number of decimals.
ParameterList value_list_
List of parameters.
Definition ichannelconversion.h:287
virtual const std::string & Formula() const
Returns formula string.
std::vector< TextRangeConversion > text_range_conversion_list_
MDF3.
Definition ichannelconversion.h:295
virtual uint16_t Flags() const
Returns CcFlag.
std::vector< std::variant< uint64_t, double > > ParameterList
List of floating point constants.
Definition ichannelconversion.h:285
void Parameter(uint16_t index, double parameter)
Sets a floating point parameter value.
virtual std::string Reference(uint16_t index) const
Returns the reference string by its index.
uint8_t channel_data_type_
The channels data type. Needed by some conversions.
Definition ichannelconversion.h:288
virtual bool ConvertLinear(double channel_value, double &eng_value) const
Linear conversion.
bool IsChannelFloat() const
Returns true if the channel is a float.
virtual IMetaData * MetaData() const
Returns the meta-data block.
void ChannelDataType(uint8_t channel_data_type)
Sets the CN block data type.
uint16_t NofParameters() const
Returns number of parameters in the block.
bool Convert(const std::string &channel_value, std::string &eng_value) const
Converts from string to string.
Definition ichannelconversion.h:270
virtual void Description(const std::string &desc)
Sets the description.
void ParameterUint(uint16_t index, uint64_t parameter)
Sets an unsigned integer parameter value.
virtual IChannelConversion * Inverse() const
Returns the inverse conversion block. Seldom in use.
virtual bool ConvertValueRangeToValue(double channel_value, double &eng_value) const
Value range to value conversion.
virtual IChannelConversion * CreateInverse()
Creates an inverse conversion block.
virtual IMetaData * CreateMetaData()
Creates a meta-data (MD) block.
virtual void Type(ConversionType type)=0
Sets the conversion type.
virtual void Name(const std::string &name)
Sets the CC name.
virtual bool ConvertPolynomial(double channel_value, double &eng_value) const
Polynomial conversion (MDF3).
virtual std::string Description() const
Description.
Interface against an meta data block (MD) in a MDF4 file.
Definition imetadata.h:27
static std::string FormatDouble(double value, uint8_t decimals, bool fixed=false, const std::string &unit={})
Converts a float to a string.
All MDF blocks inherits from the IBlock class. The interface class is used internally in lists....
Support class for the MDF library.
constexpr uint16_t StatusString
Status string flag.
Definition ichannelconversion.h:129
constexpr uint16_t PrecisionValid
Precision is used.
Definition ichannelconversion.h:127
constexpr uint16_t RangeValid
Range is used.
Definition ichannelconversion.h:128
Main namespace for the MDF library.
Definition canmessage.h:17
ConversionType
Type of conversion formula.
Definition ichannelconversion.h:29
@ TextToTranslation
Text to text conversion. Defines a list of text string to text conversion. Ref(2*n) key to Ref(2*(n+1...
@ BitfieldToText
Bitfield to text conversion Currently unsupported conversion.
@ Rational
Rational function conversion. 6 parameters. Eng = (Par(0)*Ch^2 + Par(1)*Ch + Par(2)) / (Par(3)*Ch^2 +...
@ ValueToValue
Value to value conversion without interpolation. Defined by a list of Key value pairs....
@ Polynomial
MDF 3 polynomial conversion.
@ NoConversion
1:1 conversion. No parameters needed.
@ Logarithmic
MDF 3 logarithmic conversion.
@ Linear
Linear conversion. 2 parameters. Eng = Ch * Par(1) + Par(0).
@ ValueToText
Value to text conversion. Defined by a list of key values to text string conversions....
@ TextToValue
Text to value conversion. Defines a list of text string to value conversion. Ref(n) key to Par(n) val...
@ ValueToValueInterpolation
Value to value conversion with interpolation. Defined by a list of Key value pairs....
@ TimeConversion
MDF 3 Time conversion.
@ DateConversion
MDF 3 Date conversion.
@ Algebraic
Text formula.
@ Exponential
MDF 3 exponential conversion.
@ ValueRangeToText
Value range to text conversion. Defined by a list of key min/max values to text string conversions....
@ ValueRangeToValue
Value range to value conversion without interpolation. Defined by a list of Key min/max value triplet...
MDF 3 text conversion structure. Not used in MDF 4. Key to text conversion.
Definition ichannelconversion.h:108
std::string text
Text string.
Definition ichannelconversion.h:110
double value
Key.
Definition ichannelconversion.h:109
MDF 3 range conversion structure. Not used in MDF 4. Key min/max to text conversion.
Definition ichannelconversion.h:116
double upper
Key max value.
Definition ichannelconversion.h:118
uint32_t link_text
File position of the text value.
Definition ichannelconversion.h:119
std::string text
The text value.
Definition ichannelconversion.h:120
double lower
Key min value.
Definition ichannelconversion.h:117