2 #include "purify/config.h"
15 bool has_suffix(
const std::string &str,
const std::string &suff) {
16 return str.size() >= suff.size() && str.compare(str.size() - suff.size(), suff.size(), suff) == 0;
20 Matrix<t_real> B = Matrix<t_real>::Zero(N, 3);
21 const t_real
mean = 0;
24 std::random_device rd;
25 std::mt19937_64 rng(rd());
30 output = normal_dist(rng);
32 if (output != output)
PURIFY_DEBUG(
"New random-sample density: {}", output);
35 for (t_uint i = 0; i < N; i++) {
44 const t_real times,
const t_real theta_ra,
45 const t_real phi_dec,
const t_real latitude) {
46 return antenna_to_coverage(B, std::vector<t_real>(1, frequency), std::vector<t_real>(1, times),
47 theta_ra, phi_dec, latitude);
51 const std::vector<t_real> ×,
const t_real theta_ra,
52 const t_real phi_dec,
const t_real latitude) {
58 const std::vector<t_real> &frequency,
const t_real times,
59 const t_real theta_ra,
const t_real phi_dec,
60 const t_real latitude) {
66 const std::vector<t_real> &frequencies,
67 const std::vector<t_real> times,
const t_real theta_ra,
68 const t_real phi_dec,
const t_real latitude) {
69 auto const position_angle_RA_function = [theta_ra](
const t_real t) -> t_real {
72 auto const position_angle_DEC_function = [phi_dec](
const t_real t) -> t_real {
return phi_dec; };
74 B, frequencies, times, position_angle_RA_function, position_angle_DEC_function, latitude);
83 std::ifstream pos_file(pos_name);
84 pos_file.precision(13);
88 while (std::getline(pos_file, line)) ++row;
89 if (row < 1)
throw std::runtime_error(
"No positions in the file: " + pos_name);
90 Matrix<t_real> B = Matrix<t_real>::Zero(row, 3);
95 for (row = 0; row < B.rows(); ++row) {
113 std::random_device rd;
114 std::mt19937_64 rng(rd());
119 output = normal_dist(rng);
121 if (output != output)
PURIFY_DEBUG(
"New random-sample density: {}", output);
126 uv_vis.
u = Vector<t_real>::Zero(vis_num);
127 uv_vis.
v = Vector<t_real>::Zero(vis_num);
128 uv_vis.
w = Vector<t_real>::Zero(vis_num);
130 for (t_uint i = 0; i < vis_num; i++) {
131 uv_vis.
u(i) = sample();
132 uv_vis.
v(i) = sample();
133 uv_vis.
w(i) = sample();
136 uv_vis.
weights = Vector<t_complex>::Constant(vis_num, 1);
137 uv_vis.
vis = Vector<t_complex>::Constant(vis_num, 1);
146 if (names.size() == 1)
return output;
147 for (
int i = 1; i < names.size(); i++) output =
read_visibility(names.at(i), output);
152 const bool w_term = not uv1.
w.isZero(0);
156 uv.
u = Vector<t_real>::Zero(uv1.
size() + uv2.size());
157 uv.
v = Vector<t_real>::Zero(uv1.
size() + uv2.size());
158 uv.
w = Vector<t_real>::Zero(uv1.
size() + uv2.size());
159 uv.
vis = Vector<t_complex>::Zero(uv1.
size() + uv2.size());
160 uv.
weights = Vector<t_complex>::Zero(uv1.
size() + uv2.size());
161 uv.
u.segment(0, uv1.
size()) = uv1.
u;
162 uv.
v.segment(0, uv1.
size()) = uv1.
v;
163 uv.
w.segment(0, uv1.
size()) = uv1.
w;
166 uv.
u.segment(uv1.
size(), uv2.size()) = uv2.u;
167 uv.
v.segment(uv1.
size(), uv2.size()) = uv2.v;
168 uv.
w.segment(uv1.
size(), uv2.size()) = uv2.w;
169 uv.
vis.segment(uv1.
size(), uv2.size()) = uv2.vis;
170 uv.
weights.segment(uv1.
size(), uv2.size()) = uv2.weights;
178 return std::stod(input);
187 std::ifstream vis_file(vis_name);
191 throw std::runtime_error(
"Could not open file " + vis_name);
193 vis_file.precision(13);
197 while (std::getline(vis_file, line)) ++row;
198 Vector<t_real> utemp = Vector<t_real>::Zero(row);
199 Vector<t_real> vtemp = Vector<t_real>::Zero(row);
200 Vector<t_real> wtemp = Vector<t_real>::Zero(row);
201 Vector<t_complex> vistemp = Vector<t_complex>::Zero(row);
202 Vector<t_complex> weightstemp = Vector<t_complex>::Zero(row);
210 for (row = 0; row < vistemp.size(); ++row) {
219 vistemp(row) = t_complex(real, imag);
220 weightstemp(row) = 1 / entry;
227 uv_vis.
vis = vistemp;
250 std::ofstream out(file_name);
252 for (t_int i = 0; i < uv_vis.
u.size(); ++i) {
253 out << uv_vis.
u(i) <<
" " << -uv_vis.
v(i) <<
" ";
254 if (w_term) out << uv_vis.
w(i) <<
" ";
255 out << std::real(uv_vis.
vis(i)) <<
" " << std::imag(uv_vis.
vis(i)) <<
" "
256 << 1. / std::real(uv_vis.
weights(i)) << std::endl;
262 const t_real &max_v,
const t_real &input_cell_size_u,
263 const t_real &input_cell_size_v) {
273 t_real cell_size_u = input_cell_size_u;
274 t_real cell_size_v = input_cell_size_v;
275 if (cell_size_u == 0 and cell_size_v == 0) {
281 if (cell_size_v == 0) {
282 cell_size_v = cell_size_u;
285 PURIFY_MEDIUM_LOG(
"Using a pixel size of {} by {} arcseconds", cell_size_u, cell_size_v);
286 t_real scale_factor_u = 1;
287 t_real scale_factor_v = 1;
289 scale_factor_u = 180 * 3600 / cell_size_u /
constant::pi;
290 scale_factor_v = 180 * 3600 / cell_size_v /
constant::pi;
291 scaled_vis.
w = uv_vis.
w;
296 scaled_vis.
w = uv_vis.
w;
305 const t_real &cell_size_v) {
306 const t_real max_u = std::sqrt((uv_vis.
u.array() * uv_vis.
u.array()).maxCoeff());
307 const t_real max_v = std::sqrt((uv_vis.
v.array() * uv_vis.
v.array()).maxCoeff());
308 return set_cell_size(uv_vis, max_u, max_v, cell_size_u, cell_size_v);
312 const t_int &sizey) {
319 scaled_vis.
vis = uv_vis.
vis;
321 for (t_int i = 0; i < uv_vis.
u.size(); ++i) {
325 scaled_vis.
w = uv_vis.
w;
327 scaled_vis.
ra = uv_vis.
ra;
328 scaled_vis.
dec = uv_vis.
dec;
333 const t_real cell_y,
const t_real imsizex,
334 const t_real imsizey,
const t_real oversample_ratio) {
342 du =
constant::pi / std::floor(imsizex * oversample_ratio);
343 dv =
constant::pi / std::floor(imsizey * oversample_ratio);
346 out.
u = uv_vis.
u / du;
347 out.v = uv_vis.
v / dv;
354 #pragma omp parallel for
355 for (t_uint i = 0; i < output.
size(); i++) {
356 if (uv_vis.
w(i) < 0) {
357 output.
w(i) = -uv_vis.
w(i);
358 output.
v(i) = -uv_vis.
v(i);
359 output.
u(i) = -uv_vis.
u(i);
360 output.
vis(i) = std::conj(uv_vis.
vis(i));
362 assert(output.
w(i) >= 0);
#define PURIFY_LOW_LOG(...)
Low priority message.
#define PURIFY_DEBUG(...)
\macro Output some debugging
#define PURIFY_MEDIUM_LOG(...)
Medium priority message.
utilities::vis_params read_visibility(const std::string &vis_name, const bool w_term)
Reads an HDF5 file with u,v visibilities, constructs a vis_params object and returns it.
const t_real pi
mathematical constant
const t_real omega_e
angular velocity of the earth rad/s
void write_visibility(const utilities::vis_params &uv_vis, const std::string &file_name, const bool w_term)
Writes visibilities to txt.
utilities::vis_params antenna_to_coverage(const Matrix< t_real > &B, const t_real frequency, const t_real times, const t_real theta_ra, const t_real phi_dec, const t_real latitude)
Provided antenna positions generate a coverage for a fixed frequency a fixed time.
Matrix< t_real > generate_antennas(const t_uint N, const t_real scale)
Generate guassianly distributed (sigma = scale) antenna positions.
utilities::vis_params antenna_to_coverage_general(const Matrix< t_real > &B, const std::vector< t_real > &frequencies, const std::vector< t_real > ×, const F &position_angle_RA_function, const F &position_angle_DEC_function, const t_real latitude)
utilities::vis_params convert_to_pixels(const utilities::vis_params &uv_vis, const t_real cell_x, const t_real cell_y, const t_real imsizex, const t_real imsizey, const t_real oversample_ratio)
Converts u and v coordaintes to units of pixels.
utilities::vis_params uv_scale(const utilities::vis_params &uv_vis, const t_int &sizex, const t_int &sizey)
scales the visibilities to units of pixels
K::Scalar mean(const K x)
Calculate mean of vector.
t_real streamtoreal(std::ifstream &stream)
Reading reals from visibility file (including nan's and inf's)
Matrix< t_real > read_ant_positions(const std::string &pos_name)
Read in a text file of antenna positions into a matrix [x, y ,z].
bool has_suffix(const std::string &str, const std::string &suff)
utilities::vis_params conjugate_w(const utilities::vis_params &uv_vis)
reflects visibilities into the w >= 0 domain
utilities::vis_params read_visibility(const std::vector< std::string > &names, const bool w_term)
Read visibility files from name of vector.
utilities::vis_params set_cell_size(const sopt::mpi::Communicator &comm, utilities::vis_params const &uv_vis, const t_real &cell_x, const t_real &cell_y)
utilities::vis_params random_sample_density(const t_int vis_num, const t_real mean, const t_real standard_deviation, const t_real rms_w)
Generates a random visibility coverage.
t_real standard_deviation(const K x)
Calculates the standard deviation of a vector.
utilities::vis_params read_visibility_csv(const std::string &vis_name, const bool w_term)
Reads in visibility csv file.
t_real pixel_to_lambda(const t_real cell, const t_uint imsize, const t_real oversample_ratio)
return factors to convert between arcsecond pixel size image space and lambda for uv space
t_uint size() const
return number of measurements
Vector< t_complex > weights