nifty_gridder.cc 33.7 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 *  This file is part of nifty_gridder.
 *
 *  nifty_gridder is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  nifty_gridder is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with nifty_fridder; if not, write to the Free Software
 *  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
24
25
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <iostream>
#include <algorithm>
Martin Reinecke's avatar
Martin Reinecke committed
26
27

#define POCKETFFT_OPENMP
Martin Reinecke's avatar
updates    
Martin Reinecke committed
28
#include "pocketfft_hdronly.h"
Martin Reinecke's avatar
import  
Martin Reinecke committed
29

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
30
31
#ifdef __GNUC__
#define RESTRICT __restrict__
Martin Reinecke's avatar
Martin Reinecke committed
32
#define NOINLINE __attribute__ ((noinline))
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
33
34
35
36
#else
#define RESTRICT
#endif

Martin Reinecke's avatar
import  
Martin Reinecke committed
37
38
39
40
41
42
using namespace std;

namespace py = pybind11;

namespace {

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
43
44
45
46
//
// basic utilities
//

47
48
49
50
51
52
void myassert(bool cond, const char *msg)
  {
  if (cond) return;
  throw runtime_error(msg);
  }

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
53
54
55
56
57
58
59
60
61
62
63
/*! Returns the remainder of the division \a v1/v2.
    The result is non-negative.
    \a v1 can be positive or negative; \a v2 must be positive. */
template<typename T> inline T fmodulo (T v1, T v2)
  {
  if (v1>=0)
    return (v1<v2) ? v1 : fmod(v1,v2);
  T tmp=fmod(v1,v2)+v2;
  return (tmp==v2) ? T(0) : tmp;
  }

Martin Reinecke's avatar
Martin Reinecke committed
64
65
66
67
//
// Utilities for Gauss-Legendre quadrature
//

Martin Reinecke's avatar
Martin Reinecke committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
static inline double one_minus_x2 (double x)
  { return (fabs(x)>0.1) ? (1.+x)*(1.-x) : 1.-x*x; }

void legendre_prep(int n, vector<double> &x, vector<double> &w)
  {
  constexpr double pi = 3.141592653589793238462643383279502884197;
  constexpr double eps = 3e-14;
  int m = (n+1)>>1;
  x.resize(m);
  w.resize(m);

  double t0 = 1 - (1-1./n) / (8.*n*n);
  double t1 = 1./(4.*n+2.);

#pragma omp parallel
{
  int i;
#pragma omp for schedule(dynamic,100)
  for (i=1; i<=m; ++i)
    {
    double x0 = cos(pi * ((i<<2)-1) * t1) * t0;

    int dobreak=0;
    int j=0;
    double dpdx;
    while(1)
      {
      double P_1 = 1.0;
      double P0 = x0;
      double dx, x1;

      for (int k=2; k<=n; k++)
        {
        double P_2 = P_1;
        P_1 = P0;
//        P0 = ((2*k-1)*x0*P_1-(k-1)*P_2)/k;
        P0 = x0*P_1 + (k-1.)/k * (x0*P_1-P_2);
        }

      dpdx = (P_1 - x0*P0) * n / one_minus_x2(x0);

      /* Newton step */
      x1 = x0 - P0/dpdx;
      dx = x0-x1;
      x0 = x1;
      if (dobreak) break;

      if (abs(dx)<=eps) dobreak=1;
      if (++j>=100) throw runtime_error("convergence problem");
      }

    x[m-i] = x0;
    w[m-i] = 2. / (one_minus_x2(x0) * dpdx * dpdx);
    }
} // end of parallel region
  }

Martin Reinecke's avatar
Martin Reinecke committed
125
126
127
128
//
// Start of real gridder functionality
//

129
130
131
132
template<typename T>
  using pyarr = py::array_t<T>;
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
133

Martin Reinecke's avatar
updates    
Martin Reinecke committed
134
135
136
template<typename T> pyarr_c<T> makearray(const vector<size_t> &shape)
  { return pyarr_c<T>(shape); }

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
137
size_t get_w(double epsilon)
Martin Reinecke's avatar
Martin Reinecke committed
138
139
140
141
142
143
144
145
146
147
148
  {
  static const vector<double> maxmaperr { 1e8, 0.32, 0.021, 6.2e-4,
    1.08e-5, 1.25e-7, 8.25e-10, 5.70e-12, 1.22e-13, 2.48e-15, 4.82e-17,
    6.74e-19, 5.41e-21, 4.41e-23, 7.88e-25, 3.9e-26 };

  double epssq = epsilon*epsilon;

  for (size_t i=1; i<maxmaperr.size(); ++i)
    if (epssq>maxmaperr[i]) return i;
  throw runtime_error("requested epsilon too small - minimum is 2e-13");
  }
Martin Reinecke's avatar
Martin Reinecke committed
149

Martin Reinecke's avatar
Martin Reinecke committed
150
151
template<typename T> pyarr_c<T> complex2hartley
  (const pyarr_c<complex<T>> &grid_)
Martin Reinecke's avatar
Martin Reinecke committed
152
153
  {
  myassert(grid_.ndim()==2, "grid array must be 2D");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
154
  size_t nu = size_t(grid_.shape(0)), nv = size_t(grid_.shape(1));
Martin Reinecke's avatar
Martin Reinecke committed
155
156
  auto grid = grid_.data();

Martin Reinecke's avatar
Martin Reinecke committed
157
  auto res = makearray<T>({nu,nv});
Martin Reinecke's avatar
Martin Reinecke committed
158
  auto grid2 = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
159
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
160
  for (size_t u=0; u<nu; ++u)
Martin Reinecke's avatar
Martin Reinecke committed
161
    {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
162
163
    size_t xu = (u==0) ? 0 : nu-u;
    for (size_t v=0; v<nv; ++v)
Martin Reinecke's avatar
Martin Reinecke committed
164
      {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
165
166
167
      size_t xv = (v==0) ? 0 : nv-v;
      size_t i1 = u*nv+v;
      size_t i2 = xu*nv+xv;
Martin Reinecke's avatar
Martin Reinecke committed
168
169
      grid2[i1] = T(0.5)*(grid[i1].real()+grid[i1].imag()+
                          grid[i2].real()-grid[i2].imag());
Martin Reinecke's avatar
Martin Reinecke committed
170
171
172
173
174
      }
    }
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
175
176
template<typename T> pyarr_c<complex<T>> hartley2complex
  (const pyarr_c<T> &grid_)
Martin Reinecke's avatar
Martin Reinecke committed
177
178
  {
  myassert(grid_.ndim()==2, "grid array must be 2D");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
179
  size_t nu = size_t(grid_.shape(0)), nv = size_t(grid_.shape(1));
Martin Reinecke's avatar
Martin Reinecke committed
180
181
  auto grid = grid_.data();

Martin Reinecke's avatar
Martin Reinecke committed
182
  auto res=makearray<complex<T>>({nu, nv});
Martin Reinecke's avatar
Martin Reinecke committed
183
  auto grid2 = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
184
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
185
  for (size_t u=0; u<nu; ++u)
Martin Reinecke's avatar
Martin Reinecke committed
186
    {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
187
188
    size_t xu = (u==0) ? 0 : nu-u;
    for (size_t v=0; v<nv; ++v)
Martin Reinecke's avatar
Martin Reinecke committed
189
      {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
190
191
192
      size_t xv = (v==0) ? 0 : nv-v;
      size_t i1 = u*nv+v;
      size_t i2 = xu*nv+xv;
Martin Reinecke's avatar
Martin Reinecke committed
193
194
195
      T v1 = T(0.5)*grid[i1];
      T v2 = T(0.5)*grid[i2];
      grid2[i1] = complex<T>(v1+v2, v1-v2);
Martin Reinecke's avatar
Martin Reinecke committed
196
197
198
199
200
      }
    }
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
201
202
203
204
205
206
207
208
template<typename T> void hartley2_2D(const pyarr_c<T> &in, pyarr_c<T> &out)
  {
  size_t nu=in.shape(0), nv=in.shape(1);
  pocketfft::r2r_hartley({nu, nv},
    {in.strides(0), in.strides(1)},
    {out.strides(0), out.strides(1)}, {0,1},
    in.data(), out.mutable_data(), T(1), 0);
  auto ptmp = out.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
209
#pragma omp parallel for
Martin Reinecke's avatar
Martin Reinecke committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  for(size_t i=1; i<(nu+1)/2; ++i)
    for(size_t j=1; j<(nv+1)/2; ++j)
       {
       T a = ptmp[i*nv+j];
       T b = ptmp[(nu-i)*nv+j];
       T c = ptmp[i*nv+nv-j];
       T d = ptmp[(nu-i)*nv+nv-j];
       ptmp[i*nv+j] = T(0.5)*(a+b+c-d);
       ptmp[(nu-i)*nv+j] = T(0.5)*(a+b+d-c);
       ptmp[i*nv+nv-j] = T(0.5)*(a+c+d-b);
       ptmp[(nu-i)*nv+nv-j] = T(0.5)*(b+c+d-a);
       }
  }

Martin Reinecke's avatar
Martin Reinecke committed
224
225
/* Compute correction factors for the ES gridding kernel
   This implementation follows eqs. (3.8) to (3.10) of Barnett et al. 2018 */
Martin Reinecke's avatar
updates    
Martin Reinecke committed
226
vector<double> correction_factors (size_t n, size_t nval, size_t w)
Martin Reinecke's avatar
Martin Reinecke committed
227
228
229
230
231
232
233
234
235
236
  {
  constexpr double pi = 3.141592653589793238462643383279502884197;
  auto beta = 2.3*w;
  auto p = int(1.5*w+2);
  double alpha = pi*w/n;
  vector<double> x, wgt;
  legendre_prep(2*p,x,wgt);
  auto psi = x;
  for (auto &v:psi)
    v = exp(beta*(sqrt(1-v*v)-1.));
Martin Reinecke's avatar
updates    
Martin Reinecke committed
237
  vector<double> res(nval);
Martin Reinecke's avatar
Martin Reinecke committed
238
#pragma omp parallel for schedule(static)
Martin Reinecke's avatar
Martin Reinecke committed
239
240
241
242
243
  for (size_t k=0; k<nval; ++k)
    {
    double tmp=0;
    for (int i=0; i<p; ++i)
      tmp += wgt[i]*psi[i]*cos(alpha*k*x[i]);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
244
    res[k] = 1./(w*tmp);
Martin Reinecke's avatar
Martin Reinecke committed
245
246
247
248
    }
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
249
template<typename T> struct UVW
Martin Reinecke's avatar
updates    
Martin Reinecke committed
250
  {
Martin Reinecke's avatar
Martin Reinecke committed
251
  T u, v, w;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
252
  UVW () {}
Martin Reinecke's avatar
Martin Reinecke committed
253
254
  UVW (T u_, T v_, T w_) : u(u_), v(v_), w(w_) {}
  UVW operator* (T fct) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
255
256
    { return UVW(u*fct, v*fct, w*fct); }
  };
Martin Reinecke's avatar
Martin Reinecke committed
257

Martin Reinecke's avatar
Martin Reinecke committed
258
template<typename T> class Baselines
Martin Reinecke's avatar
Martin Reinecke committed
259
260
  {
  private:
Martin Reinecke's avatar
Martin Reinecke committed
261
262
    vector<UVW<T>> coord;
    vector<T> scaling;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
263
    size_t nrows, nchan;
Martin Reinecke's avatar
Martin Reinecke committed
264
265

  public:
266
    Baselines(const pyarr_c<T> &coord_, const pyarr_c<T> &scaling_)
Martin Reinecke's avatar
Martin Reinecke committed
267
268
      {
      myassert(coord_.ndim()==2, "coord array must be 2D");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
269
      myassert(coord_.shape(1)==3, "coord.shape[1] must be 3");
Martin Reinecke's avatar
Martin Reinecke committed
270
      myassert(scaling_.ndim()==1, "scaling array must be 1D");
Martin Reinecke's avatar
Martin Reinecke committed
271
      nrows = coord_.shape(0);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
272
273
274
      nchan = scaling_.shape(0);
      scaling.resize(nchan);
      for (size_t i=0; i<nchan; ++i)
Martin Reinecke's avatar
Martin Reinecke committed
275
276
        scaling[i] = scaling_.data()[i];
      coord.resize(nrows);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
277
      auto cood = coord_.data();
Martin Reinecke's avatar
Martin Reinecke committed
278
      for (size_t i=0; i<coord.size(); ++i)
Martin Reinecke's avatar
Martin Reinecke committed
279
        coord[i] = UVW<T>(cood[3*i], cood[3*i+1], cood[3*i+2]);
280
      myassert(nrows*nchan<(size_t(1)<<32), "too many entries in MS");
Martin Reinecke's avatar
Martin Reinecke committed
281
282
      }

Martin Reinecke's avatar
Martin Reinecke committed
283
    UVW<T> effectiveCoord(uint32_t index) const
284
285
286
287
288
289
290
      {
      size_t irow = index/nchan;
      size_t ichan = index-nchan*irow;
      return coord[irow]*scaling[ichan];
      }
    UVW<T> effectiveCoord(size_t irow, size_t ichan) const
      { return coord[irow]*scaling[ichan]; }
Martin Reinecke's avatar
Martin Reinecke committed
291
    size_t Nrows() const { return nrows; }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
292
293
    size_t Nchannels() const { return nchan; }

Martin Reinecke's avatar
Martin Reinecke committed
294
    template<typename T2> pyarr_c<T2> ms2vis(const pyarr_c<T2> &ms_,
Martin Reinecke's avatar
updates    
Martin Reinecke committed
295
296
297
298
299
300
301
302
303
304
      const pyarr_c<uint32_t> &idx_) const
      {
      myassert(idx_.ndim()==1, "idx array must be 1D");
      myassert(ms_.ndim()==2, "ms array must be 2D");
      myassert(size_t(ms_.shape(0))==nrows, "baselines 1st dim mismatch");
      myassert(size_t(ms_.shape(1))==nchan, "baselines 2nd dim mismatch");
      size_t nvis = size_t(idx_.shape(0));
      auto idx = idx_.data();
      auto ms = ms_.data();

Martin Reinecke's avatar
Martin Reinecke committed
305
      auto res=makearray<T2>({nvis});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
306
      auto vis = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
307
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
308
      for (size_t i=0; i<nvis; ++i)
309
        vis[i] = ms[idx[i]];
Martin Reinecke's avatar
updates    
Martin Reinecke committed
310
311
312
      return res;
      }

Martin Reinecke's avatar
Martin Reinecke committed
313
    template<typename T2> pyarr_c<T2> vis2ms(const pyarr_c<T2> &vis_,
Martin Reinecke's avatar
updates    
Martin Reinecke committed
314
315
316
317
318
319
320
321
322
      const pyarr_c<uint32_t> &idx_) const
      {
      myassert(idx_.ndim()==1, "idx array must be 1D");
      myassert(vis_.ndim()==1, "vis array must be 1D");
      size_t nvis = size_t(vis_.shape(0));
      myassert(int(nvis)==idx_.shape(0), "idx/vis size mismatch");
      auto idx = idx_.data();
      auto vis = vis_.data();

Martin Reinecke's avatar
Martin Reinecke committed
323
      auto res = makearray<T2>({nrows, nchan});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
324
325
      auto ms = res.mutable_data();
      for (size_t i=0; i<nrows*nchan; ++i)
Martin Reinecke's avatar
Martin Reinecke committed
326
        ms[i] = T2(0);
Martin Reinecke's avatar
Martin Reinecke committed
327
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
328
      for (size_t i=0; i<nvis; ++i)
329
        ms[idx[i]] = vis[i];
Martin Reinecke's avatar
updates    
Martin Reinecke committed
330
331
332
      return res;
      }

Martin Reinecke's avatar
Martin Reinecke committed
333
334
    template<typename T2> pyarr_c<T2> add_vis_to_ms(const pyarr_c<T2> &vis_,
      const pyarr_c<uint32_t> &idx_, pyarr_c<T2> &ms_) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
335
336
337
338
339
340
341
342
343
344
345
346
      {
      myassert(idx_.ndim()==1, "idx array must be 1D");
      myassert(vis_.ndim()==1, "vis array must be 1D");
      myassert(ms_.ndim()==2, "ms array must be 2D");
      myassert(size_t(ms_.shape(0))==nrows, "ms: bad 1st dimension");
      myassert(size_t(ms_.shape(1))==nchan, "ms: bad 2nd dimension");
      size_t nvis = size_t(vis_.shape(0));
      myassert(int(nvis)==idx_.shape(0), "idx/vis size mismatch");
      auto idx = idx_.data();
      auto vis = vis_.data();
      auto ms = ms_.mutable_data();

Martin Reinecke's avatar
Martin Reinecke committed
347
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
348
      for (size_t i=0; i<nvis; ++i)
349
        ms[idx[i]] += vis[i];
Martin Reinecke's avatar
updates    
Martin Reinecke committed
350
351
      return ms_;
      }
Martin Reinecke's avatar
Martin Reinecke committed
352
353
  };

Martin Reinecke's avatar
Martin Reinecke committed
354
355
constexpr int logsquare=4;

Martin Reinecke's avatar
Martin Reinecke committed
356
template<typename T> class GridderConfig
Martin Reinecke's avatar
Martin Reinecke committed
357
358
359
  {
  private:
    size_t nx_dirty, ny_dirty;
Martin Reinecke's avatar
Martin Reinecke committed
360
    T ucorr, vcorr;
Martin Reinecke's avatar
Martin Reinecke committed
361
    size_t w, nsafe, nu, nv;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
362
    T beta;
Martin Reinecke's avatar
Martin Reinecke committed
363
    vector<T> cfu, cfv;
Martin Reinecke's avatar
Martin Reinecke committed
364
365
366
367
368
369
370
371

  public:
    GridderConfig(size_t nxdirty, size_t nydirty, double epsilon,
      double urange, double vrange)
      : nx_dirty(nxdirty), ny_dirty(nydirty),
        ucorr(1./urange), vcorr(1./vrange),
        w(get_w(epsilon)), nsafe((w+1)/2),
        nu(max(2*nsafe,2*nx_dirty)), nv(max(2*nsafe,2*ny_dirty)),
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
372
        beta(2.3*w),
Martin Reinecke's avatar
updates    
Martin Reinecke committed
373
        cfu(nx_dirty), cfv(ny_dirty)
Martin Reinecke's avatar
Martin Reinecke committed
374
375
376
377
378
379
      {
      myassert((nx_dirty&1)==0, "nx_dirty must be even");
      myassert((ny_dirty&1)==0, "ny_dirty must be even");
      myassert(epsilon>0, "epsilon must be positive");
      myassert(urange>0, "urange must be positive");
      myassert(vrange>0, "vrange must be positive");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
380
381
382
383
384
385
386
387
388
389
390

      auto tmp = correction_factors(nu, nx_dirty/2+1, w);
      cfu[nx_dirty/2]=tmp[0];
      cfu[0]=tmp[nx_dirty/2];
      for (size_t i=1; i<nx_dirty/2; ++i)
        cfu[nx_dirty/2-i] = cfu[nx_dirty/2+i] = tmp[i];
      tmp = correction_factors(nv, ny_dirty/2+1, w);
      cfv[ny_dirty/2]=tmp[0];
      cfv[0]=tmp[ny_dirty/2];
      for (size_t i=1; i<ny_dirty/2; ++i)
        cfv[ny_dirty/2-i] = cfv[ny_dirty/2+i] = tmp[i];
Martin Reinecke's avatar
Martin Reinecke committed
391
392
393
394
      }
    size_t Nu() const { return nu; }
    size_t Nv() const { return nv; }
    size_t W() const { return w; }
395
    size_t Nsafe() const { return nsafe; }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
396
    T Beta() const { return beta; }
Martin Reinecke's avatar
Martin Reinecke committed
397
    pyarr_c<T> grid2dirty(const pyarr_c<T> &grid) const
Martin Reinecke's avatar
Martin Reinecke committed
398
      {
Martin Reinecke's avatar
Martin Reinecke committed
399
400
401
      myassert(grid.ndim()==2, "grid must be a 2D array");
      myassert(size_t(grid.shape(0))==nu, "bad 1st dimension");
      myassert(size_t(grid.shape(1))==nv, "bad 2nd dimension");
Martin Reinecke's avatar
Martin Reinecke committed
402
      auto tmp = makearray<T>({nu, nv});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
403
      auto ptmp = tmp.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
404
      hartley2_2D<T>(grid, tmp);
Martin Reinecke's avatar
Martin Reinecke committed
405
      auto res = makearray<T>({nx_dirty, ny_dirty});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
406
407
408
409
410
411
412
413
414
415
      auto pout = res.mutable_data();
      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
416
417
      return res;
      }
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    pyarr_c<complex<T>> grid2dirty_c(const pyarr_c<complex<T>> &grid) const
      {
      myassert(grid.ndim()==2, "grid must be a 2D array");
      myassert(size_t(grid.shape(0))==nu, "bad 1st dimension");
      myassert(size_t(grid.shape(1))==nv, "bad 2nd dimension");
      auto tmp = makearray<complex<T>>({nu, nv});
      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,
        grid.data(), tmp.mutable_data(), T(1), 0);
      auto res = makearray<complex<T>>({nx_dirty, ny_dirty});
      auto pout = res.mutable_data();
      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];
          }
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
441
    pyarr_c<T> dirty2grid(const pyarr_c<T> &dirty) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
442
      {
Martin Reinecke's avatar
Martin Reinecke committed
443
444
445
      myassert(dirty.ndim()==2, "dirty must be a 2D array");
      myassert(size_t(dirty.shape(0))==nx_dirty, "bad 1st dimension");
      myassert(size_t(dirty.shape(1))==ny_dirty, "bad 2nd dimension");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
446
      auto pdirty = dirty.data();
Martin Reinecke's avatar
Martin Reinecke committed
447
      auto tmp = makearray<T>({nu, nv});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
448
449
450
451
452
453
454
455
456
457
458
459
      auto ptmp = tmp.mutable_data();
      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
460
      hartley2_2D<T>(tmp, tmp);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
461
462
      return tmp;
      }
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    pyarr_c<complex<T>> dirty2grid_c(const pyarr_c<complex<T>> &dirty) const
      {
      myassert(dirty.ndim()==2, "dirty must be a 2D array");
      myassert(size_t(dirty.shape(0))==nx_dirty, "bad 1st dimension");
      myassert(size_t(dirty.shape(1))==ny_dirty, "bad 2nd dimension");
      auto pdirty = dirty.data();
      auto tmp = makearray<complex<T>>({nu, nv});
      auto ptmp = tmp.mutable_data();
      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];
          }
      pocketfft::c2c({nu,nv},{tmp.strides(0),tmp.strides(1)},
        {tmp.strides(0), tmp.strides(1)}, {0,1}, pocketfft::FORWARD,
        tmp.data(), tmp.mutable_data(), T(1), 0);
      return tmp;
      }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
487
488
489
490
491
492
493
494
495
    inline void getpix(T u_in, T v_in, T &u, T &v, int &iu0, int &iv0) const
      {
      u=fmodulo(u_in*ucorr, T(1))*nu,
      iu0 = int(u-w*0.5 + 1 + nu) - nu;
      if (iu0+w>nu+nsafe) iu0 = nu+nsafe-w;
      v=fmodulo(v_in*vcorr, T(1))*nv;
      iv0 = int(v-w*0.5 + 1 + nv) - nv;
      if (iv0+w>nv+nsafe) iv0 = nv+nsafe-w;
      }
Martin Reinecke's avatar
Martin Reinecke committed
496
497
  };

