File: aofrequencyfilter.cpp

package info (click to toggle)
aoflagger 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,232 kB
  • sloc: cpp: 61,805; python: 60; sh: 23; makefile: 8
file content (278 lines) | stat: -rw-r--r-- 8,535 bytes parent folder | download | duplicates (2)
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
#include <iostream>
#include <mutex>

#include <fftw3.h>

#include "remote/clusteredobservation.h"
#include "remote/observationtimerange.h"
#include "remote/processcommander.h"

#include "structures/system.h"

#include "util/lane.h"

#include "imaging/uvimager.h"

#include "strategy/algorithms/convolutions.h"

using namespace std;
using namespace aoRemote;

lane<ObservationTimerange*> *readLane;
lane<ObservationTimerange*> *writeLane;

std::mutex commanderMutex;
ProcessCommander *commander;

fftw_plan fftPlanForward, fftPlanBackward;
const size_t rowCountPerRequest = 128;

// fringe size is given in units of wavelength / fringe. Fringes smaller than that will be filtered.
double filterFringeSize;

bool isFilterSizeInChannels;

/**
 * This function returns the distance between the u,v points of the highest and lowest frequencies.
 * The returned distance is in wavelengths.
 */
double uvDist(double u, double v, double frequencyWidth)
{
	const double
		ud = frequencyWidth * u,
		vd = frequencyWidth * v;
	return sqrt(ud * ud + vd * vd) / UVImager::SpeedOfLight();
}

void workThread()
{
	ObservationTimerange *timerange;
	
	// These are for diagnostic info
	double maxFilterSizeInChannels = 0.0, minFilterSizeInChannels = 1e100;
	
	if(readLane->read(timerange))
	{
		const size_t channelCount = timerange->ChannelCount();
		const unsigned polarizationCount = timerange->PolarizationCount();
		fftw_complex
			*fftIn = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * channelCount),
			*fftOut = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * channelCount);
		do
		{
			for(size_t t=0;t<timerange->TimestepCount();++t)
			{
				if(timerange->Antenna1(t) != timerange->Antenna2(t))
				{
					// Calculate the frequencies to filter
					double u = timerange->U(t), v = timerange->V(t);
					double limitFrequency = isFilterSizeInChannels ?
						(channelCount / filterFringeSize) :
						(uvDist(u, v, timerange->FrequencyWidth()) / filterFringeSize);
					
					if(limitFrequency*2 <= channelCount) // otherwise no frequencies had to be removed
					{
						if(limitFrequency > maxFilterSizeInChannels) maxFilterSizeInChannels = limitFrequency;
						if(limitFrequency < minFilterSizeInChannels) minFilterSizeInChannels = limitFrequency;
						for(unsigned p=0;p<polarizationCount;++p)
						{
							// Copy data in buffer
							num_t *realPtr = timerange->RealData(t) + p;
							num_t *imagPtr = timerange->ImagData(t) + p;
							
							for(size_t c=0;c<channelCount;++c)
							{
								fftIn[c][0] = *realPtr;
								fftIn[c][1] = *imagPtr;
								realPtr += polarizationCount;
								imagPtr += polarizationCount;
							}
							
							fftw_execute_dft(fftPlanForward, fftIn, fftOut);
							size_t filterIndexSize = (limitFrequency > 1.0) ? (size_t) ceil(limitFrequency/2.0) : 1;
							// Remove the high frequencies [filterIndexSize : n-filterIndexSize]
							for(size_t f=filterIndexSize;f<channelCount - filterIndexSize;++f)
							{
								fftOut[f][0] = 0.0;
								fftOut[f][1] = 0.0;
							}
							fftw_execute_dft(fftPlanBackward, fftOut, fftIn);

							// Copy data back; fftw multiplies data with n, so divide by n.
							double factor = 1.0 / (double) channelCount;
							realPtr = timerange->RealData(t) + p;
							imagPtr = timerange->ImagData(t) + p;
							for(size_t c=0;c<channelCount;++c)
							{
								*realPtr = fftIn[c][0] * factor;
								*imagPtr = fftIn[c][1] * factor;
								realPtr += polarizationCount;
								imagPtr += polarizationCount;
							}
						}
					}
				}
			}
			writeLane->write(timerange);
		} while(readLane->read(timerange));
		fftw_free(fftIn);
		fftw_free(fftOut);
	}
	std::cout << "Worker finished. Filtersize range in channel: " << minFilterSizeInChannels << "-" << maxFilterSizeInChannels << '\n';
}

