00001 //# VisModelData.h: Calculate Model Visibilities for a buffer from model images / complist 00002 //# Copyright (C) 2011 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be adressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# 00027 //# $Id$ 00028 #ifndef TRANSFORM2_TRANSFORM2_VISMODELDATA_H 00029 #define TRANSFORM2_TRANSFORM2_VISMODELDATA_H 00030 #include <casa/aips.h> 00031 #include <casa/Containers/Record.h> 00032 #include <casa/Containers/Block.h> 00033 #include <casa/Arrays/Cube.h> 00034 #include <synthesis/TransformMachines2/ComponentFTMachine.h> 00035 #include <msvis/MSVis/VisModelDataI.h> 00036 #include <msvis/MSVis/VisBuffer.h> //here only for the pure virtual function that uses this 00037 00038 namespace casa { //# NAMESPACE CASA - BEGIN 00039 //#forward 00040 namespace vi{class VisBuffer2;} 00041 class ComponentList; 00042 class MeasurementSet; 00043 template <class T> class Vector; 00044 template <class T> class CountedPtr; 00045 namespace refim{ //namespace refim 00046 class FTMachine; 00047 // <summary> 00048 // Object to provide MODEL_DATA visibilities on demand 00049 // </summary> 00050 00051 // <reviewed reviewer="" date="" tests="" demos=""> 00052 00053 // <prerequisite> 00054 // FTMachine and ComponentFTMachine 00055 // 00056 // </prerequisite> 00057 // 00058 // <etymology> 00059 // Vis for Visibility 00060 // ModelData is self explanatory 00061 // </etymology> 00062 // 00063 // <synopsis> 00064 // This Class also offer a lot of helper functions to query add and delete/modify models 00065 // To be noted the models that are supported are FTMachines and Componentlist 00066 // A given FIELD_ID can have multiple models added to the same record 00067 // (i.e for e.g multiple spw models or multiple outlier images or in the 00068 // case of componentlist multiple component list models. 00069 // A model may be valid for multiple fields e.g a mosaic 00070 // To save storage in such cases multiple field id will point to the same model 00071 // Look for the keyword "definedmodel_field_x" in the Source table keywords (or the main 00072 // table if source is not present). 00073 // This will point to another key which is going to point to the model. 00074 // Thus multiple "definedmodel_field_x" may point to the same key. 00075 // If there is no valid SOURCE table the key will hold a Record of the model to use 00076 // in the main table 00077 // Otherwise in the SOURCE table the key will hold the row of the SOURCE table in whose 00078 // SOURCE_MODEL cell the Record of the model to use will be resident. 00079 // Now a given model Record in a SOURCE_MODEL cell 00080 // can hold multiple FTMachine's or Compnentlist (e.g multiple direction images or 00081 // spw images associated with a given field) 00082 // and they are all cumulative (while respecting spw selection conditions) 00083 // when a request is made for the model visibility 00084 // </synopsis> 00085 // 00086 // <example> 00087 // <srcblock> 00088 // MeasurementSet theMS(.....) 00089 00090 // VisibilityIterator vi(theMS,sort); 00091 // VisBuffer vb(vi); 00092 // MDirection myDir=vi.msColumns().field().phaseDirMeas(0); 00093 // ComponentList cl; 00094 // SkyComponent otherPoint(ComponentType::POINT); 00095 // otherPoint.flux() = Flux<Double>(0.00001, 0.0, 0.0, 0.00000); 00096 // otherPoint.shape().setRefDirection(myDir); 00097 // cl.add(otherPoint); 00098 // Record clrec; 00099 // clrec.define("type", "componentlist"); 00100 // ///Define the fields ans spw for which this model is valid 00101 // clrec.define("fields", field); 00102 // clrec.define("spws", Vector<Int>(1, 0)); 00103 // clrec.defineRecord("container", container); 00104 // Record outRec; 00105 // outRec.define("numcl", 1); 00106 // outRec.defineRecord("cl_0", clrec); 00107 00108 // Vector<Int>spws(1,0); 00109 // Save model to the MS 00110 // VisModelData.putModel(theMS, container, field, spws, Vector<Int>(1,0), Vector<Int>(1,63), Vector<Int>(1,1), True, False); 00111 // 00113 // vi.origin(); 00115 //VisModelData vm; 00117 // Int snum; 00118 // Bool hasmodkey=VisModelData::isModelDefined(vb.fieldId(), vi.ms(), modelkey, snum); 00119 // Setup vm to serve the model for the fieldid() 00120 // if( hasmodkey){ 00121 // TableRecord modrec; 00122 // VisModelData::getModelRecord(modelkey, modrec, visIter_p->ms()) 00123 // vm.addModel(modrec, Vector<Int>(1, msId()), vb); 00124 // } 00125 // for (vi.originChunks();vi.moreChunks(); vi.nextChunk()){ 00126 // for (vi.origin(); vi.more(); vi++){ 00127 // This fills the vb.modelVisCube with the appropriate model visibility 00128 // vm.getModelVis(vb); 00129 // cerr << "field " << vb.fieldId() << " spw " << vb.spectralWindow() <<" max " << max(amplitude(vb.modelVisCube())) << endl; 00130 00131 // } 00132 // } 00133 // </srcblock> 00134 // </example> 00135 // 00136 // <motivation> 00137 // </motivation> 00138 // 00139 // <todo asof="2013/05/24"> 00140 // Allow validity of models for a given section of time only 00141 // 00142 // </todo> 00143 00144 class VisModelData : public VisModelDataI { 00145 public: 00146 //empty constructor 00147 VisModelData(); 00148 //From a FTMachine Record 00149 //VisModelData(const Record& ftmachinerec, const Vector<Int>& validfieldids, const Vector<Int>& msIds); 00150 virtual ~VisModelData(); 00151 //Add Image/FTMachine to generate visibilities for 00152 //void addFTMachine(const Record& recordFTMachine, const Vector<Int>& validfieldids, const Vector<Int>& msIds); 00153 //Add componentlist to generate visibilities for 00154 //void addCompFTMachine(const ComponentList& cl, const Vector<Int>& validfieldids, 00155 // const Vector<Int>& msIds); 00156 //For simple model a special case for speed 00157 00158 void addFlatModel(const Vector<Double>& value, const Vector<Int>& validfieldids, 00159 const Vector<Int>& msIds); 00160 00161 //add componentlists or ftmachines 00162 void addModel(const RecordInterface& rec, const Vector<Int>& msids, const vi::VisBuffer2& vb); 00163 void addModel(const RecordInterface& /*rec*/, const Vector<Int>& /*msids*/, const VisBuffer& /*vb*/){throw(AipsError("Called the wrong version of VisModelData"));}; 00164 00165 00166 VisModelDataI * clone (); 00167 00168 //put the model data for this VisBuffer in the modelVisCube 00169 Bool getModelVis(vi::VisBuffer2& vb); 00170 Bool getModelVis(VisBuffer& /*vb*/){throw(AipsError("called the wrong version of VisModelData"));}; 00171 //this is a helper function that writes the model record to the ms 00172 void putModelI(const MeasurementSet& thems, const RecordInterface& rec, 00173 const Vector<Int>& validfields, const Vector<Int>& spws, 00174 const Vector<Int>& starts, const Vector<Int>& nchan, 00175 const Vector<Int>& incr, Bool iscomponentlist=True, Bool incremental=False) 00176 { 00177 putModel (thems, rec, validfields, spws, starts, nchan, incr, iscomponentlist, incremental); 00178 } 00179 static void putModel(const MeasurementSet& thems, const RecordInterface& rec, const Vector<Int>& validfields, const Vector<Int>& spws, const Vector<Int>& starts, const Vector<Int>& nchan, const Vector<Int>& incr, Bool iscomponentlist=True, Bool incremental=False); 00180 00181 //helper function to clear the keywordSet of the ms of the model for the fields 00182 //in that ms 00183 void clearModelI(const MeasurementSet& thems) { clearModel (thems); } 00184 static void clearModel(const MeasurementSet& thems); 00185 // ...with field selection and optionally spw 00186 static void clearModel(const MeasurementSet& thems, const String field, const String spws=String("")); 00187 00188 //Functions to see if model is defined in the MS either in the SOURCE table or else in the MAIN 00189 Bool isModelDefinedI(const Int fieldId, const MeasurementSet& thems, String& key, Int& sourceRow) 00190 { 00191 return isModelDefined (fieldId, thems, key, sourceRow); 00192 } 00193 static Bool isModelDefined(const Int fieldId, const MeasurementSet& thems, String& key, Int& sourceRow); 00194 static Bool isModelDefined(const String& elkey, const MeasurementSet& thems); 00195 00196 //Get a given model that is defined by key 00197 //Forcing user to use a TableRecord rather than Generic RecordInterface ...just so as to avoid a copy. 00198 Bool getModelRecordI(const String& theKey, TableRecord& theRec, const MeasurementSet& theMs) 00199 { 00200 return getModelRecord (theKey, theRec, theMs); 00201 } 00202 static Bool getModelRecord(const String& theKey, TableRecord& theRec, const MeasurementSet& theMs); 00203 00204 // List the fields 00205 static void listModel(const MeasurementSet& thems); 00206 00207 static FTMachine* NEW_FT(const Record& ftrec); 00208 //check if an addFT or addCompFT is necessary 00209 //Bool hasFT(Int msid, Int fieldid); 00210 //Bool hasCL(Int msid, Int fieldid); 00211 //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 00212 //returns a -2 if it has been tested before but does have it. 00213 //returns a 1 if it has a model stored 00214 Int hasModel(Int msid, Int field, Int spw); 00215 private: 00216 void initializeToVis(); 00217 Vector<CountedPtr<ComponentList> >getCL(const Int msId, const Int fieldId, Int spw); 00218 Vector<CountedPtr<FTMachine> >getFT(const Int msId, const Int fieldId, Int spw); 00219 static Bool addToRec(TableRecord& therec, const Vector<Int>& spws); 00220 static Bool removeSpwFromMachineRec(RecordInterface& ftclrec, const Vector<Int>& spws); 00221 static Bool removeFTFromRec(TableRecord& therec, const String& keyval, const Bool relabel=True); 00222 static Bool removeSpw(TableRecord& therec, const Vector<Int>& spws, const Vector<Int>& fields=Vector<Int>(0)); 00223 static Bool putModelRecord(const Vector<Int>& fieldIds, TableRecord& theRec, MeasurementSet& theMS); 00224 //Remove the Record which has the given key...will exterminate it from both the Source table or Main table 00225 static void removeRecordByKey(MeasurementSet& theMS, const String& theKey); 00226 //put the Record by key if sourcerownum=-1 then it is saved in the main table 00227 //this default should only be used if the optional SOURCE table in non-existant 00228 static void putRecordByKey(MeasurementSet& theMS, const String& theKey, const TableRecord& theRec, const Int sourceRowNum=-1); 00229 static void deleteDiskImage(MeasurementSet& theMS, const String& theKey); 00230 static Int firstSourceRowRecord(const Int field, const MeasurementSet& theMS, 00231 TableRecord& rec); 00232 static void modifyDiskImagePath(Record& rec, const vi::VisBuffer2& vb); 00233 Block<Vector<CountedPtr<ComponentList> > > clholder_p; 00234 Block<Vector<CountedPtr<FTMachine> > > ftholder_p; 00235 Block<Vector<Double> > flatholder_p; 00236 CountedPtr<ComponentFTMachine> cft_p; 00237 Cube<Int> ftindex_p; 00238 Cube<Int> clindex_p; 00239 static Bool initialize; 00240 }; 00241 00242 }// end namespace refim 00243 }//end namespace 00244 00245 #endif // VISMODELDATA_H