Line data Source code
1 : //# PhaseShiftingTVI.h: This file contains the implementation of the PhaseShiftingTVI class.
2 : //#
3 : //# CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
4 : //# Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
5 : //# Copyright (C) European Southern Observatory, 2011, All rights reserved.
6 : //#
7 : //# This library is free software; you can redistribute it and/or
8 : //# modify it under the terms of the GNU Lesser General Public
9 : //# License as published by the Free software Foundation; either
10 : //# version 2.1 of the License, or (at your option) any later version.
11 : //#
12 : //# This library is distributed in the hope that it will be useful,
13 : //# but WITHOUT ANY WARRANTY, without even the implied warranty of
14 : //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : //# Lesser General Public License for more details.
16 : //#
17 : //# You should have received a copy of the GNU Lesser General Public
18 : //# License along with this library; if not, write to the Free Software
19 : //# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 : //# MA 02111-1307 USA
21 : //# $Id: $
22 :
23 : #include <mstransform/TVI/PhaseShiftingTVI.h>
24 :
25 : using namespace casacore;
26 : namespace casa { //# NAMESPACE CASA - BEGIN
27 :
28 : namespace vi { //# NAMESPACE VI - BEGIN
29 :
30 : //////////////////////////////////////////////////////////////////////////
31 : // PhaseShiftingTVI class
32 : //////////////////////////////////////////////////////////////////////////
33 :
34 : // -----------------------------------------------------------------------
35 : //
36 : // -----------------------------------------------------------------------
37 79 : PhaseShiftingTVI::PhaseShiftingTVI( ViImplementation2 * inputVii,
38 79 : const Record &configuration):
39 79 : FreqAxisTVI (inputVii)
40 : {
41 79 : dx_p = 0;
42 79 : dy_p = 0;
43 :
44 : // CAS-12706 Zero initialization for wide-field phase shifting algorithm
45 79 : wideFieldMode_p = false;
46 79 : phaseCenterName_p = "";
47 79 : selectedInputMsCols_p = NULL;
48 :
49 : // Parse and check configuration parameters
50 : // Note: if a constructor finishes by throwing an exception, the memory
51 : // associated with the object itself is cleaned up — there is no memory leak.
52 79 : if (not parseConfiguration(configuration))
53 : {
54 0 : throw AipsError("Error parsing PhaseShiftingTVI configuration");
55 : }
56 :
57 79 : initialize();
58 :
59 79 : return;
60 0 : }
61 :
62 : // -----------------------------------------------------------------------
63 : //
64 : // -----------------------------------------------------------------------
65 79 : Bool PhaseShiftingTVI::parseConfiguration(const Record &configuration)
66 : {
67 79 : int exists = -1;
68 79 : Bool ret = true;
69 :
70 79 : exists = -1;
71 79 : exists = configuration.fieldNumber ("XpcOffset");
72 79 : if (exists >= 0)
73 : {
74 0 : configuration.get (exists, dx_p);
75 : }
76 :
77 79 : exists = -1;
78 79 : exists = configuration.fieldNumber ("YpcOffset");
79 79 : if (exists >= 0)
80 : {
81 0 : configuration.get (exists, dy_p);
82 : }
83 :
84 79 : if (abs(dx_p) > 0 or abs(dy_p) > 0)
85 : {
86 0 : logger_p << LogIO::NORMAL << LogOrigin("PhaseShiftingTVI", __FUNCTION__)
87 0 : << "Phase shift is dx="<< dx_p << " dy=" << dy_p << LogIO::POST;
88 : }
89 :
90 : // CAS-12706 Add support for shifting across large offset/angles
91 79 : exists = -1;
92 79 : exists = configuration.fieldNumber ("phasecenter");
93 79 : if (exists >= 0)
94 : {
95 79 : configuration.get (exists, phaseCenterName_p);
96 : // casaMDirection requires a variant
97 79 : casac::variant phaseCenterVar(phaseCenterName_p);
98 :
99 79 : if(!casaMDirection(phaseCenterVar, phaseCenter_p))
100 : {
101 0 : logger_p << LogIO::SEVERE << LogOrigin("PhaseShiftingTVI", __FUNCTION__)
102 0 : << "Cannot interpret phase center " << phaseCenterName_p << LogIO::POST;
103 0 : ret = false;
104 : }
105 : else
106 : {
107 : MDirection::Types mdtype;
108 79 : const auto myFrame = phaseCenter_p.getRefString();
109 79 : MDirection::getType(mdtype, myFrame);
110 79 : ThrowIf(
111 : mdtype == MDirection::HADEC || mdtype == MDirection::AZEL
112 : || mdtype == MDirection::AZELSW || mdtype == MDirection::AZELNE
113 : || mdtype == MDirection::AZELGEO || mdtype == MDirection::AZELSWGEO
114 : || mdtype == MDirection::MECLIPTIC || mdtype == MDirection::TECLIPTIC
115 : || mdtype == MDirection::TOPO,
116 : myFrame + " is a time dependent reference frame and so is not supported"
117 : );
118 79 : ThrowIf(
119 : mdtype == MDirection::MERCURY || mdtype == MDirection::VENUS
120 : || mdtype == MDirection::MARS || mdtype == MDirection::JUPITER
121 : || mdtype == MDirection::SATURN || mdtype == MDirection::URANUS
122 : || mdtype == MDirection::NEPTUNE || mdtype == MDirection::PLUTO
123 : || mdtype == MDirection::SUN || mdtype == MDirection::MOON
124 : || mdtype == MDirection::COMET,
125 : myFrame + " denotes an ephemeris object and so is not supported"
126 : );
127 79 : wideFieldMode_p = true;
128 158 : logger_p << LogIO::NORMAL << LogOrigin("PhaseShiftingTVI", __FUNCTION__)
129 158 : << "Phase center " << phaseCenterName_p << " successfully parsed"<< LogIO::POST;
130 79 : }
131 79 : }
132 :
133 79 : return ret;
134 : }
135 :
136 : // -----------------------------------------------------------------------
137 : //
138 : // -----------------------------------------------------------------------
139 79 : void PhaseShiftingTVI::initialize()
140 : {
141 : // Populate nchan input-output maps
142 : Int spw;
143 79 : uInt spw_idx = 0;
144 79 : map<Int,vector<Int> >::iterator iter;
145 160 : for(iter=spwInpChanIdxMap_p.begin();iter!=spwInpChanIdxMap_p.end();iter++)
146 : {
147 81 : spw = iter->first;
148 81 : spwOutChanNumMap_p[spw] = spwInpChanIdxMap_p[spw].size();
149 :
150 81 : spw_idx++;
151 : }
152 :
153 : // CAS-12706 Add support for shifting across large offset/angles
154 : // Access observatory position and observation start (reference) time.
155 79 : if (wideFieldMode_p)
156 : {
157 79 : selectedInputMsCols_p = new MSColumns(getVii()->ms());
158 79 : observatoryPosition_p = selectedInputMsCols_p->antenna().positionMeas()(0);
159 79 : referenceTime_p = selectedInputMsCols_p->timeMeas()(0);
160 79 : referenceTimeUnits_p = selectedInputMsCols_p->timeQuant()(0).getUnit();
161 : }
162 :
163 158 : return;
164 : }
165 :
166 : // -----------------------------------------------------------------------
167 : // CAS-12706 Recalculate UVW and Phases according to wide-field phase shifting
168 : // algorithm. This method should do the same as ComponentFTMachine::rotateUVW
169 : // -----------------------------------------------------------------------
170 2231 : void PhaseShiftingTVI::shiftUVWPhases()
171 : {
172 : // Get input VisBuffer
173 2231 : VisBuffer2 *vb = getVii()->getVisBuffer();
174 :
175 2231 : auto convertedPhaseCenter = phaseCenter_p;
176 2231 : if (phaseCenter_p.getRefString() != vb->phaseCenter().getRefString()) {
177 : MDirection::Types mdtype;
178 912 : MDirection::getType(mdtype, vb->phaseCenter().getRefString());
179 912 : convertedPhaseCenter = MDirection::Convert(phaseCenter_p, mdtype)();
180 : }
181 :
182 : // Initialize epoch corresponding to current buffer
183 : // with time reference to the first row in the MS
184 4462 : MEpoch epoch(Quantity(vb->time()(0),referenceTimeUnits_p),referenceTime_p.getRef());
185 2231 : MeasFrame refFrame(epoch,observatoryPosition_p);
186 2231 : UVWMachine uvwMachine(convertedPhaseCenter, vb->phaseCenter(), refFrame,false,false);
187 : // Initialize phase array and uvw matrix
188 2231 : phaseShift_p.resize(vb->nRows(),false);
189 2231 : newUVW_p.resize(vb->uvw().shape(),false);
190 :
191 : // Obtain phase shift and new uvw coordinates
192 2231 : Vector<Double> dummy(3,0.0);
193 2231 : double phase2radPerHz = -2.0 * C::pi / C::c;
194 693465 : for (uInt row=0;row<vb->nRows();row++)
195 : {
196 : // Copy current uvw coordinates so that they are not modified
197 : // Note: Columns in uvw correspond to rows in the main table/VisBuffer!
198 691234 : dummy = vb->uvw().column(row);
199 :
200 : // Have to change (u,v,w) to (-u,-v,w) because is the convention used by uvwMachine
201 691234 : dummy(0) = -1*dummy(0);
202 691234 : dummy(1) = -1*dummy(1);
203 :
204 : // Transform uvw coordinates and obtain corresponding phase shift
205 691234 : uvwMachine.convertUVW(phaseShift_p(row), dummy);
206 :
207 : // Store new uvw coordinates
208 : // Have to change back (-u,-v,w) to (u,v,w) because is the convention used by the MS
209 691234 : dummy(0) = -1*dummy(0);
210 691234 : dummy(1) = -1*dummy(1);
211 691234 : newUVW_p.column(row) = dummy;
212 : // Convert phase shift to radian/Hz
213 691234 : phaseShift_p(row) = phase2radPerHz*phaseShift_p(row);
214 : }
215 4462 : return;
216 2231 : }
217 :
218 : // -----------------------------------------------------------------------
219 : //
220 : // -----------------------------------------------------------------------
221 136 : void PhaseShiftingTVI::origin()
222 : {
223 : // Drive underlying ViImplementation2
224 136 : getVii()->origin();
225 :
226 : // CAS-12706 Add support for shifting across large offset/angles
227 136 : if (wideFieldMode_p) shiftUVWPhases();
228 :
229 : // Define the shapes in the VB2, patch provided by cgarcia in CAS-12706
230 136 : configureShapes();
231 :
232 : // Synchronize own VisBuffer
233 136 : configureNewSubchunk();
234 :
235 136 : return;
236 : }
237 :
238 : // -----------------------------------------------------------------------
239 : //
240 : // -----------------------------------------------------------------------
241 2095 : void PhaseShiftingTVI::next()
242 : {
243 : // Drive underlying ViImplementation2
244 2095 : getVii()->next();
245 :
246 : // CAS-12706 Add support for shifting across large offset/angles
247 2095 : if (wideFieldMode_p) shiftUVWPhases();
248 :
249 : // Define the shapes in the VB2, patch provided by cgarcia in CAS-12706
250 2095 : configureShapes();
251 :
252 : // Synchronize own VisBuffer
253 2095 : configureNewSubchunk();
254 :
255 2095 : return;
256 : }
257 :
258 :
259 : // -----------------------------------------------------------------------
260 : //
261 : // -----------------------------------------------------------------------
262 2095 : void PhaseShiftingTVI::visibilityObserved (Cube<Complex> & vis) const
263 : {
264 : // Get input VisBuffer
265 2095 : VisBuffer2 *vb = getVii()->getVisBuffer();
266 2095 : Matrix<Double> uvw = vb->uvw();
267 2095 : Vector<Double> frequencies = vb->getFrequencies(0);
268 :
269 : // Reshape output data before passing it to the DataCubeHolder
270 2095 : vis.resize(getVisBuffer()->getShape(),false);
271 :
272 : // Gather input data
273 2095 : DataCubeMap inputData;
274 2095 : DataCubeHolder<Complex> inputVisCubeHolder(vb->visCube());
275 2095 : inputData.add(MS::DATA,inputVisCubeHolder);
276 :
277 : // Gather output data
278 2095 : DataCubeMap outputData;
279 2095 : DataCubeHolder<Complex> outputVisCubeHolder(vis);
280 2095 : outputData.add(MS::DATA,outputVisCubeHolder);
281 :
282 2095 : if (wideFieldMode_p)
283 : {
284 : // Configure Transformation Engine
285 2095 : WideFieldPhaseShiftingTransformEngine<Complex> transformer(phaseShift_p,&uvw,&frequencies,&inputData,&outputData);
286 :
287 : // Transform data
288 2095 : transformFreqAxis2(vb->getShape(),transformer);
289 2095 : }
290 : else
291 : {
292 : // Configure Transformation Engine
293 0 : PhaseShiftingTransformEngine<Complex> transformer(dx_p,dy_p,&uvw,&frequencies,&inputData,&outputData);
294 :
295 : // Transform data
296 0 : transformFreqAxis2(vb->getShape(),transformer);
297 : }
298 :
299 4190 : return;
300 2095 : }
301 :
302 : // -----------------------------------------------------------------------
303 : //
304 : // -----------------------------------------------------------------------
305 1757 : void PhaseShiftingTVI::visibilityCorrected (Cube<Complex> & vis) const
306 : {
307 : // Get input VisBuffer
308 1757 : VisBuffer2 *vb = getVii()->getVisBuffer();
309 1757 : Matrix<Double> uvw = vb->uvw();
310 1757 : Vector<Double> frequencies = vb->getFrequencies(0);
311 :
312 : // Reshape output data before passing it to the DataCubeHolder
313 1757 : vis.resize(getVisBuffer()->getShape(),false);
314 :
315 : // Gather input data
316 1757 : DataCubeMap inputData;
317 1757 : DataCubeHolder<Complex> inputVisCubeHolder(vb->visCubeCorrected());
318 1757 : inputData.add(MS::DATA,inputVisCubeHolder);
319 :
320 : // Gather output data
321 1757 : DataCubeMap outputData;
322 1757 : DataCubeHolder<Complex> outputVisCubeHolder(vis);
323 1757 : outputData.add(MS::DATA,outputVisCubeHolder);
324 :
325 1757 : if (wideFieldMode_p)
326 : {
327 : // Configure Transformation Engine
328 1757 : WideFieldPhaseShiftingTransformEngine<Complex> transformer(phaseShift_p,&uvw,&frequencies,&inputData,&outputData);
329 :
330 : // Transform data
331 1757 : transformFreqAxis2(vb->getShape(),transformer);
332 1757 : }
333 : else
334 : {
335 : // Configure Transformation Engine
336 0 : PhaseShiftingTransformEngine<Complex> transformer(dx_p,dy_p,&uvw,&frequencies,&inputData,&outputData);
337 :
338 : // Transform data
339 0 : transformFreqAxis2(vb->getShape(),transformer);
340 : }
341 :
342 3514 : return;
343 1757 : }
344 :
345 : // -----------------------------------------------------------------------
346 : //
347 : // -----------------------------------------------------------------------
348 1757 : void PhaseShiftingTVI::visibilityModel (Cube<Complex> & vis) const
349 : {
350 : // Get input VisBuffer
351 1757 : VisBuffer2 *vb = getVii()->getVisBuffer();
352 1757 : Matrix<Double> uvw = vb->uvw();
353 1757 : Vector<Double> frequencies = vb->getFrequencies(0);
354 :
355 : // Reshape output data before passing it to the DataCubeHolder
356 1757 : vis.resize(getVisBuffer()->getShape(),false);
357 :
358 : // Gather input data
359 1757 : DataCubeMap inputData;
360 1757 : DataCubeHolder<Complex> inputVisCubeHolder(vb->visCubeModel());
361 1757 : inputData.add(MS::DATA,inputVisCubeHolder);
362 :
363 : // Gather output data
364 1757 : DataCubeMap outputData;
365 1757 : DataCubeHolder<Complex> outputVisCubeHolder(vis);
366 1757 : outputData.add(MS::DATA,outputVisCubeHolder);
367 :
368 1757 : if (wideFieldMode_p)
369 : {
370 : // Configure Transformation Engine
371 1757 : WideFieldPhaseShiftingTransformEngine<Complex> transformer(phaseShift_p,&uvw,&frequencies,&inputData,&outputData);
372 :
373 : // Transform data
374 1757 : transformFreqAxis2(vb->getShape(),transformer);
375 1757 : }
376 : else
377 : {
378 : // Configure Transformation Engine
379 0 : PhaseShiftingTransformEngine<Complex> transformer(dx_p,dy_p,&uvw,&frequencies,&inputData,&outputData);
380 :
381 : // Transform data
382 0 : transformFreqAxis2(vb->getShape(),transformer);
383 : }
384 :
385 3514 : return;
386 1757 : }
387 :
388 : // -----------------------------------------------------------------------
389 : //
390 : // -----------------------------------------------------------------------
391 2095 : void PhaseShiftingTVI::uvw (casacore::Matrix<double> & uvw) const
392 : {
393 2095 : if (wideFieldMode_p)
394 : {
395 2095 : uvw.resize(newUVW_p.shape(),false);
396 2095 : uvw = newUVW_p;
397 : }
398 : else
399 : {
400 0 : getVii()->uvw (uvw);
401 : }
402 :
403 2095 : return;
404 : }
405 :
406 : //////////////////////////////////////////////////////////////////////////
407 : // PhaseShiftingTVIFactory class
408 : //////////////////////////////////////////////////////////////////////////
409 :
410 : // -----------------------------------------------------------------------
411 : //
412 : // -----------------------------------------------------------------------
413 0 : PhaseShiftingTVIFactory::PhaseShiftingTVIFactory ( Record &configuration,
414 0 : ViImplementation2 *inputVii)
415 : {
416 0 : inputVii_p = inputVii;
417 0 : configuration_p = configuration;
418 0 : }
419 :
420 : // -----------------------------------------------------------------------
421 : //
422 : // -----------------------------------------------------------------------
423 0 : vi::ViImplementation2 * PhaseShiftingTVIFactory::createVi(VisibilityIterator2 *) const
424 : {
425 0 : return new PhaseShiftingTVI(inputVii_p,configuration_p);
426 : }
427 :
428 : // -----------------------------------------------------------------------
429 : //
430 : // -----------------------------------------------------------------------
431 0 : vi::ViImplementation2 * PhaseShiftingTVIFactory::createVi() const
432 : {
433 0 : return new PhaseShiftingTVI(inputVii_p,configuration_p);
434 : }
435 :
436 : //////////////////////////////////////////////////////////////////////////
437 : // PhaseShiftingTVILayerFactory class
438 : //////////////////////////////////////////////////////////////////////////
439 :
440 79 : PhaseShiftingTVILayerFactory::PhaseShiftingTVILayerFactory(Record &configuration) :
441 : ViiLayerFactory(),
442 79 : configuration_p(configuration)
443 79 : {}
444 :
445 : ViImplementation2*
446 79 : PhaseShiftingTVILayerFactory::createInstance(ViImplementation2* vii0) const
447 : {
448 : // Make the PhaseShiftingTVi2, using supplied ViImplementation2, and return it
449 79 : ViImplementation2 *vii = new PhaseShiftingTVI(vii0,configuration_p);
450 79 : return vii;
451 : }
452 :
453 : //////////////////////////////////////////////////////////////////////////
454 : // PhaseShiftingTransformEngine class
455 : //////////////////////////////////////////////////////////////////////////
456 :
457 : // -----------------------------------------------------------------------
458 : //
459 : // -----------------------------------------------------------------------
460 0 : template<class T> PhaseShiftingTransformEngine<T>::PhaseShiftingTransformEngine(
461 : Double dx, Double dy,
462 : Matrix<Double> *uvw,
463 : Vector<Double> *frequencies,
464 : DataCubeMap *inputData,
465 : DataCubeMap *outputData):
466 0 : FreqAxisTransformEngine2<T>(inputData,outputData)
467 : {
468 0 : uvw_p = uvw;
469 0 : frequencies_p = frequencies;
470 :
471 : // Offsets in radians (input is arcsec)
472 0 : dx_p = dx*(C::pi / 180.0 / 3600.0);
473 0 : dy_p = dy*(C::pi / 180.0 / 3600.0);
474 0 : }
475 :
476 : // -----------------------------------------------------------------------
477 : //
478 : // -----------------------------------------------------------------------
479 0 : template<class T> void PhaseShiftingTransformEngine<T>::transform( )
480 : {
481 0 : transformCore(inputData_p,outputData_p);
482 0 : }
483 :
484 : // -----------------------------------------------------------------------
485 : //
486 : // -----------------------------------------------------------------------
487 0 : template<class T> void PhaseShiftingTransformEngine<T>::transformCore( DataCubeMap *inputData,
488 : DataCubeMap *outputData)
489 : {
490 : // Get input/output data
491 0 : Vector<T> &inputVector = inputData->getVector<T>(MS::DATA);
492 0 : Vector<T> &outputVector = outputData->getVector<T>(MS::DATA);
493 :
494 : // Extra path as fraction of U and V in m
495 0 : Double phase = dx_p*(*uvw_p)(0,rowIndex_p) + dy_p*(*uvw_p)(1,rowIndex_p);
496 :
497 : // In radian/Hz
498 0 : phase *= -2.0 * C::pi / C::c;
499 :
500 : // Main loop
501 : Double phase_i;
502 0 : Complex factor;
503 0 : for (uInt chan_i=0;chan_i<inputVector.size();chan_i++)
504 : {
505 0 : phase_i = phase * (*frequencies_p)(chan_i);
506 0 : factor = Complex(cos(phase_i), sin(phase_i));
507 0 : outputVector(chan_i) = factor*inputVector(chan_i);
508 : }
509 0 : }
510 :
511 : //////////////////////////////////////////////////////////////////////////
512 : // WideFieldPhaseShiftingTransformEngine class
513 : //////////////////////////////////////////////////////////////////////////
514 :
515 : // -----------------------------------------------------------------------
516 : //
517 : // -----------------------------------------------------------------------
518 5609 : template<class T> WideFieldPhaseShiftingTransformEngine<T>::WideFieldPhaseShiftingTransformEngine(
519 : const Vector<Double> &phaseShift,
520 : Matrix<Double> *uvw,
521 : Vector<Double> *frequencies,
522 : DataCubeMap *inputData,
523 : DataCubeMap *outputData):
524 5609 : FreqAxisTransformEngine2<T>(inputData,outputData)
525 : {
526 5609 : uvw_p = uvw;
527 5609 : frequencies_p = frequencies;
528 5609 : phaseShift_p = phaseShift;
529 5609 : }
530 :
531 : // -----------------------------------------------------------------------
532 : //
533 : // -----------------------------------------------------------------------
534 3780720 : template<class T> void WideFieldPhaseShiftingTransformEngine<T>::transform( )
535 : {
536 3780720 : transformCore(inputData_p,outputData_p);
537 3780720 : }
538 :
539 : // -----------------------------------------------------------------------
540 : //
541 : // -----------------------------------------------------------------------
542 3780720 : template<class T> void WideFieldPhaseShiftingTransformEngine<T>::transformCore( DataCubeMap *inputData,
543 : DataCubeMap *outputData)
544 : {
545 : // Get input/output data
546 3780720 : Vector<T> &inputVector = inputData->getVector<T>(MS::DATA);
547 3780720 : Vector<T> &outputVector = outputData->getVector<T>(MS::DATA);
548 :
549 : // Main loop
550 : Double phase_i;
551 3780720 : Complex factor;
552 11135172 : for (uInt chan_i=0;chan_i<inputVector.size();chan_i++)
553 : {
554 7354452 : phase_i = phaseShift_p(rowIndex_p) * (*frequencies_p)(chan_i);
555 7354452 : factor = Complex(cos(phase_i), sin(phase_i));
556 7354452 : outputVector(chan_i) = factor*inputVector(chan_i);
557 : }
558 3780720 : }
559 :
560 :
561 : } //# NAMESPACE VI - END
562 :
563 : } //# NAMESPACE CASA - END
564 :
565 :
|