Line data Source code
1 : //# CalTable.cc: Implementation of CalTable.h
2 : //# Copyright (C) 1996,1997,1998,1999,2000,2001,2003
3 : //# Associated Universities, Inc. Washington DC, USA.
4 : //#
5 : //# This library is free software; you can redistribute it and/or modify it
6 : //# under the terms of the GNU Library General Public License as published by
7 : //# the Free Software Foundation; either version 2 of the License, or (at your
8 : //# option) any later version.
9 : //#
10 : //# This library is distributed in the hope that it will be useful, but WITHOUT
11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 : //# License for more details.
14 : //#
15 : //# You should have received a copy of the GNU Library General Public License
16 : //# along with this library; if not, write to the Free Software Foundation,
17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 : //#
19 : //# Correspondence concerning AIPS++ should be addressed as follows:
20 : //# Internet email: aips2-request@nrao.edu.
21 : //# Postal address: AIPS++ Project Office
22 : //# National Radio Astronomy Observatory
23 : //# 520 Edgemont Road
24 : //# Charlottesville, VA 22903-2475 USA
25 : //#
26 : //# $Id$
27 : //----------------------------------------------------------------------------
28 :
29 : #include <synthesis/CalTables/CalTable.h>
30 : #include <casacore/tables/Tables/SetupNewTab.h>
31 : #include <casacore/tables/Tables/TableRow.h>
32 : #include <casacore/tables/TaQL/TableParse.h>
33 : #include <casacore/tables/Tables/ScalarColumn.h>
34 : #include <casacore/casa/Arrays.h>
35 : #include <casacore/casa/Arrays/ArrayMath.h>
36 : #include <msvis/MSVis/MSCalEnums.h>
37 :
38 : using namespace casacore;
39 : namespace casa { //# NAMESPACE CASA - BEGIN
40 :
41 : //----------------------------------------------------------------------------
42 :
43 1 : CalTable::CalTable() : itsMainTable(NULL), itsDescTable(NULL),
44 1 : itsHistoryTable(NULL), itsObservationTable(NULL)
45 : {
46 : // Default null constructor for calibration table; do nothing for now
47 : // Output to private data:
48 : // itsMainTable Table* Ptr to cal_main Table object
49 : // itsDescTable Table* Ptr to cal_desc Table object
50 : // itsHistoryTable Table* Ptr to cal_history Table object
51 : // itsObservationTable Table* Ptr to observation Table object
52 : //
53 1 : };
54 :
55 : //----------------------------------------------------------------------------
56 :
57 2 : CalTable::~CalTable()
58 : {
59 : // Default desctructor
60 : // Output to private data:
61 : // itsMainTable Table* Ptr to cal_main Table object
62 : // itsDescTable Table* Ptr to cal_desc Table object
63 : // itsHistoryTable Table* Ptr to cal_history Table object
64 : // itsObservationTable Table* Ptr to observation Table object
65 : //
66 1 : if (itsMainTable) {
67 1 : delete (itsMainTable);
68 : };
69 1 : if (itsDescTable) {
70 1 : delete (itsDescTable);
71 : };
72 1 : if (itsHistoryTable) {
73 1 : delete (itsHistoryTable);
74 : };
75 1 : if (itsObservationTable) {
76 0 : delete (itsObservationTable);
77 : };
78 1 : };
79 :
80 : //----------------------------------------------------------------------------
81 :
82 0 : CalTable::CalTable (const String& tableName, CalTableDesc& ctableDesc,
83 0 : Table::TableOption access) :
84 : itsMainTable(NULL), itsDescTable(NULL), itsHistoryTable(NULL),
85 0 : itsObservationTable(NULL)
86 : {
87 : // Construct from a cal table name, descriptor and access option.
88 : // Used for creating new tables.
89 : // Input:
90 : // tableName const String& Cal table name
91 : // ctableDesc const CalTableDesc& Cal table descriptor
92 : // access Table::TableOption Access mode
93 : // Output to private data:
94 : // itsMainTable Table* Ptr to cal_main Table object
95 : // itsDescTable Table* Ptr to cal_desc Table object
96 : // itsHistoryTable Table* Ptr to cal_history Table object
97 : // itsObservationTable Table* Ptr to observation Table object
98 : //
99 0 : createCalTable (tableName, ctableDesc, access);
100 0 : };
101 :
102 : //----------------------------------------------------------------------------
103 :
104 0 : CalTable::CalTable (const String& tableName, Table::TableOption access) :
105 : itsMainTable(NULL), itsDescTable(NULL), itsHistoryTable(NULL),
106 0 : itsObservationTable(NULL)
107 : {
108 : // Construct from a cal table name and access option. Used for
109 : // accessing existing tables.
110 : // Input:
111 : // tableName const String& Cal table name
112 : // access Table::TableOption Access option
113 : // Output to private data:
114 : // itsMainTable Table* Ptr to cal_main Table object
115 : // itsDescTable Table* Ptr to cal_desc Table object
116 : // itsHistoryTable Table* Ptr to cal_history Table object
117 : // itsObservationTable Table* Ptr to observation Table object
118 : //
119 0 : openCalTable (tableName, access);
120 0 : };
121 :
122 : //----------------------------------------------------------------------------
123 :
124 0 : CalTable::CalTable (const Table& table)
125 : {
126 : // Construct from an existing table object
127 : // Input:
128 : // table const Table& Input table
129 : // Output to private data:
130 : // itsMainTable Table* Ptr to cal_main Table object
131 : // itsDescTable Table* Ptr to cal_desc Table object
132 : // itsHistoryTable Table* Ptr to cal_history Table object
133 : // itsObservationTable Table* Ptr to observation Table object
134 : //
135 0 : itsMainTable = new Table (table);
136 0 : itsDescTable = new Table
137 0 : (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_DESC)));
138 0 : itsHistoryTable = new Table
139 0 : (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_HISTORY)));
140 0 : if (hasObsTable())
141 0 : itsObservationTable = new Table
142 0 : (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::OBSERVATION)));
143 : else
144 0 : itsObservationTable = NULL;
145 0 : };
146 :
147 : //----------------------------------------------------------------------------
148 :
149 0 : CalTable::CalTable (const CalTable& other)
150 : {
151 : // Copy constructor
152 : // Input:
153 : // other const CalTable& Existing CalTable object
154 : // Output to private data:
155 : // itsMainTable Table* Ptr to cal_main Table object
156 : // itsDescTable Table* Ptr to cal_desc Table object
157 : // itsHistoryTable Table* Ptr to cal_history Table object
158 : // itsObservationTable Table* Ptr to observation Table object
159 : //
160 0 : itsMainTable = new Table (*(other.itsMainTable));
161 0 : itsDescTable = new Table
162 0 : (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_DESC)));
163 0 : itsHistoryTable = new Table
164 0 : (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_HISTORY)));
165 0 : if (hasObsTable())
166 0 : itsObservationTable = new Table
167 0 : (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::OBSERVATION)));
168 : else
169 0 : itsObservationTable = NULL;
170 0 : };
171 :
172 : //----------------------------------------------------------------------------
173 :
174 0 : CalTable& CalTable::operator= (const CalTable& other)
175 : {
176 : // Assignment operator
177 : // Input:
178 : // other const CalTable& RHS CalTable object
179 : // Output to private data:
180 : // itsMainTable Table* Ptr to cal_main Table object
181 : // itsDescTable Table* Ptr to cal_desc Table object
182 : // itsHistoryTable Table* Ptr to cal_history Table object
183 : // itsObservationTable Table* Ptr to observation Table object
184 : //
185 0 : Bool identity = (this == &other);
186 0 : if (itsMainTable && !identity) {
187 0 : *itsMainTable = *(other.itsMainTable);
188 : };
189 0 : if (itsDescTable && !identity) {
190 0 : *itsDescTable = *(other.itsDescTable);
191 : };
192 0 : if (itsHistoryTable && !identity) {
193 0 : *itsHistoryTable = *(other.itsHistoryTable);
194 : };
195 0 : if (itsObservationTable && !identity && other.hasObsTable()) {
196 0 : *itsObservationTable = *(other.itsObservationTable);
197 : };
198 0 : return *this;
199 : };
200 :
201 : //----------------------------------------------------------------------------
202 :
203 0 : CalTable CalTable::sort (const Block <String>& columnNames, Sort::Order order,
204 : Sort::Option option)
205 : {
206 : // Sort the calibration table on multiple columns
207 : // Input:
208 : // columnNames const Block <String>& Sort columns
209 : // order Sort::Order Sort order
210 : // option Sort::Option Sort option
211 : // Output:
212 : // sort CalTable Sorted reference cal table
213 : // Input from private data:
214 : // itsMainTable Table* Underlying table object
215 : //
216 : // Sort the cal_main table directly
217 0 : Table result = itsMainTable->sort (columnNames, order, option);
218 0 : return CalTable (result);
219 : };
220 :
221 : //----------------------------------------------------------------------------
222 :
223 0 : void CalTable::sort2 (const Block <String>& columnNames, Sort::Order order,
224 : Sort::Option option)
225 : {
226 : // Sort the calibration table on multiple columns
227 : // Input:
228 : // columnNames const Block <String>& Sort columns
229 : // order Sort::Order Sort order
230 : // option Sort::Option Sort option
231 : // Output:
232 : // sort CalTable Sorted reference cal table
233 : // Input from private data:
234 : // itsMainTable Table* Underlying table object
235 : //
236 : // Sort the cal_main table directly (IN PLACE!)
237 0 : Table *sorted = new Table(itsMainTable->sort (columnNames, order, option));
238 0 : delete itsMainTable;
239 0 : itsMainTable=sorted;
240 :
241 0 : return;
242 : };
243 :
244 : //----------------------------------------------------------------------------
245 :
246 0 : CalTable CalTable::select (const String& calSelect)
247 : {
248 : // Apply selection to the calibration table
249 : // Input:
250 : // calSelect const String& TAQL selection string
251 : // Output:
252 : // select CalTable Selected reference cal table
253 : // Input from private data:
254 : // itsMainTable Table* Underlying table object
255 : //
256 : // Apply selection to the cal_main table directly
257 0 : Int len = calSelect.length();
258 0 : Int nspace = calSelect.freq (' ');
259 0 : if (calSelect.empty() || nspace==len) {
260 0 : return *this;
261 : } else {
262 0 : String parseString = "select from $1 where " + calSelect;
263 0 : Table result = tableCommand (parseString, *itsMainTable).table();
264 0 : return CalTable (result);
265 : };
266 : };
267 :
268 : //----------------------------------------------------------------------------
269 :
270 0 : void CalTable::select2 (const String& calSelect)
271 : {
272 : // Apply selection to the calibration table
273 : // Input:
274 : // calSelect const String& TAQL selection string
275 : // Output:
276 : // select CalTable Selected reference cal table
277 : // Input from private data:
278 : // itsMainTable Table* Underlying table object
279 : //
280 : // Apply selection to the cal_main table directly
281 0 : Int len = calSelect.length();
282 0 : Int nspace = calSelect.freq (' ');
283 0 : if (!calSelect.empty() && nspace!=len) {
284 0 : String parseString = "select from $1 where " + calSelect;
285 0 : Table *selected = new Table(tableCommand (parseString, *itsMainTable).table());
286 0 : delete itsMainTable;
287 0 : itsMainTable=selected;
288 : };
289 0 : return;
290 : };
291 :
292 : //----------------------------------------------------------------------------
293 :
294 5 : Int CalTable::nRowMain() const
295 : {
296 : // Return the number of rows in cal_main
297 : // Output:
298 : // nRowMain Int No of rows in cal_main
299 : //
300 5 : return itsMainTable->nrow();
301 : };
302 :
303 : //----------------------------------------------------------------------------
304 :
305 0 : Int CalTable::nRowDesc() const
306 : {
307 : // Return the number of rows in cal_desc
308 : // Output:
309 : // nRowDesc Int No of rows in cal_desc
310 : //
311 0 : return itsDescTable->nrow();
312 : };
313 :
314 : //----------------------------------------------------------------------------
315 :
316 0 : Int CalTable::nRowHistory() const
317 : {
318 : // Return the number of rows in cal_history
319 : // Output:
320 : // nRowHistory Int No of rows in cal_history
321 : //
322 0 : return itsHistoryTable->nrow();
323 : };
324 :
325 : //----------------------------------------------------------------------------
326 :
327 0 : Int CalTable::nRowObservation() const
328 : {
329 : // Return the number of rows in observation
330 : // Output:
331 : // nRowObservation Int No of rows in observation
332 : //
333 0 : if (hasObsTable())
334 0 : return itsObservationTable->nrow();
335 : else
336 0 : return -1;
337 : };
338 :
339 : //----------------------------------------------------------------------------
340 0 : Record CalTable::getRowMain (const Int& jrow)
341 : {
342 : // Get a row from cal_main
343 : // Input:
344 : // jrow const Int& Row number
345 : // Output:
346 : // getRowMain Record Row record
347 : //
348 0 : ROTableRow trow (*itsMainTable);
349 0 : trow.get (jrow);
350 0 : return trow.record();
351 : };
352 :
353 : //----------------------------------------------------------------------------
354 :
355 0 : Record CalTable::getRowDesc (const Int& jrow)
356 : {
357 : // Get a row from cal_desc
358 : // Input:
359 : // jrow const Int& Row number
360 : // Output:
361 : // getRowDesc Record Row record
362 : //
363 0 : ROTableRow trow (*itsDescTable);
364 0 : trow.get (jrow);
365 0 : return trow.record();
366 : };
367 :
368 : //----------------------------------------------------------------------------
369 :
370 0 : Record CalTable::getRowHistory (const Int& jrow)
371 : {
372 : // Get a row from cal_history
373 : // Input:
374 : // jrow const Int& Row number
375 : // Output:
376 : // getRowHistory Record Row record
377 : //
378 0 : ROTableRow trow (*itsHistoryTable);
379 0 : trow.get (jrow);
380 0 : return trow.record();
381 : };
382 :
383 : //----------------------------------------------------------------------------
384 :
385 0 : Record CalTable::getRowObservation (const Int& jrow)
386 : {
387 : // Get a row from observation
388 : // Input:
389 : // jrow const Int& Row number
390 : // Output:
391 : // getRowObservation Record Row record
392 : //
393 0 : if (hasObsTable()) {
394 0 : ROTableRow trow (*itsObservationTable);
395 0 : trow.get (jrow);
396 0 : return trow.record();
397 : } else {
398 0 : return Record();
399 : }
400 : };
401 :
402 : //----------------------------------------------------------------------------
403 0 : void CalTable::putRowMain (const Int& jrow, CalMainRecord& tableRec)
404 : {
405 : // Put a row to cal_main
406 : // Input:
407 : // jrow const Int& Row number
408 : // tableRec CalMainRecord& Table record
409 : //
410 : // Add rows as required
411 0 : Int nMaxRow = itsMainTable->nrow();
412 0 : Int nAdd = jrow - nMaxRow + 1;
413 0 : if (nAdd > 0) {
414 0 : itsMainTable->addRow (nAdd);
415 : };
416 :
417 : // Write the record
418 0 : TableRow trow (*itsMainTable);
419 0 : TableRecord trec = tableRec.record();
420 0 : trow.putMatchingFields (jrow, trec);
421 0 : };
422 :
423 : //----------------------------------------------------------------------------
424 :
425 1 : void CalTable::putRowDesc (const Int& jrow, CalDescRecord& tableRec)
426 : {
427 : // Put a row to cal_desc
428 : // Input:
429 : // jrow const Int& Row number
430 : // tableRec CalDescRecord& Table record
431 : //
432 : // Add rows as required
433 1 : Int nMaxRow = itsDescTable->nrow();
434 1 : Int nAdd = jrow - nMaxRow + 1;
435 1 : if (nAdd > 0) {
436 1 : itsDescTable->addRow (nAdd);
437 : };
438 :
439 : // Write the record
440 2 : TableRow trow (*itsDescTable);
441 2 : TableRecord trec = tableRec.record();
442 1 : trow.putMatchingFields (jrow, trec);
443 1 : };
444 :
445 : //----------------------------------------------------------------------------
446 :
447 0 : void CalTable::putRowHistory (const Int& jrow, CalHistoryRecord& tableRec)
448 : {
449 : // Put a row to cal_history
450 : // Input:
451 : // jrow const Int& Row number
452 : // tableRec CalHistoryRecord& Table record
453 : //
454 : // Add rows as required
455 0 : Int nMaxRow = itsHistoryTable->nrow();
456 0 : Int nAdd = jrow - nMaxRow + 1;
457 0 : if (nAdd > 0) {
458 0 : itsHistoryTable->addRow (nAdd);
459 : };
460 0 : TableRow trow (*itsHistoryTable);
461 0 : TableRecord trec = tableRec.record();
462 0 : trow.putMatchingFields (jrow, trec);
463 0 : };
464 :
465 : //----------------------------------------------------------------------------
466 :
467 0 : Int CalTable::maxAntenna()
468 : {
469 : // Return the maximum antenna no. found in the calibration table
470 : // Output:
471 : // maxAntenna Int Max. antenna no. found
472 : //
473 0 : ScalarColumn <Int> antCol (*itsMainTable, MSC::fieldName (MSC::ANTENNA1));
474 0 : Vector <Int> antVal;
475 0 : antCol.getColumn (antVal);
476 : uInt i;
477 0 : Int maxAnt = 0;
478 0 : for (i = 0; i < antVal.nelements(); i++) {
479 0 : maxAnt = max (antVal(i), maxAnt);
480 : };
481 :
482 0 : return maxAnt;
483 : };
484 :
485 : //----------------------------------------------------------------------------
486 :
487 0 : Int CalTable::numberTimeSlots (const Double& fracError)
488 : {
489 : // Return the no. of unique time slots found in the calibration table
490 : // Input:
491 : // fracError const Double& Fractional error allowed in comparison
492 : // Output:
493 : // numberTimeSlots Int No. of unique time stamps found
494 : //
495 0 : ScalarColumn <Double> timeCol (*itsMainTable, MSC::fieldName (MSC::TIME));
496 0 : ScalarColumn <Double> intervalCol (*itsMainTable,
497 0 : MSC::fieldName (MSC::INTERVAL));
498 0 : Vector <Double> timeVal;
499 0 : Vector <Double> intervalVal;
500 0 : timeCol.getColumn (timeVal);
501 0 : intervalCol.getColumn (intervalVal);
502 :
503 : // Index of matches
504 0 : Vector <Int> match;
505 0 : Int n = timeVal.nelements();
506 0 : match.resize (n);
507 0 : match = 0;
508 : Int i, j;
509 : Bool found;
510 :
511 : // Loop through all time stamps
512 0 : for (i = 0; i < (n-1); i++) {
513 0 : found = false;
514 0 : for (j = i+1; j < n; j++) {
515 0 : if (abs (timeVal(i) - timeVal(j)) < fracError * intervalVal(i) &&
516 0 : abs (intervalVal(i) - intervalVal(j)) < fracError * intervalVal(i)) {
517 0 : found = true;
518 : };
519 : };
520 0 : if (!found) {
521 0 : match(i) = 1;
522 : };
523 : };
524 0 : if (n > 0) {
525 0 : match(n-1) = 1;
526 : };
527 :
528 : // Sum the number of matches
529 0 : Int sum = 0;
530 0 : for (i = 0; i < n; i++) {
531 0 : sum = sum + match(i);
532 : };
533 :
534 0 : return sum;
535 : };
536 :
537 : //----------------------------------------------------------------------------
538 :
539 : // Return the number of rows per CalDescId
540 0 : void CalTable::rowsPerCalDescId(Vector<Int>& rowspercdi) {
541 :
542 0 : Int nCDI=nRowDesc();
543 :
544 0 : rowspercdi.resize(nCDI);
545 :
546 0 : ScalarColumn<Int> cdidcol(*itsMainTable,"CAL_DESC_ID");
547 0 : Vector<Int> cdi;
548 0 : cdidcol.getColumn(cdi);
549 :
550 0 : for (Int icdi=0;icdi<nCDI;++icdi)
551 0 : rowspercdi(icdi)=ntrue(cdi==icdi);
552 :
553 0 : }
554 :
555 :
556 : //----------------------------------------------------------------------------
557 :
558 1 : void CalTable::createCalTable (const String& tableName,
559 : CalTableDesc& ctableDesc,
560 : Table::TableOption access)
561 : {
562 : // Create a new cal table
563 : // Input:
564 : // tableName const String& Cal table name
565 : // ctableDesc const CalTableDesc& Cal table descriptor
566 : // access Table::TableOption Access mode
567 : // Output to private data:
568 : // itsMainTable Table* Ptr to cal_main Table object
569 : // itsDescTable Table* Ptr to cal_desc Table object
570 : // itsHistoryTable Table* Ptr to cal_history Table object
571 : //
572 : // Construct the table descriptor, including the sub-table structure
573 : // for cal_desc and cal_history. Add subtables to CAL_DESC and
574 : // CAL_HISTORY keywords.
575 2 : SetupNewTable calMainSetup (tableName, ctableDesc.calMainDesc(), access);
576 1 : itsMainTable = new Table (calMainSetup);
577 2 : SetupNewTable calDescSetup (tableName + "/CAL_DESC",
578 3 : ctableDesc.calDescDesc(), access);
579 1 : itsDescTable = new Table (calDescSetup);
580 2 : itsMainTable->rwKeywordSet().defineTable (MSC::fieldName (MSC::CAL_DESC),
581 1 : *itsDescTable);
582 2 : SetupNewTable calHistorySetup (tableName + "/CAL_HISTORY",
583 3 : ctableDesc.calHistoryDesc(), access);
584 1 : itsHistoryTable = new Table (calHistorySetup);
585 2 : itsMainTable->rwKeywordSet().defineTable (MSC::fieldName (MSC::CAL_HISTORY),
586 1 : *itsHistoryTable);
587 : // Set table type and sub-type
588 2 : String reqdType = TableInfo::type (TableInfo::ME_CALIBRATION);
589 1 : itsMainTable->tableInfo().setType (reqdType);
590 1 : itsMainTable->tableInfo().setSubType (itsMainTable->tableDesc().getType());
591 1 : };
592 :
593 : //----------------------------------------------------------------------------
594 :
595 0 : void CalTable::openCalTable (const String& tableName,
596 : Table::TableOption access)
597 : {
598 : // Open an existing cal table
599 : // Input:
600 : // tableName const String& Cal table name
601 : // access Table::TableOption Access option
602 : // Output to private data:
603 : // itsMainTable Table* Ptr to cal_main Table object
604 : // itsDescTable Table* Ptr to cal_desc Table object
605 : // itsHistoryTable Table* Ptr to cal_history Table object
606 : // itsObservationTable Table* Ptr to cal_history Table object
607 : //
608 0 : itsMainTable = new Table (tableName, access);
609 0 : itsDescTable = new Table (itsMainTable->keywordSet().asTable
610 0 : (MSC::fieldName(MSC::CAL_DESC)));
611 0 : itsHistoryTable = new Table (itsMainTable->keywordSet().asTable
612 0 : (MSC::fieldName(MSC::CAL_HISTORY)));
613 0 : if (hasObsTable())
614 0 : itsObservationTable = new Table (itsMainTable->keywordSet().asTable
615 0 : (MSC::fieldName(MSC::OBSERVATION)));
616 : else
617 0 : itsObservationTable = NULL;
618 0 : };
619 :
620 : //----------------------------------------------------------------------------
621 :
622 : } //# NAMESPACE CASA - END
623 :
|