File: TableMeasDescBase.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 (221 lines) | stat: -rw-r--r-- 7,130 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
//# TableMeasDefBase.cc: Definition of a Measure in a Table.
//# Copyright (C) 1997,1998,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/TableMeasDescBase.h>
#include <casacore/measures/TableMeasures/TableQuantumDesc.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/TableColumn.h>
#include <casacore/tables/Tables/TableDesc.h>
#include <casacore/tables/Tables/ColumnDesc.h>
#include <casacore/tables/Tables/TableRecord.h>
#include <casacore/measures/Measures/Measure.h>
#include <casacore/measures/Measures/MeasureHolder.h>
#include <casacore/casa/Quanta/Quantum.h>
#include <casacore/casa/Quanta/Unit.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/BasicSL/String.h>
#include <casacore/casa/Exceptions/Error.h>


namespace casacore { //# NAMESPACE CASACORE - BEGIN

TableMeasDescBase::TableMeasDescBase()
{}

TableMeasDescBase::TableMeasDescBase (const TableMeasValueDesc& value,
				      const TableMeasRefDesc& ref)
: itsValue (value),
  itsRef   (ref)
{}

TableMeasDescBase::TableMeasDescBase (const TableMeasDescBase& that)
: itsValue    (that.itsValue),
  itsRef      (that.itsRef),
  itsMeasType (that.itsMeasType),
  itsUnits    (that.itsUnits)
{}

TableMeasDescBase::~TableMeasDescBase()
{}

TableMeasDescBase* TableMeasDescBase::clone() const
{
  return new TableMeasDescBase(*this);
}

TableMeasDescBase* TableMeasDescBase::reconstruct (const Table& tab, 
						   const String& columnName)
{
  Int fnr;
  TableRecord mtype;
  TableRecord measInfo;
  const TableRecord& columnKeyset = tab.tableDesc()[columnName].keywordSet();
  
  // get the Measure type
  fnr = columnKeyset.fieldNumber("MEASINFO");
  if (fnr >= 0) {
    measInfo = columnKeyset.asRecord(fnr);
    // Older TableMeasures has a separate Type record for itsMeasType.
    // Newer ones simply have the type field in the MEASINFO.
    if (measInfo.isDefined("Type")) {
      mtype = measInfo.asRecord("Type");    	
    } else {
      mtype = measInfo;
    }
  } else {
    throw(AipsError("TableMeasDescBase::reconstruct; MEASINFO record not "
		    "found for column " + columnName));
  }
  
  // get the units
  TableQuantumDesc* tqdesc = TableQuantumDesc::reconstruct (tab.tableDesc(),
							    columnName);
  Vector<String> names(tqdesc->getUnits());
  Vector<Unit> units(names.nelements());
  for (uInt i=0; i<names.nelements(); i++) {
    units(i) = names(i);
  }
  delete tqdesc;
  
  String error;
  MeasureHolder measHolder;
  measHolder.fromType (error, mtype);
  
  TableMeasDescBase* p = new TableMeasDescBase();
  p->itsValue = TableMeasValueDesc (tab.tableDesc(), columnName);
  p->itsMeasType = TableMeasType(measHolder.asMeasure());
  p->itsUnits = units;
  p->itsRef = TableMeasRefDesc (measInfo, tab, measHolder, *p);
  return p;
}

TableMeasDescBase& TableMeasDescBase::operator= (const TableMeasDescBase& that)
{
  if (this != &that) {
    itsValue    = that.itsValue;
    itsRef      = that.itsRef;
    itsMeasType = that.itsMeasType;
    itsUnits    = that.itsUnits;
  }
  return *this;
}
    
void TableMeasDescBase::write (TableDesc& td)
{
  TableRecord measInfo;
  // Create a record from the MeasType and add it to measInfo
  itsMeasType.toRecord (measInfo);
  // Put the units.
  // Use TableQuantumDesc, so the column can be used that way too.
  TableQuantumDesc tqdesc(td, itsValue.columnName(), itsUnits);
  tqdesc.write (td);
		    
  // Write the reference.
  itsRef.write (td, measInfo, *this);
  // Write the MEASINFO record into the keywords of the value column.
  itsValue.write (td, measInfo);
}

void TableMeasDescBase::write (Table& tab)
{
  TableRecord measInfo;
  // Create a record from the MeasType and add it to measInfo
  itsMeasType.toRecord (measInfo);
  // Put the units.
  // Use TableQuantumDesc, so the column can be used that way too.
  TableQuantumDesc tqdesc(tab.tableDesc(), itsValue.columnName(), itsUnits);
  tqdesc.write (tab);
		    
  // Write the reference.
  itsRef.write (tab, measInfo, *this);
  // Write the MEASINFO record into the keywords of the value column.
  itsValue.write (tab, measInfo);
}

void TableMeasDescBase::writeIfOld (const Table& tab)
{
  if (! itsRef.hasRefTab()) {
    write (const_cast<Table&>(tab));
  }
}

void TableMeasDescBase::initTabRef (const MeasureHolder& measHolder)
{
  itsRef.initTabRef (measHolder);
}

void TableMeasDescBase::setMeasUnits (const Measure& meas,
				      const Vector<Quantum<Double> >& val,
				      const Vector<Unit>& units) 
{ 
  itsMeasType = TableMeasType(meas);
  // The input unit vector cannot be longer.
  if (units.nelements() > val.nelements()) {
    throw (AipsError ("TableMeasDescBase::setMeasUnits; Unit vector"
		      " for column " + columnName() + " is too long"));
  }
  // An empty or non-given unit gets the default Quantum one.
  itsUnits.resize (val.nelements());
  for (uInt i=0; i<val.nelements(); i++) {
    if (i >= units.nelements()  ||  units(i).empty()) {
      itsUnits(i) = val(i).getUnit();
    } else {
      if (! (units(i) == val(i).getUnit())) {
	throw (AipsError ("TableMeasDescBase::setMeasUnits; invalid unit "
			  + units(i).getName() + " for column "
			  + columnName()));
      }
      itsUnits(i) = units(i);
    }
  }
}

void TableMeasDescBase::resetUnits (const Vector<Unit>& units)
{
  if (units.nelements() > itsUnits.nelements()) {
    throw (AipsError ("TableMeasDescBase::resetUnits: Unit vector"
		      " for column " + columnName() + " is too long"));
  }
  // An empty or non-given unit does not change.
  for (uInt i=0; i<units.nelements(); i++) {
    if (! units(i).empty()) {
      if (! (units(i) == itsUnits(i))) {
	throw (AipsError ("TableMeasDescBase::resetUnits; invalid unit "
			  + units(i).getName() + " for column "
			  + columnName()));
      }
      itsUnits(i) = units(i);
    }
  }
}

Bool TableMeasDescBase::hasMeasures (const TableColumn& column)
{
  return column.keywordSet().isDefined ("MEASINFO");
}

} //# NAMESPACE CASACORE - END