nifty_gridder.cc 30.8 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 *  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
Martin Reinecke's avatar
Martin Reinecke committed
15
 *  along with nifty_gridder; if not, write to the Free Software
Martin Reinecke's avatar
Martin Reinecke committed
16
17
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

Martin Reinecke's avatar
Martin Reinecke committed
19
20
21
/* Copyright (C) 2019 Max-Planck-Society
   Author: Martin Reinecke */

Martin Reinecke's avatar
import  
Martin Reinecke committed
22
23
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
Martin Reinecke's avatar
Martin Reinecke committed
24

Martin Reinecke's avatar
Martin Reinecke committed
25
#include "gridder_cxx.h"
Martin Reinecke's avatar
import  
Martin Reinecke committed
26
27

using namespace std;
Martin Reinecke's avatar
Martin Reinecke committed
28
using namespace gridder;
Martin Reinecke's avatar
import  
Martin Reinecke committed
29
30
31
32
33

namespace py = pybind11;

namespace {

Martin Reinecke's avatar
Martin Reinecke committed
34
35
auto None = py::none();

36
template<typename T>
Martin Reinecke's avatar
updates    
Martin Reinecke committed
37
  using pyarr = py::array_t<T, 0>;
Martin Reinecke's avatar
import  
Martin Reinecke committed
38

39
40
41
42
43
44
45
46
47
48
template<typename T> bool isPytype(const py::array &arr)
  {
  auto t1=arr.dtype();
  auto t2=pybind11::dtype::of<T>();
  auto k1=t1.kind();
  auto k2=t2.kind();
  auto s1=t1.itemsize();
  auto s2=t2.itemsize();
  return (k1==k2)&&(s1==s2);
  }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
49
50
51
52
template<typename T> pyarr<T> getPyarr(const py::array &arr, const string &name)
  {
  auto t1=arr.dtype();
  auto t2=pybind11::dtype::of<T>();
53
54
55
56
57
58
59
  auto k1=t1.kind();
  auto k2=t2.kind();
  auto s1=t1.itemsize();
  auto s2=t2.itemsize();
  myassert((k1==k2)&&(s1==s2),
    "type mismatch for array '", name, "': expected '", k2, s2,
    "', but got '", k1, s1, "'.");
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
60
61
62
  return arr.cast<pyarr<T>>();
  }

Martin Reinecke's avatar
Martin Reinecke committed
63
64
template<typename T> pyarr<T> makeArray(const vector<size_t> &shape)
  { return pyarr<T>(shape); }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
65

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
66
void checkArray(const py::array &arr, const string &aname,
Martin Reinecke's avatar
merge    
Martin Reinecke committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  const vector<size_t> &shape)
  {
  if (size_t(arr.ndim())!=shape.size())
    {
    cerr << "Array '" << aname << "' has " << arr.ndim() << " dimensions; "
            "expected " << shape.size() << endl;
    throw runtime_error("bad dimensionality");
    }
  for (size_t i=0; i<shape.size(); ++i)
    if ((shape[i]!=0) && (size_t(arr.shape(i))!=shape[i]))
      {
      cerr << "Dimension " << i << " of array '" << aname << "' has size "
           << arr.shape(i) << "; expected " << shape[i] << endl;
      throw runtime_error("bad array size");
      }
  }

Martin Reinecke's avatar
Martin Reinecke committed
84
template<typename T> pyarr<T> provideArray(const py::object &in,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
85
  const string &name, const vector<size_t> &shape)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
86
  {
87
  if (in.is_none())
Martin Reinecke's avatar
merge    
Martin Reinecke committed
88
89
90
91
92
93
94
95
    {
    auto tmp_ = makeArray<T>(shape);
    size_t sz = size_t(tmp_.size());
    auto tmp = tmp_.mutable_data();
    for (size_t i=0; i<sz; ++i)
      tmp[i] = T(0);
    return tmp_;
    }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
96
97
  auto tmp_ = getPyarr<T>(in.cast<py::array>(), name);
  checkArray(tmp_, name, shape);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
98
99
100
  return tmp_;
  }

Martin Reinecke's avatar
Martin Reinecke committed
101
template<typename T> pyarr<T> providePotentialArray(const py::object &in,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
102
  const string &name, const vector<size_t> &shape)
Martin Reinecke's avatar
Martin Reinecke committed
103
104
105
  {
  if (in.is_none())
    return makeArray<T>(vector<size_t>(shape.size(), 0));
Martin Reinecke's avatar
Martin Reinecke committed
106
  return getPyarr<T>(in.cast<py::array>(), name);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
107
108
  }

Martin Reinecke's avatar
Martin Reinecke committed
109
template<size_t ndim, typename T> mav<T,ndim> make_mav(pyarr<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
110
  {
Martin Reinecke's avatar
Martin Reinecke committed
111
112
113
114
  myassert(ndim==in.ndim(), "dimension mismatch");
  array<size_t,ndim> dims;
  array<ptrdiff_t,ndim> str;
  for (size_t i=0; i<ndim; ++i)
Martin Reinecke's avatar
Martin Reinecke committed
115
    {
Martin Reinecke's avatar
Martin Reinecke committed
116
117
118
    dims[i]=in.shape(i);
    str[i]=in.strides(i)/sizeof(T);
    myassert(str[i]*ptrdiff_t(sizeof(T))==in.strides(i), "weird strides");
Martin Reinecke's avatar
Martin Reinecke committed
119
    }
Martin Reinecke's avatar
Martin Reinecke committed
120
  return mav<T, ndim>(in.mutable_data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
121
  }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
122
template<size_t ndim, typename T> const_mav<T,ndim> make_const_mav(const pyarr<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
123
124
125
126
127
128
129
130
131
132
  {
  myassert(ndim==in.ndim(), "dimension mismatch");
  array<size_t,ndim> dims;
  array<ptrdiff_t,ndim> str;
  for (size_t i=0; i<ndim; ++i)
    {
    dims[i]=in.shape(i);
    str[i]=in.strides(i)/sizeof(T);
    myassert(str[i]*ptrdiff_t(sizeof(T))==in.strides(i), "weird strides");
    }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
133
  return const_mav<T, ndim>(in.data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
134
  }
Martin Reinecke's avatar
Martin Reinecke committed
135

Martin Reinecke's avatar
Martin Reinecke committed
136
constexpr auto PyBaselines_DS = R"""(
137
138
139
140
141
142
143
144
145
Class storing UVW coordinates and channel information.

Parameters
==========
coord: np.array((nrows, 3), dtype=np.float)
    u, v and w coordinates for each row
freq: np.array((nchannels,), dtype=np.float)
    frequency for each individual channel (in Hz)
)""";
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
146
class PyBaselines: public Baselines
Martin Reinecke's avatar
Martin Reinecke committed
147
148
  {
  public:
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
149
150
151
    using Baselines::Baselines;
    template<typename T> PyBaselines(const pyarr<T> &coord, const pyarr<T> &freq)
      : Baselines(make_const_mav<2>(coord), make_const_mav<1>(freq))
Martin Reinecke's avatar
Martin Reinecke committed
152
      {}
Martin Reinecke's avatar
Martin Reinecke committed
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    static constexpr auto ms2vis_DS = R"""(
    Extracts visibility data from a measurement for the provided indices.

    Parameters
    ==========
    ms: np.array((nrows, nchannels), dtype=np.complex)
        the measurement set's visibility data
    idx: np.array((nvis,), dtype=np.uint32)
        the indices to be extracted

    Returns
    =======
    np.array((nvis,), dtype=np.complex)
        The visibility data for the index array
    )""";
169

Martin Reinecke's avatar
step 1    
Martin Reinecke committed
170
    template<typename T> pyarr<T> effectiveuvw(const pyarr<uint32_t> &idx_) const
Martin Reinecke's avatar
Martin Reinecke committed
171
      {
172
      size_t nvis = size_t(idx_.shape(0));
Martin Reinecke's avatar
Martin Reinecke committed
173
      auto idx=make_const_mav<1>(idx_);
174
      auto res_=makeArray<T>({nvis, 3});
Martin Reinecke's avatar
Martin Reinecke committed
175
      auto res=make_mav<2>(res_);
Martin Reinecke's avatar
Martin Reinecke committed
176
177
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
178
      Baselines::effectiveUVW(idx,res);
Martin Reinecke's avatar
Martin Reinecke committed
179
      }
180
      return res_;
Martin Reinecke's avatar
Martin Reinecke committed
181
      }
182

Martin Reinecke's avatar
step 1    
Martin Reinecke committed
183
    template<typename T> pyarr<T> ms2vis(const pyarr<T> &ms_,
Martin Reinecke's avatar
Martin Reinecke committed
184
      const pyarr<uint32_t> &idx_, size_t nthreads) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
185
      {
Martin Reinecke's avatar
Martin Reinecke committed
186
      auto idx=make_const_mav<1>(idx_);
Martin Reinecke's avatar
Martin Reinecke committed
187
      size_t nvis = size_t(idx.shape(0));
Martin Reinecke's avatar
Martin Reinecke committed
188
      auto ms = make_const_mav<2>(ms_);
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
189
      auto res=makeArray<T>({nvis});
Martin Reinecke's avatar
Martin Reinecke committed
190
      auto vis = make_mav<1>(res);
Martin Reinecke's avatar
Martin Reinecke committed
191
192
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
193
      Baselines::ms2vis(ms, idx, vis, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
194
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
195
196
197
      return res;
      }

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    static constexpr auto vis2ms_DS = R"""(
    Produces a new MS with the provided visibilities set.

    Parameters
    ==========
    vis: np.array((nvis,), dtype=np.complex)
        The visibility data for the index array
    idx: np.array((nvis,), dtype=np.uint32)
        the indices to be inserted
    ms_in: np.array((nrows, nchannels), dtype=np.complex), optional
        input measurement set to which the visibilities are added.

    Returns
    =======
    np.array((nrows, nchannels), dtype=np.complex)
        the measurement set's visibility data (0 where not covered by idx)
    )""";
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
215
    template<typename T> pyarr<T> vis2ms(const pyarr<T> &vis_,
Martin Reinecke's avatar
Martin Reinecke committed
216
      const pyarr<uint32_t> &idx_, py::object &ms_in, size_t nthreads) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
217
      {
Martin Reinecke's avatar
Martin Reinecke committed
218
219
      auto vis=make_const_mav<1>(vis_);
      auto idx=make_const_mav<1>(idx_);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
220
      auto res = provideArray<T>(ms_in, "ms_in", {nrows, nchan});
Martin Reinecke's avatar
Martin Reinecke committed
221
      auto ms = make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
222
223
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
224
      Baselines::vis2ms(vis, idx, ms, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
225
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
226
227
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
228
229
  };

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
constexpr auto grid2dirty_DS = R"""(
Converts from UV grid to dirty image (FFT, cropping, correction)

Parameters
==========
grid: np.array((nu, nv), dtype=np.float64)
    gridded UV data

Returns
=======
nd.array((nxdirty, nydirty), dtype=np.float64)
    the dirty image
)""";

constexpr auto dirty2grid_DS = R"""(
Converts from a dirty image to a UV grid (correction, padding, FFT)

Parameters
==========
dirty: nd.array((nxdirty, nydirty), dtype=np.float64)
    the dirty image

Returns
=======
np.array((nu, nv), dtype=np.float64)
    gridded UV data
)""";

Martin Reinecke's avatar
Martin Reinecke committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
constexpr auto apply_taper_DS = R"""(
Applies the taper (or its inverse) to an image

Parameters
==========
img: nd.array((nxdirty, nydirty), dtype=np.float64)
    the image
divide: bool
    if True, the routine dividex by the taper, otherwise it multiplies by it

Returns
=======
np.array((nxdirty, nydirty), dtype=np.float64)
    the image with the taper applied
)""";

Martin Reinecke's avatar
Martin Reinecke committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
constexpr auto apply_wscreen_DS = R"""(
Applies the w screen to an image

Parameters
==========
dirty: nd.array((nxdirty, nydirty), dtype=np.complex128)
    the image
w : float
    the w value to use
adjoint: bool
    if True, apply the complex conjugate of the w screen

Returns
=======
np.array((nxdirty, nydirty), dtype=np.complex128)
    the image with the w screen applied
)""";

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
constexpr auto GridderConfig_DS = R"""(
Class storing information related to the gridding/degridding process.

Parameters
==========
nxdirty: int
    x resolution of the dirty image; must be even
nydirty: int
    y resolution of the dirty image; must be even
epsilon: float
    required accuracy for the gridding/degridding step
    Must be >= 2e-13.
pixsize_x: float
    Pixel size in x direction (radians)
pixsize_y: float
    Pixel size in y direction (radians)
)""";
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
309
class PyGridderConfig: public GridderConfig
Martin Reinecke's avatar
Martin Reinecke committed
310
311
  {
  public:
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
312
    using GridderConfig::GridderConfig;
Martin Reinecke's avatar
Martin Reinecke committed
313
    PyGridderConfig(size_t nxdirty, size_t nydirty, double epsilon,
Martin Reinecke's avatar
Martin Reinecke committed
314
      double pixsize_x, double pixsize_y, size_t nthreads)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
315
316
317
      : GridderConfig(nxdirty, nydirty, epsilon, pixsize_x, pixsize_y, nthreads) {}

    template<typename T> pyarr<T> apply_taper(const pyarr<T> &img, bool divide) const
Martin Reinecke's avatar
Martin Reinecke committed
318
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
319
      auto res = makeArray<T>({nx_dirty, ny_dirty});
Martin Reinecke's avatar
Martin Reinecke committed
320
321
      auto img2 = make_const_mav<2>(img);
      auto res2 = make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
322
323
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
324
      GridderConfig::apply_taper(img2, res2, divide);
Martin Reinecke's avatar
Martin Reinecke committed
325
      }
Martin Reinecke's avatar
Martin Reinecke committed
326
327
      return res;
      }
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
328
    template<typename T> pyarr<T> grid2dirty(const pyarr<T> &grid) const
329
330
331
332
333
334
      {
      auto res = makeArray<T>({nx_dirty, ny_dirty});
      auto grid2=make_const_mav<2>(grid);
      auto res2=make_mav<2>(res);
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
335
      GridderConfig::grid2dirty(grid2,res2);
336
337
338
      }
      return res;
      }
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
339
    template<typename T> pyarr<complex<T>> grid2dirty_c(const pyarr<complex<T>> &grid) const
Martin Reinecke's avatar
Martin Reinecke committed
340
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
341
      auto res = makeArray<complex<T>>({nx_dirty, ny_dirty});
Martin Reinecke's avatar
Martin Reinecke committed
342
343
      auto grid2=make_const_mav<2>(grid);
      auto res2=make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
344
345
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
346
      GridderConfig::grid2dirty_c(grid2,res2);
Martin Reinecke's avatar
Martin Reinecke committed
347
      }
348
349
      return res;
      }
350

Martin Reinecke's avatar
step 1    
Martin Reinecke committed
351
    template<typename T> pyarr<T> dirty2grid(const pyarr<T> &dirty) const
352
353
354
355
356
357
      {
      auto dirty2 = make_const_mav<2>(dirty);
      auto grid = makeArray<T>({nu, nv});
      auto grid2=make_mav<2>(grid);
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
358
      GridderConfig::dirty2grid(dirty2, grid2);
359
360
361
      }
      return grid;
      }
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
362
    template<typename T> pyarr<complex<T>> dirty2grid_c(const pyarr<complex<T>> &dirty) const
363
      {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
364
365
366
      auto dirty2 = make_const_mav<2>(dirty);
      auto grid = makeArray<complex<T>>({nu, nv});
      auto grid2=make_mav<2>(grid);
Martin Reinecke's avatar
Martin Reinecke committed
367
368
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
369
      GridderConfig::dirty2grid_c(dirty2, grid2);
Martin Reinecke's avatar
Martin Reinecke committed
370
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
371
      return grid;
372
      }
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
373
    template<typename T> pyarr<complex<T>> apply_wscreen(const pyarr<complex<T>> &dirty, double w, bool adjoint) const
Martin Reinecke's avatar
test1    
Martin Reinecke committed
374
      {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
375
376
377
      auto dirty2 = make_const_mav<2>(dirty);
      auto res = makeArray<complex<T>>({nx_dirty, ny_dirty});
      auto res2 = make_mav<2>(res);
Martin Reinecke's avatar
test1    
Martin Reinecke committed
378
379
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
380
      GridderConfig::apply_wscreen(dirty2, res2, w, adjoint);
381
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
382
      return res;
383
      }
Martin Reinecke's avatar
Martin Reinecke committed
384
385
  };

Martin Reinecke's avatar
import  
Martin Reinecke committed
386

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
constexpr auto vis2grid_c_DS = R"""(
Grids visibilities onto a UV grid

Parameters
==========
baselines: Baselines
    the Baselines object
gconf: GridderConf
    the GridderConf object to be used
    (used to optimize the ordering of the indices)
idx: np.array((nvis,), dtype=np.uint32)
    the indices for the entries to be gridded
vis: np.array((nvis,), dtype=np.complex)
    The visibility data for the index array
grid_in: np.array((nu,nv), dtype=np.complex128), optional
    If present, the result is added to this array.
Martin Reinecke's avatar
Martin Reinecke committed
403
404
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
405
406
407
408
409
410

Returns
=======
np.array((nu,nv), dtype=np.complex128):
    the gridded visibilities
)""";
Martin Reinecke's avatar
updates    
Martin Reinecke committed
411
template<typename T> pyarr<complex<T>> Pyvis2grid_c(
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
412
  const PyBaselines &baselines, const PyGridderConfig &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
413
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &vis_,
Martin Reinecke's avatar
Martin Reinecke committed
414
  py::object &grid_in, const py::object &wgt_)
415
  {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
416
417
418
  auto vis2 = make_const_mav<1>(vis_);
  size_t nvis = vis2.shape(0);
  auto idx2 = make_const_mav<1>(idx_);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
419
  pyarr<T> wgt = providePotentialArray<T>(wgt_, "wgt", {nvis});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
420
  auto wgt2 = make_const_mav<1>(wgt);
Martin Reinecke's avatar
Martin Reinecke committed
421
  auto res = provideArray<complex<T>>(grid_in, "grid_in", {gconf.Nu(), gconf.Nv()});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
422
  auto grid = make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
423
424
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
425
  vis2grid_c<T>(baselines, gconf, idx2, vis2, grid, wgt2);
Martin Reinecke's avatar
Martin Reinecke committed
426
  }
427
428
429
  return res;
  }

