PURIFY
Next-generation radio interferometric imaging
read_measurements.cc
Go to the documentation of this file.
2 
3 #include "purify/uvfits.h"
4 
5 #ifdef PURIFY_CASACORE
6 #include "purify/casacore.h"
7 #endif
8 #include <iostream>
9 #include <boost/filesystem.hpp>
10 #include <sys/stat.h>
11 
12 #ifdef PURIFY_MPI
13 #include "purify/mpi_utilities.h"
14 #endif
15 namespace purify {
16 namespace read_measurements {
17 utilities::vis_params read_measurements(const std::string &name, const bool w_term,
18  const stokes pol, const utilities::vis_units units) {
19  return read_measurements(std::vector<std::string>{name}, w_term, pol, units);
20 }
21 utilities::vis_params read_measurements(const std::vector<std::string> &names, const bool w_term,
22  const stokes pol, const utilities::vis_units units) {
23  std::vector<std::string> found_files;
24  format format_type = format::uvfits;
25  for (t_int i = 0; i < names.size(); i++) {
26  const boost::filesystem::path file_path(names.at(i));
27  if (not file_exists(file_path.native())) {
28  PURIFY_HIGH_LOG("Missing file will be removed from list: {}", names.at(i));
29  } else {
30  found_files.emplace_back(names.at(i));
31  format format_check = format::uvfits;
32  // checking if reading measurement set or .vis file
33  std::size_t found = names.at(i).find_last_of(".");
34  std::string format_string = "." + names.at(i).substr(found + 1);
35  std::transform(format_string.begin(), format_string.end(), format_string.begin(), ::tolower);
36  if (format_string == ".ms") {
37  format_check = format::ms;
38  if (not dir_exists(file_path.native()))
39  throw std::runtime_error(names.at(i) +
40  " is not a directory, as expected for a measurement set.");
41  } else if (format_string == ".vis") {
42  format_check = format::vis;
43  if (not file_exists(file_path.native()))
44  throw std::runtime_error(names.at(i) + " is not a regular file.");
45  } else if (format_string == ".h5") {
46  format_check = format::h5;
47  if (not file_exists(file_path.native()))
48  throw std::runtime_error(names.at(i) + " is not a regular file.");
49  } else if (format_string == ".uvfits") {
50  format_check = format::uvfits;
51  if (not file_exists(file_path.native()))
52  throw std::runtime_error(names.at(i) + " is not a regular file.");
53  } else
54  throw std::runtime_error("File extension for " + names.at(i) +
55  " not recognised. Must be .vis, .h5, .uvfits, or .ms.");
56  if (i == 0) format_type = format_check;
57  if (i > 0 and (format_check != format_type))
58  throw std::runtime_error("File extension is not the same for " + names.at(i) + " " +
59  names.at(i - 1));
60  }
61  }
62 
63  if (found_files.size() == 0) throw std::runtime_error("No files found.");
64  switch (format_type) {
65  case (format::vis): {
66  if (pol != stokes::I)
67  throw std::runtime_error("Stokes I assumed for vis files, but a different type is chosen.");
68  auto measurements = utilities::read_visibility(found_files, w_term);
69  measurements.units = units;
70  return measurements;
71  break;
72  }
73  case (format::h5): {
74 #ifdef PURIFY_H5
75  if (pol != stokes::I)
76  throw std::runtime_error("Stokes I assumed for HDF5 files, but a different type is chosen.");
77  auto measurements = utilities::read_visibility(found_files, w_term);
78  measurements.units = units;
79  return measurements;
80  break;
81 #else
82  throw std::runtime_error("HDF5 interface required to read h5 files.");
83 #endif
84  }
85  case (format::uvfits): {
86  return pfitsio::read_uvfits(found_files, true, pol);
87  break;
88  }
89  case (format::ms): {
90 #ifdef PURIFY_CASACORE
91  return casa::read_measurementset(found_files, pol);
92  break;
93 #else
94  throw std::runtime_error("Casacore interface required to read a measurement set.");
95 #endif
96  }
97  default:
98  throw std::runtime_error("Format not recognised.");
99  }
100 }
101 
102 #ifdef PURIFY_MPI
103 utilities::vis_params read_measurements(const std::string &name,
104  sopt::mpi::Communicator const &comm,
105  const distribute::plan plan, const bool w_term,
106  const stokes pol, const utilities::vis_units units) {
107  return read_measurements(std::vector<std::string>{name}, comm, plan, w_term, pol, units);
108 }
109 utilities::vis_params read_measurements(const std::vector<std::string> &names,
110  sopt::mpi::Communicator const &comm,
111  const distribute::plan plan, const bool w_term,
112  const stokes pol, const utilities::vis_units units) {
113  if (comm.size() == 1) {
114  try {
115  return read_measurements(names, w_term, pol, units);
116  } catch (const std::runtime_error &e) {
117  comm.abort(e.what());
118  }
119  }
120  if (comm.is_root()) {
121  utilities::vis_params result;
122  try {
123  result = read_measurements(names, w_term, pol, units);
124  } catch (const std::runtime_error &e) {
125  comm.abort(e.what());
126  }
127  auto const order = distribute::distribute_measurements(result, comm, plan);
128  return utilities::regroup_and_scatter(result, order, comm);
129  }
130  auto result = utilities::scatter_visibilities(comm);
131  return result;
132 }
133 #endif
135 bool file_exists(const std::string &path) {
136  struct stat buf;
137  return (stat(path.c_str(), &buf) == 0);
138 }
140 bool dir_exists(const std::string &path) {
141  struct stat buf;
142  return (stat(path.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode));
143 }
144 
145 } // namespace read_measurements
146 std::vector<std::string> split(const std::string &s, char delim) {
147  std::vector<std::string> elems;
148  split(s, delim, std::back_inserter(elems));
149  return elems;
150 }
151 void mkdir_recursive(const std::string &path) {
152  if (read_measurements::dir_exists(path)) return;
153  const auto folders = split(path, '/');
154  std::string current_path = "";
155  for (const auto f : folders) {
156  if (f == "") {
157  current_path += "/";
158  continue;
159  }
160  current_path += f;
161  if (not read_measurements::dir_exists(current_path)) {
162  const t_int status = mkdir(current_path.c_str(), ACCESSPERMS);
163  if (status != 0)
164  throw std::runtime_error("Error making recursive directory: " + current_path);
165  }
166  current_path += "/";
167  }
168  return;
169 }
170 } // namespace purify
#define PURIFY_HIGH_LOG(...)
High priority message.
Definition: logging.h:203
utilities::vis_params read_measurementset(std::string const &filename, const stokes polarization, const std::vector< t_int > &channels_input, std::string const &filter)
Read measurement set into vis_params structure.
Definition: casacore.cc:147
std::vector< t_int > distribute_measurements(Vector< t_real > const &u, Vector< t_real > const &v, Vector< t_real > const &w, t_int const number_of_nodes, distribute::plan const distribution_plan, t_int const &grid_size)
Distribute visiblities into groups.
Definition: distribute.cc:6
utilities::vis_params read_uvfits(const std::vector< std::string > &names, const bool flag, const stokes pol)
Read uvfits files from name of vector.
Definition: uvfits.cc:12
utilities::vis_params read_measurements(const std::vector< std::string > &names, const bool w_term, const stokes pol, const utilities::vis_units units)
read in measurements from a vector of file names
bool dir_exists(const std::string &path)
check that directory path exists
bool file_exists(const std::string &path)
check that file path exists
utilities::vis_params read_measurements(const std::string &name, const bool w_term, const stokes pol, const utilities::vis_units units)
read in single measurement file
vis_params scatter_visibilities(vis_params const &params, std::vector< t_int > const &sizes, sopt::mpi::Communicator const &comm)
vis_params regroup_and_scatter(vis_params const &params, std::vector< t_int > const &groups, sopt::mpi::Communicator const &comm)
utilities::vis_params read_visibility(const std::vector< std::string > &names, const bool w_term)
Read visibility files from name of vector.
void mkdir_recursive(const std::string &path)
recursively create directories when they do not exist
std::vector< std::string > split(const std::string &s, char delim)
splits string into vector
stokes
Definition: types.h:44