File: dftpredictionalgorithm.cpp

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 (329 lines) | stat: -rw-r--r-- 10,077 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "dftpredictionalgorithm.h"
#include "units/imagecoordinates.h"
#include "matrix2x2.h"
#include "model/model.h"
#include "progressbar.h"

#include <casacore/measures/TableMeasures/ArrayMeasColumn.h>

DFTPredictionImage::DFTPredictionImage(size_t width, size_t height, ImageBufferAllocator& allocator) :
	_width(width),
	_height(height),
	_allocator(&allocator)
{
	const size_t s = width*height;
	for(size_t p=0; p!=4; ++p)
	{
		_allocator->Allocate(s, _images[p]);
		for(size_t i=0; i!=s; ++i)
			_images[p][i] = 0.0;
	}
}

void DFTPredictionImage::Add(PolarizationEnum polarization, const double* image)
{
	size_t size = _width * _height;
	for(size_t i=0; i!=size; ++i)
	{
		std::complex<double> temp[4];
		double stokes[4];
		switch(polarization)
		{
		case Polarization::XX:
			temp[0] = image[i];
			Polarization::LinearToStokes(temp, stokes);
			_images[0][i] += stokes[0];
			_images[1][i] += stokes[1];
			break;
		case Polarization::XY:
		case Polarization::YX:
		case Polarization::Instrumental:
			throw std::runtime_error("Invalid call to Add()");
		case Polarization::YY:
			temp[3] = image[i];
			Polarization::LinearToStokes(temp, stokes);
			_images[0][i] += stokes[0];
			_images[1][i] += stokes[1];
			break;
		case Polarization::StokesI:
			_images[0][i] += image[i];
			break;
		case Polarization::StokesQ:
			_images[1][i] += image[i];
			break;
		case Polarization::StokesU:
			_images[2][i] += image[i];
			break;
		case Polarization::StokesV:
			_images[3][i] += image[i];
			break;
		case Polarization::RR:
			temp[0] = image[i];
			Polarization::CircularToStokes(temp, stokes);
			_images[0][i] += stokes[0];
			_images[3][i] += stokes[3];
			break;
		case Polarization::RL:
		case Polarization::LR:
			throw std::runtime_error("Invalid call to Add()");
		case Polarization::LL:
			temp[0] = image[i];
			Polarization::CircularToStokes(temp, stokes);
			_images[0][i] += stokes[0];
			_images[3][i] += stokes[3];
			break;
		}
	}
	_pols.push_back(polarization);
}

void DFTPredictionImage::Add(PolarizationEnum polarization, const double* real, const double* imaginary)
{
	size_t size = _width * _height;
	for(size_t i=0; i!=size; ++i)
	{
		std::complex<double> temp[4];
		double stokes[4];
		switch(polarization)
		{
		case Polarization::XX:
		case Polarization::YY:
		case Polarization::StokesI:
		case Polarization::StokesQ:
		case Polarization::StokesU:
		case Polarization::StokesV:
		case Polarization::RR:
		case Polarization::LL:
		case Polarization::Instrumental:
			throw std::runtime_error("Invalid call to Add()");
		case Polarization::XY:
			temp[1] = std::complex<double>(real[i], imaginary[i]);
			Polarization::LinearToStokes(temp, stokes);
			_images[2][i] += stokes[2];
			_images[3][i] += stokes[3];
			break;
		case Polarization::YX:
			temp[2] = std::complex<double>(real[i], imaginary[i]);
			Polarization::LinearToStokes(temp, stokes);
			_images[2][i] += stokes[2];
			_images[3][i] += stokes[3];
			break;
		case Polarization::RL:
			temp[1] = std::complex<double>(real[i], imaginary[i]);
			Polarization::CircularToStokes(temp, stokes);
			_images[1][i] += stokes[1];
			_images[2][i] += stokes[2];
			break;
		case Polarization::LR:
			temp[2] = std::complex<double>(real[i], imaginary[i]);
			Polarization::CircularToStokes(temp, stokes);
			_images[1][i] += stokes[1];
			_images[2][i] += stokes[2];
			break;
		}
	}
	_pols.push_back(polarization);
}

