File: tForwardColRow.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 (275 lines) | stat: -rw-r--r-- 9,814 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
//# tForwardColRow.cc: Test program for class ForwardColumn
//# Copyright (C) 1995,1996,1997,1998,1999,2000,2001,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/casa/stdio.h>

#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/ForwardColRow.h>
#include <casacore/tables/DataMan/ForwardCol.h>
#include <casacore/tables/Tables/ArrayColumn.h>
#include <casacore/tables/Tables/ScalarColumn.h>
#include <casacore/tables/DataMan/StManAipsIO.h>
#include <casacore/tables/DataMan/IncrementalStMan.h>
#include <casacore/casa/Arrays/Cube.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/tables/Tables/TableError.h>
#include <casacore/casa/iostream.h>

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

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

TableDesc makeDesc();
void a (const TableDesc&);
void c (const TableDesc&);
void check(const String& tableName, Int abOffset, Int acOffset);

int main ()
{
    try {
	TableDesc td = makeDesc();
	a (td);
	check("tForwardColRow_tmp.data0", 0, 0);
	check("tForwardColRow_tmp.data1", 0, 0);
	c (td);
	check("tForwardColRow_tmp.data0", 0, 0);
	check("tForwardColRow_tmp.data1", 0, 0);
	check("tForwardColRow_tmp.data2", 0, 0);
	check("tForwardColRow_tmp.data3", 0, 0);
    } catch (std::exception& x) {
	cout << "Caught an exception: " << x.what() << endl;
	return 1;
    } 
    return 0;                           // exit with success status
}

// First build a description.
TableDesc makeDesc() {
    // First register the virtual column engines.
    ForwardColumnEngine::registerClass();
    ForwardColumnIndexedRowEngine::registerClass();

    // Build the table description.
    TableDesc td("tTableDesc", "1", TableDesc::Scratch);
    td.comment() = "A test of class ForwardColumn";
    td.addColumn (ScalarColumnDesc<uInt>("row"));
    td.addColumn (ScalarColumnDesc<Int>("ab","Comment for column ab"));
    td.addColumn (ScalarColumnDesc<Int>("ac"));
    td.addColumn (ScalarColumnDesc<uInt>("ad","comment for ad"));
    td.addColumn (ScalarColumnDesc<float>("ae"));
    td.addColumn (ScalarColumnDesc<String>("af"));
    td.addColumn (ScalarColumnDesc<DComplex>("ag"));
    td.addColumn (ArrayColumnDesc<float>("arr1",3,ColumnDesc::Direct));
    td.addColumn (ArrayColumnDesc<float>("arr2",0));
    td.addColumn (ArrayColumnDesc<float>("arr3",0,ColumnDesc::Direct));
    return td;
}

void a (const TableDesc& td)
{
    // Now create a new table from the description.
    SetupNewTable newtab("tForwardColRow_tmp.data0", td, Table::New);
    newtab.setShapeColumn("arr1",IPosition(3,2,3,4));
    newtab.setShapeColumn("arr3",IPosition(3,2,3,4));
    Table tab(newtab, 10);

    ScalarColumn<Int> ab1(tab,"ab");
    ScalarColumn<Int> ab2(tab,"ab");
    ScalarColumn<Int> ac (tab,"ac");
    ScalarColumn<uInt> ad(tab,"ad");
    ScalarColumn<float> ae(tab,"ae");
    ScalarColumn<String> af(tab,"af");
    TableColumn ag1(tab,"ag");
    ScalarColumn<DComplex> ag(tab,"ag");
    ArrayColumn<float> arr1(tab,"arr1");
    ArrayColumn<float> arr2(tab,"arr2");
    ArrayColumn<float> arr3(tab,"arr3");
    Cube<float> arrf(IPosition(3,2,3,4));
    uInt i;
    char str[8];
    indgen (arrf);
    for (i=0; i<10; i++) {
	ab1.put (i, i);
	ac.put (i, i+1);
	ad.put (i, i+2);
	ae.put (i, i+3);
	snprintf (str, sizeof(str), "V%i", i);
	af.put (i, str);
	arr1.put(i,arrf);
	arr2.put(i,arrf);
	arr3.put(i,arrf);
	arrf += (float)(arrf.nelements());
    }
    ag1.putColumn (ad);

    // Now use the description to make a table, which will use the
    // forwarding engine.
    SetupNewTable newtab1("tForwardColRow_tmp.data1", td, Table::New);
    newtab1.setShapeColumn("arr1",IPosition(3,2,3,4));
    newtab1.setShapeColumn("arr3",IPosition(3,2,3,4));
    ForwardColumnEngine fce(tab);
    newtab1.bindAll (fce);
    Table forwTab(newtab1, 10);
}

