File: tBitFlagsEngine.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 (278 lines) | stat: -rw-r--r-- 9,703 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
//# tBitFlagsEngine.cc: Test program for class BitFlagsEngine
//# Copyright (C) 2009
//# 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/BitFlagsEngine.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/Vector.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 BitFlagsEngine </summary>

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


// Build a description.
void createTable()
{
  // First register the virtual column engine.
  BitFlagsEngine<Short>::registerClass();
  BitFlagsEngine<Int>::registerClass();
  BitFlagsEngine<uChar>::registerClass();

  // Build the table description.
  TableDesc td("", "1", TableDesc::Scratch);
  td.addColumn (ArrayColumnDesc<Bool> ("virtualcol1"));
  td.addColumn (ArrayColumnDesc<Int> ("storedcol1"));
  td.addColumn (ArrayColumnDesc<Bool> ("virtualcol2"));
  td.addColumn (ArrayColumnDesc<Short> ("storedcol2"));
  td.addColumn (ArrayColumnDesc<Bool> ("virtualcol3", "",
                                       IPosition(3,2,3,4),
                                       ColumnDesc::Direct));
  td.addColumn (ArrayColumnDesc<uChar> ("storedcol3", "",
                                        IPosition(2,3,4),
                                        ColumnDesc::Direct));
  // Define keywords telling the bitmask.
  ColumnDesc& cdesc = td.rwColumnDesc("storedcol1");
  Record brec;
  brec.define ("bit01", 3);
  brec.define ("bit12", 6);
  brec.define ("bit23", 12);
  cdesc.rwKeywordSet().defineRecord ("FLAGSETS", brec);
  // Now create a new table from the description.
  SetupNewTable newtab("tBitFlagsEngine_tmp.data", td, Table::New);
  // Create the virtual column engines and bind the columns to them.
  // Use keywords to define the mask.
  Vector<String> writeMask(2);
  writeMask[0] = "bit01";
  writeMask[1] = "bit12";
  BitFlagsEngine<Int> engine1("virtualcol1", "storedcol1",
                              Vector<String>(1,"bit23"), writeMask);
  BitFlagsEngine<Short> engine2("virtualcol2", "storedcol2");
  BitFlagsEngine<uChar> engine3("virtualcol3", "storedcol3");
  newtab.bindColumn ("virtualcol1", engine1);
  newtab.bindColumn ("virtualcol2", engine2);
  newtab.bindColumn ("virtualcol3", engine3);
  Table tab(newtab, 10);
  // Fill the table via the flag columns.
  ArrayColumn<Int> storedcol1 (tab, "storedcol1");
  ArrayColumn<Short> storedcol2 (tab, "storedcol2");
  ArrayColumn<uChar> storedcol3 (tab, "storedcol3");
  Matrix<Int> arri(IPosition(2,3,4));
  Matrix<Short> arrs(IPosition(2,3,4));
  Matrix<uChar> arrc(IPosition(2,3,4));
  for (Int j=0; j<10; j++) {
    Int i=0;
    for (uInt i2=0; i2<4; i2++) {
      for (uInt i1=0; i1<3; i1++) {
        arri(i1,i2) = (j + i)%2;
        arrs(i1,i2) = (j + i)%5;
        arrc(i1,i2) = (j + i)%4;
        ++i;
      }
    }
    storedcol1.put (j, arri);
    storedcol2.put (j, arrs);
    storedcol3.put (j, arrc);
    arri += 840;
    arrs += Short(210);
    arrc += uChar(1);
  }
}