430
431
432
433
434
435
436
437
438
439
440
441
442
443
constexpr auto vis2grid_DS = R"""(
Grids visibilities onto a UV grid

Parameters
==========
baselines: Baselines
    the Baselines object
gconf: GridderConf
    the GridderConf object to be used
    (used to optimize the ordering of the indices)
idx: np.array((nvis,), dtype=np.uint32)
    the indices for the entries to be gridded
vis: np.array((nvis,), dtype=np.complex)
    The visibility data for the index array
Martin Reinecke's avatar
Martin Reinecke committed
444
445
grid_in: np.array((nu,nv), dtype=np.float64), optional
    If present, the result is added to this array.
Martin Reinecke's avatar
Martin Reinecke committed
446
447
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
448
449
450
451
452
453

Returns
=======
np.array((nu,nv), dtype=np.float64):
    the gridded visibilities (made real by making use of Hermitian symmetry)
)""";
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
454
455
template<typename T> pyarr<T> Pyvis2grid(const PyBaselines &baselines,
  const PyGridderConfig &gconf, const pyarr<uint32_t> &idx_,
456
457
458
  const pyarr<complex<T>> &vis_, py::object &grid_in, const py::object &wgt_)
  {
  auto tmp=Pyvis2grid_c(baselines, gconf, idx_, vis_, None, wgt_);
Martin Reinecke's avatar
Martin Reinecke committed
459
  auto grd=provideArray<T>(grid_in, "grid_in", {gconf.Nu(), gconf.Nv()});
Martin Reinecke's avatar
Martin Reinecke committed
460
461
  {
  py::gil_scoped_release release;
462
  gridder::detail::complex2hartley(make_const_mav<2>(tmp), make_mav<2>(grd), gconf.Nthreads());
Martin Reinecke's avatar
Martin Reinecke committed
463
  }
464
465
  return grd;
  }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
