nifty_gridder.cc 32.9 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
merge    
Martin Reinecke committed
134
template<typename T> pyarr_c<T> makeArray(const vector<size_t> &shape)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
135
136
  { 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
merge    
Martin Reinecke committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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");
      }
  }

template<typename T> pyarr_c<T> provideArray(py::object &in,
  const vector<size_t> &shape)
  {
  if (in.is(py::none()))
    {
    auto tmp_ = makeArray<T>(shape);
    size_t sz = size_t(tmp_.size());
    auto tmp = tmp_.mutable_data();
    for (size_t i=0; i<sz; ++i)
      tmp[i] = T(0);
    return tmp_;
    }
  auto tmp_ = in.cast<pyarr_c<T>>();
  checkArray(tmp_, "temporary", shape);
  return tmp_;
  }

Martin Reinecke's avatar
Martin Reinecke committed
185
186
template<typename T> pyarr_c<T> complex2hartley
  (const pyarr_c<complex<T>> &grid_)
Martin Reinecke's avatar
Martin Reinecke committed
187
  {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
188
  checkArray(grid_, "grid", {0,0});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
189
  size_t nu = size_t(grid_.shape(0)), nv = size_t(grid_.shape(1));
Martin Reinecke's avatar
Martin Reinecke committed
190
191
  auto grid = grid_.data();

Martin Reinecke's avatar
merge    
Martin Reinecke committed
192
  auto res = makeArray<T>({nu,nv});
Martin Reinecke's avatar
Martin Reinecke committed
193
  auto grid2 = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
194
195
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
196
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
197
  for (size_t u=0; u<nu; ++u)
Martin Reinecke's avatar
Martin Reinecke committed
198
    {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
199
200
    size_t xu = (u==0) ? 0 : nu-u;
    for (size_t v=0; v<nv; ++v)
Martin Reinecke's avatar
Martin Reinecke committed
201
      {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
202
203
204
      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
205
206
      grid2[i1] = T(0.5)*(grid[i1].real()+grid[i1].imag()+
                          grid[i2].real()-grid[i2].imag());
Martin Reinecke's avatar
Martin Reinecke committed
207
208
      }
    }
Martin Reinecke's avatar
Martin Reinecke committed
209
  }
Martin Reinecke's avatar
Martin Reinecke committed
210
211
212
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
213
214
template<typename T> pyarr_c<complex<T>> hartley2complex
  (const pyarr_c<T> &grid_)
Martin Reinecke's avatar
Martin Reinecke committed
215
  {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
216
  checkArray(grid_, "grid", {0, 0});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
217
  size_t nu = size_t(grid_.shape(0)), nv = size_t(grid_.shape(1));
Martin Reinecke's avatar
Martin Reinecke committed
218
219
  auto grid = grid_.data();

Martin Reinecke's avatar
merge    
Martin Reinecke committed
220
  auto res=makeArray<complex<T>>({nu, nv});
Martin Reinecke's avatar
Martin Reinecke committed
221
  auto grid2 = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
222
223
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
224
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
225
  for (size_t u=0; u<nu; ++u)
Martin Reinecke's avatar
Martin Reinecke committed
226
    {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
227
228
    size_t xu = (u==0) ? 0 : nu-u;
    for (size_t v=0; v<nv; ++v)
Martin Reinecke's avatar
Martin Reinecke committed
229
      {
Martin Reinecke's avatar
updates    
Martin Reinecke committed
230
231
232
      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
233
234
235
      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
236
237
      }
    }
Martin Reinecke's avatar
Martin Reinecke committed
238
  }
Martin Reinecke's avatar
Martin Reinecke committed
239
240
241
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
242
243
244
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);
Martin Reinecke's avatar
Martin Reinecke committed
245
246
247
  pocketfft::stride_t s_i{in.strides(0), in.strides(1)},
                      s_o{out.strides(0), out.strides(1)};
  auto d_i = in.data();
Martin Reinecke's avatar
Martin Reinecke committed
248
  auto ptmp = out.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
249
250
251
  {
  py::gil_scoped_release release;
  pocketfft::r2r_hartley({nu, nv}, s_i, s_o, {0,1}, d_i, ptmp, T(1), 0);
Martin Reinecke's avatar
Martin Reinecke committed
252
#pragma omp parallel for
Martin Reinecke's avatar
Martin Reinecke committed
253
254
255
256
257
258
259
260
261
262
263
264
265
  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
266
  }
