source: trunk/src/MSWriter.cpp@ 2744

Last change on this file since 2744 was 2743, checked in by Takeshi Nakazato, 12 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: Yes/No

What Interface Changed: Please list interface changes

Test Programs: test_sdsave

export scantable only contains POLNO 1 data

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Several bug fixes on MSWriter:

  • fixed a bug that export fails when data only contain POLNO 1 data
  • fixed many warning messages when compiling
  • fixed minor typo


File size: 77.8 KB
Line 
1//
2// C++ Interface: MSWriter
3//
4// Description:
5//
6// This class is specific writer for MS format
7//
8// Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2010
9//
10// Copyright: See COPYING file that comes with this distribution
11//
12//
13#include <assert.h>
14
15#include <set>
16
17#include <casa/OS/File.h>
18#include <casa/OS/RegularFile.h>
19#include <casa/OS/Directory.h>
20#include <casa/OS/SymLink.h>
21#include <casa/BasicSL/String.h>
22#include <casa/Arrays/Cube.h>
23#include <casa/Containers/RecordField.h>
24
25#include <tables/Tables/ExprNode.h>
26#include <tables/Tables/TableDesc.h>
27#include <tables/Tables/SetupNewTab.h>
28#include <tables/Tables/TableIter.h>
29#include <tables/Tables/RefRows.h>
30#include <tables/Tables/TableRow.h>
31
32#include <ms/MeasurementSets/MeasurementSet.h>
33#include <ms/MeasurementSets/MSColumns.h>
34#include <ms/MeasurementSets/MSPolIndex.h>
35#include <ms/MeasurementSets/MSDataDescIndex.h>
36#include <ms/MeasurementSets/MSSourceIndex.h>
37
38#include "MSWriter.h"
39#include "STHeader.h"
40#include "STFrequencies.h"
41#include "STMolecules.h"
42#include "STTcal.h"
43#include "MathUtils.h"
44#include "TableTraverse.h"
45
46using namespace casa ;
47using namespace std ;
48
49namespace asap {
50
51class CorrTypeHandler {
52public:
53 CorrTypeHandler()
54 {}
55 virtual ~CorrTypeHandler() {}
56 virtual Vector<Stokes::StokesTypes> corrType() = 0 ;
57 virtual void reset()
58 {
59 npol = 0 ;
60 }
61 void append( uInt polno )
62 {
63 polnos[npol] = polno ;
64 npol++ ;
65 }
66 uInt nPol() { return npol ; }
67protected:
68 Vector<Stokes::StokesTypes> polmap ;
69 uInt polnos[4] ;
70 uInt npol ;
71};
72
73class LinearHandler : public CorrTypeHandler {
74public:
75 LinearHandler()
76 : CorrTypeHandler()
77 {
78 initMap() ;
79 }
80 virtual ~LinearHandler() {}
81 virtual Vector<Stokes::StokesTypes> corrType()
82 {
83 Vector<Stokes::StokesTypes> ret( npol, Stokes::Undefined ) ;
84 if ( npol < 4 ) {
85 for ( uInt ipol = 0 ; ipol < npol ; ipol++ )
86 ret[ipol] = polmap[polnos[ipol]] ;
87 }
88 else if ( npol == 4 ) {
89 ret[0] = polmap[0] ;
90 ret[1] = polmap[2] ;
91 ret[2] = polmap[3] ;
92 ret[3] = polmap[1] ;
93 }
94 else {
95 throw( AipsError("npol > 4") ) ;
96 }
97 return ret ;
98 }
99protected:
100 void initMap()
101 {
102 polmap.resize( 4 ) ;
103 polmap[0] = Stokes::XX ;
104 polmap[1] = Stokes::YY ;
105 polmap[2] = Stokes::XY ;
106 polmap[3] = Stokes::YX ;
107 }
108};
109class CircularHandler : public CorrTypeHandler {
110public:
111 CircularHandler()
112 : CorrTypeHandler()
113 {
114 initMap() ;
115 }
116 virtual ~CircularHandler() {}
117 virtual Vector<Stokes::StokesTypes> corrType()
118 {
119 Vector<Stokes::StokesTypes> ret( npol, Stokes::Undefined ) ;
120 if ( npol < 4 ) {
121 for ( uInt ipol = 0 ; ipol < npol ; ipol++ )
122 ret[ipol] = polmap[polnos[ipol]] ;
123 }
124 else if ( npol == 4 ) {
125 ret[0] = polmap[0] ;
126 ret[1] = polmap[2] ;
127 ret[2] = polmap[3] ;
128 ret[3] = polmap[1] ;
129 }
130 else {
131 throw( AipsError("npol > 4") ) ;
132 }
133 return ret ;
134 }
135private:
136 void initMap()
137 {
138 polmap.resize( 4 ) ;
139 polmap[0] = Stokes::RR ;
140 polmap[1] = Stokes::LL ;
141 polmap[2] = Stokes::RL ;
142 polmap[3] = Stokes::LR ;
143 }
144};
145class StokesHandler : public CorrTypeHandler {
146public:
147 StokesHandler()
148 : CorrTypeHandler()
149 {
150 initMap() ;
151 }
152 virtual ~StokesHandler() {}
153 virtual Vector<Stokes::StokesTypes> corrType()
154 {
155 Vector<Stokes::StokesTypes> ret( npol, Stokes::Undefined ) ;
156 if ( npol <= 4 ) {
157 for ( uInt ipol = 0 ; ipol < npol ; ipol++ )
158 ret[ipol] = polmap[polnos[ipol]] ;
159 }
160 else {
161 throw( AipsError("npol > 4") ) ;
162 }
163 return ret ;
164 }
165private:
166 void initMap()
167 {
168 polmap.resize( 4 ) ;
169 polmap[0] = Stokes::I ;
170 polmap[1] = Stokes::Q ;
171 polmap[2] = Stokes::U ;
172 polmap[3] = Stokes::V ;
173 }
174};
175class LinPolHandler : public CorrTypeHandler {
176public:
177 LinPolHandler()
178 : CorrTypeHandler()
179 {
180 initMap() ;
181 }
182 virtual ~LinPolHandler() {}
183 virtual Vector<Stokes::StokesTypes> corrType()
184 {
185 Vector<Stokes::StokesTypes> ret( npol, Stokes::Undefined ) ;
186 if ( npol <= 2 ) {
187 for ( uInt ipol = 0 ; ipol < npol ; ipol++ )
188 ret[ipol] = polmap[polnos[ipol]] ;
189 }
190 else {
191 throw( AipsError("npol > 4") ) ;
192 }
193 return ret ;
194 }
195private:
196 void initMap()
197 {
198 polmap.resize( 2 ) ;
199 polmap[0] = Stokes::Plinear ;
200 polmap[1] = Stokes::Pangle ;
201 }
202};
203
204class DataHolder {
205public:
206 DataHolder( TableRow &tableRow, String polType )
207 : row( tableRow )
208 {
209 nchan = 0 ;
210 npol = 0 ;
211 makeCorrTypeHandler( polType ) ;
212 attach() ;
213 flagRow.resize( 4 ) ;
214 reset() ;
215 sigmaTemplate.resize( 4 ) ;
216 sigmaTemplate = 1.0 ;
217 }
218 virtual ~DataHolder() {}
219 void post()
220 {
221 postData() ;
222 postFlag() ;
223 postFlagRow() ;
224 postAuxiliary() ;
225 }
226 virtual void reset()
227 {
228 corr->reset() ;
229 flagRow = False ;
230 npol = 0 ;
231 for ( uInt i = 0 ; i < 4 ; i++ )
232 isFilled[i] = False ;
233 }
234 virtual void accumulate( uInt id, Vector<Float> &sp, Vector<Bool> &fl, Bool &flr )
235 {
236 accumulateCorrType( id ) ;
237 accumulateData( id, sp ) ;
238 accumulateFlag( id, fl ) ;
239 accumulateFlagRow( id, flr ) ;
240 isFilled[id] = True;
241 }
242 uInt nPol() { return npol ; }
243 uInt nChan() { return nchan ; }
244 Vector<Int> corrTypeInt()
245 {
246 Vector<Int> v( npol ) ;
247 convertArray( v, corr->corrType() ) ;
248 return v ;
249 }
250 Vector<Stokes::StokesTypes> corrType() { return corr->corrType() ; }
251 void setNchan( uInt num )
252 {
253 nchan = num ;
254 resize() ;
255 }
256protected:
257 void postAuxiliary()
258 {
259 Vector<Float> w = sigmaTemplate( IPosition(1,0), IPosition(1,npol-1) ) ;
260 sigmaRF.define( w ) ;
261 weightRF.define( w ) ;
262 Cube<Bool> c( npol, nchan, 1, False ) ;
263 flagCategoryRF.define( c ) ;
264 }
265 inline void accumulateCorrType( uInt &id )
266 {
267 corr->append( id ) ;
268 npol = corr->nPol() ;
269 }
270 inline void accumulateFlagRow( uInt &id, Bool &flr )
271 {
272 flagRow[id] = flr ;
273 }
274 void postFlagRow()
275 {
276 *flagRowRF = anyEQ( flagRow, True ) ;
277 }
278 inline void accumulateFlag( uInt &id, Vector<Bool> &fl )
279 {
280 flag.row( id ) = fl ;
281 }
282 virtual void postFlag() = 0 ;
283 inline void accumulateData( uInt &id, Vector<Float> &sp )
284 {
285 data.row( id ) = sp ;
286 }
287 uInt filledIndex()
288 {
289 uInt idx = 0;
290 while( !isFilled[idx] && idx < 4 ) ++idx;
291 return idx;
292 }
293 virtual void postData() = 0 ;
294 TableRow &row ;
295 uInt nchan ;
296 uInt npol ;
297 CountedPtr<CorrTypeHandler> corr;
298 RecordFieldPtr< Vector<Float> > sigmaRF ;
299 RecordFieldPtr< Vector<Float> > weightRF ;
300 RecordFieldPtr< Array<Bool> > flagRF ;
301 RecordFieldPtr<Bool> flagRowRF ;
302 RecordFieldPtr< Cube<Bool> > flagCategoryRF ;
303 Vector<Bool> flagRow ;
304 Matrix<Bool> flag ;
305 Matrix<Float> data ;
306 Vector<Float> sigmaTemplate ;
307 Bool isFilled[4] ;
308private:
309 void makeCorrTypeHandler( String &polType )
310 {
311 if ( polType == "linear" )
312 corr = new LinearHandler() ;
313 else if ( polType == "circular" )
314 corr = new CircularHandler() ;
315 else if ( polType == "stokes" )
316 corr = new StokesHandler() ;
317 else if ( polType == "linpol" )
318 corr = new LinPolHandler() ;
319 else
320 throw( AipsError("Invalid polarization type") ) ;
321 }
322 void attach()
323 {
324 TableRecord &rec = row.record() ;
325 sigmaRF.attachToRecord( rec, "SIGMA" ) ;
326 weightRF.attachToRecord( rec, "WEIGHT" ) ;
327 flagRF.attachToRecord( rec, "FLAG" ) ;
328 flagRowRF.attachToRecord( rec, "FLAG_ROW" ) ;
329 flagCategoryRF.attachToRecord( rec, "FLAG_CATEGORY" ) ;
330 }
331 void resize()
332 {
333 flag.resize( 4, nchan ) ;
334 data.resize( 4, nchan ) ;
335 }
336};
337
338class FloatDataHolder : public DataHolder {
339public:
340 FloatDataHolder( TableRow &tableRow, String polType )
341 : DataHolder( tableRow, polType )
342 {
343 attachData() ;
344 }
345 virtual ~FloatDataHolder() {}
346protected:
347 virtual void postFlag()
348 {
349 if ( npol == 2 ) {
350 flagRF.define( flag( IPosition( 2, 0, 0 ), IPosition( 2, npol-1, nchan-1 ) ) ) ;
351 }
352 else {
353 // should be npol == 1
354 uInt idx = filledIndex() ;
355 flagRF.define( flag( IPosition( 2, idx, 0 ), IPosition( 2, idx, nchan-1 ) ) ) ;
356 }
357 }
358 virtual void postData()
359 {
360 if ( npol == 2 ) {
361 dataRF.define( data( IPosition( 2, 0, 0 ), IPosition( 2, npol-1, nchan-1 ) ) ) ;
362 }
363 else {
364 // should be npol == 1
365 uInt idx = filledIndex() ;
366 dataRF.define( data( IPosition( 2, idx, 0 ), IPosition( 2, idx, nchan-1 ) ) ) ;
367 }
368 }
369private:
370 void attachData()
371 {
372 dataRF.attachToRecord( row.record(), "FLOAT_DATA" ) ;
373 }
374 RecordFieldPtr< Matrix<Float> > dataRF;
375};
376
377class ComplexDataHolder : public DataHolder {
378public:
379 ComplexDataHolder( TableRow &tableRow, String polType )
380 : DataHolder( tableRow, polType )
381 {
382 attachData() ;
383 }
384 virtual ~ComplexDataHolder() {}
385protected:
386 virtual void postFlag()
387 {
388 if ( npol == 4 ) {
389 Vector<Bool> tmp = flag.row( 3 ) ;
390 flag.row( 3 ) = flag.row( 1 ) ;
391 flag.row( 2 ) = flag.row( 2 ) || tmp ;
392 flag.row( 1 ) = flag.row( 2 ) ;
393 flagRF.define( flag ) ;
394 }
395 else if ( npol == 2 ) {
396 flagRF.define( flag( IPosition( 2, 0, 0 ), IPosition( 2, npol-1, nchan-1 ) ) ) ;
397 }
398 else {
399 // should be npol == 1
400 uInt idx = filledIndex() ;
401 flagRF.define( flag( IPosition( 2, idx, 0 ), IPosition( 2, idx, nchan-1 ) ) ) ;
402 }
403 }
404 virtual void postData()
405 {
406 Matrix<Float> tmp( 2, nchan, 0.0 ) ;
407 Matrix<Complex> v( npol, nchan ) ;
408 if ( isFilled[0] ) {
409 tmp.row( 0 ) = data.row( 0 ) ;
410 v.row( 0 ) = RealToComplex( tmp ) ;
411 }
412 if ( isFilled[1] ) {
413 tmp.row( 0 ) = data.row( 1 ) ;
414 v.row( npol-1 ) = RealToComplex( tmp ) ;
415 }
416 if ( isFilled[2] && isFilled[3] ) {
417 tmp.row( 0 ) = data.row( 2 ) ;
418 tmp.row( 1 ) = data.row( 3 ) ;
419 v.row( 1 ) = RealToComplex( tmp ) ;
420 v.row( 2 ) = conj( v.row( 1 ) ) ;
421 }
422 dataRF.define( v ) ;
423 }
424private:
425 void attachData()
426 {
427 dataRF.attachToRecord( row.record(), "DATA" ) ;
428 }
429 RecordFieldPtr< Matrix<Complex> > dataRF;
430};
431
432class BaseMSWriterVisitor: public TableVisitor {
433 const String *lastFieldName;
434 uInt lastRecordNo;
435 uInt lastBeamNo, lastScanNo, lastIfNo, lastPolNo;
436 Int lastSrcType;
437 uInt lastCycleNo;
438 Double lastTime;
439protected:
440 const Table &table;
441 uInt count;
442public:
443 BaseMSWriterVisitor(const Table &table)
444 : table(table)
445 {
446 static const String dummy;
447 lastFieldName = &dummy;
448 count = 0;
449 }
450
451 virtual void enterFieldName(const uInt /*recordNo*/, const String &/*columnValue*/) {
452 }
453 virtual void leaveFieldName(const uInt /*recordNo*/, const String &/*columnValue*/) {
454 }
455 virtual void enterBeamNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
456 virtual void leaveBeamNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
457 virtual void enterScanNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
458 virtual void leaveScanNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
459 virtual void enterIfNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
460 virtual void leaveIfNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
461 virtual void enterSrcType(const uInt /*recordNo*/, Int /*columnValue*/) { }
462 virtual void leaveSrcType(const uInt /*recordNo*/, Int /*columnValue*/) { }
463 virtual void enterCycleNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
464 virtual void leaveCycleNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
465 virtual void enterTime(const uInt /*recordNo*/, Double /*columnValue*/) { }
466 virtual void leaveTime(const uInt /*recordNo*/, Double /*columnValue*/) { }
467 virtual void enterPolNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
468 virtual void leavePolNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
469
470 virtual Bool visitRecord(const uInt /*recordNo*/,
471 const String &/*fieldName*/,
472 const uInt /*beamNo*/,
473 const uInt /*scanNo*/,
474 const uInt /*ifNo*/,
475 const Int /*srcType*/,
476 const uInt /*cycleNo*/,
477 const Double /*time*/,
478 const uInt /*polNo*/) { return True ;}
479
480 virtual Bool visit(Bool isFirst, const uInt recordNo,
481 const uInt nCols, void const *const colValues[]) {
482 const String *fieldName = NULL;
483 uInt beamNo, scanNo, ifNo;
484 Int srcType;
485 uInt cycleNo;
486 Double time;
487 uInt polNo;
488 { // prologue
489 uInt i = 0;
490 {
491 const String *col = (const String*)colValues[i++];
492 fieldName = &col[recordNo];
493 }
494 {
495 const uInt *col = (const uInt *)colValues[i++];
496 beamNo = col[recordNo];
497 }
498 {
499 const uInt *col = (const uInt *)colValues[i++];
500 scanNo = col[recordNo];
501 }
502 {
503 const uInt *col = (const uInt *)colValues[i++];
504 ifNo = col[recordNo];
505 }
506 {
507 const Int *col = (const Int *)colValues[i++];
508 srcType = col[recordNo];
509 }
510 {
511 const uInt *col = (const uInt *)colValues[i++];
512 cycleNo = col[recordNo];
513 }
514 {
515 const Double *col = (const Double *)colValues[i++];
516 time = col[recordNo];
517 }
518 {
519 const Int *col = (const Int *)colValues[i++];
520 polNo = col[recordNo];
521 }
522 assert(nCols == i);
523 }
524
525 if (isFirst) {
526 enterFieldName(recordNo, *fieldName);
527 enterBeamNo(recordNo, beamNo);
528 enterScanNo(recordNo, scanNo);
529 enterIfNo(recordNo, ifNo);
530 enterSrcType(recordNo, srcType);
531 enterCycleNo(recordNo, cycleNo);
532 enterTime(recordNo, time);
533 enterPolNo(recordNo, polNo);
534 } else {
535 if (lastFieldName->compare(*fieldName) != 0) {
536 leavePolNo(lastRecordNo, lastPolNo);
537 leaveTime(lastRecordNo, lastTime);
538 leaveCycleNo(lastRecordNo, lastCycleNo);
539 leaveSrcType(lastRecordNo, lastSrcType);
540 leaveIfNo(lastRecordNo, lastIfNo);
541 leaveScanNo(lastRecordNo, lastScanNo);
542 leaveBeamNo(lastRecordNo, lastBeamNo);
543 leaveFieldName(lastRecordNo, *lastFieldName);
544
545 enterFieldName(recordNo, *fieldName);
546 enterBeamNo(recordNo, beamNo);
547 enterScanNo(recordNo, scanNo);
548 enterIfNo(recordNo, ifNo);
549 enterSrcType(recordNo, srcType);
550 enterCycleNo(recordNo, cycleNo);
551 enterTime(recordNo, time);
552 enterPolNo(recordNo, polNo);
553 } else if (lastBeamNo != beamNo) {
554 leavePolNo(lastRecordNo, lastPolNo);
555 leaveTime(lastRecordNo, lastTime);
556 leaveCycleNo(lastRecordNo, lastCycleNo);
557 leaveSrcType(lastRecordNo, lastSrcType);
558 leaveIfNo(lastRecordNo, lastIfNo);
559 leaveScanNo(lastRecordNo, lastScanNo);
560 leaveBeamNo(lastRecordNo, lastBeamNo);
561
562 enterBeamNo(recordNo, beamNo);
563 enterScanNo(recordNo, scanNo);
564 enterIfNo(recordNo, ifNo);
565 enterSrcType(recordNo, srcType);
566 enterCycleNo(recordNo, cycleNo);
567 enterTime(recordNo, time);
568 enterPolNo(recordNo, polNo);
569 } else if (lastScanNo != scanNo) {
570 leavePolNo(lastRecordNo, lastPolNo);
571 leaveTime(lastRecordNo, lastTime);
572 leaveCycleNo(lastRecordNo, lastCycleNo);
573 leaveSrcType(lastRecordNo, lastSrcType);
574 leaveIfNo(lastRecordNo, lastIfNo);
575 leaveScanNo(lastRecordNo, lastScanNo);
576
577 enterScanNo(recordNo, scanNo);
578 enterIfNo(recordNo, ifNo);
579 enterSrcType(recordNo, srcType);
580 enterCycleNo(recordNo, cycleNo);
581 enterTime(recordNo, time);
582 enterPolNo(recordNo, polNo);
583 } else if (lastIfNo != ifNo) {
584 leavePolNo(lastRecordNo, lastPolNo);
585 leaveTime(lastRecordNo, lastTime);
586 leaveCycleNo(lastRecordNo, lastCycleNo);
587 leaveSrcType(lastRecordNo, lastSrcType);
588 leaveIfNo(lastRecordNo, lastIfNo);
589
590 enterIfNo(recordNo, ifNo);
591 enterSrcType(recordNo, srcType);
592 enterCycleNo(recordNo, cycleNo);
593 enterTime(recordNo, time);
594 enterPolNo(recordNo, polNo);
595 } else if (lastSrcType != srcType) {
596 leavePolNo(lastRecordNo, lastPolNo);
597 leaveTime(lastRecordNo, lastTime);
598 leaveCycleNo(lastRecordNo, lastCycleNo);
599 leaveSrcType(lastRecordNo, lastSrcType);
600
601 enterSrcType(recordNo, srcType);
602 enterCycleNo(recordNo, cycleNo);
603 enterTime(recordNo, time);
604 enterPolNo(recordNo, polNo);
605 } else if (lastCycleNo != cycleNo) {
606 leavePolNo(lastRecordNo, lastPolNo);
607 leaveTime(lastRecordNo, lastTime);
608 leaveCycleNo(lastRecordNo, lastCycleNo);
609
610 enterCycleNo(recordNo, cycleNo);
611 enterTime(recordNo, time);
612 enterPolNo(recordNo, polNo);
613 } else if (lastTime != time) {
614 leavePolNo(lastRecordNo, lastPolNo);
615 leaveTime(lastRecordNo, lastTime);
616
617 enterTime(recordNo, time);
618 enterPolNo(recordNo, polNo);
619 } else if (lastPolNo != polNo) {
620 leavePolNo(lastRecordNo, lastPolNo);
621 enterPolNo(recordNo, polNo);
622 }
623 }
624 count++;
625 Bool result = visitRecord(recordNo, *fieldName, beamNo, scanNo, ifNo, srcType,
626 cycleNo, time, polNo);
627
628 { // epilogue
629 lastRecordNo = recordNo;
630
631 lastFieldName = fieldName;
632 lastBeamNo = beamNo;
633 lastScanNo = scanNo;
634 lastIfNo = ifNo;
635 lastSrcType = srcType;
636 lastCycleNo = cycleNo;
637 lastTime = time;
638 lastPolNo = polNo;
639 }
640 return result ;
641 }
642
643 virtual void finish() {
644 if (count > 0) {
645 leavePolNo(lastRecordNo, lastPolNo);
646 leaveTime(lastRecordNo, lastTime);
647 leaveCycleNo(lastRecordNo, lastCycleNo);
648 leaveSrcType(lastRecordNo, lastSrcType);
649 leaveIfNo(lastRecordNo, lastIfNo);
650 leaveScanNo(lastRecordNo, lastScanNo);
651 leaveBeamNo(lastRecordNo, lastBeamNo);
652 leaveFieldName(lastRecordNo, *lastFieldName);
653 }
654 }
655};
656
657class MSWriterVisitor: public BaseMSWriterVisitor, public MSWriterUtils {
658public:
659 MSWriterVisitor(const Table &table, Table &mstable)
660 : BaseMSWriterVisitor(table),
661 ms(mstable)
662 {
663 rowidx = 0 ;
664 fieldName = "" ;
665 defaultFieldId = 0 ;
666 spwId = -1 ;
667 subscan = 1 ;
668 ptName = "" ;
669 srcId = 0 ;
670
671 row = TableRow( ms ) ;
672
673 initPolarization() ;
674 initFrequencies() ;
675
676 //
677 // add rows to MS
678 //
679 uInt addrow = table.nrow() ;
680 ms.addRow( addrow ) ;
681
682 // attach to Scantable columns
683 spectraCol.attach( table, "SPECTRA" ) ;
684 flagtraCol.attach( table, "FLAGTRA" ) ;
685 flagRowCol.attach( table, "FLAGROW" ) ;
686 tcalIdCol.attach( table, "TCAL_ID" ) ;
687 intervalCol.attach( table, "INTERVAL" ) ;
688 directionCol.attach( table, "DIRECTION" ) ;
689 scanRateCol.attach( table, "SCANRATE" ) ;
690 timeCol.attach( table, "TIME" ) ;
691 freqIdCol.attach( table, "FREQ_ID" ) ;
692 sourceNameCol.attach( table, "SRCNAME" ) ;
693 sourceDirectionCol.attach( table, "SRCDIRECTION" ) ;
694 fieldNameCol.attach( table, "FIELDNAME" ) ;
695
696 // MS subtables
697 attachSubtables() ;
698
699 // attach to MS columns
700 attachMain() ;
701 attachPointing() ;
702 }
703
704 virtual void enterFieldName(const uInt recordNo, const String &/*columnValue*/) {
705 //printf("%u: FieldName: %s\n", recordNo, columnValue.c_str());
706 fieldName = fieldNameCol.asString( recordNo ) ;
707 String::size_type pos = fieldName.find( "__" ) ;
708 if ( pos != String::npos ) {
709 fieldId = String::toInt( fieldName.substr( pos+2 ) ) ;
710 fieldName = fieldName.substr( 0, pos ) ;
711 }
712 else {
713 fieldId = defaultFieldId ;
714 defaultFieldId++ ;
715 }
716 Double tSec = timeCol.asdouble( recordNo ) * 86400.0 ;
717 Vector<Double> srcDir = sourceDirectionCol( recordNo ) ;
718 Vector<Double> srate = scanRateCol( recordNo ) ;
719 String srcName = sourceNameCol.asString( recordNo ) ;
720
721 addField( fieldId, fieldName, srcName, srcDir, srate, tSec ) ;
722
723 // put value
724 *fieldIdRF = fieldId ;
725 }
726 virtual void leaveFieldName(const uInt /*recordNo*/, const String &/*columnValue*/) {
727 }
728 virtual void enterBeamNo(const uInt /*recordNo*/, uInt columnValue) {
729 //printf("%u: BeamNo: %u\n", recordNo, columnValue);
730
731 feedId = (Int)columnValue ;
732
733 // put value
734 *feed1RF = feedId ;
735 *feed2RF = feedId ;
736 }
737 virtual void leaveBeamNo(const uInt /*recordNo*/, uInt /*columnValue*/) {
738 }
739 virtual void enterScanNo(const uInt /*recordNo*/, uInt columnValue) {
740 //printf("%u: ScanNo: %u\n", recordNo, columnValue);
741
742 // put value
743 // SCAN_NUMBER is 0-based in Scantable while 1-based in MS
744 *scanNumberRF = (Int)columnValue + 1 ;
745 }
746 virtual void leaveScanNo(const uInt /*recordNo*/, uInt /*columnValue*/) {
747 subscan = 1 ;
748 }
749 virtual void enterIfNo(const uInt recordNo, uInt columnValue) {
750 //printf("%u: IfNo: %u\n", recordNo, columnValue);
751
752 spwId = (Int)columnValue ;
753 uInt freqId = freqIdCol.asuInt( recordNo ) ;
754
755 Vector<Float> sp = spectraCol( recordNo ) ;
756 uInt nchan = sp.nelements() ;
757 holder->setNchan( nchan ) ;
758
759 addSpectralWindow( spwId, freqId ) ;
760
761 addFeed( feedId, spwId ) ;
762 }
763 virtual void leaveIfNo(const uInt /*recordNo*/, uInt /*columnValue*/) {
764 }
765 virtual void enterSrcType(const uInt /*recordNo*/, Int columnValue) {
766 //printf("%u: SrcType: %d\n", recordNo, columnValue);
767
768 Int stateId = addState( columnValue ) ;
769
770 // put value
771 *stateIdRF = stateId ;
772 }
773 virtual void leaveSrcType(const uInt /*recordNo*/, Int /*columnValue*/) {
774 }
775 virtual void enterCycleNo(const uInt /*recordNo*/, uInt /*columnValue*/) {
776 //printf("%u: CycleNo: %u\n", recordNo, columnValue);
777 }
778 virtual void leaveCycleNo(const uInt /*recordNo*/, uInt /*columnValue*/) {
779 }
780 virtual void enterTime(const uInt recordNo, Double columnValue) {
781 //printf("%u: Time: %f\n", recordNo, columnValue);
782
783 Double timeSec = columnValue * 86400.0 ;
784 Double interval = intervalCol.asdouble( recordNo ) ;
785
786 if ( ptName.empty() ) {
787 Vector<Double> dir = directionCol( recordNo ) ;
788 Vector<Double> rate = scanRateCol( recordNo ) ;
789 if ( anyNE( rate, 0.0 ) ) {
790 Matrix<Double> msdir( 2, 2 ) ;
791 msdir.column( 0 ) = dir ;
792 msdir.column( 1 ) = rate ;
793 addPointing( timeSec, interval, msdir ) ;
794 }
795 else {
796 Matrix<Double> msdir( 2, 1 ) ;
797 msdir.column( 0 ) = dir ;
798 addPointing( timeSec, interval, msdir ) ;
799 }
800 }
801
802 // put value
803 *timeRF = timeSec ;
804 *timeCentroidRF = timeSec ;
805 *intervalRF = interval ;
806 *exposureRF = interval ;
807 }
808 virtual void leaveTime(const uInt /*recordNo*/, Double /*columnValue*/) {
809 if ( holder->nPol() > 0 ) {
810 Int polId = addPolarization() ;
811 Int ddId = addDataDescription( polId, spwId ) ;
812
813 // put field
814 *dataDescIdRF = ddId ;
815 holder->post() ;
816
817 // commit row
818 row.put( rowidx ) ;
819 rowidx++ ;
820
821 // reset holder
822 holder->reset() ;
823 }
824 }
825 virtual void enterPolNo(const uInt /*recordNo*/, uInt /*columnValue*/) {
826 //printf("%u: PolNo: %d\n", recordNo, columnValue);
827 }
828 virtual void leavePolNo(const uInt /*recordNo*/, uInt /*columnValue*/) {
829 }
830
831 virtual Bool visitRecord(const uInt recordNo,
832 const String &/*fieldName*/,
833 const uInt /*beamNo*/,
834 const uInt /*scanNo*/,
835 const uInt /*ifNo*/,
836 const Int /*srcType*/,
837 const uInt /*cycleNo*/,
838 const Double /*time*/,
839 const uInt polNo) {
840 //printf("%u: %s, %u, %u, %u, %d, %u, %f, %d\n", recordNo,
841 // fieldName.c_str(), beamNo, scanNo, ifNo, srcType, cycleNo, time, polNo);
842
843 Vector<Float> sp = spectraCol( recordNo ) ;
844 Vector<uChar> tmp = flagtraCol( recordNo ) ;
845 Vector<Bool> fl( tmp.shape() ) ;
846 convertArray( fl, tmp ) ;
847 Bool flr = (Bool)flagRowCol.asuInt( recordNo ) ;
848 holder->accumulate( polNo, sp, fl, flr ) ;
849
850 return True ;
851 }
852
853 virtual void finish() {
854 BaseMSWriterVisitor::finish();
855 //printf("Total: %u\n", count);
856
857 // remove rows
858 if ( ms.nrow() > rowidx ) {
859 uInt numRemove = ms.nrow() - rowidx ;
860 //cout << "numRemove = " << numRemove << endl ;
861 Vector<uInt> rows( numRemove ) ;
862 indgen( rows, rowidx ) ;
863 ms.removeRow( rows ) ;
864 }
865
866 // fill empty SPECTRAL_WINDOW rows
867 infillSpectralWindow() ;
868 }
869
870 void dataColumnName( String name )
871 {
872 if ( name == "DATA" )
873 holder = new ComplexDataHolder( row, poltype ) ;
874 else if ( name == "FLOAT_DATA" )
875 holder = new FloatDataHolder( row, poltype ) ;
876 }
877 void pointingTableName( String name ) {
878 ptName = name ;
879 }
880 void setSourceRecord( Record &r ) {
881 srcRec = r ;
882 }
883private:
884 void addField( Int &fid, String &fname, String &srcName,
885 Vector<Double> &sdir, Vector<Double> &srate,
886 Double &tSec )
887 {
888 uInt nrow = fieldtab.nrow() ;
889 while( (Int)nrow <= fid ) {
890 fieldtab.addRow( 1, True ) ;
891 nrow++ ;
892 }
893
894 Matrix<Double> dir ;
895 Int numPoly = 0 ;
896 if ( anyNE( srate, 0.0 ) ) {
897 dir.resize( 2, 2 ) ;
898 dir.column( 0 ) = sdir ;
899 dir.column( 1 ) = srate ;
900 numPoly = 1 ;
901 }
902 else {
903 dir.resize( 2, 1 ) ;
904 dir.column( 0 ) = sdir ;
905 }
906 srcId = srcRec.asInt( srcName ) ;
907
908 TableRow tr( fieldtab ) ;
909 TableRecord &r = tr.record() ;
910 putField( "NAME", r, fname ) ;
911 putField( "NUM_POLY", r, numPoly ) ;
912 putField( "TIME", r, tSec ) ;
913 putField( "SOURCE_ID", r, srcId ) ;
914 defineField( "DELAY_DIR", r, dir ) ;
915 defineField( "REFERENCE_DIR", r, dir ) ;
916 defineField( "PHASE_DIR", r, dir ) ;
917 tr.put( fid ) ;
918
919 // for POINTING table
920 *poNameRF = fname ;
921 }
922 Int addState( Int &id )
923 {
924 String obsMode ;
925 Bool isSignal ;
926 Double tnoise ;
927 Double tload ;
928 queryType( id, obsMode, isSignal, tnoise, tload ) ;
929
930 String key = obsMode+"_"+String::toString( subscan ) ;
931 Int idx = -1 ;
932 uInt nEntry = stateEntry.nelements() ;
933 for ( uInt i = 0 ; i < nEntry ; i++ ) {
934 if ( stateEntry[i] == key ) {
935 idx = i ;
936 break ;
937 }
938 }
939 if ( idx == -1 ) {
940 uInt nrow = statetab.nrow() ;
941 statetab.addRow( 1, True ) ;
942 TableRow tr( statetab ) ;
943 TableRecord &r = tr.record() ;
944 putField( "OBS_MODE", r, obsMode ) ;
945 putField( "SIG", r, isSignal ) ;
946 isSignal = !isSignal ;
947 putField( "REF", r, isSignal ) ;
948 putField( "CAL", r, tnoise ) ;
949 putField( "LOAD", r, tload ) ;
950 tr.put( nrow ) ;
951 idx = nrow ;
952
953 stateEntry.resize( nEntry+1, True ) ;
954 stateEntry[nEntry] = key ;
955 }
956 subscan++ ;
957
958 return idx ;
959 }
960 void addPointing( Double &tSec, Double &interval, Matrix<Double> &dir )
961 {
962 uInt nrow = potab.nrow() ;
963 potab.addRow() ;
964
965 *poNumPolyRF = dir.ncolumn() - 1 ;
966 *poTimeRF = tSec ;
967 *poTimeOriginRF = tSec ;
968 *poIntervalRF = interval ;
969 poDirectionRF.define( dir ) ;
970 poTargetRF.define( dir ) ;
971 porow.put( nrow ) ;
972 }
973 Int addPolarization()
974 {
975 Int idx = -1 ;
976 Vector<Int> corrType = holder->corrTypeInt() ;
977 uInt nEntry = polEntry.size() ;
978 for ( uInt i = 0 ; i < nEntry ; i++ ) {
979 if ( polEntry[i].conform( corrType ) && allEQ( polEntry[i], corrType ) ) {
980 idx = i ;
981 break ;
982 }
983 }
984
985 Int numCorr = holder->nPol() ;
986 Matrix<Int> corrProduct = corrProductTemplate[numCorr].copy() ;
987 if ( numCorr == 1 && (corrType[0] == Stokes::YY || corrType[0] == Stokes::LL ) ) {
988 corrProduct = 1;
989 }
990
991 if ( idx == -1 ) {
992 uInt nrow = poltab.nrow() ;
993 poltab.addRow( 1, True ) ;
994 TableRow tr( poltab ) ;
995 TableRecord &r = tr.record() ;
996 putField( "NUM_CORR", r, numCorr ) ;
997 defineField( "CORR_TYPE", r, corrType ) ;
998 defineField( "CORR_PRODUCT", r, corrProduct ) ;
999 tr.put( nrow ) ;
1000 idx = nrow ;
1001
1002 polEntry.resize( nEntry+1 ) ;
1003 polEntry[nEntry] = corrType ;
1004 }
1005
1006 return idx ;
1007 }
1008 Int addDataDescription( Int pid, Int sid )
1009 {
1010 Int idx = -1 ;
1011 uInt nItem = 2 ;
1012 uInt len = ddEntry.nelements() ;
1013 uInt nEntry = len / nItem ;
1014 const Int *dd_p = ddEntry.storage() ;
1015 for ( uInt i = 0 ; i < nEntry ; i++ ) {
1016 Int pol = *dd_p ;
1017 dd_p++ ;
1018 Int spw = *dd_p ;
1019 dd_p++ ;
1020 if ( pid == pol && sid == spw ) {
1021 idx = i ;
1022 break ;
1023 }
1024 }
1025
1026 if ( idx == -1 ) {
1027 uInt nrow = ddtab.nrow() ;
1028 ddtab.addRow( 1, True ) ;
1029 TableRow tr( ddtab ) ;
1030 TableRecord &r = tr.record() ;
1031 putField( "POLARIZATION_ID", r, pid ) ;
1032 putField( "SPECTRAL_WINDOW_ID", r, sid ) ;
1033 tr.put( nrow ) ;
1034 idx = nrow ;
1035
1036 ddEntry.resize( len+nItem ) ;
1037 ddEntry[len] = pid ;
1038 ddEntry[len+1] = sid ;
1039 }
1040
1041 return idx ;
1042 }
1043 void infillSpectralWindow()
1044 {
1045 ROScalarColumn<Int> nchanCol( spwtab, "NUM_CHAN" ) ;
1046 Vector<Int> nchan = nchanCol.getColumn() ;
1047 TableRow tr( spwtab ) ;
1048 TableRecord &r = tr.record() ;
1049 Int mfr = 1 ;
1050 Vector<Double> dummy( 1, 0.0 ) ;
1051 putField( "MEAS_FREQ_REF", r, mfr ) ;
1052 defineField( "CHAN_FREQ", r, dummy ) ;
1053 defineField( "CHAN_WIDTH", r, dummy ) ;
1054 defineField( "EFFECTIVE_BW", r, dummy ) ;
1055 defineField( "RESOLUTION", r, dummy ) ;
1056
1057 for ( uInt i = 0 ; i < spwtab.nrow() ; i++ ) {
1058 if ( nchan[i] == 0 )
1059 tr.put( i ) ;
1060 }
1061 }
1062 void addSpectralWindow( Int sid, uInt fid )
1063 {
1064 if (processedFreqId.find((uInt)fid) == processedFreqId.end()
1065 || processedIFNO.find((uInt)sid) == processedIFNO.end() ) {
1066 uInt nrow = spwtab.nrow() ;
1067 while( (Int)nrow <= sid ) {
1068 spwtab.addRow( 1, True ) ;
1069 nrow++ ;
1070 }
1071 processedFreqId.insert((uInt)fid);
1072 processedIFNO.insert((uInt)sid);
1073 }
1074 else {
1075 return ;
1076 }
1077
1078
1079 Double rp = refpix[fid] ;
1080 Double rv = refval[fid] ;
1081 Double ic = increment[fid] ;
1082
1083 Int mfrInt = (Int)freqframe ;
1084 Int nchan = holder->nChan() ;
1085 Double bw = nchan * abs( ic ) ;
1086 Double reffreq = rv - rp * ic ;
1087 Int netsb = 0 ; // USB->0, LSB->1
1088 if ( ic < 0 )
1089 netsb = 1 ;
1090 Vector<Double> res( nchan, abs(ic) ) ;
1091 Vector<Double> cw( nchan, ic ) ;
1092 Vector<Double> chanf( nchan ) ;
1093 indgen( chanf, reffreq, ic ) ;
1094
1095 TableRow tr( spwtab ) ;
1096 TableRecord &r = tr.record() ;
1097 putField( "MEAS_FREQ_REF", r, mfrInt ) ;
1098 putField( "NUM_CHAN", r, nchan ) ;
1099 putField( "TOTAL_BANDWIDTH", r, bw ) ;
1100 putField( "REF_FREQUENCY", r, reffreq ) ;
1101 putField( "NET_SIDEBAND", r, netsb ) ;
1102 defineField( "RESOLUTION", r, res ) ;
1103// defineField( "CHAN_WIDTH", r, res ) ;
1104 defineField( "CHAN_WIDTH", r, cw ) ;
1105 defineField( "EFFECTIVE_BW", r, res ) ;
1106 defineField( "CHAN_FREQ", r, chanf ) ;
1107 tr.put( sid ) ;
1108 }
1109 void addFeed( Int fid, Int sid )
1110 {
1111 Int idx = -1 ;
1112 uInt nItem = 2 ;
1113 uInt len = feedEntry.nelements() ;
1114 uInt nEntry = len / nItem ;
1115 const Int *fe_p = feedEntry.storage() ;
1116 for ( uInt i = 0 ; i < nEntry ; i++ ) {
1117 Int feed = *fe_p ;
1118 fe_p++ ;
1119 Int spw = *fe_p ;
1120 fe_p++ ;
1121 if ( fid == feed && sid == spw ) {
1122 idx = i ;
1123 break ;
1124 }
1125 }
1126
1127
1128 if ( idx == -1 ) {
1129 uInt nrow = feedtab.nrow() ;
1130 feedtab.addRow( 1, True ) ;
1131 Int numReceptors = 2 ;
1132 Vector<String> polType( numReceptors ) ;
1133 Matrix<Double> beamOffset( 2, numReceptors, 0.0 ) ;
1134 Vector<Double> receptorAngle( numReceptors, 0.0 ) ;
1135 if ( poltype == "linear" ) {
1136 polType[0] = "X" ;
1137 polType[1] = "Y" ;
1138 }
1139 else if ( poltype == "circular" ) {
1140 polType[0] = "R" ;
1141 polType[1] = "L" ;
1142 }
1143 else {
1144 polType[0] = "X" ;
1145 polType[1] = "Y" ;
1146 }
1147 Matrix<Complex> polResponse( numReceptors, numReceptors, 0.0 ) ;
1148
1149 TableRow tr( feedtab ) ;
1150 TableRecord &r = tr.record() ;
1151 putField( "FEED_ID", r, fid ) ;
1152 putField( "BEAM_ID", r, fid ) ;
1153 Int tmp = 0 ;
1154 putField( "ANTENNA_ID", r, tmp ) ;
1155 putField( "SPECTRAL_WINDOW_ID", r, sid ) ;
1156 putField( "NUM_RECEPTORS", r, numReceptors ) ;
1157 defineField( "POLARIZATION_TYPE", r, polType ) ;
1158 defineField( "BEAM_OFFSET", r, beamOffset ) ;
1159 defineField( "RECEPTOR_ANGLE", r, receptorAngle ) ;
1160 defineField( "POL_RESPONSE", r, polResponse ) ;
1161 tr.put( nrow ) ;
1162
1163 feedEntry.resize( len+nItem ) ;
1164 feedEntry[len] = fid ;
1165 feedEntry[len+1] = sid ;
1166 }
1167 }
1168 void initPolarization()
1169 {
1170 const TableRecord &keys = table.keywordSet() ;
1171 poltype = keys.asString( "POLTYPE" ) ;
1172
1173 initCorrProductTemplate() ;
1174 }
1175 void initFrequencies()
1176 {
1177 const TableRecord &keys = table.keywordSet() ;
1178 Table tab = keys.asTable( "FREQUENCIES" ) ;
1179 ROScalarColumn<uInt> idcol( tab, "ID" ) ;
1180 ROScalarColumn<Double> rpcol( tab, "REFPIX" ) ;
1181 ROScalarColumn<Double> rvcol( tab, "REFVAL" ) ;
1182 ROScalarColumn<Double> iccol( tab, "INCREMENT" ) ;
1183 Vector<uInt> id = idcol.getColumn() ;
1184 Vector<Double> rp = rpcol.getColumn() ;
1185 Vector<Double> rv = rvcol.getColumn() ;
1186 Vector<Double> ic = iccol.getColumn() ;
1187 for ( uInt i = 0 ; i < id.nelements() ; i++ ) {
1188 refpix.insert( pair<uInt,Double>( id[i], rp[i] ) ) ;
1189 refval.insert( pair<uInt,Double>( id[i], rv[i] ) ) ;
1190 increment.insert( pair<uInt,Double>( id[i], ic[i] ) ) ;
1191 }
1192 String frameStr = tab.keywordSet().asString( "BASEFRAME" ) ;
1193 MFrequency::getType( freqframe, frameStr ) ;
1194 }
1195 void attachSubtables()
1196 {
1197 //const TableRecord &keys = table.keywordSet() ;
1198 TableRecord &mskeys = ms.rwKeywordSet() ;
1199
1200 // FIELD table
1201 fieldtab = mskeys.asTable( "FIELD" ) ;
1202
1203 // SPECTRAL_WINDOW table
1204 spwtab = mskeys.asTable( "SPECTRAL_WINDOW" ) ;
1205
1206 // POINTING table
1207 potab = mskeys.asTable( "POINTING" ) ;
1208
1209 // POLARIZATION table
1210 poltab = mskeys.asTable( "POLARIZATION" ) ;
1211
1212 // DATA_DESCRIPTION table
1213 ddtab = mskeys.asTable( "DATA_DESCRIPTION" ) ;
1214
1215 // STATE table
1216 statetab = mskeys.asTable( "STATE" ) ;
1217
1218 // FEED table
1219 feedtab = mskeys.asTable( "FEED" ) ;
1220 }
1221 void attachMain()
1222 {
1223 TableRecord &r = row.record() ;
1224 dataDescIdRF.attachToRecord( r, "DATA_DESC_ID" ) ;
1225 timeRF.attachToRecord( r, "TIME" ) ;
1226 timeCentroidRF.attachToRecord( r, "TIME_CENTROID" ) ;
1227 intervalRF.attachToRecord( r, "INTERVAL" ) ;
1228 exposureRF.attachToRecord( r, "EXPOSURE" ) ;
1229 fieldIdRF.attachToRecord( r, "FIELD_ID" ) ;
1230 feed1RF.attachToRecord( r, "FEED1" ) ;
1231 feed2RF.attachToRecord( r, "FEED2" ) ;
1232 scanNumberRF.attachToRecord( r, "SCAN_NUMBER" ) ;
1233 stateIdRF.attachToRecord( r, "STATE_ID" ) ;
1234
1235 // constant values
1236 //Int id = 0 ;
1237 RecordFieldPtr<Int> intRF( r, "OBSERVATION_ID" ) ;
1238 *intRF = 0 ;
1239 intRF.attachToRecord( r, "ANTENNA1" ) ;
1240 *intRF = 0 ;
1241 intRF.attachToRecord( r, "ANTENNA2" ) ;
1242 *intRF = 0 ;
1243 intRF.attachToRecord( r, "ARRAY_ID" ) ;
1244 *intRF = 0 ;
1245 intRF.attachToRecord( r, "PROCESSOR_ID" ) ;
1246 *intRF = 0 ;
1247 RecordFieldPtr< Vector<Double> > arrayRF( r, "UVW" ) ;
1248 arrayRF.define( Vector<Double>( 3, 0.0 ) ) ;
1249 }
1250 void attachPointing()
1251 {
1252 porow = TableRow( potab ) ;
1253 TableRecord &r = porow.record() ;
1254 poNumPolyRF.attachToRecord( r, "NUM_POLY" ) ;
1255 poTimeRF.attachToRecord( r, "TIME" ) ;
1256 poTimeOriginRF.attachToRecord( r, "TIME_ORIGIN" ) ;
1257 poIntervalRF.attachToRecord( r, "INTERVAL" ) ;
1258 poNameRF.attachToRecord( r, "NAME" ) ;
1259 poDirectionRF.attachToRecord( r, "DIRECTION" ) ;
1260 poTargetRF.attachToRecord( r, "TARGET" ) ;
1261
1262 // constant values
1263 RecordFieldPtr<Int> antIdRF( r, "ANTENNA_ID" ) ;
1264 *antIdRF = 0 ;
1265 RecordFieldPtr<Bool> trackingRF( r, "TRACKING" ) ;
1266 *trackingRF = True ;
1267 }
1268 void queryType( Int type, String &stype, Bool &b, Double &t, Double &l )
1269 {
1270 t = 0.0 ;
1271 l = 0.0 ;
1272
1273 String sep1="#" ;
1274 String sep2="," ;
1275 String target="OBSERVE_TARGET" ;
1276 String atmcal="CALIBRATE_TEMPERATURE" ;
1277 String onstr="ON_SOURCE" ;
1278 String offstr="OFF_SOURCE" ;
1279 String pswitch="POSITION_SWITCH" ;
1280 String nod="NOD" ;
1281 String fswitch="FREQUENCY_SWITCH" ;
1282 String sigstr="SIG" ;
1283 String refstr="REF" ;
1284 String unspecified="UNSPECIFIED" ;
1285 String ftlow="LOWER" ;
1286 String fthigh="HIGHER" ;
1287 switch ( type ) {
1288 case SrcType::PSON:
1289 stype = target+sep1+onstr+sep2+pswitch ;
1290 b = True ;
1291 break ;
1292 case SrcType::PSOFF:
1293 stype = target+sep1+offstr+sep2+pswitch ;
1294 b = False ;
1295 break ;
1296 case SrcType::NOD:
1297 stype = target+sep1+onstr+sep2+nod ;
1298 b = True ;
1299 break ;
1300 case SrcType::FSON:
1301 stype = target+sep1+onstr+sep2+fswitch+sep1+sigstr ;
1302 b = True ;
1303 break ;
1304 case SrcType::FSOFF:
1305 stype = target+sep1+onstr+sep2+fswitch+sep1+refstr ;
1306 b = False ;
1307 break ;
1308 case SrcType::SKY:
1309 stype = atmcal+sep1+offstr+sep2+unspecified ;
1310 b = False ;
1311 break ;
1312 case SrcType::HOT:
1313 stype = atmcal+sep1+offstr+sep2+unspecified ;
1314 b = False ;
1315 break ;
1316 case SrcType::WARM:
1317 stype = atmcal+sep1+offstr+sep2+unspecified ;
1318 b = False ;
1319 break ;
1320 case SrcType::COLD:
1321 stype = atmcal+sep1+offstr+sep2+unspecified ;
1322 b = False ;
1323 break ;
1324 case SrcType::PONCAL:
1325 stype = atmcal+sep1+onstr+sep2+pswitch ;
1326 b = True ;
1327 break ;
1328 case SrcType::POFFCAL:
1329 stype = atmcal+sep1+offstr+sep2+pswitch ;
1330 b = False ;
1331 break ;
1332 case SrcType::NODCAL:
1333 stype = atmcal+sep1+onstr+sep2+nod ;
1334 b = True ;
1335 break ;
1336 case SrcType::FONCAL:
1337 stype = atmcal+sep1+onstr+sep2+fswitch+sep1+sigstr ;
1338 b = True ;
1339 break ;
1340 case SrcType::FOFFCAL:
1341 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+refstr ;
1342 b = False ;
1343 break ;
1344 case SrcType::FSLO:
1345 stype = target+sep1+onstr+sep2+fswitch+sep1+ftlow ;
1346 b = True ;
1347 break ;
1348 case SrcType::FLOOFF:
1349 stype = target+sep1+offstr+sep2+fswitch+sep1+ftlow ;
1350 b = False ;
1351 break ;
1352 case SrcType::FLOSKY:
1353 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+ftlow ;
1354 b = False ;
1355 break ;
1356 case SrcType::FLOHOT:
1357 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+ftlow ;
1358 b = False ;
1359 break ;
1360 case SrcType::FLOWARM:
1361 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+ftlow ;
1362 b = False ;
1363 break ;
1364 case SrcType::FLOCOLD:
1365 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+ftlow ;
1366 b = False ;
1367 break ;
1368 case SrcType::FSHI:
1369 stype = target+sep1+onstr+sep2+fswitch+sep1+fthigh ;
1370 b = True ;
1371 break ;
1372 case SrcType::FHIOFF:
1373 stype = target+sep1+offstr+sep2+fswitch+sep1+fthigh ;
1374 b = False ;
1375 break ;
1376 case SrcType::FHISKY:
1377 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+fthigh ;
1378 b = False ;
1379 break ;
1380 case SrcType::FHIHOT:
1381 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+fthigh ;
1382 b = False ;
1383 break ;
1384 case SrcType::FHIWARM:
1385 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+fthigh ;
1386 b = False ;
1387 break ;
1388 case SrcType::FHICOLD:
1389 stype = atmcal+sep1+offstr+sep2+fswitch+sep1+fthigh ;
1390 b = False ;
1391 break ;
1392 case SrcType::SIG:
1393 stype = target+sep1+onstr+sep2+unspecified ;
1394 b = True ;
1395 break ;
1396 case SrcType::REF:
1397 stype = target+sep1+offstr+sep2+unspecified ;
1398 b = False ;
1399 break ;
1400 default:
1401 stype = unspecified ;
1402 b = True ;
1403 break ;
1404 }
1405 }
1406 void initCorrProductTemplate()
1407 {
1408 Int n = 1 ;
1409 {
1410 Matrix<Int> c( 2, n, 0 ) ;
1411 corrProductTemplate[n] = c ;
1412 }
1413 n = 2 ;
1414 {
1415 Matrix<Int> c( 2, n, 0 ) ;
1416 c.column( 1 ) = 1 ;
1417 corrProductTemplate[n] = c ;
1418 }
1419 n = 4 ;
1420 {
1421 Matrix<Int> c( 2, n, 0 ) ;
1422 c( 0, 2 ) = 1 ;
1423 c( 0, 3 ) = 1 ;
1424 c( 1, 1 ) = 1 ;
1425 c( 1, 3 ) = 1 ;
1426 corrProductTemplate[n] = c ;
1427 }
1428 }
1429
1430 Table &ms;
1431 TableRow row;
1432 uInt rowidx;
1433 String fieldName;
1434 Int fieldId;
1435 Int srcId;
1436 Int defaultFieldId;
1437 Int spwId;
1438 Int feedId;
1439 Int subscan;
1440 CountedPtr<DataHolder> holder;
1441 String ptName;
1442 Bool useFloat;
1443 String poltype;
1444
1445 // MS subtables
1446 Table spwtab;
1447 Table statetab;
1448 Table ddtab;
1449 Table poltab;
1450 Table fieldtab;
1451 Table feedtab;
1452 Table potab;
1453
1454 // Scantable MAIN columns
1455 ROArrayColumn<Float> spectraCol;
1456 ROArrayColumn<Double> directionCol,scanRateCol,sourceDirectionCol;
1457 ROArrayColumn<uChar> flagtraCol;
1458 ROTableColumn tcalIdCol,intervalCol,flagRowCol,timeCol,freqIdCol,
1459 sourceNameCol,fieldNameCol;
1460
1461 // MS MAIN columns
1462 RecordFieldPtr<Int> dataDescIdRF,fieldIdRF,feed1RF,feed2RF,
1463 scanNumberRF,stateIdRF;
1464 RecordFieldPtr<Double> timeRF,timeCentroidRF,intervalRF,exposureRF;
1465
1466 // MS POINTING columns
1467 TableRow porow;
1468 RecordFieldPtr<Int> poNumPolyRF ;
1469 RecordFieldPtr<Double> poTimeRF,
1470 poTimeOriginRF,
1471 poIntervalRF ;
1472 RecordFieldPtr<String> poNameRF ;
1473 RecordFieldPtr< Matrix<Double> > poDirectionRF,
1474 poTargetRF ;
1475
1476 Vector<String> stateEntry;
1477 Block<Int> ddEntry;
1478 Block<Int> feedEntry;
1479 vector< Vector<Int> > polEntry;
1480 set<uInt> processedFreqId;
1481 set<uInt> processedIFNO;
1482 map<uInt,Double> refpix;
1483 map<uInt,Double> refval;
1484 map<uInt,Double> increment;
1485 MFrequency::Types freqframe;
1486 Record srcRec;
1487 map< Int, Matrix<Int> > corrProductTemplate;
1488};
1489
1490class BaseMSSysCalVisitor: public TableVisitor {
1491 uInt lastRecordNo;
1492 uInt lastBeamNo, lastIfNo, lastPolNo;
1493 Double lastTime;
1494protected:
1495 const Table &table;
1496 uInt count;
1497public:
1498 BaseMSSysCalVisitor(const Table &table)
1499 : table(table)
1500 {
1501 count = 0;
1502 }
1503
1504 virtual void enterBeamNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
1505 virtual void leaveBeamNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
1506 virtual void enterIfNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
1507 virtual void leaveIfNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
1508 virtual void enterPolNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
1509 virtual void leavePolNo(const uInt /*recordNo*/, uInt /*columnValue*/) { }
1510 virtual void enterTime(const uInt /*recordNo*/, Double /*columnValue*/) { }
1511 virtual void leaveTime(const uInt /*recordNo*/, Double /*columnValue*/) { }
1512
1513 virtual Bool visitRecord(const uInt /*recordNo*/,
1514 const uInt /*beamNo*/,
1515 const uInt /*ifNo*/,
1516 const uInt /*polNo*/,
1517 const Double /*time*/) { return True ;}
1518
1519 virtual Bool visit(Bool isFirst, const uInt recordNo,
1520 const uInt nCols, void const *const colValues[]) {
1521 uInt beamNo, ifNo, polNo;
1522 Double time;
1523 { // prologue
1524 uInt i = 0;
1525 {
1526 const uInt *col = (const uInt *)colValues[i++];
1527 beamNo = col[recordNo];
1528 }
1529 {
1530 const uInt *col = (const uInt *)colValues[i++];
1531 ifNo = col[recordNo];
1532 }
1533 {
1534 const Double *col = (const Double *)colValues[i++];
1535 time = col[recordNo];
1536 }
1537 {
1538 const uInt *col = (const uInt *)colValues[i++];
1539 polNo = col[recordNo];
1540 }
1541 assert(nCols == i);
1542 }
1543
1544 if (isFirst) {
1545 enterBeamNo(recordNo, beamNo);
1546 enterIfNo(recordNo, ifNo);
1547 enterTime(recordNo, time);
1548 enterPolNo(recordNo, polNo);
1549 } else {
1550 if (lastBeamNo != beamNo) {
1551 leavePolNo(lastRecordNo, lastPolNo);
1552 leaveTime(lastRecordNo, lastTime);
1553 leaveIfNo(lastRecordNo, lastIfNo);
1554 leaveBeamNo(lastRecordNo, lastBeamNo);
1555
1556 enterBeamNo(recordNo, beamNo);
1557 enterIfNo(recordNo, ifNo);
1558 enterTime(recordNo, time);
1559 enterPolNo(recordNo, polNo);
1560 } else if (lastIfNo != ifNo) {
1561 leavePolNo(lastRecordNo, lastPolNo);
1562 leaveTime(lastRecordNo, lastTime);
1563 leaveIfNo(lastRecordNo, lastIfNo);
1564
1565 enterIfNo(recordNo, ifNo);
1566 enterTime(recordNo, time);
1567 enterPolNo(recordNo, polNo);
1568 } else if (lastTime != time) {
1569 leavePolNo(lastRecordNo, lastPolNo);
1570 leaveTime(lastRecordNo, lastTime);
1571
1572 enterTime(recordNo, time);
1573 enterPolNo(recordNo, polNo);
1574 } else if (lastPolNo != polNo) {
1575 leavePolNo(lastRecordNo, lastPolNo);
1576 enterPolNo(recordNo, polNo);
1577 }
1578 }
1579 count++;
1580 Bool result = visitRecord(recordNo, beamNo, ifNo, polNo, time);
1581
1582 { // epilogue
1583 lastRecordNo = recordNo;
1584
1585 lastBeamNo = beamNo;
1586 lastIfNo = ifNo;
1587 lastPolNo = polNo;
1588 lastTime = time;
1589 }
1590 return result ;
1591 }
1592
1593 virtual void finish() {
1594 if (count > 0) {
1595 leavePolNo(lastRecordNo, lastPolNo);
1596 leaveTime(lastRecordNo, lastTime);
1597 leaveIfNo(lastRecordNo, lastIfNo);
1598 leaveBeamNo(lastRecordNo, lastBeamNo);
1599 }
1600 }
1601};
1602
1603class BaseTsysHolder
1604{
1605public:
1606 BaseTsysHolder( ROArrayColumn<Float> &tsysCol )
1607 : col( tsysCol ),
1608 nchan(0)
1609 {
1610 reset() ;
1611 }
1612 virtual ~BaseTsysHolder() {}
1613 virtual Array<Float> getTsys() = 0 ;
1614 void setNchan( uInt n ) { nchan = n ; }
1615 void appendTsys( uInt row )
1616 {
1617 Vector<Float> v = col( row ) ;
1618 uInt len = tsys.nrow() ;
1619 tsys.resize( len+1, nchan, True ) ;
1620 if ( v.nelements() == nchan )
1621 tsys.row( len ) = v ;
1622 else
1623 tsys.row( len ) = v[0] ;
1624 }
1625 void setTsys( uInt row, uInt idx )
1626 {
1627 if ( idx >= nrow() )
1628 appendTsys( row ) ;
1629 else {
1630 Vector<Float> v = col( row ) ;
1631 if ( v.nelements() == nchan )
1632 tsys.row( idx ) = v ;
1633 else
1634 tsys.row( idx ) = v[0] ;
1635 }
1636 }
1637 void reset()
1638 {
1639 tsys.resize() ;
1640 }
1641 uInt nrow() { return tsys.nrow() ; }
1642 Bool isEffective()
1643 {
1644 return ( !(tsys.empty()) && anyNE( tsys, (Float)1.0 ) ) ;
1645 }
1646 BaseTsysHolder &operator= ( const BaseTsysHolder &v )
1647 {
1648 if ( this != &v )
1649 tsys.assign( v.tsys ) ;
1650 return *this ;
1651 }
1652protected:
1653 ROArrayColumn<Float> col ;
1654 Matrix<Float> tsys ;
1655 uInt nchan ;
1656};
1657
1658class TsysHolder : public BaseTsysHolder
1659{
1660public:
1661 TsysHolder( ROArrayColumn<Float> &tsysCol )
1662 : BaseTsysHolder( tsysCol )
1663 {}
1664 virtual ~TsysHolder() {}
1665 virtual Array<Float> getTsys()
1666 {
1667 return tsys.column( 0 ) ;
1668 }
1669};
1670
1671class TsysSpectrumHolder : public BaseTsysHolder
1672{
1673public:
1674 TsysSpectrumHolder( ROArrayColumn<Float> &tsysCol )
1675 : BaseTsysHolder( tsysCol )
1676 {}
1677 virtual ~TsysSpectrumHolder() {}
1678 virtual Array<Float> getTsys()
1679 {
1680 return tsys ;
1681 }
1682};
1683
1684class BaseTcalProcessor
1685{
1686public:
1687 BaseTcalProcessor( ROArrayColumn<Float> &tcalCol )
1688 : col( tcalCol )
1689 {}
1690 virtual ~BaseTcalProcessor() {}
1691 void setTcalId( Vector<uInt> &tcalId ) { id.assign( tcalId ) ; }
1692 virtual Array<Float> getTcal() = 0 ;
1693protected:
1694 ROArrayColumn<Float> col ;
1695 Vector<uInt> id ;
1696};
1697
1698class TcalProcessor : public BaseTcalProcessor
1699{
1700public:
1701 TcalProcessor( ROArrayColumn<Float> &tcalCol )
1702 : BaseTcalProcessor( tcalCol )
1703 {}
1704 virtual ~TcalProcessor() {}
1705 virtual Array<Float> getTcal()
1706 {
1707 uInt npol = id.nelements() ;
1708 Vector<Float> tcal( npol ) ;
1709 for ( uInt ipol = 0 ; ipol < npol ; ipol++ )
1710 tcal[ipol] = col( id[ipol] ).data()[0] ;
1711 //cout << "TcalProcessor: tcal = " << tcal << endl ;
1712 return tcal ;
1713 }
1714};
1715
1716class TcalSpectrumProcessor : public BaseTcalProcessor
1717{
1718public:
1719 TcalSpectrumProcessor( ROArrayColumn<Float> &tcalCol )
1720 : BaseTcalProcessor( tcalCol )
1721 {}
1722 virtual ~TcalSpectrumProcessor() {}
1723 virtual Array<Float> getTcal()
1724 {
1725 uInt npol = id.nelements() ;
1726 Vector<Float> tcal0 = col( 0 ) ;
1727 uInt nchan = tcal0.nelements() ;
1728 Matrix<Float> tcal( npol, nchan ) ;
1729 tcal.row( 0 ) = tcal0 ;
1730 for ( uInt ipol = 1 ; ipol < npol ; ipol++ )
1731 tcal.row( ipol ) = col( id[ipol] ) ;
1732 return tcal ;
1733 }
1734};
1735
1736class MSSysCalVisitor : public BaseMSSysCalVisitor
1737{
1738public:
1739 MSSysCalVisitor( const Table &from, Table &to )
1740 : BaseMSSysCalVisitor( from ),
1741 sctab( to ),
1742 rowidx( 0 ),
1743 polno_()
1744 {
1745 scrow = TableRow( sctab ) ;
1746
1747 lastTcalId.resize() ;
1748 theTcalId.resize() ;
1749 startTime = 0.0 ;
1750 endTime = 0.0 ;
1751
1752 const TableRecord &keys = table.keywordSet() ;
1753 Table tcalTable = keys.asTable( "TCAL" ) ;
1754 tcalCol.attach( tcalTable, "TCAL" ) ;
1755 tsysCol.attach( table, "TSYS" ) ;
1756 tcalIdCol.attach( table, "TCAL_ID" ) ;
1757 intervalCol.attach( table, "INTERVAL" ) ;
1758 effectiveTcal.resize( tcalTable.nrow() ) ;
1759 for ( uInt irow = 0 ; irow < tcalTable.nrow() ; irow++ ) {
1760 if ( allEQ( tcalCol( irow ), (Float)1.0 ) )
1761 effectiveTcal[irow] = False ;
1762 else
1763 effectiveTcal[irow] = True ;
1764 }
1765
1766 TableRecord &r = scrow.record() ;
1767 RecordFieldPtr<Int> antennaIdRF( r, "ANTENNA_ID" ) ;
1768 *antennaIdRF = 0 ;
1769 feedIdRF.attachToRecord( r, "FEED_ID" ) ;
1770 specWinIdRF.attachToRecord( r, "SPECTRAL_WINDOW_ID" ) ;
1771 timeRF.attachToRecord( r, "TIME" ) ;
1772 intervalRF.attachToRecord( r, "INTERVAL" ) ;
1773 if ( r.isDefined( "TCAL" ) ) {
1774 tcalRF.attachToRecord( r, "TCAL" ) ;
1775 tcalProcessor = new TcalProcessor( tcalCol ) ;
1776 }
1777 else if ( r.isDefined( "TCAL_SPECTRUM" ) ) {
1778 tcalRF.attachToRecord( r, "TCAL_SPECTRUM" ) ;
1779 tcalProcessor = new TcalSpectrumProcessor( tcalCol ) ;
1780 }
1781 if ( r.isDefined( "TSYS" ) ) {
1782 tsysRF.attachToRecord( r, "TSYS" ) ;
1783 theTsys = new TsysHolder( tsysCol ) ;
1784 lastTsys = new TsysHolder( tsysCol ) ;
1785 }
1786 else {
1787 tsysRF.attachToRecord( r, "TSYS_SPECTRUM" ) ;
1788 theTsys = new TsysSpectrumHolder( tsysCol ) ;
1789 lastTsys = new TsysSpectrumHolder( tsysCol ) ;
1790 }
1791
1792 }
1793
1794 virtual void enterBeamNo(const uInt /*recordNo*/, uInt columnValue)
1795 {
1796 *feedIdRF = (Int)columnValue ;
1797 }
1798 virtual void leaveBeamNo(const uInt /*recordNo*/, uInt /*columnValue*/)
1799 {
1800 }
1801 virtual void enterIfNo(const uInt recordNo, uInt columnValue)
1802 {
1803 //cout << "enterIfNo" << endl ;
1804 ROArrayColumn<Float> sp( table, "SPECTRA" ) ;
1805 uInt nchan = sp( recordNo ).nelements() ;
1806 theTsys->setNchan( nchan ) ;
1807 lastTsys->setNchan( nchan ) ;
1808
1809 *specWinIdRF = (Int)columnValue ;
1810 }
1811 virtual void leaveIfNo(const uInt /*recordNo*/, uInt /*columnValue*/)
1812 {
1813 //cout << "leaveIfNo" << endl ;
1814 post() ;
1815 reset(true);
1816 startTime = 0.0 ;
1817 endTime = 0.0 ;
1818 }
1819 virtual void enterTime(const uInt recordNo, Double columnValue)
1820 {
1821 //cout << "enterTime" << endl ;
1822 interval = intervalCol.asdouble( recordNo ) ;
1823 // start time and end time
1824 if ( startTime == 0.0 ) {
1825 startTime = columnValue * 86400.0 - 0.5 * interval ;
1826 endTime = columnValue * 86400.0 + 0.5 * interval ;
1827 }
1828 }
1829 virtual void leaveTime(const uInt /*recordNo*/, Double columnValue)
1830 {
1831 //cout << "leaveTime" << endl ;
1832 if ( isUpdated() ) {
1833 post() ;
1834 *lastTsys = *theTsys ;
1835 lastTcalId = theTcalId ;
1836 reset(false);
1837 startTime = columnValue * 86400.0 - 0.5 * interval ;
1838 endTime = columnValue * 86400.0 + 0.5 * interval ;
1839 }
1840 else {
1841 endTime = columnValue * 86400.0 + 0.5 * interval ;
1842 }
1843 }
1844 virtual void enterPolNo(const uInt recordNo, uInt columnValue)
1845 {
1846 //cout << "enterPolNo" << endl ;
1847 Vector<Float> tsys = tsysCol( recordNo ) ;
1848 uInt tcalId = tcalIdCol.asuInt( recordNo ) ;
1849 polno_.insert( columnValue ) ;
1850 uInt numPol = polno_.size() ;
1851 if ( lastTsys->nrow() < numPol )
1852 lastTsys->appendTsys( recordNo ) ;
1853 if ( lastTcalId.nelements() <= numPol )
1854 appendTcalId( lastTcalId, tcalId, numPol-1 ) ;
1855 if ( theTsys->nrow() < numPol )
1856 theTsys->appendTsys( recordNo ) ;
1857 else {
1858 theTsys->setTsys( recordNo, numPol-1 ) ;
1859 }
1860 if ( theTcalId.nelements() < numPol )
1861 appendTcalId( theTcalId, tcalId, numPol-1 ) ;
1862 else
1863 setTcalId( theTcalId, tcalId, numPol-1 ) ;
1864 }
1865 virtual void leavePolNo( const uInt /*recordNo*/, uInt /*columnValue*/ )
1866 {
1867 }
1868
1869private:
1870 void reset(bool completely)
1871 {
1872 if (completely) {
1873 lastTsys->reset() ;
1874 lastTcalId.resize() ;
1875 }
1876 theTsys->reset() ;
1877 theTcalId.resize() ;
1878 polno_.clear();
1879 }
1880 void appendTcalId( Vector<uInt> &v, uInt &elem, uInt polId )
1881 {
1882 v.resize( polId+1, True ) ;
1883 v[polId] = elem ;
1884 }
1885 void setTcalId( Vector<uInt> &v, uInt &elem, uInt polId )
1886 {
1887 v[polId] = elem ;
1888 }
1889 void post()
1890 {
1891 // check if given Tcal and Tsys is effective
1892 Bool isEffective = False ;
1893 for ( uInt ipol = 0 ; ipol < lastTcalId.nelements() ; ipol++ ) {
1894 if ( effectiveTcal[lastTcalId[ipol]] ) {
1895 isEffective = True ;
1896 break ;
1897 }
1898 }
1899 if ( !isEffective ) {
1900 if ( !(lastTsys->isEffective()) )
1901 return ;
1902 }
1903
1904 //cout << " interval: " << (endTime-startTime) << " lastTcalId = " << lastTcalId << endl ;
1905 Double midTime = 0.5 * ( startTime + endTime ) ;
1906 Double interval = endTime - startTime ;
1907 *timeRF = midTime ;
1908 *intervalRF = interval ;
1909 tcalProcessor->setTcalId( lastTcalId ) ;
1910 Array<Float> tcal = tcalProcessor->getTcal() ;
1911 Array<Float> tsys = lastTsys->getTsys() ;
1912 tcalRF.define( tcal ) ;
1913 tsysRF.define( tsys ) ;
1914 sctab.addRow( 1, True ) ;
1915 scrow.put( rowidx ) ;
1916 rowidx++ ;
1917 }
1918
1919 Bool isUpdated()
1920 {
1921 Bool ret = (anyNE( theTcalId, lastTcalId ) || anyNE( theTsys->getTsys(), lastTsys->getTsys() )) ;
1922 return ret ;
1923 }
1924
1925 Table &sctab;
1926 TableRow scrow;
1927 uInt rowidx;
1928
1929 Double startTime,endTime,interval;
1930
1931 CountedPtr<BaseTsysHolder> lastTsys,theTsys;
1932 Vector<uInt> lastTcalId,theTcalId;
1933 set<uInt> polno_;
1934 CountedPtr<BaseTcalProcessor> tcalProcessor ;
1935 Vector<Bool> effectiveTcal;
1936
1937 RecordFieldPtr<Int> feedIdRF,specWinIdRF;
1938 RecordFieldPtr<Double> timeRF,intervalRF;
1939 RecordFieldPtr< Array<Float> > tcalRF,tsysRF;
1940
1941 ROArrayColumn<Float> tsysCol,tcalCol;
1942 ROTableColumn tcalIdCol,intervalCol;
1943};
1944
1945MSWriter::MSWriter(CountedPtr<Scantable> stable)
1946 : table_(stable),
1947 mstable_(NULL),
1948 isWeather_(False),
1949 tcalSpec_(False),
1950 tsysSpec_(False),
1951 ptTabName_("")
1952{
1953 os_ = LogIO() ;
1954 os_.origin( LogOrigin( "MSWriter", "MSWriter()", WHERE ) ) ;
1955// os_ << "MSWriter::MSWriter()" << LogIO::POST ;
1956
1957 // initialize writer
1958 init() ;
1959}
1960
1961MSWriter::~MSWriter()
1962{
1963 os_.origin( LogOrigin( "MSWriter", "~MSWriter()", WHERE ) ) ;
1964// os_ << "MSWriter::~MSWriter()" << LogIO::POST ;
1965
1966 if ( mstable_ != 0 )
1967 delete mstable_ ;
1968}
1969
1970bool MSWriter::write(const string& filename, const Record& rec)
1971{
1972 os_.origin( LogOrigin( "MSWriter", "write()", WHERE ) ) ;
1973 //double startSec = mathutil::gettimeofday_sec() ;
1974 //os_ << "start MSWriter::write() startSec=" << startSec << LogIO::POST ;
1975
1976 filename_ = filename ;
1977
1978 // parsing MS options
1979 Bool overwrite = False ;
1980 if ( rec.isDefined( "ms" ) ) {
1981 Record msrec = rec.asRecord( "ms" ) ;
1982 if ( msrec.isDefined( "overwrite" ) ) {
1983 overwrite = msrec.asBool( "overwrite" ) ;
1984 }
1985 }
1986
1987 os_ << "Parsing MS options" << endl ;
1988 os_ << " overwrite = " << overwrite << LogIO::POST ;
1989
1990 File file( filename_ ) ;
1991 if ( file.exists() ) {
1992 if ( overwrite ) {
1993 os_ << filename_ << " exists. Overwrite existing data... " << LogIO::POST ;
1994 if ( file.isRegular() ) RegularFile(file).remove() ;
1995 else if ( file.isDirectory() ) Directory(file).removeRecursive() ;
1996 else SymLink(file).remove() ;
1997 }
1998 else {
1999 os_ << LogIO::SEVERE << "ERROR: " << filename_ << " exists..." << LogIO::POST ;
2000 return False ;
2001 }
2002 }
2003
2004 // set up MS
2005 setupMS() ;
2006
2007 // subtables
2008 // OBSERVATION
2009 fillObservation() ;
2010
2011 // ANTENNA
2012 fillAntenna() ;
2013
2014 // PROCESSOR
2015 fillProcessor() ;
2016
2017 // SOURCE
2018 fillSource() ;
2019
2020 // WEATHER
2021 if ( isWeather_ )
2022 fillWeather() ;
2023
2024 // SYSCAL
2025 fillSysCal() ;
2026
2027 /***
2028 * Start iteration using TableVisitor
2029 ***/
2030 {
2031 static const char *cols[] = {
2032 "FIELDNAME", "BEAMNO", "SCANNO", "IFNO", "SRCTYPE", "CYCLENO", "TIME",
2033 "POLNO",
2034 NULL
2035 };
2036 static const TypeManagerImpl<uInt> tmUInt;
2037 static const TypeManagerImpl<Int> tmInt;
2038 static const TypeManagerImpl<Double> tmDouble;
2039 static const TypeManagerImpl<String> tmString;
2040 static const TypeManager *const tms[] = {
2041 &tmString, &tmUInt, &tmUInt, &tmUInt, &tmInt, &tmUInt, &tmDouble, &tmUInt, NULL
2042 };
2043 //double t0 = mathutil::gettimeofday_sec() ;
2044 MSWriterVisitor myVisitor(table_->table(),*mstable_);
2045 //double t1 = mathutil::gettimeofday_sec() ;
2046 //cout << "MSWriterVisitor(): elapsed time " << t1-t0 << " sec" << endl ;
2047 String dataColName = "FLOAT_DATA" ;
2048 if ( useData_ )
2049 dataColName = "DATA" ;
2050 myVisitor.dataColumnName( dataColName ) ;
2051 myVisitor.pointingTableName( ptTabName_ ) ;
2052 myVisitor.setSourceRecord( srcRec_ ) ;
2053 //double t2 = mathutil::gettimeofday_sec() ;
2054 traverseTable(table_->table(), cols, tms, &myVisitor);
2055 //double t3 = mathutil::gettimeofday_sec() ;
2056 //cout << "traverseTable(): elapsed time " << t3-t2 << " sec" << endl ;
2057 }
2058 /***
2059 * End iteration using TableVisitor
2060 ***/
2061
2062 // ASDM tables
2063 const TableRecord &stKeys = table_->table().keywordSet() ;
2064 TableRecord &msKeys = mstable_->rwKeywordSet() ;
2065 uInt nfields = stKeys.nfields() ;
2066 for ( uInt ifield = 0 ; ifield < nfields ; ifield++ ) {
2067 String kname = stKeys.name( ifield ) ;
2068 if ( kname.find( "ASDM" ) != String::npos ) {
2069 String asdmpath = stKeys.asString( ifield ) ;
2070 os_ << "found ASDM table: " << asdmpath << LogIO::POST ;
2071 if ( Table::isReadable( asdmpath ) ) {
2072 Table newAsdmTab( asdmpath, Table::Old ) ;
2073 newAsdmTab.copy( filename_+"/"+kname, Table::New ) ;
2074 os_ << "add subtable: " << kname << LogIO::POST ;
2075 msKeys.defineTable( kname, Table( filename_+"/"+kname, Table::Old ) ) ;
2076 }
2077 }
2078 }
2079
2080 // replace POINTING table with original one if exists
2081 if ( ptTabName_ != "" ) {
2082 delete mstable_ ;
2083 mstable_ = 0 ;
2084 Table newPtTab( ptTabName_, Table::Old ) ;
2085 newPtTab.copy( filename_+"/POINTING", Table::New ) ;
2086 }
2087
2088 //double endSec = mathutil::gettimeofday_sec() ;
2089 //os_ << "end MSWriter::write() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2090
2091 os_ << "Exported data as MS" << LogIO::POST ;
2092
2093 return True ;
2094}
2095
2096void MSWriter::init()
2097{
2098// os_.origin( LogOrigin( "MSWriter", "init()", WHERE ) ) ;
2099// double startSec = mathutil::gettimeofday_sec() ;
2100// os_ << "start MSWriter::init() startSec=" << startSec << LogIO::POST ;
2101
2102 // access to scantable
2103 header_ = table_->getHeader() ;
2104
2105 // FLOAT_DATA? or DATA?
2106 if ( header_.npol > 2 ) {
2107 useFloatData_ = False ;
2108 useData_ = True ;
2109 }
2110 else {
2111 useFloatData_ = True ;
2112 useData_ = False ;
2113 }
2114
2115 // polarization type
2116 polType_ = header_.poltype ;
2117 if ( polType_ == "" )
2118 polType_ = "stokes" ;
2119 else if ( polType_.find( "linear" ) != String::npos )
2120 polType_ = "linear" ;
2121 else if ( polType_.find( "circular" ) != String::npos )
2122 polType_ = "circular" ;
2123 else if ( polType_.find( "stokes" ) != String::npos )
2124 polType_ = "stokes" ;
2125 else if ( polType_.find( "linpol" ) != String::npos )
2126 polType_ = "linpol" ;
2127 else
2128 polType_ = "notype" ;
2129
2130 // Check if some subtables are exists
2131 Bool isTcal = False ;
2132 if ( table_->tcal().table().nrow() != 0 ) {
2133 ROTableColumn col( table_->tcal().table(), "TCAL" ) ;
2134 if ( col.isDefined( 0 ) ) {
2135 os_ << "TCAL table exists: nrow=" << table_->tcal().table().nrow() << LogIO::POST ;
2136 isTcal = True ;
2137 }
2138 else {
2139 os_ << "No TCAL rows" << LogIO::POST ;
2140 }
2141 }
2142 else {
2143 os_ << "No TCAL rows" << LogIO::POST ;
2144 }
2145 if ( table_->weather().table().nrow() != 0 ) {
2146 ROTableColumn col( table_->weather().table(), "TEMPERATURE" ) ;
2147 if ( col.isDefined( 0 ) ) {
2148 os_ << "WEATHER table exists: nrow=" << table_->weather().table().nrow() << LogIO::POST ;
2149 isWeather_ =True ;
2150 }
2151 else {
2152 os_ << "No WEATHER rows" << LogIO::POST ;
2153 }
2154 }
2155 else {
2156 os_ << "No WEATHER rows" << LogIO::POST ;
2157 }
2158
2159 // Are TCAL_SPECTRUM and TSYS_SPECTRUM necessary?
2160 if ( header_.nchan != 1 ) {
2161 if ( isTcal ) {
2162 // examine TCAL subtable
2163 Table tcaltab = table_->tcal().table() ;
2164 ROArrayColumn<Float> tcalCol( tcaltab, "TCAL" ) ;
2165 for ( uInt irow = 0 ; irow < tcaltab.nrow() ; irow++ ) {
2166 if ( tcalCol( irow ).size() != 1 )
2167 tcalSpec_ = True ;
2168 }
2169 }
2170 // examine spectral data
2171 TableIterator iter0( table_->table(), "IFNO" ) ;
2172 while( !iter0.pastEnd() ) {
2173 Table t0( iter0.table() ) ;
2174 ROArrayColumn<Float> sharedFloatArrCol( t0, "SPECTRA" ) ;
2175 uInt len = sharedFloatArrCol( 0 ).size() ;
2176 if ( len != 1 ) {
2177 sharedFloatArrCol.attach( t0, "TSYS" ) ;
2178 if ( sharedFloatArrCol( 0 ).size() != 1 )
2179 tsysSpec_ = True ;
2180 }
2181 iter0.next() ;
2182 }
2183 }
2184
2185 // check if reference for POINTING table exists
2186 const TableRecord &rec = table_->table().keywordSet() ;
2187 if ( rec.isDefined( "POINTING" ) ) {
2188 ptTabName_ = rec.asString( "POINTING" ) ;
2189 if ( !Table::isReadable( ptTabName_ ) ) {
2190 ptTabName_ = "" ;
2191 }
2192 }
2193
2194// double endSec = mathutil::gettimeofday_sec() ;
2195// os_ << "end MSWriter::init() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2196}
2197
2198void MSWriter::setupMS()
2199{
2200// os_.origin( LogOrigin( "MSWriter", "setupMS()", WHERE ) ) ;
2201// double startSec = mathutil::gettimeofday_sec() ;
2202// os_ << "start MSWriter::setupMS() startSec=" << startSec << LogIO::POST ;
2203
2204 String dunit = table_->getHeader().fluxunit ;
2205
2206 TableDesc msDesc = MeasurementSet::requiredTableDesc() ;
2207 if ( useFloatData_ )
2208 MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::FLOAT_DATA, 2 ) ;
2209 else if ( useData_ )
2210 MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::DATA, 2 ) ;
2211
2212 SetupNewTable newtab( filename_, msDesc, Table::New ) ;
2213
2214 mstable_ = new MeasurementSet( newtab ) ;
2215
2216 TableColumn col ;
2217 if ( useFloatData_ )
2218 col.attach( *mstable_, "FLOAT_DATA" ) ;
2219 else if ( useData_ )
2220 col.attach( *mstable_, "DATA" ) ;
2221 col.rwKeywordSet().define( "UNIT", dunit ) ;
2222
2223 // create subtables
2224 TableDesc antennaDesc = MSAntenna::requiredTableDesc() ;
2225 SetupNewTable antennaTab( mstable_->antennaTableName(), antennaDesc, Table::New ) ;
2226 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::ANTENNA ), Table( antennaTab ) ) ;
2227
2228 TableDesc dataDescDesc = MSDataDescription::requiredTableDesc() ;
2229 SetupNewTable dataDescTab( mstable_->dataDescriptionTableName(), dataDescDesc, Table::New ) ;
2230 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DATA_DESCRIPTION ), Table( dataDescTab ) ) ;
2231
2232 TableDesc dopplerDesc = MSDoppler::requiredTableDesc() ;
2233 SetupNewTable dopplerTab( mstable_->dopplerTableName(), dopplerDesc, Table::New ) ;
2234 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DOPPLER ), Table( dopplerTab ) ) ;
2235
2236 TableDesc feedDesc = MSFeed::requiredTableDesc() ;
2237 SetupNewTable feedTab( mstable_->feedTableName(), feedDesc, Table::New ) ;
2238 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FEED ), Table( feedTab ) ) ;
2239
2240 TableDesc fieldDesc = MSField::requiredTableDesc() ;
2241 SetupNewTable fieldTab( mstable_->fieldTableName(), fieldDesc, Table::New ) ;
2242 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FIELD ), Table( fieldTab ) ) ;
2243
2244 TableDesc flagCmdDesc = MSFlagCmd::requiredTableDesc() ;
2245 SetupNewTable flagCmdTab( mstable_->flagCmdTableName(), flagCmdDesc, Table::New ) ;
2246 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FLAG_CMD ), Table( flagCmdTab ) ) ;
2247
2248 TableDesc freqOffsetDesc = MSFreqOffset::requiredTableDesc() ;
2249 SetupNewTable freqOffsetTab( mstable_->freqOffsetTableName(), freqOffsetDesc, Table::New ) ;
2250 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FREQ_OFFSET ), Table( freqOffsetTab ) ) ;
2251
2252 TableDesc historyDesc = MSHistory::requiredTableDesc() ;
2253 SetupNewTable historyTab( mstable_->historyTableName(), historyDesc, Table::New ) ;
2254 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::HISTORY ), Table( historyTab ) ) ;
2255
2256 TableDesc observationDesc = MSObservation::requiredTableDesc() ;
2257 SetupNewTable observationTab( mstable_->observationTableName(), observationDesc, Table::New ) ;
2258 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::OBSERVATION ), Table( observationTab ) ) ;
2259
2260 TableDesc pointingDesc = MSPointing::requiredTableDesc() ;
2261 SetupNewTable pointingTab( mstable_->pointingTableName(), pointingDesc, Table::New ) ;
2262 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POINTING ), Table( pointingTab ) ) ;
2263
2264 TableDesc polarizationDesc = MSPolarization::requiredTableDesc() ;
2265 SetupNewTable polarizationTab( mstable_->polarizationTableName(), polarizationDesc, Table::New ) ;
2266 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POLARIZATION ), Table( polarizationTab ) ) ;
2267
2268 TableDesc processorDesc = MSProcessor::requiredTableDesc() ;
2269 SetupNewTable processorTab( mstable_->processorTableName(), processorDesc, Table::New ) ;
2270 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::PROCESSOR ), Table( processorTab ) ) ;
2271
2272 TableDesc sourceDesc = MSSource::requiredTableDesc() ;
2273 MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::TRANSITION, 1 ) ;
2274 MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::REST_FREQUENCY, 1 ) ;
2275 MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::SYSVEL, 1 ) ;
2276 SetupNewTable sourceTab( mstable_->sourceTableName(), sourceDesc, Table::New ) ;
2277 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SOURCE ), Table( sourceTab ) ) ;
2278
2279 TableDesc spwDesc = MSSpectralWindow::requiredTableDesc() ;
2280 SetupNewTable spwTab( mstable_->spectralWindowTableName(), spwDesc, Table::New ) ;
2281 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SPECTRAL_WINDOW ), Table( spwTab ) ) ;
2282
2283 TableDesc stateDesc = MSState::requiredTableDesc() ;
2284 SetupNewTable stateTab( mstable_->stateTableName(), stateDesc, Table::New ) ;
2285 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::STATE ), Table( stateTab ) ) ;
2286
2287 TableDesc sysCalDesc = MSSysCal::requiredTableDesc() ;
2288 if ( tcalSpec_ )
2289 MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL_SPECTRUM, 2 ) ;
2290 else
2291 MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL, 1 ) ;
2292 if ( tsysSpec_ )
2293 MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS_SPECTRUM, 2 ) ;
2294 else
2295 MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS, 1 ) ;
2296 SetupNewTable sysCalTab( mstable_->sysCalTableName(), sysCalDesc, Table::New ) ;
2297 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SYSCAL ), Table( sysCalTab ) ) ;
2298
2299 TableDesc weatherDesc = MSWeather::requiredTableDesc() ;
2300 MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::TEMPERATURE ) ;
2301 MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::PRESSURE ) ;
2302 MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::REL_HUMIDITY ) ;
2303 MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_SPEED ) ;
2304 MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_DIRECTION ) ;
2305 SetupNewTable weatherTab( mstable_->weatherTableName(), weatherDesc, Table::New ) ;
2306 mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::WEATHER ), Table( weatherTab ) ) ;
2307
2308 mstable_->initRefs() ;
2309
2310// double endSec = mathutil::gettimeofday_sec() ;
2311// os_ << "end MSWriter::setupMS() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2312}
2313
2314void MSWriter::fillObservation()
2315{
2316 //double startSec = mathutil::gettimeofday_sec() ;
2317 //os_ << "start MSWriter::fillObservation() startSec=" << startSec << LogIO::POST ;
2318
2319 // only 1 row
2320 mstable_->observation().addRow( 1, True ) ;
2321 MSObservationColumns msObsCols( mstable_->observation() ) ;
2322 msObsCols.observer().put( 0, header_.observer ) ;
2323 // tentatively put antennaname (from ANTENNA subtable)
2324 String hAntennaName = header_.antennaname ;
2325 String::size_type pos = hAntennaName.find( "//" ) ;
2326 String telescopeName ;
2327 if ( pos != String::npos ) {
2328 telescopeName = hAntennaName.substr( 0, pos ) ;
2329 }
2330 else {
2331 pos = hAntennaName.find( "@" ) ;
2332 telescopeName = hAntennaName.substr( 0, pos ) ;
2333 }
2334// os_ << "telescopeName = " << telescopeName << LogIO::POST ;
2335 msObsCols.telescopeName().put( 0, telescopeName ) ;
2336 msObsCols.project().put( 0, header_.project ) ;
2337 //ScalarMeasColumn<MEpoch> timeCol( table_->table().sort("TIME"), "TIME" ) ;
2338 Table sortedtable = table_->table().sort("TIME") ;
2339 ScalarMeasColumn<MEpoch> timeCol( sortedtable, "TIME" ) ;
2340 Vector<MEpoch> trange( 2 ) ;
2341 trange[0] = timeCol( 0 ) ;
2342 trange[1] = timeCol( table_->nrow()-1 ) ;
2343 msObsCols.timeRangeMeas().put( 0, trange ) ;
2344
2345 //double endSec = mathutil::gettimeofday_sec() ;
2346 //os_ << "end MSWriter::fillObservation() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2347}
2348
2349void MSWriter::antennaProperty( String &name, String &m, String &t, Double &d )
2350{
2351 name.upcase() ;
2352
2353 m = "ALT-AZ" ;
2354 t = "GROUND-BASED" ;
2355 if ( name.matches( Regex( "DV[0-9]+$" ) )
2356 || name.matches( Regex( "DA[0-9]+$" ) )
2357 || name.matches( Regex( "PM[0-9]+$" ) ) )
2358 d = 12.0 ;
2359 else if ( name.matches( Regex( "CM[0-9]+$" ) ) )
2360 d = 7.0 ;
2361 else if ( name.contains( "GBT" ) )
2362 d = 104.9 ;
2363 else if ( name.contains( "MOPRA" ) )
2364 d = 22.0 ;
2365 else if ( name.contains( "PKS" ) || name.contains( "PARKS" ) )
2366 d = 64.0 ;
2367 else if ( name.contains( "TIDBINBILLA" ) )
2368 d = 70.0 ;
2369 else if ( name.contains( "CEDUNA" ) )
2370 d = 30.0 ;
2371 else if ( name.contains( "HOBART" ) )
2372 d = 26.0 ;
2373 else if ( name.contains( "APEX" ) )
2374 d = 12.0 ;
2375 else if ( name.contains( "ASTE" ) )
2376 d = 10.0 ;
2377 else if ( name.contains( "NRO" ) )
2378 d = 45.0 ;
2379 else
2380 d = 1.0 ;
2381}
2382
2383void MSWriter::fillAntenna()
2384{
2385 //double startSec = mathutil::gettimeofday_sec() ;
2386 //os_ << "start MSWriter::fillAntenna() startSec=" << startSec << LogIO::POST ;
2387
2388 // only 1 row
2389 Table anttab = mstable_->antenna() ;
2390 anttab.addRow( 1, True ) ;
2391
2392 Table &table = table_->table() ;
2393 const TableRecord &keys = table.keywordSet() ;
2394 String hAntName = keys.asString( "AntennaName" ) ;
2395 String::size_type pos = hAntName.find( "//" ) ;
2396 String antennaName ;
2397 String stationName ;
2398 if ( pos != String::npos ) {
2399 stationName = hAntName.substr( 0, pos ) ;
2400 hAntName = hAntName.substr( pos+2 ) ;
2401 }
2402 pos = hAntName.find( "@" ) ;
2403 if ( pos != String::npos ) {
2404 antennaName = hAntName.substr( 0, pos ) ;
2405 stationName = hAntName.substr( pos+1 ) ;
2406 }
2407 else {
2408 antennaName = hAntName ;
2409 }
2410 Vector<Double> antpos = keys.asArrayDouble( "AntennaPosition" ) ;
2411
2412 String mount, atype ;
2413 Double diameter ;
2414 antennaProperty( antennaName, mount, atype, diameter ) ;
2415
2416 TableRow tr( anttab ) ;
2417 TableRecord &r = tr.record() ;
2418 RecordFieldPtr<String> nameRF( r, "NAME" ) ;
2419 RecordFieldPtr<String> stationRF( r, "STATION" ) ;
2420 RecordFieldPtr<String> mountRF( r, "MOUNT" ) ;
2421 RecordFieldPtr<String> typeRF( r, "TYPE" ) ;
2422 RecordFieldPtr<Double> dishDiameterRF( r, "DISH_DIAMETER" ) ;
2423 RecordFieldPtr< Vector<Double> > positionRF( r, "POSITION" ) ;
2424 *nameRF = antennaName ;
2425 *mountRF = mount ;
2426 *typeRF = atype ;
2427 *dishDiameterRF = diameter ;
2428 *positionRF = antpos ;
2429 *stationRF = stationName ;
2430
2431 tr.put( 0 ) ;
2432
2433 //double endSec = mathutil::gettimeofday_sec() ;
2434 //os_ << "end MSWriter::fillAntenna() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2435}
2436
2437void MSWriter::fillProcessor()
2438{
2439// double startSec = mathutil::gettimeofday_sec() ;
2440// os_ << "start MSWriter::fillProcessor() startSec=" << startSec << LogIO::POST ;
2441
2442 // only add empty 1 row
2443 MSProcessor msProc = mstable_->processor() ;
2444 msProc.addRow( 1, True ) ;
2445
2446// double endSec = mathutil::gettimeofday_sec() ;
2447// os_ << "end MSWriter::fillProcessor() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2448}
2449
2450void MSWriter::fillSource()
2451{
2452// double startSec = mathutil::gettimeofday_sec() ;
2453// os_ << "start MSWriter::fillSource() startSec=" << startSec << LogIO::POST ;
2454
2455 // access to MS SOURCE subtable
2456 MSSource msSrc = mstable_->source() ;
2457
2458 // access to MOLECULE subtable
2459 STMolecules stm = table_->molecules() ;
2460
2461 Int srcId = 0 ;
2462 Vector<Double> restFreq ;
2463 Vector<String> molName ;
2464 Vector<String> fMolName ;
2465
2466 // row based
2467 TableRow row( msSrc ) ;
2468 TableRecord &rec = row.record() ;
2469 RecordFieldPtr<Int> srcidRF( rec, "SOURCE_ID" ) ;
2470 RecordFieldPtr<String> nameRF( rec, "NAME" ) ;
2471 RecordFieldPtr< Array<Double> > srcpmRF( rec, "PROPER_MOTION" ) ;
2472 RecordFieldPtr< Array<Double> > srcdirRF( rec, "DIRECTION" ) ;
2473 RecordFieldPtr<Int> numlineRF( rec, "NUM_LINES" ) ;
2474 RecordFieldPtr< Array<Double> > restfreqRF( rec, "REST_FREQUENCY" ) ;
2475 RecordFieldPtr< Array<Double> > sysvelRF( rec, "SYSVEL" ) ;
2476 RecordFieldPtr< Array<String> > transitionRF( rec, "TRANSITION" ) ;
2477 RecordFieldPtr<Double> timeRF( rec, "TIME" ) ;
2478 RecordFieldPtr<Double> intervalRF( rec, "INTERVAL" ) ;
2479 RecordFieldPtr<Int> spwidRF( rec, "SPECTRAL_WINDOW_ID" ) ;
2480
2481 //
2482 // ITERATION: SRCNAME
2483 //
2484 TableIterator iter0( table_->table(), "SRCNAME" ) ;
2485 while( !iter0.pastEnd() ) {
2486 //Table t0( iter0.table() ) ;
2487 Table t0 = iter0.table() ;
2488
2489 // get necessary information
2490 ROScalarColumn<String> srcNameCol( t0, "SRCNAME" ) ;
2491 String srcName = srcNameCol( 0 ) ;
2492 ROArrayColumn<Double> sharedDArrRCol( t0, "SRCPROPERMOTION" ) ;
2493 Vector<Double> srcPM = sharedDArrRCol( 0 ) ;
2494 sharedDArrRCol.attach( t0, "SRCDIRECTION" ) ;
2495 Vector<Double> srcDir = sharedDArrRCol( 0 ) ;
2496 ROScalarColumn<Double> srcVelCol( t0, "SRCVELOCITY" ) ;
2497 Double srcVel = srcVelCol( 0 ) ;
2498 srcRec_.define( srcName, srcId ) ;
2499
2500 // NAME
2501 *nameRF = srcName ;
2502
2503 // SOURCE_ID
2504 *srcidRF = srcId ;
2505
2506 // PROPER_MOTION
2507 *srcpmRF = srcPM ;
2508
2509 // DIRECTION
2510 *srcdirRF = srcDir ;
2511
2512 //
2513 // ITERATION: MOLECULE_ID
2514 //
2515 TableIterator iter1( t0, "MOLECULE_ID" ) ;
2516 while( !iter1.pastEnd() ) {
2517 //Table t1( iter1.table() ) ;
2518 Table t1 = iter1.table() ;
2519
2520 // get necessary information
2521 ROScalarColumn<uInt> molIdCol( t1, "MOLECULE_ID" ) ;
2522 uInt molId = molIdCol( 0 ) ;
2523 stm.getEntry( restFreq, molName, fMolName, molId ) ;
2524
2525 uInt numFreq = restFreq.size() ;
2526
2527 // NUM_LINES
2528 *numlineRF = numFreq ;
2529
2530 // REST_FREQUENCY
2531 *restfreqRF = restFreq ;
2532
2533 // TRANSITION
2534 //*transitionRF = fMolName ;
2535 Vector<String> transition ;
2536 if ( fMolName.size() != 0 ) {
2537 transition = fMolName ;
2538 }
2539 else if ( molName.size() != 0 ) {
2540 transition = molName ;
2541 }
2542 else {
2543 transition.resize( numFreq ) ;
2544 transition = "" ;
2545 }
2546 *transitionRF = transition ;
2547
2548 // SYSVEL
2549 Vector<Double> sysvelArr( numFreq, srcVel ) ;
2550 *sysvelRF = sysvelArr ;
2551
2552 //
2553 // ITERATION: IFNO
2554 //
2555 TableIterator iter2( t1, "IFNO" ) ;
2556 while( !iter2.pastEnd() ) {
2557 //Table t2( iter2.table() ) ;
2558 Table t2 = iter2.table() ;
2559 uInt nrow = msSrc.nrow() ;
2560
2561 // get necessary information
2562 ROScalarColumn<uInt> ifNoCol( t2, "IFNO" ) ;
2563 uInt ifno = ifNoCol( 0 ) ; // IFNO = SPECTRAL_WINDOW_ID
2564 Double midTime ;
2565 Double interval ;
2566 getValidTimeRange( midTime, interval, t2 ) ;
2567
2568 // fill SPECTRAL_WINDOW_ID
2569 *spwidRF = ifno ;
2570
2571 // fill TIME, INTERVAL
2572 *timeRF = midTime ;
2573 *intervalRF = interval ;
2574
2575 // add row
2576 msSrc.addRow( 1, True ) ;
2577 row.put( nrow ) ;
2578
2579 iter2.next() ;
2580 }
2581
2582 iter1.next() ;
2583 }
2584
2585 // increment srcId if SRCNAME changed
2586 srcId++ ;
2587
2588 iter0.next() ;
2589 }
2590
2591// double endSec = mathutil::gettimeofday_sec() ;
2592// os_ << "end MSWriter::fillSource() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2593}
2594
2595void MSWriter::fillWeather()
2596{
2597// double startSec = mathutil::gettimeofday_sec() ;
2598// os_ << "start MSWriter::fillWeather() startSec=" << startSec << LogIO::POST ;
2599
2600 // access to MS WEATHER subtable
2601 MSWeather msw = mstable_->weather() ;
2602
2603 // access to WEATHER subtable
2604 Table stw = table_->weather().table() ;
2605 uInt nrow = stw.nrow() ;
2606
2607 if ( nrow == 0 )
2608 return ;
2609
2610 msw.addRow( nrow, True ) ;
2611 MSWeatherColumns mswCols( msw ) ;
2612
2613 // ANTENNA_ID is always 0
2614 Vector<Int> antIdArr( nrow, 0 ) ;
2615 mswCols.antennaId().putColumn( antIdArr ) ;
2616
2617 // fill weather status
2618 ROScalarColumn<Float> sharedFloatCol( stw, "TEMPERATURE" ) ;
2619 mswCols.temperature().putColumn( sharedFloatCol ) ;
2620 sharedFloatCol.attach( stw, "PRESSURE" ) ;
2621 mswCols.pressure().putColumn( sharedFloatCol ) ;
2622 sharedFloatCol.attach( stw, "HUMIDITY" ) ;
2623 mswCols.relHumidity().putColumn( sharedFloatCol ) ;
2624 sharedFloatCol.attach( stw, "WINDSPEED" ) ;
2625 mswCols.windSpeed().putColumn( sharedFloatCol ) ;
2626 sharedFloatCol.attach( stw, "WINDAZ" ) ;
2627 mswCols.windDirection().putColumn( sharedFloatCol ) ;
2628
2629 // fill TIME and INTERVAL
2630 Double midTime ;
2631 Double interval ;
2632 Vector<Double> intervalArr( nrow, 0.0 ) ;
2633 TableIterator iter( table_->table(), "WEATHER_ID" ) ;
2634 while( !iter.pastEnd() ) {
2635 //Table tab( iter.table() ) ;
2636 Table tab = iter.table() ;
2637
2638 ROScalarColumn<uInt> widCol( tab, "WEATHER_ID" ) ;
2639 uInt wid = widCol( 0 ) ;
2640
2641 getValidTimeRange( midTime, interval, tab ) ;
2642 mswCols.time().put( wid, midTime ) ;
2643 intervalArr[wid] = interval ;
2644
2645 iter.next() ;
2646 }
2647 mswCols.interval().putColumn( intervalArr ) ;
2648
2649// double endSec = mathutil::gettimeofday_sec() ;
2650// os_ << "end MSWriter::fillWeather() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2651}
2652
2653void MSWriter::fillSysCal()
2654{
2655 Table mssc = mstable_->sysCal() ;
2656
2657 {
2658 static const char *cols[] = {
2659 "BEAMNO", "IFNO", "TIME", "POLNO",
2660 NULL
2661 };
2662 static const TypeManagerImpl<uInt> tmUInt;
2663 static const TypeManagerImpl<Double> tmDouble;
2664 static const TypeManager *const tms[] = {
2665 &tmUInt, &tmUInt, &tmDouble, &tmUInt, NULL
2666 };
2667 //double t0 = mathutil::gettimeofday_sec() ;
2668 MSSysCalVisitor myVisitor(table_->table(),mssc);
2669 //double t1 = mathutil::gettimeofday_sec() ;
2670 //cout << "MSWriterVisitor(): elapsed time " << t1-t0 << " sec" << endl ;
2671 traverseTable(table_->table(), cols, tms, &myVisitor);
2672 //double t3 = mathutil::gettimeofday_sec() ;
2673 //cout << "traverseTable(): elapsed time " << t3-t2 << " sec" << endl ;
2674 }
2675
2676}
2677
2678void MSWriter::getValidTimeRange( Double &me, Double &interval, Table &tab )
2679{
2680// double startSec = mathutil::gettimeofday_sec() ;
2681// os_ << "start MSWriter::getVaridTimeRange() startSec=" << startSec << LogIO::POST ;
2682
2683 // sort table
2684 //Table stab = tab.sort( "TIME" ) ;
2685
2686 ROScalarColumn<Double> timeCol( tab, "TIME" ) ;
2687 Vector<Double> timeArr = timeCol.getColumn() ;
2688 Double minTime ;
2689 Double maxTime ;
2690 minMax( minTime, maxTime, timeArr ) ;
2691 Double midTime = 0.5 * ( minTime + maxTime ) * 86400.0 ;
2692 // unit for TIME
2693 // Scantable: "d"
2694 // MS: "s"
2695 me = midTime ;
2696 interval = ( maxTime - minTime ) * 86400.0 ;
2697
2698// double endSec = mathutil::gettimeofday_sec() ;
2699// os_ << "end MSWriter::getValidTimeRange() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2700}
2701
2702void MSWriter::getValidTimeRange( Double &me, Double &interval, Vector<Double> &atime, Vector<Double> &ainterval )
2703{
2704// double startSec = mathutil::gettimeofday_sec() ;
2705// os_ << "start MSWriter::getVaridTimeRange() startSec=" << startSec << LogIO::POST ;
2706
2707 // sort table
2708 //Table stab = tab.sort( "TIME" ) ;
2709
2710 Double minTime ;
2711 Double maxTime ;
2712 minMax( minTime, maxTime, atime ) ;
2713 Double midTime = 0.5 * ( minTime + maxTime ) * 86400.0 ;
2714 // unit for TIME
2715 // Scantable: "d"
2716 // MS: "s"
2717 me = midTime ;
2718 interval = ( maxTime - minTime ) * 86400.0 + mean( ainterval ) ;
2719
2720// double endSec = mathutil::gettimeofday_sec() ;
2721// os_ << "end MSWriter::getValidTimeRange() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
2722}
2723
2724}
Note: See TracBrowser for help on using the repository browser.