466

467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
constexpr auto ms2grid_c_DS = R"""(
Grids measurement set data onto a UV grid

Parameters
==========
baselines: Baselines
    the Baselines object
gconf: GridderConf
    the GridderConf object to be used
    (used to optimize the ordering of the indices)
idx: np.array((nvis,), dtype=np.uint32)
    the indices for the entries to be gridded
ms: np.array((nrows, nchannels), dtype=np.complex128)
    the measurement set.
grid_in: np.array((nu,nv), dtype=np.complex128), optional
    If present, the result is added to this array.
Martin Reinecke's avatar
Martin Reinecke committed
483
484
wgt: np.array((nrows, nchannels), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
485
486
487
488
489
490

Returns
=======
np.array((nu,nv), dtype=np.complex128):
    the gridded visibilities
)""";
Martin Reinecke's avatar
updates    
Martin Reinecke committed
491
template<typename T> pyarr<complex<T>> Pyms2grid_c(
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
492
  const PyBaselines &baselines, const PyGridderConfig &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
493
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &ms_,
Martin Reinecke's avatar
Martin Reinecke committed
494
  py::object &grid_in, const py::object &wgt_)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
495
496
497
  {
  auto nrows = baselines.Nrows();
  auto nchan = baselines.Nchannels();
Martin Reinecke's avatar
updates    
Martin Reinecke committed
498
499
  auto ms2 = make_const_mav<2>(ms_);
  auto idx2 = make_const_mav<1>(idx_);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
500
  pyarr<T> wgt = providePotentialArray<T>(wgt_, "wgt", {nrows, nchan});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
501
  auto wgt2 = make_const_mav<2>(wgt);
Martin Reinecke's avatar
Martin Reinecke committed
502
  auto res = provideArray<complex<T>>(grid_in, "grid_in", {gconf.Nu(), gconf.Nv()});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
503
  auto grid = make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
504
505
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
506
  ms2grid_c<T>(baselines, gconf, idx2, ms2, grid, wgt2);
Martin Reinecke's avatar
Martin Reinecke committed
507
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
508
509
510
  return res;
  }

