nifty_gridder.cc 34.6 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();

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

Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
//
// Start of real gridder functionality
//
Martin Reinecke's avatar
Martin Reinecke committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
constexpr auto set_nthreads_DS = R"""(
Specifies the number of threads to be used by the module

Parameters
==========
nthreads: int
    the number of threads to be used. Must be >=1.
)""";
constexpr auto get_nthreads_DS = R"""(
Returns the number of threads used by the module

Returns
=======
int : the number of threads used by the module
)""";
Martin Reinecke's avatar
Martin Reinecke committed
58

59
60
template<typename T>
  using pyarr = py::array_t<T>;
Martin Reinecke's avatar
import  
Martin Reinecke committed
61

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

Martin Reinecke's avatar
merge    
Martin Reinecke committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
83
template<typename T> pyarr<T> provideArray(const py::object &in,
Martin Reinecke's avatar
merge    
Martin Reinecke committed
84
85
  const vector<size_t> &shape)
  {
86
  if (in.is_none())
Martin Reinecke's avatar
merge    
Martin Reinecke committed
87
88
89
90
91
92
93
94
    {
    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_;
    }
95
  auto tmp_ = in.cast<pyarr<T>>();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
96
97
98
99
  checkArray(tmp_, "temporary", shape);
  return tmp_;
  }