Martin Reinecke's avatar
Martin Reinecke committed
498
template<typename T> class Helper
Martin Reinecke's avatar
import  
Martin Reinecke committed
499
  {
Martin Reinecke's avatar
Martin Reinecke committed
500
  protected:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
501
502
    const GridderConfig<T> &gconf;
    int su;
Martin Reinecke's avatar
Martin Reinecke committed
503
504
  public:
    int sv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
505

Martin Reinecke's avatar
Martin Reinecke committed
506
    vector<T> kernel;
Martin Reinecke's avatar
Martin Reinecke committed
507
508
509
    int iu0, iv0; // start index of the current visibility
    int bu0, bv0; // start index of the current buffer

Martin Reinecke's avatar
Martin Reinecke committed
510
    NOINLINE void update(T u_in, T v_in)
Martin Reinecke's avatar
import  
Martin Reinecke committed
511
      {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
512
513
514
      T u, v;
      size_t w = gconf.W();
      gconf.getpix(u_in, v_in, u, v, iu0, iv0);
Martin Reinecke's avatar
Martin Reinecke committed
515
      T xw=T(2)/w;
Martin Reinecke's avatar
Martin Reinecke committed
516
517
      auto x0 = xw*(iu0-u);
      auto y0 = xw*(iv0-v);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
518
519
      T beta = gconf.Beta();
      for (size_t i=0; i<w; ++i)
Martin Reinecke's avatar
import  
Martin Reinecke committed
520
        {
Martin Reinecke's avatar
Martin Reinecke committed
521
522
523
524
        auto x = x0+i*xw;
        kernel[i  ] = beta*sqrt(T(1)-x*x);
        auto y = y0+i*xw;
        kernel[i+w] = beta*sqrt(T(1)-y*y);
Martin Reinecke's avatar
Martin Reinecke committed
525
        }
Martin Reinecke's avatar
Martin Reinecke committed
526
      for (auto &k : kernel)
Martin Reinecke's avatar
Martin Reinecke committed
527
        k = exp(k);
Martin Reinecke's avatar
import  
Martin Reinecke committed
528
529
      }

Martin Reinecke's avatar
Martin Reinecke committed
530
    bool need_to_move() const
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
531
532
533
534
      {
      int w = int(gconf.W());
      return (iu0<bu0) || (iv0<bv0) || (iu0+w>bu0+su) || (iv0+w>bv0+sv);
      }
Martin Reinecke's avatar
import  
Martin Reinecke committed
535

Martin Reinecke's avatar
Martin Reinecke committed
536
    void update_position()
Martin Reinecke's avatar
import  
Martin Reinecke committed
537
      {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
538
539
      bu0=((((iu0+gconf.Nsafe())>>logsquare)<<logsquare))-gconf.Nsafe();
      bv0=((((iv0+gconf.Nsafe())>>logsquare)<<logsquare))-gconf.Nsafe();
Martin Reinecke's avatar
import  
Martin Reinecke committed
540
541
      }

Martin Reinecke's avatar
Martin Reinecke committed
542
  protected:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
543
544
545
546
    Helper(const GridderConfig<T> &gconf_)
      : gconf(gconf_),
        su(2*gconf.Nsafe()+(1<<logsquare)), sv(2*gconf.Nsafe()+(1<<logsquare)),
        kernel(2*gconf.W()),
Martin Reinecke's avatar
Martin Reinecke committed
547
        bu0(-1000000), bv0(-1000000)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
548
      {}
Martin Reinecke's avatar
import  
Martin Reinecke committed
549
550
  };

