File: timefrequencydata.h

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 (991 lines) | stat: -rw-r--r-- 31,585 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
#ifndef TIMEFREQUENCYDATA_H
#define TIMEFREQUENCYDATA_H

#include <array>
#include <string>
#include <vector>
#include <utility>
#include <sstream>
#include <stdexcept>

#include "image2d.h"
#include "mask2d.h"
#include "polarization.h"

#include "../baseexception.h"

class TimeFrequencyData
{
	public:
		enum ComplexRepresentation { PhasePart, AmplitudePart, RealPart, ImaginaryPart, ComplexParts };

		TimeFrequencyData() :
			_complexRepresentation(AmplitudePart),
			_data()
		{ }

		TimeFrequencyData(ComplexRepresentation complexRepresentation,
				PolarizationEnum polarizationType, const Image2DCPtr& image) :
			_complexRepresentation(complexRepresentation)
		{
			if(complexRepresentation == ComplexParts)
				throw BadUsageException("Incorrect construction of time/frequency data: trying to create complex representation from single image");
			_data.emplace_back(polarizationType, image);
		}

		TimeFrequencyData(PolarizationEnum polarizationType,
											const Image2DCPtr &real,
											const Image2DCPtr &imaginary) :
				_complexRepresentation(ComplexParts)
		{
			_data.emplace_back(polarizationType, real, imaginary);
		}
		
		TimeFrequencyData(ComplexRepresentation complexRepresentation,
											PolarizationEnum polarizationA,
											const Image2DCPtr &imageA,
											PolarizationEnum polarizationB,
											const Image2DCPtr &imageB) :
				_complexRepresentation(complexRepresentation)
		{
			_data.reserve(2);
			_data.emplace_back(polarizationA, imageA);
			_data.emplace_back(polarizationB, imageB);
		}
		
		TimeFrequencyData(PolarizationEnum polarizationA,
											const Image2DCPtr &realA,
											const Image2DCPtr &imaginaryA,
											PolarizationEnum polarizationB,
											const Image2DCPtr &realB,
											const Image2DCPtr &imaginaryB) :
				    _complexRepresentation(ComplexParts)
		{
			_data.reserve(2);
			_data.emplace_back(polarizationA, realA, imaginaryA);
			_data.emplace_back(polarizationB, realB, imaginaryB);
		}
		
		TimeFrequencyData(
			ComplexRepresentation complexRepresentation,
			PolarizationEnum* polarizations,
			size_t polarizationCount,
			Image2DCPtr* images) :
				_complexRepresentation(complexRepresentation)
		{
			_data.reserve(polarizationCount);
			if(complexRepresentation == ComplexParts)
			{
				for(size_t p=0; p!=polarizationCount; p++)
					_data.emplace_back(polarizations[p], images[p*2], images[p*2+1]);
			}
			else {
				for(size_t p=0; p!=polarizationCount; p++)
					_data.emplace_back(polarizations[p], images[p]);
			}
		}
		
		TimeFrequencyData(
			PolarizationEnum* polarizations,
			size_t polarizationCount,
			Image2DCPtr* realImages, Image2DCPtr* imaginaryImages) :
				_complexRepresentation(ComplexParts)
		{
			_data.reserve(polarizationCount);
			for(size_t p=0; p!=polarizationCount; p++)
				_data.emplace_back(polarizations[p], realImages[p], imaginaryImages[p]);
		}
		
		TimeFrequencyData(
			PolarizationEnum* polarizations,
			size_t polarizationCount,
			Image2DPtr* realImages, Image2DPtr* imaginaryImages) :
				_complexRepresentation(ComplexParts)
		{
			_data.reserve(polarizationCount);
			for(size_t p=0; p!=polarizationCount; p++)
				_data.emplace_back(polarizations[p], realImages[p], imaginaryImages[p]);
		}
		
		static TimeFrequencyData FromLinear(
			ComplexRepresentation complexRepresentation,
			const Image2DCPtr& xx,
			const Image2DCPtr& xy,
			const Image2DCPtr& yx,
			const Image2DCPtr& yy)
		{
			TimeFrequencyData data;
			data._complexRepresentation = complexRepresentation;
			if(complexRepresentation == ComplexParts)
				throw BadUsageException("Incorrect construction of time/frequency data: trying to create complex full-Stokes representation from four images");
			data._data.reserve(4);
			data._data.emplace_back(Polarization::XX, xx);
			data._data.emplace_back(Polarization::XY, xy);
			data._data.emplace_back(Polarization::YX, yx);
			data._data.emplace_back(Polarization::YY, yy);
			return data;
		}

