HOPS
HOPS class reference
MHO_PyScanStoreInterface.hh
Go to the documentation of this file.
1 #ifndef MHO_PyScanStoreInterface_HH__
2 #define MHO_PyScanStoreInterface_HH__
3 
6 
7 #include "MHO_ContainerStore.hh"
9 #include "MHO_ScanDataStore.hh"
10 
11 //need these extras to be able to translate between nl:json and py:dict or py::object
12 #include "pybind11_json/pybind11_json.hpp"
13 #include <pybind11/embed.h>
14 #include <pybind11/pybind11.h>
15 namespace py = pybind11;
16 namespace nl = nlohmann;
17 using namespace pybind11::literals;
18 
19 namespace hops
20 {
21 
31 {
32  public:
33  //this constructor to be called from c++ side (to provide interface to a pre-existing object)
34  MHO_PyScanStoreInterface(MHO_ScanDataStore* scan_store): fScanStore(scan_store)
35  {
36  fIsOwned = false;
37  fInitialized = false;
38  };
39 
40  //this constructor to be called from python side to create a stand-alone object
42  {
43  fScanStore = new MHO_ScanDataStore();
44  fIsOwned = true;
45  fInitialized = false;
46  };
47 
49  {
50  if(fIsOwned)
51  {
52  Clear();
53  delete fScanStore;
54  }
55  };
56 
57  void SetDirectory(std::string dir)
58  {
59  fScanStore->SetDirectory(dir);
60  fInitialized = false;
61  }
62 
63  bool Initialize()
64  {
65  fInitialized = fScanStore->Initialize();
66  return fInitialized;
67  };
68 
69  bool IsValid() const { return fScanStore->IsValid(); };
70 
71  bool IsBaselinePresent(std::string bl) const { return fScanStore->IsBaselinePresent(bl); }
72 
73  bool IsStationPresent(std::string st) const { return fScanStore->IsStationPresent(st); }
74 
75  bool IsFringePresent(std::string fr) const { return fScanStore->IsFringePresent(fr); }
76 
77  std::size_t GetNBaselines() const { return fScanStore->GetNBaselines(); }
78 
79  std::size_t GetNStations() const { return fScanStore->GetNStations(); }
80 
81  std::size_t GetNFringes() const { return fScanStore->GetNFringes(); }
82 
83  std::vector< std::string > GetBaselinesPresent() const { return fScanStore->GetBaselinesPresent(); }
84 
85  std::vector< std::string > GetStationsPresent() const { return fScanStore->GetStationsPresent(); }
86 
87  std::vector< std::string > GetFringesPresent() const { return fScanStore->GetFringesPresent(); }
88 
89  //retieve file data (root, baseline, station)
90  py::dict GetRootFileData() const { return fScanStore->GetRootFileData(); };
91 
92  std::string GetRootFileBasename() const { return fScanStore->GetRootFileBasename(); }
93 
94  std::string GetBaselineFilename(std::string baseline) const { return fScanStore->GetBaselineFilename(baseline); }
95 
96  std::string GetStationFilename(std::string station) const { return fScanStore->GetStationFilename(station); }
97 
98  std::string GetFringeFilename(std::string fringe) const { return fScanStore->GetFringeFilename(fringe); }
99 
100  int LoadBaseline(std::string baseline) //perhaps we should have an optional force-reload parameter?
101  {
102  if(!IsValid())
103  {
104  return -1;
105  } //not initialized, bail out
106 
107  if(!IsBaselinePresent(baseline))
108  {
109  return -2;
110  } //not present in this scan
111 
112  auto it = fBaselineContainers.find(baseline);
113  if(it != fBaselineContainers.end())
114  {
115  return 1;
116  } //ok, but already loaded
117 
119  fScanStore->LoadBaseline(baseline, con);
121  fBaselineContainers[baseline] = std::make_pair(con, inter);
122  return 0; //ok
123  }
124 
125  bool IsBaselineLoaded(std::string baseline) const
126  {
127  if(!fInitialized)
128  {
129  return false;
130  }
131  auto it = fBaselineContainers.find(baseline);
132  if(it != fBaselineContainers.end())
133  {
134  return true;
135  }
136  return false;
137  }
138 
139  int LoadStation(std::string station)
140  {
141  if(!IsValid())
142  {
143  return -1;
144  } //not initialized, bail out
145 
146  if(!IsStationPresent(station))
147  {
148  return -2;
149  } //not present in this scan
150 
151  auto it = fStationContainers.find(station);
152  if(it != fStationContainers.end())
153  {
154  return 1;
155  } //ok, but already loaded
156 
158  fScanStore->LoadStation(station, con);
160  fStationContainers[station] = std::make_pair(con, inter);
161  return 0; //ok
162  }
163 
164  bool IsStationLoaded(std::string station) const
165  {
166  if(!fInitialized)
167  {
168  return false;
169  }
170  auto it = fStationContainers.find(station);
171  if(it != fStationContainers.end())
172  {
173  return true;
174  }
175  return false;
176  }
177 
178  int LoadFringe(std::string fringe)
179  {
180  if(!IsValid())
181  {
182  return -1;
183  } //not initialized, bail out
184 
185  if(!IsFringePresent(fringe))
186  {
187  return -2;
188  } //not present in this scan
189 
190  auto it = fFringeContainers.find(fringe);
191  if(it != fFringeContainers.end())
192  {
193  return 1;
194  } //ok, but already loaded
195 
197  fScanStore->LoadFringe(fringe, con);
199  fFringeContainers[fringe] = std::make_pair(con, inter);
200  return 0; //ok
201  }
202 
203  bool IsFringeLoaded(std::string fringe) const
204  {
205  if(!fInitialized)
206  {
207  return false;
208  }
209  auto it = fFringeContainers.find(fringe);
210  if(it != fFringeContainers.end())
211  {
212  return true;
213  }
214  return false;
215  }
216 
217  std::vector< std::string > GetBaselinesLoaded() const
218  {
219  std::vector< std::string > baselines;
220  for(auto it = fBaselineContainers.begin(); it != fBaselineContainers.end(); it++)
221  {
222  baselines.push_back(it->first);
223  }
224  return baselines;
225  }
226 
227  std::vector< std::string > GetStationsLoaded() const
228  {
229  std::vector< std::string > stations;
230  for(auto it = fStationContainers.begin(); it != fStationContainers.end(); it++)
231  {
232  stations.push_back(it->first);
233  }
234  return stations;
235  }
236 
237  std::vector< std::string > GetFringesLoaded() const
238  {
239  std::vector< std::string > fringes;
240  for(auto it = fFringeContainers.begin(); it != fFringeContainers.end(); it++)
241  {
242  fringes.push_back(it->first);
243  }
244  return fringes;
245  }
246 
248  {
249  auto it = fBaselineContainers.find(baseline);
250  if(it != fBaselineContainers.end())
251  {
252  return *(it->second.second);
253  }
254  //should not be able to reach here from python interface (via lambda)
255  //but since we must have a return value, return an empty container
256  py::print("could not find baseline: ", baseline, ", returning empty container ");
257  return fEmptyContainer;
258  }
259 
261  {
262  auto it = fStationContainers.find(station);
263  if(it != fStationContainers.end())
264  {
265  return *(it->second.second);
266  }
267  //should not be able to reach here from python interface (via lambda)
268  //but since we must have a return value, return an empty container
269  py::print("could not find station: ", station, ", returning empty container ");
270  return fEmptyContainer;
271  }
272 
274  {
275  auto it = fFringeContainers.find(fringe);
276  if(it != fFringeContainers.end())
277  {
278  return *(it->second.second);
279  }
280  //should not be able to reach here from python interface (via lambda)
281  //but since we must have a return value, return an empty container
282  py::print("could not find fringe: ", fringe, ", returning empty container ");
283  return fEmptyContainer;
284  }
285 
286  //deletes all loaded containers and resets the state for another scan.
287  void Clear()
288  {
289  for(auto it = fBaselineContainers.begin(); it != fBaselineContainers.end(); it++)
290  {
291  delete it->second.second;
292  delete it->second.first;
293  }
294  for(auto it = fStationContainers.begin(); it != fStationContainers.end(); it++)
295  {
296  delete it->second.second;
297  delete it->second.first;
298  }
299  for(auto it = fFringeContainers.begin(); it != fFringeContainers.end(); it++)
300  {
301  delete it->second.second;
302  delete it->second.first;
303  }
304  fBaselineContainers.clear();
305  fStationContainers.clear();
306  fFringeContainers.clear();
307  fInitialized = false;
308  }
309 
310  private:
311  //provides directory/data-loading interface
312  MHO_ScanDataStore* fScanStore;
313  bool fIsOwned; //declare ownership of fScanStore
314  bool fInitialized;
315 
316  //an empty container so we can a reference under certain error conditions
317  MHO_PyContainerStoreInterface fEmptyContainer;
318 
319  std::map< std::string, std::pair< MHO_ContainerStore*, MHO_PyContainerStoreInterface* > > fBaselineContainers;
320  std::map< std::string, std::pair< MHO_ContainerStore*, MHO_PyContainerStoreInterface* > > fStationContainers;
321  std::map< std::string, std::pair< MHO_ContainerStore*, MHO_PyContainerStoreInterface* > > fFringeContainers;
322 };
323 
324 inline void DeclarePyScanStoreInterface(py::module& m, std::string pyclass_name)
325 {
326  py::class_< MHO_PyScanStoreInterface >(m, pyclass_name.c_str())
327  .def(py::init<>()) //can build this class from python
328  .def("set_directory", &hops::MHO_PyScanStoreInterface::SetDirectory,
329  "specify the path to the directory of the scan to be loaded", py::arg("directory"))
330  .def("initialize", &hops::MHO_PyScanStoreInterface::Initialize,
331  "initialize the scan storage container after setting the directory")
332  .def("is_valid", &hops::MHO_PyScanStoreInterface::IsValid, "check if initialization was successful")
334  "check if baseline (2-char code) is present in this scan", py::arg("baseline"))
336  "check if station (1-char code) is present in this scan", py::arg("station"))
338  "check if fringe file (file basename) is present in this scan", py::arg("fringe_file_basename"))
339  .def("is_baseline_loaded", &hops::MHO_PyScanStoreInterface::IsBaselineLoaded,
340  "check if baseline has been loaded into memory, must be true before get_baseline_data can be called",
341  py::arg("baseline"))
342  .def("is_station_loaded", &hops::MHO_PyScanStoreInterface::IsStationLoaded,
343  "check if station has been loaded into memory, must be true before get_station_data can be called",
344  py::arg("station"))
345  .def("is_fringe_loaded", &hops::MHO_PyScanStoreInterface::IsFringeLoaded,
346  "check if fringe has been loaded into memory, must be true before get_fringe_data can be called",
347  py::arg("fringe_file_basename"))
348  .def("get_nbaselines", &hops::MHO_PyScanStoreInterface::GetNBaselines,
349  "get the number of baselines in the current scan")
350  .def("get_nstations", &hops::MHO_PyScanStoreInterface::GetNStations, "get the number of stations in the current scan")
351  .def("get_nfringes", &hops::MHO_PyScanStoreInterface::GetNFringes, "get the number of fringe files in the current scan")
352  .def("get_baseline_list", &hops::MHO_PyScanStoreInterface::GetBaselinesPresent,
353  "get the list of baselines in the current scan")
354  .def("get_station_list", &hops::MHO_PyScanStoreInterface::GetStationsPresent,
355  "get the list of stations in the current scan")
356  .def("get_fringe_list", &hops::MHO_PyScanStoreInterface::GetFringesPresent,
357  "get the list of fringe files available in the current scan")
358  .def("get_rootfile_data", &hops::MHO_PyScanStoreInterface::GetRootFileData,
359  "get the root/ovex file data object as a dictionary")
360  .def("get_rootfile_basename", &hops::MHO_PyScanStoreInterface::GetRootFileBasename,
361  "get the file basename of the root/ovex file")
362  .def("get_baseline_filename", &hops::MHO_PyScanStoreInterface::GetBaselineFilename,
363  "get the filename associated with the baseline 2-char code", py::arg("baseline"))
364  .def("get_station_filename", &hops::MHO_PyScanStoreInterface::GetStationFilename,
365  "get the filename associated with the station 1-char code", py::arg("station"))
366  .def("get_fringe_filename", &hops::MHO_PyScanStoreInterface::GetFringeFilename,
367  "get the filename associated with the fringe basename", py::arg("fringe"))
368  .def("load_baseline", &hops::MHO_PyScanStoreInterface::LoadBaseline, "load the baseline data container into memory",
369  py::arg("baseline"))
370  .def("load_station", &hops::MHO_PyScanStoreInterface::LoadStation, "load the station data container into memory",
371  py::arg("station"))
372  .def("load_fringe", &hops::MHO_PyScanStoreInterface::LoadFringe, "load the fringe data container into memory",
373  py::arg("fringe"))
374  .def("get_baselines_loaded", &hops::MHO_PyScanStoreInterface::GetBaselinesLoaded,
375  "get the list of baseline container objects that have been loaded into memory")
376  .def("get_stations_loaded", &hops::MHO_PyScanStoreInterface::GetStationsLoaded,
377  "get the list of station container objects that have been loaded into memory")
378  .def("get_fringes_loaded", &hops::MHO_PyScanStoreInterface::GetFringesLoaded,
379  "get the list of fringe container objects that have been loaded into memory")
380  .def(
381  "get_baseline_data", //lambda for returing either baseline data or None-type
382  [=](MHO_PyScanStoreInterface& m, std::string baseline) -> py::object {
383  if(m.IsBaselineLoaded(baseline))
384  {
385  return py::cast(m.GetBaselineData(baseline));
386  }
387  py::print("data for baseline ", baseline, " either has not been loaded or it does not exist in this scan.");
388  return py::object(py::cast(nullptr));
389  },
390  py::return_value_policy::reference, "get the baseline data container that has been loaded into memory",
391  py::arg("baseline"))
392  .def(
393  "get_station_data", //lambda for returing either station data or None-type
394  [=](MHO_PyScanStoreInterface& m, std::string station) -> py::object {
395  if(m.IsStationLoaded(station))
396  {
397  return py::cast(m.GetStationData(station));
398  }
399  py::print("data for station ", station, " either has not been loaded or it does not exist in this scan.");
400  return py::object(py::cast(nullptr));
401  },
402  py::return_value_policy::reference, "get the station data container that has been loaded into memory",
403  py::arg("station"))
404  .def(
405  "get_fringe_data", //lambda for returing either fringe data or None-type
406  [=](MHO_PyScanStoreInterface& m, std::string fringe) -> py::object {
407  if(m.IsFringeLoaded(fringe))
408  {
409  return py::cast(m.GetFringeData(fringe));
410  }
411  py::print("data for fringe ", fringe, " either has not been loaded or it does not exist in this scan.");
412  return py::object(py::cast(nullptr));
413  },
414  py::return_value_policy::reference, "get the fringe data container that has been loaded into memory",
415  py::arg("fringe"))
417  "clear memory of the current scan contents and reset to uninitialized state");
418 }
419 
420 } // namespace hops
421 
422 #endif
Class MHO_ContainerStore.
Definition: MHO_ContainerStore.hh:32
python binding to the MHO_ContainerStore
Definition: MHO_PyContainerStoreInterface.hh:30
python bindings for the MHO_ScanDataStore
Definition: MHO_PyScanStoreInterface.hh:31
bool Initialize()
Definition: MHO_PyScanStoreInterface.hh:63
std::vector< std::string > GetStationsLoaded() const
Definition: MHO_PyScanStoreInterface.hh:227
MHO_PyContainerStoreInterface & GetStationData(std::string station)
Definition: MHO_PyScanStoreInterface.hh:260
py::dict GetRootFileData() const
Definition: MHO_PyScanStoreInterface.hh:90
MHO_PyContainerStoreInterface & GetFringeData(std::string fringe)
Definition: MHO_PyScanStoreInterface.hh:273
std::string GetRootFileBasename() const
Definition: MHO_PyScanStoreInterface.hh:92
void Clear()
Definition: MHO_PyScanStoreInterface.hh:287
virtual ~MHO_PyScanStoreInterface()
Definition: MHO_PyScanStoreInterface.hh:48
int LoadBaseline(std::string baseline)
Definition: MHO_PyScanStoreInterface.hh:100
std::string GetStationFilename(std::string station) const
Definition: MHO_PyScanStoreInterface.hh:96
std::vector< std::string > GetBaselinesLoaded() const
Definition: MHO_PyScanStoreInterface.hh:217
std::vector< std::string > GetFringesLoaded() const
Definition: MHO_PyScanStoreInterface.hh:237
std::size_t GetNStations() const
Definition: MHO_PyScanStoreInterface.hh:79
bool IsBaselinePresent(std::string bl) const
Definition: MHO_PyScanStoreInterface.hh:71
std::vector< std::string > GetFringesPresent() const
Definition: MHO_PyScanStoreInterface.hh:87
MHO_PyScanStoreInterface()
Definition: MHO_PyScanStoreInterface.hh:41
bool IsFringePresent(std::string fr) const
Definition: MHO_PyScanStoreInterface.hh:75
bool IsStationPresent(std::string st) const
Definition: MHO_PyScanStoreInterface.hh:73
MHO_PyScanStoreInterface(MHO_ScanDataStore *scan_store)
Definition: MHO_PyScanStoreInterface.hh:34
MHO_PyContainerStoreInterface & GetBaselineData(std::string baseline)
Definition: MHO_PyScanStoreInterface.hh:247
std::vector< std::string > GetBaselinesPresent() const
Definition: MHO_PyScanStoreInterface.hh:83
std::size_t GetNFringes() const
Definition: MHO_PyScanStoreInterface.hh:81
bool IsStationLoaded(std::string station) const
Definition: MHO_PyScanStoreInterface.hh:164
void SetDirectory(std::string dir)
Definition: MHO_PyScanStoreInterface.hh:57
bool IsFringeLoaded(std::string fringe) const
Definition: MHO_PyScanStoreInterface.hh:203
int LoadFringe(std::string fringe)
Definition: MHO_PyScanStoreInterface.hh:178
std::size_t GetNBaselines() const
Definition: MHO_PyScanStoreInterface.hh:77
std::string GetBaselineFilename(std::string baseline) const
Definition: MHO_PyScanStoreInterface.hh:94
int LoadStation(std::string station)
Definition: MHO_PyScanStoreInterface.hh:139
std::string GetFringeFilename(std::string fringe) const
Definition: MHO_PyScanStoreInterface.hh:98
bool IsValid() const
Definition: MHO_PyScanStoreInterface.hh:69
bool IsBaselineLoaded(std::string baseline) const
Definition: MHO_PyScanStoreInterface.hh:125
std::vector< std::string > GetStationsPresent() const
Definition: MHO_PyScanStoreInterface.hh:85
Class to catalog and organize data files that are associated with a single scan, and handle retrieval...
Definition: MHO_ScanDataStore.hh:28
Definition: difx2mark4.h:102
struct mk4_fringe fringe
Definition: fourfit3.c:54
int baseline
Definition: fourfit3.c:62
Definition: MHO_ChannelLabeler.hh:17
void Clear()
Definition: MHO_OpenCLFastFourierTransformPlan.cc:83
void DeclarePyScanStoreInterface(py::module &m, std::string pyclass_name)
Definition: MHO_PyScanStoreInterface.hh:324
Definition: vex.h:94