LCOV - code coverage report
Current view: top level - synthesis/MeasurementComponents - CalSolVi2Organizer.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 101 146 69.2 %
Date: 2024-12-11 20:54:31 Functions: 12 17 70.6 %

          Line data    Source code
       1             : //# CalSolVi2Organizer.cc: Definition of CalSolVi2Organizer
       2             : //# Copyright (C) 2016
       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 adressed 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             : //#
      27             : 
      28             : 
      29             : //#include <casacore/ms/MeasurementSets/MeasurementSet.h>
      30             : #include <synthesis/MeasurementComponents/CalSolVi2Organizer.h>
      31             : #include <msvis/MSVis/IteratingParameters.h>
      32             : #include <msvis/MSVis/LayeredVi2Factory.h>
      33             : #include <msvis/MSVis/SimpleSimVi2.h>
      34             : #include <msvis/MSVis/AveragingTvi2.h>
      35             : #include <synthesis/MeasurementComponents/FiltrationTVI.h>
      36             : #include <casacore/casa/aips.h>
      37             : #include <iostream>
      38             : 
      39             : using namespace casacore;
      40             : 
      41             : namespace casa { //# NAMESPACE CASA - BEGIN
      42             : 
      43             : using namespace vi;
      44             :   
      45             : // Constructor
      46         168 : CalSolVi2Organizer::CalSolVi2Organizer() : 
      47         168 :   data_(NULL),
      48         168 :   cal_(NULL),
      49         168 :   chanave_(NULL),
      50         168 :   timeave_(NULL),
      51         168 :   polave_(NULL),
      52         168 :   calfilter_(NULL),
      53         168 :   factories_(),
      54         168 :   vi_(NULL)
      55             : {
      56             :   // nothing else
      57         168 : }
      58             : 
      59             : // Destructor
      60         168 : CalSolVi2Organizer::~CalSolVi2Organizer()
      61             : {
      62         168 :   cleanUp();
      63         168 : }
      64             : 
      65         168 : vi::VisibilityIterator2& CalSolVi2Organizer::makeFullVI() {
      66             : 
      67         168 :   AlwaysAssert(factories_.nelements()>0, casacore::AipsError);
      68             : 
      69         168 :   if (vi_) delete vi_;
      70         168 :   vi_=new VisibilityIterator2(factories_);
      71             : 
      72         168 :   return *vi_;
      73             : }
      74             : 
      75           0 : int CalSolVi2Organizer::countSolutions(casacore::Vector<int>& nChunkPerSolve) {
      76             : 
      77             :   // TBD:  Make this smart w.r.t. combine options...
      78             : 
      79           0 :   AlwaysAssert(factories_.nelements()>0, AipsError);
      80             : 
      81             :   // A local (temporary) VI2 using just the bottom layer
      82             :   //  This assumes each solutions constains data
      83             :   //  from one or more chunk in the bottom layer
      84             :   //  (i.e., one chunk contains data for at most
      85             :   //   one distict solution)
      86             :   // Using this VI2 (rather than the full one) ensures
      87             :   //  that we do not invoke the averaging layers
      88             :   //  in non-trivial ways
      89             :   //  (NB:the loop below will need to vi.origin() to
      90             :   //   examine times and indices to handle 
      91             :   //   non-trivial solution-counting cases, e.g.,
      92             :   //   combine='spw', etc.)
      93           0 :   VisibilityIterator2 vi(factories_(Slice(0,1,1)));
      94             : 
      95             :   // Q: is MS sort init cost too high here?
      96             : 
      97           0 :   Int nchunks(0);
      98           0 :   for (vi.originChunks();vi.moreChunks();vi.nextChunk()) {
      99           0 :     ++nchunks;
     100             :   }
     101             :   //  not needed!
     102             :   //    for (vi.origin();vi.more();vi.next()) 
     103             : 
     104             :   // Trivial, for now
     105             :   //  Later, this will be filled in comb*-dependent manner
     106           0 :   nChunkPerSolve.resize(nchunks);
     107           0 :   nChunkPerSolve.set(1);
     108             : 
     109           0 :   return nchunks;
     110             : 
     111           0 : }
     112             : 
     113             : 
     114         168 : void CalSolVi2Organizer::addDiskIO(MeasurementSet* ms,Float interval,
     115             :                                    Bool combobs,Bool combscan,
     116             :                                    Bool combfld,Bool combspw,
     117             :                                    Bool useMSIter2,
     118             :                                    std::shared_ptr<FrequencySelections> freqSel) 
     119             : {
     120             : 
     121             :   //  Must be first specified layer
     122         168 :   AlwaysAssert(factories_.nelements()==0, AipsError);
     123             : 
     124             : 
     125         168 :   Block<Int> sc;
     126         168 :   deriveVI2Sort(sc,combobs,combscan,combfld,combspw);
     127         168 :   IteratingParameters iterpar(interval,SortColumns(sc));
     128             : 
     129         168 :   auto diskLayerFactory=new VisIterImpl2LayerFactory(ms,iterpar,True,useMSIter2);
     130             : 
     131         168 :   if(freqSel)
     132         168 :     diskLayerFactory->setFrequencySelections(freqSel);
     133             : 
     134         168 :   data_ = diskLayerFactory;
     135             : 
     136         168 :   factories_.resize(1);
     137         168 :   factories_[0]=data_;
     138         168 : }
     139             : 
     140           0 : void CalSolVi2Organizer::addSimIO() {
     141             : 
     142             :   //  Must be first specified layer
     143           0 :   AlwaysAssert(factories_.nelements()==0, AipsError);
     144             : 
     145           0 :   SimpleSimVi2Parameters ss;
     146           0 :   data_=new SimpleSimVi2LayerFactory(ss);
     147             : 
     148           0 :   factories_.resize(1);
     149           0 :   factories_[0]=data_;
     150           0 : }
     151             : 
     152           0 : void CalSolVi2Organizer::addSimIO(const SimpleSimVi2Parameters& ss) {
     153             : 
     154             :   //  Must be first specified layer
     155           0 :   AlwaysAssert(factories_.nelements()==0, AipsError);
     156             : 
     157           0 :   data_=new SimpleSimVi2LayerFactory(ss);
     158             : 
     159           0 :   factories_.resize(1);
     160           0 :   factories_[0]=data_;
     161           0 : }
     162             : 
     163           0 : void CalSolVi2Organizer::addCalForSolving(Float calfactor) {
     164             : 
     165             :   // Must not have added one already!
     166           0 :   AlwaysAssert(!cal_, AipsError);
     167             : 
     168             :   //  Must be at least one other layer already...
     169           0 :   AlwaysAssert(factories_.nelements()>0, AipsError);
     170             : 
     171           0 :   CalibratingParameters cp(calfactor);
     172             :   // Make the layer factory
     173           0 :   cal_= new vi::CalSolvingVi2LayerFactory(cp);
     174             : 
     175             :   // Add it to the list...
     176           0 :   this->appendFactory(cal_);
     177             : 
     178           0 : }
     179             : 
     180             : //
     181         168 : void CalSolVi2Organizer::addCalForSolving(VisEquation& ve, const Bool& corrDepFlags) {
     182             : 
     183             :   // Must not have added one already!
     184         168 :   AlwaysAssert(!cal_, AipsError);
     185             : 
     186             :   //  Must be at least one other layer already...
     187         168 :   AlwaysAssert(factories_.nelements()>0, AipsError);
     188             : 
     189             :   // Make the layer factory
     190         168 :   cal_= new CalSolvingVi2LayerFactoryByVE(&ve,corrDepFlags);
     191             : 
     192             :   // Add it to the list...
     193         168 :   this->appendFactory(cal_);
     194             : 
     195         168 : }
     196             : 
     197             : 
     198          92 : void CalSolVi2Organizer::addChanAve(Vector<Int> chanbin) {
     199             : 
     200             :   // Must not have added one already!
     201          92 :   AlwaysAssert(!chanave_, AipsError);
     202             : 
     203             :   //  Must be at least one other layer already...
     204          92 :   AlwaysAssert(factories_.nelements()>0, AipsError);
     205             : 
     206             :   // Force no averaging with chanbin[i]=0 in ChannelAverageTVI
     207             :   //  (NB: chanbin[i]=1 means nchan averaging)
     208          92 :   chanbin(chanbin==1)=0;
     209             : 
     210          92 :   Record config;
     211          92 :   config.define("chanbin",chanbin);
     212             : 
     213             :   // Make the layer factory
     214          92 :   chanave_= new ChannelAverageTVILayerFactory(config);
     215             : 
     216             :   // Add it to the list...
     217          92 :   this->appendFactory(chanave_);
     218             : 
     219          92 : }
     220             : 
     221             : // Add time-averaging layer factory
     222         130 : void CalSolVi2Organizer::addTimeAve(Float timebin) {
     223             : 
     224             :   // Must not have added one already!
     225         130 :   AlwaysAssert(!timeave_, AipsError);
     226             : 
     227             :   //  Must be at least one other layer already...
     228         130 :   AlwaysAssert(factories_.nelements()>0, AipsError);
     229             : 
     230             :   AveragingOptions aveopt(AveragingOptions::AverageCorrected|
     231             :                           AveragingOptions::CorrectedFlagWeightAvgFromWEIGHT|
     232             :                           AveragingOptions::AverageModel|
     233         130 :                           AveragingOptions::ModelPlainAvg);
     234         260 :   AveragingParameters avepar(timebin,0.0,SortColumns(),aveopt);
     235         130 :   timeave_ = new AveragingVi2LayerFactory(avepar);
     236             : 
     237             :   // Add it to the list...
     238         130 :   this->appendFactory(timeave_);
     239             : 
     240         130 : }
     241             : 
     242             : // Add data-filtering layer factory
     243           9 : void CalSolVi2Organizer::addCalFilter(Record const &config) {
     244             :   // Must not have added one already!
     245           9 :   AlwaysAssert(!timeave_, AipsError);
     246             : 
     247             :   //  Must be at least one other layer already...
     248           9 :   AlwaysAssert(factories_.nelements()>0, AipsError);
     249             : 
     250             :   // config must have "mode" field
     251           9 :   AlwaysAssert(config.isDefined("mode"), AipsError);
     252             : 
     253           9 :   Record configuration(config);
     254           9 :   String const mode = configuration.asString("mode");
     255           9 :   if (mode == "SDGAIN_OTFD") {
     256           9 :     configuration.define("type", (Int)FilteringType::SDDoubleCircleFilter);
     257             :   } else {
     258             :     // not supported
     259           0 :     configuration.define("type", (Int)FilteringType::NoTypeFilter);
     260             :   }
     261             : 
     262           9 :   calfilter_ = new FiltrationTVILayerFactory(configuration);
     263             : 
     264             :   // Add it to the list...
     265           9 :   this->appendFactory(calfilter_);
     266           9 : }
     267             : 
     268             : 
     269             : 
     270           1 : void CalSolVi2Organizer::addCorrCombine() {
     271             :   //  Must be at least one other layer already...
     272           1 :     AlwaysAssert(factories_.nelements()>0, AipsError);
     273             : 
     274           1 :   Record config;
     275           1 :   config.define("mode", "default");
     276           1 :   polave_= new PolAverageTVILayerFactory(config);
     277           1 :   this->appendFactory(polave_);
     278           1 : }
     279             : 
     280             : 
     281             : 
     282         400 : void CalSolVi2Organizer::appendFactory(ViiLayerFactory* f) {
     283             : 
     284         400 :   Int nf=factories_.nelements();
     285         400 :   factories_.resize(nf+1,True);  // copies exising values
     286         400 :   factories_[nf]=f;
     287             : 
     288         400 : }
     289             : 
     290             : 
     291         168 : void CalSolVi2Organizer::cleanUp() {
     292             : 
     293         168 :   if (vi_) delete vi_; vi_=NULL;
     294         168 :   if (data_) delete data_; data_=NULL;
     295         168 :   if (cal_) delete cal_;  cal_=NULL;
     296         168 :   if (chanave_) delete chanave_;  chanave_=NULL;
     297         168 :   if (timeave_) delete timeave_;  timeave_=NULL;
     298         168 :   if (polave_) delete polave_; polave_=NULL;
     299         168 :   if (calfilter_) delete calfilter_; calfilter_=NULL;
     300         168 :   factories_.resize(0);
     301             : 
     302         168 : }
     303             : 
     304           0 : void CalSolVi2Organizer::barf() {
     305             :   
     306           0 :   cout << "data_        = " << data_ << endl;
     307           0 :   cout << "cal_       = " << cal_ << endl;
     308           0 :   cout << "chanave_   = " << chanave_ << endl;
     309           0 :   cout << "timeave_   = " << timeave_ << endl;
     310           0 :   cout << "factories_ = " << factories_ << endl;
     311             : 
     312           0 :   if (vi_)
     313           0 :     cout << "VI Layers: " << vi_->ViiType() << endl;
     314             : 
     315           0 : }
     316             : 
     317         168 : void CalSolVi2Organizer::deriveVI2Sort(Block<Int>& sortcols, 
     318             :                                        Bool combobs,Bool combscan,
     319             :                                        Bool combfld,Bool combspw) 
     320             : {
     321             : 
     322             : /*  NB: interval is now just the prescribed averaging interval: min(solint,preavg)
     323             :      ---> no need to re-interpret it
     324             : 
     325             :   // Interpret solution interval for the VI2
     326             :   iterInterval=(max(interval(),DBL_MIN));
     327             :   if (interval() < 0.0) {   // means no interval (infinite solint)
     328             :     iterInterval=0.0;
     329             :     interval()=DBL_MAX;
     330             :   }
     331             : */  
     332         168 :   Bool verbose(False);
     333         168 :   if (verbose) {
     334             :     //cout << "   interval()=" << interval() ;
     335           0 :     cout << boolalpha << "; combobs =" << combobs;
     336           0 :     cout << boolalpha << "; combscan=" << combscan;
     337           0 :     cout << boolalpha << "; combfld =" << combfld ;
     338           0 :     cout << boolalpha << "; combspw =" << combspw ;
     339           0 :     cout << endl;
     340             :   }
     341             :   
     342         168 :   Int nsortcol(4+(combscan?0:1)+(combobs?0:1) );  // include room for scan,obs
     343         168 :   sortcols.resize(nsortcol);
     344         168 :   Int i(0);
     345         168 :   sortcols[i++]=MS::ARRAY_ID;
     346         168 :   if (!combobs) sortcols[i++]=MS::OBSERVATION_ID;  // force obsid boundaries
     347         168 :   if (!combscan) sortcols[i++]=MS::SCAN_NUMBER;  // force scan boundaries
     348         168 :   if (!combfld) sortcols[i++]=MS::FIELD_ID;      // force field boundaries
     349         168 :   if (!combspw) sortcols[i++]=MS::DATA_DESC_ID;  // force spw boundaries
     350         168 :   sortcols[i++]=MS::TIME;
     351         168 :   if (combfld) sortcols[i++]=MS::FIELD_ID;      // effectively ignore field boundaries
     352         168 :   if (combspw) sortcols[i++]=MS::DATA_DESC_ID;  // effectively ignore spw boundaries
     353             :   if (true || verbose) {
     354             :     //    cout << " sort sortcols: " << Vector<Int>(sortcols) << endl;
     355             :   }
     356             :   
     357         168 : }
     358             : 
     359             : 
     360             : 
     361             : 
     362             : 
     363             : } //# NAMESPACE CASA - END

Generated by: LCOV version 1.16