Line data Source code
1 : //# SDMSManager.cc: this defines single dish MS transform manager
2 : //# inheriting MSTransformManager.
3 : //#
4 : //# Copyright (C) 2015
5 : //# National Astronomical Observatory of Japan
6 : //#
7 : //# This library is free software; you can redistribute it and/or modify it
8 : //# under the terms of the GNU Library General Public License as published by
9 : //# the Free Software Foundation; either version 2 of the License, or (at your
10 : //# option) any later version.
11 : //#
12 : //# This library is distributed in the hope that it will be useful, but WITHOUT
13 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 : //# License for more details.
16 : //#
17 : //# You should have received a copy of the GNU Library General Public License
18 : //# along with this library; if not, write to the Free Software Foundation,
19 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20 : //#
21 : //# Correspondence concerning AIPS++ should be addressed as follows:
22 : //# Internet email: casa-feedback@nrao.edu.
23 : //# Postal address: AIPS++ Project Office
24 : //# National Radio Astronomy Observatory
25 : //# 520 Edgemont Road
26 : //# Charlottesville, VA 22903-2475 USA
27 : //#
28 : //# $Id$
29 : #include <algorithm>
30 : #include <array>
31 : #include <iostream>
32 : #include <iterator>
33 : #include <list>
34 :
35 : //#include <libsakura/sakura.h>
36 : //#include <libsakura/config.h>
37 :
38 : #include <casacore/casa/Logging/LogIO.h>
39 : #include <casacore/casa/Logging/LogOrigin.h>
40 : #include <casacore/casa/Utilities/Assert.h>
41 : #include <casacore/casa/Arrays/ArrayMath.h>
42 : #include <casacore/casa/Utilities/Sort.h>
43 :
44 : #include <casacore/ms/MSSel/MSSelectionTools.h>
45 : #include <msvis/MSVis/VisibilityIterator2.h>
46 : #include <msvis/MSVis/VisSetUtil.h>
47 :
48 : #include <casa_sakura/SakuraUtils.h>
49 : #include <singledish/SingleDish/SDMSManager.h>
50 :
51 :
52 : #define _ORIGIN LogOrigin("SDMSManager", __func__, WHERE)
53 :
54 : using namespace casacore;
55 :
56 : namespace casa {
57 :
58 751 : SDMSManager::SDMSManager() : doSmoothing_(false) {
59 751 : }
60 :
61 : // SDMSManager &SDMSManager::operator=(SDMSManager const &other)
62 : // {
63 : // return *this;
64 : // }
65 :
66 1502 : SDMSManager::~SDMSManager() {
67 1502 : LogIO os(_ORIGIN);
68 1502 : }
69 :
70 : // -----------------------------------------------------------------------
71 : // Fill output MS with data from an input VisBuffer
72 : // -----------------------------------------------------------------------
73 1836 : void SDMSManager::fillCubeToOutputMs(vi::VisBuffer2 *vb,Cube<Float> const &data_cube,
74 : Cube<Bool> const *flag_cube, Matrix<Float> const *weight_matrix) {
75 1836 : setupBufferTransformations(vb);
76 :
77 : // make sure the shape of cube matches the number of rows to add.
78 1836 : AlwaysAssert(data_cube.nplane() == nRowsToAdd_p, AipsError);
79 :
80 1836 : if (bufferMode_p) {
81 0 : return;
82 : }
83 :
84 : // Create RowRef object to fill new rows
85 1836 : uInt currentRows = outputMs_p->nrow();
86 1836 : RefRows rowRef(currentRows, currentRows + nRowsToAdd_p / nspws_p - 1);
87 :
88 : // Add new rows to output MS
89 1836 : outputMs_p->addRow(nRowsToAdd_p, false);
90 :
91 : // Fill new rows
92 1836 : if (weight_matrix == nullptr) {
93 1799 : weightSpectrumFlatFilled_p = false;
94 1799 : weightSpectrumFromSigmaFilled_p = false;
95 1799 : fillWeightCols(vb, rowRef);
96 : }
97 1836 : fillCubeToDataCols(vb, rowRef, data_cube, flag_cube);
98 1836 : fillIdCols(vb, rowRef);
99 1836 : if (weight_matrix != nullptr) { // for update_weight=True (CAS-13161)
100 37 : outputMsCols_p->weight().putColumnCells(rowRef, *weight_matrix);
101 : }
102 1836 : }
103 :
104 : // ----------------------------------------------------------------------------------------
105 : // Fill main (data) columns which have to be combined together to produce bigger SPWs
106 : // ----------------------------------------------------------------------------------------
107 1836 : void SDMSManager::fillCubeToDataCols(vi::VisBuffer2 *vb, RefRows &rowRef,
108 : Cube<Float> const &data_cube, Cube<Bool> const *flag_cube) {
109 1836 : ArrayColumn<Bool> *outputFlagCol = NULL;
110 3672 : for (dataColMap::iterator iter = dataColMap_p.begin(); iter != dataColMap_p.end(); iter++) {
111 : // Get applicable *_SPECTRUM (copy constructor uses reference semantics)
112 : // If channel average or combine, otherwise no need to copy
113 1836 : const Cube<Float> applicableSpectrum = getApplicableSpectrum(vb, iter->first);
114 :
115 : // Apply transformations
116 1836 : switch (iter->first) {
117 942 : case MS::DATA:
118 : {
119 942 : if (mainColumn_p == MS::DATA) {
120 942 : outputFlagCol = &(outputMsCols_p->flag());
121 942 : setTileShape(rowRef, outputMsCols_p->flag());
122 : } else {
123 0 : outputFlagCol = NULL;
124 : }
125 942 : setTileShape(rowRef, outputMsCols_p->data());
126 942 : Cube<Complex> cdata_cube(data_cube.shape());
127 942 : convertArray(cdata_cube, data_cube);
128 942 : transformCubeOfData(vb, rowRef, cdata_cube, outputMsCols_p->data(),
129 : outputFlagCol, applicableSpectrum);
130 942 : break;
131 942 : }
132 12 : case MS::CORRECTED_DATA:
133 : {
134 12 : if (mainColumn_p == MS::CORRECTED_DATA) {
135 12 : outputFlagCol = &(outputMsCols_p->flag());
136 12 : setTileShape(rowRef, outputMsCols_p->flag());
137 : } else {
138 0 : outputFlagCol = NULL;
139 : }
140 12 : Cube<Complex> cdata_cube(data_cube.shape());
141 12 : convertArray(cdata_cube, data_cube);
142 12 : if (iter->second == MS::DATA) {
143 12 : setTileShape(rowRef, outputMsCols_p->data());
144 12 : transformCubeOfData(vb, rowRef, cdata_cube, outputMsCols_p->data(),
145 : outputFlagCol, applicableSpectrum);
146 : } else {
147 0 : setTileShape(rowRef, outputMsCols_p->correctedData());
148 0 : transformCubeOfData(vb, rowRef, cdata_cube, outputMsCols_p->correctedData(),
149 : outputFlagCol, applicableSpectrum);
150 : }
151 12 : break;
152 12 : }
153 0 : case MS::MODEL_DATA:
154 : {
155 0 : if (mainColumn_p == MS::MODEL_DATA) {
156 0 : outputFlagCol = &(outputMsCols_p->flag());
157 0 : setTileShape(rowRef, outputMsCols_p->flag());
158 : } else {
159 0 : outputFlagCol = NULL;
160 : }
161 :
162 0 : if (iter->second == MS::DATA) {
163 0 : setTileShape(rowRef, outputMsCols_p->data());
164 0 : transformCubeOfData(vb, rowRef, vb->visCubeModel(), outputMsCols_p->data(),
165 : outputFlagCol, applicableSpectrum);
166 : } else {
167 0 : setTileShape(rowRef, outputMsCols_p->modelData());
168 0 : transformCubeOfData(vb, rowRef, vb->visCubeModel(), outputMsCols_p->modelData(),
169 : outputFlagCol, applicableSpectrum);
170 : }
171 0 : break;
172 : }
173 882 : case MS::FLOAT_DATA:
174 : {
175 882 : if (mainColumn_p == MS::FLOAT_DATA) {
176 882 : outputFlagCol = &(outputMsCols_p->flag());
177 882 : setTileShape(rowRef, outputMsCols_p->flag());
178 : } else {
179 0 : outputFlagCol = NULL;
180 : }
181 882 : setTileShape(rowRef, outputMsCols_p->floatData());
182 882 : transformCubeOfData(vb, rowRef, data_cube, outputMsCols_p->floatData(),
183 : outputFlagCol, applicableSpectrum);
184 882 : break;
185 : }
186 0 : case MS::LAG_DATA:
187 : {
188 : // jagonzal: TODO
189 0 : break;
190 : }
191 0 : default:
192 : {
193 : // jagonzal: TODO
194 0 : break;
195 : }
196 : } // end switch
197 :
198 : //KS: THIS PART ASSUMES INROW==OUTROW
199 1836 : if ((outputFlagCol != NULL) && (flag_cube != nullptr)) {
200 1836 : setTileShape(rowRef, outputMsCols_p->flag());
201 1836 : writeCube(*flag_cube, *outputFlagCol, rowRef);
202 : }
203 1836 : } // end loop for dataColMap
204 :
205 : // Special case for flag category
206 1836 : if (inputFlagCategoryAvailable_p) {
207 0 : if (spectrumReshape_p) {
208 0 : IPosition transformedCubeShape = getShape(); //[nC,nF,nR]
209 0 : IPosition inputFlagCategoryShape = vb->flagCategory().shape(); // [nC,nF,nCategories,nR]
210 : IPosition flagCategoryShape(4,
211 0 : inputFlagCategoryShape(1),
212 0 : transformedCubeShape(2),
213 0 : inputFlagCategoryShape(2),
214 0 : transformedCubeShape(2));
215 0 : Array<Bool> flagCategory(flagCategoryShape, false);
216 :
217 0 : outputMsCols_p->flagCategory().putColumnCells(rowRef, flagCategory);
218 0 : } else {
219 0 : outputMsCols_p->flagCategory().putColumnCells(rowRef, vb->flagCategory());
220 : }
221 : }
222 :
223 1836 : return;
224 : }
225 :
226 : // -----------------------------------------------------------------------
227 : // Method to determine sort columns order
228 : // -----------------------------------------------------------------------
229 746 : void SDMSManager::setSortColumns(Block<Int> sortColumns,
230 : bool addDefaultSortCols,
231 : Double timebin) {
232 746 : size_t const num_in = sortColumns.nelements();
233 1492 : LogIO os(_ORIGIN);
234 746 : os << "Setting user sort columns with " << num_in << " elements" << LogIO::POST;
235 746 : if (addDefaultSortCols) {
236 157 : Block<Int> defaultCols(6);
237 157 : defaultCols[0] = MS::OBSERVATION_ID;
238 157 : defaultCols[1] = MS::ARRAY_ID;
239 : // Assuming auto-correlation here
240 157 : defaultCols[2] = MS::ANTENNA1;
241 157 : defaultCols[3] = MS::FEED1;
242 157 : defaultCols[4] = MS::DATA_DESC_ID;
243 157 : defaultCols[5] = MS::TIME;
244 157 : os << "Adding default sort columns to user sort column" << LogIO::POST;
245 157 : if (num_in > 0) {
246 0 : size_t num_elem(num_in);
247 0 : sortColumns.resize(num_in + defaultCols.nelements(), false, true);
248 0 : for (size_t i = 0; i < defaultCols.nelements(); ++i) {
249 0 : bool addcol = true;
250 0 : if (getBlockId(sortColumns, defaultCols[i]) > -1) {
251 0 : addcol = false; // the columns is in sortColumns
252 : }
253 0 : if (addcol) {
254 0 : sortColumns[num_elem] = defaultCols[i];
255 0 : ++num_elem;
256 : }
257 : }
258 : // shrink block
259 0 : sortColumns.resize(num_elem, true, true);
260 0 : userSortCols_ = sortColumns;
261 : } else {
262 157 : userSortCols_ = defaultCols;
263 : }
264 157 : } else { // do not add
265 589 : if (num_in > 0) {
266 589 : userSortCols_ = sortColumns;
267 : } else {
268 0 : userSortCols_ = Block<Int>();
269 : }
270 : }
271 746 : os << "Defined user sort columns with " << userSortCols_.nelements() << " elements" << LogIO::POST;
272 746 : timeBin_p = timebin;
273 746 : os << "Time bin is " << timeBin_p << " sec" << LogIO::POST;
274 : // regenerate iterator if necessary
275 746 : if (visibilityIterator_p != NULL) {
276 0 : os << "Regenerating iterator" << LogIO::POST;
277 0 : setIterationApproach();
278 0 : generateIterator();
279 : }
280 :
281 1492 : return;
282 746 : }
283 :
284 30 : int SDMSManager::getBlockId(Block<Int> const &data, Int const value) {
285 60 : for (size_t i = 0; i < data.nelements(); ++i) {
286 30 : if (data[i] == value) {
287 0 : return (int)i;
288 : }
289 : }
290 :
291 30 : return -1;
292 : }
293 :
294 746 : void SDMSManager::setIterationApproach() {
295 746 : if (userSortCols_.nelements() == 0) {
296 0 : sortColumns_p = Block<Int>();
297 :
298 0 : return;
299 : }
300 :
301 : // User column is set.
302 746 : logger_p.origin(_ORIGIN);
303 :
304 : using ColumnId = MSMainEnums::PredefinedColumns;
305 : struct CheckItem {
306 2238 : CheckItem(ColumnId const &id, String const &name, String const &type, Bool const &remove)
307 2238 : : columnId{id}, columnName{name}, paramType{type}, removeIt{remove}, addIt{!remove}
308 2238 : {}
309 : ColumnId columnId;
310 : String columnName;
311 : String paramType;
312 : Bool removeIt;
313 : Bool addIt;
314 : };
315 30 : auto const userSortColExists = [&](ColumnId const &columnId) {
316 30 : return getBlockId(userSortCols_, columnId) > -1;
317 746 : };
318 :
319 : // copy userSortCols_ to std::list
320 746 : std::list<ColumnId> userSortColsList;
321 746 : std::transform(
322 : userSortCols_.begin(), userSortCols_.end(),
323 : std::back_inserter(userSortColsList),
324 1615 : [](Int const &i) {return static_cast<ColumnId>(i);}
325 : );
326 :
327 : // list of columns that may be added/removed depending on the value of timespan_p
328 : std::array<CheckItem, 3> const checkList {{
329 746 : {MS::SCAN_NUMBER, "SCAN_NUMBER", "scan", timespan_p.contains("scan")},
330 746 : {MS::STATE_ID, "STATE_ID", "state", timespan_p.contains("state")},
331 746 : {MS::FIELD_ID, "FIELD_ID", "field", timespan_p.contains("field")},
332 2984 : }};
333 :
334 : // remove columns from userSortColsList if necessary
335 2984 : for (auto const &item: checkList) {
336 2238 : if (item.removeIt && userSortColExists(item.columnId)) {
337 0 : logger_p << LogIO::NORMAL;
338 0 : logger_p << "Combining data through " << item.paramType << "s for time average. ";
339 0 : logger_p << "Removing " << item.columnName << " from user sort list." << LogIO::POST;
340 0 : userSortColsList.remove(item.columnId);
341 : }
342 : }
343 :
344 : // add columns to userSortColsList if necessary
345 746 : if (timeAverage_p) {
346 40 : for (auto const &item: checkList) {
347 30 : if (item.addIt && !userSortColExists(item.columnId)) {
348 15 : logger_p << LogIO::NORMAL
349 15 : << "Splitting data by " << item.paramType << "s for time average. "
350 15 : << "Adding " << item.columnName << " to user sort list." << LogIO::POST;
351 15 : userSortColsList.push_back(item.columnId);
352 : }
353 : }
354 : }
355 :
356 : // copy back userSortColsList to sortColumns_p
357 746 : constexpr bool forceSmaller = true;
358 746 : constexpr bool copyElements = false;
359 746 : sortColumns_p.resize(userSortColsList.size(), forceSmaller, copyElements);
360 746 : std::transform(
361 : userSortColsList.begin(), userSortColsList.end(), sortColumns_p.begin(),
362 1630 : [](ColumnId const &i) {return static_cast<Int>(i);}
363 : );
364 :
365 746 : ostringstream oss;
366 2376 : for (size_t i = 0; i < sortColumns_p.nelements(); ++i) {
367 1630 : oss << sortColumns_p[i] << ", ";
368 : }
369 746 : logger_p << LogIO::DEBUG1 << "Final Sort order: "
370 746 : << oss.str() << LogIO::POST;
371 :
372 746 : return;
373 746 : }
374 :
375 638 : Record SDMSManager::getSelRec(string const &spw) {
376 638 : MeasurementSet myms = MeasurementSet(inpMsName_p);
377 1914 : return myms.msseltoindex(toCasaString(spw));
378 638 : }
379 :
380 : /*
381 : MeasurementSet SDMSManager::getMS()
382 : {
383 : MeasurementSet myms = MeasurementSet(inpMsName_p);
384 : return myms;
385 : }
386 : */
387 :
388 157 : void SDMSManager::setSmoothing(string const &kernelType, float const &kernelWidth) {
389 : // kernel type
390 157 : VectorKernel::KernelTypes type = VectorKernel::toKernelType(kernelType);
391 :
392 : // Fail if type is neither GAUSSIAN nor BOXCAR since other ones are yet to be supported
393 157 : if ((type != VectorKernel::GAUSSIAN) && (type != VectorKernel::BOXCAR)) {
394 0 : stringstream oss;
395 0 : oss << "Smoothing kernel type \"" << kernelType << "\" is not supported yet.";
396 0 : throw AipsError(oss.str());
397 0 : }
398 :
399 : // Fail if kernel width is zero or negative
400 157 : if (kernelWidth <= 0.0) {
401 0 : throw AipsError("Zero or negative kernel width is not allowed.");
402 : }
403 :
404 157 : doSmoothing_ = true;
405 157 : kernelType_ = type;
406 157 : kernelWidth_ = kernelWidth;
407 157 : }
408 :
409 0 : void SDMSManager::unsetSmoothing() {
410 0 : doSmoothing_ = false;
411 0 : }
412 :
413 157 : void SDMSManager::initializeSmoothing() {
414 314 : LogIO os(_ORIGIN);
415 157 : if (!doSmoothing_) {
416 0 : return;
417 : }
418 :
419 157 : Vector<Int> numChanList = inspectNumChan();
420 332 : for (size_t i = 0; i < numChanList.nelements(); ++i) {
421 175 : Int numChan = numChanList[i];
422 175 : Vector<Float> theKernel = VectorKernel::make(kernelType_, kernelWidth_, numChan, true, false);
423 : //shift 1 channel for boxcar kernel---(for CAS-7442, 2015/11/18 WK)
424 175 : if (kernelType_ == VectorKernel::BOXCAR) {
425 188056 : for (size_t j = theKernel.nelements()-1; j >= 1; --j) {
426 187958 : theKernel[j] = theKernel[j-1];
427 : }
428 98 : theKernel[0] = 0.0;
429 : }
430 : //------
431 175 : convolverPool_[numChan] = Convolver<Float>();
432 175 : convolverPool_[numChan].setPsf(theKernel, IPosition(1, numChan));
433 175 : }
434 :
435 : // set smoothing kernel for weight
436 157 : smoothBin_p = static_cast<uInt>(kernelWidth_ + 0.5f);
437 157 : smoothBin_p += (smoothBin_p % 2 == 0) ? 1 : 0; // to make smoothBin_p odd
438 157 : uInt numChanMinimum = min(numChanList);
439 157 : smoothBin_p = min(smoothBin_p, numChanMinimum - ((numChanMinimum % 2 == 0) ? 1 : 0)); // smoothBin_p < numChanMinimum
440 157 : uInt halfWidth = smoothBin_p / 2;
441 157 : Sort sort;
442 157 : Vector<Float> kernelForMinimumNumChan = convolverPool_[numChanMinimum].getPsf();
443 157 : sort.sortKey(kernelForMinimumNumChan.data(), TpFloat, 0, Sort::Descending);
444 157 : Vector<uInt> indexArray;
445 : //uInt indexArrayLength = sort.sort(indexArray, numChanMinimum);
446 157 : sort.sort(indexArray, numChanMinimum);
447 157 : uInt startChan = indexArray[0] - halfWidth;
448 157 : uInt endChan = startChan + smoothBin_p;
449 157 : smoothCoeff_p.resize(smoothBin_p, false);
450 888 : for (uInt i = startChan, j = 0; i < endChan; ++i, ++j) {
451 731 : smoothCoeff_p[j] = kernelForMinimumNumChan[i];
452 : }
453 : // normalize smoothCoeff_p
454 157 : smoothCoeff_p /= sum(smoothCoeff_p);
455 157 : os << LogIO::DEBUGGING << "smoothBin_p = " << smoothBin_p << LogIO::POST;
456 157 : os << LogIO::DEBUGGING << "smoothCoeff_p = " << smoothCoeff_p << LogIO::POST;
457 157 : }
458 :
459 157 : Vector<Int> SDMSManager::inspectNumChan() {
460 314 : LogIO os(_ORIGIN);
461 :
462 157 : if (selectedInputMs_p == NULL) {
463 0 : throw AipsError("Input MS is not opened yet.");
464 : }
465 :
466 157 : ScalarColumn<Int> col(*selectedInputMs_p, "DATA_DESC_ID");
467 157 : Vector<Int> ddIdList = col.getColumn();
468 157 : uInt numDDId = GenSort<Int>::sort(ddIdList, Sort::Ascending,
469 : Sort::QuickSort | Sort::NoDuplicates);
470 157 : col.attach(selectedInputMs_p->dataDescription(), "SPECTRAL_WINDOW_ID");
471 157 : Vector<Int> spwIdList(numDDId);
472 334 : for (uInt i = 0; i < numDDId; ++i) {
473 177 : spwIdList[i] = col(ddIdList[i]);
474 : }
475 157 : Vector<Int> numChanList(numDDId);
476 157 : col.attach(selectedInputMs_p->spectralWindow(), "NUM_CHAN");
477 157 : os << LogIO::DEBUGGING << "spwIdList = " << spwIdList << LogIO::POST;
478 334 : for (size_t i = 0; i < spwIdList.nelements(); ++i) {
479 177 : Int spwId = spwIdList[i];
480 177 : Int numChan = col(spwId);
481 177 : numChanList[i] = numChan;
482 177 : os << LogIO::DEBUGGING << "examine spw " << spwId << ": nchan = " << numChan << LogIO::POST;
483 177 : if (numChan == 1) {
484 0 : stringstream ss;
485 0 : ss << "smooth: Failed due to wrong spw " << i;
486 0 : throw AipsError(ss.str());
487 0 : }
488 : }
489 :
490 157 : uInt numNumChan = GenSort<Int>::sort(numChanList, Sort::Ascending,
491 : Sort::QuickSort | Sort::NoDuplicates);
492 314 : return numChanList(Slice(0, numNumChan));
493 157 : }
494 :
495 : } // End of casa namespace.
|