HOPS
HOPS class reference
MHO_DiFXInputHelpers.hh
Go to the documentation of this file.
1 #ifndef MHO_DiFXInputHelpers_HH__
2 #define MHO_DiFXInputHelpers_HH__
3 
4 #include <cctype>
5 #include <string>
6 #include <tuple>
7 #include <utility>
8 #include <vector>
9 
10 #include "MHO_JSONHeaderWrapper.hh"
11 
12 namespace hops
13 {
14 
15 namespace MHO_DiFXInputHelpers
16 {
17 
18 inline bool IsZoomBand(const mho_json& ds, int band)
19 {
20  return band >= ds.value("nRecBand", 0);
21 }
22 
23 inline int GetDatastreamBandFreqId(const mho_json& ds, int band)
24 {
25  int nRecBand = ds.value("nRecBand", 0);
26  int nZoomBand = ds.value("nZoomBand", 0);
27  if(band < 0 || band >= nRecBand + nZoomBand)
28  {
29  return -1;
30  }
31 
32  const char* band_key = "recBandFreqId";
33  const char* freq_key = "recFreqId";
34  int local_band = band;
35  if(band >= nRecBand)
36  {
37  band_key = "zoomBandFreqId";
38  freq_key = "zoomFreqId";
39  local_band = band - nRecBand;
40  }
41 
42  if(!ds.contains(band_key) || !ds.contains(freq_key) || local_band >= (int)ds[band_key].size())
43  {
44  return -1;
45  }
46  int local_freq = ds[band_key][local_band].get< int >();
47  if(local_freq < 0 || local_freq >= (int)ds[freq_key].size())
48  {
49  return -1;
50  }
51  return ds[freq_key][local_freq].get< int >();
52 }
53 
54 inline std::string GetDatastreamBandPol(const mho_json& ds, int band)
55 {
56  int nRecBand = ds.value("nRecBand", 0);
57  const char* pol_key = "recBandPolName";
58  int local_band = band;
59  if(band >= nRecBand)
60  {
61  pol_key = "zoomBandPolName";
62  local_band = band - nRecBand;
63  }
64  if(band < 0 || !ds.contains(pol_key) || local_band >= (int)ds[pol_key].size())
65  {
66  return "";
67  }
68  return ds[pol_key][local_band].get< std::string >();
69 }
70 
71 inline bool HasTargetFreqs(const mho_json& input)
72 {
73  if(!input.contains("baseline"))
74  {
75  return false;
76  }
77  for(const auto& base : input["baseline"])
78  {
79  if(!base.contains("destFq"))
80  {
81  return false;
82  }
83  }
84  return true;
85 }
86 
87 inline std::vector< int > GetBaselineTargetFreqs(const mho_json& input, int bl)
88 {
89  std::vector< int > target_freqs;
90  if(!input.contains("baseline") || bl < 0 || bl >= (int)input["baseline"].size())
91  {
92  return target_freqs;
93  }
94 
95  const mho_json& base = input["baseline"][bl];
96  if(base.contains("destFq"))
97  {
98  return base["destFq"].get< std::vector< int > >();
99  }
100 
101  int nFreq = base.value("nFreq", 0);
102  target_freqs.assign(nFreq, -1);
103  int dsA = base.value("dsA", -1);
104  if(!input.contains("datastream") || dsA < 0 || dsA >= (int)input["datastream"].size() || !base.contains("bandA"))
105  {
106  return target_freqs;
107  }
108 
109  const mho_json& ds = input["datastream"][dsA];
110  for(int f = 0; f < nFreq && f < (int)base["bandA"].size(); f++)
111  {
112  if(!base["bandA"][f].empty())
113  {
114  target_freqs[f] = GetDatastreamBandFreqId(ds, base["bandA"][f][0].get< int >());
115  }
116  }
117  return target_freqs;
118 }
119 
120 inline long GetPhaseCalIntervalDivisor(const mho_json& ds)
121 {
122  long divisor = ds.value("phaseCalIntervalDivisor", 1L);
123  return (divisor > 0) ? divisor : 1L;
124 }
125 
126 inline double GetPhaseCalIntervalMHz(const mho_json& ds)
127 {
128  return ds.value("phaseCalIntervalMHz", 0.0) / (double)GetPhaseCalIntervalDivisor(ds);
129 }
130 
131 inline double GetPhaseCalBaseMHz(const mho_json& ds)
132 {
133  return ds.value("phaseCalBaseMHz", 0.0);
134 }
135 
136 inline std::string GetDatastreamAntennaName(const mho_json& input, const mho_json& ds)
137 {
138  int antennaId = ds.value("antennaId", -1);
139  if(!input.contains("antenna") || antennaId < 0 || antennaId >= (int)input["antenna"].size())
140  {
141  return "";
142  }
143  std::string name = input["antenna"][antennaId].value("name", std::string());
144  std::size_t len = 0;
145  while(len < name.size() && (std::isalnum((unsigned char)name[len]) || name[len] == '_'))
146  {
147  len++;
148  }
149  return name.substr(0, len);
150 }
151 
152 //band label for a sky frequency (MHz)
153 //when ranges overlap the narrower band gets precendence
154 //returns "" on no match
155 inline std::string DetermineFreqBand(const std::vector< std::tuple< std::string, double, double > >& bands, double freq)
156 {
157  //decide based on sky-freq of channel (we ignore bandwidth/sideband for now)
158  //so may not be correct for extra-wide channels...but if that is the case,
159  //probably some customized fband settings should be passed by user to specify the freq band labeling
160 
161  std::string fband = "";
162  double fband_width = 1e30;
163  for(const auto& band_tup : bands)
164  {
165  double low = std::get< 1 >(band_tup);
166  double high = std::get< 2 >(band_tup);
167  if(high < low)
168  {
169  std::swap(low, high);
170  }
171  double width = high - low;
172  if(freq < high && low < freq && width < fband_width)
173  {
174  fband = std::get< 0 >(band_tup);
175  fband_width = width;
176  //do not break here, so that narrower freq-bands will override larger ones
177  }
178  }
179  return fband;
180 }
181 
182 } // namespace MHO_DiFXInputHelpers
183 
184 } // namespace hops
185 
186 #endif /* end of include guard: MHO_DiFXInputHelpers_HH__ */
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
struct fbands fband[MAX_FBANDS]
Definition: difx2mark4.c:86
int base
Definition: fourfit3.c:62
long GetPhaseCalIntervalDivisor(const mho_json &ds)
Definition: MHO_DiFXInputHelpers.hh:120
std::string GetDatastreamBandPol(const mho_json &ds, int band)
Definition: MHO_DiFXInputHelpers.hh:54
bool HasTargetFreqs(const mho_json &input)
Definition: MHO_DiFXInputHelpers.hh:71
double GetPhaseCalBaseMHz(const mho_json &ds)
Definition: MHO_DiFXInputHelpers.hh:131
double GetPhaseCalIntervalMHz(const mho_json &ds)
Definition: MHO_DiFXInputHelpers.hh:126
std::string GetDatastreamAntennaName(const mho_json &input, const mho_json &ds)
Definition: MHO_DiFXInputHelpers.hh:136
std::string DetermineFreqBand(const std::vector< std::tuple< std::string, double, double > > &bands, double freq)
Definition: MHO_DiFXInputHelpers.hh:155
int GetDatastreamBandFreqId(const mho_json &ds, int band)
Definition: MHO_DiFXInputHelpers.hh:23
bool IsZoomBand(const mho_json &ds, int band)
Definition: MHO_DiFXInputHelpers.hh:18
std::vector< int > GetBaselineTargetFreqs(const mho_json &input, int bl)
Definition: MHO_DiFXInputHelpers.hh:87
Definition: MHO_AdhocFlagging.hh:18