		static TimeFrequencyData FromLinear(
			const Image2DCPtr &xxReal, const Image2DCPtr &xxImag,
			const Image2DCPtr &xyReal, const Image2DCPtr &xyImag,
			const Image2DCPtr &yxReal, const Image2DCPtr &yxImag,
			const Image2DCPtr &yyReal, const Image2DCPtr &yyImag)
		{
			TimeFrequencyData data;
			data._complexRepresentation = ComplexParts;
			data._data.reserve(4);
			data._data.emplace_back(Polarization::XX, xxReal, xxImag);
			data._data.emplace_back(Polarization::XY, xyReal, xyImag);
			data._data.emplace_back(Polarization::YX, yxReal, yxImag);
			data._data.emplace_back(Polarization::YY, yyReal, yyImag);
			return data;
		}

		static TimeFrequencyData FromLinear(size_t polarizationCount, Image2DCPtr *realImages, Image2DCPtr *imagImages)
		{
			switch(polarizationCount)
			{
				case 1:
					return TimeFrequencyData(Polarization::StokesI, realImages[0], imagImages[0]);
				case 2:
					return TimeFrequencyData(Polarization::XX, realImages[0], imagImages[0],
																	 Polarization::YY, realImages[1], imagImages[1]);
				case 4:
					return FromLinear(realImages[0], imagImages[0], realImages[1], imagImages[1], realImages[2], imagImages[2], realImages[3], imagImages[3]);
				default:
					throw BadUsageException("Can not create TimeFrequencyData structure with polarization type other than 1, 2 or 4 polarizations using FromLinear().");
			}
		}
		
		bool IsEmpty() const { return _data.empty(); }

		bool HasPolarization(PolarizationEnum polarization) const
		{
			for(const PolarizedTimeFrequencyData& data : _data)
				if(data._polarization == polarization)
					return true;
			return false;
		}
		
		PolarizationEnum GetPolarization(size_t index) const
		{ return _data[index]._polarization; }
		
		bool HasXX() const { return HasPolarization(Polarization::XX); }

		bool HasXY() const { return HasPolarization(Polarization::XY); }

		bool HasYX() const { return HasPolarization(Polarization::YX); }

		bool HasYY() const { return HasPolarization(Polarization::YY); }
		
		/**
		 * This function returns a new Image2D that contains
		 * an image that can be used best for thresholding-like
		 * RFI methods, or visualization. The encapsulated data
		 * may be converted in order to do so.
		 * @return A new image containing the TF-data.
		 */
		Image2DCPtr GetSingleImage() const
		{
			switch(_complexRepresentation)
			{
				case PhasePart:
				case AmplitudePart:
				case RealPart:
				case ImaginaryPart:
					return GetSingleImageFromSingleComplexPart();
				case ComplexParts:
					return GetSingleAbsoluteFromComplex();
			}
			throw BadUsageException("Incorrect complex representation");
		}

		Mask2DCPtr GetSingleMask() const
		{
			return GetCombinedMask();
		}

		std::array<Image2DCPtr,2> GetSingleComplexImage() const
		{
			if(_complexRepresentation != ComplexParts)
				throw BadUsageException("Trying to create single complex image, but no complex data available");
			if(_data.size() != 1)
				throw BadUsageException("Not implemented");
			if(_data[0]._images[0] == nullptr || _data[0]._images[1] == nullptr)
				throw BadUsageException("Requesting non-existing image");
			return std::array<Image2DCPtr,2>{{ _data[0]._images[0], _data[0]._images[1] }};
		}
		
		void Set(PolarizationEnum polarizationType,
			const Image2DCPtr &real,
			const Image2DCPtr &imaginary)
		{
			     _complexRepresentation = ComplexParts;
			_data.clear();
			_data.emplace_back(polarizationType, real, imaginary);
		}

		void SetNoMask()
		{
			for(PolarizedTimeFrequencyData& data : _data)
				data._flagging = nullptr;
		}

		void SetGlobalMask(const Mask2DCPtr &mask)
		{
			SetNoMask();
			for(PolarizedTimeFrequencyData& data : _data)
				data._flagging = mask;
		}

		Mask2DCPtr GetMask(PolarizationEnum polarization) const
		{
			for(const PolarizedTimeFrequencyData& data : _data)
			{
				if(data._polarization == polarization)
				{
					if(data._flagging == nullptr)
						return GetSetMask<false>();
					else
						return data._flagging;
				}
			}
			return GetSingleMask();
		}

