LCOV - code coverage report
Current view: top level - synthesis/CalTables - CalTable2.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 181 0.0 %
Date: 2024-10-10 15:00:01 Functions: 0 25 0.0 %

          Line data    Source code
       1             : //# CalTable2.cc: Implementation of CalTable2.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: casa-feedback@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/CalTable2.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           0 : CalTable2::CalTable2() : itsMainTable(0), itsDescTable(0), itsHistoryTable(0)
      44             : {
      45             : // Default null constructor for calibration table; do nothing for now
      46             : // Output to private data:
      47             : //    itsMainTable         Table*           Ptr to cal_main Table object
      48             : //    itsDescTable         Table*           Ptr to cal_desc Table object
      49             : //    itsHistoryTable      Table*           Ptr to cal_history Table object
      50             : //
      51           0 : };
      52             : 
      53             : //----------------------------------------------------------------------------
      54             : 
      55           0 : CalTable2::~CalTable2()
      56             : {
      57             : // Default desctructor
      58             : // Output to private data:
      59             : //    itsMainTable         Table*           Ptr to cal_main Table object
      60             : //    itsDescTable         Table*           Ptr to cal_desc Table object
      61             : //    itsHistoryTable      Table*           Ptr to cal_history Table object
      62             : //
      63           0 :   if (itsMainTable) {
      64           0 :     delete (itsMainTable);
      65             :   };
      66           0 :   if (itsDescTable) {
      67           0 :     delete (itsDescTable);
      68             :   };
      69           0 :   if (itsHistoryTable) {
      70           0 :     delete (itsHistoryTable);
      71             :   };
      72           0 : };
      73             : 
      74             : //----------------------------------------------------------------------------
      75             : 
      76           0 : CalTable2::CalTable2 (const String& tableName, CalTableDesc2& ctableDesc,
      77           0 :                     Table::TableOption access) : 
      78           0 :   itsMainTable(0), itsDescTable(0), itsHistoryTable(0)
      79             : {
      80             : // Construct from a cal table name, descriptor and access option.
      81             : // Used for creating new tables.
      82             : // Input:
      83             : //    tableName        const String&         Cal table name
      84             : //    ctableDesc       const CalTableDesc&   Cal table descriptor
      85             : //    access           Table::TableOption    Access mode
      86             : // Output to private data:
      87             : //    itsMainTable     Table*                Ptr to cal_main Table object
      88             : //    itsDescTable     Table*                Ptr to cal_desc Table object
      89             : //    itsHistoryTable  Table*                Ptr to cal_history Table object
      90             : //
      91           0 :   if (access == Table::New || access == Table::NewNoReplace ||
      92             :       access == Table::Scratch) 
      93           0 :       createCalTable (tableName, ctableDesc, access);
      94             :   else 
      95           0 :       openCalTable (tableName, access);
      96             : 
      97             :   //  createCalTable (tableName, ctableDesc, access);
      98           0 :   parType_ = ctableDesc.parType();
      99           0 : };
     100             : 
     101             : //----------------------------------------------------------------------------
     102             : 
     103           0 : CalTable2::CalTable2 (const String& tableName, Table::TableOption access) :
     104           0 :   itsMainTable(0), itsDescTable(0), itsHistoryTable(0)
     105             : {
     106             : // Construct from a cal table name and access option. Used for
     107             : // accessing existing tables.
     108             : // Input:
     109             : //    tableName        const String&         Cal table name
     110             : //    access           Table::TableOption    Access option
     111             : // Output to private data:
     112             : //    itsMainTable     Table*                Ptr to cal_main Table object
     113             : //    itsDescTable     Table*                Ptr to cal_desc Table object
     114             : //    itsHistoryTable  Table*                Ptr to cal_history Table object
     115             : //
     116           0 :   openCalTable (tableName, access);
     117           0 : };
     118             : 
     119             : //----------------------------------------------------------------------------
     120             : 
     121           0 : CalTable2::CalTable2 (const Table& table)
     122             : {
     123             : // Construct from an existing table object
     124             : // Input:
     125             : //    table            const Table&          Input table
     126             : // Output to private data:
     127             : //    itsMainTable     Table*                Ptr to cal_main Table object
     128             : //    itsDescTable     Table*                Ptr to cal_desc Table object
     129             : //    itsHistoryTable  Table*                Ptr to cal_history Table object
     130             : //
     131           0 :   itsMainTable = new Table (table);
     132           0 :   itsDescTable = new Table 
     133           0 :     (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_DESC)));
     134           0 :   itsHistoryTable = new Table
     135           0 :     (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_HISTORY)));
     136           0 : };
     137             : 
     138             : //----------------------------------------------------------------------------
     139             : 
     140           0 : CalTable2::CalTable2 (const CalTable2& other)
     141             : {
     142             : // Copy constructor
     143             : // Input:
     144             : //    other            const CalTable2&       Existing CalTable2 object
     145             : // Output to private data:
     146             : //    itsMainTable     Table*                Ptr to cal_main Table object
     147             : //    itsDescTable     Table*                Ptr to cal_desc Table object
     148             : //    itsHistoryTable  Table*                Ptr to cal_history Table object
     149             : //
     150           0 :   itsMainTable = new Table (*(other.itsMainTable));
     151           0 :   itsDescTable = new Table 
     152           0 :     (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_DESC)));
     153           0 :   itsHistoryTable = new Table
     154           0 :     (itsMainTable->keywordSet().asTable (MSC::fieldName (MSC::CAL_HISTORY)));
     155           0 : };
     156             : 
     157             : //----------------------------------------------------------------------------
     158             : 
     159           0 : CalTable2& CalTable2::operator= (const CalTable2& other)
     160             : {
     161             : // Assignment operator
     162             : // Input:
     163             : //    other            const CalTable&       RHS CalTable object
     164             : // Output to private data:
     165             : //    itsMainTable     Table*                Ptr to cal_main Table object
     166             : //    itsDescTable     Table*                Ptr to cal_desc Table object
     167             : //    itsHistoryTable  Table*                Ptr to cal_history Table object
     168             : //
     169           0 :   Bool identity = (this == &other);
     170           0 :   if (itsMainTable && !identity) {
     171           0 :     *itsMainTable = *(other.itsMainTable);
     172             :   };
     173           0 :   if (itsDescTable && !identity) {
     174           0 :     *itsDescTable = *(other.itsDescTable);
     175             :   };
     176           0 :   if (itsHistoryTable && !identity) {
     177           0 :     *itsHistoryTable = *(other.itsHistoryTable);
     178             :   };
     179           0 :   return *this;
     180             : };
     181             : 
     182             : //----------------------------------------------------------------------------
     183             : 
     184           0 : CalTable2 CalTable2::sort (const Block <String>& columnNames, Sort::Order order,
     185             :                          Sort::Option option)
     186             : {
     187             : // Sort the calibration table on multiple columns
     188             : // Input:
     189             : //    columnNames      const Block <String>&    Sort columns
     190             : //    order            Sort::Order              Sort order
     191             : //    option           Sort::Option             Sort option
     192             : // Output:
     193             : //    sort             CalTable2                 Sorted reference cal table
     194             : // Input from private data:
     195             : //    itsMainTable     Table*                   Underlying table object
     196             : // 
     197             :   // Sort the cal_main table directly
     198           0 :   Table result = itsMainTable->sort (columnNames, order, option);
     199           0 :   return CalTable2 (result);
     200           0 : };
     201             : 
     202             : //----------------------------------------------------------------------------
     203             : 
     204           0 : void CalTable2::sort2 (const Block <String>& columnNames, Sort::Order order,
     205             :                          Sort::Option option)
     206             : {
     207             : // Sort the calibration table on multiple columns
     208             : // Input:
     209             : //    columnNames      const Block <String>&    Sort columns
     210             : //    order            Sort::Order              Sort order
     211             : //    option           Sort::Option             Sort option
     212             : // Output:
     213             : //    sort             CalTable2                 Sorted reference cal table
     214             : // Input from private data:
     215             : //    itsMainTable     Table*                   Underlying table object
     216             : // 
     217             :   // Sort the cal_main table directly (IN PLACE!)
     218           0 :   Table *sorted = new Table(itsMainTable->sort (columnNames, order, option));
     219           0 :   delete itsMainTable;
     220           0 :   itsMainTable=sorted;
     221             : 
     222           0 :   return;
     223             : };
     224             : 
     225             : //----------------------------------------------------------------------------
     226             : 
     227           0 : CalTable2 CalTable2::select (const String& calSelect)
     228             : {
     229             : // Apply selection to the calibration table
     230             : // Input:
     231             : //    calSelect        const String&            TAQL selection string
     232             : // Output:
     233             : //    select           CalTable2                 Selected reference cal table
     234             : // Input from private data:
     235             : //    itsMainTable     Table*                   Underlying table object
     236             : //
     237             :   // Apply selection to the cal_main table directly
     238           0 :   Int len = calSelect.length();
     239           0 :   Int nspace = calSelect.freq (' ');
     240           0 :   if (calSelect.empty() || nspace==len) {
     241           0 :     return *this;
     242             :   } else {
     243           0 :     String parseString = "select from $1 where " + calSelect;
     244           0 :     Table result = tableCommand(parseString, *itsMainTable).table();
     245           0 :     return CalTable2 (result);
     246           0 :   };
     247             : };
     248             : 
     249             : //----------------------------------------------------------------------------
     250             : 
     251           0 : void CalTable2::select2 (const String& calSelect)
     252             : {
     253             : // Apply selection to the calibration table
     254             : // Input:
     255             : //    calSelect        const String&            TAQL selection string
     256             : // Output:
     257             : //    select           CalTable2                 Selected reference cal table
     258             : // Input from private data:
     259             : //    itsMainTable     Table*                   Underlying table object
     260             : //
     261             :   // Apply selection to the cal_main table directly
     262           0 :   Int len = calSelect.length();
     263           0 :   Int nspace = calSelect.freq (' ');
     264           0 :   if (!calSelect.empty() && nspace!=len) {
     265           0 :     String parseString = "select from $1 where " + calSelect;
     266           0 :     Table *selected = new Table(tableCommand(parseString, *itsMainTable).table());
     267           0 :     delete itsMainTable;
     268           0 :     itsMainTable=selected;
     269           0 :   };
     270           0 :   return;
     271             : };
     272             : 
     273             : //----------------------------------------------------------------------------
     274             : 
     275           0 : Int CalTable2::nRowMain() const
     276             : {
     277             : // Return the number of rows in cal_main
     278             : // Output:
     279             : //    nRowMain         Int                   No of rows in cal_main
     280             : //
     281           0 :   return itsMainTable->nrow();
     282             : };
     283             : 
     284             : //----------------------------------------------------------------------------
     285             : 
     286           0 : Int CalTable2::nRowDesc() const
     287             : {
     288             : // Return the number of rows in cal_desc
     289             : // Output:
     290             : //    nRowDesc         Int                   No of rows in cal_desc
     291             : //
     292           0 :   return itsDescTable->nrow();
     293             : };
     294             : 
     295             : //----------------------------------------------------------------------------
     296             : 
     297           0 : Int CalTable2::nRowHistory() const
     298             : {
     299             : // Return the number of rows in cal_history
     300             : // Output:
     301             : //    nRowHistory      Int                   No of rows in cal_history
     302             : //
     303           0 :   return itsHistoryTable->nrow();
     304             : };
     305             : 
     306             : //----------------------------------------------------------------------------
     307             : 
     308           0 : Record CalTable2::getRowMain (const Int& jrow)
     309             : {
     310             : // Get a row from cal_main
     311             : // Input:
     312             : //    jrow             const Int&            Row number
     313             : // Output:
     314             : //    getRowMain       Record                Row record
     315             : //
     316           0 :   ROTableRow trow (*itsMainTable);
     317           0 :   trow.get (jrow);
     318           0 :   return trow.record();
     319           0 : };
     320             : 
     321             : //----------------------------------------------------------------------------
     322             : 
     323           0 : Record CalTable2::getRowDesc (const Int& jrow)
     324             : {
     325             : // Get a row from cal_desc
     326             : // Input:
     327             : //    jrow             const Int&            Row number
     328             : // Output:
     329             : //    getRowDesc       Record                Row record
     330             : //
     331           0 :   ROTableRow trow (*itsDescTable);
     332           0 :   trow.get (jrow);
     333           0 :   return trow.record();
     334           0 : };
     335             : 
     336             : //----------------------------------------------------------------------------
     337             : 
     338           0 : Record CalTable2::getRowHistory (const Int& jrow)
     339             : {
     340             : // Get a row from cal_history
     341             : // Input:
     342             : //    jrow             const Int&            Row number
     343             : // Output:
     344             : //    getRowHistory    Record                Row record
     345             : //
     346           0 :   ROTableRow trow (*itsHistoryTable);
     347           0 :   trow.get (jrow);
     348           0 :   return trow.record();
     349           0 : };
     350             : 
     351             : //----------------------------------------------------------------------------
     352             : 
     353           0 : void CalTable2::putRowMain (const Int& jrow, CalMainRecord& tableRec)
     354             : {
     355             : // Put a row to cal_main
     356             : // Input:
     357             : //    jrow             const Int&            Row number
     358             : //    tableRec         CalMainRecord&        Table record
     359             : //
     360             :   // Add rows as required
     361           0 :   Int nMaxRow = itsMainTable->nrow();
     362           0 :   Int nAdd = jrow - nMaxRow + 1;
     363           0 :   if (nAdd > 0) {
     364           0 :     itsMainTable->addRow (nAdd);
     365             :   };
     366             : 
     367             :   // Write the record
     368           0 :   TableRow trow (*itsMainTable);
     369           0 :   TableRecord trec = tableRec.record();
     370           0 :   trow.putMatchingFields (jrow, trec);
     371           0 : };
     372             : 
     373             : //----------------------------------------------------------------------------
     374             : 
     375           0 : void CalTable2::putRowDesc (const Int& jrow, CalDescRecord& tableRec)
     376             : {
     377             : // Put a row to cal_desc
     378             : // Input:
     379             : //    jrow             const Int&            Row number
     380             : //    tableRec         CalDescRecord&        Table record
     381             : //
     382             :   // Add rows as required
     383           0 :   Int nMaxRow = itsDescTable->nrow();
     384           0 :   Int nAdd = jrow - nMaxRow + 1;
     385           0 :   if (nAdd > 0) {
     386           0 :     itsDescTable->addRow (nAdd);
     387             :   };
     388             : 
     389             :   // Write the record
     390           0 :   TableRow trow (*itsDescTable);
     391           0 :   TableRecord trec = tableRec.record();
     392           0 :   trow.putMatchingFields (jrow, trec);
     393           0 : };
     394             : 
     395             : //----------------------------------------------------------------------------
     396             : 
     397           0 : void CalTable2::putRowHistory (const Int& jrow, CalHistoryRecord& tableRec)
     398             : {
     399             : // Put a row to cal_history
     400             : // Input:
     401             : //    jrow             const Int&            Row number
     402             : //    tableRec         CalHistoryRecord&     Table record
     403             : //
     404             :   // Add rows as required
     405           0 :   Int nMaxRow = itsHistoryTable->nrow();
     406           0 :   Int nAdd = jrow - nMaxRow + 1;
     407           0 :   if (nAdd > 0) {
     408           0 :     itsHistoryTable->addRow (nAdd);
     409             :   };
     410           0 :   TableRow trow (*itsHistoryTable);
     411           0 :   TableRecord trec = tableRec.record();
     412           0 :   trow.putMatchingFields (jrow, trec);
     413           0 : };
     414             : 
     415             : //----------------------------------------------------------------------------
     416             : 
     417           0 : Int CalTable2::maxAntenna()
     418             : {
     419             : // Return the maximum antenna no. found in the calibration table
     420             : // Output:
     421             : //    maxAntenna          Int         Max. antenna no. found
     422             : //
     423           0 :   ScalarColumn <Int> antCol (*itsMainTable, MSC::fieldName (MSC::ANTENNA1));
     424           0 :   Vector <Int> antVal;
     425           0 :   antCol.getColumn (antVal);
     426             :   uInt i;
     427           0 :   Int maxAnt = 0;
     428           0 :   for (i = 0; i < antVal.nelements(); i++) {
     429           0 :     maxAnt = max (antVal(i), maxAnt);
     430             :   };
     431             : 
     432           0 :   return maxAnt;
     433           0 : };
     434             : 
     435             : //----------------------------------------------------------------------------
     436             : 
     437           0 : Int CalTable2::numberTimeSlots (const Double& fracError)
     438             : {
     439             : // Return the no. of unique time slots found in the calibration table
     440             : // Input:
     441             : //    fracError        const Double&   Fractional error allowed in comparison
     442             : // Output:
     443             : //    numberTimeSlots  Int             No. of unique time stamps found
     444             : //    
     445           0 :   ScalarColumn <Double> timeCol (*itsMainTable, MSC::fieldName (MSC::TIME));
     446           0 :   ScalarColumn <Double> intervalCol (*itsMainTable, 
     447           0 :                                      MSC::fieldName (MSC::INTERVAL));
     448           0 :   Vector <Double> timeVal;
     449           0 :   Vector <Double> intervalVal;
     450           0 :   timeCol.getColumn (timeVal);
     451           0 :   intervalCol.getColumn (intervalVal);
     452             :   
     453             :   // Index of matches
     454           0 :   Vector <Int> match;
     455           0 :   Int n = timeVal.nelements();
     456           0 :   match.resize (n);
     457           0 :   match = 0;
     458             :   Int i, j;
     459             :   Bool found;
     460             : 
     461             :   // Loop through all time stamps
     462           0 :   for (i = 0; i < (n-1); i++) {
     463           0 :     found = false;
     464           0 :     for (j = i+1; j < n; j++) {
     465           0 :       if (abs (timeVal(i) - timeVal(j)) < fracError * intervalVal(i) &&
     466           0 :           abs (intervalVal(i) - intervalVal(j)) < fracError * intervalVal(i)) {
     467           0 :         found = true;
     468             :       };
     469             :     };
     470           0 :     if (!found) {
     471           0 :       match(i) = 1;
     472             :     };
     473             :   };
     474           0 :   if (n > 0) {
     475           0 :     match(n-1) = 1;
     476             :   };
     477             : 
     478             :   // Sum the number of matches
     479           0 :   Int sum = 0;
     480           0 :   for (i = 0; i < n; i++) {
     481           0 :     sum = sum + match(i);
     482             :   };
     483             : 
     484           0 :   return sum;
     485           0 : };
     486             : 
     487             : //----------------------------------------------------------------------------
     488             : 
     489           0 : void CalTable2::createCalTable (const String& tableName, 
     490             :                                CalTableDesc2& ctableDesc,
     491             :                                Table::TableOption access)
     492             : {
     493             : // Create a new cal table
     494             : // Input:
     495             : //    tableName        const String&         Cal table name
     496             : //    ctableDesc       const CalTableDesc&   Cal table descriptor
     497             : //    access           Table::TableOption    Access mode
     498             : // Output to private data:
     499             : //    itsMainTable     Table*                Ptr to cal_main Table object
     500             : //    itsDescTable     Table*                Ptr to cal_desc Table object
     501             : //    itsHistoryTable  Table*                Ptr to cal_history Table object
     502             : //
     503             :   // Construct the table descriptor, including the sub-table structure
     504             :   // for cal_desc and cal_history. Add subtables to CAL_DESC and
     505             :   // CAL_HISTORY keywords.
     506           0 :   SetupNewTable calMainSetup (tableName, ctableDesc.calMainDesc(), access);
     507           0 :   itsMainTable = new Table (calMainSetup);
     508           0 :   SetupNewTable calDescSetup (tableName + "/CAL_DESC", 
     509           0 :                               ctableDesc.calDescDesc(), access);
     510           0 :   itsDescTable = new Table (calDescSetup);
     511           0 :   itsMainTable->rwKeywordSet().defineTable (MSC::fieldName (MSC::CAL_DESC),
     512           0 :                                             *itsDescTable);
     513           0 :   SetupNewTable calHistorySetup (tableName + "/CAL_HISTORY",
     514           0 :                                  ctableDesc.calHistoryDesc(), access);
     515           0 :   itsHistoryTable = new Table (calHistorySetup);
     516           0 :   itsMainTable->rwKeywordSet().defineTable (MSC::fieldName (MSC::CAL_HISTORY),
     517           0 :                                             *itsHistoryTable);
     518             :   // Set table type and sub-type
     519           0 :   String reqdType = TableInfo::type (TableInfo::ME_CALIBRATION);
     520           0 :   itsMainTable->tableInfo().setType (reqdType);
     521           0 :   itsMainTable->tableInfo().setSubType (itsMainTable->tableDesc().getType());
     522           0 : };
     523             : 
     524             : //----------------------------------------------------------------------------
     525             : 
     526           0 : void CalTable2::openCalTable (const String& tableName, 
     527             :                              Table::TableOption access)
     528             : {
     529             : // Open an existing cal table
     530             : // Input:
     531             : //    tableName        const String&         Cal table name
     532             : //    access           Table::TableOption    Access option
     533             : // Output to private data:
     534             : //    itsMainTable     Table*                Ptr to cal_main Table object
     535             : //    itsDescTable     Table*                Ptr to cal_desc Table object
     536             : //    itsHistoryTable  Table*                Ptr to cal_history Table object
     537             : //
     538           0 :   itsMainTable = new Table (tableName, access);
     539           0 :   itsDescTable = new Table (itsMainTable->keywordSet().asTable
     540           0 :     (MSC::fieldName(MSC::CAL_DESC)));
     541           0 :   itsHistoryTable = new Table (itsMainTable->keywordSet().asTable
     542           0 :     (MSC::fieldName(MSC::CAL_HISTORY)));
     543           0 : };
     544             : 
     545             : //----------------------------------------------------------------------------
     546             : 
     547             : } //# NAMESPACE CASA - END
     548             : 

Generated by: LCOV version 1.16