511
template<typename T> pyarr<T> Pyms2grid(
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
512
  const PyBaselines &baselines, const PyGridderConfig &gconf,
513
514
515
516
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &ms_,
  py::object &grid_in, const py::object &wgt_)
  {
  auto tmp = Pyms2grid_c(baselines, gconf, idx_, ms_, None, wgt_);
Martin Reinecke's avatar
Martin Reinecke committed
517
  auto res_ = provideArray<T>(grid_in, "grid_in", {gconf.Nu(), gconf.Nv()});
518
  auto res = make_mav<2>(res_);
Martin Reinecke's avatar
Martin Reinecke committed
519
520
  {
  py::gil_scoped_release release;
521
  gridder::detail::complex2hartley(make_const_mav<2>(tmp), res, gconf.Nthreads());
Martin Reinecke's avatar
Martin Reinecke committed
522
  }
523
524
525
  return res_;
  }

Martin Reinecke's avatar
updates    
Martin Reinecke committed
526
template<typename T> pyarr<complex<T>> Pygrid2vis_c(
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
527
  const PyBaselines &baselines, const PyGridderConfig &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
528
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &grid_,
Martin Reinecke's avatar
Martin Reinecke committed
529
  const py::object &wgt_)
530
  {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
531
532
533
  auto grid2 = make_const_mav<2>(grid_);
  auto idx2 = make_const_mav<1>(idx_);
  size_t nvis = idx2.shape(0);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
534
  pyarr<T> wgt = providePotentialArray<T>(wgt_, "wgt", {nvis});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
535
  auto wgt2 = make_const_mav<1>(wgt);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
536
  auto res = makeArray<complex<T>>({nvis});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
537
  auto vis = make_mav<1>(res);
538
  vis.fill(0);
Martin Reinecke's avatar
Martin Reinecke committed
539
540
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
541
  grid2vis_c<T>(baselines, gconf, idx2, grid2, vis, wgt2);
Martin Reinecke's avatar
Martin Reinecke committed
542
  }
543
544
545
  return res;
  }