Martin Reinecke's avatar
Martin Reinecke committed
100
101
102
103
104
105
106
107
108
109
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
110
template<typename T> pyarr<T> provideCArray(py::object &in,
Martin Reinecke's avatar
merge    
Martin Reinecke committed
111
112
  const vector<size_t> &shape)
  {
113
  if (in.is_none())
Martin Reinecke's avatar
merge    
Martin Reinecke committed
114
115
116
117
118
119
120
121
    {
    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
Martin Reinecke committed
122
  auto tmp_ = in.cast<pyarr<T>>();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
123
124
125
126
  checkArray(tmp_, "temporary", shape);
  return tmp_;
  }

Martin Reinecke's avatar
Martin Reinecke committed
127
template<size_t ndim, typename T> mav<T,ndim> make_mav(pyarr<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
128
  {
Martin Reinecke's avatar
Martin Reinecke committed
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)
Martin Reinecke's avatar
Martin Reinecke committed
133
    {
Martin Reinecke's avatar
Martin Reinecke committed
134
135
136
    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
137
    }
Martin Reinecke's avatar
Martin Reinecke committed
138
  return mav<T, ndim>(in.mutable_data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
139
  }
Martin Reinecke's avatar
Martin Reinecke committed
140
template<size_t ndim, typename T> mav<const T,ndim> make_const_mav(const pyarr<T> &in)
Martin Reinecke's avatar
Martin Reinecke committed
141
142
143
144
145
146
147
148
149
150
  {
  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
Martin Reinecke committed
151
  return mav<const T, ndim>(in.data(),dims,str);
Martin Reinecke's avatar
Martin Reinecke committed
152
  }
Martin Reinecke's avatar
Martin Reinecke committed
153

Martin Reinecke's avatar
Martin Reinecke committed
154
constexpr auto PyBaselines_DS = R"""(
155
156
157
158
159
160
161
162
163
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
164
template<typename T> class PyBaselines: public Baselines<T>
Martin Reinecke's avatar
Martin Reinecke committed
165
  {
Martin Reinecke's avatar
Martin Reinecke committed
166
167
168
169
170
  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
171
172

  public:
Martin Reinecke's avatar
Martin Reinecke committed
173
174
    using Baselines<T>::Baselines;
    PyBaselines(const pyarr<T> &coord, const pyarr<T> &freq)
Martin Reinecke's avatar
Martin Reinecke committed
175
      : Baselines<T>(make_const_mav<2>(coord), make_const_mav<1>(freq))
Martin Reinecke's avatar
Martin Reinecke committed
176
      {}
Martin Reinecke's avatar
Martin Reinecke committed
177

Martin Reinecke's avatar
Martin Reinecke committed
178
179
180
    using Baselines<T>::effectiveCoord;
    using Baselines<T>::Nrows;
    using Baselines<T>::Nchannels;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
181

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    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
    )""";
197

Martin Reinecke's avatar
Martin Reinecke committed
198
   // using Baselines<T>::effectiveUVW;
Martin Reinecke's avatar
Martin Reinecke committed
199
    pyarr<T> effectiveuvw(const pyarr<uint32_t> &idx_) const
Martin Reinecke's avatar
Martin Reinecke committed
200
      {
201
      size_t nvis = size_t(idx_.shape(0));
Martin Reinecke's avatar
Martin Reinecke committed
202
      auto idx=make_const_mav<1>(idx_);
203
      auto res_=makeArray<T>({nvis, 3});
Martin Reinecke's avatar
Martin Reinecke committed
204
      auto res=make_mav<2>(res_);
Martin Reinecke's avatar
Martin Reinecke committed
205
206
207
208
      {
      py::gil_scoped_release release;
      Baselines<T>::effectiveUVW(idx,res);
      }
209
      return res_;
Martin Reinecke's avatar
Martin Reinecke committed
210
      }
211

Martin Reinecke's avatar
Martin Reinecke committed
212
213
    template<typename T2> pyarr<T2> ms2vis(const pyarr<T2> &ms_,
      const pyarr<uint32_t> &idx_) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
214
      {
Martin Reinecke's avatar
Martin Reinecke committed
215
      auto idx=make_const_mav<1>(idx_);
Martin Reinecke's avatar
Martin Reinecke committed
216
      size_t nvis = size_t(idx.shape(0));
Martin Reinecke's avatar
Martin Reinecke committed
217
      auto ms = make_const_mav<2>(ms_);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
218
      auto res=makeArray<T2>({nvis});
Martin Reinecke's avatar
Martin Reinecke committed
219
      auto vis = make_mav<1>(res);
Martin Reinecke's avatar
Martin Reinecke committed
220
221
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
222
      Baselines<T>::ms2vis(ms, idx, vis);
Martin Reinecke's avatar
Martin Reinecke committed
223
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
224
225
226
      return res;
      }

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    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
Martin Reinecke committed
244
    template<typename T2> pyarr<T2> vis2ms(const pyarr<T2> &vis_,
245
      const pyarr<uint32_t> &idx_, py::object &ms_in) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
246
      {
Martin Reinecke's avatar
Martin Reinecke committed
247
248
      auto vis=make_const_mav<1>(vis_);
      auto idx=make_const_mav<1>(idx_);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
249
      auto res = provideArray<T2>(ms_in, {nrows, nchan});
Martin Reinecke's avatar
Martin Reinecke committed
250
      auto ms = make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
251
252
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
253
      Baselines<T>::vis2ms(vis, idx, ms);
Martin Reinecke's avatar
Martin Reinecke committed
254
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
255
256
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
257
258
  };

Martin Reinecke's avatar
Martin Reinecke committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
)""";

293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
310
template<typename T> class PyGridderConfig: public GridderConfig<T>
Martin Reinecke's avatar
Martin Reinecke committed
311
  {
Martin Reinecke's avatar
Martin Reinecke committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  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
326

Martin Reinecke's avatar
Martin Reinecke committed
327
  public:
Martin Reinecke's avatar
Martin Reinecke committed
328
329
    using GridderConfig<T>::GridderConfig;
    PyGridderConfig(size_t nxdirty, size_t nydirty, double epsilon,
Martin Reinecke's avatar
Martin Reinecke committed
330
      double pixsize_x, double pixsize_y)
Martin Reinecke's avatar
Martin Reinecke committed
331
332
333
334
335
336
337
338
339
340
341
      : 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;
342

Martin Reinecke's avatar
Martin Reinecke committed
343
    pyarr<T> apply_taper(const pyarr<T> &img, bool divide) const
Martin Reinecke's avatar
Martin Reinecke committed
344
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
345
      auto res = makeArray<T>({nx_dirty, ny_dirty});
Martin Reinecke's avatar
Martin Reinecke committed
346
347
      auto img2 = make_const_mav<2>(img);
      auto res2 = make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
348
349
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
350
      GridderConfig<T>::apply_taper(img2, res2, divide);
Martin Reinecke's avatar
Martin Reinecke committed
351
      }
Martin Reinecke's avatar
Martin Reinecke committed
352
353
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
354
    pyarr<complex<T>> grid2dirty_c(const pyarr<complex<T>> &grid) const
Martin Reinecke's avatar
Martin Reinecke committed
355
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
356
      auto res = makeArray<complex<T>>({nx_dirty, ny_dirty});
Martin Reinecke's avatar
Martin Reinecke committed
357
358
      auto grid2=make_const_mav<2>(grid);
      auto res2=make_mav<2>(res);
Martin Reinecke's avatar
Martin Reinecke committed
359
360
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
361
      GridderConfig<T>::grid2dirty_c(grid2,res2);
Martin Reinecke's avatar
Martin Reinecke committed
362
      }
363
364
      return res;
      }
365

Martin Reinecke's avatar
Martin Reinecke committed
366
    pyarr<complex<T>> dirty2grid_c(const pyarr<complex<T>> &dirty) const
367
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
368
      checkArray(dirty, "dirty", {nx_dirty, ny_dirty});
369
      auto pdirty = dirty.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
370
      auto tmp = makeArray<complex<T>>({nu, nv});
371
      auto ptmp = tmp.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
372
373
374
      pocketfft::stride_t strides{tmp.strides(0),tmp.strides(1)};
      {
      py::gil_scoped_release release;
375
376
377
378
379
380
381
382
383
384
385
      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
386
      pocketfft::c2c({nu,nv}, strides, strides, {0,1}, pocketfft::FORWARD,
387
        ptmp, ptmp, T(1), nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
388
      }
389
390
      return tmp;
      }
Martin Reinecke's avatar
Martin Reinecke committed
391
    pyarr<complex<T>> apply_wscreen(const pyarr<complex<T>> &dirty_, double w, bool adjoint) const
Martin Reinecke's avatar
test1    
Martin Reinecke committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
      {
      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
412
413
          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
414
415
          if ((i>0)&&(i<i2))
            {
Martin Reinecke's avatar
Martin Reinecke committed
416
            res[ny_dirty*i2+j] = dirty[ny_dirty*i2+j]*ws; // lower right
Martin Reinecke's avatar
Martin Reinecke committed
417
            if ((j>0)&&(j<j2))
Martin Reinecke's avatar
Martin Reinecke committed
418
              res[ny_dirty*i2+j2] = dirty[ny_dirty*i2+j2]*ws; // upper right
Martin Reinecke's avatar
Martin Reinecke committed
419
420
            }
          if ((j>0)&&(j<j2))
Martin Reinecke's avatar
Martin Reinecke committed
421
            res[ny_dirty*i+j2] = dirty[ny_dirty*i+j2]*ws; // upper left
Martin Reinecke's avatar
test1    
Martin Reinecke committed
422
423
          }
        }
424
425
426
427
}
      }
      return res_;
      }
Martin Reinecke's avatar
Martin Reinecke committed
428
429
  };

Martin Reinecke's avatar
import  
Martin Reinecke committed
430

431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
447
448
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
449
450
451
452
453
454

Returns
=======
np.array((nu,nv), dtype=np.complex128):
    the gridded visibilities
)""";
Martin Reinecke's avatar
Martin Reinecke committed
455
template<typename T> pyarr<complex<T>> vis2grid_c(
Martin Reinecke's avatar
Martin Reinecke committed
456
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
457
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &vis_,
Martin Reinecke's avatar
Martin Reinecke committed
458
  py::object &grid_in, const py::object &wgt_)
459
  {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
460
461
462
  checkArray(vis_, "vis", {0});
  size_t nvis = size_t(vis_.shape(0));
  checkArray(idx_, "idx", {nvis});
463
464
  auto vis=vis_.template unchecked<1>();
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
465
466
467
  pyarr<T> wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
468

469
  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
Martin Reinecke committed
470
  auto res = provideCArray<complex<T>>(grid_in, {nu, nv});
471
  auto grid = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
472
473
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
474
  T beta = gconf.Beta();
475
  size_t supp = gconf.Supp();
476

477
#pragma omp parallel num_threads(nthreads)
478
{
479
  Helper<T> hlp(gconf, nullptr, grid);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
480
  T emb = exp(-2*beta);
481
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
482
  const T * ku = hlp.kernel.data();
483
  const T * kv = hlp.kernel.data()+supp;
484
485
486
487
488

  // Loop over sampling points
#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
489
    UVW<T> coord = baselines.effectiveCoord(idx(ipart));
Martin Reinecke's avatar
Martin Reinecke committed
490
    hlp.prep(coord.u, coord.v);
Martin Reinecke's avatar
Martin Reinecke committed
491
    auto * ptr = hlp.p0w;
492
    auto v(vis(ipart)*emb);
Martin Reinecke's avatar
Martin Reinecke committed
493
494
    if (have_wgt)
      v*=wgt(ipart);
495
    for (size_t cu=0; cu<supp; ++cu)
496
497
      {
      complex<T> tmp(v*ku[cu]);
498
      for (size_t cv=0; cv<supp; ++cv)
499
        ptr[cv] += tmp*kv[cv];
500
      ptr+=jump;
501
502
503
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
504
  }
505
506
507
  return res;
  }

508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
522
523
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
524
525
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
526
527
528
529
530
531

Returns
=======
np.array((nu,nv), dtype=np.float64):
    the gridded visibilities (made real by making use of Hermitian symmetry)
)""";
Martin Reinecke's avatar
updates    
Martin Reinecke committed
532

533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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
549
550
wgt: np.array((nrows, nchannels), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
551
552
553
554
555
556

Returns
=======
np.array((nu,nv), dtype=np.complex128):
    the gridded visibilities
)""";
Martin Reinecke's avatar
Martin Reinecke committed
557
template<typename T> pyarr<complex<T>> ms2grid_c(
Martin Reinecke's avatar
Martin Reinecke committed
558
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
559
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &ms_,
Martin Reinecke's avatar
Martin Reinecke committed
560
  py::object &grid_in, const py::object &wgt_)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
561
562
563
564
565
566
  {
  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));