Martin Reinecke's avatar
Martin Reinecke committed
267

Martin Reinecke's avatar
Martin Reinecke committed
268
269
/* 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
270
vector<double> correction_factors (size_t n, size_t nval, size_t w)
Martin Reinecke's avatar
Martin Reinecke committed
271
272
273
274
275
276
277
278
279
280
  {
  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
281
  vector<double> res(nval);
Martin Reinecke's avatar
Martin Reinecke committed
282
#pragma omp parallel for schedule(static)
Martin Reinecke's avatar
Martin Reinecke committed
283
284
285
286
287
  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
288
    res[k] = 1./(w*tmp);
Martin Reinecke's avatar
Martin Reinecke committed
289
290
291
292
    }
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
293
template<typename T> struct UVW
Martin Reinecke's avatar
updates    
Martin Reinecke committed
294
  {
Martin Reinecke's avatar
Martin Reinecke committed
295
  T u, v, w;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
296
  UVW () {}
Martin Reinecke's avatar
Martin Reinecke committed
297
298
  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
299
300
    { return UVW(u*fct, v*fct, w*fct); }
  };
Martin Reinecke's avatar
Martin Reinecke committed
301

Martin Reinecke's avatar
Martin Reinecke committed
302
template<typename T> class Baselines
Martin Reinecke's avatar
Martin Reinecke committed
303
304
  {
  private:
Martin Reinecke's avatar
Martin Reinecke committed
305
    vector<UVW<T>> coord;
306
    vector<T> f_over_c;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
307
    size_t nrows, nchan;
Martin Reinecke's avatar
Martin Reinecke committed
308
309

  public:
310
    Baselines(const pyarr_c<T> &coord_, const pyarr_c<T> &freq_)
Martin Reinecke's avatar
Martin Reinecke committed
311
      {
312
      constexpr double speedOfLight = 299792458.;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
313
      checkArray(coord_, "coord", {0, 3});
314
      checkArray(freq_, "freq", {0});
Martin Reinecke's avatar
Martin Reinecke committed
315
      nrows = coord_.shape(0);
316
      nchan = freq_.shape(0);
Martin Reinecke's avatar
Martin Reinecke committed
317
318
319
320
321
      myassert(nrows*nchan<(size_t(1)<<32), "too many entries in MS");
      auto freq = freq_.data();
      auto cood = coord_.data();
      {
      py::gil_scoped_release release;
322
      f_over_c.resize(nchan);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
323
      for (size_t i=0; i<nchan; ++i)
Martin Reinecke's avatar
Martin Reinecke committed
324
        f_over_c[i] = freq[i]/speedOfLight;
Martin Reinecke's avatar
Martin Reinecke committed
325
326
      coord.resize(nrows);
      for (size_t i=0; i<coord.size(); ++i)
Martin Reinecke's avatar
Martin Reinecke committed
327
        coord[i] = UVW<T>(cood[3*i], cood[3*i+1], cood[3*i+2]);
Martin Reinecke's avatar
Martin Reinecke committed
328
      }
Martin Reinecke's avatar
Martin Reinecke committed
329
330
      }

Martin Reinecke's avatar
Martin Reinecke committed
331
    UVW<T> effectiveCoord(uint32_t index) const
332
333
334
      {
      size_t irow = index/nchan;
      size_t ichan = index-nchan*irow;
335
      return coord[irow]*f_over_c[ichan];
336
337
      }
    UVW<T> effectiveCoord(size_t irow, size_t ichan) const
338
      { return coord[irow]*f_over_c[ichan]; }
Martin Reinecke's avatar
Martin Reinecke committed
339
    size_t Nrows() const { return nrows; }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
340
341
    size_t Nchannels() const { return nchan; }

Martin Reinecke's avatar
Martin Reinecke committed
342
    template<typename T2> pyarr_c<T2> ms2vis(const pyarr_c<T2> &ms_,
Martin Reinecke's avatar
updates    
Martin Reinecke committed
343
344
      const pyarr_c<uint32_t> &idx_) const
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
345
346
      checkArray(idx_, "idx", {0});
      checkArray(ms_, "ms", {nrows, nchan});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
347
348
349
350
      size_t nvis = size_t(idx_.shape(0));
      auto idx = idx_.data();
      auto ms = ms_.data();

Martin Reinecke's avatar
merge    
Martin Reinecke committed
351
      auto res=makeArray<T2>({nvis});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
352
      auto vis = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
353
354
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
355
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
356
      for (size_t i=0; i<nvis; ++i)
357
        vis[i] = ms[idx[i]];
Martin Reinecke's avatar
Martin Reinecke committed
358
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
359
360
361
      return res;
      }

Martin Reinecke's avatar
Martin Reinecke committed
362
    template<typename T2> pyarr_c<T2> vis2ms(const pyarr_c<T2> &vis_,
Martin Reinecke's avatar
merge    
Martin Reinecke committed
363
      const pyarr_c<uint32_t> &idx_, py::object &ms_in) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
364
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
365
      checkArray(vis_, "vis", {0});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
366
      size_t nvis = size_t(vis_.shape(0));
Martin Reinecke's avatar
merge    
Martin Reinecke committed
367
      checkArray(idx_, "idx", {nvis});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
368
369
370
      auto idx = idx_.data();
      auto vis = vis_.data();

Martin Reinecke's avatar
merge    
Martin Reinecke committed
371
      auto res = provideArray<T2>(ms_in, {nrows, nchan});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
372
      auto ms = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
373
374
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
375
#pragma omp parallel for
Martin Reinecke's avatar
updates    
Martin Reinecke committed
376
      for (size_t i=0; i<nvis; ++i)
377
        ms[idx[i]] = vis[i];
Martin Reinecke's avatar
Martin Reinecke committed
378
      }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
379
380
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
381
382
  };

Martin Reinecke's avatar
Martin Reinecke committed
383
384
constexpr int logsquare=4;

Martin Reinecke's avatar
Martin Reinecke committed
385
template<typename T> class GridderConfig
Martin Reinecke's avatar
Martin Reinecke committed
386
387
388
  {
  private:
    size_t nx_dirty, ny_dirty;
Martin Reinecke's avatar
Martin Reinecke committed
389
    double eps, psx, psy;
Martin Reinecke's avatar
Martin Reinecke committed
390
    size_t w, nsafe, nu, nv;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
391
    T beta;
Martin Reinecke's avatar
Martin Reinecke committed
392
    vector<T> cfu, cfv;
Martin Reinecke's avatar
Martin Reinecke committed
393
394
395

  public:
    GridderConfig(size_t nxdirty, size_t nydirty, double epsilon,
Martin Reinecke's avatar
Martin Reinecke committed
396
      double pixsize_x, double pixsize_y)
Martin Reinecke's avatar
Martin Reinecke committed
397
398
      : nx_dirty(nxdirty), ny_dirty(nydirty), eps(epsilon),
        psx(pixsize_x), psy(pixsize_y),
Martin Reinecke's avatar
Martin Reinecke committed
399
400
        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
401
        beta(2.3*w),
Martin Reinecke's avatar
updates    
Martin Reinecke committed
402
        cfu(nx_dirty), cfv(ny_dirty)
Martin Reinecke's avatar
Martin Reinecke committed
403
      {
Martin Reinecke's avatar
Martin Reinecke committed
404
405
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
406
407
408
      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");
Martin Reinecke's avatar
Martin Reinecke committed
409
410
      myassert(pixsize_x>0, "pixsize_x must be positive");
      myassert(pixsize_y>0, "pixsize_y must be positive");
Martin Reinecke's avatar
updates    
Martin Reinecke committed
411
412
413
414
415
416
417
418
419
420
421

      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
422
      }
Martin Reinecke's avatar
Martin Reinecke committed
423
      }
Martin Reinecke's avatar
Martin Reinecke committed
424
425
426
427
428
    size_t Nxdirty() const { return nx_dirty; }
    size_t Nydirty() const { return ny_dirty; }
    double Epsilon() const { return eps; }
    double Pixsize_x() const { return psx; }
    double Pixsize_y() const { return psy; }
Martin Reinecke's avatar
Martin Reinecke committed
429
430
431
    size_t Nu() const { return nu; }
    size_t Nv() const { return nv; }
    size_t W() const { return w; }
432
    size_t Nsafe() const { return nsafe; }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
433
    T Beta() const { return beta; }
Martin Reinecke's avatar
Martin Reinecke committed
434
    pyarr_c<T> grid2dirty(const pyarr_c<T> &grid) const
Martin Reinecke's avatar
Martin Reinecke committed
435
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
436
437
      checkArray(grid, "grid", {nu, nv});
      auto tmp = makeArray<T>({nu, nv});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
438
      auto ptmp = tmp.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
439
      hartley2_2D<T>(grid, tmp);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
440
      auto res = makeArray<T>({nx_dirty, ny_dirty});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
441
      auto pout = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
442
443
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
444
445
446
447
448
449
450
451
452
      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
453
      }
Martin Reinecke's avatar
Martin Reinecke committed
454
455
      return res;
      }
456
457
    pyarr_c<complex<T>> grid2dirty_c(const pyarr_c<complex<T>> &grid) const
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
458
459
      checkArray(grid, "grid", {nu, nv});
      auto tmp = makeArray<complex<T>>({nu, nv});
460
461
462
463
      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);
Martin Reinecke's avatar
merge    
Martin Reinecke committed
464
      auto res = makeArray<complex<T>>({nx_dirty, ny_dirty});
465
      auto pout = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
466
467
      {
      py::gil_scoped_release release;
468
469
470
471
472
473
474
475
476
      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
477
      }
478
479
      return res;
      }
Martin Reinecke's avatar
Martin Reinecke committed
480
    pyarr_c<T> dirty2grid(const pyarr_c<T> &dirty) const
Martin Reinecke's avatar
updates    
Martin Reinecke committed
481
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
482
      checkArray(dirty, "dirty", {nx_dirty, ny_dirty});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
483
      auto pdirty = dirty.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
484
      auto tmp = makeArray<T>({nu, nv});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
485
      auto ptmp = tmp.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
486
487
      {
      py::gil_scoped_release release;
Martin Reinecke's avatar
updates    
Martin Reinecke committed
488
489
490
491
492
493
494
495
496
497
498
      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
499
      }
Martin Reinecke's avatar
Martin Reinecke committed
500
      hartley2_2D<T>(tmp, tmp);
Martin Reinecke's avatar
updates    
Martin Reinecke committed
501
502
      return tmp;
      }
503
504
    pyarr_c<complex<T>> dirty2grid_c(const pyarr_c<complex<T>> &dirty) const
      {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
505
      checkArray(dirty, "dirty", {nx_dirty, ny_dirty});
506
      auto pdirty = dirty.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
507
      auto tmp = makeArray<complex<T>>({nu, nv});
508
      auto ptmp = tmp.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
509
510
511
      pocketfft::stride_t strides{tmp.strides(0),tmp.strides(1)};
      {
      py::gil_scoped_release release;
512
513
514
515
516
517
518
519
520
521
522
      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
523
524
525
      pocketfft::c2c({nu,nv}, strides, strides, {0,1}, pocketfft::FORWARD,
        ptmp, ptmp, T(1), 0);
      }
526
527
      return tmp;
      }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
528
529
    inline void getpix(T u_in, T v_in, T &u, T &v, int &iu0, int &iv0) const
      {
Martin Reinecke's avatar
Martin Reinecke committed
530
      u=fmodulo(u_in*psx, T(1))*nu,
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
531
532
      iu0 = int(u-w*0.5 + 1 + nu) - nu;
      if (iu0+w>nu+nsafe) iu0 = nu+nsafe-w;
Martin Reinecke's avatar
Martin Reinecke committed
533
      v=fmodulo(v_in*psy, T(1))*nv;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
534
535
536
      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
537
538
  };

Martin Reinecke's avatar
Martin Reinecke committed
539
template<typename T> class Helper
Martin Reinecke's avatar
import  
Martin Reinecke committed
540
  {
Martin Reinecke's avatar
Martin Reinecke committed
541
  private:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
542
    const GridderConfig<T> &gconf;
Martin Reinecke's avatar
Martin Reinecke committed
543
544
    int nu, nv, nsafe, w;
    T beta;
Martin Reinecke's avatar
Martin Reinecke committed
545
546
    complex<T> *grid;
    bool write;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
547
    int su;
Martin Reinecke's avatar
Martin Reinecke committed
548
549
  public:
    int sv;
Martin Reinecke's avatar
Martin Reinecke committed
550
  private:
Martin Reinecke's avatar
Martin Reinecke committed
551
552
553
    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
554
    vector<complex<T>> data;
Martin Reinecke's avatar
import  
Martin Reinecke committed
555

Martin Reinecke's avatar
Martin Reinecke committed
556
    void dump() const
Martin Reinecke's avatar
import  
Martin Reinecke committed
557
      {
Martin Reinecke's avatar
Martin Reinecke committed
558
      if (bu0<-nsafe) return; // nothing written into buffer yet
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
559

Martin Reinecke's avatar
merge    
Martin Reinecke committed
560
#pragma omp critical (gridder_writing_to_grid)
Martin Reinecke's avatar
import  
Martin Reinecke committed
561
{
Martin Reinecke's avatar
Martin Reinecke committed
562
563
564
      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
565
566
        {
        int idxv = idxv0;
Martin Reinecke's avatar
Martin Reinecke committed
567
        for (int iv=0; iv<sv; ++iv)
Martin Reinecke's avatar
import  
Martin Reinecke committed
568
          {
Martin Reinecke's avatar
Martin Reinecke committed
569
          grid[idxu*nv + idxv] += data[iu*sv + iv];
Martin Reinecke's avatar
import  
Martin Reinecke committed
570
571
572
573
574
575
576
577
578
          if (++idxv>=nv) idxv=0;
          }
        if (++idxu>=nu) idxu=0;
        }
}
      }

    void load()
      {
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
          data[iu*sv + iv] = grid[idxu*nv + idxv];
Martin Reinecke's avatar
import  
Martin Reinecke committed
587
588
589
590
591
592
593
          if (++idxv>=nv) idxv=0;
          }
        if (++idxu>=nu) idxu=0;
        }
      }

  public:
Martin Reinecke's avatar
Martin Reinecke committed
594
595
    complex<T> *p0;
    vector<T> kernel;
Martin Reinecke's avatar
import  
Martin Reinecke committed
596

Martin Reinecke's avatar
Martin Reinecke committed
597
    Helper(const GridderConfig<T> &gconf_, const complex<T> *grid_, bool write_)
Martin Reinecke's avatar
Martin Reinecke committed
598
599
600
601
602
      : gconf(gconf_), nu(gconf.Nu()), nv(gconf.Nv()), nsafe(gconf.Nsafe()), w(gconf.W()),
        beta(gconf.Beta()),
        grid(const_cast<complex<T> *>(grid_)), write(write_),
        su(2*nsafe+(1<<logsquare)), sv(2*nsafe+(1<<logsquare)),
        bu0(-1000000), bv0(-1000000), data(su*sv,T(0)), kernel(2*w)
Martin Reinecke's avatar
Martin Reinecke committed
603
604
605
606
      {}
    ~Helper() { if (write) dump(); }

    void prep(T u_in, T v_in)
Martin Reinecke's avatar
import  
Martin Reinecke committed
607
      {
Martin Reinecke's avatar
Martin Reinecke committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
      T u, v;
      gconf.getpix(u_in, v_in, u, v, iu0, iv0);
      T xw=T(2)/w;
      auto x0 = xw*(iu0-u);
      auto y0 = xw*(iv0-v);
      for (int i=0; i<w; ++i)
        {
        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);
        }
      for (auto &k : kernel)
        k = exp(k);

      if ((iu0<bu0) || (iv0<bv0) || (iu0+w>bu0+su) || (iv0+w>bv0+sv))
Martin Reinecke's avatar
import  
Martin Reinecke committed
624
        {
Martin Reinecke's avatar
Martin Reinecke committed
625
        if (write) { dump(); fill(data.begin(), data.end(), T(0)); }
Martin Reinecke's avatar
Martin Reinecke committed
626
627
        bu0=((((iu0+nsafe)>>logsquare)<<logsquare))-nsafe;
        bv0=((((iv0+nsafe)>>logsquare)<<logsquare))-nsafe;
Martin Reinecke's avatar
Martin Reinecke committed
628
        if (!write) load();
Martin Reinecke's avatar
import  
Martin Reinecke committed
629
        }
Martin Reinecke's avatar
Martin Reinecke committed
630
      p0 = data.data() + sv*(iu0-bu0) + iv0-bv0;
Martin Reinecke's avatar
import  
Martin Reinecke committed
631
632
633
      }
  };

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
634
635
template<typename T> pyarr_c<complex<T>> vis2grid_c(
  const Baselines<T> &baselines, const GridderConfig<T> &gconf,
636
  const pyarr_c<uint32_t> &idx_, const pyarr_c<complex<T>> &vis_,
637
  py::object user_grid)
638
  {
Martin Reinecke's avatar
merge    
Martin Reinecke committed
639
640
  checkArray(vis_, "vis", {0});
  size_t nvis = size_t(vis_.shape(0));
641
  size_t nu=gconf.Nu(), nv=gconf.Nv();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
642
  checkArray(idx_, "idx", {nvis});
643

644
645
646
  auto vis=vis_.data();
  auto idx = idx_.data();

647
648
  auto res = !user_grid.is(py::none()) ? user_grid.cast<pyarr_c<complex<T>>>()
                                       : makeArray<complex<T>>({nu, nv});
649
  auto grid = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
650
651
  {
  py::gil_scoped_release release;
652
  for (size_t i=0; i<nu*nv; ++i) grid[i] = 0.;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
653
654
  T beta = gconf.Beta();
  size_t w = gconf.W();
655
656
657

#pragma omp parallel
{
Martin Reinecke's avatar
Martin Reinecke committed
658
  Helper<T> hlp(gconf, grid, true);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
659
  T emb = exp(-2*beta);
660
  const T * RESTRICT ku = hlp.kernel.data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
661
  const T * RESTRICT kv = hlp.kernel.data()+w;
662
663
664
665
666
667

  // 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
Martin Reinecke committed
668
    hlp.prep(coord.u, coord.v);
669
670
    auto * RESTRICT ptr = hlp.p0;
    auto v(vis[ipart]*emb);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
671
    for (size_t cu=0; cu<w; ++cu)
672
673
      {
      complex<T> tmp(v*ku[cu]);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
674
      for (size_t cv=0; cv<w; ++cv)
675
676
677
678
679
        ptr[cv] += tmp*kv[cv];
      ptr+=hlp.sv;
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
680
  }
681
682
683
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
684
template<typename T> pyarr_c<T> vis2grid(const Baselines<T> &baselines,
Martin Reinecke's avatar
Martin Reinecke committed
685
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
Simon Perkins's avatar
Simon Perkins committed
686
  const pyarr_c<complex<T>> &vis_, py::object user_grid)
687
688
  {
    return complex2hartley(vis2grid_c(baselines, gconf,
Simon Perkins's avatar
Simon Perkins committed
689
                                      idx_, vis_, user_grid));
690
  }
Martin Reinecke's avatar
updates    
Martin Reinecke committed
691

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
692
693
694
template<typename T> pyarr_c<complex<T>> ms2grid_c(
  const Baselines<T> &baselines, const GridderConfig<T> &gconf,
  const pyarr_c<uint32_t> &idx_, const pyarr_c<complex<T>> &ms_)
Martin Reinecke's avatar
merge    
Martin Reinecke committed
695
696
697
698
699
700
701
702
703
704
705
706
  {
  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));
  auto ms = ms_.data();
  auto idx = idx_.data();

  size_t nu=gconf.Nu(), nv=gconf.Nv();
  auto res = makeArray<complex<T>>({nu, nv});
  auto grid = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
707
708
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
  for (size_t i=0; i<nu*nv; ++i) grid[i] = 0.;
  T beta = gconf.Beta();
  size_t w = gconf.W();

#pragma omp parallel
{
  Helper<T> hlp(gconf, grid, true);
  T emb = exp(-2*beta);
  const T * RESTRICT ku = hlp.kernel.data();
  const T * RESTRICT kv = hlp.kernel.data()+w;

  // 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]);
    hlp.prep(coord.u, coord.v);
    auto * RESTRICT ptr = hlp.p0;
    auto v(ms[idx[ipart]]*emb);
    for (size_t cu=0; cu<w; ++cu)
      {
      complex<T> tmp(v*ku[cu]);
      for (size_t cv=0; cv<w; ++cv)
        ptr[cv] += tmp*kv[cv];
      ptr+=hlp.sv;
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
737
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
738
739
740
741
742
743
744
745
  return res;
  }

template<typename T> pyarr_c<T> ms2grid(const Baselines<T> &baselines,
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
  const pyarr_c<complex<T>> &ms_)
  { return complex2hartley(ms2grid_c(baselines, gconf, idx_, ms_)); }

746
747
748
749
750
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();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
751
  checkArray(idx_, "idx", {0});
752
  auto grid = grid_.data();
Martin Reinecke's avatar
merge    
Martin Reinecke committed
753
  checkArray(grid_, "grid", {nu, nv});
754
755
756
  size_t nvis = size_t(idx_.shape(0));
  auto idx = idx_.data();

Martin Reinecke's avatar
merge    
Martin Reinecke committed
757
  auto res = makeArray<complex<T>>({nvis});
758
  auto vis = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
759
760
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
761
762
  T beta = gconf.Beta();
  size_t w = gconf.W();
763
764
765
766

  // Loop over sampling points
#pragma omp parallel
{
Martin Reinecke's avatar
Martin Reinecke committed
767
  Helper<T> hlp(gconf, grid, false);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
768
  T emb = exp(-2*beta);
769
  const T * RESTRICT ku = hlp.kernel.data();
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
770
  const T * RESTRICT kv = hlp.kernel.data()+w;
771
772
773
774
775

#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
Martin Reinecke committed
776
    hlp.prep(coord.u, coord.v);
777
778
    complex<T> r = 0;
    auto * RESTRICT ptr = hlp.p0;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
779
    for (size_t cu=0; cu<w; ++cu)
780
781
      {
      complex<T> tmp(0);
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
782
      for (size_t cv=0; cv<w; ++cv)
783
784
785
786
787
788
789
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
      ptr += hlp.sv;
      }
    vis[ipart] = r*emb;
    }
}
Martin Reinecke's avatar
Martin Reinecke committed
790
  }
791
792
793
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
794
795
796
797
798
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> &grid_)
  { return grid2vis_c(baselines, gconf, idx_, hartley2complex(grid_)); }

Martin Reinecke's avatar
merge    
Martin Reinecke committed
799
800
801
802
803
804
805
806
807
808
809
810
811
812
template<typename T> pyarr_c<complex<T>> grid2ms_c(const Baselines<T> &baselines,
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
  const pyarr_c<complex<T>> &grid_, py::object &ms_in)
  {
  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));
  auto idx = idx_.data();

  auto res = provideArray<complex<T>>(ms_in,
    {baselines.Nrows(), baselines.Nchannels()});
  auto ms = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
813
814
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
merge    
Martin Reinecke committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  T beta = gconf.Beta();
  size_t w = gconf.W();

  // Loop over sampling points
#pragma omp parallel
{
  Helper<T> hlp(gconf, grid, false);
  T emb = exp(-2*beta);
  const T * RESTRICT ku = hlp.kernel.data();
  const T * RESTRICT kv = hlp.kernel.data()+w;

#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);
    complex<T> r = 0;
    auto * RESTRICT ptr = hlp.p0;
    for (size_t cu=0; cu<w; ++cu)
      {
      complex<T> tmp(0);
      for (size_t cv=0; cv<w; ++cv)
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
      ptr += hlp.sv;
      }
    ms[idx[ipart]] += r*emb;
    }
}
Martin Reinecke's avatar
Martin Reinecke committed
844
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
845
846
847
848
849
850
851
852
  return res;
  }

template<typename T> pyarr_c<complex<T>> grid2ms(const Baselines<T> &baselines,
  const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
  const pyarr_c<T> &grid_, py::object &ms_in)
  { return grid2ms_c(baselines, gconf, idx_, hartley2complex(grid_), ms_in); }

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
  if (chbegin<0) chbegin=0;
  if (chend<0) chend=nchan;
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
862
863
864
  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
865
  checkArray(flags_, "flags", {nrow, nchan});
866
867
868
869
  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
870
871
  vector<uint32_t> acc(nbu*nbv+1, 0);
  vector<uint32_t> tmp(nrow*(chend-chbegin));
Martin Reinecke's avatar
Martin Reinecke committed
872
873
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
874
  for (size_t irow=0, idx=0; irow<nrow; ++irow)
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
875
    for (int ichan=chbegin; ichan<chend; ++ichan)
Martin Reinecke's avatar
Martin Reinecke committed
876
      if (!flags[irow*nchan+ichan])
877
        {
878
        auto uvw = baselines.effectiveCoord(irow, ichan);
879
880
        if ((uvw.w>=wmin) && (uvw.w<wmax))
          {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
881
882
883
884
885
          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
886
          ++acc[nbv*iu0 + iv0 + 1];
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
887
          tmp[idx++] = nbv*iu0 + iv0;
888
889
          }
        }
Martin Reinecke's avatar
Martin Reinecke committed
890
891
892

  for (size_t i=1; i<acc.size(); ++i)
    acc[i] += acc[i-1];
Martin Reinecke's avatar
Martin Reinecke committed
893
  }
Martin Reinecke's avatar
merge    
Martin Reinecke committed
894
  auto res = makeArray<uint32_t>({acc.back()});
Martin Reinecke's avatar
updates    
Martin Reinecke committed
895
  auto iout = res.mutable_data();
Martin Reinecke's avatar
Martin Reinecke committed
896
897
  {
  py::gil_scoped_release release;
Martin Reinecke's avatar
Martin Reinecke committed
898
  for (size_t irow=0, idx=0; irow<nrow; ++irow)
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
899
    for (int ichan=chbegin; ichan<chend; ++ichan)
Martin Reinecke's avatar
Martin Reinecke committed
900
      if (!flags[irow*nchan+ichan])
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
901
902
903
904
905
        {
        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
906
  }
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
917
918
freq: np.array((nchannels,), dtype=np.float)
    frequency for each individual channel (in Hz)
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
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)
)""";

Martin Reinecke's avatar
Martin Reinecke committed
953
954
955
956
957
958
959
960
961
962
963
964
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.
Martin Reinecke's avatar
Martin Reinecke committed
965
966
967
968
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
969
970
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
)""";

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)
1009
1010
flags: np.array((nrows, nchannels), dtype=np.bool)
    "True" indicates that the value should not be used
Martin Reinecke's avatar
Martin Reinecke committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
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
Simon Perkins's avatar
Simon Perkins committed
1041
1042
1043
user_grid: np.array((nv, nu), dtype=np.complex128), optional
    User supplied grid which will be used instead of internally
    allocating an array. Defaults to None
Martin Reinecke's avatar
Martin Reinecke committed
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

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
1073
1074
1075
1076
} // unnamed namespace

PYBIND11_MODULE(nifty_gridder, m)
  {
1077
1078
  using namespace pybind11::literals;

Martin Reinecke's avatar
Martin Reinecke committed
1079
  py::class_<Baselines<double>> (m, "Baselines", Baselines_DS)
1080
    .def(py::init<const pyarr_c<double> &, const pyarr_c<double> &>(),
1081
      "coord"_a, "freq"_a)
Martin Reinecke's avatar
Martin Reinecke committed
1082
1083
    .def ("Nrows",&Baselines<double>::Nrows)
    .def ("Nchannels",&Baselines<double>::Nchannels)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
1084
1085
1086
1087
    .def ("ms2vis",&Baselines<double>::ms2vis<complex<double>>, BL_ms2vis_DS,
      "ms"_a, "idx"_a)
    .def ("vis2ms",&Baselines<double>::vis2ms<complex<double>>, BL_vis2ms_DS,
      "vis"_a, "idx"_a, "ms_in"_a=py::none());
Martin Reinecke's avatar
Martin Reinecke committed
1088
  py::class_<GridderConfig<double>> (m, "GridderConfig", GridderConfig_DS)
1089
    .def(py::init<size_t, size_t, double, double, double>(),"nxdirty"_a,
Martin Reinecke's avatar
Martin Reinecke committed
1090
      "nydirty"_a, "epsilon"_a, "pixsize_x"_a, "pixsize_y"_a)
Martin Reinecke's avatar
Martin Reinecke committed
1091
1092
1093
1094
1095
    .def("Nxdirty", &GridderConfig<double>::Nxdirty)
    .def("Nydirty", &GridderConfig<double>::Nydirty)
    .def("Epsilon", &GridderConfig<double>::Epsilon)
    .def("Pixsize_x", &GridderConfig<double>::Pixsize_x)
    .def("Pixsize_y", &GridderConfig<double>::Pixsize_y)
Martin Reinecke's avatar
Martin Reinecke committed
1096
1097
    .def("Nu", &GridderConfig<double>::Nu)
    .def("Nv", &GridderConfig<double>::Nv)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
1098
1099
    .def("grid2dirty", &GridderConfig<double>::grid2dirty, grid2dirty_DS,
      "grid"_a)
1100
    .def("grid2dirty_c", &GridderConfig<double>::grid2dirty_c, "grid"_a)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
1101
1102
    .def("dirty2grid", &GridderConfig<double>::dirty2grid, dirty2grid_DS,
      "dirty"_a)
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
    .def("dirty2grid_c", &GridderConfig<double>::dirty2grid_c, "dirty"_a)

    // pickle support
    .def(py::pickle(
        // __getstate__
        [](const GridderConfig<double> & gc) {
          // Encode object state in tuple
          return py::make_tuple(gc.Nxdirty(), gc.Nydirty(), gc.Epsilon(),
                                gc.Pixsize_x(), gc.Pixsize_y());
        },
        // __setstate__
        [](py::tuple t) {
          if(t.size() != 5)
            { throw std::runtime_error("Invalid state"); }

          // Reconstruct from tuple
          return GridderConfig<double>(t[0].cast<size_t>(), t[1].cast<size_t>(),
                                       t[2].cast<double>(), t[3].cast<double>(),
                                       t[4].cast<double>());

        }));
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
1124
1125
1126
1127
  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);
  m.def(