		void SetIndividualPolarizationMasks(const Mask2DCPtr* maskPerPolarization)
		{
			for(size_t p=0; p!=_data.size(); ++p)
				_data[p]._flagging = maskPerPolarization[p];
		}

		void SetIndividualPolarizationMasks(const Mask2DPtr* maskPerPolarization)
		{
			for(size_t p=0; p!=_data.size(); ++p)
				_data[p]._flagging = maskPerPolarization[p];
		}

		void SetIndividualPolarizationMasks(const Mask2DCPtr& maskA, const Mask2DCPtr& maskB)
		{
			if(_data.size() != 2)
				throw BadUsageException("Trying to set two individual mask in non-matching time frequency data");
			_data[0]._flagging = maskA;
			_data[1]._flagging = maskB;
		}

		void SetIndividualPolarizationMasks(const Mask2DCPtr& maskA, const Mask2DCPtr& maskB, const Mask2DCPtr& maskC, const Mask2DCPtr& maskD)
		{
			if(_data.size() != 4)
				throw BadUsageException("Trying to set four individual masks in non-matching time frequency data");
			_data[0]._flagging = maskA;
			_data[1]._flagging = maskB;
			_data[2]._flagging = maskC;
			_data[3]._flagging = maskD;
		}

		TimeFrequencyData Make(ComplexRepresentation representation) const;
		
		TimeFrequencyData Make(PolarizationEnum polarization) const
		{
			for(const PolarizedTimeFrequencyData& data : _data)
			{
				if(data._polarization == polarization)
					return TimeFrequencyData(_complexRepresentation, data);
			}
			
			TimeFrequencyData newData;
			size_t
				xxPol = GetPolarizationIndex(Polarization::XX),
				xyPol = GetPolarizationIndex(Polarization::XY),
				yxPol = GetPolarizationIndex(Polarization::YX),
				yyPol = GetPolarizationIndex(Polarization::YY);
			bool hasLinear = xxPol < _data.size() || xyPol < _data.size();
			if(hasLinear)
			{
				if(_complexRepresentation == ComplexParts)
				{
					switch(polarization)
					{
					case Polarization::StokesI:
						newData = TimeFrequencyData(Polarization::StokesI, getFirstSum(xxPol, yyPol), getSecondSum(xxPol, yyPol));
						break;
					case Polarization::StokesQ:
						newData = TimeFrequencyData(Polarization::StokesQ, getFirstDiff(xxPol, yyPol), getSecondDiff(xxPol, yyPol));
						break;
					case Polarization::StokesU:
						newData = TimeFrequencyData(Polarization::StokesU, getFirstSum(xyPol, yxPol), getSecondSum(xyPol, yxPol));
						break;
					case Polarization::StokesV:
						newData = TimeFrequencyData(Polarization::StokesV, getNegRealPlusImag(xyPol, yxPol), getRealMinusImag(xyPol, yxPol));
						break;
					default:
						throw BadUsageException("Polarization not available or not implemented");
					}
				}
				else // _complexRepresentation != ComplexParts
				{
					// TODO should be done on real or imaginary
					switch(polarization)
					{
						case Polarization::StokesI:
							newData = TimeFrequencyData(_complexRepresentation, Polarization::StokesI, getFirstSum(xxPol, yyPol));
							break;
						case Polarization::StokesQ:
							newData = TimeFrequencyData(_complexRepresentation, Polarization::StokesQ, getFirstDiff(xxPol, yyPol));
							break;
						default:
							throw BadUsageException("Requested polarization type not available in time frequency data");
					}
				}
			}
			else {
				size_t
					rrPol = GetPolarizationIndex(Polarization::RR),
					rlPol = GetPolarizationIndex(Polarization::RL),
					lrPol = GetPolarizationIndex(Polarization::LR),
					llPol = GetPolarizationIndex(Polarization::LL);
				bool hasCircular = rrPol < _data.size() || rlPol < _data.size();
				if(hasCircular)
				{
					if(_complexRepresentation == ComplexParts)
					{
						switch(polarization)
						{
						case Polarization::StokesI:
							newData = TimeFrequencyData(Polarization::StokesI, getFirstSum(rrPol, llPol), getSecondSum(rrPol, llPol));
							break;
						case Polarization::StokesQ: // Q = RL + LR
							newData = TimeFrequencyData(Polarization::StokesQ, getFirstSum(rlPol, rlPol), getSecondSum(rlPol, lrPol));
							break;
						case Polarization::StokesU: // U_r = RL_i - LR_i, U_i = -RL_r + LR_r
							newData = TimeFrequencyData(Polarization::StokesU, getSecondDiff(rlPol, lrPol), getFirstDiff(lrPol, rlPol));
							break;
						case Polarization::StokesV: // V = RR - LL
							newData = TimeFrequencyData(Polarization::StokesV, getFirstDiff(rrPol, llPol), getSecondDiff(rrPol, llPol));
							break;
						default:
							throw BadUsageException("Requested polarization type not available in time frequency data");
							break;
						}
					}
					else {
						switch(polarization)
						{
						case Polarization::StokesI:
							newData = TimeFrequencyData(_complexRepresentation, Polarization::StokesI, getFirstSum(rrPol, llPol));
							break;
						default:
							throw BadUsageException("Requested conversion is not implemented for circular polarizations");
						}
					}
				}
				else
					throw BadUsageException("Trying to convert the polarization in time frequency data in an invalid way");
			}
			newData.SetGlobalMask(GetMask(polarization));
			return newData;
		}

