File: tScaledComplexData.cc

package info (click to toggle)
casacore 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,912 kB
  • sloc: cpp: 471,569; fortran: 16,372; ansic: 7,416; yacc: 4,714; lex: 2,346; sh: 1,865; python: 629; perl: 531; sed: 499; csh: 201; makefile: 32
file content (290 lines) | stat: -rw-r--r-- 10,172 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
279
280
281
282
283
284
285
286
287
288
289
290
//# tScaledComplexData.cc: Test program for class ScaledComplexData
//# Copyright (C) 1999,2000,2002
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This program is free software; you can redistribute it and/or modify it
//# under the terms of the GNU General Public License as published by the Free
//# Software Foundation; either version 2 of the License, or (at your option)
//# any later version.
//#
//# This program is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
//# more details.
//#
//# You should have received a copy of the GNU General Public License along
//# with this program; if not, write to the Free Software Foundation, Inc.,
//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: casa-feedback@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA

#include <casacore/tables/Tables/TableDesc.h>
#include <casacore/tables/Tables/SetupNewTab.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/ScaColDesc.h>
#include <casacore/tables/Tables/ArrColDesc.h>
#include <casacore/tables/DataMan/ScaledComplexData.h>
#include <casacore/tables/Tables/ArrayColumn.h>
#include <casacore/tables/Tables/ScalarColumn.h>
#include <casacore/casa/BasicSL/Complex.h>
#include <casacore/casa/Arrays/Cube.h>
#include <casacore/casa/Arrays/Matrix.h>
#include <casacore/casa/Arrays/IPosition.h>
#include <casacore/casa/Arrays/ArrayMath.h>
#include <casacore/casa/Arrays/ArrayLogical.h>
#include <casacore/casa/Arrays/Slicer.h>
#include <casacore/casa/Arrays/Slice.h>
#include <casacore/casa/IO/ArrayIO.h>
#include <casacore/tables/Tables/TableError.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/iostream.h>

#include <casacore/casa/namespace.h>
// <summary> Test program for class ScaledComplexData </summary>

// This program tests the virtual column engine ScaledComplexData.
// The results are written to stdout. The script executing this program,
// compares the results with the reference output file.

void a();
void b();

int main () {
    try {
	a();
	b();
    } catch (std::exception& x) {
	cout << "Caught an exception: " << x.what() << endl;
	return 1;
    } 
    return 0;                           // exit with success status
}