546
547
548
549
550
551
552
553
554
555
556
557
558
559
constexpr auto grid2vis_DS = R"""(
Degrids visibilities from a UV grid

Parameters
==========
baselines: Baselines
    the Baselines object
gconf: GridderConf
    the GridderConf object to be used
    (used to optimize the ordering of the indices)
idx: np.array((nvis,), dtype=np.uint32)
    the indices for the entries to be degridded
grid: np.array((nu,nv), dtype=np.float64):
    the gridded visibilities (made real by making use of Hermitian symmetry)
Martin Reinecke's avatar
Martin Reinecke committed
560
561
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
562
563
564
565
566
567

Returns
=======
np.array((nvis,), dtype=np.complex)
    The degridded visibility data
)""";
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
568
569
template<typename T> pyarr<complex<T>> Pygrid2vis(const PyBaselines &baselines,
  const PyGridderConfig &gconf, const pyarr<uint32_t> &idx_,
570
571
572
573
574
575
576
  const pyarr<T> &grid_, const py::object &wgt_)
  {
  auto tmp=makeArray<complex<T>>({gconf.Nu(), gconf.Nv()});
  gridder::detail::hartley2complex(make_const_mav<2>(grid_),make_mav<2>(tmp), gconf.Nthreads());
  return Pygrid2vis_c(baselines, gconf, idx_, tmp, wgt_);
  }

Martin Reinecke's avatar
updates    
Martin Reinecke committed
577
template<typename T> pyarr<complex<T>> Pygrid2ms_c(
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
578
  const PyBaselines &baselines, const PyGridderConfig &gconf,
Martin Reinecke's avatar
updates    
Martin Reinecke committed
579
580
581
582
583
584
585
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &grid_,
  py::object &ms_in, const py::object &wgt_)
  {
  auto nrows = baselines.Nrows();
  auto nchan = baselines.Nchannels();
  auto grid2 = make_const_mav<2>(grid_);
  auto idx2 = make_const_mav<1>(idx_);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
586
  pyarr<T> wgt = providePotentialArray<T>(wgt_, "wgt", {nrows, nchan});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
587
  auto wgt2 = make_const_mav<2>(wgt);
Martin Reinecke's avatar
Martin Reinecke committed
588
  auto res = provideArray<complex<T>>(ms_in, "ms_in", {nrows, nchan});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
589
590
591
592
593
594
595
  auto ms = make_mav<2>(res);
  {
  py::gil_scoped_release release;
  grid2ms_c<T>(baselines, gconf, idx2, grid2, ms, wgt2);
  }
  return res;
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
596

Martin Reinecke's avatar
step 1    
Martin Reinecke committed
597
598
template<typename T> pyarr<complex<T>> Pygrid2ms(const PyBaselines &baselines,
  const PyGridderConfig &gconf, const pyarr<uint32_t> &idx_,
599
600
601
602
603
604
605
606
  const pyarr<T> &grid_, py::object &ms_in, const py::object &wgt_)
  {
  auto grid2_ = makeArray<complex<T>>({gconf.Nu(), gconf.Nv()});
  auto grid2 = make_mav<2>(grid2_);
  gridder::detail::hartley2complex(make_const_mav<2>(grid_), grid2, gconf.Nthreads());
  return Pygrid2ms_c(baselines, gconf, idx_, grid2_, ms_in, wgt_);
  }

Martin Reinecke's avatar
Martin Reinecke committed
607
template<typename T> pyarr<complex<T>> apply_holo2(
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
608
  const PyBaselines &baselines, const PyGridderConfig &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
609
  const py::array &idx_, const py::array &grid_, const py::object &wgt_)
610
  {
Martin Reinecke's avatar
Martin Reinecke committed
611
612
613
614
615
616
617
618
  auto idx = getPyarr<uint32_t>(idx_, "idx");
  auto idx2 = make_const_mav<1>(idx);
  auto grid = getPyarr<complex<T>>(grid_, "grid");
  auto grid2 = make_const_mav<2>(grid);
  auto wgt = providePotentialArray<T>(wgt_, "wgt", {idx2.shape(0)});
  auto wgt2 = make_const_mav<1>(wgt);
  auto res = makeArray<complex<T>>({grid2.shape(0),grid2.shape(1)});
  auto res2 = make_mav<2>(res);
619
620
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
621
  apply_holo(baselines, gconf, idx2, grid2, res2, wgt2);
622
623
624
  }
  return res;
  }
Martin Reinecke's avatar
Martin Reinecke committed
625
626
627
628
629
630
631
632
633
634
py::array Pyapply_holo(
  const PyBaselines &baselines, const PyGridderConfig &gconf,
  const py::array &idx, const py::array &grid, const py::object &wgt)
  {
  if (isPytype<complex<float>>(grid))
    return apply_holo2<float>(baselines, gconf, idx, grid, wgt);
  if (isPytype<complex<double>>(grid))
    return apply_holo2<double>(baselines, gconf, idx, grid, wgt);
  myfail("type matching failed: 'grid' has neither type 'c8' nor 'c16'");
  }
635

Martin Reinecke's avatar
Martin Reinecke committed
636
template<typename T> pyarr<T> Pyget_correlations(
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
637
  const PyBaselines &baselines, const PyGridderConfig &gconf,
638
  const pyarr<uint32_t> &idx_, int du, int dv, const py::object &wgt_)
639
  {
Martin Reinecke's avatar
Martin Reinecke committed
640
  auto idx = make_const_mav<1>(idx_);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
641
  pyarr<T> wgt2 = providePotentialArray<T>(wgt_, "wgt", {idx.shape(0)});
Martin Reinecke's avatar
Martin Reinecke committed
642
643
644
645
  auto wgt=make_const_mav<1>(wgt2);

  auto res = makeArray<T>({gconf.Nu(),gconf.Nv()});
  auto ogrid = make_mav<2>(res);
646
647
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
648
  get_correlations(baselines, gconf, idx, du, dv, ogrid, wgt);
649
650
651
652
  }
  return res;
  }

653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
constexpr auto getIndices_DS = R"""(
Selects a subset of entries from a `Baselines` object.

Parameters
==========
baselines: Baselines
    the Baselines object
gconf: GridderConf
    the GridderConf object to be used with the returned indices.
    (used to optimize the ordering of the indices)
flags: np.array((nrows, nchannels), dtype=np.bool)
    "True" indicates that the value should not be used
chbegin: int
    first channel to use (-1: start with the first available channel)
chend: int
    one-past last channel to use (-1: one past the last available channel)
wmin: float
    only select entries with w>=wmin
wmax: float
    only select entries with w<wmax

Returns
=======
np.array((nvis,), dtype=np.uint32)
    the compressed indices for all entries which match the selected criteria
    and are not flagged.
)""";
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
680
pyarr<uint32_t> PygetIndices(const PyBaselines &baselines,
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
681
  const PyGridderConfig &gconf, const pyarr<bool> &flags_, int chbegin,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
682
  int chend, double wmin, double wmax)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