		Image2DCPtr GetRealPart() const
		{
			if(_data.size() != 1)
			{
				throw BadUsageException("This tfdata contains !=1 polarizations; which real part should I return?");
			} else if(_complexRepresentation == ComplexParts || _complexRepresentation == RealPart) {
				return _data[0]._images[0];
			} else {
				throw BadUsageException("Trying to retrieve real part from time frequency data in which values are not stored as complex or reals");
			}
		}

		Image2DCPtr GetImaginaryPart() const
		{
			if(_data.size() != 1)
			{
				throw BadUsageException("This tfdata contains !=1 polarizations; which imaginary part should I return?");
			} else if(_complexRepresentation == ComplexParts) {
				return _data[0]._images[1];
			} else if(_complexRepresentation == ImaginaryPart) {
				return _data[0]._images[0];
			} else {
				throw BadUsageException("Trying to retrieve imaginary part from time frequency data in which values are not stored as complex or imaginary representation");
			}
		}

		size_t ImageWidth() const
		{
			if(!_data.empty() && _data[0]._images[0] != nullptr)
				return _data[0]._images[0]->Width();
			else
				return 0;
		}
		
		size_t ImageHeight() const
		{
			if(!_data.empty() && _data[0]._images[0] != nullptr)
				return _data[0]._images[0]->Height();
			else
				return 0;
		}

		enum ComplexRepresentation ComplexRepresentation() const
		{
			return _complexRepresentation;
		}
		
		std::vector<PolarizationEnum> Polarizations() const
		{
			std::vector<PolarizationEnum> pols;
			for(const PolarizedTimeFrequencyData& data : _data)
				pols.push_back(data._polarization);
			return pols;
		}

		void Subtract(const TimeFrequencyData &rhs)
		{
			if(rhs._data.size() != _data.size() || rhs._complexRepresentation != _complexRepresentation)
			{
				std::stringstream s;
				s << "Can not subtract time-frequency data: they do not have the same number of polarizations or complex representation! ("
					<< rhs._data.size() << " vs. " << _data.size() << ")";
				throw BadUsageException(s.str());
			}
			for(size_t i=0; i!=_data.size(); ++i)
			{
				if(_data[i]._images[0] == nullptr)
					throw BadUsageException("Can't subtract TFs with unset image data");
				_data[i]._images[0].reset(new Image2D(Image2D::MakeFromDiff(*_data[i]._images[0], *rhs._data[i]._images[0])));
				if(_data[i]._images[1])
					_data[i]._images[1].reset(new Image2D(Image2D::MakeFromDiff(*_data[i]._images[1], *rhs._data[i]._images[1])));
			}
		}

		void SubtractAsRHS(const TimeFrequencyData &lhs)
		{
			if(lhs._data.size() != _data.size() || lhs._complexRepresentation != _complexRepresentation)
			{
				std::stringstream s;
				s << "Can not subtract time-frequency data: they do not have the same number of polarizations or complex representation! ("
					<< lhs._data.size() << " vs. " << _data.size() << ")";
				throw BadUsageException(s.str());
			}
			for(size_t i=0; i!=_data.size(); ++i)
			{
				if(_data[i]._images[0] == nullptr)
					throw BadUsageException("Can't subtract TFs with unset image data");
				_data[i]._images[0].reset(new Image2D(Image2D::MakeFromDiff(*lhs._data[i]._images[0], *_data[i]._images[0])));
				if(_data[i]._images[1])
					_data[i]._images[1].reset(new Image2D(Image2D::MakeFromDiff(*lhs._data[i]._images[1], *_data[i]._images[1])));
			}
		}