567
568
  auto ms = ms_.template unchecked<2>();
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
569
570
571
  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
572

Martin Reinecke's avatar
merge    
Martin Reinecke committed
573
  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
Martin Reinecke committed
574
  auto res = provideCArray<complex<T>>(grid_in, {nu, nv});
Martin Reinecke's avatar
merge    
Martin Reinecke committed
575
  auto grid = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
576
577
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
578
  T beta = gconf.Beta();
579
  size_t supp = gconf.Supp();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
580

581
#pragma omp parallel num_threads(nthreads)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
582
{
583
  Helper<T> hlp(gconf, nullptr, grid);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
584
  T emb = exp(-2*beta);
585
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
586
  const T * ku = hlp.kernel.data();
587
  const T * kv = hlp.kernel.data()+supp;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
588
589
590
591
592

  // Loop over sampling points
#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
593
594
595
596
    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
597
    hlp.prep(coord.u, coord.v);
Martin Reinecke's avatar
Martin Reinecke committed
598
    auto * ptr = hlp.p0w;
599
    auto v(ms(row,chan)*emb);
Martin Reinecke's avatar
Martin Reinecke committed
600
601
    if (have_wgt)
      v*=wgt(row, chan);
602
    for (size_t cu=0; cu<supp; ++cu)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
603
604
      {
      complex<T> tmp(v*ku[cu]);
605
      for (size_t cv=0; cv<supp; ++cv)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
606
        ptr[cv] += tmp*kv[cv];
607
      ptr+=jump;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
608
609
610
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
611
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
612
613
614
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
615
template<typename T> pyarr<complex<T>> grid2vis_c(
Martin Reinecke's avatar
Martin Reinecke committed
616
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
617
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &grid_,
Martin Reinecke's avatar
Martin Reinecke committed
618
  const py::object &wgt_)
619
620
  {
  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
621
  checkArray(idx_, "idx", {0});
622
  auto grid = grid_.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
623
  checkArray(grid_, "grid", {nu, nv});
624
  size_t nvis = size_t(idx_.shape(0));
625
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
626
627
628
  auto wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
629

Martin Reinecke's avatar
merge    
Martin Reinecke committed
630
  auto res = makeArray<complex<T>>({nvis});
631
  auto vis = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
632
633
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
634
  T beta = gconf.Beta();
635
  size_t supp = gconf.Supp();
636
637

  // Loop over sampling points
638
#pragma omp parallel num_threads(nthreads)
639
{
640
  Helper<T> hlp(gconf, grid, nullptr);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
641
  T emb = exp(-2*beta);
642
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
643
  const T * ku = hlp.kernel.data();
644
  const T * kv = hlp.kernel.data()+supp;
645
646
647
648

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
649
    UVW<T> coord = baselines.effectiveCoord(idx(ipart));
Martin Reinecke's avatar
Martin Reinecke committed
650
    hlp.prep(coord.u, coord.v);
651
    complex<T> r = 0;
Martin Reinecke's avatar
Martin Reinecke committed
652
    const auto * ptr = hlp.p0r;
653
    for (size_t cu=0; cu<supp; ++cu)
654
655
      {
      complex<T> tmp(0);
656
      for (size_t cv=0; cv<supp; ++cv)
657
658
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
659
      ptr += jump;
660
      }
Martin Reinecke's avatar
Martin Reinecke committed
661
    if (have_wgt) r*=wgt[ipart];
662
663
664
    vis[ipart] = r*emb;
    }
}
Martin Reinecke's avatar
Martin Reinecke committed
665
  }
666
667
668
  return res;
  }

