File: dftpredictionalgorithm.h

package info (click to toggle)
wsclean 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,196 kB
  • sloc: cpp: 34,504; ansic: 234; python: 174; makefile: 10
file content (200 lines) | stat: -rw-r--r-- 7,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifndef DFT_PREDICTION_ALGORITHM_H
#define DFT_PREDICTION_ALGORITHM_H

#include "banddata.h"
#include "matrix2x2.h"
#include "polarization.h"
#include "uvector.h"

#include "wsclean/imagebufferallocator.h"
#include "lofar/lbeamevaluator.h"

#include <cmath>
#include <vector>
#include <complex>

/**
 * Structure:
 * - PredictionImage: images[4] -- collects the model images.
 * - PredictionInput: components[nComponents] -- made from image, used as input for prediction.
 * - PredictionComponent: l, m, flux[nChannel x 4], antennaBeamValues[nAntenna] (these are updated per timestep)
 * - DFTAntennaInfo: beamValuesPerChannel[nChannel] of Matrix2x2
 */

class DFTAntennaInfo
{
public:
	const MC2x2& BeamValue(size_t channelIndex) const { return _beamValuesPerChannel[channelIndex]; }
	MC2x2& BeamValue(size_t channelIndex) { return _beamValuesPerChannel[channelIndex]; }
	
	std::vector<MC2x2>::iterator begin() { return _beamValuesPerChannel.begin(); }
	std::vector<MC2x2>::iterator end() { return _beamValuesPerChannel.end(); }
	size_t ChannelCount() const { return _beamValuesPerChannel.size(); }
	void InitializeChannelBuffers(size_t channelCount) { _beamValuesPerChannel.resize(channelCount); }
	void SetUnitaryBeam() {
		for(MC2x2& m : _beamValuesPerChannel)
			m = MC2x2::Unity();
	}
private:
	std::vector<MC2x2> _beamValuesPerChannel;
};

class DFTPredictionComponent
{
public:
	DFTPredictionComponent() : 
		_ra(0.0), _dec(0.0), _l(0.0), _m(0.0), _lmSqrt(0.0),
		_isGaussian(false)
	{ }
	
	DFTPredictionComponent(double ra, double dec, double l, double m, std::complex<double> fluxLinear[4], size_t channelCount) :
		_ra(ra), _dec(dec), _l(l), _m(m), _lmSqrt(sqrt(1.0 - l*l - m*m)),
		_isGaussian(false),
		_flux(channelCount)
	{
		for(size_t ch=0; ch!=channelCount; ++ch)
		{
			for(size_t p=0; p!=4; ++p) _flux[ch][p] = fluxLinear[p];
		}
	}
	void SetPosition(double ra, double dec, double l, double m)
	{
		_ra = ra; _dec = dec;
		_l = l; _m = m;
		 _lmSqrt = sqrt(1.0 - l*l - m*m);
	}
	void SetGaussianInfo(double positionAngle, double major, double minor)
	{
		initializeGaussian(positionAngle, major, minor);
	}
	void SetChannelCount(size_t channelCount) { _flux.resize(channelCount); }
	void SetFlux(const std::vector<MC2x2>& fluxPerChannel)
	{
		_flux = fluxPerChannel;
	}
	double L() const { return _l; }
	double M() const { return _m; }
	double RA() const { return _ra; }
	double Dec() const { return _dec; }
	double LMSqrt() const { return _lmSqrt; }
	bool IsGaussian() const { return _isGaussian; }
	const double* GausTransformationMatrix() const { return _gausTransf; }
	const DFTAntennaInfo& AntennaInfo(size_t antennaIndex) const { return _beamValuesPerAntenna[antennaIndex]; }
	DFTAntennaInfo& AntennaInfo(size_t antennaIndex) { return _beamValuesPerAntenna[antennaIndex]; }
	MC2x2& LinearFlux(size_t channelIndex) { return _flux[channelIndex]; }
	const MC2x2& LinearFlux(size_t channelIndex) const { return _flux[channelIndex]; }
	size_t AntennaCount() const { return _beamValuesPerAntenna.size(); }
	void InitializeBeamBuffers(size_t antennaCount, size_t channelCount)
	{
		_beamValuesPerAntenna.resize(antennaCount);
		for(std::vector<DFTAntennaInfo>::iterator a = _beamValuesPerAntenna.begin(); a!=_beamValuesPerAntenna.end(); ++a)
			a->InitializeChannelBuffers(channelCount);
	}
	void SetUnitaryBeam() {
		for(std::vector<DFTAntennaInfo>::iterator a = _beamValuesPerAntenna.begin(); a!=_beamValuesPerAntenna.end(); ++a)
			a->SetUnitaryBeam();
	}
private:
	void initializeGaussian(double positionAngle, double majorAxis, double minorAxis)
	{
		// Using the FWHM formula for a Gaussian:
		double sigmaMaj = majorAxis / (2.0L * sqrtl(2.0L * logl(2.0L)));
		double sigmaMin = minorAxis / (2.0L * sqrtl(2.0L * logl(2.0L)));
		// Position angle is angle from North:
		// (TODO this and next statements can be optimized to remove add)
		double
			paSin = std::sin(positionAngle+0.5*M_PI),
			paCos = std::cos(positionAngle+0.5*M_PI);
		// Make rotation matrix
		long double transf[4];
		transf[0] = paCos;
		transf[1] = -paSin;
		transf[2] = paSin;
		transf[3] = paCos;
		// Multiply with scaling matrix to make variance 1.
		// sigmamaj/min are multiplications and include pi^2 factor, because the sigma
		// of the Fourier transform of a Gaus is 1/sigma of the normal Gaus and has a sqrt(2 pi^2) factor.
		_gausTransf[0] = transf[0] * sigmaMaj * M_PI * sqrt(2.0);
		_gausTransf[1] = transf[1] * sigmaMaj * M_PI * sqrt(2.0);
		_gausTransf[2] = transf[2] * sigmaMin * M_PI * sqrt(2.0);
		_gausTransf[3] = transf[3] * sigmaMin * M_PI * sqrt(2.0);
		_isGaussian = true;
	}
	double _ra, _dec, _l, _m, _lmSqrt;
	bool _isGaussian;
	double _gausTransf[4];
	std::vector<MC2x2> _flux;
	std::vector<DFTAntennaInfo> _beamValuesPerAntenna;
};