		static TimeFrequencyData MakeFromDiff(const TimeFrequencyData& lhs, const TimeFrequencyData& rhs)
		{
			if(lhs._data.size() != rhs._data.size() || lhs._complexRepresentation != rhs._complexRepresentation)
			{
				std::stringstream s;
				s << "Can not subtract time-frequency data: they do not have the same number of polarizations or complex representation! ("
					<< lhs._data.size() << " vs. " << rhs._data.size() << ")";
				throw BadUsageException(s.str());
			}
			TimeFrequencyData data(lhs);
			for(size_t i=0;i<lhs._data.size();++i)
			{
				if(lhs._data[i]._images[0] == nullptr)
					throw BadUsageException("Can't subtract TFs with unset image data");
				data._data[i]._images[0].reset(new Image2D(Image2D::MakeFromDiff(*lhs._data[i]._images[0], *rhs._data[i]._images[0])));
				if(lhs._data[i]._images[1])
					data._data[i]._images[1].reset(new Image2D(Image2D::MakeFromDiff(*lhs._data[i]._images[1], *rhs._data[i]._images[1])));
			}
			return data;
		}

		static TimeFrequencyData MakeFromSum(const TimeFrequencyData& lhs, const TimeFrequencyData& rhs)
		{
			if(lhs._data.size() != rhs._data.size() || lhs._complexRepresentation != rhs._complexRepresentation)
			{
				std::stringstream s;
				s << "Can not add time-frequency data: they do not have the same number of polarizations or complex representation! ("
					<< lhs._data.size() << " vs. " << rhs._data.size() << ")";
				throw BadUsageException(s.str());
			}
			TimeFrequencyData data(lhs);
			for(size_t i=0;i<lhs._data.size();++i)
			{
				if(lhs._data[i]._images[0] == nullptr)
					throw BadUsageException("Can't add TFs with unset image data");
				data._data[i]._images[0].reset(new Image2D(Image2D::MakeFromSum(*lhs._data[i]._images[0], *rhs._data[i]._images[0])));
				if(lhs._data[i]._images[1])
					data._data[i]._images[1].reset(new Image2D(Image2D::MakeFromSum(*lhs._data[i]._images[1], *rhs._data[i]._images[1])));
			}
			return data;
		}

		size_t ImageCount() const {
			size_t masks = 0;
			for(const PolarizedTimeFrequencyData& data : _data)
			{
				if(data._images[0]) ++masks;
				if(data._images[1]) ++masks;
			}
			return masks; 
		}
		
		size_t MaskCount() const {
			size_t masks = 0;
			for(const PolarizedTimeFrequencyData& data : _data)
				if(data._flagging) ++masks;
			return masks; 
		}

		const Image2DCPtr& GetImage(size_t imageIndex) const
		{
			size_t index = 0;
			for(const PolarizedTimeFrequencyData& data : _data)
			{
				if(data._images[0])
				{
					if(index == imageIndex)
						return data._images[0];
					++index;
				}
				if(data._images[1])
				{
					if(index == imageIndex)
						return data._images[1];
					++index;
				}
			}
			throw BadUsageException("Invalid image index in GetImage()");
		}
		const Mask2DCPtr& GetMask(size_t maskIndex) const
		{
			size_t index = 0;
			for(const PolarizedTimeFrequencyData& data : _data)
			{
				if(data._flagging)
				{
					if(index == maskIndex)
						return data._flagging;
					++index;
				}
			}
			std::ostringstream msg;
			msg << "Invalid mask index of " << maskIndex << " in GetMask(): mask count is " << MaskCount();
			throw BadUsageException(msg.str());
		}
		
		void SetImage(size_t imageIndex, const Image2DCPtr& image)
		{
			size_t index = 0;
			for(PolarizedTimeFrequencyData& data : _data)
			{
				if(data._images[0])
				{
					if(index == imageIndex)
					{
						data._images[0] = image;
						return;
					}
					++index;
				}
				if(data._images[1])
				{
					if(index == imageIndex)
					{
						data._images[1] = image;
						return;
					}
					++index;
				}
			}
			throw BadUsageException("Invalid image index in SetImage()");
		}
		
