HOPS
HOPS class reference
MHO_VexHelpers.hh
Go to the documentation of this file.
1 #ifndef MHO_VexHelpers_HH__
2 #define MHO_VexHelpers_HH__
3 
4 #include <map>
5 #include <string>
6 
8 
9 namespace hops
10 {
11 
24 namespace MHO_VexHelpers
25 {
26 
27 //build map: 2-char station_code -> keyword (table name) for the $MODE.<section> entries
28 //whose qualifiers list a station code present in $STATION. Section entries with no
29 //qualifiers (shared/fallback tables) are NOT inserted, callers handle that explicitly
30 //via FirstKeywordInModeSection().
31 inline std::map< std::string, std::string > StationToKeywordMap(const mho_json& vex_root, const std::string& mode_name,
32  const std::string& section)
33 {
34  std::map< std::string, std::string > out;
35  if(!vex_root.contains("$MODE") || !vex_root["$MODE"].contains(mode_name))
36  {
37  return out;
38  }
39  if(!vex_root["$MODE"][mode_name].contains(section))
40  {
41  return out;
42  }
43  for(auto& entry : vex_root["$MODE"][mode_name][section])
44  {
45  std::string keyword = entry["keyword"].get< std::string >();
46  for(auto& qual : entry["qualifiers"])
47  {
48  std::string station_code = qual.get< std::string >();
49  if(vex_root["$STATION"].contains(station_code))
50  {
51  out[station_code] = keyword;
52  }
53  }
54  }
55  return out;
56 }
57 
58 //return the first entry's keyword in $MODE.<section> (the conventional shared-table
59 //fallback when no station-qualified entry exists).
60 //Returns "" if missing/empty.
61 inline std::string FirstKeywordInModeSection(const mho_json& vex_root, const std::string& mode_name, const std::string& section)
62 {
63  if(!vex_root.contains("$MODE") || !vex_root["$MODE"].contains(mode_name))
64  {
65  return "";
66  }
67  const auto& mode = vex_root["$MODE"][mode_name];
68  if(!mode.contains(section) || mode[section].empty())
69  {
70  return "";
71  }
72  return mode[section][0]["keyword"].get< std::string >();
73 }
74 
75 //resolve a per-station table name: prefer the station-qualified entry, fall back to
76 //the first entry in the section.
77 //returns "" if neither is available.
78 inline std::string TableForStation(const mho_json& vex_root, const std::string& mode_name, const std::string& section,
79  const std::string& station_code,
80  const std::map< std::string, std::string >* prebuilt_map = nullptr)
81 {
82  if(prebuilt_map)
83  {
84  auto it = prebuilt_map->find(station_code);
85  if(it != prebuilt_map->end())
86  return it->second;
87  }
88  else
89  {
90  auto m = StationToKeywordMap(vex_root, mode_name, section);
91  auto it = m.find(station_code);
92  if(it != m.end())
93  return it->second;
94  }
95  return FirstKeywordInModeSection(vex_root, mode_name, section);
96 }
97 
98 //traverse the ridiculous the BBC -> IF -> polarization chain for a single chan_def.bbc_id link
99 //returns "-" (nonsense) when any link in the chain can't be resolved
100 inline std::string ResolvePolarization(const mho_json& vex_root, const std::string& bbc_table, const std::string& if_table,
101  const std::string& bbc_id)
102 {
103  if(bbc_table.empty() || if_table.empty())
104  {
105  return "-";
106  }
107  if(!vex_root.contains("$BBC") || !vex_root["$BBC"].contains(bbc_table))
108  {
109  return "-";
110  }
111  if(!vex_root.contains("$IF") || !vex_root["$IF"].contains(if_table))
112  {
113  return "-";
114  }
115 
116  const auto& bbc_assigns = vex_root["$BBC"][bbc_table]["BBC_assign"];
117  for(const auto& ba : bbc_assigns)
118  {
119  if(ba["logical_bbc_id"].get< std::string >() != bbc_id)
120  {
121  continue;
122  }
123  std::string if_id = ba["logical_if"].get< std::string >();
124  const auto& if_defs = vex_root["$IF"][if_table]["if_def"];
125  for(const auto& ifd : if_defs)
126  {
127  if(ifd["if_id"].get< std::string >() == if_id)
128  {
129  return ifd["polarization"].get< std::string >();
130  }
131  }
132  return "-"; //matched BBC but no IF def
133  }
134  return "-";
135 }
136 
137 //resolve the mk4 1-char site_ID for a given 2-char vex station code (via $STATION -> $SITE link)
138 //returns "" when any link is missing.
139 inline std::string StationMk4SiteId(const mho_json& vex_root, const std::string& station_code)
140 {
141  if(!vex_root.contains("$STATION") || !vex_root["$STATION"].contains(station_code))
142  {
143  return "";
144  }
145  const auto& site_refs = vex_root["$STATION"][station_code]["$SITE"];
146  if(site_refs.empty())
147  {
148  return "";
149  }
150  std::string site_key = site_refs[0]["keyword"].get< std::string >();
151  if(!vex_root.contains("$SITE") || !vex_root["$SITE"].contains(site_key))
152  {
153  return "";
154  }
155  if(!vex_root["$SITE"][site_key].contains("mk4_site_ID"))
156  {
157  return "";
158  }
159  return vex_root["$SITE"][site_key]["mk4_site_ID"].get< std::string >();
160 }
161 
162 } // namespace MHO_VexHelpers
163 
164 } // namespace hops
165 
166 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
std::string ResolvePolarization(const mho_json &vex_root, const std::string &bbc_table, const std::string &if_table, const std::string &bbc_id)
Definition: MHO_VexHelpers.hh:100
std::string StationMk4SiteId(const mho_json &vex_root, const std::string &station_code)
Definition: MHO_VexHelpers.hh:139
std::string FirstKeywordInModeSection(const mho_json &vex_root, const std::string &mode_name, const std::string &section)
Definition: MHO_VexHelpers.hh:61
std::string TableForStation(const mho_json &vex_root, const std::string &mode_name, const std::string &section, const std::string &station_code, const std::map< std::string, std::string > *prebuilt_map=nullptr)
Definition: MHO_VexHelpers.hh:78
std::map< std::string, std::string > StationToKeywordMap(const mho_json &vex_root, const std::string &mode_name, const std::string &section)
Definition: MHO_VexHelpers.hh:31
Definition: MHO_AdhocFlagging.hh:18