00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef SYNTHESIS_SOLVABLEVISCAL_H
00029 #define SYNTHESIS_SOLVABLEVISCAL_H
00030
00031 #include <casa/aips.h>
00032 #include <casa/Containers/Record.h>
00033 #include <casa/BasicSL/Complex.h>
00034 #include <casa/BasicSL/Constants.h>
00035 #include <synthesis/MeasurementComponents/VisCal.h>
00036 #include <synthesis/MeasurementComponents/Mueller.h>
00037 #include <synthesis/MeasurementComponents/Jones.h>
00038 #include <synthesis/MeasurementComponents/VisVector.h>
00039 #include <synthesis/MeasurementComponents/SynthesisError.h>
00040 #include <calibration/CalTables/CalSet.h>
00041 #include <calibration/CalTables/CalSetMetaInfo.h>
00042 #include <calibration/CalTables/CalInterp.h>
00043 #include <calibration/CalTables/VisCalEnum.h>
00044 #include <msvis/MSVis/VisSet.h>
00045 #include <msvis/MSVis/CalVisBuffer.h>
00046 #include <msvis/MSVis/VisBuffGroupAcc.h>
00047
00048 #include <casa/Logging/LogMessage.h>
00049 #include <casa/Logging/LogSink.h>
00050 #include <casa/Logging/LogIO.h>
00051 #include <casa/OS/Timer.h>
00052
00053 namespace casa {
00054
00055
00056
00057
00058
00059
00060
00061 class VisEquation;
00062
00063
00064 class CalCorruptor {
00065
00066 public:
00067
00068 CalCorruptor(const Int nSim);
00069 virtual ~CalCorruptor();
00070 inline Int& nSim() { return nSim_; };
00071 inline Bool& initialized() { return initialized_; };
00072 inline Int& prtlev() { return prtlev_; };
00073 inline Int& curr_slot() { return curr_slot_; };
00074 inline Double& curr_time() { return curr_time_; };
00075 inline Double& startTime() { return starttime_; };
00076 inline Double& stopTime() { return stoptime_; };
00077 inline Double& slot_time(const Int i) { return slot_times_(i); };
00078 inline Double& slot_time() { return slot_times_(curr_slot()); };
00079 inline Int& currAnt() { return curr_ant_; };
00080 inline Int& currSpw() { return curr_spw_; };
00081 inline Int& nAnt() { return nAnt_; };
00082 inline Int& nSpw() { return nSpw_; };
00083 inline Int& currChan() { return curr_chan_; };
00084 inline Int& nChan() { return fnChan_[currSpw()]; };
00085 inline Vector<Float>& fRefFreq() { return fRefFreq_; };
00086 inline Vector<Float>& fWidth() { return fWidth_; };
00087 inline Vector<Int>& fnChan() { return fnChan_; };
00088 virtual void initialize()=0;
00089
00090 protected:
00091
00092 Int nSim_;
00093 Bool initialized_;
00094 Int prtlev_;
00095 Int curr_slot_;
00096 Int nAnt_,curr_ant_;
00097 Int nSpw_,curr_spw_,curr_chan_;
00098 Double curr_time_,starttime_,stoptime_;
00099 Vector<Double> slot_times_;
00100 Vector<Float> fRefFreq_,fWidth_;
00101 Vector<Int> fnChan_;
00102
00103 private:
00104
00105 };
00106
00107
00108
00109 class SolvableVisCal : virtual public VisCal {
00110 public:
00111
00112 SolvableVisCal(VisSet& vs);
00113
00114 SolvableVisCal(const Int& nAnt);
00115
00116 virtual ~SolvableVisCal();
00117
00118
00119 inline String& calTableName() { return calTableName_; };
00120 inline String& calTableSelect() { return calTableSelect_; };
00121 inline Bool& append() { return append_; };
00122 inline String& tInterpType() { return tInterpType_; };
00123 inline String& fInterpType() { return fInterpType_; };
00124 inline Vector<Int>& spwMap() { return spwMap_; };
00125 inline Int& refant() { return refant_; };
00126 inline Int& minblperant() { return minblperant_; };
00127 inline String& apmode() { return apmode_; };
00128 inline String& solint() { return solint_; };
00129 inline Double& preavg() { return preavg_; };
00130 inline Bool& solnorm() { return solnorm_;};
00131 inline Float& minSNR() { return minSNR_; };
00132
00133 inline String& combine() { return combine_; };
00134 inline Bool combspw() { return upcase(combine_).contains("SPW"); };
00135 inline Bool combfld() { return upcase(combine_).contains("FIELD"); };
00136 inline Bool combscan() { return upcase(combine_).contains("SCAN"); };
00137
00138
00139
00140 virtual Int nTotalPar()=0;
00141
00142
00143
00144 virtual Vector<Bool> spwOK() {
00145 return cint_ ? ci().spwOK() : Vector<Bool>(nSpw(),True); };
00146
00147
00148 virtual Bool standardSolve() { return True; };
00149
00150
00151
00152 virtual Int solvePol() { return 0; };
00153
00154
00155
00156 virtual Bool normalizable()=0;
00157
00158
00159 virtual Bool accumulatable() { return False; };
00160
00161
00162 virtual Bool smoothable() { return False; };
00163
00164
00165 inline Int& focusChan() { return focusChan_; };
00166
00167
00168 inline Bool isSolved() {return solved_;};
00169
00170
00171 virtual Bool isSolvable() {return True;};
00172
00173
00174 virtual void setApply();
00175 virtual void setApply(const Record& apply);
00176 virtual void setModel(const String& modelImage)
00177 {throw(SynthesisError("Internal error: setModel() not yet supported for non EPJones type."));};
00178
00179
00180 virtual String applyinfo();
00181
00182
00183 virtual void setSolve();
00184 virtual void setSolve(const Record& solve);
00185
00186
00187 virtual String solveinfo();
00188
00189
00190 virtual void setAccumulate(VisSet& vs,
00191 const String& table,
00192 const String& select,
00193 const Double& t,
00194 const Int& refAnt=-1);
00195
00196
00197 Int sizeUpSolve(VisSet& vs, Vector<Int>& nChunkPerSol);
00198
00199
00200 void initSolve(VisSet& vs);
00201
00202
00203 void inflate(VisSet& vs, const Bool& fillMeta=False);
00204
00205
00206 void inflate(const Vector<Int>& nChanDat,
00207 const Vector<Int>& startChanDat,
00208 const Vector<Int>& nSlot);
00209
00210
00211 virtual void guessPar(VisBuffer& vb)=0;
00212
00213
00214 inline virtual Cube<Complex>& solveCPar() {return (*solveCPar_[currSpw()]);};
00215 inline virtual Cube<Float>& solveRPar() {return (*solveRPar_[currSpw()]);};
00216 inline virtual Cube<Bool>& solveParOK() {return (*solveParOK_[currSpw()]);};
00217 inline virtual Cube<Float> & solveParErr() {return (*solveParErr_[currSpw()]);};
00218 inline virtual Cube<Float> & solveParSNR() {return (*solveParSNR_[currSpw()]);};
00219
00220
00221 inline Vector<Complex>& srcPolPar() { return srcPolPar_; };
00222
00223
00224
00225 Bool syncSolveMeta(VisBuffer& vb, const Int& fieldId);
00226 Bool syncSolveMeta(VisBuffGroupAcc& vbga);
00227
00228
00229 virtual void enforceAPonData(VisBuffer& vb);
00230
00231
00232 virtual Bool verifyConstraints(VisBuffGroupAcc& vbag);
00233 virtual Bool verifyForSolve(VisBuffer& vb);
00234
00235
00236 virtual void selfSolve(VisSet& vs, VisEquation& ve);
00237 virtual void selfSolve2(VisBuffGroupAcc& vs);
00238
00239
00240 void setUpForPolSolve(VisBuffer& vb);
00241
00242
00243 virtual void differentiate(CalVisBuffer& cvb)=0;
00244 virtual void differentiate(VisBuffer& vb,
00245 Cube<Complex>& V,
00246 Array<Complex>& dV,
00247 Matrix<Bool>& Vflg)=0;
00248 virtual void differentiate(VisBuffer& vb,
00249 VisBuffer& dV0 ,
00250 VisBuffer& dV1,
00251 Matrix<Bool>& Vflg){ throw(AipsError("Invalid use of differentiate(vb,dV0,dv1)")); };
00252
00253
00254
00255 virtual void diffSrc(VisBuffer& vb,
00256 Array<Complex>& dV)=0;
00257
00258
00259 virtual void updatePar(const Vector<Complex> dCalPar,const Vector<Complex> dSrcPar);
00260
00261
00262 virtual void formSolveSNR();
00263
00264
00265 virtual void applySNRThreshold();
00266
00267
00268 virtual void reReference()=0;
00269
00270
00271 virtual void accumulate(SolvableVisCal* incr,
00272 const Vector<Int>& fields)=0;
00273
00274 virtual void smooth(Vector<Int>& fields,
00275 const String& smtype,
00276 const Double& smtime);
00277
00278
00279 virtual void reportSolvedQU();
00280
00281
00282 virtual void keep(const Int& slot);
00283
00284
00285 virtual void globalPostSolveTinker();
00286
00287
00288 virtual void enforceAPonSoln();
00289
00290
00291 virtual void normalize();
00292
00293
00294 virtual void fluxscale(const Vector<Int>& refFieldIn,
00295 const Vector<Int>& tranFieldIn,
00296 const Vector<Int>& inRefSpwMap,
00297 const Vector<String>& fldNames,
00298 Matrix<Double>& fluxScaleFactor)=0;
00299
00300
00301 void store();
00302 void store(const String& tableName,const Bool& append);
00303
00304
00305 inline virtual void state() { stateSVC(True); };
00306 inline virtual Int nSlots(Int spw)
00307 {
00308 Int nslots=0;
00309 if (parType() == VisCalEnum::COMPLEX) nslots=cs().nTime(spw);
00310 else if (parType() == VisCalEnum::REAL) nslots=rcs().nTime(spw);
00311 return nslots;
00312 };
00313 virtual VisCalEnum::VCParType setParType(VisCalEnum::VCParType type)
00314 {parType_ = type;return (VisCalEnum::VCParType)parType_;};
00315 virtual void currMetaNote();
00316
00317 virtual void listCal(const Vector<Int> ufldids, const Vector<Int> uantids,
00318 const Matrix<Int> uchanids,
00319 const String& listfile="",const Int& pagerows=50)=0;
00320
00321
00322 inline void setChanMask(PtrBlock<Vector<Bool>*>& chanmask) { chanmask_=&chanmask; };
00323 inline void clearChanMask() { chanmask_=NULL; };
00324 void applyChanMask(VisBuffer& vb);
00325
00326 virtual void printActivity(const Int nSlots, const Int slotNo,
00327 const Int fieldId, const Int spw,
00328 const Int nSolutions);
00329 virtual void markTimer() {timer_p.mark();};
00330
00331
00332
00333
00334 virtual void setSimulate(const Record& simpar);
00335
00336
00337 virtual Int setupSim(VisSet& vs, const Record& simpar, Vector<Int>& nChunkPerSol, Vector<Double>& solTimes);
00338
00339
00340 virtual Bool simPar(VisBuffGroupAcc& vbga);
00341
00342
00343 inline String& simint() { return simint_; };
00344
00345
00346 virtual String siminfo();
00347
00348
00349 inline Bool isSimulated() {return simulated_;};
00350
00351
00352 CalCorruptor *corruptor_p;
00353
00354
00355 Int sizeUpSim(VisSet& vs, Vector<Int>& nChunkPerSol, Vector<Double>& solTimes);
00356
00357
00358
00359
00360 protected:
00361
00362
00363 inline void setSolved(const Bool& flag) {solved_=flag;};
00364
00365
00366 inline virtual CalSet<Complex>& cs() { return *cs_; };
00367 inline virtual CalSet<Float>& rcs() { return *rcs_; };
00368 inline virtual CalInterp& ci() { return *cint_; };
00369
00370
00371 virtual void initSolvePar()=0;
00372
00373
00374 inline virtual void invalidateDiffCalMat()=0;
00375
00376
00377 using VisCal::syncPar;
00378 void syncPar(const Int& spw, const Int& slot);
00379
00380
00381 void setSolveChannelization(VisSet& vs);
00382
00383
00384 void fillMetaData(VisSet& vs);
00385
00386
00387 void syncSolveCal();
00388
00389
00390 void syncSolvePar();
00391
00392
00393 virtual void calcPar();
00394
00395
00396 virtual void syncDiffMat()=0;
00397
00398
00399 virtual void stateSVC(const Bool& doVC);
00400
00401
00402 void normSolnArray(Array<Complex>& sol,
00403 const Array<Bool>& solOK,
00404 const Bool doPhase=False);
00405
00406
00407 LogIO& logSink() { return logsink_p; };
00408
00409 void makeCalSet();
00410
00411
00412 void verifyCalTable(const String& caltablename);
00413
00414 Int parType_;
00415
00416 CalSet<Complex> *cs_;
00417 CalSet<Float> *rcs_;
00418 CalInterp *cint_;
00419 CalSetMetaInfo csmi;
00420
00421 Double maxTimePerSolution_p, minTimePerSolution_p, avgTimePerSolution_p;
00422 Float userPrintActivityInterval_p, userPrintActivityFraction_p;
00423 uInt caiRC_p, cafRC_p;
00424 Timer timer_p;
00425
00426
00427 inline void setSimulated(const Bool& flag) {simulated_=flag;};
00428
00429
00430
00431 private:
00432
00433
00434 SolvableVisCal();
00435
00436
00437 void initSVC();
00438
00439
00440 void deleteSVC();
00441
00442
00443
00444 String calTableName_;
00445 String calTableSelect_;
00446 Bool append_;
00447
00448
00449 String tInterpType_;
00450 String fInterpType_;
00451
00452
00453 Vector<Int> spwMap_;
00454
00455
00456 Int refant_;
00457
00458
00459 Int minblperant_;
00460
00461
00462 Bool solved_;
00463
00464
00465 String apmode_;
00466
00467
00468 String solint_;
00469
00470
00471 Double preavg_;
00472
00473
00474 Bool solnorm_;
00475
00476
00477 Float minSNR_;
00478
00479
00480 String combine_;
00481
00482
00483 Int focusChan_;
00484
00485
00486 Double dataInterval_;
00487 Double fitWt_;
00488 Double fit_;
00489
00490
00491
00492 PtrBlock<Cube<Complex>*> solveCPar_;
00493 PtrBlock<Cube<Float>*> solveRPar_;
00494 PtrBlock<Cube<Bool>*> solveParOK_;
00495 PtrBlock<Cube<Float>*> solveParErr_;
00496 PtrBlock<Cube<Float>*> solveParSNR_;
00497
00498 Vector<Complex> srcPolPar_;
00499
00500
00501 PtrBlock<Vector<Bool>*> *chanmask_;
00502
00503
00504 LogIO logsink_p;
00505
00506
00507 Bool simulated_;
00508
00509
00510 String simint_;
00511
00512
00513 };
00514
00515
00516
00517
00518
00519
00520
00521 class SolvableVisMueller : public SolvableVisCal, virtual public VisMueller
00522 {
00523
00524 public:
00525
00526 SolvableVisMueller(VisSet& vs);
00527
00528 SolvableVisMueller(const Int& nAnt);
00529
00530 virtual ~SolvableVisMueller();
00531
00532
00533
00534 virtual Int nTotalPar() { return nPar(); };
00535
00536
00537 virtual Bool normalizable() { return (this->muellerType() < Mueller::General); };
00538
00539
00540 virtual void guessPar(VisBuffer& vb) { throw(AipsError("NYI")); };
00541
00542
00543 virtual void differentiate(CalVisBuffer& cvb) {throw(AipsError("NYI")); };
00544 virtual void differentiate(VisBuffer& vb,
00545 Cube<Complex>& V,
00546 Array<Complex>& dV,
00547 Matrix<Bool>& Vflg) { throw(AipsError("NYI")); };
00548 using SolvableVisCal::differentiate;
00549
00550
00551 virtual void diffSrc(VisBuffer& vb,
00552 Array<Complex>& dV) {throw(AipsError("NYI")); };
00553
00554
00555 virtual void reReference() {};
00556
00557
00558 virtual void accumulate(SolvableVisCal* incr,
00559 const Vector<Int>& fields) { throw(AipsError("NYI")); };
00560
00561
00562 virtual void fluxscale(const Vector<Int>& refFieldIn,
00563 const Vector<Int>& tranFieldIn,
00564 const Vector<Int>& inRefSpwMap,
00565 const Vector<String>& fldNames,
00566 Matrix<Double>& fluxScaleFactor) { throw(AipsError("NYI")); };
00567
00568
00569 inline virtual void state() { stateSVM(True); };
00570
00571
00572 virtual void listCal(const Vector<Int> ufldids,
00573 const Vector<Int> uantids,
00574 const Matrix<Int> uchanids,
00575 const String& listfile = "",
00576 const Int& pagerows = 50)
00577 { throw(AipsError(String("Calibration listing not supported for "+typeName()))); };
00578
00579 protected:
00580
00581
00582
00583 virtual Int nCalMat() { return isSolved() ? 1 : nBln(); };
00584
00585
00586 virtual Bool trivialDM() { return False; };
00587
00588
00589
00590 virtual void initSolvePar();
00591
00592
00593 inline Mueller& dM() { return *dM_; };
00594
00595
00596 inline Array<Complex>& diffMElem() {return diffMElem_;};
00597
00598
00599 inline virtual void invalidateDiffCalMat() { invalidateM(); invalidateDM(); };
00600
00601
00602 inline void invalidateDM() {DMValid_=False;};
00603 inline void validateDM() {DMValid_=True;};
00604 inline Bool DMValid() {return DMValid_;};
00605
00606
00607 virtual void syncDiffMat();
00608
00609
00610 virtual void syncDiffMueller();
00611
00612
00613 virtual void calcAllDiffMueller();
00614
00615
00616 virtual void calcOneDiffMueller(Matrix<Complex>& mat, const Vector<Complex>& par);
00617
00618
00619 void createDiffMueller();
00620
00621
00622 virtual void initTrivDM();
00623
00624
00625 virtual void stateSVM(const Bool& doVC);
00626
00627 private:
00628
00629
00630 SolvableVisMueller();
00631
00632
00633 Mueller *dM_;
00634
00635
00636 Array<Complex> diffMElem_;
00637
00638
00639 Bool DMValid_;
00640
00641
00642 };
00643
00644
00645
00646
00647
00648 class SolvableVisJones : public SolvableVisMueller, public VisJones {
00649
00650 public:
00651
00652 SolvableVisJones(VisSet& vs);
00653
00654 SolvableVisJones(const Int& nAnt);
00655
00656 virtual ~SolvableVisJones();
00657
00658
00659
00660 virtual Int nTotalPar() { return nPar()*nAnt(); };
00661
00662
00663 virtual Bool normalizable() { return (this->jonesType() < Jones::GenLinear); };
00664
00665
00666 virtual void differentiate(CalVisBuffer& cvb);
00667 virtual void differentiate(VisBuffer& vb,
00668 Cube<Complex>& V,
00669 Array<Complex>& dV,
00670 Matrix<Bool>& Vflg);
00671 using SolvableVisMueller::differentiate;
00672
00673
00674 virtual void diffSrc(VisBuffer& vb,
00675 Array<Complex>& dV);
00676
00677
00678 virtual void reReference();
00679
00680
00681 virtual void accumulate(SolvableVisCal* incr,
00682 const Vector<Int>& fields);
00683
00684
00685 virtual void globalPostSolveTinker();
00686
00687
00688 virtual void applyRefAnt();
00689
00690
00691 void fluxscale(const Vector<Int>& refFieldIn,
00692 const Vector<Int>& tranFieldIn,
00693 const Vector<Int>& inRefSpwMap,
00694 const Vector<String>& fldNames,
00695 Matrix<Double>& fluxScaleFactor);
00696
00697
00698 inline virtual void state() { stateSVJ(True); };
00699
00700
00701 virtual void listCal(const Vector<Int> ufldids, const Vector<Int> uantids,
00702 const Matrix<Int> uchanids,
00703 const String& listfile="",const Int& pagerows=50);
00704
00705
00706 int writeHeader(const uInt numAntCols,
00707 const uInt numAnts,
00708 const uInt iElem);
00709
00710 protected:
00711
00712
00713
00714 virtual Int nCalMat() { return nAnt(); };
00715
00716
00717 virtual Bool trivialDM() { return False; };
00718
00719
00720 inline virtual Bool trivialDJ() { return False; };
00721
00722
00723
00724 virtual void initSolvePar();
00725
00726
00727 inline Jones& dJ1() { return *dJ1_; };
00728 inline Jones& dJ2() { return *dJ2_; };
00729
00730
00731 Array<Complex>& diffJElem() { return diffJElem_; };
00732
00733
00734 inline virtual void invalidateDiffCalMat() {
00735 SolvableVisMueller::invalidateDiffCalMat(); invalidateJ(); invalidateDJ(); };
00736
00737
00738 inline void invalidateDJ() {DJValid_=False;};
00739 inline void validateDJ() {DJValid_=True;};
00740 inline Bool DJValid() {return DJValid_;};
00741
00742
00743 virtual void syncDiffMat();
00744
00745
00746 virtual void syncDiffJones();
00747
00748
00749 virtual void calcAllDiffJones();
00750
00751
00752 virtual void calcOneDiffJones(Matrix<Complex>& mat, const Vector<Complex>& par);
00753
00754
00755 void createDiffJones();
00756
00757
00758 virtual void initTrivDJ();
00759
00760 virtual void stateSVJ(const Bool& doVC);
00761 private:
00762
00763
00764 SolvableVisJones();
00765
00766
00767 Jones *dJ1_;
00768 Jones *dJ2_;
00769
00770
00771 Array<Complex> diffJElem_;
00772
00773
00774 Bool DJValid_;
00775
00776
00777 uInt wTime_p, wField_p, wChan_p, wAmp_p,
00778 wPhase_p, wFlag_p, wPol_p, wAntCol_p,
00779 wTotal_p, wPreAnt_p;
00780
00781 };
00782
00783
00784
00785
00786 String calTableType(const String& tablename);
00787
00788
00789
00790
00791
00792 }
00793
00794 #endif