File: badstations.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 (215 lines) | stat: -rw-r--r-- 6,945 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
#include <iostream>

#include "strategy/algorithms/antennaselector.h"
#include "strategy/algorithms/baselineselector.h"

#include "structures/measurementset.h"

#include "quality/defaultstatistics.h"
#include "quality/histogramcollection.h"
#include "quality/qualitytablesformatter.h"
#include "quality/statisticscollection.h"
#include "quality/statisticsderivator.h"

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

#include "quality/histogramtablesformatter.h"

#include <casacore/ms/MeasurementSets/MeasurementSet.h>

#include <casacore/tables/Tables/ArrayColumn.h>
#include <casacore/tables/Tables/ScalarColumn.h>

StatisticsCollection load(const std::string& filename, std::vector<AntennaInfo>& antennae)
{
	bool remote = aoRemote::ClusteredObservation::IsClusteredFilename(filename);
	StatisticsCollection statisticsCollection;
	HistogramCollection histogramCollection;
	if(remote)
	{
		std::unique_ptr<aoRemote::ClusteredObservation> observation =
			aoRemote::ClusteredObservation::Load(filename);
		aoRemote::ProcessCommander commander(*observation);
		commander.PushReadAntennaTablesTask();
		commander.PushReadQualityTablesTask(&statisticsCollection, &histogramCollection);
		commander.Run();
		antennae = commander.Antennas();
	}
	else {
		MeasurementSet ms(filename);
		const unsigned polarizationCount = ms.PolarizationCount();
		
		statisticsCollection.SetPolarizationCount(polarizationCount);
		QualityTablesFormatter qualityData(filename);
		statisticsCollection.Load(qualityData);
		unsigned antennaCount = ms.AntennaCount();
		for(unsigned a=0;a<antennaCount;++a)
			antennae.push_back(ms.GetAntennaInfo(a));
	}
	return statisticsCollection;
}

std::set<size_t> detectRFIPercentage(const char* filename)
{
	std::vector<AntennaInfo> antennae;
	StatisticsCollection statisticsCollection = load(filename, antennae);

	rfiStrategy::BaselineSelector selector;
	selector.SetUseLog(true);
	
	statisticsCollection.IntegrateBaselinesToOneChannel();
	const BaselineStatisticsMap &baselineMap = statisticsCollection.BaselineStatistics();
	const std::vector<std::pair<unsigned, unsigned> > list = baselineMap.BaselineList();
	for(std::vector<std::pair<unsigned, unsigned> >::const_iterator i=list.begin();i!=list.end();++i)
	{
		const unsigned a1 = i->first, a2 = i->second;
		
		DefaultStatistics statistic = baselineMap.GetStatistics(a1, a2);
		selector.Add(statistic, antennae[a1], antennae[a2]);
	}
	std::vector<rfiStrategy::BaselineSelector::SingleBaselineInfo> markedBaselines;
	std::set<unsigned> badStations;
	
	selector.Search(markedBaselines);
	selector.ImplyStations(markedBaselines, 0.3, badStations);
	
	std::cout << "List of " << badStations.size() << " bad stations:\n";
	for(size_t ant : badStations)
	{
		std::cout << antennae[ant].name << " (" << ant << ")\n";
	}
	return std::set<size_t>(badStations.begin(), badStations.end());
}

std::set<size_t> detectStddev(const char* filename)
{
	std::vector<AntennaInfo> antennae;
	StatisticsCollection statisticsCollection = load(filename, antennae);
	rfiStrategy::AntennaSelector selector;
	std::vector<size_t> badStations = selector.Run(statisticsCollection);

	std::cout << "List of " << badStations.size() << " bad stations:\n";
	for(size_t ant : badStations)
	{
		std::cout << antennae[ant].name << " (" << ant << ")\n";
	}
	return std::set<size_t>(badStations.begin(), badStations.end());
}