683
  {
684
685
  size_t nidx;
  auto flags = make_const_mav<2>(flags_);
Martin Reinecke's avatar
Martin Reinecke committed
686
687
  {
  py::gil_scoped_release release;
688
  nidx = getIdxSize(baselines, flags, chbegin, chend, wmin, wmax, gconf.Nthreads());
Martin Reinecke's avatar
Martin Reinecke committed
689
  }
690
691
  auto res = makeArray<uint32_t>({nidx});
  auto res2 = make_mav<1>(res);
Martin Reinecke's avatar
Martin Reinecke committed
692
693
  {
  py::gil_scoped_release release;
694
  fillIdx(baselines, gconf, flags, chbegin, chend, wmin, wmax, res2);
Martin Reinecke's avatar
Martin Reinecke committed
695
  }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
696
697
698
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
699
700
701
template<typename T> pyarr<T> vis2dirty2(const PyBaselines &baselines,
  const PyGridderConfig &gconf, const py::array &idx_,
  const py::array &vis_, bool do_wstacking)
Martin Reinecke's avatar
Martin Reinecke committed
702
  {
Martin Reinecke's avatar
Martin Reinecke committed
703
704
705
706
707
708
  auto idx = getPyarr<uint32_t>(idx_, "idx");
  auto idx2 = make_const_mav<1>(idx);
  auto dirty = makeArray<T>({gconf.Nxdirty(), gconf.Nydirty()});
  auto dirty2 = make_mav<2>(dirty);
  auto vis = getPyarr<complex<T>>(vis_, "vis");
  auto vis2 = make_const_mav<1>(vis);
Martin Reinecke's avatar
Martin Reinecke committed
709
710
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
711
  vis2dirty<T>(baselines, gconf, idx2, vis2, dirty2, do_wstacking);
Martin Reinecke's avatar
Martin Reinecke committed
712
  }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
713
  return dirty;
Martin Reinecke's avatar
Martin Reinecke committed
714
  }
Martin Reinecke's avatar
Martin Reinecke committed
715
716
717
718
719
720
721
722
723
724
py::array Pyvis2dirty(const PyBaselines &baselines,
  const PyGridderConfig &gconf, const py::array &idx,
  const py::array &vis, bool do_wstacking)
  {
  if (isPytype<complex<float>>(vis))
    return vis2dirty2<float>(baselines, gconf, idx, vis, do_wstacking);
  if (isPytype<complex<double>>(vis))
    return vis2dirty2<double>(baselines, gconf, idx, vis, do_wstacking);
  myfail("type matching failed: 'vis' has neither type 'c8' nor 'c16'");
  }
Martin Reinecke's avatar
Martin Reinecke committed
725

Martin Reinecke's avatar
Martin Reinecke committed
726
template<typename T> pyarr<complex<T>> dirty2vis2(const PyBaselines &baselines,
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
727
  const PyGridderConfig &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
728
  const pyarr<T> &dirty_, bool do_wstacking)
729
  {
Martin Reinecke's avatar
Martin Reinecke committed
730
731
732
733
734
735
  auto idx = getPyarr<uint32_t>(idx_, "idx");
  auto idx2 = make_const_mav<1>(idx);
  auto dirty = getPyarr<T>(dirty_, "dirty");
  auto dirty2 = make_const_mav<2>(dirty_);
  auto vis = makeArray<complex<T>>({idx2.shape(0)});
  auto vis2 = make_mav<1>(vis);
736
  vis2.fill(0);
737
738
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
739
  vis2.fill(0);
Martin Reinecke's avatar
Martin Reinecke committed
740
  dirty2vis<T>(baselines, gconf, idx2, dirty2, vis2, do_wstacking);
741
742
743
  }
  return vis;
  }
Martin Reinecke's avatar
Martin Reinecke committed
744
745
746
747
748
749
750
751
752
753
py::array Pydirty2vis(const PyBaselines &baselines,
  const PyGridderConfig &gconf, const py::array &idx,
  const py::array &dirty, bool do_wstacking)
  {
  if (isPytype<float>(dirty))
    return dirty2vis2<float>(baselines, gconf, idx, dirty, do_wstacking);
  if (isPytype<double>(dirty))
    return dirty2vis2<double>(baselines, gconf, idx, dirty, do_wstacking);
  myfail("type matching failed: 'dirty' has neither type 'f4' nor 'f8'");
  }
754

Martin Reinecke's avatar
Martin Reinecke committed
755
template<typename T> py::array ms2dirty2(const py::array &uvw_,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
756
  const py::array &freq_, const py::array &ms_, const py::object &wgt_,
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
757
  size_t npix_x, size_t npix_y, double pixsize_x, double pixsize_y, double epsilon,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
758
  bool do_wstacking, size_t nthreads, size_t verbosity)
759
  {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
760
  auto uvw = getPyarr<double>(uvw_, "uvw");
761
  auto uvw2 = make_const_mav<2>(uvw);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
762
  auto freq = getPyarr<double>(freq_, "freq");
763
  auto freq2 = make_const_mav<1>(freq);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
764
  auto ms = getPyarr<complex<T>>(ms_, "ms");
765
  auto ms2 = make_const_mav<2>(ms);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
766
  auto wgt = providePotentialArray<T>(wgt_, "wgt", {ms2.shape(0),ms2.shape(1)});
767
  auto wgt2 = make_const_mav<2>(wgt);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
768
769
  auto dirty = makeArray<T>({npix_x,npix_y});
  auto dirty2 = make_mav<2>(dirty);
Martin Reinecke's avatar
Martin Reinecke committed
770
771
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
772
  ms2dirty(uvw2,freq2,ms2,wgt2,pixsize_x,pixsize_y,epsilon,do_wstacking,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
773
    nthreads,dirty2,verbosity);
Martin Reinecke's avatar
Martin Reinecke committed
774
  }
775
776
777
  return dirty;
  }

