| /* | |
| * This file is part of nifty_gridder. | |
| * | |
| * nifty_gridder is free software; you can redistribute it and/or modify | |
| * it under the terms of the GNU General Public License as published by | |
| * the Free Software Foundation; either version 2 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * nifty_gridder is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with nifty_gridder; if not, write to the Free Software | |
| * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
| */ | |
| /* Copyright (C) 2019-2026 Max-Planck-Society | |
| Author: Martin Reinecke */ | |
| // IMPORTANT: always include "pybind_utils.h" first, even before standard headers | |
| #include "ducc0/bindings/pybind_utils.h" | |
| #include "ducc0/wgridder/wgridder.h" | |
| namespace DUCC0_NAMESPACE { | |
| namespace detail_pymodule_wgridder { | |
| using namespace std; | |
| template<typename T> static NpArr Py2_vis2dirty(const CNpArr &uvw_, | |
| const CNpArr &freq_, const CNpArr &vis_, const OptCNpArr &wgt_, const OptCNpArr &mask_, | |
| size_t npix_x, size_t npix_y, double pixsize_x, double pixsize_y, | |
| double epsilon, bool do_wgridding, size_t nthreads, size_t verbosity, | |
| bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, const OptNpArr &dirty_, double sigma_min, | |
| double sigma_max, double center_x, double center_y, bool allow_nshift, | |
| bool double_precision_accumulation) | |
| { | |
| auto uvw = to_cmav<double,2>(uvw_); | |
| auto freq = to_cmav<double,1>(freq_); | |
| auto vis = to_cmav<complex<T>,2>(vis_); | |
| auto wgt = get_OptCNpArr<T>(wgt_, {vis.shape(0),vis.shape(1)}); | |
| auto wgt2 = to_cmav<T,2>(wgt); | |
| auto mask = get_OptCNpArr<uint8_t>(mask_, {uvw.shape(0),freq.shape(0)}); | |
| auto mask2 = to_cmav<uint8_t,2>(mask); | |
| // sizes must be either both zero or both nonzero | |
| MR_assert((npix_x==0)==(npix_y==0), "inconsistent dirty image dimensions"); | |
| auto dirty = (npix_x==0) ? dirty_.value() | |
| : get_OptNpArr<T>(dirty_, {npix_x, npix_y}); | |
| auto dirty2 = to_vmav<T,2>(dirty); | |
| { | |
| py::gil_scoped_release release; | |
| double_precision_accumulation ? | |
| ms2dirty<T,double>(uvw,freq,vis,wgt2,mask2,pixsize_x,pixsize_y,epsilon, | |
| do_wgridding,nthreads,dirty2,verbosity,flip_u,flip_v,flip_w,divide_by_n, sigma_min, | |
| sigma_max, center_x, center_y, allow_nshift) : | |
| ms2dirty<T,T>(uvw,freq,vis,wgt2,mask2,pixsize_x,pixsize_y,epsilon, | |
| do_wgridding,nthreads,dirty2,verbosity,flip_u,flip_v,flip_w,divide_by_n, sigma_min, | |
| sigma_max, center_x, center_y, allow_nshift); | |
| } | |
| return dirty; | |
| } | |
| NpArr Py_vis2dirty(const CNpArr &uvw, | |
| const CNpArr &freq, const CNpArr &vis, const OptCNpArr &wgt, | |
| size_t npix_x, size_t npix_y, double pixsize_x, double pixsize_y, | |
| double epsilon, bool do_wgridding, size_t nthreads, | |
| size_t verbosity, const OptCNpArr &mask, bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, | |
| const OptNpArr &dirty, double sigma_min, double sigma_max, | |
| double center_x, double center_y, bool allow_nshift, | |
| bool double_precision_accumulation) | |
| { | |
| if (isPyarr<complex<float>>(vis)) | |
| return Py2_vis2dirty<float>(uvw, freq, vis, wgt, mask, npix_x, npix_y, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, dirty, sigma_min, sigma_max, center_x, center_y, | |
| allow_nshift, double_precision_accumulation); | |
| if (isPyarr<complex<double>>(vis)) | |
| return Py2_vis2dirty<double>(uvw, freq, vis, wgt, mask, npix_x, npix_y, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, dirty, sigma_min, sigma_max, center_x, center_y, | |
| allow_nshift, double_precision_accumulation); | |
| MR_fail("type matching failed: 'vis' has neither type 'c8' nor 'c16'"); | |
| } | |
| constexpr auto vis2dirty_DS = R"""( | |
| Converts visibilities to a dirty image. | |
| Parameters | |
| ---------- | |
| uvw: numpy.ndarray((nrows, 3), dtype=numpy.float64) | |
| UVW coordinates from the measurement set | |
| freq: numpy.ndarray((nchan,), dtype=numpy.float64) | |
| channel frequencies | |
| vis: numpy.ndarray((nrows, nchan), dtype=numpy.complex64 or numpy.complex128) | |
| the input visibilities. | |
| Its data type determines the precision in which the calculation is carried | |
| out. | |
| wgt: numpy.ndarray((nrows, nchan), float with same precision as `vis`), optional | |
| If present, its values are multiplied to the input before gridding | |
| mask: numpy.ndarray((nrows, nchan), dtype=numpy.uint8), optional | |
| If present, only visibilities are processed for which mask!=0 | |
| npix_x, npix_y: int | |
| dimensions of the dirty image (must both be even and at least 32) | |
| If the `dirty` argument is provided, image dimensions will be inferred from | |
| the passed array; in this case npix_x and npix_y must be either consistent | |
| with these dimensions, or be zero. | |
| pixsize_x, pixsize_y: float | |
| angular pixel size (in projected radians) of the dirty image | |
| center_x, center_y: float | |
| center of the dirty image relative to the phase center | |
| (in projected radians) | |
| epsilon: float | |
| accuracy at which the computation should be done. Must be larger than 2e-13. | |
| If `vis` has type numpy.complex64, it must be larger than 1e-5. | |
| do_wgridding: bool | |
| if True, the full w-gridding algorithm is carried out, otherwise | |
| the w values are assumed to be zero. | |
| flip_[uvw]: bool | |
| if True, all [uvw] coordinates in uvw are multiplied by -1 | |
| divide_by_n: bool | |
| if True, the dirty image pixels are divided by n | |
| sigma_min, sigma_max: float | |
| minimum and maximum allowed oversampling factors | |
| nthreads: int | |
| number of threads to use for the calculation | |
| verbosity: int | |
| 0: no output | |
| 1: some diagnostic output and timings | |
| dirty: numpy.ndarray((npix_x, npix_y), dtype=float of same precision as `vis`), | |
| optional | |
| If provided, the dirty image will be written to this array and a handle | |
| to it will be returned. | |
| double_precision_accumulation: bool | |
| If True, always use double precision for accumulating operations onto the | |
| uv grid. This is necessary to reduce numerical errors in special cases. | |
| Returns | |
| ------- | |
| numpy.ndarray((npix_x, npix_y), dtype=float of same precision as `vis`) | |
| the dirty image | |
| Notes | |
| ----- | |
| The input arrays should be contiguous and in C memory order. | |
| Other strides will work, but can degrade performance significantly. | |
| )"""; | |
| template<typename T> static NpArr Py2_vis2dirty_bda(const CNpArr &uvw_, | |
| const CNpArr &freqlist_id_, const CNpArr &freqlist_nfreqs_, const CNpArr &freqlist_freqs_, | |
| const CNpArr &vis_, const OptCNpArr &wgt_, const OptCNpArr &mask_, | |
| size_t npix_x, size_t npix_y, double pixsize_x, double pixsize_y, | |
| double epsilon, bool do_wgridding, size_t nthreads, size_t verbosity, | |
| bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, const OptNpArr &dirty_, double sigma_min, | |
| double sigma_max, double center_x, double center_y, bool allow_nshift, | |
| bool double_precision_accumulation) | |
| { | |
| auto uvw = to_cmav<double,2>(uvw_); | |
| auto freqlist_id = to_cmav<uint64_t,1>(freqlist_id_); | |
| auto freqlist_nfreqs = to_cmav<uint64_t,1>(freqlist_nfreqs_); | |
| auto freqlist_freqs = to_cmav<double,1>(freqlist_freqs_); | |
| auto vis = to_cmav<complex<T>,1>(vis_); | |
| auto wgt = get_OptCNpArr<T>(wgt_, {vis.shape(0)}); | |
| auto wgt2 = to_cmav<T,1>(wgt); | |
| auto mask = get_OptCNpArr<uint8_t>(mask_, {vis.shape(0)}); | |
| auto mask2 = to_cmav<uint8_t,1>(mask); | |
| // sizes must be either both zero or both nonzero | |
| MR_assert((npix_x==0)==(npix_y==0), "inconsistent dirty image dimensions"); | |
| auto dirty = (npix_x==0) ? dirty_.value() | |
| : get_OptNpArr<T>(dirty_, {npix_x, npix_y}); | |
| auto dirty2 = to_vmav<T,2>(dirty); | |
| { | |
| py::gil_scoped_release release; | |
| double_precision_accumulation ? | |
| ms2dirty_bda<T,double>(uvw,freqlist_id,freqlist_nfreqs,freqlist_freqs,vis,wgt2,mask2,pixsize_x,pixsize_y,epsilon, | |
| do_wgridding,nthreads,dirty2,verbosity,flip_u,flip_v,flip_w,divide_by_n, sigma_min, | |
| sigma_max, center_x, center_y, allow_nshift) : | |
| ms2dirty_bda<T,T>(uvw,freqlist_id,freqlist_nfreqs,freqlist_freqs,vis,wgt2,mask2,pixsize_x,pixsize_y,epsilon, | |
| do_wgridding,nthreads,dirty2,verbosity,flip_u,flip_v,flip_w,divide_by_n, sigma_min, | |
| sigma_max, center_x, center_y, allow_nshift); | |
| } | |
| return dirty; | |
| } | |
| NpArr Py_vis2dirty_bda(const CNpArr &uvw, | |
| const CNpArr &freqlist_id, const CNpArr &freqlist_nfreqs, const CNpArr &freqlist_freqs, | |
| const CNpArr &vis, const OptCNpArr &wgt, | |
| size_t npix_x, size_t npix_y, double pixsize_x, double pixsize_y, | |
| double epsilon, bool do_wgridding, size_t nthreads, | |
| size_t verbosity, const OptCNpArr &mask, bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, | |
| const OptNpArr &dirty, double sigma_min, double sigma_max, | |
| double center_x, double center_y, bool allow_nshift, | |
| bool double_precision_accumulation) | |
| { | |
| if (isPyarr<complex<float>>(vis)) | |
| return Py2_vis2dirty_bda<float>(uvw, freqlist_id, freqlist_nfreqs, freqlist_freqs, vis, wgt, mask, npix_x, npix_y, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, dirty, sigma_min, sigma_max, center_x, center_y, | |
| allow_nshift, double_precision_accumulation); | |
| if (isPyarr<complex<double>>(vis)) | |
| return Py2_vis2dirty_bda<double>(uvw, freqlist_id, freqlist_nfreqs, freqlist_freqs, vis, wgt, mask, npix_x, npix_y, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, dirty, sigma_min, sigma_max, center_x, center_y, | |
| allow_nshift, double_precision_accumulation); | |
| MR_fail("type matching failed: 'vis' has neither type 'c8' nor 'c16'"); | |
| } | |
| constexpr auto vis2dirty_bda_DS = R"""( | |
| Converts visibilities to a dirty image. | |
| Parameters | |
| ---------- | |
| uvw: numpy.ndarray((nrows, 3), dtype=numpy.float64) | |
| UVW coordinates from the measurement set | |
| freqlist_id: numpy.ndarray((nrows,), dtype=numpy.uint64) | |
| ID of the associated frequency list for every row | |
| The IDs must start from 0 and have no gaps | |
| freqlist_nfreqs: numpy.ndarray((nID,), dtype=numpy.uint64) | |
| number of frequencies in the list for each freqlist_ID | |
| freqlist_freqs: numpy.ndarray((np.sum(freqlist_nfreqs),), dtype=numpy.float64) | |
| frequencies for all frequency lists, concatenated in order of ascending ID | |
| vis: numpy.ndarray((nrows, nchan), dtype=numpy.complex64 or numpy.complex128) | |
| the input visibilities. | |
| Its data type determines the precision in which the calculation is carried | |
| out. | |
| wgt: numpy.ndarray((nrows, nchan), float with same precision as `vis`), optional | |
| If present, its values are multiplied to the input before gridding | |
| mask: numpy.ndarray((nrows, nchan), dtype=numpy.uint8), optional | |
| If present, only visibilities are processed for which mask!=0 | |
| npix_x, npix_y: int | |
| dimensions of the dirty image (must both be even and at least 32) | |
| If the `dirty` argument is provided, image dimensions will be inferred from | |
| the passed array; in this case npix_x and npix_y must be either consistent | |
| with these dimensions, or be zero. | |
| pixsize_x, pixsize_y: float | |
| angular pixel size (in projected radians) of the dirty image | |
| center_x, center_y: float | |
| center of the dirty image relative to the phase center | |
| (in projected radians) | |
| epsilon: float | |
| accuracy at which the computation should be done. Must be larger than 2e-13. | |
| If `vis` has type numpy.complex64, it must be larger than 1e-5. | |
| do_wgridding: bool | |
| if True, the full w-gridding algorithm is carried out, otherwise | |
| the w values are assumed to be zero. | |
| flip_[uvw]: bool | |
| if True, all [uvw] coordinates in uvw are multiplied by -1 | |
| divide_by_n: bool | |
| if True, the dirty image pixels are divided by n | |
| sigma_min, sigma_max: float | |
| minimum and maximum allowed oversampling factors | |
| nthreads: int | |
| number of threads to use for the calculation | |
| verbosity: int | |
| 0: no output | |
| 1: some diagnostic output and timings | |
| dirty: numpy.ndarray((npix_x, npix_y), dtype=float of same precision as `vis`), | |
| optional | |
| If provided, the dirty image will be written to this array and a handle | |
| to it will be returned. | |
| double_precision_accumulation: bool | |
| If True, always use double precision for accumulating operations onto the | |
| uv grid. This is necessary to reduce numerical errors in special cases. | |
| Returns | |
| ------- | |
| numpy.ndarray((npix_x, npix_y), dtype=float of same precision as `vis`) | |
| the dirty image | |
| Notes | |
| ----- | |
| The input arrays should be contiguous and in C memory order. | |
| Other strides will work, but can degrade performance significantly. | |
| )"""; | |
| template<typename T> static NpArr Py2_dirty2vis(const CNpArr &uvw_, | |
| const CNpArr &freq_, const CNpArr &dirty_, const OptCNpArr &wgt_, const OptCNpArr &mask_, | |
| double pixsize_x, double pixsize_y, double epsilon, bool do_wgridding, | |
| size_t nthreads, size_t verbosity, bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, | |
| const OptNpArr &vis_, double sigma_min, double sigma_max, double center_x, double center_y, bool allow_nshift) | |
| { | |
| auto uvw = to_cmav<double,2>(uvw_); | |
| auto freq = to_cmav<double,1>(freq_); | |
| auto dirty = to_cmav<T,2>(dirty_); | |
| auto wgt = get_OptCNpArr<T>(wgt_, {uvw.shape(0),freq.shape(0)}); | |
| auto wgt2 = to_cmav<T,2>(wgt); | |
| auto mask = get_OptCNpArr<uint8_t>(mask_, {uvw.shape(0),freq.shape(0)}); | |
| auto mask2 = to_cmav<uint8_t,2>(mask); | |
| auto [vis, vis2] = get_OptNpArr_and_vmav<complex<T>,2>(vis_, {uvw.shape(0),freq.shape(0)}); | |
| { | |
| py::gil_scoped_release release; | |
| dirty2ms<T,T>(uvw,freq,dirty,wgt2,mask2,pixsize_x,pixsize_y,epsilon, | |
| do_wgridding,nthreads,vis2,verbosity,flip_u,flip_v,flip_w,divide_by_n, sigma_min, | |
| sigma_max, center_x, center_y, allow_nshift); | |
| } | |
| return vis; | |
| } | |
| NpArr Py_dirty2vis(const CNpArr &uvw, | |
| const CNpArr &freq, const CNpArr &dirty, const OptCNpArr &wgt, | |
| double pixsize_x, double pixsize_y, double epsilon, bool do_wgridding, | |
| size_t nthreads, size_t verbosity, const OptCNpArr &mask, | |
| bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, const OptNpArr &vis, double sigma_min, | |
| double sigma_max, double center_x, double center_y, bool allow_nshift) | |
| { | |
| if (isPyarr<float>(dirty)) | |
| return Py2_dirty2vis<float>(uvw, freq, dirty, wgt, mask, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, vis, sigma_min, sigma_max, center_x, center_y, allow_nshift); | |
| if (isPyarr<double>(dirty)) | |
| return Py2_dirty2vis<double>(uvw, freq, dirty, wgt, mask, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, vis, sigma_min, sigma_max, center_x, center_y, allow_nshift); | |
| MR_fail("type matching failed: 'dirty' has neither type 'f4' nor 'f8'"); | |
| } | |
| constexpr auto dirty2vis_DS = R"""( | |
| Converts a dirty image to visibilities. | |
| Parameters | |
| ---------- | |
| uvw: numpy.ndarray((nrows, 3), dtype=numpy.float64) | |
| UVW coordinates from the measurement set | |
| freq: numpy.ndarray((nchan,), dtype=numpy.float64) | |
| channel frequencies | |
| dirty: numpy.ndarray((npix_x, npix_y), dtype=numpy.float32 or numpy.float64) | |
| dirty image | |
| Its data type determines the precision in which the calculation is carried | |
| out. | |
| Both dimensions must be even and at least 32. | |
| wgt: numpy.ndarray((nrows, nchan), same dtype as `dirty`), optional | |
| If present, its values are multiplied to the output | |
| mask: numpy.ndarray((nrows, nchan), dtype=numpy.uint8), optional | |
| If present, only visibilities are processed for which mask!=0 | |
| pixsize_x, pixsize_y: float | |
| angular pixel size (in projected radians) of the dirty image | |
| center_x, center_y: float | |
| center of the dirty image relative to the phase center | |
| (in projected radians) | |
| epsilon: float | |
| accuracy at which the computation should be done. Must be larger than 2e-13. | |
| If `dirty` has type numpy.float32, it must be larger than 1e-5. | |
| do_wgridding: bool | |
| if True, the full w-gridding algorithm is carried out, otherwise | |
| the w values are assumed to be zero. | |
| flip_[uvw]: bool | |
| if True, all [uvw] coordinates in uvw are multiplied by -1 | |
| divide_by_n: bool | |
| if True, the dirty image pixels are divided by n | |
| sigma_min, sigma_max: float | |
| minimum and maximum allowed oversampling factors | |
| nthreads: int | |
| number of threads to use for the calculation | |
| verbosity: int | |
| 0: no output | |
| 1: some diagnostic output and timings | |
| vis: numpy.ndarray((nrows, nchan), dtype=complex of same precision as `dirty`), | |
| optional | |
| If provided, the computed visibilities will be stored in this array, and | |
| a handle to it will be returned. | |
| Returns | |
| ------- | |
| numpy.ndarray((nrows, nchan), dtype=complex of same precision as `dirty`) | |
| the computed visibilities. | |
| Notes | |
| ----- | |
| The input arrays should be contiguous and in C memory order. | |
| Other strides will work, but can degrade performance significantly. | |
| )"""; | |
| template<typename T> static NpArr Py2_dirty2vis_bda(const CNpArr &uvw_, | |
| const CNpArr &freqlist_id_, const CNpArr &freqlist_nfreqs_, const CNpArr &freqlist_freqs_, | |
| const CNpArr &dirty_, const OptCNpArr &wgt_, const OptCNpArr &mask_, | |
| double pixsize_x, double pixsize_y, double epsilon, bool do_wgridding, | |
| size_t nthreads, size_t verbosity, bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, | |
| const OptNpArr &vis_, double sigma_min, double sigma_max, double center_x, double center_y, bool allow_nshift) | |
| { | |
| auto uvw = to_cmav<double,2>(uvw_); | |
| auto freqlist_id = to_cmav<uint64_t,1>(freqlist_id_); | |
| auto freqlist_nfreqs = to_cmav<uint64_t,1>(freqlist_nfreqs_); | |
| auto freqlist_freqs = to_cmav<double,1>(freqlist_freqs_); | |
| auto dirty = to_cmav<T,2>(dirty_); | |
| size_t vissize = 0; | |
| for (size_t i=0; i<freqlist_id.shape(0); ++i) | |
| vissize += freqlist_nfreqs(freqlist_id(i)); | |
| auto [vis, vis2] = get_OptNpArr_and_vmav<complex<T>,1>(vis_, {vissize}); | |
| auto wgt = get_OptCNpArr<T>(wgt_, {vis2.shape(0)}); | |
| auto wgt2 = to_cmav<T,1>(wgt); | |
| auto mask = get_OptCNpArr<uint8_t>(mask_, {vis2.shape(0)}); | |
| auto mask2 = to_cmav<uint8_t,1>(mask); | |
| { | |
| py::gil_scoped_release release; | |
| dirty2ms_bda<T,T>(uvw,freqlist_id,freqlist_nfreqs,freqlist_freqs,dirty,wgt2,mask2,pixsize_x,pixsize_y,epsilon, | |
| do_wgridding,nthreads,vis2,verbosity,flip_u,flip_v,flip_w,divide_by_n, sigma_min, | |
| sigma_max, center_x, center_y, allow_nshift); | |
| } | |
| return vis; | |
| } | |
| NpArr Py_dirty2vis_bda(const CNpArr &uvw, | |
| const CNpArr &freqlist_id, const CNpArr &freqlist_nfreqs, const CNpArr &freqlist_freqs, | |
| const CNpArr &dirty, const OptCNpArr &wgt, | |
| double pixsize_x, double pixsize_y, double epsilon, bool do_wgridding, | |
| size_t nthreads, size_t verbosity, const OptCNpArr &mask, | |
| bool flip_u, bool flip_v, bool flip_w, bool divide_by_n, const OptNpArr &vis, double sigma_min, | |
| double sigma_max, double center_x, double center_y, bool allow_nshift) | |
| { | |
| if (isPyarr<float>(dirty)) | |
| return Py2_dirty2vis_bda<float>(uvw, freqlist_id, freqlist_nfreqs, freqlist_freqs, dirty, wgt, mask, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, vis, sigma_min, sigma_max, center_x, center_y, allow_nshift); | |
| if (isPyarr<double>(dirty)) | |
| return Py2_dirty2vis_bda<double>(uvw, freqlist_id, freqlist_nfreqs, freqlist_freqs, dirty, wgt, mask, | |
| pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, | |
| flip_u, flip_v, flip_w, divide_by_n, vis, sigma_min, sigma_max, center_x, center_y, allow_nshift); | |
| MR_fail("type matching failed: 'dirty' has neither type 'f4' nor 'f8'"); | |
| } | |
| constexpr auto dirty2vis_bda_DS = R"""( | |
| Converts a dirty image to visibilities. | |
| Parameters | |
| ---------- | |
| uvw: numpy.ndarray((nrows, 3), dtype=numpy.float64) | |
| UVW coordinates from the measurement set | |
| freqlist_id: numpy.ndarray((nrows,), dtype=numpy.uint64) | |
| ID of the associated frequency list for every row | |
| The IDs must start from 0 and have no gaps | |
| freqlist_nfreqs: numpy.ndarray((nID,), dtype=numpy.uint64) | |
| number of frequencies in the list for each freqlist_ID | |
| freqlist_freqs: numpy.ndarray((np.sum(freqlist_nfreqs),), dtype=numpy.float64) | |
| frequencies for all frequency lists, concatenated in order of ascending ID | |
| dirty: numpy.ndarray((npix_x, npix_y), dtype=numpy.float32 or numpy.float64) | |
| dirty image | |
| Its data type determines the precision in which the calculation is carried | |
| out. | |
| Both dimensions must be even and at least 32. | |
| wgt: numpy.ndarray((nrows, nchan), same dtype as `dirty`), optional | |
| If present, its values are multiplied to the output | |
| mask: numpy.ndarray((nrows, nchan), dtype=numpy.uint8), optional | |
| If present, only visibilities are processed for which mask!=0 | |
| pixsize_x, pixsize_y: float | |
| angular pixel size (in projected radians) of the dirty image | |
| center_x, center_y: float | |
| center of the dirty image relative to the phase center | |
| (in projected radians) | |
| epsilon: float | |
| accuracy at which the computation should be done. Must be larger than 2e-13. | |
| If `dirty` has type numpy.float32, it must be larger than 1e-5. | |
| do_wgridding: bool | |
| if True, the full w-gridding algorithm is carried out, otherwise | |
| the w values are assumed to be zero. | |
| flip_[uvw]: bool | |
| if True, all [uvw] coordinates in uvw are multiplied by -1 | |
| divide_by_n: bool | |
| if True, the dirty image pixels are divided by n | |
| sigma_min, sigma_max: float | |
| minimum and maximum allowed oversampling factors | |
| nthreads: int | |
| number of threads to use for the calculation | |
| verbosity: int | |
| 0: no output | |
| 1: some diagnostic output and timings | |
| vis: numpy.ndarray((nrows, nchan), dtype=complex of same precision as `dirty`), | |
| optional | |
| If provided, the computed visibilities will be stored in this array, and | |
| a handle to it will be returned. | |
| Returns | |
| ------- | |
| numpy.ndarray((nrows, nchan), dtype=complex of same precision as `dirty`) | |
| the computed visibilities. | |
| Notes | |
| ----- | |
| The input arrays should be contiguous and in C memory order. | |
| Other strides will work, but can degrade performance significantly. | |
| )"""; | |
| NpArr Py_ms2dirty(const CNpArr &uvw, | |
| const CNpArr &freq, const CNpArr &ms, const OptCNpArr &wgt, | |
| size_t npix_x, size_t npix_y, double pixsize_x, double pixsize_y, size_t /*nu*/, | |
| size_t /*nv*/, double epsilon, bool do_wgridding, size_t nthreads, | |
| size_t verbosity, const OptCNpArr &mask, | |
| bool double_precision_accumulation) | |
| { | |
| return Py_vis2dirty(uvw, freq, ms, wgt, npix_x, npix_y, pixsize_x, pixsize_y, | |
| epsilon, do_wgridding, nthreads, verbosity, mask, false, false, false, true, OptNpArr(), 1.1, | |
| 2.6, 0., 0., true, double_precision_accumulation); | |
| } | |
| constexpr auto ms2dirty_DS = R"""( | |
| Converts an MS object to dirty image. | |
| Parameters | |
| ---------- | |
| uvw: numpy.ndarray((nrows, 3), dtype=numpy.float64) | |
| UVW coordinates from the measurement set | |
| freq: numpy.ndarray((nchan,), dtype=numpy.float64) | |
| channel frequencies | |
| ms: numpy.ndarray((nrows, nchan), dtype=numpy.complex64 or numpy.complex128) | |
| the input measurement set data. | |
| Its data type determines the precision in which the calculation is carried | |
| out. | |
| wgt: numpy.ndarray((nrows, nchan), float with same precision as `ms`), optional | |
| If present, its values are multiplied to the input before gridding | |
| npix_x, npix_y: int | |
| dimensions of the dirty image (must both be even and at least 32) | |
| pixsize_x, pixsize_y: float | |
| angular pixel size (in projected radians) of the dirty image | |
| nu, nv: int | |
| obsolete, ignored | |
| epsilon: float | |
| accuracy at which the computation should be done. Must be larger than 2e-13. | |
| If `ms` has type numpy.complex64, it must be larger than 1e-5. | |
| do_wstacking: bool | |
| if True, the full w-gridding algorithm is carried out, otherwise | |
| the w values are assumed to be zero. | |
| nthreads: int | |
| number of threads to use for the calculation | |
| verbosity: int | |
| 0: no output | |
| 1: some output | |
| mask: numpy.ndarray((nrows, nchan), dtype=numpy.uint8), optional | |
| If present, only visibilities are processed for which mask!=0 | |
| double_precision_accumulation: bool | |
| If True, always use double precision for accumulating operations onto the | |
| uv grid. This is necessary to reduce numerical errors in special cases. | |
| Returns | |
| ------- | |
| numpy.ndarray((npix_x, npix_y), dtype=float of same precision as `ms`) | |
| the dirty image | |
| Notes | |
| ----- | |
| This function is obsolescent, please switch to vis2dirty! | |
| The input arrays should be contiguous and in C memory order. | |
| Other strides will work, but can degrade performance significantly. | |
| )"""; | |
| NpArr Py_dirty2ms(const CNpArr &uvw, | |
| const CNpArr &freq, const CNpArr &dirty, const OptCNpArr &wgt, | |
| double pixsize_x, double pixsize_y, size_t /*nu*/, size_t /*nv*/, double epsilon, | |
| bool do_wgridding, size_t nthreads, size_t verbosity, const OptCNpArr &mask) | |
| { | |
| return Py_dirty2vis(uvw, freq, dirty, wgt, pixsize_x, pixsize_y, epsilon, do_wgridding, nthreads, verbosity, mask, false, false, false, true, OptNpArr(), 1.1, 2.6, 0, 0, true); | |
| } | |
| constexpr auto dirty2ms_DS = R"""( | |
| Converts a dirty image to an MS object. | |
| Parameters | |
| ---------- | |
| uvw: numpy.ndarray((nrows, 3), dtype=numpy.float64) | |
| UVW coordinates from the measurement set | |
| freq: numpy.ndarray((nchan,), dtype=numpy.float64) | |
| channel frequencies | |
| dirty: numpy.ndarray((npix_x, npix_y), dtype=numpy.float32 or numpy.float64) | |
| dirty image | |
| Its data type determines the precision in which the calculation is carried | |
| out. | |
| Both dimensions must be even and at least 32. | |
| wgt: numpy.ndarray((nrows, nchan), same dtype as `dirty`), optional | |
| If present, its values are multiplied to the output | |
| pixsize_x, pixsize_y: float | |
| angular pixel size (in projected radians) of the dirty image | |
| nu, nv: int | |
| obsolete, ignored | |
| epsilon: float | |
| accuracy at which the computation should be done. Must be larger than 2e-13. | |
| If `dirty` has type numpy.float32, it must be larger than 1e-5. | |
| do_wstacking: bool | |
| if True, the full w-gridding algorithm is carried out, otherwise | |
| the w values are assumed to be zero. | |
| nthreads: int | |
| number of threads to use for the calculation | |
| verbosity: int | |
| 0: no output | |
| 1: some output | |
| mask: numpy.ndarray((nrows, nchan), dtype=numpy.uint8), optional | |
| If present, only visibilities are processed for which mask!=0 | |
| Returns | |
| ------- | |
| numpy.ndarray((nrows, nchan), dtype=complex of same precision as `dirty`) | |
| the measurement set data. | |
| Notes | |
| ----- | |
| This function is obsolescent, please switch to dirty2vis! | |
| The input arrays should be contiguous and in C memory order. | |
| Other strides will work, but can degrade performance significantly. | |
| )"""; | |
| constexpr const char *wgridder_experimental_DS = R"""( | |
| Experimental, more powerful interface to the gridding code | |
| Notes | |
| ----- | |
| The functionality in this module is not considered to have a stable interface | |
| and also may be moved to other modules in the future. If you use it, be prepared | |
| to adjust your code at some point ion the future! | |
| )"""; | |
| void add_pythonfuncs(py::module_ &m) | |
| { | |
| using namespace py::literals; | |
| auto None = py::none(); | |
| m.def("vis2dirty", &Py_vis2dirty, vis2dirty_DS, py::kw_only(), "uvw"_a, "freq"_a, "vis"_a, | |
| "wgt"_a=None, "npix_x"_a=0, "npix_y"_a=0, "pixsize_x"_a, "pixsize_y"_a, | |
| "epsilon"_a, "do_wgridding"_a=false, "nthreads"_a=1, "verbosity"_a=0, | |
| "mask"_a=None, "flip_u"_a=false, "flip_v"_a=false, "flip_w"_a=false, "divide_by_n"_a=true, "dirty"_a=None, | |
| "sigma_min"_a=1.1, "sigma_max"_a=2.6, "center_x"_a=0., "center_y"_a=0., | |
| "allow_nshift"_a=true, "double_precision_accumulation"_a=false); | |
| m.def("dirty2vis", &Py_dirty2vis, dirty2vis_DS, py::kw_only(), "uvw"_a, "freq"_a, "dirty"_a, | |
| "wgt"_a=None, "pixsize_x"_a, "pixsize_y"_a, "epsilon"_a, | |
| "do_wgridding"_a=false, "nthreads"_a=1, "verbosity"_a=0, "mask"_a=None, | |
| "flip_u"_a=false, "flip_v"_a=false, "flip_w"_a=false, "divide_by_n"_a=true, "vis"_a=None, "sigma_min"_a=1.1, | |
| "sigma_max"_a=2.6, "center_x"_a=0., "center_y"_a=0., "allow_nshift"_a=true); | |
| } | |
| void add_wgridder(py::module_ &msup) | |
| { | |
| using namespace py::literals; | |
| auto None = py::none(); | |
| auto m = msup.def_submodule("wgridder"); | |
| auto m2 = m.def_submodule("experimental", wgridder_experimental_DS); | |
| add_pythonfuncs(m); | |
| add_pythonfuncs(m2); | |
| m.def("ms2dirty", &Py_ms2dirty, ms2dirty_DS, "uvw"_a, "freq"_a, "ms"_a, | |
| "wgt"_a=None, "npix_x"_a, "npix_y"_a, "pixsize_x"_a, "pixsize_y"_a, "nu"_a=0, "nv"_a=0, | |
| "epsilon"_a, "do_wstacking"_a=false, "nthreads"_a=1, "verbosity"_a=0, "mask"_a=None, | |
| "double_precision_accumulation"_a=false); | |
| m.def("dirty2ms", &Py_dirty2ms, dirty2ms_DS, "uvw"_a, "freq"_a, "dirty"_a, | |
| "wgt"_a=None, "pixsize_x"_a, "pixsize_y"_a, "nu"_a=0, "nv"_a=0, "epsilon"_a, | |
| "do_wstacking"_a=false, "nthreads"_a=1, "verbosity"_a=0, "mask"_a=None); | |
| m2.def("vis2dirty_bda", &Py_vis2dirty_bda, vis2dirty_bda_DS, py::kw_only(), "uvw"_a, | |
| "freqlist_id"_a, "freqlist_nfreqs"_a, "freqlist_freqs"_a, "vis"_a, | |
| "wgt"_a=None, "npix_x"_a=0, "npix_y"_a=0, "pixsize_x"_a, "pixsize_y"_a, | |
| "epsilon"_a, "do_wgridding"_a=false, "nthreads"_a=1, "verbosity"_a=0, | |
| "mask"_a=None, "flip_u"_a=false, "flip_v"_a=false, "flip_w"_a=false, "divide_by_n"_a=true, "dirty"_a=None, | |
| "sigma_min"_a=1.1, "sigma_max"_a=2.6, "center_x"_a=0., "center_y"_a=0., | |
| "allow_nshift"_a=true, "double_precision_accumulation"_a=false); | |
| m2.def("dirty2vis_bda", &Py_dirty2vis_bda, dirty2vis_bda_DS, py::kw_only(), "uvw"_a, | |
| "freqlist_id"_a, "freqlist_nfreqs"_a, "freqlist_freqs"_a, "dirty"_a, | |
| "wgt"_a=None, "pixsize_x"_a, "pixsize_y"_a, "epsilon"_a, | |
| "do_wgridding"_a=false, "nthreads"_a=1, "verbosity"_a=0, "mask"_a=None, | |
| "flip_u"_a=false, "flip_v"_a=false, "flip_w"_a=false, "divide_by_n"_a=true, "vis"_a=None, "sigma_min"_a=1.1, | |
| "sigma_max"_a=2.6, "center_x"_a=0., "center_y"_a=0., "allow_nshift"_a=true); | |
| } | |
| }} |