void flagAntennas(const char* filename, const std::set<size_t>& antennae)
{
	casacore::MeasurementSet ms(filename, casacore::Table::Update);

	/**
		* Read some meta data from the measurement set
		*/
	casacore::MSSpectralWindow spwTable = ms.spectralWindow();
	size_t spwCount = spwTable.nrow();
	if(spwCount != 1) throw std::runtime_error("Set should have exactly one spectral window");
	
	casacore::ScalarColumn<int> numChanCol(spwTable, casacore::MSSpectralWindow::columnName(casacore::MSSpectralWindowEnums::NUM_CHAN));
	size_t channelCount = numChanCol.get(0);
	if(channelCount == 0) throw std::runtime_error("No channels in set");
	
	casacore::ScalarColumn<int> ant1Column(ms, ms.columnName(casacore::MSMainEnums::ANTENNA1));
	casacore::ScalarColumn<int> ant2Column(ms, ms.columnName(casacore::MSMainEnums::ANTENNA2));
	casacore::ArrayColumn<bool> flagsColumn(ms, ms.columnName(casacore::MSMainEnums::FLAG));
	
	if(ms.nrow() == 0) throw std::runtime_error("Table has no rows (no data)");
	casacore::IPosition flagsShape = flagsColumn.shape(0);
	
	casacore::Array<bool> flags(flagsShape, true);
	
	std::cout << "Flagging... " << std::flush;
	
	/**
		* Flag
		*/
	size_t crossCount = 0, autoCount = 0;
	for(size_t rowIndex=0; rowIndex!=ms.nrow(); ++rowIndex)
	{
		// Selected?
		if(antennae.find(ant1Column.get(rowIndex)) != antennae.end() || antennae.find(ant2Column.get(rowIndex)) != antennae.end())
		{
			if(ant1Column.get(rowIndex) == ant2Column.get(rowIndex))
				++autoCount;
			else
				++crossCount;
			flagsColumn.put(rowIndex, flags);
		}
	}
	
	std::cout << "DONE (selected " << crossCount << " cross- and " << autoCount << " auto-correlated timesteps)\n";
}

void printSyntax(std::ostream &stream, char *argv[])
{
	stream <<
		"The executable 'badstations' will give a list of stations that are outliers\n"
		"according to the RFI statistics.\n"
		"\n"
		"Syntax: badstations [options] <filename>\n"
		"\n"
		"Options:\n"
		"-flag\n"
		"  Will also flag all antennas in the measurement set.\n"
		"  (this only works if the given filename is a measurement set, not a .ref).\n"
		"-method <stddev / percentage>\n"
		"  Select detection method. Method 'stddev' is the default, and simply detects\n"
		"  stations with an outlyer standard deviation. Method 'percentage' detects\n"
		"  outliers based on the percentage RFI statistic, taking into account that\n"
		"  short baselines often see fewer RFI, by fitting a curve to the statistic\n"
		"  as a function of baseline.\n";
}

int main(int argc, char *argv[])
{
#ifdef HAS_LOFARSTMAN
	register_lofarstman();
#endif // HAS_LOFARSTMAN

	int argi = 1;
	bool doFlag = false;
	enum Method { StddevMethod, RFIPercentangeMethod } method = StddevMethod;
	while(argi < argc && argv[argi][0] == '-')
	{
		std::string p(argv[argi]+1);
		if(p == "flag")
			doFlag = true;
		else if(p == "method")
		{
			++argi;
			std::string m = argv[argi];
			if(m == "stddev")
				method = StddevMethod;
			else if(m == "percentage")
				method = RFIPercentangeMethod;
			else
				throw std::runtime_error("Unknown method given");
		}
		else
			throw std::runtime_error("Unknown parameter");
		++argi;
	}
	if(argi >= argc)
	{
		printSyntax(std::cerr, argv);
		return -1;
	}
	else {
		const char* filename = argv[argi];
		std::set<size_t> badAntennas;
		switch(method) {
			case StddevMethod:
			badAntennas = detectStddev(filename);
			break;
			case RFIPercentangeMethod:
			badAntennas = detectRFIPercentage(filename);
			break;
		}
		if(doFlag)
			flagAntennas(filename, badAntennas);
		return 0;
	}
}