void readTable()
{
  // Read back the table.
  Table tab("tBitFlagsEngine_tmp.data");
  ArrayColumn<Int> storedcol1 (tab, "storedcol1");
  ArrayColumn<Short> storedcol2 (tab, "storedcol2");
  ArrayColumn<uChar> storedcol3 (tab, "storedcol3");
  ArrayColumn<Bool> virtualcol1 (tab, "virtualcol1");
  ArrayColumn<Bool> virtualcol2 (tab, "virtualcol2");
  ArrayColumn<Bool> virtualcol3 (tab, "virtualcol3");
  Matrix<Bool> arrd1(IPosition(2,3,4));
  Matrix<Bool> arrd2(IPosition(2,3,4));
  Matrix<Bool> arrd3(IPosition(2,3,4));
  Matrix<Bool> arrd3slice(arrd3(Slice(0,1,2),Slice(0,2,2)));
  Matrix<Int> arri(IPosition(2,3,4));
  Matrix<Short> arrs(IPosition(2,3,4));
  Matrix<uChar> arrc(IPosition(2,3,4));
  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 i=0;
      for (uInt i2=0; i2<4; i2++) {
        for (uInt i1=0; i1<3; i1++) {
          arri(i1,i2) = (j + i)%2;
          arrs(i1,i2) = (j + i)%5;
          arrc(i1,i2) = (j + i)%4;
          arrd1(i1,i2) = arri(i1,i2) & 12;
          arrd2(i1,i2) = arrs(i1,i2);
          arrd3(i1,i2) = arrc(i1,i2);
          ++i;
        }
      }
    }
    Array<Int> ai;
    Array<Short> as;
    Array<uChar> ac;
    Array<Bool> arrbool;
    Array<Bool> arrboolslice;
    cout << "get row " << j << endl;
    storedcol1.get (j, ai);
    if (!allEQ (ai, arri)) {
      cout << "error in storedcol1 in row " << j << endl;
      cout << ai << endl;
      cout << arri << endl;
    }
    virtualcol1.get (j, arrbool);
    if (!allEQ (arrbool, arrd1)) {
      cout << "error in virtualcol1 in row " << j << endl;
      cout << arrbool << endl;
      cout << arrd1 << endl;
    }
    storedcol2.get (j, as);
    if (!allEQ (as, arrs)) {
      cout << "error in storedcol2 in row " << j << endl;
      cout << as << endl;
      cout << arrs << endl;
    }
    virtualcol2.get (j, arrbool);
    if (!allEQ (arrbool, arrd2)) {
      cout << "error in virtualcol2 in row " << j << endl;
      cout << arrbool << endl;
      cout << arrd2 << endl;
    }
    storedcol3.get (j, ac);
    if (!allEQ (ac, arrc)) {
      cout << "error in storedcol3 in row " << j << endl;
      cout << ac << endl;
      cout << arrc << endl;
    }
    virtualcol3.get (j, arrbool);
    if (!allEQ (arrbool, arrd3)) {
      cout << "error in virtualcol3 in row " << j << endl;
      cout << arrbool << endl;
      cout << arrd3 << endl;
    }
    virtualcol3.getSlice (j, nslice, arrbool);
    if (!allEQ (arrbool, arrd3)) {
      cout << "error in virtualcol3 (entire slice) in row " << j << endl;
      cout << arrbool << endl;
      cout << arrd3 << endl;
    }
    virtualcol3.getSlice (j, nslice2, arrboolslice);
    if (!allEQ (arrboolslice, arrd3slice)) {
      cout << "error in virtualcol3 (partial slice) in row " << j << endl;
      cout << arrboolslice << endl;
      cout << arrd3slice << endl;
    }
  }

  // Now test getting the columns.
  {
    Cube<Bool> arrd2(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 + i)%5;
          arrd2(i1,i2,j) = val;
          ++i;
        }
      }
    }
    {
      Cube<Bool> arrvald = virtualcol2.getColumn();
      if (!allEQ (arrvald, arrd2)) {
        cout << "error in virtualcol2 getcolumn " << endl;
        cout << arrvald << endl;
        cout << arrd2 << endl;
      }
    }
    {
      Cube<Bool> arrvald = virtualcol2.getColumnRange(Slice(1,4,2));
      if (!allEQ (arrvald, arrd2(Slice(0,3,1),Slice(0,4,1),Slice(1,4,2)))) {
        cout << "error in virtualcol2 getcolumnrange " << endl;
        cout << arrvald << endl;
        cout << arrd2 << endl;
      }
    }
    {
      Cube<Bool> arrvald = virtualcol2.getColumn(nslice2);
      if (!allEQ (arrvald, arrd2(Slice(0,2,1),Slice(1,2,2),Slice(0,10,1)))) {
        cout << "error in virtualcol2 getcolumnslice " << endl;
        cout << arrvald << endl;
        cout << arrd2 << endl;
      }
    }
    {
      Cube<Bool> arrvald = virtualcol2.getColumnRange(Slice(1,4,2), nslice2);
      if (!allEQ (arrvald, arrd2(Slice(0,2,1),Slice(1,2,2),Slice(1,4,2)))) {
        cout << "error in virtualcol2 getcolumnrangeslice " << endl;
        cout << arrvald << endl;
        cout << arrd2 << endl;
      }
    }
  }
}


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