		void SetImage(size_t imageIndex, Image2DCPtr&& image)
		{
			size_t index = 0;
			for(PolarizedTimeFrequencyData& data : _data)
			{
				if(data._images[0])
				{
					if(index == imageIndex)
					{
						data._images[0] = std::move(image);
						return;
					}
					++index;
				}
				if(data._images[1])
				{
					if(index == imageIndex)
					{
						data._images[1] = std::move(image);
						return;
					}
					++index;
				}
			}
			throw BadUsageException("Invalid image index in SetImage()");
		}
		
		void SetMask(size_t maskIndex, const Mask2DCPtr& mask)
		{
			size_t index = 0;
			for(PolarizedTimeFrequencyData& data : _data)
			{
				if(data._flagging)
				{
					if(index == maskIndex)
					{
						data._flagging = mask;
						return;
					}
					++index;
				}
			}
			throw BadUsageException("Invalid mask index in SetMask()");
		}
		
		void SetMask(size_t maskIndex, Mask2DCPtr&& mask)
		{
			size_t index = 0;
			for(PolarizedTimeFrequencyData& data : _data)
			{
				if(data._flagging)
				{
					if(index == maskIndex)
					{
						data._flagging = std::move(mask);
						return;
					}
					++index;
				}
			}
			throw BadUsageException("Invalid mask index in SetMask()");
		}
		
		void SetMask(const TimeFrequencyData& source)
		{
			source.CopyFlaggingTo(this);
		}

		static TimeFrequencyData MakeFromComplexCombination(const TimeFrequencyData &real, const TimeFrequencyData &imaginary);

		static TimeFrequencyData MakeFromPolarizationCombination(const TimeFrequencyData &xx, const TimeFrequencyData &xy, const TimeFrequencyData &yx, const TimeFrequencyData &yy);

		static TimeFrequencyData MakeFromPolarizationCombination(const TimeFrequencyData &first, const TimeFrequencyData &second);

		void SetImagesToZero();
		template<bool Value>
		void SetMasksToValue()
		{
			if(!IsEmpty())
			{
				Mask2DPtr mask = Mask2D::CreateSetMaskPtr<Value>(ImageWidth(), ImageHeight());
				for(PolarizedTimeFrequencyData& data : _data)
				{
					data._flagging = mask;
				}
			}
		}
		
		void MultiplyImages(long double factor);
		void JoinMask(const TimeFrequencyData &other);

		void Trim(unsigned timeStart, unsigned freqStart, unsigned timeEnd, unsigned freqEnd)
		{
			for(PolarizedTimeFrequencyData& data : _data)
			{
				if(data._images[0])
					data._images[0].reset(new Image2D(data._images[0]->Trim(timeStart, freqStart, timeEnd, freqEnd)));
				if(data._images[1])
					data._images[1].reset(new Image2D(data._images[1]->Trim(timeStart, freqStart, timeEnd, freqEnd)));
				if(data._flagging)
					data._flagging.reset(new Mask2D(data._flagging->Trim(timeStart, freqStart, timeEnd, freqEnd)));
			}
		}
		
		std::string Description() const
		{
			std::ostringstream s;
			switch(_complexRepresentation)
			{
				case RealPart: s << "Real component of "; break;
				case ImaginaryPart: s << "Imaginary component of "; break;
				case PhasePart: s << "Phase of "; break;
				case AmplitudePart: s << "Amplitude of "; break;
				case ComplexParts:
					break;
			}
			if(_data.empty())
				s << "empty";
			else
			{
				s << "(" << Polarization::TypeToFullString(_data[0]._polarization);
				for(size_t i=1; i!=_data.size(); ++i)
					s << "," << Polarization::TypeToFullString(_data[i]._polarization);
				s << ")";
			}
			return s.str();
		}
		
		size_t PolarizationCount() const
		{
			return _data.size();
		}
		
		TimeFrequencyData MakeFromPolarizationIndex(size_t index) const
		{
			return TimeFrequencyData(_complexRepresentation, _data[index]);
		}
		
		void SetPolarizationData(size_t polarizationIndex, const TimeFrequencyData& data)
		{
			if(data.PolarizationCount() != 1)
				throw BadUsageException("Trying to set multiple polarizations by single polarization index");
			else if(data.ComplexRepresentation() != ComplexRepresentation())
				throw BadUsageException("Trying to combine TFData's with different complex representations");
			else
				_data[polarizationIndex] = data._data[0];
		}

