LCOV - code coverage report
Current view: top level - synthesis/CalTables/test - tCTIter.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 127 180 70.6 %
Date: 2024-12-11 20:54:31 Functions: 3 3 100.0 %

          Line data    Source code
       1             : //# tNewCalTable.cc: Test program for NewCalTable class
       2             : //# Copyright (C) 2011
       3             : //# Associated Universities, Inc. Washington DC, USA.
       4             : //#
       5             : //# This program is free software; you can redistribute it and/or modify it
       6             : //# under the terms of the GNU General Public License as published by the Free
       7             : //# Software Foundation; either version 2 of the License, or (at your option)
       8             : //# any later version.
       9             : //#
      10             : //# This program 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 General Public License for
      13             : //# more details.
      14             : //#
      15             : //# You should have received a copy of the GNU General Public License along
      16             : //# with this program; if not, write to the Free Software Foundation, Inc.,
      17             : //# 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: tNewCalTable.cc 15602 2011-07-14 00:03:34Z tak.tsutsumi $
      27             : 
      28             : #include <synthesis/CalTables/NewCalTable.h>
      29             : #include <synthesis/CalTables/CTMainColumns.h>
      30             : #include <synthesis/CalTables/CTIter.h>
      31             : #include <casacore/casa/Arrays/Cube.h>
      32             : #include <casacore/casa/Exceptions/Error.h>
      33             : #include <casacore/casa/iostream.h>
      34             : #include <casacore/casa/BasicMath/Math.h>
      35             : 
      36             : using namespace casa;
      37             : using namespace casacore;
      38             : 
      39             : // <summary>
      40             : // Test program for CTIter class.
      41             : // </summary>
      42             : 
      43             : // Control verbosity
      44             : #define CTITERTEST_VERBOSE false
      45             : 
      46             : Bool foundError = false;
      47             : 
      48           1 : void doTest1 (Bool verbose=false) {
      49             : 
      50           1 :   cout << "****----tCTIter doTest1()----****" << endl;
      51             :   
      52             :   // Make a testing NewCalTable (Table::Memory)
      53           1 :   uInt nFld(1), nAnt(10), nSpw(2), nObs(1), nScan(1),nTime(3);
      54           1 :   Vector<Int> nChan(nSpw,1);
      55           1 :   Bool disk(verbose);
      56             :   NewCalTable tnct("tCTIter1.ct","Complex",
      57             :                    nObs,nScan,nTime,
      58             :                    nAnt,nSpw,nChan,
      59             :                    nFld,
      60             :                    0.0,0.0,   // rtime,tint defaults
      61           2 :                    disk,false);
      62             : 
      63             :   // some sanity checks on the test NewCalTable
      64           1 :   if (verbose) cout << "Table::Type: " << tnct.tableType() 
      65           0 :                     << " (should be " << Table::Memory << ")"
      66           0 :                     << endl;
      67           1 :   AlwaysAssert( (tnct.tableType() == Table::Memory), AipsError);
      68           1 :   if (verbose) cout << "nrow = " << tnct.nrow() 
      69           0 :                     << "  (should be " << nObs*nScan*nTime*nSpw*nAnt << ")"
      70           0 :                     << endl;
      71           1 :   AlwaysAssert( (tnct.nrow()==nObs*nScan*nTime*nSpw*nAnt), AipsError);
      72             :   
      73             :   // Set up iteration
      74           1 :   Block<String> sortcol(2);
      75           1 :   sortcol[0]="SPECTRAL_WINDOW_ID";
      76           1 :   sortcol[1]="ANTENNA1";
      77           1 :   ROCTIter nctiter(tnct,sortcol);
      78             : 
      79             :   // Count iterations
      80             :   //  TBD: provide this service in CalIter itself!
      81           1 :   uInt niter(0);
      82          21 :   while (!nctiter.pastEnd()) {
      83          20 :     niter+=1;
      84          20 :     nctiter.next();
      85             :   }
      86             : 
      87           1 :   if (verbose) cout << "niter = " << niter 
      88           0 :                     << " (should be " << nAnt*nSpw << ")"
      89           0 :                     << endl;
      90           1 :   AlwaysAssert( (niter==nAnt*nSpw), AipsError);
      91             :   
      92             :   // Test individual iterations
      93           1 :   nctiter.reset();
      94           1 :   Vector<NewCalTable> tablist(niter);
      95           1 :   Int iter=0;
      96           1 :   if (verbose) cout << "Testing iteration." << endl;
      97          21 :   while (!nctiter.pastEnd()) {
      98          20 :     Int expectSpw(iter/nAnt);
      99          20 :     Int expectAnt(iter%nAnt);    
     100          20 :     Int thisspw(nctiter.thisSpw());
     101          20 :     Int thisant(nctiter.thisAntenna1());
     102             : 
     103          20 :     NewCalTable tab(nctiter.table());
     104          20 :     tablist(iter)=tab;
     105             : 
     106          20 :     if (verbose) {
     107             :       cout << iter 
     108           0 :            << " nrow=" << nctiter.nrow()
     109           0 :            << " spw="<<thisspw
     110           0 :            << " (should be " << expectSpw << ")"
     111           0 :            << " ant1="<<thisant
     112           0 :            << " (should be " << expectAnt << ")"
     113           0 :            << endl;
     114             :     }
     115             : 
     116          20 :     AlwaysAssert( (thisspw==expectSpw) , AipsError);
     117          20 :     AlwaysAssert( (thisant==expectAnt) , AipsError);
     118             : 
     119          20 :     nctiter.next();
     120          20 :     ++iter;
     121          20 :   }
     122             :   
     123             : 
     124             :   // Test recorded reference table contents
     125           1 :   if (verbose) cout << "Testing iteration reference table contents:" << endl;
     126          21 :   for (uInt i=0;i<niter;++i) {
     127          20 :     Int expectSpw(i/nAnt);
     128          20 :     Int expectAnt(i%nAnt);
     129             :     
     130          20 :     CTMainColumns mc(tablist(i));
     131             :     
     132          20 :     Vector<Int> thisspw=mc.spwId().getColumn();
     133          20 :     Vector<Int> thisant=mc.antenna1().getColumn();
     134             : 
     135          20 :     if (verbose) {
     136           0 :       cout << i << " "
     137           0 :            << "nrow=" << tablist(i).nrow() << " "
     138           0 :            << "spw=" << thisspw(0) 
     139           0 :            << " (should be " << expectSpw << ")"
     140           0 :            << " ant1=" << thisant(0) 
     141           0 :            << " (should be " << expectAnt << ")"
     142             :         //         << "paramshape= " << mc.cparam().shape(0) << " "
     143             :         //         << "param= " << mc.cparam()(0) << " "
     144           0 :            << endl;
     145             :     }
     146             : 
     147             :     // Tests
     148          20 :     AlwaysAssert( (tablist(i).nrow()==nObs*nScan*nTime) , AipsError);
     149          20 :     AlwaysAssert( allEQ(thisspw,expectSpw) , AipsError);
     150          20 :     AlwaysAssert( allEQ(thisant,expectAnt) , AipsError);
     151             : 
     152             :     // TBD: Add param value tests here...
     153             : 
     154          20 :   }
     155           1 : }
     156           1 : void doTest2 (Bool verbose=false) {
     157             :   
     158           1 :   cout << "****----tCTIter doTest2()----****" << endl;
     159             : 
     160             : 
     161             :   // Test (the writable) CTIter
     162             : 
     163             :   // Make a testing NewCalTable (Table::Memory)
     164           1 :   uInt nFld(2), nAnt(10), nSpw(3), nObs(4), nScan(2), nTime(3);
     165           1 :   Vector<Int> nChan(nSpw,1);
     166           1 :   Double rtime(0.0), tint(60.0);  // trigger default times
     167           1 :   Bool disk(verbose);
     168             :   NewCalTable tnct("tCTIter2.ct","Complex",
     169             :                    nObs,nScan,nTime,
     170             :                    nAnt,nSpw,nChan,
     171             :                    nFld,
     172             :                    rtime,tint,
     173           2 :                    disk,false);
     174             : 
     175             : 
     176           1 :   if (verbose)
     177           0 :     cout << "OBS_ID col ok? " << boolalpha << tnct.tableDesc().isColumn(NCT::fieldName(NCT::OBSERVATION_ID)) << endl;
     178           1 :   AlwaysAssert( tnct.tableDesc().isColumn(NCT::fieldName(NCT::OBSERVATION_ID)), AipsError);
     179             : 
     180             :   // some sanity checks on the test NewCalTable
     181           1 :   if (verbose) cout << "Table::Type: " << tnct.tableType() 
     182           0 :                     << " (should be " << Table::Memory << ")" << endl;
     183           1 :   AlwaysAssert( (tnct.tableType() == Table::Memory), AipsError);
     184             : 
     185           1 :   Int nTotRow(tnct.nrow());
     186           1 :   if (verbose) cout << "nrow = " << nTotRow
     187           0 :                     << " (should be " << nObs*nScan*nTime*nSpw*nAnt << ")"
     188           0 :                     << endl;
     189           1 :   AlwaysAssert( (tnct.nrow()==nObs*nScan*nTime*nSpw*nAnt), AipsError);
     190             :   
     191             :   // Set up iteration
     192           1 :   Block<String> sortcol(3);
     193           1 :   sortcol[0]="OBSERVATION_ID";
     194           1 :   sortcol[1]="SPECTRAL_WINDOW_ID";
     195           1 :   sortcol[2]="ANTENNA1";
     196           1 :   CTIter nctiter(tnct,sortcol);
     197             : 
     198             :   // Count iterations
     199             :   //  TBD: provide this service in CalIter itself!
     200           1 :   uInt niter(0);
     201         121 :   while (!nctiter.pastEnd()) {
     202         120 :     niter+=1;
     203         120 :     nctiter.next();
     204             :   }
     205           1 :   if (verbose) cout << "niter = " << niter 
     206           0 :                     << " (should be " << nObs*nSpw*nAnt << ")"
     207           0 :                     << endl;
     208           1 :   AlwaysAssert( (niter==nObs*nSpw*nAnt), AipsError)
     209             :   
     210             :   // Test individual iterations
     211           1 :   nctiter.reset();
     212           1 :   Vector<NewCalTable> tablist(niter);
     213           1 :   Int iter=0;
     214           1 :   if (verbose) cout << "Testing iteration." << endl;
     215         121 :   while (!nctiter.pastEnd()) {
     216         120 :     Int expectObs(iter/(nAnt*nSpw));
     217         120 :     Int expectSpw((iter%(nAnt*nSpw))/nAnt);
     218         120 :     Int expectAnt(iter%nAnt);    
     219         120 :     Int thisobs(nctiter.thisObs());
     220         120 :     Int thisspw(nctiter.thisSpw());
     221         120 :     Int thisant(nctiter.thisAntenna1());
     222             : 
     223         120 :     Vector<Int> obsv(nctiter.obs());
     224         120 :     Vector<Int> scanv(nctiter.scan());
     225             : 
     226         120 :     NewCalTable tab(nctiter.table());
     227         120 :     tablist(iter)=tab;
     228             : 
     229         120 :     if (verbose) {
     230             :       cout << iter 
     231           0 :            << " nrow=" << nctiter.nrow()
     232           0 :            << " (should be " << nTotRow/niter << ")"
     233           0 :            << " obs="<<thisobs
     234           0 :            << " (should be " << expectObs << ")"
     235           0 :            << " spw="<<thisspw
     236           0 :            << " (should be " << expectSpw << ")"
     237           0 :            << " ant1="<<thisant
     238           0 :            << " (should be " << expectAnt << ")"
     239           0 :            << endl;
     240             :     }
     241             : 
     242         120 :     AlwaysAssert( (nctiter.nrow()==Int(nTotRow/niter)) , AipsError);
     243         120 :     AlwaysAssert( (thisobs==expectObs) , AipsError);
     244         120 :     AlwaysAssert( (thisspw==expectSpw) , AipsError);
     245         120 :     AlwaysAssert( (thisant==expectAnt) , AipsError);
     246             : 
     247             :     // Play with setable columns
     248         120 :     Cube<Bool> flag(nctiter.flag());
     249             :     //    cout << boolalpha << " flag = " << flag << endl;
     250         120 :     if (thisant==Int(nAnt/2))
     251          12 :       flag=true;
     252         120 :     nctiter.setflag(flag);
     253             : 
     254         120 :     nctiter.next();
     255         120 :     ++iter;
     256         120 :   }
     257             :   
     258             : 
     259             :   // Test recorded reference table contents
     260           1 :   if (verbose) cout << "Testing iteration reference table contents:" << endl;
     261         121 :   for (uInt i=0;i<niter;++i) {
     262         120 :     Int expectObs(i/(nAnt*nSpw));
     263         120 :     Int expectSpw((i%(nAnt*nSpw))/nAnt);
     264         120 :     Int expectAnt(i%nAnt);
     265             :     
     266         120 :     CTMainColumns mc(tablist(i));
     267             :     
     268         120 :     Vector<Int> thisobs=mc.obsId().getColumn();
     269         120 :     Vector<Int> thisspw=mc.spwId().getColumn();
     270         120 :     Vector<Int> thisant=mc.antenna1().getColumn();
     271             : 
     272         120 :     if (verbose) {
     273           0 :       cout.precision(15);
     274           0 :       cout << i << " "
     275           0 :            << "nrow=" << tablist(i).nrow() << " "
     276           0 :            << " (should be " << nScan*nTime << ")"
     277           0 :            << " obs=" << thisobs(0) 
     278           0 :            << " (should be " << expectObs << ")"
     279           0 :            << " spw=" << thisspw(0) 
     280           0 :            << " (should be " << expectSpw << ")"
     281           0 :            << " ant1=" << thisant(0) 
     282           0 :            << " (should be " << expectAnt << ")"
     283           0 :            << endl;
     284             :     }
     285             : 
     286             :     // Tests
     287         120 :     AlwaysAssert( (tablist(i).nrow()==nScan*nTime) , AipsError);
     288         120 :     AlwaysAssert( allEQ(thisobs,expectObs) , AipsError);
     289         120 :     AlwaysAssert( allEQ(thisspw,expectSpw) , AipsError);
     290         120 :     AlwaysAssert( allEQ(thisant,expectAnt) , AipsError);
     291             : 
     292             :     // Test the flags we wrote
     293         120 :     if (thisant(0)==Int(nAnt/2)) {
     294          12 :       Bool f(Cube<Bool>(mc.flag().getColumn())(0,0,0));
     295          12 :       AlwaysAssert( (f), AipsError);
     296             :     }
     297             : 
     298             : 
     299             :     // TBD: Add param value tests here...
     300             : 
     301         120 :   }
     302             : 
     303           1 : }
     304             : 
     305           1 : int main ()
     306             : {
     307             :   try {
     308             : 
     309           1 :     doTest1(CTITERTEST_VERBOSE);
     310           1 :     doTest2(CTITERTEST_VERBOSE);
     311             : 
     312           0 :   } catch (AipsError x) {
     313           0 :     cout << "Unexpected exception: " << x.getMesg() << endl;
     314           0 :     exit(1);
     315           0 :   } catch (...) {
     316           0 :     cout << "Unexpected unknown exception" << endl;
     317           0 :     exit(1);
     318           0 :   }
     319           1 :   if (foundError) {
     320           0 :     exit(1);
     321             :   }
     322           1 :   cout << "OK" << endl;
     323           1 :   exit(0);
     324             : };

Generated by: LCOV version 1.16