Line data Source code
1 : /* Copyright (C) 2025 Robert Schiwon
2 : *
3 : * This file is part of libjdx.
4 : *
5 : * libjdx is free software: you can redistribute it and/or modify it under the
6 : * terms of the GNU Lesser General Public License as published by the Free
7 : * Software Foundation, either version 3 of the License, or (at your option) any
8 : * later version.
9 : *
10 : * libjdx is distributed in the hope that it will be useful, but WITHOUT ANY
11 : * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 : * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with libjdx. If not, see <https://www.gnu.org/licenses/>.
17 : */
18 : #include "jdx/RaData.hpp"
19 : #include "jdx/ParseException.hpp"
20 : #include "jdx/RaParameters.hpp"
21 : #include "util/DataParser.hpp"
22 : #include "util/LdrUtils.hpp"
23 :
24 : #include <limits>
25 :
26 18 : libjdx::jdx::RaData::RaData(const std::string& label,
27 : const std::string& variableList, const std::vector<StringLdr>& ldrs,
28 18 : io::TextReader& reader, std::optional<std::string>& nextLine)
29 20 : : Data2D(label, variableList, reader)
30 : {
31 18 : validateInput(label, variableList, s_raDataLabel,
32 54 : std::vector<std::string>{s_raDataVariableList});
33 18 : m_parameters = parseParameters(ldrs);
34 17 : util::skipToNextLdr(reader, nextLine, true);
35 17 : }
36 :
37 3 : const libjdx::jdx::RaParameters& libjdx::jdx::RaData::getParameters() const
38 : {
39 3 : return m_parameters;
40 : }
41 :
42 5 : std::vector<std::pair<double, double>> libjdx::jdx::RaData::getData()
43 : {
44 : return Data2D::parseXppYYData(getLabel(), getReader(), m_parameters.firstR,
45 5 : m_parameters.lastR, m_parameters.rFactor, m_parameters.nPoints);
46 : }
47 :
48 18 : libjdx::jdx::RaParameters libjdx::jdx::RaData::parseParameters(
49 : const std::vector<StringLdr>& ldrs)
50 : {
51 : // required
52 : // string
53 54 : auto rUnits = util::parseLdrValue<std::string>(ldrs, "RUNITS");
54 54 : auto aUnits = util::parseLdrValue<std::string>(ldrs, "AUNITS");
55 : // double
56 18 : auto firstR = util::parseLdrValue<double>(ldrs, "FIRSTR");
57 18 : auto lastR = util::parseLdrValue<double>(ldrs, "LASTR");
58 18 : auto rFactor = util::parseLdrValue<double>(ldrs, "RFACTOR");
59 18 : auto aFactor = util::parseLdrValue<double>(ldrs, "AFACTOR");
60 : // uint64_t
61 18 : auto nPoints = util::parseLdrValue<uint64_t>(ldrs, "NPOINTS");
62 : // optional
63 : // double
64 18 : auto firstA = util::parseLdrValue<double>(ldrs, "FIRSTA");
65 : auto maxA = util::parseLdrValue<double>(
66 18 : ldrs, "MAXA"); // required, according to standard
67 : auto minA = util::parseLdrValue<double>(
68 18 : ldrs, "MINA"); // required, according to standard
69 18 : auto resolution = util::parseLdrValue<double>(ldrs, "RESOLUTION");
70 18 : auto deltaR = util::parseLdrValue<double>(ldrs, "DELTAR");
71 18 : auto zdp = util::parseLdrValue<double>(ldrs, "ZDP");
72 : // string
73 54 : auto alias = util::parseLdrValue<std::string>(ldrs, "ALIAS");
74 :
75 36 : std::string missing{};
76 18 : missing += rUnits.has_value() ? "" : " RUNITS";
77 18 : missing += aUnits.has_value() ? "" : " AUNITS";
78 18 : missing += firstR.has_value() ? "" : " FIRSTR";
79 18 : missing += lastR.has_value() ? "" : " LASTR";
80 18 : missing += rFactor.has_value() ? "" : " RFACTOR";
81 18 : missing += aFactor.has_value() ? "" : " AFACTOR";
82 18 : missing += nPoints.has_value() ? "" : " NPOINTS";
83 18 : if (!missing.empty())
84 : {
85 : throw ParseException(
86 1 : "Required LDR(s) missing for RADATA: {" + missing + " }");
87 : }
88 :
89 : RaParameters parameters{
90 34 : rUnits.value(),
91 34 : aUnits.value(),
92 17 : firstR.value(),
93 34 : lastR.value(),
94 : maxA,
95 : minA,
96 34 : rFactor.value(),
97 34 : aFactor.value(),
98 34 : nPoints.value(),
99 : firstA,
100 : resolution,
101 : deltaR,
102 : zdp,
103 : alias,
104 34 : };
105 :
106 34 : return parameters;
107 : }
|