		void SetPolarizationData(size_t polarizationIndex, TimeFrequencyData&& data)
		{
			if(data.PolarizationCount() != 1)
				throw BadUsageException("Trying to set multiple polarizations by single polarization index");
			else if(data.ComplexRepresentation() != ComplexRepresentation())
				throw BadUsageException("Trying to combine TFData's with different complex representations");
			else {
				_data[polarizationIndex] = std::move(data._data[0]);
				data._data.clear();
			}
		}

		void SetImageSize(size_t width, size_t height)
		{
			for(size_t i=0;i<_data.size();++i)
			{
				if(_data[i]._images[0])
					_data[i]._images[0] = Image2D::CreateUnsetImagePtr(width, height);
				if(_data[i]._images[1])
					_data[i]._images[1] = Image2D::CreateUnsetImagePtr(width, height);
				if(_data[i]._flagging)
					_data[i]._flagging = Mask2D::CreateUnsetMaskPtr(width, height);
			}
		}

		void CopyFrom(const TimeFrequencyData &source, size_t destX, size_t destY)
		{
			if(source._data.size() != _data.size())
				throw BadUsageException("CopyFrom: tf data do not match");
			for(size_t i=0;i<_data.size();++i)
			{
				if(_data[i]._images[0])
				{
					Image2D image(*_data[i]._images[0]);
					image.CopyFrom(*source._data[i]._images[0], destX, destY);
					_data[i]._images[0].reset(new Image2D(std::move(image)));
				}
				if(_data[i]._images[1])
				{
					Image2D image(*_data[i]._images[1]);
					image.CopyFrom(*source._data[i]._images[1], destX, destY);
					_data[i]._images[1].reset(new Image2D(std::move(image)));
				}
				if(_data[i]._flagging)
				{
					Mask2D mask(*_data[i]._flagging);
					mask.CopyFrom(*source._data[i]._flagging, destX, destY);
					_data[i]._flagging.reset(new Mask2D(std::move(mask)));
				}
			}
		}
		
		/**
		 * Will return true when this is the imaginary part of the visibilities. Will throw
		 * an exception when the data is neither real nor imaginary.
		 */
		bool IsImaginary() const
		{
			if(ComplexRepresentation() == RealPart)
				return false;
			else if(ComplexRepresentation() == ImaginaryPart)
				return true;
			else
				throw std::runtime_error("Data is not real or imaginary");
		}
		
		/**
		 * Returns the data index of the given polarization, or _data.size() if
		 * not found.
		 */
		size_t GetPolarizationIndex(PolarizationEnum polarization) const
		{
			for(size_t i=0; i!=_data.size(); ++i)
				if(_data[i]._polarization == polarization)
					return i;
			return _data.size();
		}

	private:
		Image2DCPtr GetSingleAbsoluteFromComplex() const
		{
			if(_data.size() == 4)
				return GetAbsoluteFromComplex(getFirstSum(0, 3), getSecondSum(0, 3));
			else if(_data.size() == 2)
				return GetAbsoluteFromComplex(getFirstSum(0, 1), getSecondSum(0, 1));
			else
				return getAbsoluteFromComplex(0);
		}

		Image2DCPtr GetSingleImageFromSingleComplexPart() const
		{
			if(_data.size() == 4)
			{
				if(_complexRepresentation == PhasePart)
					return getSinglePhaseFromTwoPolPhase(0, 3);
				else
					return getFirstSum(0, 3);
			}
			if(_data.size() == 2)
			{
				if(_complexRepresentation == PhasePart)
					return getSinglePhaseFromTwoPolPhase(0, 1);
				else
					return getFirstSum(0, 1);
			}
			else // if(_data.size() == 1)
				return _data[0]._images[0];
		}

		void CopyFlaggingTo(TimeFrequencyData *data) const
		{
			if(MaskCount() == 0)
				data->SetNoMask();
			else if(MaskCount() == 1)
				data->SetGlobalMask(GetMask(0));
			else
			{
				if(_data.size() == data->_data.size())
				{
					for(size_t i=0; i!=_data.size(); ++i)
						data->_data[i]._flagging = _data[i]._flagging;
				} else
					throw BadUsageException("Trying to copy flagging from incompatible time frequency data");
			}
		}
		
		Image2DCPtr getAbsoluteFromComplex(size_t polIndex) const
		{
			return GetAbsoluteFromComplex(_data[polIndex]._images[0], _data[polIndex]._images[1]);
		}

		Image2DCPtr GetAbsoluteFromComplex(const Image2DCPtr &real, const Image2DCPtr &imag) const;

