nifty_gridder.cc 41 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
28
29
30
31
32

using namespace std;

namespace py = pybind11;

namespace {

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

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
35
36
37
38
//
// basic utilities
//

Martin Reinecke's avatar
Martin Reinecke committed
39
40
41
42
//
// Start of real gridder functionality
//

43
44
template<typename T>
  using pyarr = py::array_t<T>;
45
// The "_c" suffix here stands for "C memory order, contiguous"
46
47
template<typename T>
  using pyarr_c = py::array_t<T, py::array::c_style | py::array::forcecast>;
Martin Reinecke's avatar
import  
Martin Reinecke committed
48

Martin Reinecke's avatar
merge    
Martin Reinecke committed
49
template<typename T> pyarr_c<T> makeArray(const vector<size_t> &shape)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
50
51
  { return pyarr_c<T>(shape); }

Martin Reinecke's avatar
merge    
Martin Reinecke committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
void checkArray(const py::array &arr, const char *aname,
  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
70
template<typename T> pyarr<T> provideArray(const py::object &in,
Martin Reinecke's avatar
merge    
Martin Reinecke committed
71
72
  const vector<size_t> &shape)
  {
73
  if (in.is_none())
Martin Reinecke's avatar
merge    
Martin Reinecke committed
74
75
76
77
78
79
80
81
    {
    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_;
    }
82
  auto tmp_ = in.cast<pyarr<T>>();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
83
84
85
86
  checkArray(tmp_, "temporary", shape);
  return tmp_;
  }

Martin Reinecke's avatar
Martin Reinecke committed
87
88
89
90
91
92
93
94
95
96
template<typename T> pyarr<T> providePotentialArray(const py::object &in,
  const vector<size_t> &shape)
  {
  if (in.is_none())
    return makeArray<T>(vector<size_t>(shape.size(), 0));
  auto tmp_ = in.cast<pyarr<T>>();
  checkArray(tmp_, "temporary", shape);
  return tmp_;
  }

Martin Reinecke's avatar
Martin Reinecke committed
97
template<typename T> pyarr_c<T> provideCArray(py::object &in,
Martin Reinecke's avatar
merge    
Martin Reinecke committed
98
99
  const vector<size_t> &shape)
  {
100
  if (in.is_none())
Martin Reinecke's avatar
merge    
Martin Reinecke committed
101
102
103
104
105
106
107
108
109
110
111
112
113
    {
    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_;
    }
  auto tmp_ = in.cast<pyarr_c<T>>();
  checkArray(tmp_, "temporary", shape);
  return tmp_;
  }

Martin Reinecke's avatar
Martin Reinecke committed
114
template<size_t ndim, typename T> cmav<T,ndim> make_cmav(pyarr_c<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
115
  {
Martin Reinecke's avatar
Martin Reinecke committed
116
117
118
119
120
121
  myassert(ndim==in.ndim(), "dimension mismatch");
  array<size_t,ndim> dims;
  for (size_t i=0; i<ndim; ++i) dims[i]=in.shape(i);
  return cmav<T, ndim>(in.mutable_data(),dims);
  }
template<size_t ndim, typename T> smav<T,ndim> make_smav(pyarr<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
122
  {
Martin Reinecke's avatar
Martin Reinecke committed
123
124
125
126
  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
127
    {
Martin Reinecke's avatar
Martin Reinecke committed
128
129
130
    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
131
    }
Martin Reinecke's avatar
Martin Reinecke committed
132
  return smav<T, ndim>(in.mutable_data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
133
  }
Martin Reinecke's avatar
Martin Reinecke committed
134
135
136
137
138
139
140
141
142
143
144
145
template<size_t ndim, typename T> smav<T,ndim> make_smav(pyarr_c<T> &in)
  {
  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");
    }
  return smav<T, ndim>(in.mutable_data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
146
  }
Martin Reinecke's avatar
Martin Reinecke committed
147
template<size_t ndim, typename T> cmav<const T,ndim> make_const_cmav(const pyarr_c<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
148
  {
Martin Reinecke's avatar
Martin Reinecke committed
149
150
151
152
153
154
  myassert(ndim==in.ndim(), "dimension mismatch");
  array<size_t,ndim> dims;
  for (size_t i=0; i<ndim; ++i) dims[i]=in.shape(i);
  return cmav<const T, ndim>(in.data(),dims);
  }
template<size_t ndim, typename T> smav<const T,ndim> make_const_smav(const pyarr<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
155
  {
Martin Reinecke's avatar
Martin Reinecke committed
156
157
158
159
  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
160
    {
Martin Reinecke's avatar
Martin Reinecke committed
161
162
163
    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
164
    }
Martin Reinecke's avatar
Martin Reinecke committed
165
  return smav<const T, ndim>(in.data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
166
  }
Martin Reinecke's avatar
Martin Reinecke committed
167
168
169
170
171
172
173
174
175
176
177
178
template<size_t ndim, typename T> smav<const T,ndim> make_const_smav(const pyarr_c<T> &in)
  {
  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");
    }
  return smav<const T, ndim>(in.data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
179
  }
Martin Reinecke's avatar
Martin Reinecke committed
180
181
template<typename T> pyarr_c<T> complex2hartley
  (const pyarr_c<complex<T>> &grid_, py::object &grid_in)
Martin Reinecke's avatar
Martin Reinecke committed
182
  {
Martin Reinecke's avatar
Martin Reinecke committed
183
184
185
186
187
  auto grid = make_const_smav<2>(grid_);
  size_t nu=grid.shape(0), nv=grid.shape(1);

  auto res = provideCArray<T>(grid_in, {nu, nv});
  auto grid2 = make_smav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
188
189
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
190
  complex2hartley(grid, grid2);
Martin Reinecke's avatar
Martin Reinecke committed
191
  }
Martin Reinecke's avatar
Martin Reinecke committed
192
  return res;
Martin Reinecke's avatar
Martin Reinecke committed
193
  }
Martin Reinecke's avatar
Martin Reinecke committed
194

Martin Reinecke's avatar
Martin Reinecke committed
195
196
template<typename T> pyarr_c<complex<T>> hartley2complex
  (const pyarr_c<T> &grid_)
197
  {
Martin Reinecke's avatar
Martin Reinecke committed
198
199
  auto grid = make_const_smav<2>(grid_);
  size_t nu=grid.shape(0), nv=grid.shape(1);
200

Martin Reinecke's avatar
Martin Reinecke committed
201
202
  auto res = makeArray<complex<T>>({nu, nv});
  auto grid2 = make_smav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
203
  {
Martin Reinecke's avatar
Martin Reinecke committed
204
205
206
  py::gil_scoped_release release;
  hartley2complex(grid, grid2);
  }
Martin Reinecke's avatar
Martin Reinecke committed
207
208
209
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
210
template<typename T> void hartley2_2D(const pyarr_c<T> &in, pyarr_c<T> &out)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
211
  {
Martin Reinecke's avatar
Martin Reinecke committed
212
213
214
215
216
  auto grid = make_const_smav<2>(in);
  auto grid2 = make_smav<2>(out);
  py::gil_scoped_release release;
  hartley2_2D(grid, grid2);
  }
Martin Reinecke's avatar
Martin Reinecke committed
217

Martin Reinecke's avatar
Martin Reinecke committed
218
constexpr auto PyBaselines_DS = R"""(
219
220
221
222
223
224
225
226
227
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
Martin Reinecke committed
228
template<typename T> class PyBaselines: public Baselines<T>
Martin Reinecke's avatar
Martin Reinecke committed
229
  {
Martin Reinecke's avatar
Martin Reinecke committed
230
231
232
233
234
  protected:
    using Baselines<T>::coord;
    using Baselines<T>::f_over_c;
    using Baselines<T>::nrows;
    using Baselines<T>::nchan;
Martin Reinecke's avatar
Martin Reinecke committed
235
236

  public:
Martin Reinecke's avatar
Martin Reinecke committed
237
238
239
240
    using Baselines<T>::Baselines;
    PyBaselines(const pyarr<T> &coord, const pyarr<T> &freq)
      : Baselines<T>(make_const_smav<2>(coord), make_const_smav<1>(freq))
      {}
Martin Reinecke's avatar
Martin Reinecke committed
241

Martin Reinecke's avatar
Martin Reinecke committed
242
243
244
    using Baselines<T>::effectiveCoord;
    using Baselines<T>::Nrows;
    using Baselines<T>::Nchannels;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
245

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    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
    )""";
261

Martin Reinecke's avatar
Martin Reinecke committed
262
263
264
   // using Baselines<T>::effectiveUVW;
    pyarr_c<T> effectiveuvw(const pyarr<uint32_t> &idx_) const
      {
265
      size_t nvis = size_t(idx_.shape(0));
Martin Reinecke's avatar
Martin Reinecke committed
266
      auto idx=make_const_smav<1>(idx_);
267
      auto res_=makeArray<T>({nvis, 3});
Martin Reinecke's avatar
Martin Reinecke committed
268
269
270
271
272
      auto res=make_smav<2>(res_);
      {
      py::gil_scoped_release release;
      Baselines<T>::effectiveUVW(idx,res);
      }
273
      return res_;
Martin Reinecke's avatar
Martin Reinecke committed
274
      }
275

276
    template<typename T2> pyarr_c<T2> ms2vis(const pyarr<T2> &ms_,
Martin Reinecke's avatar
updates    
Martin Reinecke committed
277
278
      const pyarr_c<uint32_t> &idx_) const
      {
Martin Reinecke's avatar
Martin Reinecke committed
279
280
281
      auto idx=make_const_smav<1>(idx_);
      size_t nvis = size_t(idx.shape(0));
      auto ms = make_const_smav<2>(ms_);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
282
      auto res=makeArray<T2>({nvis});
Martin Reinecke's avatar
Martin Reinecke committed
283
      auto vis = make_smav<1>(res);
Martin Reinecke's avatar
Martin Reinecke committed
284
285
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
286
      Baselines<T>::ms2vis(ms, idx, vis);
Martin Reinecke's avatar
Martin Reinecke committed
287
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
288
289
290
      return res;
      }

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    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)
    )""";
308
309
    template<typename T2> pyarr_c<T2> vis2ms(const pyarr<T2> &vis_,
      const pyarr<uint32_t> &idx_, py::object &ms_in) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
310
      {
Martin Reinecke's avatar
Martin Reinecke committed
311
312
      auto vis=make_const_smav<1>(vis_);
      auto idx=make_const_smav<1>(idx_);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
313
      auto res = provideArray<T2>(ms_in, {nrows, nchan});
Martin Reinecke's avatar
Martin Reinecke committed
314
      auto ms = make_smav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
315
316
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
317
      Baselines<T>::vis2ms(vis, idx, ms);
Martin Reinecke's avatar
Martin Reinecke committed
318
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
319
320
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
321
322
  };

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
)""";

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
Martin Reinecke committed
402
template<typename T> class PyGridderConfig: public GridderConfig<T>
Martin Reinecke's avatar
Martin Reinecke committed
403
  {
Martin Reinecke's avatar
Martin Reinecke committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  protected:
    using GridderConfig<T>::nx_dirty;
    using GridderConfig<T>::ny_dirty;
    using GridderConfig<T>::eps;
    using GridderConfig<T>::psx;
    using GridderConfig<T>::psy;
    using GridderConfig<T>::supp;
    using GridderConfig<T>::nsafe;
    using GridderConfig<T>::nu;
    using GridderConfig<T>::nv;
    using GridderConfig<T>::beta;
    using GridderConfig<T>::cfu;
    using GridderConfig<T>::cfv;
    using GridderConfig<T>::wscreen;
Martin Reinecke's avatar
Martin Reinecke committed
418

Martin Reinecke's avatar
Martin Reinecke committed
419
  public:
Martin Reinecke's avatar
Martin Reinecke committed
420
421
    using GridderConfig<T>::GridderConfig;
    PyGridderConfig(size_t nxdirty, size_t nydirty, double epsilon,
Martin Reinecke's avatar
Martin Reinecke committed
422
      double pixsize_x, double pixsize_y)
Martin Reinecke's avatar
Martin Reinecke committed
423
424
425
426
427
428
429
430
431
432
433
      : GridderConfig<T>(nxdirty, nydirty, epsilon, pixsize_x, pixsize_y) {}
    using GridderConfig<T>::Nxdirty;
    using GridderConfig<T>::Nydirty;
    using GridderConfig<T>::Epsilon;
    using GridderConfig<T>::Pixsize_x;
    using GridderConfig<T>::Pixsize_y;
    using GridderConfig<T>::Nu;
    using GridderConfig<T>::Nv;
    using GridderConfig<T>::Supp;
    using GridderConfig<T>::Nsafe;
    using GridderConfig<T>::Beta;
434

Martin Reinecke's avatar
Martin Reinecke committed
435
    pyarr_c<T> grid2dirty(const pyarr_c<T> &grid) const
Martin Reinecke's avatar
Martin Reinecke committed
436
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
437
438
      checkArray(grid, "grid", {nu, nv});
      auto res = makeArray<T>({nx_dirty, ny_dirty});
Martin Reinecke's avatar
Martin Reinecke committed
439
440
      auto grid2=make_const_smav<2>(grid);
      auto res2=make_smav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
441
442
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
443
      GridderConfig<T>::grid2dirty(grid2,res2);
Martin Reinecke's avatar
Martin Reinecke committed
444
      }
Martin Reinecke's avatar
Martin Reinecke committed
445
446
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
    pyarr_c<T> apply_taper(const pyarr_c<T> &img, bool divide) const
      {
      checkArray(img, "img", {nx_dirty, ny_dirty});
      auto pin = img.data();
      auto res = makeArray<T>({nx_dirty, ny_dirty});
      auto pout = res.mutable_data();
      {
      py::gil_scoped_release release;
      if (divide)
        for (size_t i=0; i<nx_dirty; ++i)
          for (size_t j=0; j<ny_dirty; ++j)
            pout[ny_dirty*i + j] = pin[ny_dirty*i + j]/(cfu[i]*cfv[j]);
      else
        for (size_t i=0; i<nx_dirty; ++i)
          for (size_t j=0; j<ny_dirty; ++j)
            pout[ny_dirty*i + j] = pin[ny_dirty*i + j]*cfu[i]*cfv[j];
      }
      return res;
      }
466
467
    pyarr_c<complex<T>> grid2dirty_c(const pyarr_c<complex<T>> &grid) const
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
468
469
      checkArray(grid, "grid", {nu, nv});
      auto tmp = makeArray<complex<T>>({nu, nv});
470
471
472
      auto ptmp = tmp.mutable_data();
      pocketfft::c2c({nu,nv},{grid.strides(0),grid.strides(1)},
        {tmp.strides(0), tmp.strides(1)}, {0,1}, pocketfft::BACKWARD,
473
        grid.data(), tmp.mutable_data(), T(1), nthreads);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
474
      auto res = makeArray<complex<T>>({nx_dirty, ny_dirty});
475
      auto pout = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
476
477
      {
      py::gil_scoped_release release;
478
479
480
481
482
483
484
485
486
      for (size_t i=0; i<nx_dirty; ++i)
        for (size_t j=0; j<ny_dirty; ++j)
          {
          size_t i2 = nu-nx_dirty/2+i;
          if (i2>=nu) i2-=nu;
          size_t j2 = nv-ny_dirty/2+j;
          if (j2>=nv) j2-=nv;
          pout[ny_dirty*i + j] = ptmp[nv*i2+j2]*cfu[i]*cfv[j];
          }
Martin Reinecke's avatar
Martin Reinecke committed
487
      }
488
489
      return res;
      }
490

Martin Reinecke's avatar
Martin Reinecke committed
491
    pyarr_c<T> dirty2grid(const pyarr_c<T> &dirty) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
492
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
493
      checkArray(dirty, "dirty", {nx_dirty, ny_dirty});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
494
      auto pdirty = dirty.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
495
      auto tmp = makeArray<T>({nu, nv});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
496
      auto ptmp = tmp.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
497
498
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
499
500
501
502
503
504
505
506
507
508
509
      for (size_t i=0; i<nu*nv; ++i)
        ptmp[i] = 0.;
      for (size_t i=0; i<nx_dirty; ++i)
        for (size_t j=0; j<ny_dirty; ++j)
          {
          size_t i2 = nu-nx_dirty/2+i;
          if (i2>=nu) i2-=nu;
          size_t j2 = nv-ny_dirty/2+j;
          if (j2>=nv) j2-=nv;
          ptmp[nv*i2+j2] = pdirty[ny_dirty*i + j]*cfu[i]*cfv[j];
          }
Martin Reinecke's avatar
Martin Reinecke committed
510
      }
Martin Reinecke's avatar
Martin Reinecke committed
511
      hartley2_2D<T>(tmp, tmp);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
512
513
      return tmp;
      }
514
515
    pyarr_c<complex<T>> dirty2grid_c(const pyarr_c<complex<T>> &dirty) const
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
516
      checkArray(dirty, "dirty", {nx_dirty, ny_dirty});
517
      auto pdirty = dirty.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
518
      auto tmp = makeArray<complex<T>>({nu, nv});
519
      auto ptmp = tmp.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
520
521
522
      pocketfft::stride_t strides{tmp.strides(0),tmp.strides(1)};
      {
      py::gil_scoped_release release;
523
524
525
526
527
528
529
530
531
532
533
      for (size_t i=0; i<nu*nv; ++i)
        ptmp[i] = 0.;
      for (size_t i=0; i<nx_dirty; ++i)
        for (size_t j=0; j<ny_dirty; ++j)
          {
          size_t i2 = nu-nx_dirty/2+i;
          if (i2>=nu) i2-=nu;
          size_t j2 = nv-ny_dirty/2+j;
          if (j2>=nv) j2-=nv;
          ptmp[nv*i2+j2] = pdirty[ny_dirty*i + j]*cfu[i]*cfv[j];
          }
Martin Reinecke's avatar
Martin Reinecke committed
534
      pocketfft::c2c({nu,nv}, strides, strides, {0,1}, pocketfft::FORWARD,
535
        ptmp, ptmp, T(1), nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
536
      }
537
538
      return tmp;
      }
Martin Reinecke's avatar
test1    
Martin Reinecke committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
    pyarr_c<complex<T>> apply_wscreen(const pyarr_c<complex<T>> &dirty_, double w, bool adjoint) const
      {
      checkArray(dirty_, "dirty", {nx_dirty, ny_dirty});
      auto dirty = dirty_.data();
      auto res_ = makeArray<complex<T>>({nx_dirty, ny_dirty});
      auto res = res_.mutable_data();
      double x0 = -0.5*nx_dirty*psx,
             y0 = -0.5*ny_dirty*psy;
      {
      py::gil_scoped_release release;
#pragma omp parallel num_threads(nthreads)
{
#pragma omp for schedule(static)
      for (size_t i=0; i<=nx_dirty/2; ++i)
        {
        double fx = x0+i*psx;
        fx *= fx;
        for (size_t j=0; j<=ny_dirty/2; ++j)
          {
          double fy = y0+j*psy;
          auto ws = wscreen(fx, fy*fy, w, adjoint);
Martin Reinecke's avatar
Martin Reinecke committed
560
561
          res[ny_dirty*i+j] = dirty[ny_dirty*i+j]*ws; // lower left
          size_t i2 = nx_dirty-i, j2 = ny_dirty-j;
Martin Reinecke's avatar
Martin Reinecke committed
562
563
          if ((i>0)&&(i<i2))
            {
Martin Reinecke's avatar
Martin Reinecke committed
564
            res[ny_dirty*i2+j] = dirty[ny_dirty*i2+j]*ws; // lower right
Martin Reinecke's avatar
Martin Reinecke committed
565
            if ((j>0)&&(j<j2))
Martin Reinecke's avatar
Martin Reinecke committed
566
              res[ny_dirty*i2+j2] = dirty[ny_dirty*i2+j2]*ws; // upper right
Martin Reinecke's avatar
Martin Reinecke committed
567
568
            }
          if ((j>0)&&(j<j2))
Martin Reinecke's avatar
Martin Reinecke committed
569
            res[ny_dirty*i+j2] = dirty[ny_dirty*i+j2]*ws; // upper left
Martin Reinecke's avatar
test1    
Martin Reinecke committed
570
571
          }
        }
572
573
574
575
}
      }
      return res_;
      }
Martin Reinecke's avatar
Martin Reinecke committed
576
577
  };

Martin Reinecke's avatar
import  
Martin Reinecke committed
578

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
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
595
596
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
597
598
599
600
601
602

Returns
=======
np.array((nu,nv), dtype=np.complex128):
    the gridded visibilities
)""";
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
603
template<typename T> pyarr_c<complex<T>> vis2grid_c(
Martin Reinecke's avatar
Martin Reinecke committed
604
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
605
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &vis_,
Martin Reinecke's avatar
Martin Reinecke committed
606
  py::object &grid_in, const py::object &wgt_)
607
  {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
608
609
610
  checkArray(vis_, "vis", {0});
  size_t nvis = size_t(vis_.shape(0));
  checkArray(idx_, "idx", {nvis});
611
612
  auto vis=vis_.template unchecked<1>();
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
613
614
615
  pyarr<T> wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
616

617
  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
Martin Reinecke committed
618
  auto res = provideCArray<complex<T>>(grid_in, {nu, nv});
619
  auto grid = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
620
621
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
622
  T beta = gconf.Beta();
623
  size_t supp = gconf.Supp();
624

625
#pragma omp parallel num_threads(nthreads)
626
{
627
  Helper<T> hlp(gconf, nullptr, grid);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
628
  T emb = exp(-2*beta);
629
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
630
  const T * ku = hlp.kernel.data();
631
  const T * kv = hlp.kernel.data()+supp;
632
633
634
635
636

  // Loop over sampling points
#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
637
    UVW<T> coord = baselines.effectiveCoord(idx(ipart));
Martin Reinecke's avatar
Martin Reinecke committed
638
    hlp.prep(coord.u, coord.v);
Martin Reinecke's avatar
Martin Reinecke committed
639
    auto * ptr = hlp.p0w;
640
    auto v(vis(ipart)*emb);
Martin Reinecke's avatar
Martin Reinecke committed
641
642
    if (have_wgt)
      v*=wgt(ipart);
643
    for (size_t cu=0; cu<supp; ++cu)
644
645
      {
      complex<T> tmp(v*ku[cu]);
646
      for (size_t cv=0; cv<supp; ++cv)
647
        ptr[cv] += tmp*kv[cv];
648
      ptr+=jump;
649
650
651
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
652
  }
653
654
655
  return res;
  }

656
657
658
659
660
661
662
663
664
665
666
667
668
669
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
670
671
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
672
673
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
674
675
676
677
678
679

Returns
=======
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
680
681
template<typename T> pyarr_c<T> vis2grid(const PyBaselines<T> &baselines,
  const PyGridderConfig<T> &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
682
683
  const pyarr<complex<T>> &vis_, py::object &grid_in, const py::object &wgt_)
  { return complex2hartley(vis2grid_c(baselines, gconf, idx_, vis_, None, wgt_), grid_in); }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
684

685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
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
701
702
wgt: np.array((nrows, nchannels), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
703
704
705
706
707
708

Returns
=======
np.array((nu,nv), dtype=np.complex128):
    the gridded visibilities
)""";
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
709
template<typename T> pyarr_c<complex<T>> ms2grid_c(
Martin Reinecke's avatar
Martin Reinecke committed
710
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
711
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &ms_,
Martin Reinecke's avatar
Martin Reinecke committed
712
  py::object &grid_in, const py::object &wgt_)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
713
714
715
716
717
718
  {
  auto nrows = baselines.Nrows();
  auto nchan = baselines.Nchannels();
  checkArray(ms_, "ms", {nrows, nchan});
  checkArray(idx_, "idx", {0});
  size_t nvis = size_t(idx_.shape(0));
719
720
  auto ms = ms_.template unchecked<2>();
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
721
722
723
  auto wgt2 = providePotentialArray<T>(wgt_, {nrows, nchan});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<2>();
Simon Perkins's avatar
Simon Perkins committed
724

Martin Reinecke's avatar
merge    
Martin Reinecke committed
725
  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
Martin Reinecke committed
726
  auto res = provideCArray<complex<T>>(grid_in, {nu, nv});
Martin Reinecke's avatar
merge    
Martin Reinecke committed
727
  auto grid = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
728
729
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
730
  T beta = gconf.Beta();
731
  size_t supp = gconf.Supp();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
732

733
#pragma omp parallel num_threads(nthreads)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
734
{
735
  Helper<T> hlp(gconf, nullptr, grid);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
736
  T emb = exp(-2*beta);
737
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
738
  const T * ku = hlp.kernel.data();
739
  const T * kv = hlp.kernel.data()+supp;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
740
741
742
743
744

  // Loop over sampling points
#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
745
746
747
748
    auto tidx = idx(ipart);
    auto row = tidx/nchan;
    auto chan = tidx-row*nchan;
    UVW<T> coord = baselines.effectiveCoord(tidx);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
749
    hlp.prep(coord.u, coord.v);
Martin Reinecke's avatar
Martin Reinecke committed
750
    auto * ptr = hlp.p0w;
751
    auto v(ms(row,chan)*emb);
Martin Reinecke's avatar
Martin Reinecke committed
752
753
    if (have_wgt)
      v*=wgt(row, chan);
754
    for (size_t cu=0; cu<supp; ++cu)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
755
756
      {
      complex<T> tmp(v*ku[cu]);
757
      for (size_t cv=0; cv<supp; ++cv)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
758
        ptr[cv] += tmp*kv[cv];
759
      ptr+=jump;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
760
761
762
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
763
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
764
765
766
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
767
768
template<typename T> pyarr_c<T> ms2grid(const PyBaselines<T> &baselines,
  const PyGridderConfig<T> &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
769
770
  const pyarr<complex<T>> &ms_, py::object &grid_in, const py::object &wgt_)
  { return complex2hartley(ms2grid_c(baselines, gconf, idx_, ms_, None, wgt_), grid_in); }
771

772
template<typename T> pyarr_c<complex<T>> grid2vis_c(
Martin Reinecke's avatar
Martin Reinecke committed
773
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
774
775
  const pyarr<uint32_t> &idx_, const pyarr_c<complex<T>> &grid_,
  const py::object &wgt_)
776
777
  {
  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
778
  checkArray(idx_, "idx", {0});
779
  auto grid = grid_.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
780
  checkArray(grid_, "grid", {nu, nv});
781
  size_t nvis = size_t(idx_.shape(0));
782
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
783
784
785
  auto wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
786

Martin Reinecke's avatar
merge    
Martin Reinecke committed
787
  auto res = makeArray<complex<T>>({nvis});
788
  auto vis = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
789
790
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
791
  T beta = gconf.Beta();
792
  size_t supp = gconf.Supp();
793
794

  // Loop over sampling points
795
#pragma omp parallel num_threads(nthreads)
796
{
797
  Helper<T> hlp(gconf, grid, nullptr);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
798
  T emb = exp(-2*beta);
799
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
800
  const T * ku = hlp.kernel.data();
801
  const T * kv = hlp.kernel.data()+supp;
802
803
804
805

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
806
    UVW<T> coord = baselines.effectiveCoord(idx(ipart));
Martin Reinecke's avatar
Martin Reinecke committed
807
    hlp.prep(coord.u, coord.v);
808
    complex<T> r = 0;
Martin Reinecke's avatar
Martin Reinecke committed
809
    const auto * ptr = hlp.p0r;
810
    for (size_t cu=0; cu<supp; ++cu)
811
812
      {
      complex<T> tmp(0);
813
      for (size_t cv=0; cv<supp; ++cv)
814
815
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
816
      ptr += jump;
817
      }
Martin Reinecke's avatar
Martin Reinecke committed
818
    if (have_wgt) r*=wgt[ipart];
819
820
821
    vis[ipart] = r*emb;
    }
}
Martin Reinecke's avatar
Martin Reinecke committed
822
  }
823
824
825
  return res;
  }

826
827
828
829
830
831
832
833
834
835
836
837
838
839
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
840
841
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
842
843
844
845
846
847

Returns
=======
np.array((nvis,), dtype=np.complex)
    The degridded visibility data
)""";
Martin Reinecke's avatar
Martin Reinecke committed
848
849
template<typename T> pyarr_c<complex<T>> grid2vis(const PyBaselines<T> &baselines,
  const PyGridderConfig<T> &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
850
851
  const pyarr_c<T> &grid_, const py::object &wgt_)
  { return grid2vis_c(baselines, gconf, idx_, hartley2complex(grid_), wgt_); }
Martin Reinecke's avatar
Martin Reinecke committed
852

Martin Reinecke's avatar
Martin Reinecke committed
853
854
template<typename T> pyarr_c<complex<T>> grid2ms_c(const PyBaselines<T> &baselines,
  const PyGridderConfig<T> &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
855
  const pyarr_c<complex<T>> &grid_, py::object &ms_in, const py::object &wgt_)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
856
  {
857
858
  auto nrows = baselines.Nrows();
  auto nchan = baselines.Nchannels();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
859
860
861
862
863
  size_t nu=gconf.Nu(), nv=gconf.Nv();
  checkArray(idx_, "idx", {0});
  auto grid = grid_.data();
  checkArray(grid_, "grid", {nu, nv});
  size_t nvis = size_t(idx_.shape(0));
864
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
865
866
867
  auto wgt2 = providePotentialArray<T>(wgt_, {nrows, nchan});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<2>();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
868

869
870
  auto res = provideArray<complex<T>>(ms_in, {nrows, nchan});
  auto ms = res.template mutable_unchecked<2>();
Martin Reinecke's avatar
Martin Reinecke committed
871
872
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
873
  T beta = gconf.Beta();
874
  size_t supp = gconf.Supp();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
875
876

  // Loop over sampling points
877
#pragma omp parallel num_threads(nthreads)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
878
{
879
  Helper<T> hlp(gconf, grid, nullptr);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
880
  T emb = exp(-2*beta);
881
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
882
  const T * ku = hlp.kernel.data();
883
  const T * kv = hlp.kernel.data()+supp;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
884
885
886
887

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
888
889
890
891
    auto tidx = idx(ipart);
    auto row = tidx/nchan;
    auto chan = tidx-row*nchan;
    UVW<T> coord = baselines.effectiveCoord(tidx);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
892
893
    hlp.prep(coord.u, coord.v);
    complex<T> r = 0;
Martin Reinecke's avatar
Martin Reinecke committed
894
    const auto * ptr = hlp.p0r;
895
    for (size_t cu=0; cu<supp; ++cu)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
896
897
      {
      complex<T> tmp(0);
898
      for (size_t cv=0; cv<supp; ++cv)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
899
900
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
901
      ptr += jump;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
902
      }
Martin Reinecke's avatar
Martin Reinecke committed
903
904
905
    r*=emb;
    if (have_wgt)
      r*=wgt(row, chan);
906
    ms(row,chan) += r*emb;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
907
908
    }
}
Martin Reinecke's avatar
Martin Reinecke committed
909
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
910
911
912
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
913
914
template<typename T> pyarr_c<complex<T>> grid2ms(const PyBaselines<T> &baselines,
  const PyGridderConfig<T> &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
915
916
  const pyarr_c<T> &grid_, py::object &ms_in, const py::object &wgt_)
  { return grid2ms_c(baselines, gconf, idx_, hartley2complex(grid_), ms_in, wgt_); }
917

918
template<typename T> pyarr_c<complex<T>> apply_holo(
Martin Reinecke's avatar
Martin Reinecke committed
919
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
920
921
  const pyarr<uint32_t> &idx_, const pyarr_c<complex<T>> &grid_,
  const py::object &wgt_)
922
923
924
925
926
927
  {
  size_t nu=gconf.Nu(), nv=gconf.Nv();
  checkArray(idx_, "idx", {0});
  auto grid = grid_.data();
  checkArray(grid_, "grid", {nu, nv});
  size_t nvis = size_t(idx_.shape(0));
928
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
929
930
931
  pyarr<T> wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
932
933
934
935
936

  auto res = makeArray<complex<T>>({nu, nv});
  auto ogrid = res.mutable_data();
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
937
938

  for (size_t i=0; i<nu*nv; ++i) ogrid[i] = T(0);
939
  T beta = gconf.Beta();
940
  size_t supp = gconf.Supp();
941
942

  // Loop over sampling points
943
#pragma omp parallel num_threads(nthreads)
944
945
946
947
{
  Helper<T> hlp(gconf, grid, ogrid);
  T emb = exp(-2*beta);
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
948
  const T * ku = hlp.kernel.data();
949
  const T * kv = hlp.kernel.data()+supp;
950
951
952
953

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
Martin Reinecke's avatar
Martin Reinecke committed
954
955
    auto tidx = idx(ipart);
    UVW<T> coord = baselines.effectiveCoord(tidx);
956
957
    hlp.prep(coord.u, coord.v);
    complex<T> r = 0;
Martin Reinecke's avatar
Martin Reinecke committed
958
    const auto * ptr = hlp.p0r;
959
    for (size_t cu=0; cu<supp; ++cu)
960
961
      {
      complex<T> tmp(0);
962
      for (size_t cv=0; cv<supp; ++cv)
963
964
965
966
967
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
      ptr += jump;
      }
    r*=emb*emb;
Martin Reinecke's avatar
Martin Reinecke committed
968
969
970
971
972
    if (have_wgt)
      {
      auto twgt = wgt(ipart);
      r*=twgt*twgt;
      }
Martin Reinecke's avatar
Martin Reinecke committed
973
    auto * wptr = hlp.p0w;
974
    for (size_t cu=0; cu<supp; ++cu)
975
976
      {
      complex<T> tmp(r*ku[cu]);
977
      for (size_t cv=0; cv<supp; ++cv)
978
        wptr[cv] += tmp*kv[cv];
Martin Reinecke's avatar
Martin Reinecke committed
979
      wptr += jump;
980
981
982
983
984
985
      }
    }
}
  }
  return res;
  }
986

987
template<typename T> pyarr_c<T> get_correlations(
Martin Reinecke's avatar
Martin Reinecke committed
988
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
989
  const pyarr<uint32_t> &idx_, int du, int dv, const py::object &wgt_)
990
991
  {
  size_t nu=gconf.Nu(), nv=gconf.Nv();
992
993
994
  size_t supp = gconf.Supp();
  myassert(size_t(abs(du))<supp, "|du| must be smaller than Supp");
  myassert(size_t(abs(dv))<supp, "|dv| must be smaller than Supp");
995
996
997
  checkArray(idx_, "idx", {0});
  size_t nvis = size_t(idx_.shape(0));
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
998
999
1000
  pyarr<T> wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
1001
1002
1003
1004
1005
1006
1007
1008

  auto res = makeArray<T>({nu, nv});
  auto ogrid = res.mutable_data();
  {
  py::gil_scoped_release release;
  T beta = gconf.Beta();
  for (size_t i=0; i<nu*nv; ++i) ogrid[i] = 0.;

1009
1010
  size_t u0, u1, v0, v1;
  if (du>=0)
1011
    { u0=0; u1=supp-du; }
1012
  else
1013
    { u0=-du; u1=supp; }
1014
  if (dv>=0)
1015
    { v0=0; v1=supp-dv; }
1016
  else
1017
    { v0=-dv; v1=supp; }
1018

1019
1020
1021
1022
1023
1024
  // Loop over sampling points
#pragma omp parallel num_threads(nthreads)
{
  Helper<T,T> hlp(gconf, nullptr, ogrid);
  T emb = exp(-2*beta);
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
1025
  const T * ku = hlp.kernel.data();
1026
  const T * kv = hlp.kernel.data()+supp;
1027
1028
1029
1030
1031
1032

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
    UVW<T> coord = baselines.effectiveCoord(idx(ipart));
    hlp.prep(coord.u, coord.v);