void TimeFrequencyImager::ReadTimeFlags(size_t xOffset, int frequencyCount, const casa::Array<bool> flag)
{
	casa::Array<bool>::const_iterator j = flag.begin();
	for(size_t f=0;f<(size_t) frequencyCount;++f) {
		bool stokesIF = false;
		if(_stokesIIndex >= 0) {
			stokesIF = *j;
			++j;
		}
		if(_xxIndex >= 0)
		{
			if(_readXX) _flagXX->SetValue(xOffset, f, *j);
			stokesIF |= *j;
			++j;
		}
		if(_xyIndex >= 0)
		{
			if(_readXY) _flagXY->SetValue(xOffset, f, *j);
			stokesIF |= *j;
			++j;
		}
		if(_yxIndex >= 0)
		{
			if(_readYX) _flagYX->SetValue(xOffset, f, *j);
			stokesIF |= *j;
			++j;
		}
		if(_yyIndex >= 0)
		{
			if(_readYY) _flagYY->SetValue(xOffset, f, *j);
			stokesIF |= *j;
			++j;
		}
		if(_readStokesI)
		{
			_flagCombined->SetValue(xOffset, f, stokesIF);
		}
	} 
}
void TimeFrequencyImager::ReadWeights(size_t xOffset, int frequencyCount, const casa::Array<float> weight)
{
	casa::Array<float>::const_iterator j = weight.begin();
	float xx = *j;
	++j;
	float xy = *j;
	++j;
	float yx = *j;
	++j;
	float yy = *j;
	++j;
	for(size_t f=0;f<(size_t) frequencyCount;++f) {
		if(_readXX)
		{
			_realXX->SetValue(xOffset, f, xx);
			_imaginaryXX->SetValue(xOffset, f, 0.0);
		}
		if(_readXY)
		{
			_realXY->SetValue(xOffset, f, xy);
			_imaginaryXY->SetValue(xOffset, f, 0.0);
		}
		if(_readYX)
		{
			_realYX->SetValue(xOffset, f, yx);
			_imaginaryYX->SetValue(xOffset, f, 0.0);
		}
		if(_readYY)
		{
			_realYY->SetValue(xOffset, f, yy);
			_imaginaryYY->SetValue(xOffset, f, 0.0);
		}
		if(_readStokesI)
		{
			_realStokesI->SetValue(xOffset, f, xx + yy);
			_imaginaryStokesI->SetValue(xOffset, f, 0.0);
		}
	} 
}
Exemple #3
0
void actionCollect(const std::string &filename, enum CollectingMode mode, StatisticsCollection &statisticsCollection, HistogramCollection &histogramCollection, bool mwaChannels, size_t flaggedTimesteps, const std::set<size_t> &flaggedAntennae)
{
	MeasurementSet *ms = new MeasurementSet(filename);
	const unsigned polarizationCount = ms->PolarizationCount();
	const unsigned bandCount = ms->BandCount();
	const bool ignoreChannelZero = ms->IsChannelZeroRubish();
	const std::string stationName = ms->GetStationName();
	BandInfo *bands = new BandInfo[bandCount];
	double **frequencies = new double*[bandCount];
	unsigned totalChannels = 0;
	for(unsigned b=0;b<bandCount;++b)
	{
		bands[b] = ms->GetBandInfo(b);
		frequencies[b] = new double[bands[b].channels.size()];
		totalChannels += bands[b].channels.size();
		for(unsigned c=0;c<bands[b].channels.size();++c)
		{
			frequencies[b][c] = bands[b].channels[c].frequencyHz;
		}
	}
	delete ms;
	
	std::cout
		<< "Polarizations: " << polarizationCount << '\n'
		<< "Bands: " << bandCount << '\n'
		<< "Channels/band: " << (totalChannels / bandCount) << '\n';
	if(ignoreChannelZero)
		std::cout << "Channel zero will be ignored, as this looks like a LOFAR data set with bad channel 0.\n";
	else
		std::cout << "Channel zero will be included in the statistics, as it seems that channel 0 is okay.\n";
	
	// Initialize statisticscollection
	statisticsCollection.SetPolarizationCount(polarizationCount);
	if(mode == CollectDefault)
	{
		for(unsigned b=0;b<bandCount;++b)
		{
			if(ignoreChannelZero)
				statisticsCollection.InitializeBand(b, (frequencies[b]+1), bands[b].channels.size()-1);
			else
				statisticsCollection.InitializeBand(b, frequencies[b], bands[b].channels.size());
		}
	}
	// Initialize Histograms collection
	histogramCollection.SetPolarizationCount(polarizationCount);

	// get columns
	casa::Table table(filename, casa::Table::Update);
	const char *dataColumnName = "DATA";
	casa::ROArrayColumn<casa::Complex> dataColumn(table, dataColumnName);
	casa::ROArrayColumn<bool> flagColumn(table, "FLAG");
	casa::ROScalarColumn<double> timeColumn(table, "TIME");
	casa::ROScalarColumn<int> antenna1Column(table, "ANTENNA1"); 
	casa::ROScalarColumn<int> antenna2Column(table, "ANTENNA2");
	casa::ROScalarColumn<int> windowColumn(table, "DATA_DESC_ID");
	
	std::cout << "Collecting statistics..." << std::endl;
	
	size_t channelCount = bands[0].channels.size();
	bool *correlatorFlags = new bool[channelCount];
	bool *correlatorFlagsForBadAntenna = new bool[channelCount];
	for(size_t ch=0; ch!=channelCount; ++ch)
	{
		correlatorFlags[ch] = false;
		correlatorFlagsForBadAntenna[ch] = true;
	}
	
	if(mwaChannels)
	{
		if(channelCount%24 != 0)
			std::cout << "MWA channels requested, but nr of channels not a multiply of 24. Ignoring.\n";
		else {
			size_t chanPerSb = channelCount/24;
			for(size_t x=0;x!=24;++x)
			{
				correlatorFlags[x*chanPerSb] = true;
				correlatorFlags[x*chanPerSb + chanPerSb/2] = true;
				correlatorFlags[x*chanPerSb + chanPerSb-1] = true;
			}
		}
	}
	
	const unsigned nrow = table.nrow();
	size_t timestepIndex = (size_t) -1;
	double prevtime = -1.0;
	for(unsigned row = 0; row!=nrow; ++row)
	{
		const double time = timeColumn(row);
		const unsigned antenna1Index = antenna1Column(row);
		const unsigned antenna2Index = antenna2Column(row);
		const unsigned bandIndex = windowColumn(row);
		
		if(time != prevtime)
		{
			++timestepIndex;
			prevtime = time;
		}
		
		const BandInfo &band = bands[bandIndex];
		
		const casa::Array<casa::Complex> dataArray = dataColumn(row);
		const casa::Array<bool> flagArray = flagColumn(row);
		
		std::vector<std::complex<float>* > samples(polarizationCount);
		bool **isRFI = new bool*[polarizationCount];
		for(unsigned p = 0; p < polarizationCount; ++p)
		{
			isRFI[p] = new bool[band.channels.size()];
			samples[p] = new std::complex<float>[band.channels.size()];
		}
		const bool antennaIsFlagged =
			flaggedAntennae.find(antenna1Index) != flaggedAntennae.end() ||
			flaggedAntennae.find(antenna2Index) != flaggedAntennae.end();
		
		casa::Array<casa::Complex>::const_iterator dataIter = dataArray.begin();
		casa::Array<bool>::const_iterator flagIter = flagArray.begin();
		const unsigned startChannel = ignoreChannelZero ? 1 : 0;
		if(ignoreChannelZero)
		{
			for(unsigned p = 0; p < polarizationCount; ++p)
			{
				++dataIter;
				++flagIter;
			}
		}
		for(unsigned channel = startChannel ; channel<band.channels.size(); ++channel)
		{
			for(unsigned p = 0; p < polarizationCount; ++p)
			{
				samples[p][channel - startChannel] = *dataIter;
				isRFI[p][channel - startChannel] = *flagIter;
				
				++dataIter;
				++flagIter;
			}
		}
		
		for(unsigned p = 0; p < polarizationCount; ++p)
		{
			switch(mode)
			{
				case CollectDefault:
					if(antennaIsFlagged || timestepIndex < flaggedTimesteps)
						statisticsCollection.Add(antenna1Index, antenna2Index, time, bandIndex, p, &samples[p]->real(), &samples[p]->imag(), isRFI[p], correlatorFlagsForBadAntenna, band.channels.size() - startChannel, 2, 1, 1);
					else
						statisticsCollection.Add(antenna1Index, antenna2Index, time, bandIndex, p, &samples[p]->real(), &samples[p]->imag(), isRFI[p], correlatorFlags, band.channels.size() - startChannel, 2, 1, 1);
					break;
				case CollectHistograms:
					histogramCollection.Add(antenna1Index, antenna2Index, p, samples[p], isRFI[p], band.channels.size() - startChannel);
					break;
			}
		}

		for(unsigned p = 0; p < polarizationCount; ++p)
		{
			delete[] isRFI[p];
			delete[] samples[p];
		}
		delete[] isRFI;
		
		reportProgress(row, nrow);
	}
	delete[] correlatorFlags;
	delete[] correlatorFlagsForBadAntenna;
	
	for(unsigned b=0;b<bandCount;++b)
		delete[] frequencies[b];
	delete[] frequencies;
	delete[] bands;
	std::cout << "100\n";
}
void TimeFrequencyImager::ReadTimeData(size_t xOffset, int frequencyCount, const casa::Array<casa::Complex> data, const casa::Array<casa::Complex> *model)
{
	casa::Array<casa::Complex>::const_iterator i = data.begin();
	casa::Array<casa::Complex>::const_iterator m;
	if(_dataKind == ResidualData)
		m = model->begin();

	for(size_t f=0;f<(size_t) frequencyCount;++f) {
		double xxr, xxi, xyr, xyi, yxr, yxi, yyr, yyi, ir, ii;

		if(_dataKind == ResidualData) {
			const casa::Complex &iData = *i;
			const casa::Complex &iModel = *m;
			if(_stokesIIndex >= 0) { ++i; ++m; }

			const casa::Complex &xxData = *i;
			const casa::Complex &xxModel = *m;
			if(_xxIndex >= 0) { ++i; ++m; }

			const casa::Complex &xyData = *i;
			const casa::Complex &xyModel = *m;
			if(_xyIndex >= 0) { ++i; ++m; }

			const casa::Complex &yxData = *i;
			const casa::Complex &yxModel = *m;
			if(_yxIndex >= 0) { ++i; ++m; }

			const casa::Complex &yyData = *i;
			const casa::Complex &yyModel = *m;
			if(_yyIndex >= 0) { ++i; ++m; }

			ir = iData.real() - iModel.real();
			ii = iData.imag() - iModel.imag();
			xxr = xxData.real() - xxModel.real();
			xxi = xxData.imag() - xxModel.imag();
			xyr = xyData.real() - xyModel.real();
			xyi = xyData.imag() - xyModel.imag();
			yxr = yxData.real() - yxModel.real();
			yxi = yxData.imag() - yxModel.imag();
			yyr = yyData.real() - yyModel.real();
			yyi = yyData.imag() - yyModel.imag();
		} else {
			const casa::Complex &stokesI = *i;
			if(_stokesIIndex >= 0) ++i;
			const casa::Complex &xx = *i;
			if(_xxIndex >= 0) ++i;
			const casa::Complex &xy = *i;
			if(_xyIndex >= 0) ++i;
			const casa::Complex &yx = *i;
			if(_yxIndex >= 0) ++i;
			const casa::Complex &yy = *i;
			if(_yyIndex >= 0) ++i;

			xxr = xx.real();
			xxi = xx.imag();
			xyr = xy.real();
			xyi = xy.imag();
			yxr = yx.real();
			yxi = yx.imag();
			yyr = yy.real();
			yyi = yy.imag();
			ir = stokesI.real();
			ii = stokesI.imag();
		}
	
		if(_readXX)
		{
			_realXX->SetValue(xOffset, f, xxr);
			_imaginaryXX->SetValue(xOffset, f, xxi);
		}
		if(_readXY)
		{
			_realXY->SetValue(xOffset, f, xyr);
			_imaginaryXY->SetValue(xOffset, f, xyi);
		}
		if(_readYX)
		{
			_realYX->SetValue(xOffset, f, yxr);
			_imaginaryYX->SetValue(xOffset, f, yxi);
		}
		if(_readYY)
		{
			_realYY->SetValue(xOffset, f, yyr);
			_imaginaryYY->SetValue(xOffset, f, yyi);
		}
		if(_readStokesI)
		{
			if(_readStokesIDirectly) {
				_realStokesI->SetValue(xOffset, f, ir);
				_imaginaryStokesI->SetValue(xOffset, f, ii);
			} else {
				_realStokesI->SetValue(xOffset, f, xxr + yyr);
				_imaginaryStokesI->SetValue(xOffset, f, xxi + yyi);
			}
		}
	}
}