OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
SDDSParser.h
Go to the documentation of this file.
1//
2// Class SDDSParser
3// This class writes column entries of SDDS files.
4//
5// Copyright (c) 2015, Christof Metzger-Kraus, Helmholtz-Zentrum Berlin
6// All rights reserved
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
18#ifndef __SDDSPARSER_H__
19#define __SDDSPARSER_H__
20
31
33
34#include <cmath>
35#include <fstream>
36#include <iostream>
37#include <map>
38#include <string>
39
40namespace SDDS {
41
42 class SDDSParser {
43 private:
44 std::string readFile();
45 static void fixCaseSensitivity(std::string &for_string);
46 static std::string fixCaseSensitivity(const std::string &for_string) {
47 std::string retval(for_string);
48 fixCaseSensitivity(retval);
49 return retval;
50 }
51 std::string sddsFileName_m;
52
54 std::map<std::string, int> paramNameToID_m;
56 std::map<std::string, int> columnNameToID_m;
57
59
60 public:
61 SDDSParser();
62 SDDSParser(const std::string &input);
63 void setInput(const std::string &input);
64 file run();
65
66 file getData();
67 ast::columnData_t getColumnData(const std::string &columnName);
68
69 ast::datatype getColumnType(const std::string &col_name) {
70 int index = getColumnIndex(col_name);
71 return *sddsData_m.sddsColumns_m[index].type_m;
72 }
73
82 template <typename T>
83 void getValue(int t, std::string column_name, T& nval) {
84
85 fixCaseSensitivity(column_name);
86
87 int col_idx = getColumnIndex(column_name);
88
89 // round timestep to last if not in range
90 size_t row_idx = 0;
91 size_t num_rows = sddsData_m.sddsColumns_m[col_idx].values_m.size();
92 if(t <= 0 || static_cast<size_t>(t) > num_rows)
93 row_idx = num_rows - 1;
94 else
95 row_idx = static_cast<size_t>(t) - 1;
96
97 ast::variant_t val = sddsData_m.sddsColumns_m[col_idx].values_m[row_idx];
98 nval = getBoostVariantValue<T>(val, (int)getColumnType(column_name));
99 }
100
101
111 template <typename T>
112 void getInterpolatedValue(std::string ref_name, double ref_val,
113 std::string col_name, T& nval) {
114 T value_before = 0;
115 T value_after = 0;
116 double value_before_ref = 0;
117 double value_after_ref = 0;
118
119
120 size_t col_idx_ref = getColumnIndex(ref_name);
121 ast::columnData_t &ref_values = sddsData_m.sddsColumns_m[col_idx_ref].values_m;
122 int index = getColumnIndex(col_name);
123 ast::columnData_t &col_values = sddsData_m.sddsColumns_m[index].values_m;
124
125 size_t this_row = 0;
126 size_t num_rows = ref_values.size();
127 int datatype = (int)getColumnType(col_name);
128 for(this_row = 0; this_row < num_rows; this_row++) {
129 value_after_ref = boost::get<double>(ref_values[this_row]);
130
131 if(ref_val < value_after_ref) {
132
133 size_t prev_row = 0;
134 if(this_row > 0) prev_row = this_row - 1;
135
136 value_before = getBoostVariantValue<T>(col_values[prev_row], datatype);
137 value_after = getBoostVariantValue<T>(col_values[this_row], datatype);
138
139 value_before_ref = boost::get<double>(ref_values[prev_row]);
140 value_after_ref = boost::get<double>(ref_values[this_row]);
141
142 break;
143 }
144 }
145
146 if(this_row == num_rows)
147 throw SDDSParserException("SDDSParser::getInterpolatedValue",
148 "all values < specified reference value");
149
150 // simple linear interpolation
151 if(ref_val - value_before_ref < 1e-8)
152 nval = value_before;
153 else
154 nval = value_before + (ref_val - value_before_ref)
155 * (value_after - value_before)
156 / (value_after_ref - value_before_ref);
157
158 if (!std::isfinite(nval))
159 throw SDDSParserException("SDDSParser::getInterpolatedValue",
160 "Interpolated value either NaN or Inf.");
161 }
162
171 template <typename T>
172 void getInterpolatedValue(double spos, std::string col_name, T& nval) {
173 getInterpolatedValue("s", spos, col_name, nval);
174 }
175
183 template <typename T>
184 void getParameterValue(std::string parameter_name, T& nval) {
185 fixCaseSensitivity(parameter_name);
186
187 if (paramNameToID_m.count(parameter_name) > 0) {
188 size_t id = paramNameToID_m[parameter_name];
189 auto value = sddsData_m.sddsParameters_m[id].value_m;
190 nval = boost::get<T>(value);
191 } else {
192 throw SDDSParserException("SDDSParser::getParameterValue",
193 "unknown parameter name: '" + parameter_name + "'!");
194 }
195 }
196
198 // use integer instead of ast::datatype enum since otherwise boost has ambigious overloads
199 // as tested on 8-1-2019, boost 1.68, gcc 7.3
200 template <typename T>
201 T getBoostVariantValue(const ast::variant_t& val, int datatype) const {
202 T value;
203 try {
204 switch (datatype) {
205 case ast::FLOAT:
206 value = boost::get<float>(val);
207 break;
208 case ast::DOUBLE:
209 value = boost::get<double>(val);
210 break;
211 case ast::SHORT:
212 value = boost::get<short>(val);
213 break;
214 case ast::LONG:
215 value = boost::get<long>(val);
216 break;
217 default:
218 throw SDDSParserException("SDDSParser::getBoostVariantValue",
219 "can't convert value to type T");
220 }
221 }
222 catch (...) {
223 throw SDDSParserException("SDDSParser::getBoostVariantValue",
224 "can't convert value");
225 }
226 return value;
227 }
228
229 private:
230
231 int getColumnIndex(std::string col_name) const;
232 };
233
234 inline
236 return sddsData_m;
237 }
238}
239
240#endif
std::vector< variant_t > columnData_t
Definition ast.hpp:50
@ DOUBLE
Definition ast.hpp:29
@ LONG
Definition ast.hpp:31
@ FLOAT
Definition ast.hpp:28
@ SHORT
Definition ast.hpp:30
boost::variant< float, double, short, long, char, std::string > variant_t
Definition ast.hpp:48
std::map< std::string, int > columnNameToID_m
mapping from column name to ID in columns_m
Definition SDDSParser.h:56
void getParameterValue(std::string parameter_name, T &nval)
Definition SDDSParser.h:184
std::string readFile()
ast::columnData_t getColumnData(const std::string &columnName)
ast::datatype getColumnType(const std::string &col_name)
Definition SDDSParser.h:69
std::string sddsFileName_m
Definition SDDSParser.h:51
T getBoostVariantValue(const ast::variant_t &val, int datatype) const
Convert value from boost variant (only numeric types) to a value of type T.
Definition SDDSParser.h:201
void getInterpolatedValue(std::string ref_name, double ref_val, std::string col_name, T &nval)
Definition SDDSParser.h:112
static void fixCaseSensitivity(std::string &for_string)
void setInput(const std::string &input)
void getValue(int t, std::string column_name, T &nval)
Definition SDDSParser.h:83
static std::string fixCaseSensitivity(const std::string &for_string)
Definition SDDSParser.h:46
void getInterpolatedValue(double spos, std::string col_name, T &nval)
Definition SDDSParser.h:172
SDDS::file sddsData_m
Definition SDDSParser.h:58
int getColumnIndex(std::string col_name) const
std::map< std::string, int > paramNameToID_m
mapping from parameter name to offset in params_m
Definition SDDSParser.h:54
columnList sddsColumns_m
Definition file.hpp:48
parameterList sddsParameters_m
Definition file.hpp:47