source: branches/parallelCasa3.3/src/Scantable.h@ 2554

Last change on this file since 2554 was 2114, checked in by Kana Sugimoto, 14 years ago

merged fixes in trunk (r2111)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.8 KB
Line 
1//
2// C++ Interface: Scantable
3//
4// Description:
5//
6//
7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2005
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12#ifndef ASAPSCANTABLE_H
13#define ASAPSCANTABLE_H
14
15// STL
16#include <string>
17#include <vector>
18#include <iostream>
19#include <fstream>
20// AIPS++
21#include <casa/aips.h>
22#include <casa/Containers/Record.h>
23#include <casa/Arrays/MaskedArray.h>
24#include <casa/BasicSL/String.h>
25#include <casa/Utilities/CountedPtr.h>
26
27#include <tables/Tables/Table.h>
28#include <tables/Tables/ArrayColumn.h>
29#include <tables/Tables/ScalarColumn.h>
30
31#include <measures/TableMeasures/ScalarMeasColumn.h>
32
33#include <coordinates/Coordinates/SpectralCoordinate.h>
34
35#include <casa/Arrays/Vector.h>
36#include <casa/Quanta/Quantum.h>
37
38#include <casa/Exceptions/Error.h>
39
40#include "Logger.h"
41#include "STHeader.h"
42#include "STFrequencies.h"
43#include "STWeather.h"
44#include "STFocus.h"
45#include "STTcal.h"
46#include "STMolecules.h"
47#include "STSelector.h"
48#include "STHistory.h"
49#include "STPol.h"
50#include "STFit.h"
51#include "STFitEntry.h"
52#include "STFitter.h"
53
54namespace asap {
55
56/**
57 * This class contains and wraps a casa::Table, which is used to store
58 * all the information. This can be either a MemoryTable or a
59 * disk based Table.
60 * It provides access functions to the underlying table
61 * It contains n subtables:
62 * @li weather
63 * @li frequencies
64 * @li molecules
65 * @li tcal
66 * @li focus
67 * @li fits
68 *
69 * @brief The main ASAP data container
70 * @author Malte Marquarding
71 * @date
72 * @version
73*/
74class Scantable : private Logger
75{
76
77friend class STMath;
78
79public:
80 /**
81 * Default constructor
82 */
83 explicit Scantable(casa::Table::TableType ttype = casa::Table::Memory);
84
85 /**
86 * Create a Scantable object form an existing table on disk
87 * @param[in] name the name of the existing Scantable
88 */
89 explicit Scantable(const std::string& name, casa::Table::TableType ttype = casa::Table::Memory);
90
91 /// @fixme this is only sensible for MemoryTables....
92 Scantable(const Scantable& other, bool clear=true);
93
94 /**
95 * Destructor
96 */
97 virtual ~Scantable();
98
99 /**
100 * get a const reference to the underlying casa::Table
101 * @return const \ref casa::Table reference
102 */
103 const casa::Table& table() const;
104
105 /**
106 * get a reference to the underlying casa::Table with the Selection
107 * object applied if set
108 * @return casa::Table reference
109 */
110 casa::Table& table();
111
112
113 /**
114 * Get a handle to the selection object
115 * @return constant STSelector reference
116 */
117 const STSelector& getSelection() const { return selector_; }
118
119 /**
120 * Set the data to be a subset as defined by the STSelector
121 * @param selection a STSelector object
122 */
123 void setSelection(const STSelector& selection);
124
125 /**
126 * unset the selection of the data
127 */
128 void unsetSelection();
129 /**
130 * set the header
131 * @param[in] sth an STHeader object
132 */
133 void setHeader( const STHeader& sth );
134
135 /**
136 * get the header information
137 * @return an STHeader object
138 */
139 STHeader getHeader( ) const;
140
141 /**
142 * Checks if the "other" Scantable is conformant with this,
143 * i.e. if header values are the same.
144 * @param[in] other another Scantable
145 * @return true or false
146 */
147 bool conformant( const Scantable& other);
148
149 /**
150 *
151 * @param stype The type of the source, 0 = on, 1 = off
152 */
153 void setSourceType(int stype);
154
155
156 /**
157 * The number of scans in the table
158 * @return number of scans in the table
159 */
160 int nscan() const;
161
162 casa::MEpoch::Types getTimeReference() const;
163
164
165 casa::MEpoch getEpoch(int whichrow) const;
166
167 /**
168 * Get global antenna position
169 * @return casa::MPosition
170 */
171 casa::MPosition getAntennaPosition() const;
172
173 /**
174 * the @ref casa::MDirection for a specific row
175 * @param[in] whichrow the row number
176 * return casa::MDirection
177 */
178 casa::MDirection getDirection( int whichrow ) const;
179
180 /**
181 * get the direction type as a string, e.g. "J2000"
182 * @param[in] whichrow the row number
183 * return the direction string
184 */
185 std::string getDirectionString( int whichrow ) const;
186
187 /**
188 * set the direction type as a string, e.g. "J2000"
189 * @param[in] refstr the direction type
190 */
191 void setDirectionRefString(const std::string& refstr="");
192
193 /**
194 * get the direction reference string
195 * @return a string describing the direction reference
196 */
197 std::string getDirectionRefString() const;
198
199 /**
200 * Return the Flux unit of the data, e.g. "Jy" or "K"
201 * @return string
202 */
203 std::string getFluxUnit() const;
204
205 /**
206 * Set the Flux unit of the data
207 * @param unit a string representing the unit, e.g "Jy" or "K"
208 */
209 void setFluxUnit( const std::string& unit );
210
211 /**
212 * Set the Stokes type of the data
213 * @param feedtype a string representing the type, e.g "circular" or "linear"
214 */
215 void setFeedType( const std::string& feedtype );
216
217 /**
218 *
219 * @param instrument a string representing an insturment. see xxx
220 */
221 void setInstrument( const std::string& instrument );
222
223 /**
224 * (Re)calculate the azimuth and elevationnfor all rows
225 */
226 void calculateAZEL();
227
228 /**
229 * "hard" flag the data, this flags everything selected in setSelection()
230 * param[in] msk a boolean mask of length nchan describing the points to
231 * to be flagged
232 */
233 //void flag( const std::vector<bool>& msk = std::vector<bool>());
234 //void flag( const std::vector<bool>& msk = std::vector<bool>(), bool unflag=false);
235
236 void flag( int whichrow = -1, const std::vector<bool>& msk = std::vector<bool>(), bool unflag=false);
237
238 /**
239 * Flag the data in a row-based manner. (CAS-1433 Wataru Kawasaki)
240 * param[in] rows list of row numbers to be flagged
241 */
242 void flagRow( const std::vector<casa::uInt>& rows = std::vector<casa::uInt>(), bool unflag=false);
243
244 /**
245 * Get flagRow info at the specified row. If true, the whole data
246 * at the row should be flagged.
247 */
248 bool getFlagRow(int whichrow) const
249 { return (flagrowCol_(whichrow) > 0); }
250
251 /**
252 * Flag the data outside a specified range (in a channel-based manner).
253 * (CAS-1807 Wataru Kawasaki)
254 */
255 void clip(const casa::Float uthres, const casa::Float dthres, bool clipoutside, bool unflag);
256
257 /**
258 * Return a list of booleans with the size of nchan for a specified row, to get info
259 * about which channel is clipped.
260 */
261 std::vector<bool> getClipMask(int whichrow, const casa::Float uthres, const casa::Float dthres, bool clipoutside, bool unflag);
262 void srchChannelsToClip(casa::uInt whichrow, const casa::Float uthres, const casa::Float dthres, bool clipoutside, bool unflag,
263 casa::Vector<casa::uChar> flgs);
264
265 /**
266 * Return a list of row numbers with respect to the original table.
267 * @return a list of unsigned ints
268 */
269 std::vector<unsigned int> rownumbers() const;
270
271
272 /**
273 * Get the number of beams in the data or a specific scan
274 * @param scanno the scan number to get the number of beams for.
275 * If scanno<0 the number is retrieved from the header.
276 * @return an integer number
277 */
278 int nbeam(int scanno=-1) const;
279 /**
280 * Get the number of IFs in the data or a specific scan
281 * @param scanno the scan number to get the number of IFs for.
282 * If scanno<0 the number is retrieved from the header.
283 * @return an integer number
284 */
285 int nif(int scanno=-1) const;
286 /**
287 * Get the number of polarizations in the data or a specific scan
288 * @param scanno the scan number to get the number of polarizations for.
289 * If scanno<0 the number is retrieved from the header.
290 * @return an integer number
291 */
292 int npol(int scanno=-1) const;
293
294 std::string getPolType() const;
295
296
297 /**
298 * Get the number of integartion cycles
299 * @param scanno the scan number to get the number of rows for.
300 * If scanno<0 the number is retrieved from the header.
301 * @return the number of rows (for the specified scanno)
302 */
303 int nrow(int scanno=-1) const;
304
305 int getBeam(int whichrow) const;
306 std::vector<uint> getBeamNos() const { return getNumbers(beamCol_); }
307
308 int getIF(int whichrow) const;
309 std::vector<uint> getIFNos() const { return getNumbers(ifCol_); }
310
311 int getPol(int whichrow) const;
312 std::vector<uint> getPolNos() const { return getNumbers(polCol_); }
313
314 std::vector<uint> getScanNos() const { return getNumbers(scanCol_); }
315 int getScan(int whichrow) const { return scanCol_(whichrow); }
316
317 //TT addition
318 std::vector<uint> getMolNos() {return getNumbers(mmolidCol_); }
319
320 /**
321 * Get the number of channels in the data or a specific IF. This currently
322 * varies only with IF number
323 * @param ifno the IF number to get the number of channels for.
324 * If ifno<0 the number is retrieved from the header.
325 * @return an integer number
326 */
327 int nchan(int ifno=-1) const;
328 int getChannels(int whichrow) const;
329
330 int ncycle(int scanno=-1) const;
331 int getCycle(int whichrow) const { return cycleCol_(whichrow); }
332
333 double getInterval(int whichrow) const
334 { return integrCol_(whichrow); }
335
336 float getTsys(int whichrow) const
337 { return casa::Vector<casa::Float>(tsysCol_(whichrow))(0); }
338 float getElevation(int whichrow) const
339 { return elCol_(whichrow); }
340 float getAzimuth(int whichrow) const
341 { return azCol_(whichrow); }
342 float getParAngle(int whichrow) const
343 { return focus().getParAngle(mfocusidCol_(whichrow)); }
344 int getTcalId(int whichrow) const
345 { return mtcalidCol_(whichrow); }
346
347 std::string getSourceName(int whichrow) const
348 { return srcnCol_(whichrow); }
349
350 std::vector<bool> getMask(int whichrow) const;
351 std::vector<float> getSpectrum(int whichrow,
352 const std::string& poltype = "" ) const;
353
354 void setSpectrum(const std::vector<float>& spec, int whichrow);
355
356 std::string getPolarizationLabel(int index, const std::string& ptype) const
357 { return STPol::getPolLabel(index, ptype ); }
358
359 /**
360 * Write the Scantable to disk
361 * @param filename the output file name
362 */
363 void makePersistent(const std::string& filename);
364
365 std::vector<std::string> getHistory() const
366 { return historyTable_.getHistory(); };
367
368 void addHistory(const std::string& hist) { historyTable_.addEntry(hist); }
369
370 void appendToHistoryTable(const STHistory& otherhist)
371 { historyTable_.append(otherhist); }
372
373 std::string headerSummary(bool verbose=false);
374 std::string summary(bool verbose=false);
375 //std::string getTime(int whichrow=-1, bool showdate=true) const;
376 std::string getTime(int whichrow=-1, bool showdate=true, casa::uInt prec=0) const;
377 double getIntTime(int whichrow) const { return integrCol_(whichrow); }
378
379 // returns unit, conversion frame, doppler, base-frame
380
381 /**
382 * Get the frequency set up
383 * This is forwarded to the STFrequencies subtable
384 * @return unit, frame, doppler
385 */
386 std::vector<std::string> getCoordInfo() const
387 { return freqTable_.getInfo(); };
388
389 void setCoordInfo(std::vector<string> theinfo)
390 { return freqTable_.setInfo(theinfo); };
391
392
393 std::vector<double> getAbcissa(int whichrow) const;
394
395 std::vector<float> getWeather(int whichrow) const;
396
397 std::string getAbcissaLabel(int whichrow) const;
398 std::vector<double> getRestFrequencies() const
399 { return moleculeTable_.getRestFrequencies(); }
400 std::vector<double> getRestFrequency(int id) const
401 { return moleculeTable_.getRestFrequency(id); }
402
403 /**
404 void setRestFrequencies(double rf, const std::string& name = "",
405 const std::string& = "Hz");
406 **/
407 // Modified by Takeshi Nakazato 05/09/2008
408 /***
409 void setRestFrequencies(vector<double> rf, const vector<std::string>& name = "",
410 const std::string& = "Hz");
411 ***/
412 void setRestFrequencies(vector<double> rf,
413 const vector<std::string>& name = vector<std::string>(1,""),
414 const std::string& = "Hz");
415
416 //void setRestFrequencies(const std::string& name);
417 void setRestFrequencies(const vector<std::string>& name);
418
419 void shift(int npix);
420
421 casa::SpectralCoordinate getSpectralCoordinate(int whichrow) const;
422
423 void convertDirection(const std::string& newframe);
424
425 STFrequencies& frequencies() { return freqTable_; }
426 const STFrequencies& frequencies() const { return freqTable_; }
427 STWeather& weather() { return weatherTable_; }
428 const STWeather& weather() const { return weatherTable_; }
429 STFocus& focus() { return focusTable_; }
430 const STFocus& focus() const { return focusTable_; }
431 STTcal& tcal() { return tcalTable_; }
432 const STTcal& tcal() const { return tcalTable_; }
433 STMolecules& molecules() { return moleculeTable_; }
434 const STMolecules& molecules() const { return moleculeTable_; }
435 STHistory& history() { return historyTable_; }
436 const STHistory& history() const { return historyTable_; }
437 STFit& fit() { return fitTable_; }
438 const STFit& fit() const { return fitTable_; }
439
440 std::vector<std::string> columnNames() const;
441
442 void addFit(const STFitEntry& fit, int row);
443 STFitEntry getFit(int row) const
444 { STFitEntry fe; fitTable_.getEntry(fe, mfitidCol_(row)); return fe; }
445
446 //Added by TT
447 /**
448 * Get the antenna name
449 * @return antenna name string
450 */
451 casa::String getAntennaName() const;
452
453 /**
454 * For GBT MS data only. check a scan list
455 * against the information found in GBT_GO table for
456 * scan number orders to get correct pairs.
457 * @param[in] scan list
458 * @return status
459 */
460 int checkScanInfo(const std::vector<int>& scanlist) const;
461
462 /**
463 * Get the direction as a vector, for a specific row
464 * @param[in] whichrow the row numbyyer
465 * @return the direction in a vector
466 */
467 std::vector<double> getDirectionVector(int whichrow) const;
468
469 /**
470 * Set a flag indicating whether the data was parallactified
471 * @param[in] flag true or false
472 */
473 void parallactify(bool flag)
474 { focus().setParallactify(flag); }
475
476 /**
477 * Reshape spectrum
478 * @param[in] nmin, nmax minimum and maximum channel
479 * @param[in] irow row number
480 *
481 * 30/07/2008 Takeshi Nakazato
482 **/
483 void reshapeSpectrum( int nmin, int nmax ) throw( casa::AipsError );
484 void reshapeSpectrum( int nmin, int nmax, int irow ) ;
485
486 /**
487 * Change channel number under fixed bandwidth
488 * @param[in] nchan, dnu new channel number and spectral resolution
489 * @param[in] irow row number
490 *
491 * 27/08/2008 Takeshi Nakazato
492 **/
493 void regridChannel( int nchan, double dnu ) ;
494 void regridChannel( int nchan, double dnu, int irow ) ;
495
496 bool getFlagtraFast(casa::uInt whichrow);
497
498 void polyBaseline(const std::vector<bool>& mask,
499 int order,
500 bool getResidual=true,
501 bool outLogger=false,
502 const std::string& blfile="");
503 void autoPolyBaseline(const std::vector<bool>& mask,
504 int order,
505 const std::vector<int>& edge,
506 float threshold=3.0,
507 int chanAvgLimit=1,
508 bool getResidual=true,
509 bool outLogger=false,
510 const std::string& blfile="");
511 void cubicSplineBaseline(const std::vector<bool>& mask,
512 int nPiece,
513 float thresClip,
514 int nIterClip,
515 bool getResidual=true,
516 bool outLogger=false,
517 const std::string& blfile="");
518 void autoCubicSplineBaseline(const std::vector<bool>& mask,
519 int nPiece,
520 float thresClip,
521 int nIterClip,
522 const std::vector<int>& edge,
523 float threshold=3.0,
524 int chanAvgLimit=1,
525 bool getResidual=true,
526 bool outLogger=false,
527 const std::string& blfile="");
528 void sinusoidBaseline(const std::vector<bool>& mask,
529 const std::vector<int>& nWaves,
530 float maxWaveLength,
531 float thresClip,
532 int nIterClip,
533 bool getResidual=true,
534 bool outLogger=false,
535 const std::string& blfile="");
536 void autoSinusoidBaseline(const std::vector<bool>& mask,
537 const std::vector<int>& nWaves,
538 float maxWaveLength,
539 float thresClip,
540 int nIterClip,
541 const std::vector<int>& edge,
542 float threshold=3.0,
543 int chanAvgLimit=1,
544 bool getResidual=true,
545 bool outLogger=false,
546 const std::string& blfile="");
547 float getRms(const std::vector<bool>& mask, int whichrow);
548 std::string formatBaselineParams(const std::vector<float>& params,
549 const std::vector<bool>& fixed,
550 float rms,
551 const std::string& masklist,
552 int whichrow,
553 bool verbose=false,
554 int start=-1,
555 int count=-1,
556 bool resetparamid=false) const;
557 std::string formatPiecewiseBaselineParams(const std::vector<int>& ranges,
558 const std::vector<float>& params,
559 const std::vector<bool>& fixed,
560 float rms,
561 const std::string& masklist,
562 int whichrow,
563 bool verbose=false) const;
564
565
566private:
567
568 casa::Matrix<casa::Float> getPolMatrix( casa::uInt whichrow ) const;
569
570 /**
571 * Turns a time value into a formatted string
572 * @param x
573 * @return
574 */
575 std::string formatSec(casa::Double x) const;
576
577 std::string formatTime(const casa::MEpoch& me, bool showdate)const;
578 std::string formatTime(const casa::MEpoch& me, bool showdate, casa::uInt prec)const;
579
580 /**
581 * Turns a casa::MDirection into a nicely formatted string
582 * @param md an casa::MDirection
583 * @return
584 */
585 std::string formatDirection(const casa::MDirection& md) const;
586
587 /**
588 * Create a unique file name for the paged (temporary) table
589 * @return just the name
590 */
591 static casa::String generateName();
592
593 /**
594 * attach to cached columns
595 */
596 void attach();
597
598 /**
599 * Set up the main casa::Table
600 */
601 void setupMainTable();
602
603 void attachSubtables();
604 void copySubtables(const Scantable& other);
605
606 /**
607 * Convert an "old" asap1 style row index into a new index
608 * @param[in] therow
609 * @return and index into @table_
610 */
611 int rowToScanIndex(int therow);
612
613 std::vector<uint> getNumbers(const casa::ScalarColumn<casa::uInt>& col) const;
614
615 static const casa::uInt version_ = 3;
616
617 STSelector selector_;
618
619 casa::Table::TableType type_;
620
621 // the actual data
622 casa::Table table_;
623 casa::Table originalTable_;
624
625 STTcal tcalTable_;
626 STFrequencies freqTable_;
627 STWeather weatherTable_;
628 STFocus focusTable_;
629 STMolecules moleculeTable_;
630 STHistory historyTable_;
631 STFit fitTable_;
632
633 // Cached Columns to avoid reconstructing them for each row get/put
634 casa::ScalarColumn<casa::Double> integrCol_;
635 casa::MDirection::ScalarColumn dirCol_;
636 casa::MEpoch::ScalarColumn timeCol_;
637 casa::ScalarColumn<casa::Float> azCol_;
638 casa::ScalarColumn<casa::Float> elCol_;
639 casa::ScalarColumn<casa::String> srcnCol_, fldnCol_;
640 casa::ScalarColumn<casa::uInt> scanCol_, beamCol_, ifCol_, polCol_, cycleCol_, flagrowCol_;
641 casa::ScalarColumn<casa::Int> rbeamCol_, srctCol_;
642 casa::ArrayColumn<casa::Float> specCol_, tsysCol_;
643 casa::ArrayColumn<casa::uChar> flagsCol_;
644
645 // id in frequencies table
646 casa::ScalarColumn<casa::uInt> mfreqidCol_;
647 // id in tcal table
648 casa::ScalarColumn<casa::uInt> mtcalidCol_;
649
650 casa::ArrayColumn<casa::String> histitemCol_;
651 casa::ScalarColumn<casa::Int> mfitidCol_;
652 casa::ScalarColumn<casa::uInt> mweatheridCol_;
653
654 casa::ScalarColumn<casa::uInt> mfocusidCol_;
655
656 casa::ScalarColumn<casa::uInt> mmolidCol_;
657
658 static std::map<std::string, STPol::STPolFactory *> factories_;
659 void initFactories();
660
661 /**
662 * Add an auxiliary column to the main table and attach it to a
663 * cached column. Use for adding new columns that the original asap2
664 * tables do not have.
665 * @param[in] col reference to the cached column to be attached
666 * @param[in] colName column name in asap table
667 * @param[in] defValue default value to fill in the column
668 *
669 * 25/10/2009 Wataru Kawasaki
670 */
671 template<class T, class T2> void attachAuxColumnDef(casa::ScalarColumn<T>&,
672 const casa::String&,
673 const T2&);
674 template<class T, class T2> void attachAuxColumnDef(casa::ArrayColumn<T>&,
675 const casa::String&,
676 const casa::Array<T2>&);
677
678 void fitBaseline(const std::vector<bool>& mask, int whichrow, Fitter& fitter);
679 std::vector<float> doCubicSplineFitting(const std::vector<float>& data,
680 const std::vector<bool>& mask,
681 int nPiece,
682 std::vector<int>& idxEdge,
683 std::vector<float>& params,
684 float thresClip=3.0,
685 int nIterClip=1,
686 bool getResidual=true);
687 std::vector<float> doSinusoidFitting(const std::vector<float>& data,
688 const std::vector<bool>& mask,
689 const std::vector<int>& waveNumbers,
690 float maxWaveLength,
691 std::vector<float>& params,
692 float thresClip=3.0,
693 int nIterClip=1,
694 bool getResidual=true);
695 bool hasSameNchanOverIFs();
696 std::string getMaskRangeList(const std::vector<bool>& mask,
697 int whichrow,
698 const casa::String& coordInfo,
699 bool hasSameNchan,
700 bool verbose=false);
701 std::vector<int> getMaskEdgeIndices(const std::vector<bool>& mask);
702 std::string formatBaselineParamsHeader(int whichrow, const std::string& masklist, bool verbose) const;
703 std::string formatBaselineParamsFooter(float rms, bool verbose) const;
704 std::vector<bool> getCompositeChanMask(int whichrow, const std::vector<bool>& inMask);
705 //std::vector<bool> getCompositeChanMask(int whichrow, const std::vector<bool>& inMask, const std::vector<int>& edge, const int minEdgeSize, STLineFinder& lineFinder);
706 void outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, std::ofstream& ofs, const casa::String& funcName, Fitter& fitter);
707 void outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, std::ofstream& ofs, const casa::String& funcName, const std::vector<int>& edge, const std::vector<float>& params);
708 void outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, std::ofstream& ofs, const casa::String& funcName, const std::vector<float>& params);
709
710 void applyChanFlag( casa::uInt whichrow, const std::vector<bool>& msk, casa::uChar flagval);
711
712
713};
714
715} // namespace
716
717#endif
Note: See TracBrowser for help on using the repository browser.