LCOV - code coverage report
Current view: top level - synthesis/TransformMachines - VisModelData.h (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 6 13 46.2 %
Date: 2024-11-06 17:42:47 Functions: 3 7 42.9 %

          Line data    Source code
       1             : //# VisModelData.h: Calculate Model Visibilities for a buffer from model images / complist
       2             : //# Copyright (C) 2011
       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             : //# $Id$
      28             : #ifndef VISMODELDATA_H
      29             : #define VISMODELDATA_H
      30             : #include <casacore/casa/aips.h>
      31             : #include <casacore/casa/Containers/Record.h>
      32             : #include <casacore/casa/Containers/Block.h>
      33             : #include <casacore/casa/Arrays/Cube.h>
      34             : #include <synthesis/TransformMachines/ComponentFTMachine.h>
      35             : #include <msvis/MSVis/VisModelDataI.h>
      36             : #include <casacore/casa/Arrays/ArrayFwd.h>
      37             : 
      38             : namespace casacore{
      39             : 
      40             :   class MeasurementSet;
      41             :   template <class T> class CountedPtr;
      42             : }
      43             : 
      44             : namespace casa { //# NAMESPACE CASA - BEGIN
      45             : //#forward
      46             :   class VisBuffer;
      47             :   namespace vi{ class VisBuffer2;}
      48             :   class ComponentList;  
      49             :   class FTMachine;
      50             : // <summary>
      51             : // Object to provide MODEL_DATA visibilities on demand
      52             : // </summary>
      53             : 
      54             : // <reviewed reviewer="" date="" tests="" demos="">
      55             : 
      56             : // <prerequisite>
      57             : //  FTMachine and ComponentFTMachine
      58             : //
      59             : // </prerequisite>
      60             : //
      61             : // <etymology>
      62             : // Vis for Visibility
      63             : // ModelData is self explanatory
      64             : // </etymology>
      65             : //
      66             : // <synopsis>
      67             : // This Class also offer a lot of helper functions to query add and delete/modify models
      68             : // To be noted the models that are supported are FTMachines and Componentlist
      69             : // A given FIELD_ID  can have multiple models added to the same record 
      70             : // (i.e for e.g multiple spw models or multiple outlier images  or in the 
      71             : // case of componentlist multiple component list models. 
      72             : // A model may be valid for multiple fields e.g a mosaic
      73             : // To save storage in such cases multiple field id will point to the same model
      74             : // Look for the keyword "definedmodel_field_x" in the Source table keywords (or the main 
      75             : // table if source is not present).
      76             : // This will point to another key which is going to point to the model.
      77             : // Thus multiple "definedmodel_field_x"  may point to the same key.
      78             : // If there is no valid SOURCE table the key will hold a casacore::Record of the model to use 
      79             : // in the main table 
      80             : // Otherwise in the SOURCE table the key will hold the row of the SOURCE table in whose 
      81             : // SOURCE_MODEL cell the casacore::Record of the model to use will be resident.
      82             : // Now a given model casacore::Record in a SOURCE_MODEL cell  
      83             : // can hold multiple FTMachine's or Compnentlist (e.g multiple direction images or 
      84             : // spw images  associated with a given field)  
      85             : // and they are all cumulative (while respecting spw selection conditions) 
      86             : // when a request is made for the model visibility
      87             : // </synopsis>
      88             : //
      89             : // <example>
      90             : // <srcblock>
      91             : //  casacore::MeasurementSet theMS(.....)
      92             : 
      93             : //   VisibilityIterator vi(theMS,sort);
      94             : //   VisBuffer vb(vi);
      95             : //    casacore::MDirection myDir=vi.msColumns().field().phaseDirMeas(0);
      96             : //    ComponentList cl;
      97             : //    SkyComponent otherPoint(ComponentType::POINT);
      98             : //    otherPoint.flux() = Flux<casacore::Double>(0.00001, 0.0, 0.0, 0.00000);
      99             : //    otherPoint.shape().setRefDirection(myDir);
     100             : //    cl.add(otherPoint);
     101             : //    casacore::Record clrec;
     102             : //     clrec.define("type", "componentlist");
     103             : //   ///Define the fields ans spw for which this model is valid
     104             : //    clrec.define("fields", field);
     105             : //   clrec.define("spws", casacore::Vector<casacore::Int>(1, 0));
     106             : //    clrec.defineRecord("container", container);
     107             : //    casacore::Record outRec;
     108             : //    outRec.define("numcl", 1);
     109             : //    outRec.defineRecord("cl_0", clrec);
     110             : 
     111             : //     casacore::Vector<casacore::Int>spws(1,0);
     112             : //     Save model to the MS
     113             : //      VisModelData.putModel(theMS, container, field, spws, casacore::Vector<casacore::Int>(1,0), casacore::Vector<casacore::Int>(1,63), casacore::Vector<casacore::Int>(1,1), true, false);
     114             : //
     115             : //////now example to serve model visibility
     116             : // vi.origin();
     117             : ///////////////
     118             : //VisModelData vm;
     119             : ////           casacore::String modelkey; 
     120             : //            casacore::Int snum;
     121             : //   casacore::Bool hasmodkey=VisModelData::isModelDefined(vb.fieldId(), vi.ms(), modelkey, snum);
     122             : // Setup vm to serve the model for the fieldid()
     123             : //   if( hasmodkey){
     124             : //                      casacore::TableRecord modrec;
     125             : //                       VisModelData::getModelRecord(modelkey, modrec, visIter_p->ms())
     126             : //                       vm.addModel(modrec, casacore::Vector<casacore::Int>(1, msId()), vb);
     127             : //                      }
     128             : //    for (vi.originChunks();vi.moreChunks(); vi.nextChunk()){
     129             : //      for (vi.origin(); vi.more(); vi++){
     130             : //       This fills the vb.modelVisCube with the appropriate model visibility
     131             : //        vm.getModelVis(vb);
     132             : //      cerr << "field " << vb.fieldId() << "  spw " << vb.spectralWindow() <<" max " <<  max(amplitude(vb.modelVisCube())) << endl;
     133             :  
     134             : //      }
     135             : //    }
     136             : // </srcblock>
     137             : // </example>
     138             : //
     139             : // <motivation>
     140             : // </motivation>
     141             : //
     142             : // <todo asof="2013/05/24">
     143             : // Allow validity of models for a given section of time only
     144             : //
     145             : // </todo>
     146             : 
     147             :   class VisModelData : public VisModelDataI {
     148             :  public:
     149             :   //empty constructor
     150             :   VisModelData();
     151             :   //From a FTMachine Record
     152             :   //VisModelData(const casacore::Record& ftmachinerec, const casacore::Vector<casacore::Int>& validfieldids, const casacore::Vector<casacore::Int>& msIds);
     153             :   virtual ~VisModelData();
     154             :   //Add Image/FTMachine to generate visibilities for
     155             :   //void addFTMachine(const casacore::Record& recordFTMachine, const casacore::Vector<casacore::Int>& validfieldids, const casacore::Vector<casacore::Int>& msIds);
     156             :   //Add componentlist to generate visibilities for
     157             :   //void addCompFTMachine(const ComponentList& cl, const casacore::Vector<casacore::Int>& validfieldids, 
     158             :   //                    const casacore::Vector<casacore::Int>& msIds);
     159             :   //For simple model a special case for speed 
     160             :   
     161             :   void addFlatModel(const casacore::Vector<casacore::Double>& value, const casacore::Vector<casacore::Int>& validfieldids, 
     162             :                     const casacore::Vector<casacore::Int>& msIds);
     163             : 
     164             :   //add componentlists or ftmachines 
     165             :   void addModel(const casacore::RecordInterface& rec,  const casacore::Vector<casacore::Int>& msids, const VisBuffer& vb);
     166             :   void addModel(const casacore::RecordInterface& rec,  const casacore::Vector<casacore::Int>& msids, const vi::VisBuffer2& vb);
     167             :   VisModelDataI * clone ();
     168             : 
     169             :   //put the model data for this VisBuffer in the modelVisCube
     170             :   casacore::Bool getModelVis(VisBuffer& vb);
     171             :   casacore::Bool getModelVis(vi::VisBuffer2& vb);
     172             : 
     173             :   //this is a helper function that writes the model record to the ms 
     174           0 :   void putModelI(const casacore::MeasurementSet& thems, const casacore::RecordInterface& rec,
     175             :                  const casacore::Vector<casacore::Int>& validfields, const casacore::Vector<casacore::Int>& spws,
     176             :                  const casacore::Vector<casacore::Int>& starts, const casacore::Vector<casacore::Int>& nchan,
     177             :                  const casacore::Vector<casacore::Int>& incr, casacore::Bool iscomponentlist=true, casacore::Bool incremental=false)
     178             :   {
     179           0 :     putModel (thems, rec, validfields, spws, starts, nchan, incr, iscomponentlist, incremental);
     180           0 :   }
     181             :   static void putModel(const casacore::MeasurementSet& thems, const casacore::RecordInterface& rec, const casacore::Vector<casacore::Int>& validfields, const casacore::Vector<casacore::Int>& spws, const casacore::Vector<casacore::Int>& starts, const casacore::Vector<casacore::Int>& nchan,  const casacore::Vector<casacore::Int>& incr, casacore::Bool iscomponentlist=true, casacore::Bool incremental=false);
     182             :   //Verion 2.0 of putModel to support Intent
     183             :   // index Comb is a Matrix of n rows 4 columns 
     184             :   //Each row is a unique combination of field_id, spw_id, scan_number, state_id for which this
     185             :   //record is valid
     186             :   //chansel is Matrix of nspw rows and 4 columns (spw_id, start, nchan, incr)
     187             :   static void putModel(const casacore::MeasurementSet& thems,const casacore::RecordInterface& rec, const casacore::Matrix<casacore::Int>& indexComb, const casacore::Matrix<casacore::Int>& chansel,
     188             :                                           casacore::Bool iscomponentlist=true, casacore::Bool incremental=false);
     189          42 :   void putModelI(const casacore::MeasurementSet& thems,const casacore::RecordInterface& rec, const casacore::Matrix<casacore::Int>& indexComb, const casacore::Matrix<casacore::Int>& chansel,
     190             :                                           casacore::Bool iscomponentlist=true, casacore::Bool incremental=false){
     191          42 :         putModel(thems, rec, indexComb, chansel, iscomponentlist, incremental);  
     192          42 :   };
     193             : 
     194             : 
     195             :   //helper function to clear the keywordSet of the ms of the model  for the fields 
     196             :   //in that ms
     197         133 :   void clearModelI(const casacore::MeasurementSet& thems) { clearModel (thems); }
     198             :   static void clearModel(const casacore::MeasurementSet& thems);
     199             :   // ...with field selection and optionally spw
     200             :   static void clearModel(const casacore::MeasurementSet& thems, const casacore::String field, const casacore::String spws=casacore::String(""));
     201             : 
     202             :   //Functions to see if model is defined in the casacore::MS either in the SOURCE table or else in the MAIN
     203        2919 :   casacore::Bool isModelDefinedI(const casacore::Int fieldId, const casacore::MeasurementSet& thems, casacore::String& key, casacore::Int& sourceRow)
     204             :   {
     205        2919 :     return isModelDefined (fieldId, thems, key, sourceRow);
     206             :   }
     207             :   static casacore::Bool isModelDefined(const casacore::Int fieldId, const casacore::MeasurementSet& thems, casacore::String& key, casacore::Int& sourceRow);
     208             :   static casacore::Bool isModelDefined(const casacore::String& elkey, const casacore::MeasurementSet& thems);
     209             : 
     210             :   //Get a given model that is defined by key
     211             :   //Forcing user to use a casacore::TableRecord rather than Generic casacore::RecordInterface ...just so as to avoid a copy.
     212           0 :   casacore::Bool getModelRecordI(const casacore::String& theKey, casacore::TableRecord& theRec, const casacore::MeasurementSet& theMs)
     213             :   {
     214           0 :     return getModelRecord (theKey, theRec, theMs);
     215             :   }
     216             :   static casacore::Bool getModelRecord(const casacore::String& theKey, casacore::TableRecord& theRec, const casacore::MeasurementSet& theMs);
     217             : 
     218             :   // casacore::List the fields
     219             :   static void listModel(const casacore::MeasurementSet& thems);
     220             : 
     221             :   static FTMachine* NEW_FT(const casacore::Record& ftrec);
     222             :   //check if an addFT or addCompFT is necessary
     223             :   //casacore::Bool hasFT(casacore::Int msid, casacore::Int fieldid);
     224             :   //casacore::Bool hasCL(casacore::Int msid, casacore::Int fieldid);
     225             :   //returns a -1 if there is no model for this combination of ms,field,spw...but has not been  // checked yet if there is one
     226             :   //returns a -2 if it has been tested before but does have it.
     227             :   //returns a 1 if it has a model stored 
     228             :   casacore::Int hasModel(casacore::Int msid, casacore::Int field, casacore::Int spw); 
     229           0 :   casacore::Bool isVersion2() {return (version_p==2);};
     230             :   ///init has to be called once at least to make sure isVersion2 to be correct
     231             :   void init(const VisBuffer& vb);
     232           0 :   void init(const vi::VisBuffer2& ){throw(casacore::AipsError("Called the wrong version of VisModelData"));};
     233             :   private:
     234             :   void initializeToVis();
     235             :   casacore::Vector<casacore::CountedPtr<ComponentList> >getCL(const casacore::Int msId, const casacore::Int fieldId, casacore::Int spw);
     236             :   casacore::Vector<casacore::CountedPtr<FTMachine> >getFT(const casacore::Int msId, const casacore::Int fieldId, casacore::Int spw);
     237             :   static casacore::Bool addToRec(casacore::TableRecord& therec, const casacore::Vector<casacore::Int>& spws);
     238             :   static casacore::Bool addToRec(casacore::TableRecord& therec, const casacore::Matrix<casacore::Int>& combindex);
     239             :   static casacore::Bool removeSpwFromMachineRec(casacore::RecordInterface& ftclrec, const casacore::Vector<casacore::Int>& spws);
     240             :   static casacore::Bool removeFTFromRec(casacore::TableRecord& therec, const casacore::String& keyval, const casacore::Bool relabel=true);
     241             :   static casacore::Bool removeSpw(casacore::TableRecord& therec, const casacore::Vector<casacore::Int>& spws, const casacore::Vector<casacore::Int>& fields=casacore::Vector<casacore::Int>(0));
     242             :   static casacore::Bool putModelRecord(const casacore::Vector<casacore::Int>& fieldIds, casacore::TableRecord& theRec, casacore::MeasurementSet& theMS);
     243             :   //Remove the casacore::Record which has the given key...will exterminate it from both the Source table or Main table
     244             :   static void removeRecordByKey(casacore::MeasurementSet& theMS, const casacore::String& theKey);
     245             :   //put the casacore::Record by key if sourcerownum=-1 then it is saved in the main table
     246             :   //this default should only be used  if the optional SOURCE table in non-existant
     247             :   static void putRecordByKey(casacore::MeasurementSet& theMS, const casacore::String& theKey, const casacore::TableRecord& theRec, const casacore::Int sourceRowNum=-1);
     248             :   static void deleteDiskImage(casacore::MeasurementSet& theMS, const casacore::String& theKey);
     249             :   static casacore::Int  firstSourceRowRecord(const casacore::Int field, const casacore::MeasurementSet& theMS, 
     250             :                                    casacore::TableRecord& rec);
     251             :   static void modifyDiskImagePath(casacore::Record& rec, const VisBuffer& vb);
     252             :   void getMatchingMachines(casacore::Vector<casacore::CountedPtr<FTMachine> >& ft, casacore::Vector<casacore::CountedPtr<ComponentList> >& cl, const VisBuffer& vb);
     253             :   void updateHolders(const VisBuffer& vb, const std::vector<casacore::Int>& index);
     254             :   //get all the combinations of field, spw, scan, state in this visbuffer
     255             :   void getUniqueIndicesComb(const VisBuffer& vb, casacore::Vector< casacore::Vector<casacore::Int> >& retval);
     256             :   casacore::Block<casacore::Vector<casacore::CountedPtr<ComponentList> > > clholder_p;
     257             :   casacore::Block<casacore::Vector<casacore::CountedPtr<FTMachine> > > ftholder_p;
     258             :   casacore::Block<casacore::Vector<casacore::Double> > flatholder_p;
     259             :   casacore::CountedPtr<ComponentFTMachine> cft_p;
     260             :   casacore::Cube<casacore::Int> ftindex_p;
     261             :   casacore::Cube<casacore::Int> clindex_p;
     262             :   //These 2 maps will hold the index of ftholder_p or clholder_p from key (field_id, spw_id, scan, state, msid)
     263             :   std::map<std::vector<casacore::Int>, casacore::Int > ftindex2_p;
     264             :   std::map<std::vector<casacore::Int>, casacore::Int > clindex2_p;
     265             :   casacore::Int version_p;
     266             :   static casacore::Bool initialize;
     267             : };
     268             : 
     269             : }//end namespace
     270             : #endif // VISMODELDATA_H

Generated by: LCOV version 1.16