		Image2DCPtr getFirstSum(size_t dataIndexA, size_t dataIndexB) const
		{
			if(dataIndexA >= _data.size())
				throw BadUsageException("Polarization not available");
			if(dataIndexB >= _data.size())
				throw BadUsageException("Polarization not available");
			return GetSum(_data[dataIndexA]._images[0], _data[dataIndexB]._images[0]);
		}
		
		Image2DCPtr getSecondSum(size_t dataIndexA, size_t dataIndexB) const
		{
			if(dataIndexA >= _data.size())
				throw BadUsageException("Polarization not available");
			if(dataIndexB >= _data.size())
				throw BadUsageException("Polarization not available");
			return GetSum(_data[dataIndexA]._images[1], _data[dataIndexB]._images[1]);
		}
		
		Image2DCPtr getFirstDiff(size_t dataIndexA, size_t dataIndexB) const
		{
			if(dataIndexA >= _data.size())
				throw BadUsageException("Polarization not available");
			if(dataIndexB >= _data.size())
				throw BadUsageException("Polarization not available");
			return GetDifference(_data[dataIndexA]._images[0], _data[dataIndexB]._images[0]);
		}
		
		Image2DCPtr getSecondDiff(size_t dataIndexA, size_t dataIndexB) const
		{
			if(dataIndexA >= _data.size())
				throw BadUsageException("Polarization not available");
			if(dataIndexB >= _data.size())
				throw BadUsageException("Polarization not available");
			return GetDifference(_data[dataIndexA]._images[1], _data[dataIndexB]._images[1]);
		}
		
		Image2DCPtr getNegRealPlusImag(size_t xyIndex, size_t yxIndex) const
		{
			if(xyIndex >= _data.size())
				throw BadUsageException("Polarization not available");
			if(yxIndex >= _data.size())
				throw BadUsageException("Polarization not available");
			return GetNegatedSum(_data[xyIndex]._images[1], _data[yxIndex]._images[0]);
		}
		
		Image2DCPtr getRealMinusImag(size_t xyIndex, size_t yxIndex) const
		{
			if(xyIndex >= _data.size())
				throw BadUsageException("Polarization not available");
			if(yxIndex >= _data.size())
				throw BadUsageException("Polarization not available");
			return GetDifference(_data[xyIndex]._images[0], _data[yxIndex]._images[1]);
		}
		
		Image2DCPtr GetSum(const Image2DCPtr &left, const Image2DCPtr &right) const;
		Image2DCPtr GetNegatedSum(const Image2DCPtr &left, const Image2DCPtr &right) const;
		Image2DCPtr GetDifference(const Image2DCPtr &left, const Image2DCPtr &right) const;

		Image2DCPtr getSinglePhaseFromTwoPolPhase(size_t polA, size_t polB) const;
		//Image2DCPtr GetZeroImage() const;
		template<bool InitValue>
		Mask2DCPtr GetSetMask() const
		{
			if(ImageWidth() == 0 || ImageHeight() == 0)
				throw BadUsageException("Can't make a mask without an image");
			return Mask2D::CreateSetMaskPtr<InitValue>(ImageWidth(), ImageHeight());
		}
		Mask2DCPtr GetCombinedMask() const;

		struct PolarizedTimeFrequencyData
		{
			PolarizedTimeFrequencyData() :
				_images{nullptr, nullptr},
				_flagging(nullptr),
				_polarization(Polarization::StokesI)
			{ }
			PolarizedTimeFrequencyData(PolarizationEnum polarization, const Image2DCPtr& image) :
				_images{image, nullptr},
				_flagging(nullptr),
				_polarization(polarization)
			{ }
				
			PolarizedTimeFrequencyData(PolarizationEnum polarization, const Image2DCPtr& imageA, const Image2DCPtr& imageB) :
				_images{imageA, imageB},
				_flagging(nullptr),
				_polarization(polarization)
			{ }
				
			// Second image is only filled when complex representation = complex parts
			Image2DCPtr _images[2];
			Mask2DCPtr _flagging;
			PolarizationEnum _polarization;
		};
		
		TimeFrequencyData(enum ComplexRepresentation complexRepresentation, const PolarizedTimeFrequencyData& source) :
			_complexRepresentation(complexRepresentation), _data(1, source)
		{
		}

		enum ComplexRepresentation _complexRepresentation;
		
		std::vector<PolarizedTimeFrequencyData> _data;
};

#endif