669
670
671
672
673
674
675
676
677
678
679
680
681
682
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
683
684
wgt: np.array((nvis,), dtype=np.float64), optional
    If present, visibilities are multiplied by the corresponding entries.
685
686
687
688
689
690

Returns
=======
np.array((nvis,), dtype=np.complex)
    The degridded visibility data
)""";
Martin Reinecke's avatar
Martin Reinecke committed
691
template<typename T> pyarr<complex<T>> grid2ms_c(const PyBaselines<T> &baselines,
Martin Reinecke's avatar
Martin Reinecke committed
692
  const PyGridderConfig<T> &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
693
  const pyarr<complex<T>> &grid_, py::object &ms_in, const py::object &wgt_)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
694
  {
695
696
  auto nrows = baselines.Nrows();
  auto nchan = baselines.Nchannels();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
697
698
699
700
701
  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));
702
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
703
704
705
  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
706

707
708
  auto res = provideArray<complex<T>>(ms_in, {nrows, nchan});
  auto ms = res.template mutable_unchecked<2>();
Martin Reinecke's avatar
Martin Reinecke committed
709
710
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
711
  T beta = gconf.Beta();
712
  size_t supp = gconf.Supp();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
713
714

  // Loop over sampling points
715
#pragma omp parallel num_threads(nthreads)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
716
{
717
  Helper<T> hlp(gconf, grid, nullptr);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
718
  T emb = exp(-2*beta);
719
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
720
  const T * ku = hlp.kernel.data();
721
  const T * kv = hlp.kernel.data()+supp;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
722
723
724
725

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
726
727
728
729
    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
730
731
    hlp.prep(coord.u, coord.v);
    complex<T> r = 0;
Martin Reinecke's avatar
Martin Reinecke committed
732
    const auto * ptr = hlp.p0r;
733
    for (size_t cu=0; cu<supp; ++cu)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
734
735
      {
      complex<T> tmp(0);
736
      for (size_t cv=0; cv<supp; ++cv)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
737
738
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
739
      ptr += jump;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
740
      }
Martin Reinecke's avatar
Martin Reinecke committed
741
742
743
    r*=emb;
    if (have_wgt)
      r*=wgt(row, chan);
744
    ms(row,chan) += r*emb;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
745
746
    }
}
Martin Reinecke's avatar
Martin Reinecke committed
747
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
748
749
750
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
751
template<typename T> pyarr<complex<T>> apply_holo(
Martin Reinecke's avatar
Martin Reinecke committed
752
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
Martin Reinecke's avatar
Martin Reinecke committed
753
  const pyarr<uint32_t> &idx_, const pyarr<complex<T>> &grid_,
Martin Reinecke's avatar
Martin Reinecke committed
754
  const py::object &wgt_)
755
756
757
758
759
760
  {
  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));
761
  auto idx = idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
762
763
764
  pyarr<T> wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
765
766
767
768
769

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

  for (size_t i=0; i<nu*nv; ++i) ogrid[i] = T(0);
772
  T beta = gconf.Beta();
773
  size_t supp = gconf.Supp();
774
775

  // Loop over sampling points
776
#pragma omp parallel num_threads(nthreads)
777
778
779
780
{
  Helper<T> hlp(gconf, grid, ogrid);
  T emb = exp(-2*beta);
  int jump = hlp.lineJump();
Martin Reinecke's avatar
Martin Reinecke committed
781
  const T * ku = hlp.kernel.data();
782
  const T * kv = hlp.kernel.data()+supp;
783
784
785
786

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
Martin Reinecke's avatar
Martin Reinecke committed
787
788
    auto tidx = idx(ipart);
    UVW<T> coord = baselines.effectiveCoord(tidx);
789
790
    hlp.prep(coord.u, coord.v);
    complex<T> r = 0;
Martin Reinecke's avatar
Martin Reinecke committed
791
    const auto * ptr = hlp.p0r;
792
    for (size_t cu=0; cu<supp; ++cu)
793
794
      {
      complex<T> tmp(0);
795
      for (size_t cv=0; cv<supp; ++cv)
796
797
798
799
800
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
      ptr += jump;
      }
    r*=emb*emb;
Martin Reinecke's avatar
Martin Reinecke committed
801
802
803
804
805
    if (have_wgt)
      {
      auto twgt = wgt(ipart);
      r*=twgt*twgt;
      }
Martin Reinecke's avatar
Martin Reinecke committed
806
    auto * wptr = hlp.p0w;
807
    for (size_t cu=0; cu<supp; ++cu)
808
809
      {
      complex<T> tmp(r*ku[cu]);
810
      for (size_t cv=0; cv<supp; ++cv)
811
        wptr[cv] += tmp*kv[cv];
Martin Reinecke's avatar
Martin Reinecke committed
812
      wptr += jump;
813
814
815
816
817
818
      }
    }
}
  }
  return res;
  }
819

Martin Reinecke's avatar
Martin Reinecke committed
820
template<typename T> pyarr<T> get_correlations(
Martin Reinecke's avatar
Martin Reinecke committed
821
  const PyBaselines<T> &baselines, const PyGridderConfig<T> &gconf,
822
  const pyarr<uint32_t> &idx_, int du, int dv, const py::object &wgt_)
823
824
  {
  size_t nu=gconf.Nu(), nv=gconf.Nv();
825
826
827
  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");
828
829
830
  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
831
832
833
  pyarr<T> wgt2 = providePotentialArray<T>(wgt_, {nvis});
  bool have_wgt = wgt2.size()>0;
  auto wgt = wgt2.template unchecked<1>();
834
835
836
837
838
839
840
841

  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.;

842
843
  size_t u0, u1, v0, v1;
  if (du>=0)
844
    { u0=0; u1=supp-du; }
845
  else
846
    { u0=-du; u1=supp; }
847
  if (dv>=0)
848
    { v0=0; v1=supp-dv; }
849
  else
850
    { v0=-dv; v1=supp; }
851

852
853
854
855
856
857
  // 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
858
  const T * ku = hlp.kernel.data();
859
  const T * kv = hlp.kernel.data()+supp;
860
861
862
863
864
865

#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);
Martin Reinecke's avatar
Martin Reinecke committed
866
    auto * wptr = hlp.p0w + u0*jump;
Martin Reinecke's avatar
Martin Reinecke committed
867
868
869
870
871
872
    auto f0 = emb*emb;
    if (have_wgt)
      {
      auto twgt = wgt(ipart);
      f0*=twgt*twgt;
      }
873
    for (size_t cu=u0; cu<u1; ++cu)
874
      {
Martin Reinecke's avatar
Martin Reinecke committed
875
      auto f1=ku[cu]*ku[cu+du]*f0;
876
877
      for (size_t cv=v0; cv<v1; ++cv)
        wptr[cv] += f1*kv[cv]*kv[cv+dv];
878
879
880
881
882
883
884
885
      wptr += jump;
      }
    }
}
  }
  return res;
  }

886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
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
Martin Reinecke committed
913
914
template<typename T> pyarr<uint32_t> getIndices(const PyBaselines<T> &baselines,
  const PyGridderConfig<T> &gconf, const pyarr<bool> &flags_, int chbegin,
915
  int chend, T wmin, T wmax)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
916
  {
917
918
  size_t nrow=baselines.Nrows(),
         nchan=baselines.Nchannels(),
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
919
         nsafe=gconf.Nsafe();
920
921
  if (chbegin<0) chbegin=0;
  if (chend<0) chend=nchan;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
922
923
924
  myassert(chend>chbegin, "empty channel range selected");
  myassert(chend<=int(nchan), "chend too large");
  myassert(wmax>wmin, "empty w range selected");
Martin Reinecke's avatar
merge    
Martin Reinecke committed
925
  checkArray(flags_, "flags", {nrow, nchan});
926
927
928
929
  auto flags = flags_.data();
  constexpr int side=1<<logsquare;
  size_t nbu = (gconf.Nu()+1+side-1) >> logsquare,
         nbv = (gconf.Nv()+1+side-1) >> logsquare;
Martin Reinecke's avatar
Martin Reinecke committed
930
931
  vector<uint32_t> acc(nbu*nbv+1, 0);
  vector<uint32_t> tmp(nrow*(chend-chbegin));
Martin Reinecke's avatar
Martin Reinecke committed
932
933
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
934
  for (size_t irow=0, idx=0; irow<nrow; ++irow)
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
935
    for (int ichan=chbegin; ichan<chend; ++ichan)
Martin Reinecke's avatar
Martin Reinecke committed
936
      if (!flags[irow*nchan+ichan])
937
        {
938
        auto uvw = baselines.effectiveCoord(irow, ichan);
939
940
        if ((uvw.w>=wmin) && (uvw.w<wmax))
          {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
941
942
943
944
945
          T u, v;
          int iu0, iv0;
          gconf.getpix(uvw.u, uvw.v, u, v, iu0, iv0);
          iu0 = (iu0+nsafe)>>logsquare;
          iv0 = (iv0+nsafe)>>logsquare;
Martin Reinecke's avatar
Martin Reinecke committed
946
          ++acc[nbv*iu0 + iv0 + 1];
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
947
          tmp[idx++] = nbv*iu0 + iv0;
948
949
          }
        }
Martin Reinecke's avatar
Martin Reinecke committed
950
951
952

  for (size_t i=1; i<acc.size(); ++i)
    acc[i] += acc[i-1];
Martin Reinecke's avatar
Martin Reinecke committed
953
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
954
  auto res = makeArray<uint32_t>({acc.back()});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
955
  auto iout = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
956
957
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
958
  for (size_t irow=0, idx=0; irow<nrow; ++irow)
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
959
    for (int ichan=chbegin; ichan<chend; ++ichan)
Martin Reinecke's avatar
Martin Reinecke committed
960
      if (!flags[irow*nchan+ichan])
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
961
962
963
964
965
        {
        auto uvw = baselines.effectiveCoord(irow, ichan);
        if ((uvw.w>=wmin) && (uvw.w<wmax))
          iout[acc[tmp[idx++]]++] = irow*nchan+ichan;
        }
Martin Reinecke's avatar
Martin Reinecke committed
966
  }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
967
968
969
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
970
template<typename T> pyarr<complex<T>> vis2dirty_wstack(const PyBaselines<T> &baselines,
Martin Reinecke's avatar
Martin Reinecke committed
971
  const PyGridderConfig<T> &gconf, const pyarr<uint32_t> &idx_,
Martin Reinecke's avatar
Martin Reinecke committed
972
973
  const pyarr<complex<T>> &vis_)
  {
Martin Reinecke's avatar
Martin Reinecke committed
974
975
976
977
  auto nx_dirty=gconf.Nxdirty();
  auto ny_dirty=gconf.Nydirty();
  auto psx=gconf.Pixsize_x();
  auto psy=gconf.Pixsize_y();
Martin Reinecke's avatar
Martin Reinecke committed
978
  checkArray(vis_, "vis", {0});
Martin Reinecke's avatar
Martin Reinecke committed
979
  auto vis=vis_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
980
981
  size_t nvis = size_t(vis_.shape(0));
  checkArray(idx_, "idx", {nvis});
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
982
  auto idx=idx_.template unchecked<1>();
Martin Reinecke's avatar
Martin Reinecke committed
983
984
985
986
987
988
989
990
991
992
993
994

  // determine w values for every visibility, and min/max w;
  T wmin=T(1e38), wmax=T(-1e38);
  vector<T> wval(nvis);
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
    wval[ipart] = baselines.effectiveCoord(idx(ipart)).w;
    wmin = min(wmin,wval[ipart]);
    wmax = max(wmax,wval[ipart]);
    }
cout << "data w range: " << wmin << " to " << wmax << endl;

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
995
996
  double x0 = -0.5*nx_dirty*psx,
         y0 = -0.5*ny_dirty*psy;
997
  double nmin = sqrt(max(1.-x0*x0-y0*y0,0.))-1.;
Martin Reinecke's avatar
Martin Reinecke committed
998
  double dw = 0.25/abs(nmin);
Martin Reinecke's avatar
Martin Reinecke committed
999

Martin Reinecke's avatar
Martin Reinecke committed
1000
  double w_eps=1e-7; // FIXME
Martin Reinecke's avatar
Martin Reinecke committed
1001
  auto w_supp = get_supp(w_eps);
1002
  EC_Kernel_with_correction<T> kernel(w_supp);
Martin Reinecke's avatar
Martin Reinecke committed
1003
1004
  wmin -= 0.5*w_supp*dw;
  wmax += 0.5*