source: trunk/src/SDPol.cc @ 437

Last change on this file since 437 was 437, checked in by kil064, 19 years ago

make it work for nPol=1,2 or 4

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1//#---------------------------------------------------------------------------
2//# SDPol.cc: Polarimetric functionality
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
5//# ATNF
6//#
7//# This program is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU General Public License as published by the Free
9//# Software Foundation; either version 2 of the License, or (at your option)
10//# any later version.
11//#
12//# This program is distributed in the hope that it will be useful, but
13//# WITHOUT ANY WARRANTY; without even the implied warranty of
14//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15//# Public License for more details.
16//#
17//# You should have received a copy of the GNU General Public License along
18//# with this program; if not, write to the Free Software Foundation, Inc.,
19//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning this software should be addressed as follows:
22//#        Internet email: Malte.Marquarding@csiro.au
23//#        Postal address: Malte Marquarding,
24//#                        Australia Telescope National Facility,
25//#                        P.O. Box 76,
26//#                        Epping, NSW, 2121,
27//#                        AUSTRALIA
28//#
29//# $Id:
30//#---------------------------------------------------------------------------
31
32#include "SDPol.h"
33#include "SDDefs.h"
34
35#include <casa/Arrays/Array.h>
36#include <casa/Arrays/ArrayMath.h>
37#include <casa/Containers/Record.h>
38#include <casa/BasicSL/Constants.h>
39#include <casa/BasicSL/String.h>
40#include <casa/Utilities/Assert.h>
41
42#include <tables/Tables/Table.h>
43#include <tables/Tables/ScalarColumn.h>
44#include <tables/Tables/ArrayColumn.h>
45#include <tables/Tables/ColumnDesc.h>
46#include <tables/Tables/TableRecord.h>
47#include <tables/Tables/DataManError.h>
48
49
50using namespace casa;
51using namespace asap;
52
53
54
55SDStokesEngine::SDStokesEngine (const String& outputColumnName,
56                           const String& inputColumnName)
57: BaseMappedArrayEngine<Float,Float> (outputColumnName, inputColumnName)
58{}
59
60
61SDStokesEngine::SDStokesEngine (const Record& spec)
62: BaseMappedArrayEngine<Float,Float> ()
63{
64    if (spec.isDefined("OUTPUTNAME")  &&  spec.isDefined("INPUTNAME")) {
65        setNames (spec.asString("OUTPUTNAME"), spec.asString("INPUTNAME"));
66    }
67}
68
69SDStokesEngine::SDStokesEngine (const SDStokesEngine& that)
70: BaseMappedArrayEngine<Float,Float> (that)
71{}
72
73SDStokesEngine::~SDStokesEngine()
74{}
75
76
77DataManager* SDStokesEngine::clone() const
78{
79    DataManager* dmPtr = new SDStokesEngine (*this);
80    return dmPtr;
81}
82
83
84String SDStokesEngine::dataManagerType() const
85{
86    return className();
87}
88
89String SDStokesEngine::className()
90{
91    return "SDStokesEngine";
92}
93
94String SDStokesEngine::dataManagerName() const
95{
96    return sourceName();
97}
98
99Record SDStokesEngine::dataManagerSpec() const
100{
101    Record spec;
102    spec.define ("OUTPUTNAME", sourceName());    // Ger uses opposite meaning for source/target
103    spec.define ("INPUTNAME", targetName());
104    return spec;
105}
106
107DataManager* SDStokesEngine::makeObject (const String&,
108                                                 const Record& spec)
109{
110    DataManager* dmPtr = new SDStokesEngine(spec);
111    return dmPtr;
112}
113
114
115void SDStokesEngine::registerClass()
116{
117    DataManager::registerCtor (className(), makeObject);
118}
119
120
121void SDStokesEngine::create (uInt initialNrrow)
122{
123    BaseMappedArrayEngine<Float,Float>::create (initialNrrow);
124}
125
126void SDStokesEngine::prepare()
127{
128    BaseMappedArrayEngine<Float,Float>::prepare();
129}
130
131Bool SDStokesEngine::canAccessArrayColumnCells (Bool& reask) const
132{
133    reask = False;
134    return True;
135}
136
137
138void SDStokesEngine::getArray (uInt rownr, Array<Float>& output)
139{
140    Array<Float> input;
141    roColumn().get(rownr, input);
142//
143    computeOnGet (output, input);
144}
145
146void SDStokesEngine::putArray (uInt rownr, const Array<Float>& input)
147{
148    throw(AipsError("This Virtual Column is not writable"));
149}
150
151
152
153
154   
155IPosition SDStokesEngine::shape (uInt rownr)
156{
157   IPosition inputShape = roColumn().shape (rownr);
158   return findOutputShape(inputShape);
159}
160
161
162
163void SDStokesEngine::computeOnGet(Array<Float>& output,
164                                 const Array<Float>& input)
165//
166// array of shape (nBeam,nIF,nPol,nChan)
167//
168// We use the scaling convention I=(XX+YY)
169//
170{
171
172// Checks
173
174   const uInt nDim = input.ndim();
175   AlwaysAssert(nDim==4,AipsError);
176   AlwaysAssert(output.ndim()==4,AipsError);
177//
178   const IPosition inputShape = input.shape();
179   const uInt polAxis = asap::PolAxis;
180   const uInt nPol = inputShape(polAxis);
181   AlwaysAssert(nPol==1 || nPol==2 || nPol==3, AipsError);
182
183// The silly Array slice operator does not give me back
184// a const reference so have to caste it away
185
186   Array<Float>& input2 = const_cast<Array<Float>&>(input);
187
188// Slice coordnates
189
190   IPosition start(nDim,0);
191   IPosition end(input.shape()-1);
192
193// Generate Slices
194
195   start(polAxis) = 0;
196   end(polAxis) = 0;
197   Array<Float> C1 = input2(start,end);          // Input : C1
198//
199   start(polAxis) = 0;
200   end(polAxis) = 0;
201   Array<Float> I = output(start,end);           // Output : I
202//
203   if (nPol==1) {
204      I = Float(2.0)*C1;
205      return;
206   }
207//
208   start(polAxis) = 1;
209   end(polAxis) = 1;
210   Array<Float> C2 = input2(start,end);          // Input : C1
211//
212   I = C1 + C2;
213   if (nPol <= 2) return;
214//
215   start(polAxis) = 2;
216   end(polAxis) = 2;
217   Array<Float> C3 = input2(start,end);          // Input : C3
218//
219   start(polAxis) = 3;
220   end(polAxis) = 3;
221   Array<Float> C4 = input2(start,end);          // Input : C4
222//
223   start(polAxis) = 1;
224   end(polAxis) = 1;
225   Array<Float> Q = output(start,end);           // Output : Q
226   Q = C1 - C2;
227//
228   start(polAxis) = 2;
229   end(polAxis) = 2;
230   Array<Float> U = output(start,end);           // Output : U
231   U = Float(2.0)*C3;
232//
233   start(polAxis) = 3;
234   end(polAxis) = 3;
235   Array<Float> V = output(start,end);           // Output : V
236   V = Float(2.0)*C4;
237}
238
239
240
241IPosition SDStokesEngine::findOutputShape (const IPosition& inputShape) const
242{
243   uInt axis = 2;
244   uInt nPol = inputShape(axis);
245   IPosition outputShape = inputShape;
246   if (nPol==1) {
247      outputShape(axis) = 1;            // XX -> I
248   } else if (nPol==2) {
249      outputShape(axis) = 1;            // XX YY -> I
250   } else if (nPol==4) {
251      outputShape(axis) = 4;            // XX YY R(XY) I(XY) -> I Q U V
252   }
253   return outputShape;
254}
255
256
257
258// SDPolUtil
259
260Array<Float> SDPolUtil::polarizedIntensity (const Array<Float>& Q,
261                                            const Array<Float>& U)
262{
263   Array<Float> t1 = pow(Q,Double(2.0));
264   Array<Float> t2 = pow(U,Double(2.0));
265   return sqrt(t1+t2);
266}
267
268
269Array<Float> SDPolUtil::positionAngle (const Array<Float>& Q,
270                                       const Array<Float>& U)
271{
272   return Float(180.0/C::pi/2.0)*atan2(Q,U);       // Degrees
273}
274
275
276void SDPolUtil::rotateXYPhase (Array<Float>& C3,
277                               Array<Float>& C4,
278                               Float phase)
279{
280   Float cosVal = cos(C::pi/180.0*phase);
281   Float sinVal = sin(C::pi/180.0*phase);
282//
283   C3 = C3*cosVal - C4*sinVal;
284   C4 = C3*sinVal + C4*cosVal;
285}
286
287
288
289Array<Float> SDPolUtil::getStokesSlice (Array<Float>& in, const IPosition& start,
290                                        const IPosition& end, const String& stokes)
291{
292   IPosition s(start);
293   IPosition e(end);
294//
295   if (stokes=="I") {
296      s(asap::PolAxis) = 0;
297      e(asap::PolAxis) = 0;
298   } else if (stokes=="Q") {
299      s(asap::PolAxis) = 1;
300      e(asap::PolAxis) = 1;
301   } else if (stokes=="U") {
302      s(asap::PolAxis) = 2;
303      e(asap::PolAxis) = 2;
304   } else if (stokes=="V") {
305      s(asap::PolAxis) = 3;
306      e(asap::PolAxis) = 3;
307   }
308//
309   return in(s,e);
310}
311 
312
313Array<Float> SDPolUtil::circularPolarizationFromStokes (Array<Float>& I,
314                                                        Array<Float>& V,
315                                                        Bool doRR)
316{
317   if (doRR) {
318      return Float(0.5)*(I+V);
319   } else {
320      return Float(0.5)*(I-V);
321   }
322}
323
Note: See TracBrowser for help on using the repository browser.