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 TRANSFORM2_TRANSFORM2_VISMODELDATA_H
29 : #define TRANSFORM2_TRANSFORM2_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/TransformMachines2/ComponentFTMachine.h>
35 : #include <msvis/MSVis/VisModelDataI.h>
36 : #include <msvis/MSVis/VisBuffer.h> //here only for the pure virtual function that uses this
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 : namespace vi{class VisBuffer2;}
47 : class ComponentList;
48 : namespace refim{ //namespace refim
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 vi::VisBuffer2& vb);
166 0 : void addModel(const casacore::RecordInterface& /*rec*/, const casacore::Vector<casacore::Int>& /*msids*/, const VisBuffer& /*vb*/){throw(casacore::AipsError("Called the wrong version of VisModelData"));};
167 :
168 :
169 : VisModelDataI * clone ();
170 :
171 : //put the model data for this VisBuffer in the modelVisCube
172 : casacore::Bool getModelVis(vi::VisBuffer2& vb);
173 0 : casacore::Bool getModelVis(VisBuffer& /*vb*/){throw(casacore::AipsError("called the wrong version of VisModelData"));};
174 : //this is a helper function that writes the model record to the ms
175 0 : void putModelI(const casacore::MeasurementSet& thems, const casacore::RecordInterface& rec,
176 : const casacore::Vector<casacore::Int>& validfields, const casacore::Vector<casacore::Int>& spws,
177 : const casacore::Vector<casacore::Int>& starts, const casacore::Vector<casacore::Int>& nchan,
178 : const casacore::Vector<casacore::Int>& incr, casacore::Bool iscomponentlist=true, casacore::Bool incremental=false)
179 : {
180 0 : putModel (thems, rec, validfields, spws, starts, nchan, incr, iscomponentlist, incremental);
181 0 : }
182 : 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);
183 : //Verion 2.0 of putModel to support Intent
184 : // index Comb is a Matrix of n rows 4 columns
185 : //Each row is a unique combination of field_id, spw_id, scan_number, state_id for which this
186 : //record is valid
187 : //chansel is Matrix of nspw rows and 4 columns (spw_id, start, nchan, incr)
188 : static void putModel(const casacore::MeasurementSet& thems,const casacore::RecordInterface& rec,
189 : const casacore::Matrix<casacore::Int>& indexComb, const casacore::Matrix<casacore::Int>& chansel,
190 : casacore::Bool iscomponentlist=true, casacore::Bool incremental=false);
191 0 : void putModelI(const casacore::MeasurementSet& thems,const casacore::RecordInterface& rec, const casacore::Matrix<casacore::Int>& indexComb,
192 : const casacore::Matrix<casacore::Int>& chansel,
193 : casacore::Bool iscomponentlist=true, casacore::Bool incremental=false){
194 0 : putModel(thems, rec, indexComb, chansel, iscomponentlist, incremental);
195 0 : };
196 :
197 :
198 : //helper function to clear the keywordSet of the ms of the model for the fields
199 : //in that ms
200 0 : void clearModelI(const casacore::MeasurementSet& thems) { clearModel (thems); }
201 : static void clearModel(const casacore::MeasurementSet& thems);
202 : // ...with field selection and optionally spw
203 : static void clearModel(const casacore::MeasurementSet& thems, const casacore::String field, const casacore::String spws=casacore::String(""));
204 :
205 : //Functions to see if model is defined in the casacore::MS either in the SOURCE table or else in the MAIN
206 451018 : casacore::Bool isModelDefinedI(const casacore::Int fieldId, const casacore::MeasurementSet& thems, casacore::String& key, casacore::Int& sourceRow)
207 : {
208 451018 : return isModelDefined (fieldId, thems, key, sourceRow);
209 : }
210 : static casacore::Bool isModelDefined(const casacore::Int fieldId, const casacore::MeasurementSet& thems, casacore::String& key, casacore::Int& sourceRow);
211 : static casacore::Bool isModelDefined(const casacore::String& elkey, const casacore::MeasurementSet& thems);
212 :
213 : //Get a given model that is defined by key
214 : //Forcing user to use a casacore::TableRecord rather than Generic casacore::RecordInterface ...just so as to avoid a copy.
215 0 : casacore::Bool getModelRecordI(const casacore::String& theKey, casacore::TableRecord& theRec, const casacore::MeasurementSet& theMs)
216 : {
217 0 : return getModelRecord (theKey, theRec, theMs);
218 : }
219 : static casacore::Bool getModelRecord(const casacore::String& theKey, casacore::TableRecord& theRec, const casacore::MeasurementSet& theMs);
220 :
221 : // List the fields in the logger
222 : static void listModel(const casacore::MeasurementSet& thems);
223 :
224 : // Returns true if any valid virtual model is found, fieldids of which are also returned
225 : static casacore::Bool hasAnyModel(const casacore::MeasurementSet& thems, casacore::Vector<casacore::Int>& fieldids);
226 : static FTMachine* NEW_FT(const casacore::Record& ftrec);
227 : //check if an addFT or addCompFT is necessary
228 : //casacore::Bool hasFT(casacore::Int msid, casacore::Int fieldid);
229 : //casacore::Bool hasCL(casacore::Int msid, casacore::Int fieldid);
230 : //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
231 : //returns a -2 if it has been tested before but does have it.
232 : //returns a 1 if it has a model stored
233 : casacore::Int hasModel(casacore::Int msid, casacore::Int field, casacore::Int spw);
234 526540 : casacore::Bool isVersion2() {return (version_p==2);};
235 : void init(const vi::VisBuffer2& vb);
236 0 : void init(const VisBuffer& /*vb*/){throw(casacore::AipsError("Called the wrong version of VisModelData"));};
237 : private:
238 : void initializeToVis();
239 : casacore::Vector<casacore::CountedPtr<ComponentList> >getCL(const casacore::Int msId, const casacore::Int fieldId, casacore::Int spw);
240 : casacore::Vector<casacore::CountedPtr<FTMachine> >getFT(const casacore::Int msId, const casacore::Int fieldId, casacore::Int spw);
241 : static casacore::Bool addToRec(casacore::TableRecord& therec, const casacore::Vector<casacore::Int>& spws);
242 : //Version 2.0
243 : static casacore::Bool addToRec(casacore::TableRecord& therec, const casacore::Matrix<casacore::Int>& combindex);
244 : static casacore::Bool removeSpwFromMachineRec(casacore::RecordInterface& ftclrec, const casacore::Vector<casacore::Int>& spws);
245 : static casacore::Bool removeFTFromRec(casacore::TableRecord& therec, const casacore::String& keyval, const casacore::Bool relabel=true);
246 : static casacore::Bool removeSpw(casacore::TableRecord& therec, const casacore::Vector<casacore::Int>& spws, const casacore::Vector<casacore::Int>& fields=casacore::Vector<casacore::Int>(0));
247 : static casacore::Bool putModelRecord(const casacore::Vector<casacore::Int>& fieldIds, casacore::TableRecord& theRec, casacore::MeasurementSet& theMS);
248 : //Remove the casacore::Record which has the given key...will exterminate it from both the Source table or Main table
249 : static void removeRecordByKey(casacore::MeasurementSet& theMS, const casacore::String& theKey);
250 : //put the casacore::Record by key if sourcerownum=-1 then it is saved in the main table
251 : //this default should only be used if the optional SOURCE table in non-existant
252 : static void putRecordByKey(casacore::MeasurementSet& theMS, const casacore::String& theKey, const casacore::TableRecord& theRec, const casacore::Int sourceRowNum=-1);
253 : static void deleteDiskImage(casacore::MeasurementSet& theMS, const casacore::String& theKey);
254 : static casacore::Int firstSourceRowRecord(const casacore::Int field, const casacore::MeasurementSet& theMS,
255 : casacore::TableRecord& rec);
256 : static void modifyDiskImagePath(casacore::Record& rec, const vi::VisBuffer2& vb);
257 : void getMatchingMachines(casacore::Vector<casacore::CountedPtr<FTMachine> >& ft, casacore::Vector<casacore::CountedPtr<ComponentList> >& cl, const vi::VisBuffer2& vb);
258 : void updateHolders(const vi::VisBuffer2& vb, const std::vector<casacore::Int>& index);
259 : //get all the combinations of field, spw, scan, state in this visbuffer
260 : void getUniqueIndicesComb(const vi::VisBuffer2& vb, casacore::Vector< casacore::Vector<casacore::Int> >& retval);
261 : casacore::Block<casacore::Vector<casacore::CountedPtr<ComponentList> > > clholder_p;
262 : casacore::Block<casacore::Vector<casacore::CountedPtr<FTMachine> > > ftholder_p;
263 : casacore::Block<casacore::Vector<casacore::Double> > flatholder_p;
264 : casacore::CountedPtr<ComponentFTMachine> cft_p;
265 : casacore::Cube<casacore::Int> ftindex_p;
266 : casacore::Cube<casacore::Int> clindex_p;
267 : //These 2 maps will hold the index of ftholder_p or clholder_p from key (field_id, spw_id, scan, state, msid)
268 : std::map<std::vector<casacore::Int>, casacore::Int > ftindex2_p;
269 : std::map<std::vector<casacore::Int>, casacore::Int > clindex2_p;
270 : casacore::Int version_p;
271 : static casacore::Bool initialize;
272 : };
273 :
274 : }// end namespace refim
275 : }//end namespace
276 :
277 : #endif // VISMODELDATA_H
|