void readThreadFunction(ObservationTimerange &timerange, const size_t &totalRows)
{
	std::vector<MSRowDataExt*> rowBuffer(commander->Observation().Size());
	for(size_t i=0;i<commander->Observation().Size();++i)
		rowBuffer[i] = new MSRowDataExt[rowCountPerRequest];

	size_t currentRow = 0;
	while(currentRow < totalRows)
	{
		size_t currentRowCount = rowCountPerRequest;
		if(currentRow + currentRowCount > totalRows)
			currentRowCount = totalRows - currentRow;
		timerange.SetZero();
		
		std::unique_lock<std::mutex> lock(commanderMutex);
		std::cout << "Reading... " << std::flush;
		commander->PushReadDataRowsTask(timerange, currentRow, currentRowCount, &rowBuffer[0]);
		commander->Run(false);
		commander->CheckErrors();
		std::cout << "Done.\n" << std::flush;
		lock.unlock();
		
		currentRow += currentRowCount;
		cout << "Read " << currentRow << '/' << totalRows << '\n';
		readLane->write(new ObservationTimerange(timerange));
	}
	for(size_t i=0;i<commander->Observation().Size();++i)
		delete[] rowBuffer[i];
}

void writeThreadFunction()
{
	const ClusteredObservation &obs = commander->Observation();
	std::vector<MSRowDataExt*> rowBuffer(obs.Size());
	for(size_t i=0;i<obs.Size();++i)
		rowBuffer[i] = new MSRowDataExt[rowCountPerRequest];
		
	ObservationTimerange *timerange;
	if(writeLane->read(timerange))
	{
		for(size_t i=0;i<obs.Size();++i)
		{
			for(size_t row=0;row<rowCountPerRequest;++row)
				rowBuffer[i][row] = MSRowDataExt(timerange->PolarizationCount(), timerange->Band(i).channels.size());
		}
		do {
			std::unique_lock<std::mutex> lock(commanderMutex);
			std::cout << "Writing... " << std::flush;
			commander->PushWriteDataRowsTask(*timerange, &rowBuffer[0]);
			commander->Run(false);
			commander->CheckErrors();
			std::cout << "Done.\n" << std::flush;
			lock.unlock();
			
			delete timerange;
		} while(writeLane->read(timerange));
	}
	std::cout << "Writer thread finished.\n";
}

void initializeFFTW(size_t channelCount)
{
	fftw_complex *fftIn, *fftOut;
	
	fftIn = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * channelCount);
	fftOut = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * channelCount);
	fftPlanForward = fftw_plan_dft_1d(channelCount, fftIn, fftOut, FFTW_FORWARD, FFTW_MEASURE);
	fftPlanBackward = fftw_plan_dft_1d(channelCount, fftIn, fftOut, FFTW_BACKWARD, FFTW_MEASURE);

	fftw_free(fftIn);
	fftw_free(fftOut);
}

void deinitializeFFTW()
{
	fftw_destroy_plan(fftPlanForward);
	fftw_destroy_plan(fftPlanBackward);
}

int main(int argc, char *argv[])
{
	if(argc != 4)
	{
		cerr << "Usage: aofrequencyfilter <reffile> <mode> <filterfringesize>\n"
		"\tmode can be 'inChannels' (CH) or in uv wavelengths (UV)\n";
	}
	else {
		string modeStr(argv[2]);
		if(modeStr == "CH")
			isFilterSizeInChannels = true;
		else if(modeStr == "UV")
			isFilterSizeInChannels = false;
		else throw std::runtime_error("Bad mode");
		
		filterFringeSize = atof(argv[3]);
		std::unique_ptr<ClusteredObservation> obs = ClusteredObservation::Load(argv[1]);
		commander = new ProcessCommander(*obs);
		commander->PushReadAntennaTablesTask();
		commander->PushReadBandTablesTask();
		commander->Run(false);
		commander->CheckErrors();
		
		ObservationTimerange timerange(*obs);
		const std::vector<BandInfo> &bands = commander->Bands();
		for(size_t i=0; i!=bands.size(); ++i)
			timerange.SetBandInfo(i, bands[i]);
		
		const unsigned processorCount = System::ProcessorCount();
		cout << "CPUs: " << processorCount << '\n';
		unsigned polarizationCount = commander->PolarizationCount();
		cout << "Polarization count: " << polarizationCount << '\n';
		
		timerange.Initialize(polarizationCount, rowCountPerRequest);
		
		cout << "Initializing FFTW..." << std::flush;
		initializeFFTW(timerange.ChannelCount());
		cout << " Done.\n";
		
		// We ask for "0" rows, which means we will ask for the total number of rows
		commander->PushReadDataRowsTask(timerange, 0, 0, 0);
		commander->Run(false);
		commander->CheckErrors();
		const size_t totalRows = commander->RowsTotal();
		cout << "Total rows to filter: " << totalRows << '\n';
		
		readLane = new lane<ObservationTimerange*>(processorCount);
		writeLane = new lane<ObservationTimerange*>(processorCount);
		
		// Start worker threads
		std::vector<std::thread> threads;
		for(size_t i=0; i<processorCount; ++i)
		{
			threads.emplace_back(&workThread);
		}
		std::thread writeThread(&writeThreadFunction);
		
		readThreadFunction(timerange, totalRows);
		
		// Shut down read workers
		readLane->write_end();
		for(size_t i=0; i<processorCount; ++i)
		{
			threads[i].join();
		}
		delete readLane;
		
		// Shut down write worker
		writeLane->write_end();
		writeThread.join();
		delete writeLane;
		
		// Clean
		delete commander;
	}
}