source: trunk/src/STFrequencies.cpp @ 852

Last change on this file since 852 was 849, checked in by mar637, 18 years ago

added assignment operator and additional constructor

File size: 9.6 KB
Line 
1//
2// C++ Implementation: STFrequencies
3//
4// Description:
5//
6//
7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2006
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12#include <casa/iostream.h>
13#include <casa/iomanip.h>
14#include <casa/Exceptions/Error.h>
15#include <casa/Containers/RecordField.h>
16#include <casa/Arrays/IPosition.h>
17
18#include <tables/Tables/TableDesc.h>
19#include <tables/Tables/SetupNewTab.h>
20#include <tables/Tables/ScaColDesc.h>
21#include <tables/Tables/TableRecord.h>
22#include <tables/Tables/TableParse.h>
23#include <tables/Tables/TableRow.h>
24
25#include <coordinates/Coordinates/CoordinateSystem.h>
26#include <coordinates/Coordinates/CoordinateUtil.h>
27
28#include "STFrequencies.h"
29
30
31using namespace casa;
32
33namespace asap {
34
35const String STFrequencies::name_ = "FREQUENCIES";
36
37STFrequencies::STFrequencies(const Scantable& parent) :
38  STSubTable(parent, name_)
39{
40  cout << "STFrequencies(const Scantable& parent" << endl;
41  setup();
42  cout << "after setup" << endl;
43}
44
45asap::STFrequencies::STFrequencies( casa::Table tab ) :
46  STSubTable(tab)
47{
48  refpixCol_.attach(table_,"REFPIX");
49  refvalCol_.attach(table_,"REFVAL");
50  incrCol_.attach(table_,"INCREMENT");
51
52}
53
54STFrequencies::~STFrequencies()
55{
56}
57
58STFrequencies & asap::STFrequencies::operator =( const STFrequencies & other )
59{
60  if ( this != &other ) {
61    static_cast<STSubTable&>(*this) = other;
62    refpixCol_.attach(table_,"REFPIX");
63    refvalCol_.attach(table_,"REFVAL");
64    incrCol_.attach(table_,"INCREMENT");
65  }
66  return *this;
67}
68
69void STFrequencies::setup( )
70{
71  // add to base class table
72  table_.addColumn(ScalarColumnDesc<Double>("REFPIX"));
73  table_.addColumn(ScalarColumnDesc<Double>("REFVAL"));
74  table_.addColumn(ScalarColumnDesc<Double>("INCREMENT"));
75
76  table_.rwKeywordSet().define("FRAME", String("TOPO"));
77  table_.rwKeywordSet().define("BASEFRAME", String("TOPO"));
78  table_.rwKeywordSet().define("EQUINOX",String( "J2000"));
79  table_.rwKeywordSet().define("UNIT", String("Hz"));
80  table_.rwKeywordSet().define("DOPPLER", String("RADIO"));
81
82  // new cached columns
83  refpixCol_.attach(table_,"REFPIX");
84  refvalCol_.attach(table_,"REFVAL");
85  incrCol_.attach(table_,"INCREMENT");
86}
87
88uInt STFrequencies::addEntry( Double refpix, Double refval, Double inc )
89{
90  // test if this already exists
91  Table result = table_( near(table_.col("REFVAL"), refval)
92                    && near(table_.col("REFPIX"), refpix)
93                    && near(table_.col("INCREMENT"), inc) );
94  uInt resultid = 0;
95  if ( result.nrow() > 0) {
96    ROScalarColumn<uInt> c(result, "ID");
97    c.get(0, resultid);
98
99  } else {
100    uInt rno = table_.nrow();
101    table_.addRow();
102    // get last assigned freq_id and increment
103    if ( rno > 0 ) {
104      idCol_.get(rno-1, resultid);
105      resultid++;
106    }
107    refpixCol_.put(rno, refpix);
108    refvalCol_.put(rno, refval);
109    incrCol_.put(rno, inc);
110    idCol_.put(rno, resultid);
111  }
112  return resultid;
113}
114
115
116
117void STFrequencies::getEntry( Double& refpix, Double& refval, Double& inc,
118                              uInt id )
119{
120  Table t = table_(table_.col("ID") == Int(id) );
121  if (t.nrow() == 0 ) {
122    throw(AipsError("STFrequencies::getEntry - freqID out of range"));
123  }
124  ROTableRow row(t);
125  // get first row - there should only be one matching id
126  const TableRecord& rec = row.get(0);
127  refpix = rec.asDouble("REFPIX");
128  refval = rec.asDouble("REFVAL");
129  inc = rec.asDouble("INCREMENT");
130}
131
132SpectralCoordinate STFrequencies::getSpectralCoordinate( uInt id ) const
133{
134  Table t = table_(table_.col("ID") == Int(id) );
135
136  if (t.nrow() == 0 ) {
137    throw(AipsError("STFrequencies::getSpectralCoordinate - ID out of range"));
138  }
139
140  // get the data
141  ROTableRow row(t);
142  // get first row - there should only be one matching id
143  const TableRecord& rec = row.get(0);
144
145  return SpectralCoordinate( getFrame(true), rec.asDouble("REFVAL"),
146                             rec.asDouble("INCREMENT"),
147                             rec.asDouble("REFPIX"));
148}
149
150SpectralCoordinate
151  asap::STFrequencies::getSpectralCoordinate( const MDirection& md,
152                                              const MPosition& mp,
153                                              const MEpoch& me,
154                                              Double restfreq, uInt id ) const
155{
156  SpectralCoordinate spc = getSpectralCoordinate(id);
157  spc.setRestFrequency(restfreq, True);
158  if ( !spc.setReferenceConversion(getFrame(), me, mp, md) ) {
159    throw(AipsError("Couldn't convert frequency frame."));
160  }
161  String unitstr = getUnitString();
162  if ( !unitstr.empty() ) {
163    Unit unitu(unitstr);
164    if ( unitu == Unit("Hz") ) {
165    } else {
166      spc.setVelocity(unitstr, getDoppler());
167    }
168  }
169  return spc;
170}
171
172
173void STFrequencies::rescale( Float factor, const std::string& mode )
174{
175  TableRow row(table_);
176  TableRecord& outrec = row.record();
177  RecordFieldPtr<Double> rv(outrec, "REFVAL");
178  RecordFieldPtr<Double> rp(outrec, "REFPIX");
179  RecordFieldPtr<Double> inc(outrec, "INCREMENT");
180  for (uInt i=0; i<table_.nrow(); ++i) {
181
182    const TableRecord& rec = row.get(i);
183
184    SpectralCoordinate sc ( getFrame(true), rec.asDouble("REFVAL"),
185                            rec.asDouble("INCREMENT"), rec.asDouble("REFPIX") );
186
187    SpectralCoordinate scout;
188    if (mode == "BIN") {
189      scout = binCsys(sc, Int(factor));
190    } else if (mode == "RESAMPLE") {
191      scout = resampleCsys(sc, factor);
192    }
193    *rv = scout.referenceValue()[0];
194    *rp = scout.referencePixel()[0];
195    *inc = scout.increment()[0];
196    row.put(i);
197  }
198}
199
200SpectralCoordinate STFrequencies::binCsys(const SpectralCoordinate& sc,
201                                          Int factor)
202{
203  CoordinateSystem csys;
204  csys.addCoordinate(sc);
205  IPosition factors(1, factor);
206  CoordinateSystem binnedcs =
207    CoordinateUtil::makeBinnedCoordinateSystem(factors, csys, False);
208  return binnedcs.spectralCoordinate(0);
209}
210
211SpectralCoordinate STFrequencies::resampleCsys(const SpectralCoordinate& sc,
212                                               Float width)
213{
214  Vector<Float> offset(1,0.0);
215  Vector<Float> factors(1,1.0/width);
216  Vector<Int> newshape;
217  CoordinateSystem csys;
218  csys.addCoordinate(sc);
219  CoordinateSystem csys2 = csys.subImage(offset, factors, newshape);
220  return csys2.spectralCoordinate(0);
221}
222
223
224MFrequency::Types STFrequencies::getFrame(bool base) const
225{
226  // get the ref frame
227  String rf = table_.keywordSet().asString("BASEFRAME");
228
229  // Create SpectralCoordinate (units Hz)
230  MFrequency::Types mft;
231  if (!MFrequency::getType(mft, rf)) {
232    ostringstream oss;
233    pushLog("WARNING: Frequency type unknown assuming TOPO");
234    mft = MFrequency::TOPO;
235  }
236
237  return mft;
238}
239
240std::string asap::STFrequencies::getFrameString( bool base ) const
241{
242  if ( base ) return table_.keywordSet().asString("BASEFRAME");
243  else return table_.keywordSet().asString("FRAME");
244}
245
246std::string asap::STFrequencies::getUnitString( ) const
247{
248  return table_.keywordSet().asString("UNIT");
249}
250
251Unit asap::STFrequencies::getUnit( ) const
252{
253  return Unit(table_.keywordSet().asString("UNIT"));
254}
255
256std::string asap::STFrequencies::getDopplerString( ) const
257{
258  return table_.keywordSet().asString("DOPPLER");
259}
260
261MDoppler::Types asap::STFrequencies::getDoppler( ) const
262{
263  String dpl = table_.keywordSet().asString("DOPPLER");
264
265  // Create SpectralCoordinate (units Hz)
266  MDoppler::Types mdt;
267  if (!MDoppler::getType(mdt, dpl)) {
268    throw(AipsError("Doppler type unknown"));
269  }
270  return mdt;
271}
272
273std::string asap::STFrequencies::print( int id )
274{
275  Table t;
276  ostringstream oss;
277  if ( id < 0 ) t = table_;
278  else  t = table_(table_.col("ID") == Int(id) );
279  ROTableRow row(t);
280  for (uInt i=0; i<t.nrow(); ++i) {
281    const TableRecord& rec = row.get(i);
282    oss <<  setw(8)
283    << "frame" << setw(16) << setprecision(8)
284    << rec.asDouble("REFVAL") << setw(10)
285    << rec.asDouble("REFPIX") << setw(12)
286    << rec.asDouble("INCREMENT") << endl;
287  }
288  return String(oss);
289}
290
291float STFrequencies::getRefFreq( uInt id, uInt channel )
292{
293  Table t = table_(table_.col("ID") == Int(id) );
294  if ( t.nrow() == 0 ) throw(AipsError("Selected Illegal frequency id"));
295  ROTableRow row(t);
296  const TableRecord& rec = row.get(0);
297  return (Double(channel/2) - rec.asDouble("REFPIX"))
298          * rec.asDouble("INCREMENT") + rec.asDouble("REFVAL");
299}
300
301bool asap::STFrequencies::conformant( const STFrequencies& other ) const
302{
303  const Record& r = table_.keywordSet();
304  const Record& ro = other.table_.keywordSet();
305  return ( r.asString("FRAME") == ro.asString("FRAME") &&
306           r.asString("EQUINOX") == ro.asString("EQUINOX") &&
307           r.asString("UNIT") == ro.asString("UNIT") &&
308           r.asString("DOPPLER") == ro.asString("DOPPLER")
309          );
310}
311
312std::vector< std::string > asap::STFrequencies::getInfo( ) const
313{
314  const Record& r = table_.keywordSet();
315  std::vector<std::string> out;
316  out.push_back(r.asString("UNIT"));
317  out.push_back(r.asString("FRAME"));
318  out.push_back(r.asString("DOPPLER"));
319}
320
321void asap::STFrequencies::setInfo( const std::vector< std::string >& theinfo )
322{
323  if ( theinfo.size() != 3 ) throw(AipsError("setInfo needs three parameters"));
324  String un,rfrm,dpl;
325  un = theinfo[0];rfrm = theinfo[1];dpl = theinfo[2];
326  TableRecord& r = table_.rwKeywordSet();
327  setFrame(rfrm);
328  MDoppler::Types dtype;
329  dpl.upcase();
330  if (!MDoppler::getType(dtype, dpl)) {
331    throw(AipsError("Doppler type unknown"));
332  } else {
333    r.define("DOPPLER",dpl);
334  }
335}
336void asap::STFrequencies::setFrame( const std::string & frame )
337{
338  MFrequency::Types mdr;
339  if (!MFrequency::getType(mdr, frame)) {
340    Int a,b;const uInt* c;
341    const String* valid = MFrequency::allMyTypes(a, b, c);
342    String pfix = "Please specify a legal frame type. Types are\n";
343    throw(AipsError(pfix+(*valid)));
344  } else {
345    table_.rwKeywordSet().define("FRAME", frame);
346  }
347}
348
349
350} // namespace
Note: See TracBrowser for help on using the repository browser.