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/XyBase.hpp"
19 : #include "jdx/ParseException.hpp"
20 : #include "util/DataParser.hpp"
21 : #include "util/LdrUtils.hpp"
22 :
23 : #include <limits>
24 :
25 51 : libjdx::jdx::XyBase::XyBase(const std::string& label,
26 : const std::string& variableList, const std::vector<StringLdr>& ldrs,
27 : const std::string& expectedLabel,
28 : const std::vector<std::string>& expectedVariableLists,
29 51 : io::TextReader& reader, std::optional<std::string>& nextLine)
30 57 : : Data2D{label, variableList, reader}
31 : {
32 51 : validateInput(
33 : getLabel(), getVariableList(), expectedLabel, expectedVariableLists);
34 49 : m_parameters = parseParameters(ldrs);
35 48 : util::skipToNextLdr(reader, nextLine, true);
36 48 : }
37 :
38 5 : const libjdx::jdx::XyParameters& libjdx::jdx::XyBase::getParameters() const
39 : {
40 5 : return m_parameters;
41 : }
42 :
43 11 : std::vector<std::pair<double, double>> libjdx::jdx::XyBase::getXppYYData() const
44 : {
45 11 : return Data2D::parseXppYYData(getLabel(), getReader(), m_parameters.firstX,
46 11 : m_parameters.lastX, m_parameters.yFactor, m_parameters.nPoints);
47 : }
48 :
49 10 : std::vector<std::pair<double, double>> libjdx::jdx::XyBase::getXYXYData() const
50 : {
51 10 : return Data2D::parseXyXyData(getLabel(), getReader(), m_parameters.xFactor,
52 10 : m_parameters.yFactor, m_parameters.nPoints);
53 : }
54 :
55 49 : libjdx::jdx::XyParameters libjdx::jdx::XyBase::parseParameters(
56 : const std::vector<StringLdr>& ldrs)
57 : {
58 : // required
59 : // string
60 147 : auto xUnits = util::parseLdrValue<std::string>(ldrs, "XUNITS");
61 147 : auto yUnits = util::parseLdrValue<std::string>(ldrs, "YUNITS");
62 : // double
63 49 : auto firstX = util::parseLdrValue<double>(ldrs, "FIRSTX");
64 49 : auto lastX = util::parseLdrValue<double>(ldrs, "LASTX");
65 49 : auto xFactor = util::parseLdrValue<double>(ldrs, "XFACTOR");
66 49 : auto yFactor = util::parseLdrValue<double>(ldrs, "YFACTOR");
67 : // uint64_t
68 49 : auto nPoints = util::parseLdrValue<uint64_t>(ldrs, "NPOINTS");
69 : // optional
70 : // double
71 49 : auto firstY = util::parseLdrValue<double>(ldrs, "FIRSTY");
72 49 : auto maxX = util::parseLdrValue<double>(ldrs, "MAXX");
73 49 : auto minX = util::parseLdrValue<double>(ldrs, "MINX");
74 49 : auto maxY = util::parseLdrValue<double>(ldrs, "MAXY");
75 49 : auto minY = util::parseLdrValue<double>(ldrs, "MINY");
76 49 : auto resolution = util::parseLdrValue<double>(ldrs, "RESOLUTION");
77 49 : auto deltaX = util::parseLdrValue<double>(ldrs, "DELTAX");
78 :
79 98 : std::string missing{};
80 49 : missing += xUnits.has_value() ? "" : " XUNITS";
81 49 : missing += yUnits.has_value() ? "" : " YUNITS";
82 49 : missing += firstX.has_value() ? "" : " FIRSTX";
83 49 : missing += lastX.has_value() ? "" : " LASTX";
84 49 : missing += xFactor.has_value() ? "" : " XFACTOR";
85 49 : missing += yFactor.has_value() ? "" : " YFACTOR";
86 49 : missing += nPoints.has_value() ? "" : " NPOINTS";
87 49 : if (!missing.empty())
88 : {
89 : throw ParseException(
90 1 : "Required LDR(s) missing for XYDATA: {" + missing + " }");
91 : }
92 :
93 : XyParameters parameters{
94 96 : xUnits.value(),
95 96 : yUnits.value(),
96 48 : firstX.value(),
97 96 : lastX.value(),
98 : maxX,
99 : minX,
100 : maxY,
101 : minY,
102 96 : xFactor.value(),
103 96 : yFactor.value(),
104 96 : nPoints.value(),
105 : firstY,
106 : resolution,
107 : deltaX,
108 96 : };
109 :
110 96 : return parameters;
111 : }
|