Martin Reinecke's avatar
Martin Reinecke committed
551
template<typename T> class WriteHelper: public Helper<T>
Martin Reinecke's avatar
import  
Martin Reinecke committed
552
  {
Martin Reinecke's avatar
Martin Reinecke committed
553
  protected:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
554
    using Helper<T>::gconf;
Martin Reinecke's avatar
Martin Reinecke committed
555
556
557
558
559
560
561
562
563
564
565
566
    using Helper<T>::su;
  public:
    using Helper<T>::sv;
    using Helper<T>::kernel;
    using Helper<T>::iu0;
    using Helper<T>::iv0;
    using Helper<T>::bu0;
    using Helper<T>::bv0;
    using Helper<T>::need_to_move;
    using Helper<T>::update_position;
    using Helper<T>::update;

Martin Reinecke's avatar
import  
Martin Reinecke committed
567
  private:
Martin Reinecke's avatar
Martin Reinecke committed
568
569
    vector<complex<T>> data;
    complex<T> *grid;
Martin Reinecke's avatar
import  
Martin Reinecke committed
570
571
572

    void dump()
      {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
573
574
575
576
      if (bu0<-int(gconf.Nsafe())) return; // nothing written into buffer yet
      auto nu = int(gconf.Nu());
      auto nv = int(gconf.Nv());

Martin Reinecke's avatar
import  
Martin Reinecke committed
577
578
#pragma omp critical
{
Martin Reinecke's avatar
Martin Reinecke committed
579
580
581
      int idxu = (bu0+nu)%nu;
      int idxv0 = (bv0+nv)%nv;
      for (int iu=0; iu<su; ++iu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
582
583
        {
        int idxv = idxv0;
Martin Reinecke's avatar
Martin Reinecke committed
584
        for (int iv=0; iv<sv; ++iv)
Martin Reinecke's avatar
import  
Martin Reinecke committed
585
          {
Martin Reinecke's avatar
Martin Reinecke committed
586
          grid[idxu*nv + idxv] += data[iu*sv + iv];
Martin Reinecke's avatar
import  
Martin Reinecke committed
587
588
589
590
591
592
593
594
          if (++idxv>=nv) idxv=0;
          }
        if (++idxu>=nu) idxu=0;
        }
}
      }

  public:
Martin Reinecke's avatar
Martin Reinecke committed
595
    complex<T> *p0;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
596
597
    WriteHelper(const GridderConfig<T> &gconf_, complex<T> *grid_)
      : Helper<T>(gconf_), data(su*sv, T(0)), grid(grid_) {}
Martin Reinecke's avatar
updates    
Martin Reinecke committed
598
    ~WriteHelper() { dump(); }
Martin Reinecke's avatar
import  
Martin Reinecke committed
599

Martin Reinecke's avatar
Martin Reinecke committed
600
    void prep_write(T u_in, T v_in)
Martin Reinecke's avatar
import  
Martin Reinecke committed
601
      {
Martin Reinecke's avatar
Martin Reinecke committed
602
603
      update(u_in, v_in);
      if (need_to_move())
Martin Reinecke's avatar
import  
Martin Reinecke committed
604
605
        {
        dump();
Martin Reinecke's avatar
Martin Reinecke committed
606
        update_position();
Martin Reinecke's avatar
Martin Reinecke committed
607
        fill(data.begin(), data.end(), T(0));
Martin Reinecke's avatar
import  
Martin Reinecke committed
608
        }
Martin Reinecke's avatar
Martin Reinecke committed
609
      p0 = data.data() + sv*(iu0-bu0) + iv0-bv0;
Martin Reinecke's avatar
import  
Martin Reinecke committed
610
611
612
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
613
template<typename T> class ReadHelper: public Helper<T>
Martin Reinecke's avatar
import  
Martin Reinecke committed
614
  {
Martin Reinecke's avatar
Martin Reinecke committed
615
  protected:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
616
    using Helper<T>::gconf;
Martin Reinecke's avatar
Martin Reinecke committed
617
618
619
620
621
622
623
624
625
626
627
628
    using Helper<T>::su;
  public:
    using Helper<T>::sv;
    using Helper<T>::kernel;
    using Helper<T>::iu0;
    using Helper<T>::iv0;
    using Helper<T>::bu0;
    using Helper<T>::bv0;
    using Helper<T>::need_to_move;
    using Helper<T>::update_position;
    using Helper<T>::update;

Martin Reinecke's avatar
import  
Martin Reinecke committed
629
  private:
Martin Reinecke's avatar
Martin Reinecke committed
630
631
    vector<complex<T>> data;
    const complex<T> *grid;
Martin Reinecke's avatar
import  
Martin Reinecke committed
632
633
634

    void load()
      {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
635
636
      auto nu = int(gconf.Nu());
      auto nv = int(gconf.Nv());
Martin Reinecke's avatar
Martin Reinecke committed
637
638
639
      int idxu = (bu0+nu)%nu;
      int idxv0 = (bv0+nv)%nv;
      for (int iu=0; iu<su; ++iu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
640
641
        {
        int idxv = idxv0;
Martin Reinecke's avatar
Martin Reinecke committed
642
        for (int iv=0; iv<sv; ++iv)
Martin Reinecke's avatar
import  
Martin Reinecke committed
643
          {
Martin Reinecke's avatar
Martin Reinecke committed
644
          data[iu*sv + iv] = grid[idxu*nv + idxv];
Martin Reinecke's avatar
import  
Martin Reinecke committed
645
646
647
648
649
650
651
          if (++idxv>=nv) idxv=0;
          }
        if (++idxu>=nu) idxu=0;
        }
      }

  public:
Martin Reinecke's avatar
Martin Reinecke committed
652
    const complex<T> *p0;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
653
654
    ReadHelper(const GridderConfig<T> &gconf_, const complex<T> *grid_)
      : Helper<T>(gconf_), data(su*sv,T(0)), grid(grid_), p0(nullptr) {}
Martin Reinecke's avatar
import  
Martin Reinecke committed
655

Martin Reinecke's avatar
Martin Reinecke committed
656
    void prep_read(T u_in, T v_in)
Martin Reinecke's avatar
import  
Martin Reinecke committed
657
      {
Martin Reinecke's avatar
Martin Reinecke committed
658
659
      update(u_in, v_in);
      if (need_to_move())
Martin Reinecke's avatar
import  
Martin Reinecke committed
660
        {
Martin Reinecke's avatar
Martin Reinecke committed
661
        update_position();
Martin Reinecke's avatar
import  
Martin Reinecke committed
662
663
        load();
        }
Martin Reinecke's avatar
Martin Reinecke committed
664
      p0 = data.data() + sv*(iu0-bu0) + iv0-bv0;
Martin Reinecke's avatar
import  
Martin Reinecke committed
665
666
667
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
668
669
670
template<typename T> pyarr_c<T> vis2grid(const Baselines<T> &baselines,
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
  const pyarr_c<complex<T>> &vis_)
Martin Reinecke's avatar
import  
Martin Reinecke committed
671
  {
Martin Reinecke's avatar
Martin Reinecke committed
672
  myassert(idx_.ndim()==1, "idx array must be 1D");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
673
674
675
676
  myassert(vis_.ndim()==1, "vis must be 1D");
  auto vis=vis_.data();
  myassert(vis_.shape(0)==idx_.shape(0), "bad vis dimension");
  size_t nvis = size_t(idx_.shape(0));
Martin Reinecke's avatar
Martin Reinecke committed
677
678
679
  auto idx = idx_.data();

  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
Martin Reinecke committed
680
  auto res = makearray<complex<T>>({nu, nv});
Martin Reinecke's avatar
import  
Martin Reinecke committed
681
  auto grid = res.mutable_data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
682
  for (size_t i=0; i<nu*nv; ++i) grid[i] = 0.;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
683
684
  T beta = gconf.Beta();
  size_t w = gconf.W();
Martin Reinecke's avatar
import  
Martin Reinecke committed
685
686
687

#pragma omp parallel
{
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
688
689
  WriteHelper<T> hlp(gconf, grid);
  T emb = exp(-2*beta);
Martin Reinecke's avatar
Martin Reinecke committed
690
  const T * RESTRICT ku = hlp.kernel.data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
691
  const T * RESTRICT kv = hlp.kernel.data()+w;
Martin Reinecke's avatar
import  
Martin Reinecke committed
692
693

  // Loop over sampling points
Martin Reinecke's avatar
Martin Reinecke committed
694
#pragma omp for schedule(guided,100)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
695
  for (size_t ipart=0; ipart<nvis; ++ipart)
Martin Reinecke's avatar
import  
Martin Reinecke committed
696
    {
Martin Reinecke's avatar
Martin Reinecke committed
697
    UVW<T> coord = baselines.effectiveCoord(idx[ipart]);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
698
    hlp.prep_write(coord.u, coord.v);
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
699
    auto * RESTRICT ptr = hlp.p0;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
700
    auto v(vis[ipart]*emb);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
701
    for (size_t cu=0; cu<w; ++cu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
702
      {
Martin Reinecke's avatar
Martin Reinecke committed
703
      complex<T> tmp(v*ku[cu]);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
704
      for (size_t cv=0; cv<w; ++cv)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
705
706
        ptr[cv] += tmp*kv[cv];
      ptr+=hlp.sv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
707
708
709
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
710
  return complex2hartley(res);
Martin Reinecke's avatar
import  
Martin Reinecke committed
711
  }
Martin Reinecke's avatar
Martin Reinecke committed
712

713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
template<typename T> pyarr_c<complex<T>> vis2grid_c(const Baselines<T> &baselines,
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
  const pyarr_c<complex<T>> &vis_)
  {
  myassert(idx_.ndim()==1, "idx array must be 1D");
  myassert(vis_.ndim()==1, "vis must be 1D");
  auto vis=vis_.data();
  myassert(vis_.shape(0)==idx_.shape(0), "bad vis dimension");
  size_t nvis = size_t(idx_.shape(0));
  auto idx = idx_.data();

  size_t nu=gconf.Nu(), nv=gconf.Nv();
  auto res = makearray<complex<T>>({nu, nv});
  auto grid = res.mutable_data();
  for (size_t i=0; i<nu*nv; ++i) grid[i] = 0.;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
728
729
  T beta = gconf.Beta();
  size_t w = gconf.W();
730
731
732

#pragma omp parallel
{
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
733
734
  WriteHelper<T> hlp(gconf, grid);
  T emb = exp(-2*beta);
735
  const T * RESTRICT ku = hlp.kernel.data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
736
  const T * RESTRICT kv = hlp.kernel.data()+w;
737
738
739
740
741
742

  // Loop over sampling points
#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
    UVW<T> coord = baselines.effectiveCoord(idx[ipart]);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
743
    hlp.prep_write(coord.u, coord.v);
744
745
    auto * RESTRICT ptr = hlp.p0;
    auto v(vis[ipart]*emb);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
746
    for (size_t cu=0; cu<w; ++cu)
747
748
      {
      complex<T> tmp(v*ku[cu]);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
749
      for (size_t cv=0; cv<w; ++cv)
750
751
752
753
754
755
756
757
        ptr[cv] += tmp*kv[cv];
      ptr+=hlp.sv;
      }
    }
} // end of parallel region
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
758
759
760
template<typename T> pyarr_c<complex<T>> grid2vis(const Baselines<T> &baselines,
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
  const pyarr_c<T> &grid0_)
Martin Reinecke's avatar
Martin Reinecke committed
761
  {
Martin Reinecke's avatar
Martin Reinecke committed
762
763
  size_t nu=gconf.Nu(), nv=gconf.Nv();
  myassert(idx_.ndim()==1, "idx array must be 1D");
Martin Reinecke's avatar
Martin Reinecke committed
764
  auto grid_ = hartley2complex(grid0_);
Martin Reinecke's avatar
import  
Martin Reinecke committed
765
  auto grid = grid_.data();
Martin Reinecke's avatar
Martin Reinecke committed
766
  myassert(grid_.ndim()==2, "data must be 2D");
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
767
768
  myassert(grid_.shape(0)==int(nu), "bad grid dimension");
  myassert(grid_.shape(1)==int(nv), "bad grid dimension");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
769
  size_t nvis = size_t(idx_.shape(0));
Martin Reinecke's avatar
Martin Reinecke committed
770
  auto idx = idx_.data();
Martin Reinecke's avatar
import  
Martin Reinecke committed
771

Martin Reinecke's avatar
Martin Reinecke committed
772
  auto res = makearray<complex<T>>({nvis});
Martin Reinecke's avatar
import  
Martin Reinecke committed
773
  auto vis = res.mutable_data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
774
775
  T beta = gconf.Beta();
  size_t w = gconf.W();
Martin Reinecke's avatar
import  
Martin Reinecke committed
776
777
778
779

  // Loop over sampling points
#pragma omp parallel
{
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
780
781
  ReadHelper<T> hlp(gconf, grid);
  T emb = exp(-2*beta);
Martin Reinecke's avatar
Martin Reinecke committed
782
  const T * RESTRICT ku = hlp.kernel.data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
783
  const T * RESTRICT kv = hlp.kernel.data()+w;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
784

Martin Reinecke's avatar
Martin Reinecke committed
785
#pragma omp for schedule(guided,100)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
786
  for (size_t ipart=0; ipart<nvis; ++ipart)
Martin Reinecke's avatar
import  
Martin Reinecke committed
787
    {
Martin Reinecke's avatar
Martin Reinecke committed
788
    UVW<T> coord = baselines.effectiveCoord(idx[ipart]);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
789
    hlp.prep_read(coord.u, coord.v);
Martin Reinecke's avatar
Martin Reinecke committed
790
    complex<T> r = 0;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
791
    auto * RESTRICT ptr = hlp.p0;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
792
    for (size_t cu=0; cu<w; ++cu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
793
      {
Martin Reinecke's avatar
Martin Reinecke committed
794
      complex<T> tmp(0);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
795
      for (size_t cv=0; cv<w; ++cv)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
796
797
798
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
      ptr += hlp.sv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
799
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
800
    vis[ipart] = r*emb;
Martin Reinecke's avatar
import  
Martin Reinecke committed
801
802
803
804
    }
}
  return res;
  }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
805

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
template<typename T> pyarr_c<complex<T>> grid2vis_c(const Baselines<T> &baselines,
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
  const pyarr_c<complex<T>> &grid_)
  {
  size_t nu=gconf.Nu(), nv=gconf.Nv();
  myassert(idx_.ndim()==1, "idx array must be 1D");
  auto grid = grid_.data();
  myassert(grid_.ndim()==2, "data must be 2D");
  myassert(grid_.shape(0)==int(nu), "bad grid dimension");
  myassert(grid_.shape(1)==int(nv), "bad grid dimension");
  size_t nvis = size_t(idx_.shape(0));
  auto idx = idx_.data();

  auto res = makearray<complex<T>>({nvis});
  auto vis = res.mutable_data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
821
822
  T beta = gconf.Beta();
  size_t w = gconf.W();
823
824
825
826

  // Loop over sampling points
#pragma omp parallel
{
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
827
828
  ReadHelper<T> hlp(gconf, grid);
  T emb = exp(-2*beta);
829
  const T * RESTRICT ku = hlp.kernel.data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
830
  const T * RESTRICT kv = hlp.kernel.data()+w;
831
832
833
834
835

#pragma omp for schedule(guided,100)
  for (size_t ipart=0; ipart<nvis; ++ipart)
    {
    UVW<T> coord = baselines.effectiveCoord(idx[ipart]);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
836
    hlp.prep_read(coord.u, coord.v);
837
838
    complex<T> r = 0;
    auto * RESTRICT ptr = hlp.p0;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
839
    for (size_t cu=0; cu<w; ++cu)
840
841
      {
      complex<T> tmp(0);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
842
      for (size_t cv=0; cv<w; ++cv)
843
844
845
846
847
848
849
850
851
852
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
      ptr += hlp.sv;
      }
    vis[ipart] = r*emb;
    }
}
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
853
template<typename T> pyarr_c<uint32_t> getIndices(const Baselines<T> &baselines,
854
855
  const GridderConfig<T> &gconf, const pyarr_c<bool> &flags_, int chbegin,
  int chend, T wmin, T wmax)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
856
  {
857
858
  size_t nrow=baselines.Nrows(),
         nchan=baselines.Nchannels(),
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
859
         nsafe=gconf.Nsafe();
860
861
862
863
864
865
866
867
868
869
870
871
872
873
  if (chbegin<0) chbegin=0;
  if (chend<0) chend=nchan;
  myassert(flags_.ndim()==2, "flags must be 2D");
  myassert(size_t(flags_.shape(0))==nrow, "bad flags dimension");
  myassert(size_t(flags_.shape(1))==nchan, "bad flags dimension");
  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;
  vector<uint32_t> bincnt(nbu*nbv, 0);
  for (size_t irow=0; irow<nrow; ++irow)
    for (int ichan=chbegin; ichan<chend; ++ichan)
      if (!flags[irow*nchan + ichan])
        {
874
        auto uvw = baselines.effectiveCoord(irow, ichan);
875
876
        if ((uvw.w>=wmin) && (uvw.w<wmax))
          {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
877
878
879
880
881
          T u, v;
          int iu0, iv0;
          gconf.getpix(uvw.u, uvw.v, u, v, iu0, iv0);
          iu0 = (iu0+nsafe)>>logsquare;
          iv0 = (iv0+nsafe)>>logsquare;
882
883
884
885
886
887
888
889
          ++bincnt[nbv*iu0 + iv0];
          }
        }
  vector<uint32_t> acc(bincnt.size()+1);
  acc[0] = 0;
  for (size_t i=0; i<bincnt.size(); ++i)
    acc[i+1] = acc[i] + bincnt[i];
  auto res = makearray<uint32_t>({acc.back()});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
890
  auto iout = res.mutable_data();
891
892
893
894
  for (size_t irow=0; irow<nrow; ++irow)
    for (int ichan=chbegin; ichan<chend; ++ichan)
      if (!flags[irow*nchan + ichan])
        {
895
        auto uvw = baselines.effectiveCoord(irow, ichan);
896
897
        if ((uvw.w>=wmin) && (uvw.w<wmax))
          {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
898
899
900
901
902
          T u, v;
          int iu0, iv0;
          gconf.getpix(uvw.u, uvw.v, u, v, iu0, iv0);
          iu0 = (iu0+nsafe)>>logsquare;
          iv0 = (iv0+nsafe)>>logsquare;
903
          iout[acc[nbv*iu0 + iv0]] = irow*nchan + ichan;
904
905
906
          ++acc[nbv*iu0 + iv0];
          }
        }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
907
908
909
910
911
912
913
914
  return res;
  }

const char *Baselines_DS = R"""(
Class storing UVW coordinates and channel information.

Parameters
==========
Martin Reinecke's avatar
Martin Reinecke committed
915
916
coord: np.array((nrows, 3), dtype=np.float)
    u, v and w coordinates for each row
Martin Reinecke's avatar
updates    
Martin Reinecke committed
917
scaling: np.array((nchannels,), dtype=np.float)
Martin Reinecke's avatar
Martin Reinecke committed
918
    scaling factor for u, v, w for each individual channel
Martin Reinecke's avatar
updates    
Martin Reinecke committed
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
)""";

const char *BL_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
)""";

const char *BL_vis2ms_DS = R"""(
Martin Reinecke's avatar
Martin Reinecke committed
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
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

Returns
=======
np.array((nrows, nchannels), dtype=np.complex)
    the measurement set's visibility data (0 where not covered by idx)
)""";

const char *BL_add_vis_to_ms_DS = R"""(
Adds a set of visibilities to an existing MS.

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: np.array((nrows, nchannels), dtype=np.complex)
    a MS

Returns
=======
np.array((nrows, nchannels), dtype=np.complex)
    the updated MS
Martin Reinecke's avatar
updates    
Martin Reinecke committed
969
970
)""";

Martin Reinecke's avatar
Martin Reinecke committed
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
const char *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.
urange: float
vrange: float
)""";

const char *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
)""";

const char *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
)""";

const char *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)
1025
1026
flags: np.array((nrows, nchannels), dtype=np.bool)
    "True" indicates that the value should not be used
Martin Reinecke's avatar
Martin Reinecke committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
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.
)""";

const char *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

Returns
=======
np.array((nu,nv), dtype=np.float64):
    the gridded visibilities (made real by making use of Hermitian symmetry)
)""";

const char *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)
vis: np.array((nvis,), dtype=np.complex)
    The visibility data for the index array

Returns
=======
np.array((nvis,), dtype=np.complex)
    The degridded visibility data
)""";
Martin Reinecke's avatar
import  
Martin Reinecke committed
1086
1087
1088
1089
} // unnamed namespace

PYBIND11_MODULE(nifty_gridder, m)
  {
1090
1091
  using namespace pybind11::literals;

Martin Reinecke's avatar
Martin Reinecke committed
1092
  py::class_<Baselines<double>> (m, "Baselines", Baselines_DS)
1093
1094
    .def(py::init<const pyarr_c<double> &, const pyarr_c<double> &>(),
      "coord"_a, "scaling"_a)
Martin Reinecke's avatar
Martin Reinecke committed
1095
1096
    .def ("Nrows",&Baselines<double>::Nrows)
    .def ("Nchannels",&Baselines<double>::Nchannels)
Martin Reinecke's avatar
Martin Reinecke committed
1097
    .def ("ms2vis",&Baselines<double>::ms2vis<complex<double>>, BL_ms2vis_DS, "ms"_a, "idx"_a)
1098
//    .def ("ms2vis_f32",&Baselines<double>::ms2vis<float>, "ms"_a, "idx"_a)
Martin Reinecke's avatar
Martin Reinecke committed
1099
1100
    .def ("vis2ms",&Baselines<double>::vis2ms<complex<double>>, BL_vis2ms_DS, "vis"_a, "idx"_a)
    .def ("add_vis_to_ms",&Baselines<double>::add_vis_to_ms<complex<double>>, BL_add_vis_to_ms_DS,
Martin Reinecke's avatar
Martin Reinecke committed
1101
      "vis"_a, "idx"_a, "ms"_a.noconvert());
Martin Reinecke's avatar
Martin Reinecke committed
1102
  py::class_<GridderConfig<double>> (m, "GridderConfig", GridderConfig_DS)
1103
    .def(py::init<size_t, size_t, double, double, double>(),"nxdirty"_a,
Martin Reinecke's avatar
Martin Reinecke committed
1104
      "nydirty"_a, "epsilon"_a, "urange"_a, "vrange"_a)
Martin Reinecke's avatar
Martin Reinecke committed
1105
1106
    .def("Nu", &GridderConfig<double>::Nu)
    .def("Nv", &GridderConfig<double>::Nv)
Martin Reinecke's avatar
Martin Reinecke committed
1107
    .def("grid2dirty", &GridderConfig<double>::grid2dirty, grid2dirty_DS, "grid"_a)
1108
1109
1110
    .def("grid2dirty_c", &GridderConfig<double>::grid2dirty_c, "grid"_a)
    .def("dirty2grid", &GridderConfig<double>::dirty2grid, dirty2grid_DS, "dirty"_a)
    .def("dirty2grid_c", &GridderConfig<double>::dirty2grid_c, "dirty"_a);
1111
1112
  m.def("getIndices", getIndices<double>, getIndices_DS, "baselines"_a, "gconf"_a,
    "flags"_a, "chbegin"_a=-1, "chend"_a=-1, "wmin"_a=-1e30, "wmax"_a=1e30);
Martin Reinecke's avatar
Martin Reinecke committed
1113
1114
  m.def("vis2grid",&vis2grid<double>, vis2grid_DS, "baselines"_a, "gconf"_a, "idx"_a, "vis"_a);
  m.def("grid2vis",&grid2vis<double>, grid2vis_DS, "baselines"_a, "gconf"_a, "idx"_a, "grid"_a);
1115
1116
  m.def("vis2grid_c",&vis2grid_c<double>, "baselines"_a, "gconf"_a, "idx"_a, "vis"_a);
  m.def("grid2vis_c",&grid2vis_c<double>, "baselines"_a, "gconf"_a, "idx"_a, "grid"_a);
Martin Reinecke's avatar
Martin Reinecke committed
1117

1118
#if 0
Martin Reinecke's avatar
Martin Reinecke committed
1119
  py::class_<Baselines<float>> (m, "Baselines_f", Baselines_DS)
1120
1121
    .def(py::init<const pyarr_c<float> &, const pyarr_c<float> &>(),
      "coord"_a, "scaling"_a)
Martin Reinecke's avatar
Martin Reinecke committed
1122
1123
    .def ("Nrows",&Baselines<float>::Nrows)
    .def ("Nchannels",&Baselines<float>::Nchannels)
Martin Reinecke's avatar
Martin Reinecke committed
1124
1125
1126
    .def ("ms2vis",&Baselines<float>::ms2vis<complex<float>>, BL_ms2vis_DS, "ms"_a, "idx"_a)
    .def ("vis2ms",&Baselines<float>::vis2ms<complex<float>>, BL_vis2ms_DS, "vis"_a, "idx"_a)
    .def ("add_vis_to_ms",&Baselines<float>::add_vis_to_ms<complex<float>>, BL_add_vis_to_ms_DS,
Martin Reinecke's avatar
Martin Reinecke committed
1127
1128
1129
1130
1131
1132
1133
1134
      "vis"_a, "idx"_a, "ms"_a.noconvert());
  py::class_<GridderConfig<float>> (m, "GridderConfig_f")
    .def(py::init<size_t, size_t, float, float, float>(),"nxdirty"_a,
      "nydirty"_a, "epsilon"_a, "urange"_a, "vrange"_a)
    .def("Nu", &GridderConfig<float>::Nu)
    .def("Nv", &GridderConfig<float>::Nv)
    .def("grid2dirty", &GridderConfig<float>::grid2dirty, "grid"_a)
    .def("dirty2grid", &GridderConfig<float>::dirty2grid, "dirty"_a);
1135
1136
  m.def("getIndices_f", getIndices<float>, "baselines"_a, "gconf"_a,
    "flags"_a, "chbegin"_a=-1, "chend"_a=-1, "wmin"_a=-1e30, "wmax"_a=1e30);
Martin Reinecke's avatar
Martin Reinecke committed
1137
1138
  m.def("vis2grid_f",&vis2grid<float>, "baselines"_a, "gconf"_a, "idx"_a, "vis"_a);
  m.def("grid2vis_f",&grid2vis<float>, "baselines"_a, "gconf"_a, "idx"_a, "grid"_a);
1139
#endif
Martin Reinecke's avatar
import  
Martin Reinecke committed
1140
  }