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