DP3
ValuePerStationParsing.h
Go to the documentation of this file.
1 // Copyright (C) 2025 ASTRON (Netherlands Institute for Radio Astronomy)
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 #ifndef DP3_COMMON_VALUE_PER_STATION_PARSING_H_
5 #define DP3_COMMON_VALUE_PER_STATION_PARSING_H_
6 
7 #include <regex>
8 #include <span>
9 #include <sstream>
10 #include <stdexcept>
11 #include <string>
12 
13 #include <aocommon/logger.h>
14 #include <aocommon/uvector.h>
15 
16 #include "ParameterValue.h"
17 #include "StringTools.h"
18 
19 namespace dp3::common {
20 
27 template <typename ValueType>
28 void AssignStationValues(std::span<ValueType> result, ValueType value,
29  const std::string pattern_string,
30  std::span<const std::string> station_names) {
31  // This function is based on StationAdder::GetMatchingStations(). Because we
32  // don't need to have the indices of the stations, that function is slightly
33  // adapted here.
34  aocommon::UVector<bool> is_selected(station_names.size(), false);
35  const std::vector<std::string> patterns(
36  ParameterValue(pattern_string).getStringVector());
37  for (const std::string& pattern : patterns) {
38  int n = 0;
39  const auto SetSelected = [&](bool new_value, const std::string& pattern) {
40  const std::regex pattern_regex(pattern);
41  for (size_t i = 0; i < station_names.size(); ++i) {
42  if (std::regex_match(station_names[i], pattern_regex)) {
43  is_selected[i] = new_value;
44  n++;
45  }
46  }
47  };
48  if (pattern.size() > 1 && (pattern[0] == '!' || pattern[0] == '^')) {
49  SetSelected(false, PatternToRegex(pattern.substr(1)));
50  } else {
51  SetSelected(true, PatternToRegex(pattern));
52  }
53  if (n == 0) {
54  aocommon::Logger::Warn << "No matching stations found for pattern "
55  << pattern << '\n';
56  }
57  }
58  for (size_t i = 0; i < is_selected.size(); ++i) {
59  if (is_selected[i]) result[i] = value;
60  }
61 }
62 
78 template <typename ValueType>
79 void ParseValuePerStation(std::span<ValueType> result,
80  std::span<const std::string> string_list,
81  std::span<const std::string> station_names) {
82  assert(result.size() == station_names.size());
83 
84  for (const std::string& stations_value : string_list) {
85  const size_t colon = stations_value.find(':');
86  if (colon == std::string::npos)
87  throw std::runtime_error(
88  "Items of station-value lists must be of the form "
89  "'station-pattern:value'. Item is missing a colon: '" +
90  stations_value + "'");
91  const std::string value_string(
92  stations_value.substr(colon + 1, stations_value.size() - colon));
93  std::istringstream value_stream(value_string);
94  ValueType value;
95  value_stream >> value;
96  std::string station_pattern(stations_value.substr(0, colon));
97  AssignStationValues(result, value, station_pattern, station_names);
98  }
99 }
100 
101 } // namespace dp3::common
102 
103 #endif
The value of a parameter.
Definition: ParameterValue.h:24
Definition: BaselineSelection.h:20
void AssignStationValues(std::span< ValueType > result, ValueType value, const std::string pattern_string, std::span< const std::string > station_names)
Definition: ValuePerStationParsing.h:28
void ParseValuePerStation(std::span< ValueType > result, std::span< const std::string > string_list, std::span< const std::string > station_names)
Definition: ValuePerStationParsing.h:79
std::string PatternToRegex(const std::string &pattern)