File: TableMeasRefDesc.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 (334 lines) | stat: -rw-r--r-- 10,064 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
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
//# TableMeasRefDef.cc: Definition of a MeasRef in a Table.
//# Copyright (C) 1997,1999,2000,2001
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library 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 Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; 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


//# Includes
#include <casacore/measures/TableMeasures/TableMeasRefDesc.h>
#include <casacore/measures/TableMeasures/TableMeasDescBase.h>
#include <casacore/measures/Measures/MeasureHolder.h>
#include <casacore/measures/Measures/Measure.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/TableDesc.h>
#include <casacore/tables/Tables/TableRecord.h>
#include <casacore/casa/Logging/LogIO.h>
#include <casacore/casa/Arrays/ArrayMath.h>
#include <casacore/casa/Arrays/ArrayLogical.h>
#include <casacore/casa/Utilities/LinearSearch.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/Exceptions/Error.h>


namespace casacore { //# NAMESPACE CASACORE - BEGIN

// Set the default type getting function.
TableMeasRefDesc::TypesFunc* TableMeasRefDesc::theirTypesFunc =
                                        TableMeasRefDesc::defaultTypesFunc;


TableMeasRefDesc::TableMeasRefDesc (uInt referenceCode)
: itsRefCode       (referenceCode),
  itsRefCodeColInt (False),
  itsHasRefTab     (True),
  itsOffset        (0)
{}

TableMeasRefDesc::TableMeasRefDesc (uInt referenceCode, 
				    const TableMeasOffsetDesc& offset)
: itsRefCode       (referenceCode),
  itsRefCodeColInt (False),
  itsHasRefTab     (True),
  itsOffset        (new TableMeasOffsetDesc(offset))
{}

TableMeasRefDesc::TableMeasRefDesc (const TableDesc &td, const String& column)
: itsRefCode       (0),
  itsColumn        (column),
  itsRefCodeColInt (False),
  itsHasRefTab     (True),
  itsOffset        (0)
{
  checkColumn (td);
}

TableMeasRefDesc::TableMeasRefDesc (const TableDesc &td, const String& column,
				    const TableMeasOffsetDesc& offset)
: itsRefCode       (0),
  itsColumn        (column),
  itsRefCodeColInt (False),
  itsHasRefTab     (True),
  itsOffset        (new TableMeasOffsetDesc(offset))
{
  checkColumn (td);
}

TableMeasRefDesc::TableMeasRefDesc (const TableMeasRefDesc& that)
: itsOffset(0)
{
  operator= (that);
}
    
TableMeasRefDesc& TableMeasRefDesc::operator= (const TableMeasRefDesc& that)
{
  if (this != &that) {
    delete itsOffset;
    itsRefCode       = that.itsRefCode;
    itsColumn        = that.itsColumn;
    itsRefCodeColInt = that.itsRefCodeColInt;
    itsHasRefTab     = that.itsHasRefTab;
    itsOffset        = that.itsOffset;
    if (itsOffset != 0) {
      itsOffset = new TableMeasOffsetDesc(*itsOffset);
    }
    itsTabRefTypes = that.itsTabRefTypes;
    itsTabRefCodes = that.itsTabRefCodes;
    itsTab2Cur     = that.itsTab2Cur;
    itsCur2Tab     = that.itsCur2Tab;
  }
  return *this;
}

TableMeasRefDesc::~TableMeasRefDesc()
{
  delete itsOffset;
}

TableMeasRefDesc::TableMeasRefDesc (const TableRecord& measInfo,
				    const Table& tab,
				    const MeasureHolder& measHolder,
				    const TableMeasDescBase& mDesc)
: itsRefCode       (0),
  itsRefCodeColInt (False),
  itsHasRefTab     (True),
  itsOffset        (0)
{
  Int fnr;
  fnr = measInfo.fieldNumber("Ref");
  // Read back. The refcode is fixed or variable.
  if (fnr >= 0) {
    itsRefCode = mDesc.refCode (measInfo.asString(fnr));
  }
  fnr = measInfo.fieldNumber("VarRefCol");
  if (fnr >= 0) {
    // Variable refcode.
    itsColumn = measInfo.asString(fnr);
    // See if the refcodes/types are defined in the table.
    // If so, read back. Otherwise initialize with default.
    if (tab.tableDesc().columnDesc(itsColumn).dataType() == TpInt) {
      itsRefCodeColInt = True;
      fnr = measInfo.fieldNumber("TabRefTypes");
      if (fnr >= 0) {
	itsTabRefTypes = measInfo.asArrayString ("TabRefTypes");
	itsTabRefCodes = measInfo.toArrayuInt ("TabRefCodes");
	fillTabRefMap (measHolder);
      } else {
	itsHasRefTab = False;
	initTabRef (measHolder);
      }
    }
  }
  itsOffset = TableMeasOffsetDesc::reconstruct (measInfo, "RefOff", tab);
}

void TableMeasRefDesc::defaultTypesFunc (Vector<String>& curTypes,
					 Vector<uInt>& curCodes,
					 const MeasureHolder& measHolder)
{
  Int nall, nexact;
  const uInt* codes;
  const String* types = measHolder.asMeasure().allTypes (nall, nexact, codes);
  // Remove the duplicates which are at the end of the arrays.
  Bool found;
  while (nall > 0) {
    if (linearSearchBrackets (found, codes, codes[nall-1], nall-1) < 0) {
      break;
    }
    nall--;
  }
  IPosition shp(1, nall);
  curTypes = Vector<String> (shp, types);
  curCodes = Vector<uInt> (shp, codes);
}

void TableMeasRefDesc::initTabRef (const MeasureHolder& measHolder)
{
  itsTabRefTypes.resize (0);
  itsTabRefCodes.resize (0);
  theirTypesFunc (itsTabRefTypes, itsTabRefCodes, measHolder);
  initTabRefMap();
}

void TableMeasRefDesc::initTabRefMap()
{
  uInt maxcod = max(itsTabRefCodes);
  itsTab2Cur.resize (maxcod+1);
  itsTab2Cur = -1;
  for (uInt i=0; i<itsTabRefCodes.nelements(); ++i) {
    uInt tp = itsTabRefCodes[i];
    itsTab2Cur[tp] = tp;
  }
  itsCur2Tab = itsTab2Cur;
}

void TableMeasRefDesc::fillTabRefMap (const MeasureHolder& measHolder)
{
  Vector<String> curtyp;
  Vector<uInt>   curcod;
  theirTypesFunc (curtyp, curcod, measHolder);
  if (curtyp.nelements() == itsTabRefTypes.nelements()
  &&  allEQ (curtyp, itsTabRefTypes)
  &&  allEQ (curcod, itsTabRefCodes)) {
    initTabRefMap();
  } else {
    uInt maxtab = max(itsTabRefCodes);
    uInt maxcur = max(curcod);
    itsCur2Tab.resize (maxcur+1);
    // First map current codes to table codes; this may add table code entries.
    maxtab = fillMap (itsCur2Tab, curcod, curtyp,
		      itsTabRefCodes, itsTabRefTypes, maxtab);
    itsTab2Cur.resize (maxtab+1);
    fillMap (itsTab2Cur, itsTabRefCodes, itsTabRefTypes, curcod, curtyp, -1);
  }
}

uInt TableMeasRefDesc::fillMap (Block<Int>& f2t,
				const Vector<uInt>& codesf,
				const Vector<String>& typesf,
				Vector<uInt>& codest,
				Vector<String>& typest,
				Int maxnr)
{
  f2t = -1;
  uInt nt = typest.nelements();
  for (uInt i=0; i<typesf.size(); i++) {
    Int inx = linearSearch1 (typest, typesf[i]);
    if (inx >= 0) {
      f2t[codesf[i]] = codest[inx];
    } else {
      if (maxnr < 0) {
        LogIO os;
        os << LogIO::WARN
           << "TableMeasRefDesc warning: refcode " << typesf[i]
           << " does not exist in this Casacore version" << LogIO::POST;
        f2t[codesf[i]] = -1;
      } else {
        codest.resize (nt+1, True);
        typest.resize (nt+1, True);
        maxnr++;
        codest[nt] = maxnr;
        typest[nt] = typesf[i];
        f2t[codesf[i]] = codest[nt];
        nt++;
      }
    }
  }
  return maxnr;
}

uInt TableMeasRefDesc::tab2cur (uInt tabRefCode) const
{
  AlwaysAssert (tabRefCode < itsTab2Cur.nelements()
		&&  itsTab2Cur[tabRefCode] >= 0, AipsError);
  return itsTab2Cur[tabRefCode];
}

uInt TableMeasRefDesc::cur2tab (uInt curRefCode) const
{
  AlwaysAssert (curRefCode < itsCur2Tab.nelements()
		&&  itsCur2Tab[curRefCode] >= 0, AipsError);
  return itsCur2Tab[curRefCode];
}

void TableMeasRefDesc::write (TableDesc& td, TableRecord& measInfo, 
			      const TableMeasDescBase& measDesc)
{
  writeKeys (measInfo, measDesc);
  if (itsOffset != 0) {
    itsOffset->write (td, measInfo, "RefOff");
  }
}

void TableMeasRefDesc::write (Table& tab, TableRecord& measInfo, 
			      const TableMeasDescBase& measDesc)
{
  writeKeys (measInfo, measDesc);
  if (itsOffset != 0) {
    itsOffset->write (tab, measInfo, "RefOff");
  }
}

void TableMeasRefDesc::writeKeys (TableRecord& measInfo, 
				  const TableMeasDescBase& measDesc)
{
  if (isRefCodeVariable()) {
    measInfo.define ("VarRefCol", itsColumn);
    if (itsRefCodeColInt) {
      measInfo.define ("TabRefTypes", itsTabRefTypes);
      measInfo.define ("TabRefCodes", itsTabRefCodes);
    }
  } else {
    measInfo.define ("Ref", measDesc.refType (itsRefCode));
  } 
}

void TableMeasRefDesc::checkColumn (const TableDesc& td)
{
  if (! td.isColumn(itsColumn)) {
    throw (AipsError ("TableMeasRefDesc::checkColumn; No such column: "
		      + itsColumn));
  } else {
    if (td.columnDesc(itsColumn).dataType() != TpString) {
      if (td.columnDesc(itsColumn).dataType() != TpInt) {
	throw AipsError ("TableMeasRefDesc::checkColumn; Reference column's "
			 "type must be Int or String: " + itsColumn);
      }
      itsRefCodeColInt = True;
    }
  }
}

void TableMeasRefDesc::resetRefCode (uInt refCode)
{
  if (isRefCodeVariable()) {
    throw (AipsError ("tableMeasRefDesc::resetRefCode cannot be done;"
		      "the refcode is not fixed for the entire column"));
  }
  itsRefCode = refCode;
}

void TableMeasRefDesc::resetOffset (const Measure& offset)
{
  if (itsOffset == 0) {
    itsOffset = new TableMeasOffsetDesc (offset);
  } else {
    itsOffset->resetOffset (offset);
  }
    if (isOffsetVariable()) {
      throw (AipsError ("tableMeasRefDesc::resetOffset cannot be done;"
			"the offset is not fixed for the entire column"));
    }
}

} //# NAMESPACE CASACORE - END