M1M3 Support System
Loading...
Searching...
No Matches
TableLoader.h
1/*
2 * This file is part of LSST M1M3 support system package.
3 *
4 * Developed for the Vera C. Rubin Telescope and Site System.
5 * This product includes software developed by the LSST Project
6 * (https://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This program 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 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TABLELOADER_H_
25#define TABLELOADER_H_
26
27#include <string>
28#include <vector>
29
30#include <spdlog/fmt/fmt.h>
31
32#include <rapidcsv.h>
33
34#include <Limit.h>
35#include <SettingReader.h>
36#include <cRIO/DataTypes.h>
37
38namespace LSST {
39namespace M1M3 {
40namespace SS {
41
43std::string rowToStr(std::vector<std::string> row);
44
46public:
47 template <typename t>
48 static void loadTable(size_t columnsToSkip, size_t columnsToKeep, std::vector<t> *data,
49 const std::string &filename);
50 static void loadLimitTable(size_t columnsToSkip, std::vector<Limit> *data, const std::string &filename);
51 static void loadForceLimitTable(size_t columnsToSkip, float zLow[FA_Z_COUNT], float zHigh[FA_Z_COUNT],
52 float yLow[FA_Y_COUNT], float yHigh[FA_Y_COUNT], float xLow[FA_X_COUNT],
53 float xHigh[FA_X_COUNT], const std::string &filename);
54};
55
56template <typename t>
57void TableLoader::loadTable(size_t columnsToSkip, size_t columnsToKeep, std::vector<t> *data,
58 const std::string &filename) {
59 std::string fullPath = SettingReader::instance().getTablePath(filename);
60 try {
61 rapidcsv::Document table(fullPath, rapidcsv::LabelParams(), rapidcsv::SeparatorParams(),
62 rapidcsv::ConverterParams(), rapidcsv::LineReaderParams(true, '#'));
63 data->clear();
64 if (columnsToSkip + columnsToKeep != table.GetColumnCount()) {
65 throw std::runtime_error(fmt::format("CSV {} has {} columns, expected {}", fullPath,
66 table.GetColumnCount(), columnsToSkip + columnsToKeep));
67 }
68 for (size_t row = 0; row < table.GetRowCount(); row++) {
69 for (size_t column = columnsToSkip; column < columnsToSkip + columnsToKeep; column++) {
70 try {
71 data->push_back(table.GetCell<t>(column, row));
72 } catch (std::logic_error &er) {
73 throw std::runtime_error(fmt::format("{}:{}:{}: cannot parse {}: {}", fullPath, row,
74 column, table.GetCell<std::string>(column, row),
75 er.what()));
76 }
77 }
78 }
79 } catch (std::ios_base::failure &er) {
80 throw std::runtime_error(fmt::format("Cannot read CSV {}: {}", fullPath, er.what()));
81 }
82}
83
84} /* namespace SS */
85} /* namespace M1M3 */
86} /* namespace LSST */
87
88#endif /* TABLELOADER_H_ */
Definition TableLoader.h:45