Martin Reinecke's avatar
Martin Reinecke committed
778
py::array Pyms2dirty(const py::array &uvw,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
779
  const py::array &freq, const py::array &ms, const py::object &wgt,
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
780
  size_t npix_x, size_t npix_y, double pixsize_x, double pixsize_y, double epsilon,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
781
  bool do_wstacking, size_t nthreads, size_t verbosity)
Martin Reinecke's avatar
Martin Reinecke committed
782
  {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
783
  if (isPytype<complex<float>>(ms))
Martin Reinecke's avatar
Martin Reinecke committed
784
    return ms2dirty2<float>(uvw, freq, ms, wgt, npix_x, npix_y,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
785
786
      pixsize_x, pixsize_y, epsilon, do_wstacking, nthreads, verbosity);
  if (isPytype<complex<double>>(ms))
Martin Reinecke's avatar
Martin Reinecke committed
787
    return ms2dirty2<double>(uvw, freq, ms, wgt, npix_x, npix_y,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
788
789
      pixsize_x, pixsize_y, epsilon, do_wstacking, nthreads, verbosity);
  myfail("type matching failed: 'ms' has neither type 'c8' nor 'c16'");
Martin Reinecke's avatar
Martin Reinecke committed
790
791
  }

Martin Reinecke's avatar
Martin Reinecke committed
792
template<typename T> py::array dirty2ms2(const py::array &uvw_,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
793
794
795
  const py::array &freq_, const py::array &dirty_, const py::object &wgt_,
  double pixsize_x, double pixsize_y, double epsilon,
  bool do_wstacking, size_t nthreads, size_t verbosity)
Martin Reinecke's avatar
Martin Reinecke committed
796
  {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
797
  auto uvw = getPyarr<double>(uvw_, "uvw");
Martin Reinecke's avatar
Martin Reinecke committed
798
  auto uvw2 = make_const_mav<2>(uvw);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
799
  auto freq = getPyarr<double>(freq_, "freq");
Martin Reinecke's avatar
Martin Reinecke committed
800
  auto freq2 = make_const_mav<1>(freq);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
801
  auto dirty = getPyarr<T>(dirty_, "dirty");
Martin Reinecke's avatar
Martin Reinecke committed
802
  auto dirty2 = make_const_mav<2>(dirty);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
803
  auto wgt = providePotentialArray<T>(wgt_, "wgt", {uvw2.shape(0),freq2.shape(0)});
Martin Reinecke's avatar
Martin Reinecke committed
804
  auto wgt2 = make_const_mav<2>(wgt);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
805
806
  auto ms = makeArray<complex<T>>({uvw2.shape(0),freq2.shape(0)});
  auto ms2 = make_mav<2>(ms);
Martin Reinecke's avatar
Martin Reinecke committed
807
808
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
809
  dirty2ms(uvw2,freq2,dirty2,wgt2,pixsize_x,pixsize_y,epsilon,do_wstacking,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
810
    nthreads,ms2,verbosity);
Martin Reinecke's avatar
Martin Reinecke committed
811
  }
Martin Reinecke's avatar
Martin Reinecke committed
812
813
814
  return ms;
  }

Martin Reinecke's avatar
Martin Reinecke committed
815
py::array Pydirty2ms(const py::array &uvw,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
816
817
818
819
820
  const py::array &freq, const py::array &dirty, const py::object &wgt,
  double pixsize_x, double pixsize_y, double epsilon,
  bool do_wstacking, size_t nthreads, size_t verbosity)
  {
  if (isPytype<float>(dirty))
Martin Reinecke's avatar
Martin Reinecke committed
821
    return dirty2ms2<float>(uvw, freq, dirty, wgt,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
822
823
      pixsize_x, pixsize_y, epsilon, do_wstacking, nthreads, verbosity);
  if (isPytype<double>(dirty))
Martin Reinecke's avatar
Martin Reinecke committed
824
    return dirty2ms2<double>(uvw, freq, dirty, wgt,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
825
826
827
828
      pixsize_x, pixsize_y, epsilon, do_wstacking, nthreads, verbosity);
  myfail("type matching failed: 'dirty' has neither type 'f4' nor 'f8'");
  }

Martin Reinecke's avatar
import  
Martin Reinecke committed
829
830
831
832
} // unnamed namespace