void c (const TableDesc& tdin)
{
    Table tab("tForwardColRow_tmp.data1", Table::Update);
    // Now use the description to make a table, which will use the
    // forwarding engine again.
    TableDesc td = tdin;
    td.removeColumn ("ac");
    SetupNewTable newtab1("tForwardColRow_tmp.data2", td, Table::New);
    newtab1.setShapeColumn("arr1",IPosition(3,2,3,4));
    newtab1.setShapeColumn("arr3",IPosition(3,2,3,4));
    ForwardColumnIndexedRowEngine fce(tab, "row", "ForwardEngineRow1");
    newtab1.bindAll (fce);
    StManAipsIO sm;
    newtab1.bindColumn("row", sm);
    Table forwTab(newtab1, 20);
    cout << " canAddRow=" << forwTab.canAddRow();
    cout << " canRemoveRow=" << forwTab.canRemoveRow();
    cout << endl;
    ScalarColumn<uInt> rowCol (forwTab, "row");
    uInt i;
    for (i=0; i<20; i++) {
	rowCol.put (i, i%10);
    }
    forwTab.addColumn (ScalarColumnDesc<Int>("ac"), "ForwardEngineRow1", True);

    // Now use the description to make a table, which will use the
    // forwarding engine again.
    SetupNewTable newtab2("tForwardColRow_tmp.data3", tdin, Table::New);
    newtab2.setShapeColumn("arr1",IPosition(3,2,3,4));
    newtab2.setShapeColumn("arr3",IPosition(3,2,3,4));
    ForwardColumnIndexedRowEngine fce2(forwTab, "row");
    newtab2.bindAll (fce2);
    IncrementalStMan sm2;
    newtab2.bindColumn("row", sm2);
    Table forwTab2(newtab2);
    ScalarColumn<uInt> rowCol2 (forwTab2, "row");
    for (i=0; i<40; i++) {
	forwTab2.addRow();
	rowCol2.put (i, i%20);
    }
}

void check(const String& tableName, Int abOffset, Int acOffset)
{
    cout << "Checking table " << tableName << endl;
    // Read back the table and check the data.
    Table tab(tableName);
    uInt ntimes = tab.nrow() / 10;
    ScalarColumn<Int> ab2(tab,"ab");
    ScalarColumn<Int> ac (tab,"ac");
    ScalarColumn<uInt> ad(tab,"ad");
    ScalarColumn<float> ae(tab,"ae");
    ScalarColumn<String> af(tab,"af");
    ScalarColumn<DComplex> ag(tab,"ag");
    ArrayColumn<float> arr1(tab,"arr1");
    ArrayColumn<float> arr2(tab,"arr2");
    ArrayColumn<float> arr3(tab,"arr3");
    uInt i, j;
    Int abval, acval;
    uInt adval;
    float aeval;
    String afval;
    DComplex agval;
    char str[8];
    Cube<float> arrf(IPosition(3,2,3,4));
    Cube<float> arrval(IPosition(3,2,3,4));
    Cube<float> arrvalslice(arrval(Slice(0,1),Slice(0,1,2),Slice(0,2,2)));
    Slice tmp;
    Slicer nslice (tmp, tmp, tmp,  Slicer::endIsLength);
    Slicer nslice2(Slice(0,1), Slice(0,1,2), Slice(0,2,2),
		   Slicer::endIsLength);
    for (j=0; j<ntimes; j++) {
	indgen (arrf);
	uInt vali = 0;
	for (i=j*10; i<(j+1)*10; i++) {
	    cout << "get scalar row " << i;
	    cout << " defined=" << ab2.isDefined(i) << ac.isDefined(i)
		 << ad.isDefined(i) << ae.isDefined(i)
		 << af.isDefined(i) << ag.isDefined(i);
	    cout << endl;
	    ab2.get (i, abval);
	    ac.get (i, acval);
	    ad.get (i, adval);
	    ae.get (i, aeval);
	    af.get (i, afval);
	    ag.get (i, agval);
	    snprintf (str, sizeof(str), "V%i", vali);
	    if (abval != Int(vali)+abOffset  ||  acval != Int(vali)+1+acOffset
		||  adval != vali+2  ||  aeval != vali+3
		||  afval != str  ||  agval != DComplex(vali+2)) {
		cout << "error in row " << i << ": " << abval
		    << ", " << acval << ", " << adval
			<< ", " << aeval << ", " << afval
			    << ", " << agval << endl;
	    }
	    cout << "get array row " << i << endl;
	    cout << " defined=" << arr1.isDefined(i) << arr2.isDefined(i)
		 << arr3.isDefined(i);
	    cout << " ndim=" << arr1.ndim(i) << ","
		 << arr2.ndim(i) << "," << arr3.ndim(i);
	    cout << endl;
	    arr1.get (i, arrval);
	    if (!allEQ (arrval, arrf)) {
		cout << "error in arr1 in row " << i << endl;
	    }
	    arr2.get (i, arrval);
	    if (!allEQ (arrval, arrf)) {
		cout << "error in arr2 in row " << i << endl;
	    }
	    arr3.get (i, arrval);
	    if (!allEQ (arrval, arrf)) {
		cout << "error in arr3 in row " << i << endl;
	    }
	    arr2.getSlice (i, nslice, arrval);
	    if (!allEQ (arrval, arrf)) {
		cout << "error in arr2 (entire slice) in row " << i << endl;
	    }
	    arr2.getSlice (i, nslice2, arrvalslice);
	    if (!allEQ (arrval, arrf)) {
		cout << "error in arr2 (partial slice) in row " << i << endl;
	    }
	    arrf += (float)(arrf.nelements());
	    vali++;
	}
    }
}