// First build a description.
void a()
{
    // First register the virtual column engine.
    ScaledComplexData<Complex,Short>::registerClass();
    ScaledComplexData<DComplex,Int>::registerClass();

    // Build the table description.
    TableDesc td("", "1", TableDesc::Scratch);
    td.addColumn (ArrayColumnDesc<Int> ("target1"));
    td.addColumn (ArrayColumnDesc<DComplex> ("source1"));
    td.addColumn (ArrayColumnDesc<Short> ("target2"));
    td.addColumn (ArrayColumnDesc<Complex> ("source2","",
					  IPosition(2,3,4),
					  ColumnDesc::Direct));
    td.addColumn (ArrayColumnDesc<Int> ("target3", "",
					IPosition(3,2,3,4),
					ColumnDesc::Direct));
    td.addColumn (ArrayColumnDesc<DComplex> ("source3", "",
					   IPosition(2,3,4),
					   ColumnDesc::Direct));
    td.addColumn (ScalarColumnDesc<DComplex> ("scale3"));

    // Now create a new table from the description.
    SetupNewTable newtab("tScaledComplexData_tmp.data", td, Table::New);
    // Create the virtual column engine with the scale factors
    // and bind the columns to them.
    ScaledComplexData<DComplex,Int> engine1("source1", "target1",
					    DComplex(2.0, 3.0),
					    DComplex(4.0, 5.0));
    ScaledComplexData<Complex,Short> engine2("source2", "target2",
					     Complex(6.0, 7.0),
					     Complex(2.0, 3.0));
    ScaledComplexData<DComplex,Int> engine3("source3", "target3", "scale3");
    newtab.bindColumn ("source1", engine1);
    newtab.bindColumn ("source2", engine2);
    newtab.bindColumn ("source3", engine3);
    Table tab(newtab, 10);

    // Fill the table via the virtual columns.
    ArrayColumn<DComplex> source1 (tab, "source1");
    ArrayColumn<Complex> source2 (tab, "source2");
    ArrayColumn<DComplex> source3 (tab, "source3");
    ScalarColumn<DComplex> scale3 (tab,"scale3");

    Matrix<DComplex> arrd(IPosition(2,3,4));
    Matrix<Complex> arrf(IPosition(2,3,4));
    Int i=0;
    for (uInt i2=0; i2<4; i2++) {
        for (uInt i1=0; i1<3; i1++) {
	    arrd(i1,i2) = DComplex(4+i, 2+i);
	    arrf(i1,i2) = Complex(8+i, 3+i);
	    i += 420;
	}
    }
    for (i=0; i<10; i++) {
	scale3.put (i, Complex(1+i%3, 1+i%4));
	source1.put (i, arrd);
	source2.put (i, arrf);
	source3.put (i, arrd + DComplex(2, 22));
	arrd += DComplex(840, 840);
	arrf += Complex(840, 840);
    }

    //# Do an erroneous thing.
    //# 2005-02-15 GvD: this test does not work correctly, because the
    //# exception calls the BaseTable dtor. In there the table directory
    //# gets removed, but there is still an open file which NFS renames
    //# to .nfs... So the remove throws an exception resulting in an abort.
    //# So all open files should be closed first (in one way or another).
        SetupNewTable newtab2("tScaledComplexData_tmp.dat2", td, Table::Scratch);
        newtab2.bindColumn ("source2", engine1);
        try {
    	Table tab2(newtab2, 10);                // bound to incorrect column
        } catch (std::exception& x) {
    	cout << x.what() << endl;
        } 
    }