PYBIND11_MODULE(nifty_gridder, m)
  {
833
834
  using namespace pybind11::literals;

Martin Reinecke's avatar
step 1    
Martin Reinecke committed
835
  py::class_<PyBaselines> (m, "Baselines", PyBaselines_DS)
Martin Reinecke's avatar
Martin Reinecke committed
836
    .def(py::init<const pyarr<double> &, const pyarr<double> &>(),
837
      "coord"_a, "freq"_a)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
838
839
840
841
842
843
844
845
    .def ("Nrows",&PyBaselines::Nrows)
    .def ("Nchannels",&PyBaselines::Nchannels)
    .def ("ms2vis",&PyBaselines::ms2vis<complex<double>>,
      PyBaselines::ms2vis_DS, "ms"_a, "idx"_a, "nthreads"_a=1)
    .def ("effectiveuvw",&PyBaselines::effectiveuvw<double>, "idx"_a)
    .def ("vis2ms",&PyBaselines::vis2ms<complex<double>>,
      PyBaselines::vis2ms_DS, "vis"_a, "idx"_a, "ms_in"_a=None, "nthreads"_a=1);
  py::class_<PyGridderConfig> (m, "GridderConfig", GridderConfig_DS)
Martin Reinecke's avatar
Martin Reinecke committed
846
847
    .def(py::init<size_t, size_t, double, double, double, size_t>(),"nxdirty"_a,
      "nydirty"_a, "epsilon"_a, "pixsize_x"_a, "pixsize_y"_a, "nthreads"_a=1)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
848
849
850
851
852
853
854
855
856
    .def("Nxdirty", &PyGridderConfig::Nxdirty)
    .def("Nydirty", &PyGridderConfig::Nydirty)
    .def("Epsilon", &PyGridderConfig::Epsilon)
    .def("Pixsize_x", &PyGridderConfig::Pixsize_x)
    .def("Pixsize_y", &PyGridderConfig::Pixsize_y)
    .def("Nu", &PyGridderConfig::Nu)
    .def("Nv", &PyGridderConfig::Nv)
    .def("Supp", &PyGridderConfig::Supp)
    .def("apply_taper", &PyGridderConfig::apply_taper<double>, apply_taper_DS,
Martin Reinecke's avatar
Martin Reinecke committed
857
      "img"_a, "divide"_a=false)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
858
     .def("grid2dirty", &PyGridderConfig::grid2dirty<double>,
859
        grid2dirty_DS, "grid"_a)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
860
861
    .def("grid2dirty_c", &PyGridderConfig::grid2dirty_c<double>, "grid"_a)
    .def("dirty2grid", &PyGridderConfig::dirty2grid<double>,
862
       dirty2grid_DS, "dirty"_a)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
863
864
    .def("dirty2grid_c", &PyGridderConfig::dirty2grid_c<double>, "dirty"_a)
    .def("apply_wscreen", &PyGridderConfig::apply_wscreen<double>,
Martin Reinecke's avatar
Martin Reinecke committed
865
      apply_wscreen_DS, "dirty"_a, "w"_a, "adjoint"_a)
866
867
868
869

    // pickle support
    .def(py::pickle(
        // __getstate__
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
870
        [](const PyGridderConfig & gc) {
871
872
          // Encode object state in tuple
          return py::make_tuple(gc.Nxdirty(), gc.Nydirty(), gc.Epsilon(),
Martin Reinecke's avatar
Martin Reinecke committed
873
                                gc.Pixsize_x(), gc.Pixsize_y(), gc.Nthreads());
874
875
876
        },
        // __setstate__
        [](py::tuple t) {
Martin Reinecke's avatar
Martin Reinecke committed
877
          myassert(t.size()==6,"Invalid state");
878
879

          // Reconstruct from tuple
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
880
881
882
          return PyGridderConfig(t[0].cast<size_t>(), t[1].cast<size_t>(),
                                 t[2].cast<double>(), t[3].cast<double>(),
                                 t[4].cast<double>(), t[5].cast<size_t>());
883
884

        }));
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
885
  m.def("getIndices", PygetIndices, getIndices_DS, "baselines"_a,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
886
    "gconf"_a, "flags"_a, "chbegin"_a=-1, "chend"_a=-1,
887
    "wmin"_a=-1e30, "wmax"_a=1e30);
Martin Reinecke's avatar
Martin Reinecke committed
888
889
  m.def("vis2grid",&Pyvis2grid<double>, vis2grid_DS, "baselines"_a, "gconf"_a,
    "idx"_a, "vis"_a, "grid_in"_a=None, "wgt"_a=None);
890
891
892
893
894
895
  m.def("ms2grid",&Pyms2grid<double>, "baselines"_a, "gconf"_a, "idx"_a, "ms"_a,
    "grid_in"_a=None, "wgt"_a=None);
  m.def("grid2vis",&Pygrid2vis<double>, grid2vis_DS, "baselines"_a, "gconf"_a,
    "idx"_a, "grid"_a, "wgt"_a=None);
  m.def("grid2ms",&Pygrid2ms<double>, "baselines"_a, "gconf"_a, "idx"_a,
    "grid"_a, "ms_in"_a=None, "wgt"_a=None);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
896
  m.def("vis2grid_c",&Pyvis2grid_c<double>, vis2grid_c_DS, "baselines"_a,
Martin Reinecke's avatar
Martin Reinecke committed
897
    "gconf"_a, "idx"_a, "vis"_a, "grid_in"_a=None, "wgt"_a=None);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
898
  m.def("ms2grid_c",&Pyms2grid_c<double>, ms2grid_c_DS, "baselines"_a, "gconf"_a,
Martin Reinecke's avatar
Martin Reinecke committed
899
    "idx"_a, "ms"_a, "grid_in"_a=None, "wgt"_a=None);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
900
  m.def("grid2vis_c",&Pygrid2vis_c<double>, "baselines"_a, "gconf"_a, "idx"_a,
Martin Reinecke's avatar
Martin Reinecke committed
901
    "grid"_a, "wgt"_a=None);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
902
  m.def("grid2ms_c",&Pygrid2ms_c<double>, "baselines"_a, "gconf"_a, "idx"_a,
Martin Reinecke's avatar
Martin Reinecke committed
903
    "grid"_a, "ms_in"_a=None, "wgt"_a=None);
Martin Reinecke's avatar
Martin Reinecke committed
904
  m.def("apply_holo",&Pyapply_holo, "baselines"_a, "gconf"_a, "idx"_a,
Martin Reinecke's avatar
Martin Reinecke committed
905
    "grid"_a, "wgt"_a=None);
Martin Reinecke's avatar
Martin Reinecke committed
906
  m.def("get_correlations", &Pyget_correlations<double>, "baselines"_a, "gconf"_a,
Martin Reinecke's avatar
Martin Reinecke committed
907
    "idx"_a, "du"_a, "dv"_a, "wgt"_a=None);
Martin Reinecke's avatar
Martin Reinecke committed
908
909
910
911
  m.def("vis2dirty",&Pyvis2dirty, "baselines"_a, "gconf"_a,
    "idx"_a, "vis"_a, "do_wstacking"_a=false);
  m.def("dirty2vis",&Pydirty2vis, "baselines"_a, "gconf"_a,
    "idx"_a, "dirty"_a, "do_wstacking"_a=false);
Martin Reinecke's avatar
Martin Reinecke committed
912

Martin Reinecke's avatar
Martin Reinecke committed
913
  m.def("ms2dirty",&Pyms2dirty,"uvw"_a,"freq"_a,"ms"_a,
914
    "wgt"_a=None,"npix_x"_a,"npix_y"_a,"pixsize_x"_a,"pixsize_y"_a,"epsilon"_a,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
915
    "do_wstacking"_a=false, "nthreads"_a=1, "verbosity"_a=0);
Martin Reinecke's avatar
Martin Reinecke committed
916
  m.def("dirty2ms",&Pydirty2ms,"uvw"_a,"freq"_a,"dirty"_a,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
917
    "wgt"_a=None,"pixsize_x"_a,"pixsize_y"_a, "epsilon"_a, "do_wstacking"_a=false, "nthreads"_a=1,
918
    "verbosity"_a=0);
Martin Reinecke's avatar
import  
Martin Reinecke committed
919
  }