source: trunk/src/SDContainer.cc @ 344

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

add return True to setSpectrum

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1//#---------------------------------------------------------------------------
2//# SDContainer.cc: A container class for single dish integrations
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#include <casa/aips.h>
32#include <casa/iostream.h>
33#include <casa/iomanip.h>
34#include <casa/Exceptions.h>
35#include <tables/Tables/Table.h>
36#include <casa/Arrays/IPosition.h>
37#include <casa/Arrays/Matrix.h>
38#include <casa/Arrays/ArrayAccessor.h>
39#include <casa/BasicMath/Math.h>
40#include <casa/Quanta/MVTime.h>
41
42
43#include "SDDefs.h"
44#include "SDContainer.h"
45
46using namespace casa;
47using namespace asap;
48
49void SDHeader::print() const {
50  MVTime mvt(this->utc);
51  mvt.setFormat(MVTime::YMD);
52  cout << "Observer: " << this->observer << endl
53       << "Project: " << this->project << endl
54       << "Obstype: " << this->obstype << endl
55       << "Antenna: " << this->antennaname << endl
56       << "Ant. Position: " << this->antennaposition << endl
57       << "Equinox: " << this->equinox << endl
58       << "Freq. ref.: " << this->freqref << endl
59       << "Ref. frequency: " << this->reffreq << endl
60       << "Bandwidth: "  << this->bandwidth << endl
61       << "Time (utc): "
62       << mvt
63       << endl;
64  //setprecision(10) << this->utc << endl;
65}
66
67
68SDContainer::SDContainer(uInt nBeam, uInt nIF, uInt nPol, uInt nChan)
69  : nBeam_(nBeam),
70    nIF_(nIF),
71    nPol_(nPol),
72    nChan_(nChan),
73    spectrum_(IPosition(4,nBeam,nIF,nPol,nChan)),
74    flags_(IPosition(4,nBeam,nIF,nPol,nChan)),
75    tsys_(IPosition(4,nBeam,nIF,nPol,nChan)),
76    freqidx_(nIF),
77    direction_(IPosition(2,nBeam,2)) {
78  uChar x = 0;
79  flags_ = ~x;
80  tcal.resize(2);
81}
82
83SDContainer::SDContainer(IPosition shp)
84  : nBeam_(shp(0)),
85    nIF_(shp(1)),
86    nPol_(shp(2)),
87    nChan_(shp(3)),
88    spectrum_(shp),
89    flags_(shp),
90    tsys_(shp),
91    freqidx_(shp(1)) {
92  IPosition ip(2,shp(0),2);
93  direction_.resize(ip);
94  uChar x = 0;
95  flags_ = ~x;
96  tcal.resize(2);
97}
98
99SDContainer::~SDContainer() {
100}
101
102Bool SDContainer::resize(IPosition shp) {
103  nBeam_ = shp(0);
104  nIF_ = shp(1);
105  nPol_ = shp(2);
106  nChan_ = shp(3);
107  spectrum_.resize(shp);
108  flags_.resize(shp);
109  tsys_.resize(shp);
110  freqidx_.resize(shp(1));
111  IPosition ip(2,shp(0),2);
112  direction_.resize(ip);
113}
114
115Bool SDContainer::putSpectrum(const Array<Float>& spec) {
116  spectrum_ = spec;
117}
118Bool SDContainer::putFlags(const Array<uChar>& flag) {
119  flags_ = flag;
120}
121Bool SDContainer::putTsys(const Array<Float>& tsys) {
122  tsys_ = tsys;
123}
124
125Bool SDContainer::setSpectrum(const Matrix<Float>& spec,
126                              uInt whichBeam, uInt whichIF) {
127
128  ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(spectrum_);
129  aa0.reset(aa0.begin(whichBeam));
130  ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
131  aa1.reset(aa1.begin(whichIF));
132 
133  //Vector<Float> pols(nPol);
134  ArrayAccessor<Float, Axis<asap::IFAxis> > j(spec);
135  IPosition shp0 = spectrum_.shape();
136  IPosition shp1 = spec.shape();
137  if ( (shp0(2) != shp1(1)) || (shp0(3) != shp1(0)) ) {
138    throw(AipsError("Arrays not conformant"));
139    return False;
140  }
141  // assert dimensions are the same....
142  for (ArrayAccessor<Float, Axis<asap::PolAxis> > i(aa1);i != i.end(); ++i) {
143    ArrayAccessor<Float, Axis<asap::BeamAxis> > jj(j);
144    for (ArrayAccessor<Float, Axis<asap::ChanAxis> > ii(i);ii != ii.end(); ++ii) {
145      (*ii) = (*jj);
146      jj++;
147    }
148    j++;
149  }
150  // unset flags for this spectrum, they might be set again by the
151  // setFlags method
152
153  IPosition shp = flags_.shape();
154  IPosition start(4,whichBeam,whichIF,0,0);
155  IPosition end(4,whichBeam,whichIF,shp(2)-1,shp(3)-1);
156  Array<uChar> arr(flags_(start,end));
157  arr = uChar(0);
158//
159  return True;
160}
161
162Bool SDContainer::setFlags(const Matrix<uChar>& flag,
163                           uInt whichBeam, uInt whichIF) {
164
165  ArrayAccessor<uChar, Axis<asap::BeamAxis> > aa0(flags_);
166  aa0.reset(aa0.begin(whichBeam));
167  ArrayAccessor<uChar, Axis<asap::IFAxis> > aa1(aa0);
168  aa1.reset(aa1.begin(whichIF));
169 
170  ArrayAccessor<uChar, Axis<asap::IFAxis> > j(flag);
171  IPosition shp0 = flags_.shape();
172  IPosition shp1 = flag.shape();
173  if ( (shp0(2) != shp1(1)) || (shp0(3) != shp1(0)) ) {
174    cerr << "Arrays not conformant" << endl;     
175    return False;
176  }
177
178  // assert dimensions are the same....
179  for (ArrayAccessor<uChar, Axis<asap::PolAxis> > i(aa1);i != i.end(); ++i) {
180    ArrayAccessor<uChar, Axis<asap::BeamAxis> > jj(j);
181    for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > ii(i);ii != ii.end(); ++ii) {
182      (*ii) = (*jj);
183      jj++;
184    }
185    j++;
186  }
187  return True;
188}
189
190Bool SDContainer::setTsys(const Vector<Float>& tsys,
191                          uInt whichBeam, uInt whichIF) {
192  ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(tsys_);
193  aa0.reset(aa0.begin(whichBeam));
194  ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
195  aa1.reset(aa1.begin(whichIF));
196  // assert dimensions are the same....
197  for (ArrayAccessor<Float, Axis<asap::ChanAxis> > i(aa1);i != i.end(); ++i) {   
198    ArrayAccessor<Float, Axis<asap::BeamAxis> > j(tsys);
199    for (ArrayAccessor<Float, Axis<asap::PolAxis> > ii(i);ii != ii.end(); ++ii) {
200      (*ii) = (*j);
201      j++;
202    }
203  }
204}
205
206Array<Float> SDContainer::getSpectrum(uInt whichBeam, uInt whichIF) const
207{
208  Matrix<Float> spectra(nChan_, nPol_);
209
210  // Beam.
211  ArrayAccessor<Float, Axis<asap::BeamAxis> > i0(spectrum_);
212  i0.reset(i0.begin(whichBeam));
213
214  // IF.
215  ArrayAccessor<Float, Axis<asap::IFAxis> > i1(i0);
216  i1.reset(i1.begin(whichIF));
217
218  // Polarization.
219  ArrayAccessor<Float, Axis<asap::PolAxis> > i2(i1);
220  ArrayAccessor<Float, Axis<asap::IFAxis> > o1(spectra);
221
222  while (i2 != i2.end()) {
223    // Channel.
224    ArrayAccessor<Float, Axis<asap::ChanAxis> > i3(i2);
225    ArrayAccessor<Float, Axis<asap::BeamAxis> > o0(o1);
226
227    while (i3 != i3.end()) {
228      *o0 = *i3;
229
230      i3++;
231      o0++;
232    }
233
234    i2++;
235    o1++;
236  }
237
238  return spectra.copy();
239}
240
241
242Array<uChar> SDContainer::getFlags(uInt whichBeam, uInt whichIF) const
243{
244  Matrix<uChar> flagtra(nChan_, nPol_);
245
246  // Beam.
247  ArrayAccessor<uChar, Axis<asap::BeamAxis> > i0(flags_);
248  i0.reset(i0.begin(whichBeam));
249
250  // IF.
251  ArrayAccessor<uChar, Axis<asap::IFAxis> > i1(i0);
252  i1.reset(i1.begin(whichIF));
253
254  // Polarization.
255  ArrayAccessor<uChar, Axis<asap::PolAxis> > i2(i1);
256  ArrayAccessor<uChar, Axis<asap::IFAxis> > o1(flagtra);
257
258  while (i2 != i2.end()) {
259    // Channel.
260    ArrayAccessor<uChar, Axis<asap::ChanAxis> > i3(i2);
261    ArrayAccessor<uChar, Axis<asap::BeamAxis> > o0(o1);
262
263    while (i3 != i3.end()) {
264      *o0 = *i3;
265
266      i3++;
267      o0++;
268    }
269
270    i2++;
271    o1++;
272  }
273
274  return flagtra.copy();
275}
276
277Array<Float> SDContainer::getTsys(uInt whichBeam, uInt whichIF) const
278{
279  Vector<Float> tsys(nPol_);
280
281  // Beam.
282  ArrayAccessor<Float, Axis<asap::BeamAxis> > i0(tsys_);
283  i0.reset(i0.begin(whichBeam));
284
285  // IF.
286  ArrayAccessor<Float, Axis<asap::IFAxis> > i1(i0);
287  i1.reset(i1.begin(whichIF));
288
289  // Channel.
290  ArrayAccessor<Float, Axis<asap::ChanAxis> > i3(i1);
291
292  // Polarization.
293  ArrayAccessor<Float, Axis<asap::PolAxis> > i2(i3);
294  ArrayAccessor<Float, Axis<asap::BeamAxis> > o0(tsys);
295
296  while (i2 != i2.end()) {
297    *o0 = *i2;
298
299    i2++;
300    o0++;
301  }
302  return tsys.copy();
303}
304
305Array<Double> SDContainer::getDirection(uInt whichBeam) const {
306  Vector<Double> direct(2);
307  ArrayAccessor<Double, Axis<asap::BeamAxis> > i0(direction_);
308  i0.reset(i0.begin(whichBeam));
309  ArrayAccessor<Double, Axis<asap::BeamAxis> > o0(direct);
310  ArrayAccessor<Double, Axis<asap::IFAxis> > i1(i0);
311  while (i1 != i1.end()) {
312    *o0 = *i1;
313    i1++;
314    o0++;
315  } 
316  return direct.copy();
317}
318
319
320Bool SDContainer::setFrequencyMap(uInt freqslot, uInt whichIF) {
321  freqidx_[whichIF] = freqslot;
322  return True;
323}
324
325Bool SDContainer::putFreqMap(const Vector<uInt>& freqs) {
326  freqidx_.resize();
327  freqidx_ = freqs;
328  return True;
329}
330
331Bool SDContainer::setDirection(const Vector<Double>& point, uInt whichBeam) {
332  if (point.nelements() != 2) return False;
333  ArrayAccessor<Double, Axis<asap::BeamAxis> > aa0(direction_);
334  aa0.reset(aa0.begin(whichBeam));
335  ArrayAccessor<Double, Axis<asap::BeamAxis> > jj(point);
336  for (ArrayAccessor<Double, Axis<asap::IFAxis> > i(aa0);i != i.end(); ++i) {
337   
338    (*i) = (*jj);
339    jj++;
340  }
341  return True;
342}
343
344Bool SDContainer::putDirection(const Array<Double>& dir) {
345  direction_.resize();
346  direction_ = dir;
347  return True;
348}
349
350Bool SDContainer::appendHistory(const String& hist)
351{
352  history_.resize(history_.nelements()+1,True);
353  history_[history_.nelements()-1] = hist;
354  return True;
355}
356Bool SDContainer::putHistory(const Vector<String>& hist)
357{
358  history_.resize();
359  history_ = hist;
360  return True;
361}
362
363Int SDFrequencyTable::addFrequency(Double refPix, Double refVal, Double inc) {
364  Int idx = -1;
365  Bool addit = False;
366  if (length() > 0) {
367    for (uInt i=0; i< length();++i) {
368      if ( near(refVal,refVal_[i]) ) {
369        if (near(refPix,refPix_[i]) )
370          if ( near(inc,increment_[i]) )
371            idx = Int(i);
372      }
373    }
374    if (idx >= 0) {
375      return idx;
376    }
377  }
378  nFreq_ += 1;
379  refPix_.resize(nFreq_,True);
380  refVal_.resize(nFreq_,True);
381  increment_.resize(nFreq_,True);
382  refPix_[nFreq_-1] = refPix;
383  refVal_[nFreq_-1] = refVal;
384  increment_[nFreq_-1] = inc;
385  idx = nFreq_-1;
386  return idx;
387}
388
389void SDFrequencyTable::addRestFrequency(Double val)
390{
391  if (restFreqs_.nelements()  == 0) {
392    restFreqs_.resize(1);
393    restFreqs_[0] = val;
394  } else {
395    Bool found = False;
396    for (uInt i=0;i<restFreqs_.nelements();++i) {
397      if (restFreqs_[i] == val) {
398        found = True;
399        return;
400      }
401    }
402    if (!found) {
403      restFreqs_.resize(restFreqs_.nelements()+1,True);
404      restFreqs_[restFreqs_.nelements()-1] = val;
405    }
406  }
407}
408void SDFrequencyTable::restFrequencies(Vector<Double>& rfs,
409                                       String& rfunit ) const
410{
411  rfs.resize(restFreqs_.nelements());
412  rfs = restFreqs_;
413  rfunit = restFreqUnit_;
414}
415
416
417uInt SDDataDesc::addEntry (const String& source, uInt freqID, const MDirection& dir)
418{
419
420// See if already exists
421
422  if (n_ > 0) {
423    for (uInt i=0; i<n_; i++) {
424      if (source==source_[i] && freqID==freqID_[i]) {
425         return i;
426      }
427    }
428  }
429
430// Not found - add it
431
432  n_ += 1;
433  source_.resize(n_,True);
434  freqID_.resize(n_,True);
435  dir_.resize(n_,True,True);
436//
437  source_[n_-1] = source;
438  freqID_[n_-1] = freqID;
439  dir_[n_-1] = dir;
440//
441  return n_-1;
442}
443
444
445void SDDataDesc::summary() const
446{
447   cerr << "Source    FreqID" << endl;
448   for (uInt i=0; i<n_; i++) {
449      cerr << setw(11) << source_(i) << freqID_(i) << endl;
450   }
451}
452
Note: See TracBrowser for help on using the repository browser.