LCOV - code coverage report
Current view: top level - synthesis/MeasurementComponents - VisCal.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 721 1111 64.9 %
Date: 2024-11-06 17:42:47 Functions: 63 100 63.0 %

          Line data    Source code
       1             : //# VisCal.cc: Implementation of VisCal classes
       2             : //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,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: VisCal.cc,v 1.15 2006/02/06 19:23:11 gmoellen Exp $
      27             : 
      28             : #include <synthesis/MeasurementComponents/VisCal.h>
      29             : 
      30             : #include <synthesis/MeasurementComponents/MSMetaInfoForCal.h>
      31             : #include <msvis/MSVis/VisBuffer.h>
      32             : 
      33             : #include <casacore/casa/Arrays/ArrayMath.h>
      34             : #include <casacore/casa/Arrays/MaskArrMath.h>
      35             : #include <casacore/casa/Arrays/ArrayLogical.h>
      36             : #include <casacore/casa/Arrays/ArrayIter.h>
      37             : #include <casacore/casa/Arrays/MaskedArray.h>
      38             : #include <casacore/scimath/Mathematics/MatrixMathLA.h>
      39             : #include <casacore/casa/BasicSL/String.h>
      40             : #include <casacore/casa/Utilities/Assert.h>
      41             : #include <casacore/casa/Quanta/MVTime.h>
      42             : #include <casacore/casa/Quanta/Quantum.h>
      43             : #include <casacore/casa/Quanta/QuantumHolder.h>
      44             : #include <casacore/casa/Exceptions/Error.h>
      45             : #include <casacore/casa/OS/Memory.h>
      46             : 
      47             : #include <sstream>
      48             : 
      49             : #include <casacore/casa/Logging/LogMessage.h>
      50             : #include <casacore/casa/Logging/LogSink.h>
      51             : 
      52             : #include <casacore/casa/Quanta/MVTime.h>
      53             : 
      54             : #define PRTLEV 0
      55             : 
      56             : using namespace casacore;
      57             : namespace casa { //# NAMESPACE CASA - BEGIN
      58             : 
      59             : // **********************************************************
      60             : //  VisCal Implementations
      61             : //
      62             : 
      63         124 : VisCal::VisCal(VisSet& vs) :
      64         124 :   msName_(vs.msName()),
      65         124 :   msmc_( new MSMetaInfoForCal(vs.msName()) ),  // a local one
      66         124 :   delmsmc_(True),                              // must be delete in dtor
      67         124 :   nSpw_(vs.numberSpw()),
      68         124 :   nAnt_(vs.numberAnt()),
      69         124 :   nBln_(0),
      70         124 :   currSpw_(0),
      71         124 :   currTime_(vs.numberSpw(),0.0),
      72         124 :   currScan_(vs.numberSpw(),-1),
      73         124 :   currObs_(vs.numberSpw(),-1),
      74         124 :   currField_(vs.numberSpw(),-1),
      75         124 :   currIntent_(vs.numberSpw(),-1),
      76         124 :   currFreq_(vs.numberSpw(),-1),
      77         124 :   lastTime_(vs.numberSpw(),0.0),
      78         124 :   refTime_(0.0),
      79         124 :   refFreq_(0.0),
      80         124 :   nChanPar_(vs.numberSpw(),1),
      81         124 :   nChanMat_(vs.numberSpw(),1),
      82         124 :   startChan_(vs.numberSpw(),0),
      83         124 :   interval_(0.0),
      84         124 :   applied_(false),
      85         124 :   V_(vs.numberSpw(),NULL),
      86         124 :   currCPar_(vs.numberSpw(),NULL),
      87         124 :   currRPar_(vs.numberSpw(),NULL),
      88         124 :   currParOK_(vs.numberSpw(),NULL),
      89         124 :   PValid_(vs.numberSpw(),false),
      90         124 :   calWt_(false),
      91         124 :   currWtScale_(vs.numberSpw(),NULL),
      92         124 :   prtlev_(PRTLEV),
      93         248 :   extratag_("")
      94             : {
      95         124 :   if (prtlev()>2) cout << "VC::VC(vs)" << endl;
      96             : 
      97             :   // Initialize
      98         124 :   initVisCal();
      99         124 : }
     100             : 
     101          27 : VisCal::VisCal(String msname,Int MSnAnt,Int MSnSpw) :
     102          27 :   msName_(msname),
     103          27 :   msmc_( new MSMetaInfoForCal(msname) ),       // a local one
     104          27 :   delmsmc_(True),                              // must be delete in dtor
     105          27 :   nSpw_(MSnSpw),
     106          27 :   nAnt_(MSnAnt),
     107          27 :   nBln_(0),
     108          27 :   currSpw_(0),
     109          27 :   currTime_(MSnSpw,0.0),
     110          27 :   currScan_(MSnSpw,-1),
     111          27 :   currObs_(MSnSpw,-1),
     112          27 :   currField_(MSnSpw,-1),
     113          27 :   currIntent_(MSnSpw,-1),
     114          27 :   currFreq_(MSnSpw,-1),
     115          27 :   lastTime_(MSnSpw,0.0),
     116          27 :   refTime_(0.0),
     117          27 :   refFreq_(0.0),
     118          27 :   nChanPar_(MSnSpw,1),
     119          27 :   nChanMat_(MSnSpw,1),
     120          27 :   startChan_(MSnSpw,0),
     121          27 :   interval_(0.0),
     122          27 :   applied_(false),
     123          27 :   V_(MSnSpw,NULL),
     124          27 :   currCPar_(MSnSpw,NULL),
     125          27 :   currRPar_(MSnSpw,NULL),
     126          27 :   currParOK_(MSnSpw,NULL),
     127          27 :   PValid_(MSnSpw,false),
     128          27 :   calWt_(false),
     129          27 :   currWtScale_(MSnSpw,NULL),
     130          27 :   prtlev_(PRTLEV),
     131          54 :   extratag_("")
     132             : {
     133          27 :   if (prtlev()>2) cout << "VC::VC(msname,MSnAnt,MSnSpw)" << endl;
     134             : 
     135             :   // Initialize
     136          27 :   initVisCal();
     137          27 : }
     138             : 
     139         612 : VisCal::VisCal(const MSMetaInfoForCal& msmc) :
     140         612 :   msName_(msmc.msname()),  // from the msmc (it is still used in some places)
     141         612 :   msmc_(&msmc),
     142         612 :   delmsmc_(False),   //  not local, don't delete
     143         612 :   nSpw_(msmc.nSpw()),
     144         612 :   nAnt_(msmc.nAnt()),
     145         612 :   nBln_(0),
     146         612 :   currSpw_(0),
     147         612 :   currTime_(nSpw_,0.0),
     148         612 :   currScan_(nSpw_,-1),
     149         612 :   currObs_(nSpw_,-1),
     150         612 :   currField_(nSpw_,-1),
     151         612 :   currIntent_(nSpw_,-1),
     152         612 :   currFreq_(nSpw_,-1),
     153         612 :   lastTime_(nSpw_,0.0),
     154         612 :   refTime_(0.0),
     155         612 :   refFreq_(0.0),
     156         612 :   nChanPar_(nSpw_,1),
     157         612 :   nChanMat_(nSpw_,1),
     158         612 :   startChan_(nSpw_,0),
     159         612 :   interval_(0.0),
     160         612 :   applied_(False),
     161         612 :   V_(nSpw_,NULL),
     162         612 :   currCPar_(nSpw_,NULL),
     163         612 :   currRPar_(nSpw_,NULL),
     164         612 :   currParOK_(nSpw_,NULL),
     165         612 :   PValid_(nSpw_,False),
     166         612 :   calWt_(False),
     167         612 :   currWtScale_(nSpw_,NULL),
     168         612 :   prtlev_(PRTLEV),
     169        1224 :   extratag_("")
     170             : {
     171         612 :   if (prtlev()>2) cout << "VC::VC(MSMetaInfoForCal)" << endl;
     172             : 
     173             :   // Initialize
     174         612 :   initVisCal();
     175         612 : }
     176             : 
     177           0 : VisCal::VisCal(const Int& nAnt) :
     178           0 :   msName_(""),
     179           0 :   msmc_(NULL),
     180           0 :   delmsmc_(False),
     181           0 :   nSpw_(1),
     182           0 :   nAnt_(nAnt),
     183           0 :   nBln_(0),
     184           0 :   currSpw_(0),
     185           0 :   currTime_(1,0.0),
     186           0 :   currField_(1,-1),
     187           0 :   currIntent_(1,-1),
     188           0 :   currFreq_(1,-1),
     189           0 :   lastTime_(1,0.0),
     190           0 :   refTime_(0.0),
     191           0 :   refFreq_(0.0),
     192           0 :   nChanPar_(1,1),
     193           0 :   nChanMat_(1,1),
     194           0 :   startChan_(1,0),
     195           0 :   interval_(0.0),
     196           0 :   applied_(false),
     197           0 :   V_(1,NULL),
     198           0 :   currCPar_(1,NULL),
     199           0 :   currRPar_(1,NULL),
     200           0 :   currParOK_(1,NULL),
     201           0 :   PValid_(1,false),
     202           0 :   calWt_(false),
     203           0 :   currWtScale_(1,NULL),
     204           0 :   prtlev_(PRTLEV),
     205           0 :   extratag_("")
     206             : {
     207           0 :   if (prtlev()>2) cout << "VC::VC(i,j,k)" << endl;
     208             : 
     209             :   // Initialize
     210           0 :   initVisCal();
     211           0 : }
     212             : 
     213         763 : VisCal::~VisCal() {
     214             : 
     215         763 :   if (prtlev()>2) cout << "VC::~VC()" << endl;
     216             : 
     217         763 :   deleteVisCal();
     218             : 
     219         763 :   if (delmsmc_)
     220         151 :     delete msmc_;
     221             : 
     222         763 : }
     223             : 
     224           0 : void VisCal::setApply() {
     225             : 
     226           0 :   if (prtlev()>2) cout << "VC::setApply()" << endl;
     227             : 
     228             :   // This is the apply context
     229           0 :   setApplied(true);
     230             : 
     231             :   // Establish non-trivial paramter arrays
     232           0 :   for (Int ispw=0;ispw<nSpw();ispw++) {
     233           0 :     currSpw()=ispw;
     234             : 
     235           0 :     if (nChanPar()>0) {
     236           0 :       switch (parType()) {
     237           0 :       case VisCalEnum::COMPLEX: {
     238           0 :         currCPar().resize(nPar(),nChanPar(),nElem());
     239           0 :         currCPar()=Complex(1.0);
     240           0 :         break;
     241             :       } 
     242           0 :       case VisCalEnum::REAL: {
     243           0 :         currRPar().resize(nPar(),nChanPar(),nElem());
     244           0 :         currRPar()=1.0;
     245           0 :         break;
     246             :       }
     247           0 :       default:
     248           0 :         throw(AipsError("Parameters must be entirely Real or entirely Complex for now!"));
     249             :       }
     250           0 :       currParOK().resize(nPar(),nChanPar(),nElem());
     251           0 :     currParOK()=true;
     252             :     }
     253             :   }
     254             : 
     255           0 : }
     256             : 
     257         298 : void VisCal::setApply(const Record& apply) {
     258             :   //  Inputs:
     259             :   //    apply           Record&       Contains application params
     260             :   //    
     261             : 
     262         298 :   if (prtlev()>2) cout << "VC::setApply(apply)" << endl;
     263             : 
     264             :   // Extract calWt
     265         298 :   if (apply.isDefined("calwt"))
     266         244 :     calWt()=apply.asBool("calwt");
     267             : 
     268             :   // This is apply context  
     269         298 :   setApplied(true);
     270             : 
     271             :   // Initialize flag counting
     272         298 :   initCalFlagCount();
     273             : 
     274             : 
     275         298 : }
     276             : 
     277          43 : void VisCal::setCallib(const Record& callib,const MeasurementSet& /*selms*/) {
     278             : 
     279             :   // Forward to setApply
     280          43 :   VisCal::setApply(callib);
     281             : 
     282          43 : }
     283             : 
     284             : 
     285          76 : String VisCal::applyinfo() {
     286             : 
     287          76 :   ostringstream o;
     288          76 :   o << typeName() << " <pre-computed>";
     289             : 
     290         152 :   return String(o);
     291             : 
     292          76 : }
     293             : 
     294        9064 : void VisCal::correct(VisBuffer& vb, Bool trial) {
     295             : 
     296        9064 :   if (prtlev()>3) cout << "VC::correct(vb)" << endl;
     297             : 
     298             :   // Count pre-cal flags
     299        9064 :   countInFlag(vb);
     300             : 
     301             :   // Call non-in-place version, in-place-wise:
     302        9064 :   correct(vb,vb.visCube(),trial);
     303             : 
     304             :   // Count post-cal flags
     305        9064 :   countOutFlag(vb);
     306             : 
     307        9064 : }
     308             : 
     309      246703 : void VisCal::correct2(vi::VisBuffer2& vb, Bool trial, Bool doWtSp, Bool dosync) {
     310             : 
     311      246703 :   if (prtlev()>3) cout << "VC::correct2(vb)" << endl;
     312             : 
     313             : 
     314             :   // If calibration is unavailable, just return!
     315      246703 :   if (!calAvailable(vb)) {
     316           0 :     return;
     317             :   }
     318             : 
     319             :   // Reaching here, calibration is available, so do it!
     320             : 
     321             :   // Count pre-cal flags
     322      246703 :   countInFlag2(vb);
     323             : 
     324             :   // Bring calibration up-to-date w/ the vb
     325      246703 :   if (dosync)
     326      246703 :     syncCal2(vb,true);
     327             : 
     328             :   // Organize for weight calibration
     329      246703 :   Cube<Float> wtcube;
     330      246703 :   if (doWtSp) {
     331             :     // refer directly to weightSpectrum
     332       23229 :     wtcube.reference(vb.weightSpectrum());
     333             :   }
     334             :   else {
     335             :     // refer to unchan'd weight matrix, rendered as Cube
     336      223474 :     IPosition sh2=vb.weight().shape();
     337      223474 :     IPosition sh3(3,sh2[0],1,sh2[1]);
     338      223474 :     wtcube.reference(vb.weight().reform(sh3));
     339      223474 :   }
     340             : 
     341             :   // Refer to corrected data
     342      246703 :   Cube<Complex> vCC;
     343      246703 :   vCC.reference(vb.visCubeCorrected());
     344             : 
     345             :   // Apply the calibration
     346      246703 :   applyCal2(vb,vCC,wtcube,trial);
     347             : 
     348             :   // Count post-cal flags
     349      246703 :   countOutFlag2(vb);
     350             : 
     351      246703 : }
     352             : 
     353         102 : void VisCal::corrupt(VisBuffer& vb) {
     354             : 
     355         102 :   if (prtlev()>3) cout << "VC::corrupt(vb)" << endl;
     356             : 
     357             :   // Call non-in-place version, in-place-wise:
     358         102 :   corrupt(vb,vb.modelVisCube());
     359         102 : }
     360             : 
     361       69690 : void VisCal::corrupt2(vi::VisBuffer2& vb) {
     362             : 
     363       69690 :   if (prtlev()>3) cout << "VC::corrupt(vb2)" << endl;
     364             : 
     365             :   // If calibration is unavailable, just return!
     366       69690 :   if (!calAvailable(vb)) {
     367           0 :     return;
     368             :   }
     369             : 
     370             :   // Call non-in-place version, in-place-wise:
     371       69690 :   Cube<Complex> m;
     372       69690 :   m.reference(vb.visCubeModel());  // access model non-const
     373       69690 :   corrupt2(vb,m);
     374       69690 : }
     375             : 
     376        9064 : void VisCal::correct(VisBuffer& vb, Cube<Complex>& Vout, Bool trial) {
     377             : 
     378        9064 :   if (prtlev()>3) cout << " VC::correct(vb,Vout)" << endl;
     379             :   
     380             :   // Prepare output Cube<Complex> for its own in-place apply:
     381             :   //  (this is a no-op if referencing same storage)
     382        9064 :   Vout = vb.visCube();
     383             : 
     384             :   // Bring calibration up-to-date with the vb, 
     385             :   //   with inversion turned ON
     386        9064 :   syncCal(vb,true);
     387             : 
     388             :   // Call generic row-by-row apply, with inversion turned ON
     389        9064 :   applyCal(vb,Vout,trial);
     390             : 
     391        9064 : }
     392             : 
     393             : // void VisCal::corrupt(VisBuffer& vb, Cube<Complex>& Mout) {
     394         102 : void VisCal::corrupt(VisBuffer& vb, Cube<Complex>& Mout) {
     395             : 
     396         102 :   if (prtlev()>3) cout << " VC::corrupt()" << endl;
     397             : 
     398             : 
     399             :   // Ensure weight calibration off internally for corrupt
     400             :   //   (corruption doesn't re-scale the data!)
     401         102 :   Bool userCalWt=calWt();
     402         102 :   calWt()=false;
     403             : 
     404             :   // Prepare output Cube<Complex> for its own in-place apply:
     405             :   //  (this is a no-op if referencing same storage)
     406         102 :   Mout = vb.modelVisCube();
     407             : 
     408             :   // Bring calibration up-to-date with the vb, 
     409             :   //   with inversion turned OFF
     410         102 :   syncCal(vb,false);
     411             : 
     412             :   // Call generic row-by-row apply, with inversion turned OFF
     413         102 :   applyCal(vb,Mout);
     414             : 
     415             :   // Restore user's calWt()
     416         102 :   calWt()=userCalWt;
     417             : 
     418         102 : }
     419             : 
     420       69690 : void VisCal::corrupt2(vi::VisBuffer2& vb, Cube<Complex>& Mout) {
     421             : 
     422       69690 :   if (prtlev()>3) cout << " VC::corrupt2(VB2)" << endl;
     423             : 
     424             : 
     425             :   // Ensure weight calibration off internally for corrupt
     426             :   //   (corruption doesn't re-scale the data!)
     427       69690 :   Bool userCalWt=calWt();
     428       69690 :   calWt()=False;
     429             : 
     430             :   // Prepare output Cube<Complex> for its own in-place apply:
     431             :   //  (this is a no-op if referencing same storage)
     432       69690 :   Mout = vb.visCubeModel();
     433             : 
     434             :   // Bring calibration up-to-date with the vb, 
     435             :   //   with inversion turned OFF
     436       69690 :   syncCal2(vb,False);
     437             : 
     438             :   // Call generic row-by-row apply, with inversion turned OFF
     439       69690 :   Cube<Float> w0;  // place-holder
     440       69690 :   applyCal2(vb,Mout,w0);
     441             : 
     442             :   // Restore user's calWt()
     443       69690 :   calWt()=userCalWt;
     444             : 
     445       69690 : }
     446             : 
     447             : // VI2-related refactorings-----------
     448             : 
     449       19605 : void VisCal::setMeta(Int obs, Int scan, Double time,
     450             :                      Int spw, const Vector<Double>& freq,
     451             :                      Int fld) {
     452             : 
     453             :   // Call internal method
     454       19605 :   syncMeta(spw,time,fld,freq,freq.nelements()); 
     455       19605 :   currScan()=scan;
     456       19605 :   currObs()=obs;
     457             :   
     458       19605 : }
     459             : 
     460             : // Setup solvePar shape for a spw
     461           0 : void VisCal::sizeApplyParCurrSpw(Int nVisChan) {
     462             : 
     463             :   // Sizes the solvePar arrays for the currSpw()
     464             :   
     465           0 :   if (prtlev()>3) cout << " VC::sizeApplyParCurrSpw()" << endl;
     466             : 
     467             :   // Use nVisChan only for freqDepPar() types
     468           0 :   Int nChan = ( freqDepPar() ? nVisChan : 1);
     469             : 
     470             :   // Keep old way informed (needed?)
     471           0 :   nChanPar()=nChan;
     472             : 
     473             :   // Now, size the arrays:
     474             : 
     475           0 :   IPosition parsh(3,nPar(),nChan,nElem());  // multi-chan
     476           0 :   switch (parType()) {
     477           0 :   case VisCalEnum::COMPLEX: {
     478           0 :     currCPar().resize(parsh);
     479           0 :     currCPar()=Complex(1.0);
     480           0 :     break;
     481             :   }
     482           0 :   case VisCalEnum::REAL: {
     483           0 :     currRPar().resize(parsh);
     484           0 :     currRPar()=0.0;
     485           0 :     break;
     486             :   }
     487           0 :   default:
     488           0 :     throw(AipsError("Internal error(Calibrater Module): Unsupported parameter type "
     489           0 :                     "COMPLEXREAL found in VC::sizeApplyParCurrSpw()"));
     490             :   }
     491             :     
     492           0 :   currParOK().resize(parsh);
     493           0 :   currParOK()=True;
     494             : 
     495           0 : }
     496             : 
     497           0 : void VisCal::setDefApplyParCurrSpw(Bool sync, Bool doInv) {
     498             : 
     499             :   // TBD: generalize for type-dep def values, etc.
     500           0 :   switch (parType()) {
     501           0 :   case VisCalEnum::COMPLEX: {
     502           0 :     AlwaysAssert(currCPar().nelements()>0,AipsError);
     503           0 :     currCPar().set(1.0);
     504           0 :     break;
     505             :   }
     506           0 :   case VisCalEnum::REAL: {
     507           0 :     AlwaysAssert(currRPar().nelements()>0,AipsError);
     508           0 :     currRPar().set(0.0);
     509           0 :     break;
     510             :   }
     511           0 :   default:
     512           0 :     throw(AipsError("Internal error(Calibrater Module): Unsupported parameter type "
     513           0 :                     "COMPLEXREAL found in VC::setDefApplyParCurrSpw()"));
     514             :   }
     515             : 
     516           0 :   currParOK().set(True);
     517           0 :   validateP();
     518             :   
     519           0 :   if (sync)
     520           0 :     syncCal(doInv);
     521             : 
     522           0 : }
     523             : 
     524             : // Set parameters to specified values in the currSpw(),
     525             : //   and optionally sync matrices
     526           0 : void VisCal::setApplyParCurrSpw(const Cube<Complex> cpar,
     527             :                                 bool sync, bool doInv) {
     528             : 
     529           0 :   switch (parType()) {
     530           0 :   case VisCalEnum::COMPLEX: {
     531           0 :     AlwaysAssert(currCPar().nelements()>0,AipsError);
     532           0 :     AlwaysAssert(currCPar().shape()==cpar.shape(),AipsError);  // shapes must match
     533           0 :     currCPar()=cpar;
     534           0 :     break;
     535             :   }
     536           0 :   case VisCalEnum::REAL: {
     537           0 :     throw(AipsError("Cannot set real parameters with complext values!"));
     538             :     break;
     539             :   }
     540           0 :   default:
     541           0 :     throw(AipsError("Internal error(Calibrater Module): Unsupported parameter type "
     542           0 :                     "COMPLEXREAL found in VC::setDefApplyParCurrSpw()"));
     543             :   }
     544             : 
     545           0 :   currParOK().set(True);
     546           0 :   validateP();
     547             :   
     548             :   // Synchronize matrices
     549           0 :   if (sync)
     550           0 :     syncCal(doInv);
     551             : 
     552           0 : }
     553             : 
     554           0 : void VisCal::setApplyParCurrSpw(const Cube<float> rpar,
     555             :                                 bool sync, bool doInv) {
     556             : 
     557           0 :   switch (parType()) {
     558           0 :   case VisCalEnum::REAL: {
     559           0 :     AlwaysAssert(currRPar().nelements()>0,AipsError);
     560           0 :     AlwaysAssert(currRPar().shape()==rpar.shape(),AipsError);  // shapes must match
     561           0 :     currRPar()=rpar;
     562           0 :     break;
     563             :   }
     564           0 :   case VisCalEnum::COMPLEX: {
     565           0 :     throw(AipsError("Cannot set complex parameters with real values!"));
     566             :     break;
     567             :   }
     568           0 :   default:
     569           0 :     throw(AipsError("Internal error(Calibrater Module): Unsupported parameter type "
     570           0 :                     "COMPLEXREAL found in VC::setDefApplyParCurrSpw()"));
     571             :   }
     572             : 
     573           0 :   currParOK().set(True);
     574           0 :   validateP();
     575             :   
     576             :   // Synchronize matrices
     577           0 :   if (sync)
     578           0 :     syncCal(doInv);
     579             : 
     580           0 : }
     581             : 
     582             : 
     583         583 : void VisCal:: initCalFlagCount() {
     584         583 :   ndataIn_=nflagIn_=nflagOut_=0;
     585         583 : }
     586             : 
     587         105 : Record VisCal::actionRec() {
     588         105 :   Record cf;
     589         105 :   cf.define("type",typeName());
     590         105 :   if (isApplied()) {
     591         105 :     cf.define("ndata",ndataIn_);
     592         105 :     cf.define("nflagIn",nflagIn_);
     593         105 :     cf.define("nflagOut",nflagOut_);
     594             :   }
     595         105 :   return cf;
     596           0 : }
     597             : 
     598             : 
     599           0 : void VisCal::state() {
     600             : 
     601           0 :   cout << "===========================================================" << endl;
     602           0 :   if (prtlev()>3) cout << "VC::state():" << endl;
     603           0 :   cout << boolalpha;
     604           0 :   cout << " Type=" << typeName() << " Enum=" << type() << endl;
     605           0 :   cout << "  nSpw= " << nSpw() 
     606           0 :        << "; nAnt= " << nAnt()
     607           0 :        << "; nBln= " << nBln()
     608           0 :        << "; nPar= " << nPar() 
     609           0 :        << "; nElem= " << nElem()
     610           0 :        << "; nCalMat= " << nCalMat() << endl;
     611           0 :   cout << "  isApplied= " << isApplied()
     612           0 :        << ";  isSolvable= " << isSolvable() << endl;
     613           0 :   cout << "  freqDepPar= " << freqDepPar()
     614           0 :        << ";  freqDepMat= " << freqDepMat()
     615           0 :        << ";  timeDepMat= " << timeDepMat() << endl;
     616           0 :   cout << "  interval= " << interval() << endl;
     617           0 :   cout << "  currSpw= " << currSpw() << endl;
     618           0 :   cout << "  nChanPar= " << nChanPar()
     619           0 :        << "; nChanMat= " << nChanMat()
     620           0 :        << "; startChan= " << startChan() << endl;
     621           0 :   cout << "  lastTime= " << lastTime()
     622           0 :        << "; currTime= " << currTime()
     623           0 :        << "; refTime= " << refTime() << endl;
     624           0 :   cout << "  refFreq= " << refFreq() << endl;
     625           0 :   cout << "  currField=" << currField() << endl;
     626           0 :   cout << "  currIntent=" << currIntent() << endl;
     627           0 :   cout << "  PValid() = " << PValid() << endl;
     628           0 :   cout << "  currCPar().shape() = " << currCPar().shape()
     629           0 :        << " (" << currCPar().data() << ")" << endl;
     630           0 :   cout << "  currRPar().shape() = " << currRPar().shape()
     631           0 :        << " (" << currRPar().data() << ")" << endl;
     632           0 :   cout << "  currParOK().shape() = " << currParOK().shape()
     633           0 :        << " (" << currParOK().data() << ") "
     634           0 :        << " (ntrue=" << ntrue(currParOK()) << ")" << endl;
     635             : 
     636           0 :   cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
     637             : 
     638           0 : }
     639             : 
     640           0 : void VisCal::currMetaNote() {
     641             : 
     642             :   cout << "   ("
     643           0 :        << "time=" << MVTime(refTime()/C::day).string(MVTime::YMD,7)
     644           0 :        << " field=" << currField()
     645           0 :        << " spw=" << currSpw()
     646           0 :        << ")"
     647           0 :        << endl;
     648             : 
     649           0 : }
     650             : 
     651             : // VisCal PROTECTED:
     652             : 
     653             : 
     654        9064 : void VisCal::countInFlag(const VisBuffer& vb) {
     655        9064 :   Int ncorr=vb.nCorr();
     656        9064 :   ndataIn_+=(vb.flag().nelements()*ncorr);
     657        9064 :   nflagIn_+=(ntrue(vb.flag())*ncorr);
     658        9064 : }
     659      246703 : void VisCal::countInFlag2(const vi::VisBuffer2& vb) {
     660      246703 :   ndataIn_+=(vb.flagCube().nelements());
     661      246703 :   nflagIn_+=(ntrue(vb.flagCube()));
     662      246703 : }
     663             : 
     664        9064 : void VisCal::countOutFlag(const VisBuffer& vb){
     665        9064 :   nflagOut_+=ntrue(vb.flag())*vb.nCorr(); 
     666        9064 : }
     667             : 
     668      246703 : void VisCal::countOutFlag2(const vi::VisBuffer2& vb){
     669      246703 :   nflagOut_+=ntrue(vb.flagCube());
     670      246703 : }
     671             : 
     672        9200 : void VisCal::syncCal(const VisBuffer& vb,
     673             :                      const Bool& doInv) {
     674             :   
     675        9200 :   if (prtlev()>4) cout << "   VC::syncCal(vb)" << endl;
     676             : 
     677             :   // Set current meta date from the VisBuffer
     678        9200 :   syncMeta(vb);
     679             : 
     680             :   // Check the current calibration for validity
     681        9200 :   checkCurrCal();
     682             : 
     683             :   // Procede with generalized sync of calibration
     684        9200 :   syncCal(doInv);
     685             : 
     686        9200 : }
     687             : 
     688      316393 : void VisCal::syncCal2(const vi::VisBuffer2& vb,
     689             :                       const Bool& doInv) {
     690             :   
     691      316393 :   if (prtlev()>4) cout << "   VC::syncCal2(vb)" << endl;
     692             : 
     693             :   // Set current meta date from the VisBuffer
     694      316393 :   syncMeta2(vb);
     695             : 
     696             :   // Check the current calibration for validity
     697      316393 :   checkCurrCal();
     698             : 
     699             :   // Procede with generalized sync of calibration
     700      316393 :   syncCal(doInv);
     701             : 
     702             : 
     703      316393 : }
     704             : 
     705             : 
     706           0 : void VisCal::syncCal(VisCal& vc) {
     707             :   
     708           0 :   if (prtlev()>4) cout << "   VC::syncCal(vc)" << endl;
     709             : 
     710             :   //  cout << "    VisCal::syncCal(VisCal): " << currCPar().data() 
     711             :   //       << endl;
     712             : 
     713             :   // Set current meta date from the VisCal
     714           0 :   syncMeta(vc);
     715             : 
     716             :   // Check the current calibration for validity
     717           0 :   checkCurrCal();
     718             : 
     719             :   // Procede with generalized sync of calibration
     720           0 :   syncCal(false);
     721             : 
     722             :   //  cout << "    VisCal::syncCal(VisCal): " << currCPar().data() 
     723             :   //       << endl;
     724             : 
     725           0 : }
     726             : 
     727             : 
     728        9200 : void VisCal::syncMeta(const VisBuffer& vb) {
     729             :   
     730        9200 :   if (prtlev()>4) cout << "    VC::syncMeta(vb)" << endl;
     731             :  
     732        9200 :   syncMeta(vb.spectralWindow(),
     733        9200 :            vb.time()(0),
     734        9200 :            vb.fieldId(),
     735        9200 :            vb.frequency(),
     736        9200 :            vb.nChannel());
     737             : 
     738        9200 :   currObs()=vb.observationId()(0);
     739             : 
     740             : 
     741             :   // Ensure VisVector for data acces has correct form
     742        9200 :   Int ncorr(vb.corrType().nelements());
     743        9200 :   if (V().type() != ncorr)
     744          32 :     V().setType(visType(ncorr));
     745             : 
     746        9200 : }
     747             : 
     748      316393 : void VisCal::syncMeta2(const vi::VisBuffer2& vb) {
     749             :   
     750      316393 :   if (prtlev()>4) cout << "    VC::syncMeta2(vb)" << endl;
     751             :  
     752      316393 :   syncMeta(vb.spectralWindows()(0),
     753      316393 :            vb.time()(0),
     754      316393 :            vb.fieldId()(0),
     755      316393 :            vb.getFrequencies(0),
     756      316393 :            vb.nChannels());
     757             : 
     758      316393 :   currScan()=vb.scan()(0);
     759      316393 :   currObs()=vb.observationId()(0);
     760      316393 :   currIntent()=vb.stateId()(0);
     761             : 
     762             :   // Ensure VisVector for data acces has correct form
     763      316393 :   Int ncorr(vb.nCorrelations());
     764      316393 :   if (V().type() != ncorr)
     765         307 :     V().setType(visType(ncorr));
     766             : 
     767      316393 : }
     768             : 
     769           0 : void VisCal::syncMeta(VisCal& vc) {
     770             :   
     771           0 :   if (prtlev()>4) cout << "    VC::syncMeta(vc)" << endl;
     772             : 
     773             : 
     774           0 :   syncMeta(vc.currSpw(),
     775           0 :            vc.currTime(),
     776           0 :            vc.currField(),
     777           0 :            vc.currFreq(),
     778           0 :            vc.nChanMat());  // the number of channels to be applied to
     779             : 
     780           0 :   currObs()=vc.currObs();
     781             : 
     782             : 
     783             :   // TBD: Ensure nElem matches (here?)
     784             : 
     785           0 : }
     786             : 
     787      345198 : void VisCal::syncMeta(const Int& spw,
     788             :                       const Double& time,
     789             :                       const Int& field,
     790             :                       const Vector<Double>& freq,
     791             :                       const Int& nchan) {
     792             : 
     793      345198 :   if (prtlev()>4) cout << "     VC::syncMeta(...)" << endl;
     794             : 
     795             :   // Remember which spw this is---EVERYTHING below pivots on this
     796      345198 :   currSpw()=spw;
     797             : 
     798             :   // Remember other meta-data
     799      345198 :   currTime()=time;
     800      345198 :   currField()=field;
     801             : 
     802      345198 :   currFreq().resize(); 
     803      345198 :   currFreq()=freq;
     804      345198 :   currFreq()/=1.0e9;  // In GHz
     805             : 
     806             :   // Use center channel for now
     807      345198 :   refFreq()=currFreq()(nchan/2);
     808             : 
     809             :   // Ensure cal matrix channelization is correct vis-a-vis the target channelization
     810             :   //  (in solve context, this is handled differently, outside this method)
     811      345198 :   if (isApplied()) setCalChannelization(nchan);
     812             : 
     813      345198 :   if (prtlev()>5) cout << "      meta: t,fld,freq=" << time << field << freq << endl;
     814      345198 : }
     815             : 
     816      325593 : void VisCal::setCalChannelization(const Int& nChanDat) {
     817             : 
     818      325593 :   if (prtlev()>4) cout << "     VC::setCalChannelization()" << endl;
     819             : 
     820             :   // This method sets up cal Matrix channelization for the current 
     821             :   //  spw according to the channel count supplied by the caller.
     822             :   //  (Presumably the data channelization)
     823             : 
     824             :   // Ensure matrix channel axis length is correct
     825      325593 :   if (freqDepMat())
     826             :     
     827             :     // If parameters are channelized
     828       23150 :     if (freqDepPar())
     829             :       // follow parameter channelization
     830             :       //  (e.g., B)
     831             :       //      nChanMat() = nChanPar();  
     832       16617 :       nChanMat() = nChanDat;   // NEWCALTABLE
     833             :     else
     834             :       // cal is f(freq) for each data channel:
     835             :       //  (e.g., fringe-fitting)
     836        6533 :       nChanMat() = nChanDat;
     837             : 
     838             :   else {
     839             :     // Matrices are single channel
     840             :     //  (e.g., G)
     841      302443 :     nChanMat()=1;
     842             : 
     843             :     //    cout << "nChanPar() = " << nChanPar() << endl;
     844             : 
     845             :     // So must be parameters:
     846      302443 :     AlwaysAssert((!freqDepPar()),AipsError);
     847             :     // NCT    AlwaysAssert((nChanPar()==1),AipsError);
     848             : 
     849             :   }
     850             : 
     851      325593 : }
     852             : 
     853      325593 : void VisCal::checkCurrCal() {
     854             :   // Based on meta-data changes, determine mainly if
     855             :   //  new calibration PARAMETERS should be sought
     856             :   //  NB: A finding of "true" does not necessarily 
     857             :   //   mean new pars will be found by CalInterp (we are
     858             :   //   here only forcing it to try)
     859             :   //  NB: this method may also invalidateCalMat(), to
     860             :   //   trigger matrix reformation, independent of whether
     861             :   //   new pars are found ultimately found by CalInterp
     862             : 
     863      325593 :   if (prtlev()>4) cout << "    VC::checkCurrCal: " << endl;
     864             : 
     865             :   // We potentially need new calibration (for currSpw) IF...
     866             : 
     867             :   // ...timestamp has changed
     868      325593 :   if (currTime() != lastTime() ) {
     869      325593 :     lastTime()=currTime();
     870      325593 :     invalidateP();  // merely signals need to check (doesn't break reference)
     871             : 
     872             :     // If matrices are time-dependent within solution interval,
     873             :     //   a time change ALWAYS invalidates them
     874             :     //   (i.e., PValid=F may not yield _new_ parameters in
     875             :     //    calcPar, but this forces matrix re-sync anyway)
     876      325593 :     if (timeDepMat()) invalidateCalMat();
     877             : 
     878             :     //if (prtlev()>5 and timeDepMat()) cout << "      invalidateCalMat() " << endl;
     879             : 
     880             :   }
     881             : 
     882      325593 : }
     883             : 
     884      325593 : void VisCal::syncCal(const Bool& doInv) {
     885             : 
     886      325593 :   if (prtlev()>4) cout << "    VC::syncCal(doInv)" << endl;
     887             : 
     888             :   //  cout << "     VisCal::syncCal(doInv): " << currCPar().data() 
     889             :   //       << endl;
     890             : 
     891             :   // Synchronize the parameters
     892      325593 :   syncPar();
     893             : 
     894             :   //  cout << "     VisCal::syncCal(doInv): " << currCPar().data() 
     895             :   //       << endl;
     896             : 
     897      325593 :   if (!PValid()) 
     898             :     // TBD:  Improve this message (with some meta data)
     899           0 :     throw(AipsError("Could not find any calibration parameters."));
     900             : 
     901             :   // Synchronize the matrices
     902      325593 :   syncCalMat(doInv);
     903             : 
     904             :   //  cout << "     VisCal::syncCal(doInv): " << currCPar().data()
     905             :   //       << endl;
     906             : 
     907      325593 : }
     908             : 
     909             : 
     910             : 
     911      325593 : void VisCal::syncPar() {
     912             : 
     913      325593 :   if (prtlev()>5) cout << "     VC::syncPar()  (PValid()=" 
     914           0 :                        << PValid() << ")" << endl;
     915             : 
     916             :   // If we require new parameters, calculate them by some means (e.g., interpolation):
     917      325593 :   if (!PValid()) calcPar();
     918             :     
     919      325593 : }
     920             : 
     921           0 : void VisCal::calcPar() {
     922             : 
     923           0 :   if (prtlev()>6) cout << "      VC::calcPar()" << endl;
     924             : 
     925             :   // Ensure we have some parameters
     926           0 :   if (parType()==VisCalEnum::COMPLEX && (currCPar().shape()!=IPosition(3,nPar(),nChanPar(),nElem()) ||
     927           0 :                                 currParOK().shape()!=IPosition(3,nPar(),nChanPar(),nElem())) ) {
     928           0 :     cout << "currCPar()   = " << currCPar() << endl;
     929           0 :     cout << "currParOK() = " << currParOK() << endl;
     930           0 :     throw(AipsError("No (complex) parameters available!"));
     931             :   }
     932           0 :   else if (parType()==VisCalEnum::REAL && (currRPar().shape()!=IPosition(3,nPar(),nChanPar(),nElem()) ||
     933           0 :                                      currParOK().shape()!=IPosition(3,nPar(),nChanPar(),nElem())) ) {
     934           0 :     cout << "currRPar()   = " << currRPar() << endl;
     935           0 :     cout << "currParOK() = " << currParOK() << endl;
     936           0 :     throw(AipsError("No (real) parameters available!"));
     937             :   }
     938           0 :   else if (parType()==VisCalEnum::COMPLEXREAL)
     939           0 :     throw(AipsError("We can't handle combined Real and Complex parameters yet."));
     940             :   else
     941             :     // Parameters appear to be available, so signal validation
     942           0 :     validateP();
     943             :   
     944             :   // Force subsequent matrix calculation
     945             :   //  (Specializations will only do this if actual _new_ parameters
     946             :   //   have been found)
     947           0 :   invalidateCalMat();
     948             : 
     949           0 : }
     950             : 
     951             : 
     952             : // VisCal PRIVATE method implementations........................... 
     953             : 
     954         763 : void VisCal::initVisCal() {
     955             : 
     956         763 :   if (prtlev()>2) cout << " VC::initVisCal()" << endl;
     957             : 
     958             :   // Number of baselines (including ACs)
     959         763 :   nBln_ = nAnt_*(nAnt_+1)/2;
     960             : 
     961        9360 :   for (Int ispw=0;ispw<nSpw(); ispw++) {
     962        8597 :     currCPar_[ispw] = new Cube<Complex>();
     963        8597 :     currRPar_[ispw] = new Cube<Float>();
     964        8597 :     currParOK_[ispw] = new Cube<Bool>();
     965        8597 :     V_[ispw] = new VisVector(VisVector::Four);
     966        8597 :     currWtScale_[ispw] = new Cube<Float>();
     967             :   }
     968             : 
     969         763 : }
     970             : 
     971         763 : void VisCal::deleteVisCal() {
     972             : 
     973         763 :   if (prtlev()>2) cout << " VC::deleteVisCal()" << endl;
     974             : 
     975        9360 :   for (Int ispw=0; ispw<nSpw(); ispw++) {
     976        8597 :     if (currCPar_[ispw])     delete currCPar_[ispw];
     977        8597 :     if (currRPar_[ispw])     delete currRPar_[ispw];
     978        8597 :     if (currParOK_[ispw])    delete currParOK_[ispw];
     979        8597 :     if (V_[ispw]) delete V_[ispw];
     980        8597 :     if (currWtScale_[ispw]) delete currWtScale_[ispw];
     981             :   }
     982         763 :   currCPar_=NULL;
     983         763 :   currRPar_=NULL;
     984         763 :   currParOK_=NULL;
     985         763 :   V_=NULL;
     986         763 :   currWtScale_=NULL;
     987         763 : }
     988             : 
     989          18 : void VisCal::setCurrField(const Int& ifld) {
     990          18 :   currField_.set(ifld);
     991          18 : }
     992             : 
     993             : // **********************************************************
     994             : //  VisMueller Implementations
     995             : //
     996             : 
     997          73 : VisMueller::VisMueller(VisSet& vs) :
     998             :   VisCal(vs),
     999          73 :   M_(vs.numberSpw(),NULL),
    1000          73 :   currMElem_(vs.numberSpw(),NULL),
    1001          73 :   currMElemOK_(vs.numberSpw(),NULL),
    1002         146 :   MValid_(vs.numberSpw(),false)
    1003             : {
    1004             : 
    1005          73 :   if (prtlev()>2) cout << "VM::VM(vs)" << endl;
    1006             : 
    1007          73 :   initVisMueller();
    1008          73 : }
    1009             : 
    1010          27 : VisMueller::VisMueller(String msname,Int MSnAnt,Int MSnSpw) :
    1011             :   VisCal(msname,MSnAnt,MSnSpw),
    1012          27 :   M_(MSnSpw,NULL),
    1013          27 :   currMElem_(MSnSpw,NULL),
    1014          27 :   currMElemOK_(MSnSpw,NULL),
    1015          54 :   MValid_(MSnSpw,false)
    1016             : {
    1017             : 
    1018          27 :   if (prtlev()>2) cout << "VM::VM(msname,MSnAnt,MSnSpw)" << endl;
    1019             : 
    1020          27 :   initVisMueller();
    1021          27 : }
    1022             : 
    1023         609 : VisMueller::VisMueller(const MSMetaInfoForCal& msmc) :
    1024             :   VisCal(msmc),
    1025         609 :   M_(nSpw(),NULL),
    1026         609 :   currMElem_(nSpw(),NULL),
    1027         609 :   currMElemOK_(nSpw(),NULL),
    1028        1218 :   MValid_(nSpw(),False)
    1029             : {
    1030             : 
    1031         609 :   if (prtlev()>2) cout << "VM::VM(MSMetaInfoForCal)" << endl;
    1032             : 
    1033         609 :   initVisMueller();
    1034         609 : }
    1035             : 
    1036             : 
    1037           0 : VisMueller::VisMueller(const Int& nAnt) :
    1038             :   VisCal(nAnt),
    1039           0 :   M_(1,NULL),
    1040           0 :   currMElem_(1,NULL),
    1041           0 :   currMElemOK_(1,NULL),
    1042           0 :   MValid_(1,false)
    1043             : {
    1044           0 :   if (prtlev()>2) cout << "VM::VM(i,j,k)" << endl;
    1045             : 
    1046           0 :   initVisMueller();
    1047           0 : }
    1048             : 
    1049         709 : VisMueller::~VisMueller() {
    1050         709 :   if (prtlev()>2) cout << "VM::~VM()" << endl;
    1051             : 
    1052         709 :   deleteVisMueller();
    1053         709 : }
    1054             : 
    1055             : // Report the VisMueller portion of the state
    1056           0 : void VisMueller::state() {
    1057             : 
    1058             :   // Call parent
    1059           0 :   VisCal::state();
    1060             : 
    1061           0 :   if (applyByMueller()) {
    1062           0 :     if (prtlev()>3) cout << "VM::state()" << endl;
    1063           0 :     cout << boolalpha;
    1064             :     //    cout << "This is an baseline-based (Mueller) calibration." << endl;
    1065           0 :     cout << "  applyByMueller=" << applyByMueller() << endl;
    1066           0 :     cout << "  muellerType= " << muellerType() << endl;
    1067           0 :     cout << "  trivialMuellerElem= " << trivialMuellerElem() << endl;
    1068           0 :     cout << "  MValid() = " << MValid() << endl;
    1069             : 
    1070           0 :     cout << "  currMElem().shape() = " << currMElem().shape()
    1071           0 :          << " (" << currMElem().data() << ")" << endl;
    1072           0 :     cout << "  currMElemOK().shape() = " << currMElemOK().shape()
    1073           0 :          << " (" << currMElemOK().data() << ") "
    1074           0 :          << " (ntrue=" << ntrue(currMElemOK()) << ")" << endl;
    1075             : 
    1076             : 
    1077           0 :     cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
    1078             :   }
    1079           0 : }
    1080             : 
    1081             : // Apply this calibration to VisBuffer visibilities
    1082        4634 : void VisMueller::applyCal(VisBuffer& vb, Cube<Complex>& Vout,
    1083             :                           Bool trial) {
    1084             : 
    1085        4634 :   if (prtlev()>3) cout << "  VM::applyCal()" << endl;
    1086             : 
    1087             :   // CURRENTLY ASSUMES ONLY *ONE* TIMESTAMP IN VISBUFFER
    1088             :   /*
    1089             :   cout << "VM::applyCal: type= " << typeName() << "  trial = " 
    1090             :        << boolalpha << trial << " calWt = " << calWt() 
    1091             :        << "  freqDepPar() = " << freqDepPar()
    1092             :        << "  freqDepMat() = " << freqDepMat()
    1093             :        << endl;
    1094             :   */
    1095             : 
    1096             :   // Data info/indices
    1097             :   Int* dataChan;
    1098        4634 :   Bool* flagR=&vb.flagRow()(0);
    1099        4634 :   Bool* flag=&vb.flag()(0,0);
    1100        4634 :   Int* a1=&vb.antenna1()(0);
    1101        4634 :   Int* a2=&vb.antenna2()(0);
    1102        4634 :   Matrix<Float> wtmat;
    1103             : 
    1104             :   // Access to weights
    1105        4634 :   if (calWt() && !trial) 
    1106           0 :     wtmat.reference(vb.weightMat());
    1107             : 
    1108        4634 :   ArrayIterator<Float> wt(wtmat,1);
    1109        4634 :   Vector<Float> wtvec;
    1110             : 
    1111        4634 :   if (V().type()==VisVector::One) {
    1112           0 :     M().setScalarData(true);
    1113             :   }
    1114             :   else
    1115        4634 :     M().setScalarData(false);
    1116             : 
    1117             :   // iterate rows
    1118        4634 :   Int& nRow(vb.nRow());
    1119        4634 :   Int& nChanDat(vb.nChannel());
    1120             :   Int ibln;
    1121       19629 :   for (Int row=0; row<nRow; row++,flagR++,a1++,a2++) {
    1122             :     
    1123             :     // The basline number
    1124       14995 :     ibln=blnidx(*a1,*a2);
    1125             :     
    1126             :     // Solution channel registration
    1127       14995 :     Int solCh0(0);
    1128       14995 :     dataChan=&vb.channel()(0);
    1129             :     
    1130             :     // If cal _parameters_ are not freqDep (e.g., a delay)
    1131             :     //  the startChan() should be the same as the first data channel
    1132       14995 :     if (freqDepMat() && !freqDepPar())
    1133           0 :       startChan()=(*dataChan);
    1134             :     
    1135             :     // Solution and data array registration
    1136       14995 :     M().sync(currMElem()(0,solCh0,ibln),currMElemOK()(0,solCh0,ibln));
    1137       14995 :     if (!trial)
    1138       14995 :       V().sync(Vout(0,0,row));
    1139             :     
    1140             :     
    1141       53450 :     for (Int chn=0; chn<nChanDat; chn++,flag++,V()++,dataChan++) {
    1142             :       
    1143       38455 :       if (trial) 
    1144           0 :         M().applyFlag(*flag);
    1145             :       else 
    1146             :         // data and solution ok, do the apply
    1147       38455 :         M().apply(V(),*flag);
    1148             : 
    1149             :       // inc soln ch axis if freq-dependent 
    1150       38455 :       if (freqDepMat()) 
    1151       13975 :         M()++; 
    1152             : 
    1153             :     } // chn
    1154             : 
    1155             :     // If requested update the weights
    1156       14995 :     if (calWt() && !trial) {
    1157           0 :       wtvec.reference(wt.array());
    1158           0 :       updateWt(wtvec,*a1,*a2);
    1159             :     }
    1160             : 
    1161       14995 :     if (calWt() && !trial)
    1162           0 :       wt.next();
    1163             : 
    1164             :   }
    1165             : 
    1166        4634 : }
    1167             : 
    1168             : // Apply this calibration to VisBuffer visibilities
    1169         162 : void VisMueller::applyCal2(vi::VisBuffer2& vb, 
    1170             :                            Cube<Complex>& Vout, Cube<Float>& Wout,
    1171             :                            Bool trial) {
    1172             : 
    1173         162 :   if (prtlev()>3) cout << "  VM::applyCal()" << endl;
    1174             : 
    1175             :   // CURRENTLY ASSUMES ONLY *ONE* TIMESTAMP IN VISBUFFER
    1176             :   /*
    1177             :   cout << "VM::applyCal: type= " << typeName() << "  trial = " 
    1178             :        << boolalpha << trial << " calWt = " << calWt() 
    1179             :        << "  freqDepPar() = " << freqDepPar()
    1180             :        << "  freqDepMat() = " << freqDepMat()
    1181             :        << endl;
    1182             :   */
    1183             : 
    1184         162 :   Vector<Bool> flagRv(vb.flagRow());
    1185         162 :   Vector<Int>  a1v(vb.antenna1());
    1186         162 :   Vector<Int>  a2v(vb.antenna2());
    1187         162 :   Cube<Bool> flagCube(vb.flagCube());
    1188         162 :   Cube<Complex> visCube(Vout);
    1189         162 :   ArrayIterator<Float> wt(Wout,2);
    1190         162 :   Matrix<Float> wtmat;
    1191             : 
    1192             :   // Data info/indices
    1193             :   Int* dataChan;
    1194         162 :   Bool* flagR=&flagRv(0);
    1195         162 :   Int* a1=&a1v(0);
    1196         162 :   Int* a2=&a2v(0);
    1197             : 
    1198             :   // Ensure VisVector for data acces has correct form
    1199         162 :   Int ncorr(vb.nCorrelations());
    1200         162 :   if (V().type() != ncorr)
    1201           0 :     V().setType(visType(ncorr));
    1202             : 
    1203         162 :   if (V().type()==VisVector::One) {
    1204          60 :     M().setScalarData(true);
    1205             :   }
    1206             :   else
    1207         102 :     M().setScalarData(false);
    1208             : 
    1209             :   // iterate rows
    1210         162 :   Int nRow=vb.nRows();
    1211         162 :   Int nChanDat=vb.nChannels();
    1212             :   Int ibln;
    1213         162 :   Vector<Int> dataChanv(vb.getChannelNumbers(0));  // All rows have same chans
    1214        2442 :   for (Int row=0; row<nRow; row++,flagR++,a1++,a2++) {
    1215             :     
    1216             :     // The basline number
    1217        2280 :     ibln=blnidx(*a1,*a2);
    1218             :     
    1219             :     // Solution channel registration
    1220        2280 :     Int solCh0(0);
    1221        2280 :     dataChan=&dataChanv(0);
    1222             :     
    1223             :     // If cal _parameters_ are not freqDep (e.g., a delay)
    1224             :     //  the startChan() should be the same as the first data channel
    1225        2280 :     if (freqDepMat() && !freqDepPar())
    1226           0 :       startChan()=(*dataChan);
    1227             :     
    1228             :     // Solution and data array registration
    1229        2280 :     M().sync(currMElem()(0,solCh0,ibln),currMElemOK()(0,solCh0,ibln));
    1230        2280 :     V().sync(visCube(0,0,row),flagCube(0,0,row));
    1231             :     
    1232      188040 :     for (Int chn=0; chn<nChanDat; chn++,V()++,dataChan++) {
    1233             :       
    1234      185760 :       if (trial) 
    1235           0 :         M().flag(V());
    1236             :       else 
    1237             :         // data and solution ok, do the apply
    1238      185760 :         M().apply(V());
    1239             : 
    1240             :       // inc soln ch axis if freq-dependent 
    1241      185760 :       if (freqDepMat()) 
    1242           0 :         M()++; 
    1243             : 
    1244             :     } // chn
    1245             : 
    1246             :     // If requested update the weights
    1247             :     /*
    1248             :     if (calWt() && !trial) {
    1249             :       wtvec.reference(wt.array());
    1250             :       updateWt(wtvec,*a1,*a2);
    1251             :     }
    1252             : 
    1253             :     if (calWt() && !trial)
    1254             :       wt.next();
    1255             :     */
    1256             :   }
    1257             : 
    1258         162 : }
    1259             : 
    1260        4796 : void VisMueller::syncCalMat(const Bool& doInv) {
    1261             : 
    1262        4796 :   if (prtlev()>5) cout << "     VM::syncCalMat()" 
    1263           0 :                        << " (MValid()=" << MValid() << ")" << endl;
    1264             : 
    1265             :   // If Mueller matrices now invalid, re-sync them
    1266        4796 :   if (!MValid()) syncMueller(doInv);
    1267             : 
    1268        4796 : }
    1269             : 
    1270        4796 : void VisMueller::syncMueller(const Bool& doInv) {
    1271             : 
    1272             :   // RI
    1273             :   //prtlev()=10;
    1274             : 
    1275        4796 :   if (prtlev()>6) cout << "      VM::syncMueller()" << endl;
    1276             : 
    1277             :   // If Mueller pars are just the matrix elements:
    1278        4796 :   if (trivialMuellerElem()) {
    1279             :     // Matrix Elem cache references par cache
    1280         264 :     currMElem().reference(currCPar());
    1281         264 :     currMElemOK().reference(currParOK());
    1282             : 
    1283             :     // EXCEPT, ensure uniqueness if taking the inverse
    1284             :     //   (this makes a copy, can we avoid?)
    1285         264 :     if (doInv) currMElem().unique();
    1286             :   }
    1287             :   else {
    1288             :     // Make local matrix element cache the correct size:
    1289        4532 :     currMElem().resize(muellerNPar(this->muellerType()),nChanMat(),nCalMat());
    1290        4532 :     currMElem().unique();    // Ensure uniqueness!
    1291             : 
    1292             :     // OK is the shape of the M matrix itself
    1293        4532 :     currMElemOK().resize(muellerNPar(this->muellerType()),nChanMat(),nCalMat());
    1294        4532 :     currMElemOK().unique();
    1295        4532 :     currMElemOK()=false;
    1296             :     
    1297             :     // The matrix state is invalid until we actually calculate them
    1298        4532 :     invalidateM();
    1299             : 
    1300             :     // And calculate the matrix elements for all baselines
    1301        4532 :     calcAllMueller();
    1302             : 
    1303             :   }
    1304             : 
    1305             :   // weight calibration
    1306        4796 :   if (calWt()) syncWtScale();
    1307             : 
    1308             :   // Ensure Mueller matrix renderer is correct
    1309        4796 :   createMueller();
    1310             : 
    1311             :   // Invert, if requested
    1312        4796 :   if (doInv) invMueller();
    1313             : 
    1314             :   // Set matrix elements by their ok flags
    1315        4796 :   setMatByOk();
    1316             : 
    1317             :   // Mueller matrices now valid
    1318        4796 :   validateM();
    1319             : 
    1320        4796 : }
    1321             : 
    1322             : // Calculate Mueller matrices for all baselines
    1323           0 : void VisMueller::calcAllMueller() {
    1324             : 
    1325           0 :   if (prtlev()>6) cout << "       VM::calcAllMueller" << endl;
    1326             : 
    1327             :   // Should handle OK flags in this method, and only
    1328             :   //  do Mueller calc if OK
    1329             : 
    1330           0 :   Vector<Complex> oneMueller;
    1331           0 :   Vector<Bool> oneMOK;
    1332           0 :   Vector<Complex> onePar;
    1333           0 :   Vector<Bool> onePOK;
    1334             : 
    1335           0 :   ArrayIterator<Complex> Miter(currMElem(),1);
    1336           0 :   ArrayIterator<Bool>    MOKiter(currMElemOK(),1);
    1337           0 :   ArrayIterator<Complex> Piter(currCPar(),1);
    1338           0 :   ArrayIterator<Bool>    POKiter(currParOK(),1);
    1339             :   
    1340             :   // All required baselines
    1341           0 :   for (Int ibln=0; ibln<nCalMat(); ibln++) {
    1342             : 
    1343             :     // The following assumes that nChanPar()=1 or nChanMat()
    1344             : 
    1345           0 :     for (Int ich=0; ich<nChanMat(); ich++) {
    1346             : 
    1347           0 :       oneMueller.reference(Miter.array());
    1348           0 :       oneMOK.reference(MOKiter.array());
    1349           0 :       onePar.reference(Piter.array());
    1350           0 :       onePOK.reference(POKiter.array());
    1351             :       
    1352             :       // TBD  What if calcOneMueller needs freq value info?
    1353             :       
    1354           0 :       calcOneMueller(oneMueller,oneMOK,onePar,onePOK);
    1355             : 
    1356             :       // Advance iterators, as required
    1357           0 :       Miter.next();
    1358           0 :       MOKiter.next();
    1359           0 :       if (freqDepPar()) {
    1360           0 :         Piter.next();
    1361           0 :         POKiter.next();
    1362             :       }
    1363             : 
    1364             :     }
    1365             : 
    1366             :     // Step to next baseline's pars if we didn't in channel loop
    1367           0 :     if (!freqDepPar()) {
    1368           0 :       Piter.next();
    1369           0 :       POKiter.next();
    1370             :     }
    1371             :   }
    1372             : 
    1373           0 : }
    1374             : 
    1375           0 : void VisMueller::calcOneMueller(Vector<Complex>& /*mat*/, Vector<Bool>& /*mOk*/,
    1376             :                                 const Vector<Complex>& /*par*/, const Vector<Bool>& /*pOk*/) {
    1377             : 
    1378           0 :   if (prtlev()>10) cout << "        VM::calcOneMueller()" << endl;
    1379             : 
    1380             :   // If Mueller matrix is trivial, shouldn't get here
    1381           0 :   if (trivialMuellerElem()) 
    1382           0 :     throw(AipsError("Trivial Mueller Matrix logic error."));
    1383             : 
    1384             :   // Otherwise, this method apparently hasn't been specialized, as required
    1385             :   else
    1386           0 :     throw(AipsError("Unknown non-trivial Mueller-from-parameter calculation requested."));
    1387             : 
    1388             : }
    1389             : 
    1390        4694 : void VisMueller::invMueller() {
    1391             : 
    1392             :   // This method only called in apply context!
    1393        4694 :   AlwaysAssert((isApplied()),AipsError);
    1394             : 
    1395        4694 :   if (prtlev()>6) cout << "       VM::invMueller()" << endl;
    1396             : 
    1397        4694 :   M().sync(currMElem()(0,0,0),currMElemOK()(0,0,0));
    1398       22934 :   for (Int ibln=0;ibln<nCalMat();ibln++) 
    1399       36480 :     for (Int ichan=0; ichan<nChanMat(); ++ichan, M()++) 
    1400             :       // If matrix elements look ok so far, attempt to invert
    1401             :       //  (if invert fails, matrix is zeroed and meOk is set accordingly)
    1402       18240 :       M().invert();
    1403             : 
    1404        4694 : }
    1405             : 
    1406        4796 : void VisMueller::setMatByOk() {
    1407             : 
    1408             :   // This method only called in apply context!
    1409        4796 :   AlwaysAssert((isApplied()),AipsError);
    1410             : 
    1411        4796 :   if (prtlev()>6) 
    1412           0 :     cout << "       VM::setMatByOk()" << endl;
    1413             : 
    1414        4796 :   M().sync(currMElem()(0,0,0),currMElemOK()(0,0,0));
    1415       24566 :   for (Int ibln=0;ibln<nCalMat();ibln++) 
    1416       39540 :     for (Int ichan=0; ichan<nChanMat(); ++ichan, M()++) 
    1417             :       // If matrix elements look ok so far, attempt to invert
    1418             :       //  (if invert fails, matrix is zeroed and meOk is set accordingly)
    1419       19770 :       M().setMatByOk();
    1420             : 
    1421        4796 : }
    1422             : 
    1423        4796 : void VisMueller::createMueller() {
    1424             : 
    1425        4796 :   if (prtlev()>6) cout << "       VM::createMueller()" << endl;
    1426             : 
    1427             :   // Delete current Mueller if wrong type
    1428        4796 :   Mueller::MuellerType mtype(this->muellerType());
    1429        9592 :   if (M_[currSpw()] &&
    1430        4796 :       M().type() != mtype)
    1431           0 :     delete M_[currSpw()];
    1432             :   
    1433             :   // If needed, construct the correct Mueller
    1434        4796 :   if (!M_[currSpw()])
    1435          31 :     M_[currSpw()] = casa::createMueller(mtype);
    1436             : 
    1437             : 
    1438             :   // Nominal synchronization is with currMElem()(0,0,0);
    1439        4796 :   M().sync(currMElem()(0,0,0),currMElemOK()(0,0,0));
    1440             : 
    1441        4796 :   if (prtlev()>6) cout << "       currMElem()(0,0,0) = " << currMElem()(0,0,0) << endl;
    1442        4796 :   if (prtlev()>6) cout << "       currMElem()(0,0,1) = " << currMElem()(0,0,1) << endl;
    1443             : 
    1444             :       
    1445        4796 : }
    1446             : 
    1447           0 : void VisMueller::syncWtScale() {
    1448             : 
    1449             :   // Ensure proper size according to Mueller matrix type
    1450           0 :   switch (this->muellerType()) {
    1451           0 :   case Mueller::Scalar: 
    1452             :   case Mueller::Diagonal: {
    1453           0 :     Int nWtCorr=muellerNPar(muellerType());
    1454           0 :     currWtScale().resize(nWtCorr,1,nBln());
    1455           0 :     break;
    1456             :   }
    1457           0 :   default: {
    1458             :     // Only diag and scalar versions can adjust weights
    1459             :     //    cout<< "Turning off calWt()" << endl;
    1460           0 :     calWt()=false;
    1461           0 :     return;
    1462             :     break;
    1463             :   }
    1464             :   }
    1465             : 
    1466             :   // Calculate the weight scaling
    1467           0 :   calcWtScale();
    1468             : }
    1469             : 
    1470             : 
    1471           0 : void VisMueller::calcWtScale() {
    1472             : 
    1473             :   // Assumes currMElem hasn't yet been inverted
    1474             : 
    1475             :   // Insist on single channel operation
    1476           0 :   AlwaysAssert( (nChanMat()==1), AipsError );
    1477             : 
    1478           0 :   Cube<Float> cWS;
    1479           0 :   cWS.reference(currWtScale());
    1480             : 
    1481             :   // Simple pre-inversion square of currMElem
    1482           0 :   cWS=real(currMElem()*conj(currMElem()));
    1483           0 :   cWS(!currMElemOK())=1.0;
    1484             : 
    1485           0 : }
    1486             : 
    1487           0 : void VisMueller::updateWt(Vector<Float>& wt,const Int& a1,const Int& a2) {
    1488             : 
    1489             :   // Assumes single channel
    1490           0 :   Vector<Float> ws(currWtScale().xyPlane(blnidx(a1,a2)).column(0));
    1491             : 
    1492           0 :   switch (V().type()) {
    1493           0 :   case VisVector::One: {
    1494           0 :     wt(0)*=ws(0);
    1495             :   }
    1496           0 :   case VisVector::Two: {
    1497           0 :     for (uInt ip=0;ip<2;++ip) 
    1498           0 :       wt(ip)*=ws(ip*3);
    1499             :   }
    1500             :   default: {
    1501           0 :   for (uInt ip=0;ip<wt.nelements();++ip)
    1502           0 :     wt(ip)*=ws(ip);
    1503             :   }
    1504             :   }
    1505             : 
    1506           0 : }
    1507             : 
    1508             : 
    1509         709 : void VisMueller::initVisMueller() {
    1510             : 
    1511         709 :   if (prtlev()>2) cout << " VM::initVisMueller()" << endl;
    1512             : 
    1513        8547 :   for (Int ispw=0;ispw<nSpw(); ispw++) {
    1514        7838 :     currMElem_[ispw] = new Cube<Complex>();
    1515        7838 :     currMElemOK_[ispw] = new Cube<Bool>();
    1516             :   }
    1517         709 : }
    1518             : 
    1519         709 : void VisMueller::deleteVisMueller() {
    1520             : 
    1521         709 :   if (prtlev()>2) cout << " VM::deleteVisMueller()" << endl;
    1522             : 
    1523        8547 :   for (Int ispw=0; ispw<nSpw(); ispw++) {
    1524        7838 :     if (currMElem_[ispw])   delete currMElem_[ispw];
    1525        7838 :     if (currMElemOK_[ispw]) delete currMElemOK_[ispw];
    1526        7838 :     if (M_[ispw]) delete M_[ispw];
    1527             :   }
    1528         709 :   currMElem_=NULL;
    1529         709 :   currMElemOK_=NULL;
    1530         709 :   M_=NULL;
    1531         709 : }
    1532             : 
    1533             : 
    1534             : 
    1535             : 
    1536             : // **********************************************************
    1537             : //  VisJones Implementations
    1538             : //
    1539             : 
    1540          43 : VisJones::VisJones(VisSet& vs) :
    1541             :   VisCal(vs), VisMueller(vs),
    1542          43 :   J1_(vs.numberSpw(),NULL),
    1543          43 :   J2_(vs.numberSpw(),NULL),
    1544          43 :   currJElem_(vs.numberSpw(),NULL),
    1545          43 :   currJElemOK_(vs.numberSpw(),NULL),
    1546          86 :   JValid_(vs.numberSpw(),false)
    1547             : {
    1548          43 :   if (prtlev()>2) cout << "VJ::VJ(vs)" << endl;
    1549             : 
    1550          43 :   initVisJones();
    1551          43 : }
    1552             : 
    1553          27 : VisJones::VisJones(String msname,Int MSnAnt,Int MSnSpw) :
    1554             :   VisCal(msname,MSnAnt,MSnSpw), 
    1555             :   VisMueller(msname,MSnAnt,MSnSpw),
    1556          27 :   J1_(MSnSpw,NULL),
    1557          27 :   J2_(MSnSpw,NULL),
    1558          27 :   currJElem_(MSnSpw,NULL),
    1559          27 :   currJElemOK_(MSnSpw,NULL),
    1560          54 :   JValid_(MSnSpw,false)
    1561             : {
    1562          27 :   if (prtlev()>2) cout << "VJ::VJ(msname,MSnAnt,MSnSpw)" << endl;
    1563             : 
    1564          27 :   initVisJones();
    1565          27 : }
    1566             : 
    1567             : 
    1568         593 : VisJones::VisJones(const MSMetaInfoForCal& msmc) :
    1569             :   VisCal(msmc),
    1570             :   VisMueller(msmc),
    1571         593 :   J1_(nSpw(),NULL),
    1572         593 :   J2_(nSpw(),NULL),
    1573         593 :   currJElem_(nSpw(),NULL),
    1574         593 :   currJElemOK_(nSpw(),NULL),
    1575        1186 :   JValid_(nSpw(),False)
    1576             : {
    1577         593 :   if (prtlev()>2) cout << "VJ::VJ(MSMetaInfoForCal)" << endl;
    1578             : 
    1579         593 :   initVisJones();
    1580         593 : }
    1581             : 
    1582           0 : VisJones::VisJones(const Int& nAnt) :
    1583             :   VisCal(nAnt), 
    1584             :   VisMueller(nAnt),
    1585           0 :   J1_(1,NULL),
    1586           0 :   J2_(1,NULL),
    1587           0 :   currJElem_(1,NULL),
    1588           0 :   currJElemOK_(1,NULL),
    1589           0 :   JValid_(1,false)
    1590             : {
    1591           0 :   if (prtlev()>2) cout << "VJ::VJ(i,j,k)" << endl;
    1592             : 
    1593           0 :   initVisJones();
    1594           0 : }
    1595             : 
    1596         663 : VisJones::~VisJones() {
    1597         663 :   if (prtlev()>2) cout << "VJ::~VJ()" << endl;
    1598             : 
    1599         663 :   deleteVisJones();
    1600         663 : }
    1601             : 
    1602           0 : Mueller::MuellerType VisJones::muellerType() {
    1603             : 
    1604             :   // Ask Mueller to give us the answer:
    1605           0 :   return casa::muellerType(jonesType(),V().type());
    1606             :   
    1607             : }
    1608             : 
    1609           0 : void VisJones::state() {
    1610             : 
    1611             :   // Call parent
    1612           0 :   VisMueller::state();
    1613             : 
    1614           0 :   if (applyByJones()) {
    1615           0 :     if (prtlev()>3) cout << "VJ::state()" << endl;
    1616           0 :     cout << boolalpha;
    1617             :     //    cout << "This is an antenna-based (Jones) calibration." << endl;
    1618           0 :     cout << "  applyByJones=" << applyByJones() << endl;
    1619           0 :     cout << "  jonesType= " << jonesType() << endl;
    1620           0 :     cout << "  trivialJonesElem= " << trivialJonesElem() << endl;
    1621           0 :     cout << "  JValid() = " << JValid() << endl;
    1622             : 
    1623           0 :     cout << "  currJElem().shape() = " << currJElem().shape()
    1624           0 :          << " (" << currJElem().data() << ")" << endl;
    1625           0 :     cout << "  currJElemOK().shape() = " << currJElemOK().shape()
    1626           0 :          << " (" << currJElemOK().data() << ") "
    1627           0 :          << " (ntrue=" << ntrue(currJElemOK()) << ")" << endl;
    1628             : 
    1629           0 :     cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
    1630             :   }
    1631           0 : }
    1632             : 
    1633             : // VisJones: PROTECTED methods
    1634             : 
    1635             : 
    1636             : // Apply this calibration to VisBuffer visibilities
    1637        4532 : void VisJones::applyCal(VisBuffer& vb, Cube<Complex>& Vout,
    1638             :                         Bool trial) {
    1639             : 
    1640        4532 :   if (prtlev()>3) cout << "  VJ::applyCal()" << endl;
    1641             : 
    1642             :   // CURRENTLY ASSUMES ONLY *ONE* TIMESTAMP IN VISBUFFER
    1643             : 
    1644             :   /*
    1645             :   cout << "VJ::applyCal: type= " << typeName() << "  trial = " 
    1646             :        << boolalpha << trial << " calWt = " << calWt() 
    1647             :        << "  freqDepPar() = " << freqDepPar()
    1648             :        << "  freqDepMat() = " << freqDepMat()
    1649             :        << endl;
    1650             :   */
    1651             : 
    1652        4532 :   if (applyByMueller()) 
    1653           0 :     VisMueller::applyCal(vb,Vout);
    1654             :   else {
    1655             : 
    1656             :     // TBD: applyByJones()=true necessarily
    1657             : 
    1658             :     // Data info/indices
    1659             :     Int* dataChan;
    1660        4532 :     Bool* flagR=&vb.flagRow()(0);
    1661        4532 :     Bool* flag=&vb.flag()(0,0);
    1662        4532 :     Int* a1=&vb.antenna1()(0);
    1663        4532 :     Int* a2=&vb.antenna2()(0);
    1664        4532 :     Matrix<Float> wtmat;
    1665             : 
    1666             :     // Prepare to cal weights
    1667        4532 :     if (!trial)
    1668        4532 :       wtmat.reference(vb.weightMat());
    1669             :   
    1670        4532 :     ArrayIterator<Float> wt(wtmat,1);
    1671        4532 :     Vector<Float> wtvec;
    1672             : 
    1673             :     // Alert Jones matrices to whether data is scalar or not
    1674             :     //  (this is relevant only for proper handling of flags
    1675             :     //   in case of scalar data, for now)
    1676        4532 :     if (V().type()==VisVector::One) {
    1677           0 :       J1().setScalarData(true);
    1678           0 :       J2().setScalarData(true);
    1679             :     }
    1680             :     else {
    1681        4532 :       J1().setScalarData(false);
    1682        4532 :       J2().setScalarData(false);
    1683             :     }
    1684             : 
    1685             :     // iterate rows
    1686        4532 :     Int& nRow(vb.nRow());
    1687        4532 :     Int& nChanDat(vb.nChannel());
    1688             :     //    cout << currSpw() << " startChan() = " << startChan() << " nChanMat() = " << nChanMat() << " nChanDat="<<nChanDat <<endl;
    1689       18507 :     for (Int row=0; row<nRow; row++,flagR++,a1++,a2++) {
    1690             :       
    1691             :       // Solution channel registration
    1692       13975 :       Int solCh0(0);
    1693       13975 :       dataChan=&vb.channel()(0);
    1694             :       
    1695             :       // If cal _parameters_ are not freqDep (e.g., a delay)
    1696             :       //  the startChan() should be the same as the first data channel
    1697       13975 :       if (freqDepMat() && !freqDepPar())
    1698           0 :         startChan()=(*dataChan);
    1699             : 
    1700             :       // Solution and data array registration
    1701       13975 :       J1().sync(currJElem()(0,solCh0,*a1),currJElemOK()(0,solCh0,*a1));
    1702       13975 :       J2().sync(currJElem()(0,solCh0,*a2),currJElemOK()(0,solCh0,*a2));
    1703       13975 :       if (!trial)
    1704       13975 :         V().sync(Vout(0,0,row));
    1705             : 
    1706             : 
    1707       27950 :       for (Int chn=0; chn<nChanDat; chn++,flag++,V()++,dataChan++) {
    1708             :           
    1709       13975 :         if (trial) {
    1710             :           // only update flag info
    1711           0 :           J1().applyFlag(*flag);
    1712           0 :           J2().applyFlag(*flag);
    1713             :         }
    1714             :         else {
    1715             :           // if this data channel unflagged
    1716       13975 :           J1().applyRight(V(),*flag);
    1717       13975 :           J2().applyLeft(V(),*flag);
    1718             :         }
    1719             :           
    1720             :         // inc soln ch axis if freq-dependent (and next dataChan within soln)
    1721       13975 :         if (freqDepMat()) {
    1722       10825 :           J1()++; 
    1723       10825 :           J2()++; 
    1724             :         }
    1725             :           
    1726             :       } // chn
    1727             :         
    1728             : 
    1729             :       // If requested, update the weights
    1730       13975 :       if (!trial && calWt()) {
    1731           0 :         wtvec.reference(wt.array());
    1732           0 :         updateWt(wtvec,*a1,*a2);
    1733             :       }
    1734             : 
    1735       13975 :       if (!trial)
    1736       13975 :         wt.next();
    1737             : 
    1738             :     }
    1739             :     
    1740        4532 :   }
    1741             : 
    1742        4532 : }
    1743             : 
    1744             : // Apply this calibration to VisBuffer visibilities
    1745      311076 : void VisJones::applyCal2(vi::VisBuffer2& vb, 
    1746             :                          Cube<Complex>& Vout, Cube<Float>& Wout,
    1747             :                          Bool trial) {
    1748             : 
    1749      311076 :   if (prtlev()>3) cout << "  VJ::applyCal()" << endl;
    1750             : 
    1751             :   // CURRENTLY ASSUMES ONLY *ONE* TIMESTAMP IN VISBUFFER
    1752             : 
    1753             :   /*
    1754             :   cout << "VJ::applyCal: type= " << typeName() << "  trial = " 
    1755             :        << boolalpha << trial << " calWt = " << calWt() 
    1756             :        << "  freqDepPar() = " << freqDepPar()
    1757             :        << "  freqDepMat() = " << freqDepMat()
    1758             :        << endl;
    1759             :   */
    1760             : 
    1761      311076 :   if (applyByMueller()) 
    1762           0 :     throw(AipsError("applyByMueller call from VisJones::applyCal2 NYI."));
    1763             :   //    VisMueller::applyCal(vb,Vout);
    1764             :   else {
    1765             : 
    1766             :     // TBD: applyByJones()=true necessarily
    1767             : 
    1768             :     // References to VB2's contents' _data_
    1769      311076 :     Vector<Bool> flagRv(vb.flagRow());
    1770      311076 :     Vector<Int>  a1v(vb.antenna1());
    1771      311076 :     Vector<Int>  a2v(vb.antenna2());
    1772      311076 :     Cube<Bool> flagCube(vb.flagCube());
    1773      311076 :     Cube<Complex> visCube(Vout);
    1774      311076 :     ArrayIterator<Float> wt(Wout,2);
    1775      311076 :     Matrix<Float> wtmat;
    1776             : 
    1777             :     // Data info/indices
    1778             :     Int* dataChan;
    1779      311076 :     Bool* flagR=&flagRv(0);
    1780      311076 :     Int* a1=&a1v(0);
    1781      311076 :     Int* a2=&a2v(0);
    1782             : 
    1783             :     // Ensure VisVector for data acces has correct form
    1784      311076 :     Int ncorr(vb.nCorrelations());
    1785      311076 :     if (V().type() != ncorr)
    1786           0 :       V().setType(visType(ncorr));
    1787             : 
    1788             : 
    1789             :     // Alert Jones matrices to whether data is scalar or not
    1790             :     //  (this is relevant only for proper handling of flags
    1791             :     //   in case of scalar data, for now)
    1792      311076 :     if (V().type()==VisVector::One) {
    1793           0 :       J1().setScalarData(true);
    1794           0 :       J2().setScalarData(true);
    1795             :     }
    1796             :     else {
    1797      311076 :       J1().setScalarData(false);
    1798      311076 :       J2().setScalarData(false);
    1799             :     }
    1800             : 
    1801             :     // iterate rows
    1802      311076 :     Int nRow=vb.nRows();
    1803      311076 :     Int nChanDat=vb.nChannels();
    1804      311076 :     Vector<Int> dataChanv(vb.getChannelNumbers(0));  // All rows have same chans
    1805             :     //    cout << currSpw() << " startChan() = " << startChan() << " nChanMat() = " << nChanMat() << " nChanDat="<<nChanDat <<endl;
    1806    13921737 :     for (Int row=0; row<nRow; row++,flagR++,a1++,a2++) {
    1807             :       
    1808             :       // Solution channel registration
    1809    13610661 :       Int solCh0(0);
    1810    13610661 :       dataChan=&dataChanv(0);
    1811             :       
    1812             :       // If cal _parameters_ are not freqDep (e.g., a delay)
    1813             :       //  the startChan() should be the same as the first data channel
    1814    13610661 :       if (freqDepMat() && !freqDepPar())
    1815      106380 :         startChan()=(*dataChan);
    1816             : 
    1817             :       // Solution and data array registration
    1818    13610661 :       J1().sync(currJElem()(0,solCh0,*a1),currJElemOK()(0,solCh0,*a1));
    1819    13610661 :       J2().sync(currJElem()(0,solCh0,*a2),currJElemOK()(0,solCh0,*a2));
    1820    13610661 :       V().sync(visCube(0,0,row),flagCube(0,0,row));
    1821             : 
    1822   168496368 :       for (Int chn=0; chn<nChanDat; chn++,V()++,dataChan++) {
    1823             :           
    1824   154885707 :         if (trial) {
    1825             :           // only update flag info
    1826     7892640 :           J1().flagRight(V());
    1827     7892640 :           J2().flagLeft(V());
    1828             :         }
    1829             :         else {
    1830             :           // if this data channel unflagged
    1831   146993067 :           J1().applyRight(V());
    1832   146993067 :           J2().applyLeft(V());
    1833             :         }
    1834             :           
    1835             :         // inc soln ch axis if freq-dependent (and next dataChan within soln)
    1836   154885707 :         if (freqDepMat()) {
    1837    11275987 :           J1()++; 
    1838    11275987 :           J2()++; 
    1839             :         }
    1840             :           
    1841             :       } // chn
    1842             :         
    1843             : 
    1844             :       // If requested, update the weights
    1845    13610661 :       if (!trial && calWt()) {
    1846     8707021 :         wtmat.reference(wt.array());
    1847     8707021 :         updateWt2(wtmat,*a1,*a2);
    1848             :       }
    1849             : 
    1850    13610661 :       if (!trial)
    1851    12624081 :         wt.next();
    1852             : 
    1853             :     }
    1854             :     
    1855      311076 :   }
    1856             : 
    1857      311076 : }
    1858             : 
    1859             : 
    1860      645375 : void VisJones::syncCalMat(const Bool& doInv) {
    1861             : 
    1862      645375 :   if (prtlev()>5) cout << "     VJ::syncCalMat()"
    1863           0 :                        << " (JValid()=" << JValid() << ")" << endl;
    1864             : 
    1865             :   //  cout << "      VisCal::syncCalMat(doInv): " << currCPar().data() <<" "<< currJElem().data()
    1866             :   //       << endl;
    1867             : 
    1868             :   // If necessary, synchronize the Jones matrices
    1869      645375 :   if (!JValid()) syncJones(doInv);
    1870             : 
    1871             :   //  cout << "      VisCal::syncCalMat(doInv): " << currCPar().data() <<" "<< currJElem().data()
    1872             :   //       << endl;
    1873             : 
    1874             :   // Be sure sync'd matrices at their origin 
    1875      645375 :   J1().origin();
    1876      645375 :   J2().origin();
    1877             : 
    1878             :   // If requested and necessary, synchronize the Mueller matrices
    1879             :   //   (NEVER invert Muellers, as Jones already have been)
    1880      645375 :   if (applyByMueller() && !MValid()) syncMueller(false);
    1881             : 
    1882      645375 : }
    1883             : 
    1884             : 
    1885      186751 : void VisJones::syncJones(const Bool& doInv) {
    1886             : 
    1887      186751 :   if (prtlev()>6) cout << "      VJ::syncJones()" << endl;
    1888             : 
    1889             :   // If Jones pars are just the matrix elements:
    1890      186751 :   if (trivialJonesElem()) {
    1891             :     
    1892             :     // Matrix Elem cache references par cache
    1893      152094 :     currJElem().reference(currCPar());
    1894      152094 :     currJElemOK().reference(currParOK());
    1895             : 
    1896             :     // EXCEPT, ensure uniqueness if taking the inverse
    1897             :     //   (this makes a copy, can we avoid?)
    1898      152094 :     if (doInv) {
    1899             :       //cout << "  Unique: " << currJElem().data() << "-->";
    1900       71530 :       currJElem().unique();
    1901             :       //cout << currJElem().data() << endl;
    1902             :     }
    1903             :   }
    1904             :   else {
    1905             : 
    1906             :     // Make local matrix element cache the correct size:
    1907       34657 :     currJElem().resize(jonesNPar(jonesType()),nChanMat(),nAnt());
    1908       34657 :     currJElem().unique();    // Ensure uniqueness!
    1909             : 
    1910             :     // OK matches size of the J matrix itself
    1911       34657 :     currJElemOK().resize(jonesNPar(jonesType()),nChanMat(),nAnt());
    1912       34657 :     currJElem().unique();    // Ensure uniqueness!
    1913       34657 :     currJElem()=false;
    1914             : 
    1915             :     // The matrix state is invalid until we actually calculate them
    1916       34657 :     invalidateJ();
    1917             : 
    1918             :     // And calculate the matrix elements for all baselines
    1919       34657 :     calcAllJones();
    1920             : 
    1921             :   }
    1922             : 
    1923             :   /*
    1924             :   if (type()==VisCal::B) {
    1925             :     cout << typeName() << ": currJElem().shape() = " << currJElem().shape() << endl;
    1926             :     cout << "currJElem() = " << currJElem() << endl;
    1927             :     cout << "currJElemOK() = " << currJElemOK() << endl;
    1928             :     cout << "freqDepMat() = " << boolalpha << freqDepMat() << endl;
    1929             :   }
    1930             :   */
    1931             : 
    1932             :   // Pre-inv syncWtScale:
    1933      186751 :   if (calWt()) syncWtScale();
    1934             : 
    1935             :   // Ensure Jones Matrix renders are ok
    1936      186751 :   this->createJones();
    1937             : 
    1938             :   // Invert, if requested
    1939      186751 :   if (doInv) invJones();
    1940             : 
    1941             :   // Set matrix elements according to OK flags
    1942      186751 :   setMatByOk();
    1943             : 
    1944             :   // Jones matrices now valid
    1945      186751 :   validateJ();
    1946      186751 :   invalidateM();   // still invalid
    1947             : 
    1948      186751 : }
    1949             : 
    1950       28332 : void VisJones::calcAllJones() {
    1951             : 
    1952       28332 :   if (prtlev()>6) cout << "       VJ::calcAllJones()" << endl;
    1953             : 
    1954             :   // Should handle OK flags in this method, and only
    1955             :   //  do Jones calc if OK
    1956             : 
    1957       28332 :   Vector<Complex> oneJones;
    1958       28332 :   Vector<Bool> oneJOK;
    1959       28332 :   Vector<Complex> onePar;
    1960       28332 :   Vector<Bool> onePOK;
    1961             : 
    1962       28332 :   ArrayIterator<Complex> Jiter(currJElem(),1);
    1963       28332 :   ArrayIterator<Bool>    JOKiter(currJElemOK(),1);
    1964       28332 :   ArrayIterator<Complex> Piter(currCPar(),1);
    1965       28332 :   ArrayIterator<Bool>    POKiter(currParOK(),1);
    1966             :   
    1967      305772 :   for (Int iant=0; iant<nAnt(); iant++) {
    1968             : 
    1969             :     // The following assumes that nChanPar()=1 or nChanMat()
    1970             : 
    1971      554880 :     for (Int ich=0; ich<nChanMat(); ich++) {
    1972             :       
    1973      277440 :       oneJones.reference(Jiter.array());
    1974      277440 :       oneJOK.reference(JOKiter.array());
    1975      277440 :       onePar.reference(Piter.array());
    1976      277440 :       onePOK.reference(POKiter.array());
    1977             : 
    1978             :       // Calculate the Jones matrix
    1979      277440 :       calcOneJones(oneJones,oneJOK,onePar,onePOK);
    1980             :       
    1981             :       // Advance iterators
    1982      277440 :       Jiter.next();
    1983      277440 :       JOKiter.next();
    1984      277440 :       if (freqDepPar()) {
    1985           0 :         Piter.next();
    1986           0 :         POKiter.next();
    1987             :       }
    1988             : 
    1989             :     }
    1990             :     // Step to next antenns's pars if we didn't in channel loop
    1991      277440 :     if (!freqDepPar()) {
    1992      277440 :       Piter.next();
    1993      277440 :       POKiter.next();
    1994             :     }
    1995             :   }
    1996       28332 : }
    1997             : 
    1998           0 : void VisJones::calcOneJones(Vector<Complex>& /*mat*/, Vector<Bool>& /*mOk*/,
    1999             :                             const Vector<Complex>& /*par*/, const Vector<Bool>& /*pOk*/ ) {
    2000             : 
    2001           0 :   if (prtlev()>10) cout << "        VJ::calcOneJones()" << endl;
    2002             : 
    2003             :   // If Jones matrices are trivial, should never reach here!
    2004           0 :   if (trivialJonesElem())
    2005           0 :     throw(AipsError("Trivial Jones Matrix logic error."));
    2006             : 
    2007             :   // Otherwise, this method apparently hasn't been specialized, as required
    2008             :   else
    2009           0 :     throw(AipsError("Unknown non-trivial Jones-from-parameter calculation requested."));
    2010             : 
    2011             : }
    2012             : 
    2013       79466 : void VisJones::invJones() {
    2014             : 
    2015       79466 :   if (prtlev()>6) cout << "       VJ::invJones()" << endl;
    2016             : 
    2017       79466 :   J1().sync(currJElem()(0,0,0),currJElemOK()(0,0,0));
    2018      838675 :   for (Int iant=0;iant<nAnt();iant++) 
    2019     1547930 :     for (Int ichan=0; ichan<nChanMat();++ichan,J1()++) 
    2020             :       // If matrix elements look ok so far, attempt to invert
    2021             :       //  (if invert fails, currJElemOK will be set accordingly)
    2022      788721 :       J1().invert();
    2023             : 
    2024       79466 : }
    2025             : 
    2026      118891 : void VisJones::setMatByOk() {
    2027             : 
    2028      118891 :   if (prtlev()>6) 
    2029           0 :     cout << "       VJ::setMatByOk" << endl;
    2030             : 
    2031      118891 :   J1().sync(currJElem()(0,0,0),currJElemOK()(0,0,0));
    2032     1260989 :   for (Int iant=0;iant<nAnt();iant++) 
    2033     7203020 :     for (Int ichan=0; ichan<nChanMat();++ichan,J1()++) 
    2034             :       // If matrix elements look ok so far, attempt to invert
    2035             :       //  (if invert fails, currJElemOK will be set accordingly)
    2036     6060922 :       J1().setMatByOk();
    2037             : 
    2038      118891 : }
    2039             : 
    2040           0 : void VisJones::calcAllMueller() {
    2041             :   
    2042           0 :   if (prtlev()>6) cout << "       VJ::calcAllMueller()" << endl;
    2043             : 
    2044           0 :   M().sync(currMElem()(0,0,0));
    2045           0 :   for (Int a1=0;a1<nAnt();++a1) {
    2046           0 :     J1().sync(currJElem()(0,0,a1),currJElemOK()(0,0,a1));
    2047           0 :     for (Int a2=a1;a2<nAnt();++a2) {
    2048           0 :       J2().sync(currJElem()(0,0,a2),currJElemOK()(0,0,a2));
    2049           0 :       for (Int ich=0;ich<nChanMat();ich++,J1()++,J2()++,M()++)
    2050           0 :         M().fromJones(J1(),J2());
    2051             :     }
    2052             :   }
    2053             : 
    2054           0 : }
    2055             :  
    2056      186751 : void VisJones::createJones() {
    2057             : 
    2058      186751 :   if (prtlev()>6) cout << "       VJ::createJones()" << endl;
    2059             : 
    2060             :   // Delete current Jones if wrong type
    2061      186751 :   Jones::JonesType jtype(jonesType());
    2062             : 
    2063      373502 :   if (J1_[currSpw()] &&
    2064      186751 :       J1().type() != jtype)
    2065           0 :     delete J1_[currSpw()];
    2066             : 
    2067      373502 :   if (J2_[currSpw()] &&
    2068      186751 :       J2().type() != jtype)
    2069           0 :     delete J2_[currSpw()];
    2070             :   
    2071             :   // If needed, construct the correct Jones
    2072      186751 :   if (!J1_[currSpw()])
    2073         737 :     J1_[currSpw()] = casa::createJones(jtype);
    2074             : 
    2075      186751 :   if (!J2_[currSpw()])
    2076         737 :     J2_[currSpw()] = casa::createJones(jtype);
    2077             :       
    2078             : 
    2079             :   // Nominal synchronization is with currJElem()(0,0,0):
    2080      186751 :   J1().sync(currJElem()(0,0,0),currJElemOK()(0,0,0));
    2081      186751 :   J2().sync(currJElem()(0,0,0),currJElemOK()(0,0,0));
    2082             : 
    2083      186751 : }
    2084             : 
    2085       67786 : void VisJones::syncWtScale() {
    2086             : 
    2087             :   //  cout << "VJ::syncWtScale (" << typeName() << ")" << endl;
    2088             : 
    2089             : 
    2090             :   // Ensure proper size according to Jones matrix type
    2091       67786 :   switch (this->jonesType()) {
    2092       67786 :   case Jones::Scalar: 
    2093             :   case Jones::Diagonal: {
    2094       67786 :     Int nWtScale=jonesNPar(jonesType());
    2095       67786 :     currWtScale().resize(nWtScale,1,nAnt());
    2096       67786 :     break;
    2097             :   }
    2098           0 :   default: {
    2099             :     // Only diag and scalar versions can adjust weights
    2100             :     //    cout<< "Turning off calWt()" << endl;
    2101           0 :     calWt()=false;
    2102           0 :     return;
    2103             :     break;
    2104             :   }
    2105             :   }
    2106             : 
    2107             :    // Calculate the weight scale factors (specializable)
    2108       67786 :   calcWtScale();
    2109             : 
    2110             : 
    2111             :   //  cout << "VJ::syncWtScale: currWtScale() = " << currWtScale() << endl;
    2112             : 
    2113             : }
    2114             : 
    2115       67564 : void VisJones::calcWtScale() {
    2116             : 
    2117             :   // Assumes currJElem has not yet been inverted!
    2118             : 
    2119             :   // Insist on single channel operation
    2120       67564 :   AlwaysAssert( (nChanMat()==1), AipsError );
    2121             : 
    2122             :   // Just a reference
    2123       67564 :   Cube<Float> cWS(currWtScale());
    2124             : 
    2125             :   // We use simple (pre-inversion) square of currJElem
    2126       67564 :   cWS=real(currJElem()*conj(currJElem()));
    2127       67564 :   cWS(!currJElemOK())=1.0;
    2128             : 
    2129       67564 : }
    2130             : 
    2131           0 : void VisJones::updateWt(Vector<Float>& wt,const Int& a1,const Int& a2) {
    2132             : 
    2133           0 :   Vector<Float> ws1(currWtScale().xyPlane(a1).column(0));
    2134           0 :   Vector<Float> ws2(currWtScale().xyPlane(a2).column(0));
    2135             : 
    2136             : /*
    2137             :   if (a1==0 && a2==1) {
    2138             :     cout << "jonestype() = " << jonesType() 
    2139             :          << " jonesNPar(jonesType()) = " << jonesNPar(jonesType()) 
    2140             :          << " nPar() = " << nPar() 
    2141             :          << endl;
    2142             :     cout << currSpw() << " "
    2143             :          << a1 << " "
    2144             :          << a2 << " "
    2145             :          << wt << " "
    2146             :          << ws1 << " "
    2147             :          << ws2 << " "
    2148             :          << ws1(0/nPar()) << " "
    2149             :          << ws2(0%nPar()) << " ";
    2150             :   }
    2151             : */
    2152             : 
    2153           0 :   switch(jonesType()) {
    2154           0 :   case Jones::Scalar: {
    2155             :     // pol-indep corrections very simple; all correlations
    2156             :     //  corrected by same value
    2157           0 :     Float ws=(ws1(0)*ws2(0));
    2158           0 :     wt*=ws;
    2159           0 :     break;
    2160             :   }
    2161           0 :   case Jones::Diagonal: {
    2162           0 :     switch (V().type()) {
    2163           0 :     case VisVector::Two: {
    2164           0 :       for (uInt ip=0;ip<2;++ip) 
    2165           0 :         wt(ip)*=(ws1(ip)*ws2(ip));
    2166           0 :       break;
    2167             :     }
    2168           0 :     default: {
    2169             :       // NB: This always will apply the first weight scale info the a single corr
    2170           0 :       for (uInt ip=0;ip<wt.nelements();++ip) {
    2171           0 :         wt(ip)*=ws1(ip/2);
    2172           0 :         wt(ip)*=ws2(ip%2);
    2173             :       }
    2174           0 :       break;
    2175             :     }
    2176             :     }
    2177           0 :     break;
    2178             :   }
    2179           0 :   default:
    2180             :     // We don't calibrate weights for General Jones matrices (yet)
    2181           0 :     break;
    2182             :   }
    2183             : 
    2184             :   /*
    2185             :   if (a1==0 && a2==1)
    2186             :     cout << " ---> " << wt << endl;
    2187             :   */
    2188           0 : }
    2189             : 
    2190             : // WEIGHT_SPECTRUM-capable version
    2191     8707021 : void VisJones::updateWt2(Matrix<Float>& wt,const Int& a1,const Int& a2) {
    2192             : 
    2193     8707021 :   Int nVco=wt.shape()[0];  // ==nChanDat()?
    2194     8707021 :   Int nVch=wt.shape()[1];  // ==nCorrDat()?
    2195             : 
    2196     8707021 :   Matrix<Float> ws1(currWtScale().xyPlane(a1));
    2197     8707021 :   Matrix<Float> ws2(currWtScale().xyPlane(a2));
    2198             : 
    2199     8707021 :   Int nCch=ws1.shape()[1];  //  ==nChanMat()?
    2200             : 
    2201             :   // Channel counts either idential, or single-chan calibration
    2202             :   //  alwaysAssert( nVch==nCch || nCch==1, AipsError);
    2203             : 
    2204     8707021 :   switch(jonesType()) {
    2205     2302020 :   case Jones::Scalar: {
    2206             :     // pol-indep corrections very simple; all correlations
    2207             :     //  corrected by same value, per channel
    2208     4604040 :     for (Int ich=0;ich<nVch;++ich) {
    2209     2302020 :       Int iCch=ich%nCch;  // 0 or ich
    2210     2302020 :       Float ws=(ws1(0,iCch)*ws2(0,iCch));
    2211     2302020 :       Matrix<Float> wtm(wt(Slice(),Slice(ich,1,1)));
    2212     2302020 :       wtm*=ws;
    2213     2302020 :     }
    2214     2302020 :     break;
    2215             :   }
    2216     6405001 :   case Jones::Diagonal: {
    2217             : 
    2218     6405001 :     Int nCorrPerPol=max(nVco/2,1);  // >=1
    2219    38177444 :     for (Int ich=0;ich<nVch;++ich) {
    2220    31772443 :       Int iCch=ich%nCch;  // 0 or ich
    2221   114110373 :       for (Int ico=0;ico<nVco;++ico) 
    2222   164675860 :         wt(ico,ich) *= ( ws1(ico/nCorrPerPol,iCch) *
    2223    82337930 :                          ws2(ico%2,iCch) );
    2224             :     }
    2225     6405001 :     break;
    2226             :   }
    2227           0 :   default:
    2228             :     // We don't calibrate weights for General Jones matrices (yet)
    2229           0 :     break;
    2230             :   }
    2231             : 
    2232     8707021 : }
    2233             : 
    2234         663 : void VisJones::initVisJones() {
    2235             : 
    2236         663 :   if (prtlev()>2) cout << " VJ::initVisJones()" << endl;
    2237             : 
    2238        8319 :   for (Int ispw=0;ispw<nSpw(); ispw++) {
    2239        7656 :     currJElem_[ispw] = new Cube<Complex>();
    2240        7656 :     currJElemOK_[ispw] = new Cube<Bool>();
    2241             :   }
    2242         663 : }
    2243             : 
    2244         663 : void VisJones::deleteVisJones() {
    2245             :   
    2246         663 :   if (prtlev()>2) cout << " VJ::deleteVisJones()" << endl;
    2247             : 
    2248        8319 :   for (Int ispw=0; ispw<nSpw(); ispw++) {
    2249        7656 :     if (currJElem_[ispw])   delete currJElem_[ispw];   
    2250        7656 :     if (currJElemOK_[ispw]) delete currJElemOK_[ispw]; 
    2251        7656 :     if (J1_[ispw])          delete J1_[ispw];
    2252        7656 :     if (J2_[ispw])          delete J2_[ispw];
    2253             : 
    2254             :   }
    2255         663 :   currJElem_=NULL;
    2256         663 :   currJElemOK_=NULL;
    2257         663 :   J1_=NULL;
    2258         663 :   J2_=NULL;
    2259         663 : }
    2260             : 
    2261             : } //# NAMESPACE CASA - END
    2262             : 

Generated by: LCOV version 1.16