class DFTPredictionInput
{
public:
	typedef std::vector<DFTPredictionComponent>::iterator iterator;
	typedef std::vector<DFTPredictionComponent>::const_iterator const_iterator;
	
	DFTPredictionInput() { }
	void InitializeFromModel(const class Model& model, long double phaseCentreRA, long double phaseCentreDec, const BandData& band);
	void AddComponent(const DFTPredictionComponent& component)
	{
		_components.emplace_back(component);
	}
	DFTPredictionComponent& AddComponent()
	{
		_components.emplace_back();
		return _components.back();
	}
	size_t ComponentCount() const { return _components.size(); }
	void InitializeBeamBuffers(size_t antennaCount, size_t channelCount) {
		for(iterator c=begin(); c!=end(); ++c)
			c->InitializeBeamBuffers(antennaCount, channelCount);
	}
	void SetUnitaryBeam() {
		for(iterator c=begin(); c!=end(); ++c)
			c->SetUnitaryBeam();
	}
	void ConvertApparentToAbsolute(casacore::MeasurementSet& ms);
	
	const_iterator begin() const { return _components.begin(); }
	const_iterator end() const { return _components.end(); }
	iterator begin() { return _components.begin(); }
	iterator end() { return _components.end(); }
private:
	std::vector<DFTPredictionComponent> _components;
};

class DFTPredictionImage
{
public:
	DFTPredictionImage(size_t width, size_t height, ImageBufferAllocator& allocator);
	
	void Add(PolarizationEnum polarization, const double* image);
	void Add(PolarizationEnum polarization, const double* real, const double* imaginary);
	
	void FindComponents(DFTPredictionInput& destination, double phaseCentreRA, double phaseCentreDec, double pixelSizeX, double pixelSizeY, double dl, double dm, size_t channelCount);
private:
	size_t _width, _height;
	ImageBufferAllocator* _allocator;
	ImageBufferAllocator::Ptr _images[4];
	std::vector<PolarizationEnum> _pols;
};

class DFTPredictionAlgorithm
{
public:
	DFTPredictionAlgorithm(DFTPredictionInput& input, const BandData& band) : _input(input), _band(band), _hasBeam(false)
	{ }
	
	void Predict(MC2x2& dest, double u, double v, double w, size_t channelIndex, size_t a1, size_t a2);

	void UpdateBeam(LBeamEvaluator& beamEvaluator, size_t startChannel, size_t endChannel);
	
private:
	void predict(MC2x2& dest, double u, double v, double w, size_t channelIndex, size_t a1, size_t a2, const DFTPredictionComponent& component);
	
	DFTPredictionInput& _input;
	BandData _band;
	bool _hasBeam;
};

#endif