File: tTableVector.cc

package info (click to toggle)
casacore 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,680 kB
  • sloc: cpp: 462,815; fortran: 16,372; ansic: 7,403; yacc: 4,626; lex: 2,340; sh: 1,786; python: 629; perl: 531; sed: 499; csh: 34; makefile: 31
file content (232 lines) | stat: -rw-r--r-- 6,809 bytes parent folder | download | duplicates (4)
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
//# tTableVector.cc: This program tests the table vectors
//# Copyright (C) 1994,1995,1996,1999,2000,2001,2003
//# 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: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//# $Id$

#include <casacore/tables/Tables/ScaColDesc.h>
#include <casacore/tables/Tables/TableDesc.h>
#include <casacore/tables/Tables/SetupNewTab.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/ScalarColumn.h>
#include <casacore/tables/Tables/TableVector.h>
#include <casacore/tables/Tables/TabVecMath.h>
#include <casacore/casa/Arrays/ArrayMath.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/OS/Timer.h>

#include <casacore/casa/iostream.h>
#include <casacore/casa/sstream.h>


#include <casacore/casa/namespace.h>
// This program tests the table vectors..
// It creates a description and a table.
// It creates various vectors, operates on them and checks the results.
// It writes some timing data to stdout.

void credes();
void cretab(uInt);
void dovec (Int);

int main (int argc, const char* argv[])
{
    uInt nr = 5000;
    if (argc > 1) {
	istringstream istr(argv[1]);
	istr >> nr;
    }
    credes();          // make description
    cretab(nr);        // create table (and write it)
    dovec (nr);        // do table vector operations
    return 0;          // successfully executed
}

// Create the description of the table and its subtable.
// Define some defaults (as scalar and array) to test if they
// are filled in correctly when a row is added to a table.
void credes () {
    TableDesc txp ("tTableVector_tmp", TableDesc::New);
    txp.addColumn (ScalarColumnDesc<Int> ("col1"));
    txp.addColumn (ScalarColumnDesc<double> ("col2"));
    txp.addColumn (ScalarColumnDesc<float> ("col3"));
    txp.addColumn (ScalarColumnDesc<Complex> ("col4"));
}

// Write data into the table.
void cretab (uInt nr)
{
    uInt i;
    SetupNewTable newtab ("tTableVector_tmp.data", "tTableVector_tmp",
                          Table::New);
    Table tab (newtab);
    ScalarColumn<Int>     col1 (tab, "col1");
    ScalarColumn<double>  col2 (tab, "col2");
    ScalarColumn<float>   col3 (tab, "col3");
    ScalarColumn<Complex> col4 (tab, "col4");
    Timer tim;
    for (i=0; i<nr; i++) {
	tab.addRow();
	col1.put (i, 100000-i);
	col2.put (i, i+1);
	col3.put (i, i+2);
	col4.put (i, i+3);
    }
    cout << ">>>" << endl;
    tim.show();
    cout << "<<<" << endl;
    cout << "Filling done" << endl;
}


void dovec (Int nr) {
    Int i;
    Vector<Int> vec(nr);
    Timer tim;
    for (i=0; i<nr; i++) {
	vec(i) = i;
    }
    cout << ">>>" << endl;
    tim.show ("Initializing Vector     ");

    TableVector<Int> tvec(nr);
    tim.mark();
    for (i=0; i<nr; i++) {
	tvec.set (i, i);
    }
    tim.show ("Initializing TableVector");

    tim.mark();
    TableVector<Int> tvec2 = tvec + 1;
    tim.show ("Adding constant to TVec ");
    cout << "<<<" << endl;
    cout << tvec2.nelements() << " elements" << endl;
    for (i=0; i<nr; i++) {
	Int j = tvec2(i);
	if (j != i+1) {
	    cout << "error in addition of element " << i << endl;
	    cout << tvec(i) << " " << tvec2(i) << endl;
	}
    }

    tim.mark();
    TableVector<Int> tvec3 = tvec + tvec2;
    cout << ">>>" << endl;
    tim.show ("Adding TVec to TVec     ");
    tim.mark();
    tvec3 = tvec3 + tvec2;
    tvec3 += tvec;
    tim.show ("Adding TVec+TVec+TVec   ");
    cout << "<<<" << endl;
    for (i=0; i<nr; i++) {
	if (tvec3(i) != 4*i+2) {
	    cout << "error in addition of element " << i << endl;
	}
    }

    Table tab ("tTableVector_tmp.data",Table::Update);
    TableVector<Int> tvec4(tab,"col1");
    TableVector<float> tvecFloat(tab,"col3");
//#    tvecFloat = 3;                            // Should fail when compiling
    cout << "got tvec4" << endl;
    cout << tvec4(1) << " (must be 99999)" << endl;

    tim.mark();
    tvec3 = tvec3 + tvec4;
    cout << ">>>" << endl;
    tim.show ("Adding ColVec to TVec   ");
    cout << "<<<" << endl;
    for (i=0; i<nr; i++) {
	if (tvec3(i) != 4*i+2+100000-i) {
	    cout << "error in addition of element " << i << endl;
	}
    }

    tim.mark();
    tvec4 = tvec + tvec4;
    cout << ">>>" << endl;
    tim.show ("Adding TVec to ColVec   ");
    tvec4 = tvec4 + tvec2;
    Vector<Int> vec2;
    ScalarColumn<Int> col1 (tab, "col1");
    tim.mark();
    col1.getColumn (vec2);
    tim.show ("Getting a table column  ");
    cout << "<<<" << endl;

    tim.mark();
    for (i=0; i<nr; i++) {
	if (tvec4(i) != 100000-i+i+i+1) {
	    cout << "error in addition of element " << i << endl;
	}
    }
    cout << ">>>" << endl;
    tim.show ("Loop through TableVector");
    cout << "<<<" << endl;

    tim.mark();
    for (i=0; i<nr; i++) {
	if (vec2(i) != 100000-i+i+i+1) {
	    cout << "error in vector element " << i << endl;
	}
    }
    cout << ">>>" << endl;
    tim.show ("Loop through Vector     ");
    cout << "<<<" << endl;

    // Get a vector from the table column vector.
    Vector<Int> vec3;
    vec3 = tvec4.makeVector();
    for (i=0; i<nr; i++) {
	if (vec3(i) != 100000-i+i+i+1) {
	    cout << "error in tvec3.makeVector element " << i << endl;
	}
    }

    // Do the same for tvec.
    // Assign value to be sure that copy semantics are used.
    vec3 = tvec.makeVector();
    for (i=0; i<nr; i++) {
	if (vec3(i) != i) {
	    cout << "error in tvec.makeVector element " << i << endl;
	}
    }
    tvec *= 2;
    for (i=0; i<nr; i++) {
	if (tvec(i) != 2*i) {
	    cout << "error in tvec*=2 element " << i << endl;
	}
    }
    for (i=0; i<nr; i++) {
	if (vec3(i) != i) {
	    cout << "assign to tvec affected vec3 " << i << endl;
	}
    }
    vec3 += 1;
    for (i=0; i<nr; i++) {
	if (tvec(i) != 2*i) {
	    cout << "assign to vec3 affected tvec " << i << endl;
	}
    }
}