void b()
{
    // Read back the table.
    Table tab("tScaledComplexData_tmp.data");
    ArrayColumn<DComplex> source1 (tab, "source1");
    ArrayColumn<Complex> source2 (tab, "source2");
    ArrayColumn<DComplex> source3 (tab, "source3");
    ArrayColumn<Int> target1 (tab, "target1");
    ArrayColumn<Short> target2 (tab, "target2");
    ArrayColumn<Int> target3 (tab, "target3");
    Cube<Int> arri1(IPosition(3,2,3,4));
    Cube<Int> arri3(IPosition(3,2,3,4));
    Cube<Int> arrvali(IPosition(3,2,3,4));
    Cube<Short> arrc2(IPosition(3,2,3,4));
    Cube<Short> arrvalc(IPosition(3,2,3,4));
    Matrix<DComplex> arrd1(IPosition(2,3,4));
    Matrix<DComplex> arrd3(IPosition(2,3,4));
    Matrix<DComplex> arrvald(IPosition(2,3,4));
    Matrix<Complex> arrf2(IPosition(2,3,4));
    Matrix<Complex> arrvalf(IPosition(2,3,4));
    Matrix<DComplex> arrvalslice(arrvald(Slice(0,1,2),Slice(0,2,2)));
    Slice tmp;
    Slicer nslice (tmp, tmp, Slicer::endIsLength);
    Slicer nslice2(Slice(0,1,2), Slice(0,2,2), Slicer::endIsLength);
    for (uInt j=0; j<10; j++) {
        { 
	  Int sc1 = 1+j%3;
	  Int sc2 = 1+j%4;
	  Int i=0;
	  for (uInt i2=0; i2<4; i2++) {
	    for (uInt i1=0; i1<3; i1++) {
	      Int val = j*840 + i;
	      arrd1(i1,i2) = DComplex(val+4, val+2);
	      arrf2(i1,i2) = Complex(val+8, val+3);
	      arrd3(i1,i2) = arrd1(i1,i2) + DComplex(2,22);
	      arri1(0,i1,i2) = (val+4 - 4)/2;
	      arri1(1,i1,i2) = (val+2 - 5)/3;
	      arrc2(0,i1,i2) = (val+8 - 2)/6;
	      arrc2(1,i1,i2) = (val+3 - 3)/7;
	      arri3(0,i1,i2) = (val+4+2)/sc1;
	      arri3(1,i1,i2) = (val+2+22)/sc2;
	      i += 420;
	    }
	  }
        }
	cout << "get row " << j << endl;
	source1.get (j, arrvald);
	if (!allEQ (arrvald, arrd1)) {
	    cout << "error in source1 in row " << j << endl;
	    cout << arrvald << endl;
	    cout << arrd1 << endl;
	}
	target1.get (j, arrvali);
	if (!allEQ (arrvali, arri1)) {
	    cout << "error in target1 in row " << j << endl;
	    cout << arrvali << endl;
	    cout << arri1 << endl;
	}
	source2.get (j, arrvalf);
	if (!allEQ (arrvalf, arrf2)) {
	    cout << "error in source2 in row " << j << endl;
	    cout << arrvalf << endl;
	    cout << arrf2 << endl;
	}
	target2.get (j, arrvalc);
	if (!allEQ (arrvalc, arrc2)) {
	    cout << "error in target2 in row " << j << endl;
	    cout << arrvalc << endl;
	    cout << arrc2 << endl;
	}
	source3.get (j, arrvald);
	if (!allEQ (arrvald, arrd3)) {
	    cout << "error in source3 in row " << j << endl;
	    cout << arrvald << endl;
	    cout << arrd3 << endl;
	}
	target3.get (j, arrvali);
	if (!allEQ (arrvali, arri3)) {
	    cout << "error in target3 in row " << j << endl;
	    cout << arrvali << endl;
	    cout << arri3 << endl;
	}
	source1.getSlice (j, nslice, arrvald);
	if (!allEQ (arrvald, arrd1)) {
	    cout << "error in source1 (entire slice) in row " << j << endl;
	    cout << arrvald << endl;
	    cout << arrd1 << endl;
	}
	source1.getSlice (j, nslice2, arrvalslice);
	if (!allEQ (arrvald, arrd1)) {
	    cout << "error in source1 (partial slice) in row " << j << endl;
	    cout << arrvald << endl;
	    cout << arrd1 << endl;
	}
    }

    // Now test getting the columns.
    {
      Cube<DComplex> arrd1(IPosition(3,3,4,10));
      Slicer nslice2(Slice(0,2,1), Slice(1,2,2), Slicer::endIsLength);
      for (uInt j=0; j<10; j++) {
	Int i = 0;
	for (uInt i2=0; i2<4; i2++) {
	  for (uInt i1=0; i1<3; i1++) {
	    Int val = j*840 + i;
	    arrd1(i1,i2,j) = DComplex(val+4, val+2);
	    i += 420;
	  }
	}
      }
      {
	Cube<DComplex> arrvald = source1.getColumn();
	if (!allEQ (arrvald, arrd1)) {
	  cout << "error in source1 getcolumn " << endl;
	  cout << arrvald << endl;
	  cout << arrd1 << endl;
	}
      }
      {
	Cube<DComplex> arrvald = source1.getColumnRange(Slice(1,4,2));
	if (!allEQ (arrvald, arrd1(Slice(0,3,1),Slice(0,4,1),Slice(1,4,2)))) {
	  cout << "error in source1 getcolumnrange " << endl;
	  cout << arrvald << endl;
	  cout << arrd1 << endl;
	}
      }
      {
	Cube<DComplex> arrvald = source1.getColumn(nslice2);
	if (!allEQ (arrvald, arrd1(Slice(0,2,1),Slice(1,2,2),Slice(0,10,1)))) {
	  cout << "error in source1 getcolumnslice " << endl;
	  cout << arrvald << endl;
	  cout << arrd1 << endl;
	}
      }
      {
	Cube<DComplex> arrvald = source1.getColumnRange(Slice(1,4,2), nslice2);
	if (!allEQ (arrvald, arrd1(Slice(0,2,1),Slice(1,2,2),Slice(1,4,2)))) {
	  cout << "error in source1 getcolumnrangeslice " << endl;
	  cout << arrvald << endl;
	  cout << arrd1 << endl;
	}
      }
    }
}