void DFTPredictionImage::FindComponents(DFTPredictionInput& destination, double phaseCentreRA, double phaseCentreDec, double pixelSizeX, double pixelSizeY, double dl, double dm, size_t channelCount)
{
	size_t index = 0;
	for(size_t y=0; y!=_height; ++y)
	{
		for(size_t x=0; x!=_width; ++x)
		{
			if(_images[0][index] != 0.0 || _images[1][index] != 0.0 ||
				_images[2][index] != 0.0 || _images[3][index] != 0.0)
			{
				double l, m;
				ImageCoordinates::XYToLM(x, y, pixelSizeX, pixelSizeY, _width, _height, l, m);
				l += dl; m += dm;
				double ra, dec;
				ImageCoordinates::LMToRaDec(l, m, phaseCentreRA, phaseCentreDec, ra, dec);
				double stokes[4] = { _images[0][index], _images[1][index],
					_images[2][index], _images[3][index] };
				std::complex<double> linear[4];
				Polarization::StokesToLinear(stokes, linear);
				destination.AddComponent(DFTPredictionComponent(ra, dec, l, m, linear, channelCount));
			}
			++index;
		}
	}
}

struct ComponentInfo
{
	std::vector<size_t> count;
	std::vector<MC2x2> beamValues;
};
	
void DFTPredictionInput::ConvertApparentToAbsolute(casacore::MeasurementSet& ms)
{
	std::vector<ComponentInfo> compInfos(_components.size());
	
	const BandData band(ms.spectralWindow());
	LBeamEvaluator evaluator(ms);
	casacore::MEpoch::ROScalarColumn timeColumn(ms, ms.columnName(casacore::MSMainEnums::TIME));
	size_t nrow = ms.nrow();

	for(std::vector<ComponentInfo>::iterator cInfo=compInfos.begin(); cInfo!=compInfos.end(); ++cInfo)
	{
		cInfo->beamValues.assign(band.ChannelCount(), MC2x2::Zero());
		cInfo->count.assign(band.ChannelCount(), 0);
	}
	
	ProgressBar progress("Evaluating beam");
	for(size_t row=0; row!=nrow; ++row)
	{
		casacore::MEpoch time = timeColumn(row);
		if(time.getValue().get() != evaluator.Time().getValue().get())
		{
			evaluator.SetTime(time);
			LBeamEvaluator::PrecalcPosInfo posInfo;
			for(size_t i=0; i!=_components.size(); ++i)
			{
				const DFTPredictionComponent& c = _components[i];
				ComponentInfo& cInfo = compInfos[i];
				evaluator.PrecalculatePositionInfo(posInfo, c.RA(), c.Dec());
				for(size_t ch=0; ch!=band.ChannelCount(); ++ch)
				{
					MC2x2 timeStepValue;
					evaluator.EvaluateFullArray(posInfo, band.ChannelFrequency(ch), timeStepValue);
					cInfo.beamValues[ch] += timeStepValue;
					++cInfo.count[ch];
				}
			}
		}
		progress.SetProgress(row+1,nrow);
	}
	
	for(size_t i=0; i!=_components.size(); ++i)
	{
		DFTPredictionComponent& c = _components[i];
		ComponentInfo& cInfo = compInfos[i];
		for(size_t ch=0; ch!=band.ChannelCount(); ++ch)
		{
			cInfo.beamValues[ch] /= double(cInfo.count[ch]);
			cInfo.beamValues[ch].Invert();
			if(ch==band.ChannelCount()/2)
			{
				std::cout << RaDecCoord::RAToString(c.RA()) << " " << RaDecCoord::DecToString(c.Dec()) << " :";
				for(size_t p=0; p!=4; ++p)
					std::cout << " " << cInfo.beamValues[ch][p];
				std::cout << " -> ";
			}
			MC2x2 temp;
			MC2x2::ATimesB(temp, cInfo.beamValues[ch], c.LinearFlux(ch));
			MC2x2::ATimesHermB(c.LinearFlux(ch), temp, cInfo.beamValues[ch]);
			if(ch==band.ChannelCount()/2)
				std::cout << c.LinearFlux(ch).ToString() << " (" << c.L() << "," << c.M() << ")\n";
		}
	}
	
	
}

