18#ifndef __SDDSPARSER_H__
19#define __SDDSPARSER_H__
30#include <unordered_map>
51 void setInput(
const std::string& input);
71 void getValue(
int t,
const std::string& column_name, T& nval) {
76 std::size_t row_idx = 0;
80 "requested column has no parsed rows");
82 if (t <= 0 ||
static_cast<std::size_t
>(t) > num_rows) {
83 row_idx = num_rows - 1;
85 row_idx =
static_cast<std::size_t
>(t) - 1;
101 template <
typename T>
104 const std::string& col_name,
108 double value_before_ref = 0;
109 double value_after_ref = 0;
116 std::size_t this_row = 0;
117 std::size_t num_rows = ref_values.size();
119 for (this_row = 0; this_row < num_rows; this_row++) {
120 value_after_ref = std::get<double>(ref_values[this_row]);
122 if (ref_val < value_after_ref) {
124 std::size_t prev_row = 0;
125 if (this_row > 0) prev_row = this_row - 1;
127 value_before = getVariantValue<T>(col_values[prev_row], dataType);
128 value_after = getVariantValue<T>(col_values[this_row], dataType);
130 value_before_ref = std::get<double>(ref_values[prev_row]);
131 value_after_ref = std::get<double>(ref_values[this_row]);
137 if (this_row == num_rows) {
139 "all values < specified reference value");
143 if (ref_val - value_before_ref < 1e-8) {
146 nval = value_before + (ref_val - value_before_ref)
147 * (value_after - value_before)
148 / (value_after_ref - value_before_ref);
151 if (!std::isfinite(nval)) {
153 "Interpolated value either NaN or Inf.");
166 template <
typename T>
178 template <
typename T>
184 nval = std::get<T>(value);
187 "unknown parameter name: '" + key +
"'!");
192 template <
typename T>
195 std::is_same_v<T, float> ||
196 std::is_same_v<T, double> ||
197 std::is_same_v<T, short> ||
198 std::is_same_v<T, long>,
199 "Unsupported type T in SDDSParser::getVariantValue"
205 return static_cast<T
>(std::get<float>(val));
208 return static_cast<T
>(std::get<double>(val));
211 return static_cast<T
>(std::get<short>(val));
214 return static_cast<T
>(std::get<long>(val));
218 "unsupported ast::dataType"
223 catch (
const std::bad_variant_access&) {
225 "variant and dataType mismatch"
std::variant< float, double, short, long, char, std::string > variant_t
std::vector< variant_t > columnData_t
static std::string normalizeKey(std::string_view value)
void getParameterValue(const std::string ¶meter_name, T &nval)
std::unordered_map< std::string, std::size_t > paramNameToID_m
mapping from parameter name to offset in params_m
std::string sddsFileName_m
int getColumnIndex(const std::string &col_name) const
const file & getData() const
void setInput(const std::string &input)
T getVariantValue(const ast::variant_t &val, ast::dataType type) const
Convert value from variant (only numeric types) to a value of type T.
void getInterpolatedValue(double spos, const std::string &col_name, T &nval)
const ast::columnData_t & getColumnData(const std::string &columnName) const
void getValue(int t, const std::string &column_name, T &nval)
void getInterpolatedValue(const std::string &ref_name, double ref_val, const std::string &col_name, T &nval)
ast::dataType getColumnType(const std::string &col_name) const
std::unordered_map< std::string, std::size_t > columnNameToID_m
mapping from column name to ID in columns_m
parameterList sddsParameters_m