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: aips2-request@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 167 : CalSolVi2Organizer::CalSolVi2Organizer() :
47 : data_(NULL),
48 : cal_(NULL),
49 : chanave_(NULL),
50 : timeave_(NULL),
51 : polave_(NULL),
52 : calfilter_(NULL),
53 : factories_(),
54 167 : vi_(NULL)
55 : {
56 : // nothing else
57 167 : }
58 :
59 : // Destructor
60 167 : CalSolVi2Organizer::~CalSolVi2Organizer()
61 : {
62 167 : cleanUp();
63 167 : }
64 :
65 167 : vi::VisibilityIterator2& CalSolVi2Organizer::makeFullVI() {
66 :
67 167 : AlwaysAssert(factories_.nelements()>0, casacore::AipsError);
68 :
69 167 : if (vi_) delete vi_;
70 167 : vi_=new VisibilityIterator2(factories_);
71 :
72 167 : 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 : }
112 :
113 :
114 167 : 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 167 : AlwaysAssert(factories_.nelements()==0, AipsError);
123 :
124 :
125 334 : Block<Int> sc;
126 167 : deriveVI2Sort(sc,combobs,combscan,combfld,combspw);
127 167 : IteratingParameters iterpar(interval,SortColumns(sc));
128 :
129 167 : auto diskLayerFactory=new VisIterImpl2LayerFactory(ms,iterpar,True,useMSIter2);
130 :
131 167 : if(freqSel)
132 167 : diskLayerFactory->setFrequencySelections(freqSel);
133 :
134 167 : data_ = diskLayerFactory;
135 :
136 167 : factories_.resize(1);
137 167 : factories_[0]=data_;
138 167 : }
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 167 : void CalSolVi2Organizer::addCalForSolving(VisEquation& ve, const Bool& corrDepFlags) {
182 :
183 : // Must not have added one already!
184 167 : AlwaysAssert(!cal_, AipsError);
185 :
186 : // Must be at least one other layer already...
187 167 : AlwaysAssert(factories_.nelements()>0, AipsError);
188 :
189 : // Make the layer factory
190 167 : cal_= new CalSolvingVi2LayerFactoryByVE(&ve,corrDepFlags);
191 :
192 : // Add it to the list...
193 167 : this->appendFactory(cal_);
194 :
195 167 : }
196 :
197 :
198 91 : void CalSolVi2Organizer::addChanAve(Vector<Int> chanbin) {
199 :
200 : // Must not have added one already!
201 91 : AlwaysAssert(!chanave_, AipsError);
202 :
203 : // Must be at least one other layer already...
204 91 : 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 91 : chanbin(chanbin==1)=0;
209 :
210 182 : Record config;
211 91 : config.define("chanbin",chanbin);
212 :
213 : // Make the layer factory
214 91 : chanave_= new ChannelAverageTVILayerFactory(config);
215 :
216 : // Add it to the list...
217 91 : this->appendFactory(chanave_);
218 :
219 91 : }
220 :
221 : // Add time-averaging layer factory
222 129 : void CalSolVi2Organizer::addTimeAve(Float timebin) {
223 :
224 : // Must not have added one already!
225 129 : AlwaysAssert(!timeave_, AipsError);
226 :
227 : // Must be at least one other layer already...
228 129 : AlwaysAssert(factories_.nelements()>0, AipsError);
229 :
230 : AveragingOptions aveopt(AveragingOptions::AverageCorrected|
231 : AveragingOptions::CorrectedFlagWeightAvgFromWEIGHT|
232 : AveragingOptions::AverageModel|
233 129 : AveragingOptions::ModelPlainAvg);
234 387 : AveragingParameters avepar(timebin,0.0,SortColumns(),aveopt);
235 129 : timeave_ = new AveragingVi2LayerFactory(avepar);
236 :
237 : // Add it to the list...
238 129 : this->appendFactory(timeave_);
239 :
240 129 : }
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 18 : Record configuration(config);
254 18 : 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 2 : 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 397 : void CalSolVi2Organizer::appendFactory(ViiLayerFactory* f) {
283 :
284 397 : Int nf=factories_.nelements();
285 397 : factories_.resize(nf+1,True); // copies exising values
286 397 : factories_[nf]=f;
287 :
288 397 : }
289 :
290 :
291 167 : void CalSolVi2Organizer::cleanUp() {
292 :
293 167 : if (vi_) delete vi_; vi_=NULL;
294 167 : if (data_) delete data_; data_=NULL;
295 167 : if (cal_) delete cal_; cal_=NULL;
296 167 : if (chanave_) delete chanave_; chanave_=NULL;
297 167 : if (timeave_) delete timeave_; timeave_=NULL;
298 167 : if (polave_) delete polave_; polave_=NULL;
299 167 : if (calfilter_) delete calfilter_; calfilter_=NULL;
300 167 : factories_.resize(0);
301 :
302 167 : }
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 167 : 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 167 : Bool verbose(False);
333 167 : 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 167 : Int nsortcol(4+(combscan?0:1)+(combobs?0:1) ); // include room for scan,obs
343 167 : sortcols.resize(nsortcol);
344 167 : Int i(0);
345 167 : sortcols[i++]=MS::ARRAY_ID;
346 167 : if (!combobs) sortcols[i++]=MS::OBSERVATION_ID; // force obsid boundaries
347 167 : if (!combscan) sortcols[i++]=MS::SCAN_NUMBER; // force scan boundaries
348 167 : if (!combfld) sortcols[i++]=MS::FIELD_ID; // force field boundaries
349 167 : if (!combspw) sortcols[i++]=MS::DATA_DESC_ID; // force spw boundaries
350 167 : sortcols[i++]=MS::TIME;
351 167 : if (combfld) sortcols[i++]=MS::FIELD_ID; // effectively ignore field boundaries
352 167 : 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 167 : }
358 :
359 :
360 :
361 :
362 :
363 : } //# NAMESPACE CASA - END
|