source: trunk/src/FillerBase.cpp @ 2289

Last change on this file since 2289 was 2289, checked in by ShinnosukeKawakami, 13 years ago

merged parallel branch to trunk

File size: 9.3 KB
Line 
1//
2// C++ Interface: FillerBase
3//
4// Description:
5//
6//
7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2010
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12
13#include <casa/Containers/RecordField.h>
14#include <tables/Tables/ExprNode.h>
15
16#include "FillerBase.h"
17
18using namespace casa;
19
20namespace asap {
21
22FillerBase::FillerBase(casa::CountedPtr<Scantable> stable) :
23  table_(stable)
24{
25    row_ = TableRow(table_->table());
26
27    // FIT_ID is -1 by default
28    RecordFieldPtr<Int> fitIdCol( row_.record(), "FIT_ID" ) ;
29    *fitIdCol = -1 ;
30
31    mEntry_.resize( 0 ) ;
32    mIdx_.resize( 0 ) ;
33    fEntry_.resize( 0 ) ;
34    fIdx_.resize( 0 ) ;
35    wEntry_.resize( 0 ) ;
36    wIdx_.resize( 0 ) ;
37}
38
39void FillerBase::setHeader(const STHeader& header)
40{
41  table_->setHeader(header);
42}
43
44void FillerBase::setSpectrum(const Vector<Float>& spectrum,
45                             const Vector<uChar>& flags,
46                             const Vector<Float>& tsys)
47{
48  RecordFieldPtr< Array<Float> > specCol(row_.record(), "SPECTRA");
49  RecordFieldPtr< Array<uChar> > flagCol(row_.record(), "FLAGTRA");
50  RecordFieldPtr< Array<Float> > tsysCol(row_.record(), "TSYS");
51
52  //*specCol = spectrum;
53  //*flagCol = flags;
54  //*tsysCol = tsys;
55  specCol.define(spectrum);
56  flagCol.define(flags);
57  tsysCol.define(tsys);
58}
59
60void FillerBase::setFlagrow(uInt flag)
61{
62  RecordFieldPtr<uInt> flagrowCol(row_.record(), "FLAGROW");
63  *flagrowCol = flag;
64}
65
66void FillerBase::setOpacity(Float opacity)
67{
68  RecordFieldPtr<Float> tauCol(row_.record(), "OPACITY") ;
69  *tauCol = opacity ;
70}
71
72void FillerBase::setIndex(uInt scanno, uInt cycleno, uInt ifno, uInt polno,
73                          uInt beamno)
74{
75  RecordFieldPtr<uInt> beamCol(row_.record(), "BEAMNO");
76  RecordFieldPtr<uInt> ifCol(row_.record(), "IFNO");
77  RecordFieldPtr<uInt> polCol(row_.record(), "POLNO");
78  RecordFieldPtr<uInt> cycleCol(row_.record(), "CYCLENO");
79  RecordFieldPtr<uInt> scanCol(row_.record(), "SCANNO");
80  *beamCol = beamno;
81  *cycleCol = cycleno;
82  *ifCol = ifno;
83  *polCol = polno;
84  *scanCol = scanno;
85}
86
87void FillerBase::setFrequency(Double refpix, Double refval,
88                              Double incr)
89{
90  /// @todo this has to change when nchan isn't global anymore
91  uInt nEntry = fEntry_.size() ;
92  Int idx = -1 ;
93  Vector<Double> entry( 3 ) ;
94  entry[0] = refpix ;
95  entry[1] = refval ;
96  entry[2] = incr ;
97  for ( uInt i = 0 ; i < nEntry ; i++ ) {
98    if ( allEQ( entry, fEntry_[i] ) ) {
99      idx = i ;
100      break ;
101    }
102  }
103  uInt id ;
104  if ( idx != -1 )
105    id = fIdx_[idx] ;
106  else {
107    id= table_->frequencies().addEntry(refpix, refval, incr);
108    RecordFieldPtr<uInt> mfreqidCol(row_.record(), "FREQ_ID");
109    fEntry_.push_back( entry ) ;
110    fIdx_.push_back( id ) ;
111  }
112  RecordFieldPtr<uInt> mfreqidCol(row_.record(), "FREQ_ID");
113  *mfreqidCol = id;
114
115}
116
117
118void FillerBase::setMolecule(const Vector<Double>& restfreq)
119{
120  uInt nEntry = mEntry_.size() ;
121  Int idx = -1 ;
122  for ( uInt i = 0 ; i < nEntry ; i++ ) {
123    if ( restfreq.conform( mEntry_[i] ) ) {
124      if ( allEQ( restfreq, mEntry_[i] ) ) {
125        idx = i ;
126        break ;
127      }
128    }
129  }
130  uInt id ;
131  if ( idx != -1 )
132    id = mIdx_[idx] ;
133  else {
134    Vector<String> tmp ;
135    id = table_->molecules().addEntry(restfreq,tmp,tmp) ;
136    mEntry_.push_back( restfreq ) ;
137    mIdx_.push_back( id ) ;
138  }
139  RecordFieldPtr<uInt> molidCol(row_.record(), "MOLECULE_ID");
140  *molidCol = id;
141}
142
143void FillerBase::setDirection(const Vector<Double>& dir,
144                              Float az, Float el)
145{
146  RecordFieldPtr<Array<Double> > dirCol(row_.record(), "DIRECTION");
147  *dirCol = dir;
148  RecordFieldPtr<Float> azCol(row_.record(), "AZIMUTH");
149  *azCol = az;
150  RecordFieldPtr<Float> elCol(row_.record(), "ELEVATION");
151  *elCol = el;
152}
153
154void FillerBase::setFocus(Float pa, Float faxis,
155                      Float ftan, Float frot)
156{
157  RecordFieldPtr<uInt> mfocusidCol(row_.record(), "FOCUS_ID");
158  uInt id = table_->focus().addEntry(pa, faxis, ftan, frot);
159  *mfocusidCol = id;
160}
161
162void FillerBase::setTime(Double mjd, Double interval)
163{
164    RecordFieldPtr<Double> mjdCol(row_.record(), "TIME");
165    *mjdCol = mjd;
166    RecordFieldPtr<Double> intCol(row_.record(), "INTERVAL");
167    *intCol = interval;
168
169}
170
171void FillerBase::setWeather(Float temperature, Float pressure,
172                        Float humidity,
173                        Float windspeed, Float windaz)
174{
175  uInt nEntry = wEntry_.size() ;
176  Int idx = -1 ;
177  Vector<Float> entry( 5 ) ;
178  entry[0] = temperature ;
179  entry[1] = pressure ;
180  entry[2] = humidity ;
181  entry[3] = windspeed ;
182  entry[4] = windaz ;
183  for ( uInt i = 0 ; i < nEntry ; i++ ) {
184    if ( allEQ( entry, wEntry_[i] ) ) {
185      idx = i ;
186      break ;
187    }
188  }
189  uInt id ;
190  if ( idx != -1 )
191    id = wIdx_[idx] ;
192  else {
193    id = table_->weather().addEntry(temperature, pressure,
194                                    humidity, windspeed, windaz);
195    wEntry_.push_back( entry ) ;
196    wIdx_.push_back( id ) ;
197  }
198  RecordFieldPtr<uInt> mweatheridCol(row_.record(), "WEATHER_ID");
199  *mweatheridCol = id;
200}
201
202void FillerBase::setTcal(const String& tcaltime,
203                     const Vector<Float>& tcal)
204{
205    uInt id = table_->tcal().addEntry(tcaltime, tcal);
206    RecordFieldPtr<uInt> mcalidCol(row_.record(), "TCAL_ID");
207    *mcalidCol = id;
208}
209
210void FillerBase::setScanRate(const Vector<Double>& srate)
211{
212    RecordFieldPtr<Array<Double> > srateCol(row_.record(), "SCANRATE");
213    *srateCol = srate;
214}
215
216void FillerBase::setReferenceBeam(Int beamno)
217{
218  RecordFieldPtr<Int> rbCol(row_.record(), "REFBEAMNO");
219  *rbCol = beamno;
220}
221
222void FillerBase::setSource(const std::string& name, Int type,
223                           const std::string& fieldname,
224                           const Vector<Double>& dir,
225                           const Vector<Double>& propermot,
226                           Double velocity)
227{
228    RecordFieldPtr<String> srcnCol(row_.record(), "SRCNAME");
229    *srcnCol = name;
230    RecordFieldPtr<Int> srctCol(row_.record(), "SRCTYPE");
231    *srctCol = type;
232    RecordFieldPtr<String> fieldnCol(row_.record(), "FIELDNAME");
233    *fieldnCol = fieldname;
234    RecordFieldPtr<Array<Double> > spmCol(row_.record(), "SRCPROPERMOTION");
235    *spmCol = propermot;
236    RecordFieldPtr<Array<Double> > sdirCol(row_.record(), "SRCDIRECTION");
237    *sdirCol = dir;
238    RecordFieldPtr<Double> svelCol(row_.record(), "SRCVELOCITY");
239    *svelCol = velocity;
240}
241
242void FillerBase::commitRow()
243{
244  table_->table().addRow();
245  row_.put(table_->table().nrow()-1);
246}
247
248void FillerBase::setWeather2(Float temperature,
249                             Float pressure,
250                             Float humidity,
251                             Float windspeed,
252                             Float windaz)
253{
254  uInt nEntry = wEntry_.size() ;
255  Int idx = -1 ;
256  Vector<Float> entry( 5 ) ;
257  entry[0] = temperature ;
258  entry[1] = pressure ;
259  entry[2] = humidity ;
260  entry[3] = windspeed ;
261  entry[4] = windaz ;
262  for ( uInt i = 0 ; i < nEntry ; i++ ) {
263    if ( allEQ( entry, wEntry_[i] ) ) {
264      idx = i ;
265      break ;
266    }
267  }
268  uInt id ;
269  if ( idx != -1 )
270    id = wIdx_[idx] ;
271  else {
272    Table tab = table_->weather().table() ;
273    Table subt = tab( tab.col("TEMPERATURE") == temperature     \
274                      && tab.col("PRESSURE") == pressure        \
275                      && tab.col("HUMIDITY") == humidity        \
276                      && tab.col("WINDSPEED") == windspeed      \
277                      && tab.col("WINDAZ") == windaz, 1 ) ;
278    Int nrow = tab.nrow() ;
279    Int nrowSel = subt.nrow() ;
280    if ( nrowSel == 0 ) {
281      tab.addRow( 1, True ) ;
282      TableRow row( tab ) ;
283      TableRecord &rec = row.record() ;
284      RecordFieldPtr<casa::uInt> rfpi ;
285      rfpi.attachToRecord( rec, "ID" ) ;
286      *rfpi = (uInt)nrow ;
287      RecordFieldPtr<casa::Float> rfp ;
288      rfp.attachToRecord( rec, "TEMPERATURE" ) ;
289      *rfp = temperature ;
290      rfp.attachToRecord( rec, "PRESSURE" ) ;
291      *rfp = pressure ;
292      rfp.attachToRecord( rec, "HUMIDITY" ) ;
293      *rfp = humidity ;
294      rfp.attachToRecord( rec, "WINDSPEED" ) ;
295      *rfp = windspeed ;
296      rfp.attachToRecord( rec, "WINDAZ" ) ;
297      *rfp = windaz ;
298      row.put( nrow, rec ) ;
299      id = (uInt)nrow ;
300    }
301    else {
302      ROTableColumn tc( subt, "ID" ) ;
303      id = tc.asuInt( 0 ) ;
304    }
305    wEntry_.push_back( entry ) ;
306    wIdx_.push_back( id ) ;
307  }
308  RecordFieldPtr<uInt> mweatheridCol(row_.record(), "WEATHER_ID");
309  *mweatheridCol = id;
310}
311
312void FillerBase::setTcal2(const String& tcaltime,
313                          const Vector<Float>& tcal)
314{
315  uInt id = 0 ;
316  Table tab = table_->tcal().table() ;
317  Table result =
318    //tab( nelements(tab.col("TCAL")) == uInt (tcal.size()) &&
319    //     all(tab.col("TCAL")== tcal) &&
320    //     tab.col("TIME") == tcaltime, 1 ) ;
321    tab( nelements(tab.col("TCAL")) == uInt (tcal.size()) &&
322         all(tab.col("TCAL")== tcal), 1 ) ;
323
324  if ( result.nrow() > 0 ) {
325    ROTableColumn tmpCol( result, "ID" ) ;
326    tmpCol.getScalar( 0, id ) ;
327  }
328  else {
329    uInt rno = tab.nrow();
330    tab.addRow();
331    TableColumn idCol( tab, "ID" ) ;
332    TableColumn tctimeCol( tab, "TIME" ) ;
333    ArrayColumn<Float> tcalCol( tab, "TCAL" ) ;
334    // get last assigned _id and increment
335    if ( rno > 0 ) {
336      idCol.getScalar(rno-1, id);
337      id++;
338    }
339    tctimeCol.putScalar(rno, tcaltime);
340    tcalCol.put(rno, tcal);
341    idCol.putScalar(rno, id);
342  }
343
344  RecordFieldPtr<uInt> mcalidCol(row_.record(), "TCAL_ID");
345  *mcalidCol = id;
346}
347
348};
Note: See TracBrowser for help on using the repository browser.