void DFTPredictionInput::InitializeFromModel(const Model& model, long double phaseCentreRA, long double phaseCentreDec, const BandData& band)
{
	for(const ModelSource& s : model)
	{
		for(const ModelComponent& comp : s)
		{
			long double l, m;
			DFTPredictionComponent& component = AddComponent();
			ImageCoordinates::RaDecToLM(comp.PosRA(), comp.PosDec(), phaseCentreRA, phaseCentreDec, l, m);
			component.SetPosition(comp.PosRA(), comp.PosDec(), l, m);
			if(comp.Type() == ModelComponent::GaussianSource)
			{
				component.SetGaussianInfo(comp.PositionAngle(), comp.MajorAxis(), comp.MinorAxis());
			}
			component.SetChannelCount(band.ChannelCount());
			for(size_t ch=0; ch!=band.ChannelCount(); ++ch)
			{
				MC2x2& flux = component.LinearFlux(ch);
				double stokes[4];
				for(size_t p=0; p!=4; ++p)
					stokes[p] = comp.SED().FluxAtFrequency(band.ChannelFrequency(ch), Polarization::IndexToStokes(p));
				Polarization::StokesToLinear(stokes, flux.Data());
			}
		}
	}
}

void DFTPredictionAlgorithm::Predict(MC2x2& dest, double u, double v, double w, size_t channelIndex, size_t a1, size_t a2)
{
	dest = MC2x2::Zero();
	MC2x2 single;
	for(DFTPredictionInput::const_iterator c=_input.begin(); c!=_input.end(); ++c)
	{
		predict(single, u, v, w, channelIndex, a1, a2, *c);
		dest += single;
	}
}

void DFTPredictionAlgorithm::predict(MC2x2& dest, double u, double v, double w, size_t channelIndex, size_t a1, size_t a2, const DFTPredictionComponent& component)
{
	double l = component.L(), m = component.M(), lmsqrt = component.LMSqrt();
	double angle = 2.0*M_PI*(u*l + v*m + w*(lmsqrt-1.0));
	double
		sinangleOverLMS = sin(angle),
		cosangleOverLMS = cos(angle);
	MC2x2 appFlux;
	if(_hasBeam)
	{
		MC2x2 temp;
		MC2x2::ATimesB(temp, component.AntennaInfo(a1).BeamValue(channelIndex), component.LinearFlux(channelIndex));
		MC2x2::ATimesHermB(appFlux, temp, component.AntennaInfo(a2).BeamValue(channelIndex));
	}
	else {
		appFlux = component.LinearFlux(channelIndex);
	}
	if(component.IsGaussian())
	{
		const double* gausTrans = component.GausTransformationMatrix();
		double uTemp = u*gausTrans[0] + v*gausTrans[1];
		v = u*gausTrans[2] + v*gausTrans[3];
		u = uTemp;
		double gaus = exp(-u*u - v*v);
		for(size_t p=0; p!=4; ++p)
		{
			std::complex<double> val = appFlux[p] * gaus;
			dest[p] = std::complex<double>(
				val.real() * cosangleOverLMS - val.imag() * sinangleOverLMS,
				val.real() * sinangleOverLMS + val.imag() * cosangleOverLMS);
		}
	}
	else {
		for(size_t p=0; p!=4; ++p)
		{
			std::complex<double> val = appFlux[p];
			dest[p] = std::complex<double>(
				val.real() * cosangleOverLMS - val.imag() * sinangleOverLMS,
				val.real() * sinangleOverLMS + val.imag() * cosangleOverLMS);
		}
	}
}

void DFTPredictionAlgorithm::UpdateBeam(LBeamEvaluator& beamEvaluator, size_t startChannel, size_t endChannel)
{
	if(!_hasBeam)
	{
		_hasBeam = true;
	}
	for(DFTPredictionInput::iterator component=_input.begin(); component!=_input.end(); ++component)
	{
		LBeamEvaluator::PrecalcPosInfo posInfo;
		beamEvaluator.PrecalculatePositionInfo(posInfo, component->RA(), component->Dec());
		
		for(size_t antenna=0; antenna!=component->AntennaCount(); ++antenna)
		{
			DFTAntennaInfo& antennaInfo = component->AntennaInfo(antenna);
			
			for(size_t channel=startChannel; channel!=endChannel; ++channel)
			{
				double freq = _band.ChannelFrequency(channel);
				beamEvaluator.Evaluate(posInfo, freq, antenna, antennaInfo.BeamValue(channel));
			}
		}
	}
}