LCOV - code coverage report
Current view: top level - singledish/SingleDish - BaselineTable.cc (source / functions) Hit Total Coverage
Test: ctest_coverage.info Lines: 138 160 86.2 %
Date: 2023-11-06 10:06:49 Functions: 16 21 76.2 %

          Line data    Source code
       1             : //# BaselineTable.cc: this code defines baseline table
       2             : //# Copyright (C) 2015
       3             : //# National Astronomical Observatory of Japan
       4             : //#
       5             : //# This library is free software; you can redistribute it and/or modify it
       6             : //# under the terms of the GNU Library General Public License as published by
       7             : //# the Free Software Foundation; either version 2 of the License, or (at your
       8             : //# option) any later version.
       9             : //#
      10             : //# This library is distributed in the hope that it will be useful, but WITHOUT
      11             : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12             : //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
      13             : //# License for more details.
      14             : //#
      15             : //# You should have received a copy of the GNU Library General Public License
      16             : //# along with this library; if not, write to the Free Software Foundation,
      17             : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
      18             : //#
      19             : //# Correspondence concerning AIPS++ should be addressed as follows:
      20             : //#        Internet email: aips2-request@nrao.edu.
      21             : //#        Postal address: AIPS++ Project Office
      22             : //#                        National Radio Astronomy Observatory
      23             : //#                        520 Edgemont Road
      24             : //#                        Charlottesville, VA 22903-2475 USA
      25             : //#
      26             : //# $Id$
      27             : #include <assert.h>
      28             : 
      29             : #include <casacore/casa/Containers/ValueHolder.h>
      30             : #include <casacore/casa/Exceptions/Error.h>
      31             : #include <casacore/casa/OS/Path.h>
      32             : #include <casacore/measures/TableMeasures/TableMeasDesc.h>
      33             : #include <casacore/measures/TableMeasures/TableMeasRefDesc.h>
      34             : #include <casacore/measures/TableMeasures/TableMeasValueDesc.h>
      35             : #include <singledish/SingleDish/BaselineTable.h>
      36             : #include <stdcasa/StdCasa/CasacSupport.h>
      37             : #include <casacore/tables/Tables/TableDesc.h>
      38             : #include <casacore/tables/Tables/SetupNewTab.h>
      39             : #include <casacore/tables/Tables/ArrColDesc.h>
      40             : #include <casacore/tables/Tables/ScaColDesc.h>
      41             : #include <casacore/tables/Tables/TableRecord.h>
      42             : #include <casacore/tables/Tables/TableProxy.h>
      43             : 
      44             : using namespace casacore;
      45             : using namespace casacore;
      46             : using namespace casacore;
      47             : using namespace casacore;
      48             : using namespace casacore;
      49             : using namespace casacore;
      50             : using namespace casacore;
      51             : namespace casa {
      52             : 
      53             : const String BaselineTable::name_ = "APPLY_BASELINE";
      54             : 
      55          74 : BaselineTable::BaselineTable(const MeasurementSet& parent)
      56             : {
      57         222 :   TableDesc td("", "1", TableDesc::Scratch);
      58          74 :   td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
      59          74 :   td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
      60          74 :   td.addColumn(ScalarColumnDesc<uInt>("ANTNO"));
      61          74 :   td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
      62          74 :   td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
      63          74 :   td.addColumn(ScalarColumnDesc<Double>("TIME"));
      64         148 :   TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
      65         148 :   TableMeasValueDesc measVal(td, "TIME");
      66         148 :   TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
      67          74 :   mepochCol.write(td);
      68         148 :   String tabname = parent.tableName()+"/"+name_;
      69         148 :   SetupNewTable aNewTab(tabname, td, Table::Scratch);
      70          74 :   table_ = Table(aNewTab, Table::Memory);
      71          74 :   attachBaseColumns();
      72             : 
      73          74 :   table_.rwKeywordSet().define("VERSION", 1);
      74          74 :   table_.rwKeywordSet().define("MSName", parent.tableName());
      75          74 :   table_.rwKeywordSet().define("ApplyType", "NONE");
      76          74 :   table_.rwKeywordSet().defineTable("FREQUENCIES", parent.spectralWindow());
      77          74 :   table_.tableInfo().setType("ApplyTable");
      78          74 :   originaltable_ = table_;
      79             : 
      80          74 :   setup();
      81          74 : }
      82             : 
      83          10 : BaselineTable::BaselineTable(const String &name)
      84             : {
      85          10 :   table_ = Table(name, Table::Update);
      86          10 :   attachBaseColumns();
      87          10 :   originaltable_ = table_;
      88          10 :   attachOptionalColumns();
      89          10 : }
      90             : 
      91         168 : BaselineTable::~BaselineTable()
      92             : {
      93         168 : }
      94             : 
      95           0 : void BaselineTable::attach()
      96             : {
      97           0 :   attachBaseColumns();
      98           0 :   attachOptionalColumns();
      99           0 : }
     100             : 
     101          84 : void BaselineTable::attachBaseColumns()
     102             : {
     103          84 :   scanCol_.attach(table_, "SCANNO");
     104          84 :   beamCol_.attach(table_, "BEAMNO");
     105          84 :   antCol_.attach(table_, "ANTNO");
     106          84 :   ifCol_.attach(table_, "IFNO");
     107          84 :   timeCol_.attach(table_, "TIME");
     108          84 :   timeMeasCol_.attach(table_, "TIME");
     109          84 :   freqidCol_.attach(table_, "FREQ_ID");
     110          84 : }
     111          74 : void BaselineTable::setup()
     112             : {
     113          74 :   table_.addColumn(ArrayColumnDesc<Bool>("APPLY"));
     114          74 :   table_.addColumn(ArrayColumnDesc<uInt>("FUNC_TYPE"));
     115          74 :   table_.addColumn(ArrayColumnDesc<Int>("FUNC_PARAM"));
     116          74 :   table_.addColumn(ArrayColumnDesc<Float>("FUNC_FPARAM"));
     117          74 :   table_.addColumn(ArrayColumnDesc<uInt>("MASKLIST"));
     118          74 :   table_.addColumn(ArrayColumnDesc<Float>("RESULT"));
     119          74 :   table_.addColumn(ArrayColumnDesc<Float>("RMS"));
     120          74 :   table_.addColumn(ScalarColumnDesc<uInt>("NCHAN"));
     121          74 :   table_.addColumn(ArrayColumnDesc<Float>("CLIP_THRESHOLD"));
     122          74 :   table_.addColumn(ArrayColumnDesc<uInt>("CLIP_ITERATION"));
     123          74 :   table_.addColumn(ArrayColumnDesc<Bool>("USE_LF"));
     124          74 :   table_.addColumn(ArrayColumnDesc<Float>("LF_THRESHOLD"));
     125          74 :   table_.addColumn(ArrayColumnDesc<uInt>("LF_AVERAGE"));
     126          74 :   table_.addColumn(ArrayColumnDesc<uInt>("LF_EDGE"));
     127             : 
     128          74 :   table_.rwKeywordSet().define("ApplyType", "BASELINE");
     129             : 
     130          74 :   attachOptionalColumns();
     131          74 : }
     132             : 
     133          84 : void BaselineTable::attachOptionalColumns()
     134             : {
     135          84 :   applyCol_.attach(table_, "APPLY");
     136          84 :   ftypeCol_.attach(table_, "FUNC_TYPE");
     137          84 :   fparCol_.attach(table_, "FUNC_PARAM");
     138          84 :   ffparCol_.attach(table_, "FUNC_FPARAM");
     139          84 :   maskCol_.attach(table_, "MASKLIST");
     140          84 :   resCol_.attach(table_, "RESULT");
     141          84 :   rmsCol_.attach(table_, "RMS");
     142          84 :   nchanCol_.attach(table_, "NCHAN");
     143          84 :   cthresCol_.attach(table_, "CLIP_THRESHOLD");
     144          84 :   citerCol_.attach(table_, "CLIP_ITERATION");
     145          84 :   uselfCol_.attach(table_, "USE_LF");
     146          84 :   lfthresCol_.attach(table_, "LF_THRESHOLD");
     147          84 :   lfavgCol_.attach(table_, "LF_AVERAGE");
     148          84 :   lfedgeCol_.attach(table_, "LF_EDGE");
     149             : 
     150          84 :   ftypeCol_.rwKeywordSet().define("Polynomial",   BaselineType_kPolynomial);
     151          84 :   ftypeCol_.rwKeywordSet().define("Chebyshev",    BaselineType_kChebyshev);
     152          84 :   ftypeCol_.rwKeywordSet().define("Cubic Spline", BaselineType_kCubicSpline);
     153          84 :   ftypeCol_.rwKeywordSet().define("Sinusoid",     BaselineType_kSinusoid);
     154          84 : }
     155             : 
     156          74 : void BaselineTable::save(const std::string &filename)
     157             : {
     158         148 :   String inname(filename);
     159         148 :   Path path(inname);
     160          74 :   inname = path.expandedName();
     161          74 :   table_.deepCopy(inname, Table::New);
     162          74 : }
     163             : 
     164         124 : bool BaselineTable::getApply(uInt irow, uInt ipol) const
     165             : {
     166         124 :   Vector<Bool> apply = applyCol_.get(irow);
     167         248 :   return static_cast<bool>(apply[ipol]);
     168             : }
     169             : 
     170          61 : std::vector<bool> BaselineTable::getMask(uInt irow, uInt ipol)
     171             : {
     172          61 :   uInt nchan = nchanCol_.get(irow);
     173          61 :   Matrix<uInt> masklist = maskCol_.get(irow);
     174         122 :   return getMaskFromMaskList(nchan, masklist.row(ipol));
     175             : }
     176             : 
     177         122 : uint BaselineTable::getBaselineType(uInt irow, uInt ipol) const
     178             : {
     179         122 :   Vector<uInt> ftype = ftypeCol_.get(irow);
     180         244 :   return static_cast<uint>(ftype[ipol]);
     181             : }
     182             : 
     183          61 : int BaselineTable::getFPar(uInt irow, uInt ipol) const
     184             : {
     185          61 :   Vector<Int> fpar = fparCol_.get(irow);
     186         122 :   return static_cast<int>(fpar[ipol]);
     187             : }
     188             : 
     189         250 : void BaselineTable::setbasedata(uInt irow, uInt scanno, uInt beamno, uInt antno, 
     190             :                                 uInt ifno, uInt freqid, Double time)
     191             : {
     192         250 :   scanCol_.put(irow, scanno);
     193         250 :   beamCol_.put(irow, beamno);
     194         250 :   antCol_.put(irow, antno);
     195         250 :   ifCol_.put(irow, ifno);
     196         250 :   timeCol_.put(irow, time);
     197         250 :   freqidCol_.put(irow, freqid);
     198         250 : }
     199             : 
     200         250 : void BaselineTable::setdata(uInt irow, uInt scanno, 
     201             :                             uInt beamno, uInt antno, uInt ifno, 
     202             :                             uInt freqid, Double time, 
     203             :                             Array<Bool> apply, 
     204             :                             Array<uInt> ftype,
     205             :                             Array<Int> fpar, 
     206             :                             Array<Float> ffpar, 
     207             :                             Array<uInt> mask,
     208             :                             Array<Float> res,
     209             :                             Array<Float> rms, 
     210             :                             uInt nchan, 
     211             :                             Array<Float> cthres,
     212             :                             Array<uInt> citer, 
     213             :                             Array<Bool> uself, 
     214             :                             Array<Float> lfthres, 
     215             :                             Array<uInt> lfavg, 
     216             :                             Array<uInt> lfedge)
     217             : {
     218         250 :   if (irow >= (uInt)nrow()) {
     219           0 :     stringstream ss;
     220           0 :     ss << "row index out of range[irow=" << irow << "][nrow=" << nrow() << "]";
     221           0 :     throw AipsError(ss.str());
     222             :   }
     223             : 
     224         250 :   setbasedata(irow, scanno, beamno, antno, ifno, freqid, time);
     225         250 :   applyCol_.put(irow, apply);
     226         250 :   ftypeCol_.put(irow, ftype);
     227         250 :   fparCol_.put(irow, fpar);
     228         250 :   ffparCol_.put(irow, ffpar);
     229         250 :   maskCol_.put(irow, mask);
     230         250 :   resCol_.put(irow, res);
     231         250 :   rmsCol_.put(irow, rms);
     232         250 :   nchanCol_.put(irow, nchan);
     233         250 :   cthresCol_.put(irow, cthres);
     234         250 :   citerCol_.put(irow, citer);
     235         250 :   uselfCol_.put(irow, uself);
     236         250 :   lfthresCol_.put(irow, lfthres);
     237         250 :   lfavgCol_.put(irow, lfavg);
     238         250 :   lfedgeCol_.put(irow, lfedge);
     239         250 : }
     240             : 
     241         250 : void BaselineTable::appenddata(uInt scanno, uInt beamno, 
     242             :                                uInt antno, uInt ifno, 
     243             :                                uInt freqid, Double time, 
     244             :                                Array<Bool> apply, 
     245             :                                Array<uInt> ftype, 
     246             :                                Array<Int> fpar, 
     247             :                                Array<Float> ffpar, 
     248             :                                Array<uInt> mask,
     249             :                                Array<Float> res,
     250             :                                Array<Float> rms,
     251             :                                uInt nchan, 
     252             :                                Array<Float> cthres,
     253             :                                Array<uInt> citer, 
     254             :                                Array<Bool> uself, 
     255             :                                Array<Float> lfthres, 
     256             :                                Array<uInt> lfavg, 
     257             :                                Array<uInt> lfedge)
     258             : {
     259         250 :   uInt irow = nrow();
     260         250 :   table_.addRow(1, true);
     261         250 :   setdata(irow, scanno, beamno, antno, ifno, freqid, time, 
     262             :           apply, ftype, fpar, ffpar, mask, res, rms, 
     263             :           nchan, cthres, citer, uself, lfthres, lfavg, lfedge);
     264         250 : }
     265             : 
     266           0 : void BaselineTable::appendbasedata(int scanno, int beamno, 
     267             :                                    int antno, int ifno, 
     268             :                                    int freqid, Double time)
     269             : {
     270           0 :   uInt irow = nrow();
     271           0 :   table_.addRow(1, true);
     272           0 :   setbasedata(irow, uInt(scanno), uInt(beamno), uInt(antno), 
     273             :               uInt(ifno), uInt(freqid), time);
     274           0 : }
     275             : 
     276           0 : void BaselineTable::setresult(uInt irow, 
     277             :                               Vector<Float> res, 
     278             :                               Array<Float> rms)
     279             : {
     280           0 :   resCol_.put(irow, res);
     281           0 :   rmsCol_.put(irow, rms);
     282           0 : }
     283             : 
     284           0 : uInt BaselineTable::getNChan(int irow)
     285             : {
     286           0 :   return nchanCol_.get(irow);
     287             : }
     288             : 
     289           0 : uInt BaselineTable::nchan(uInt ifno)
     290             : {
     291           0 :   uInt tmp = ifno;
     292           0 :   return tmp;
     293             : }
     294             : 
     295          61 : std::vector<bool> BaselineTable::getMaskFromMaskList(uInt const nchan, Vector<uInt> const& masklist)
     296             : {
     297          61 :   if (masklist.size() % 2 != 0) {
     298           0 :     throw(AipsError("masklist must have even number of elements."));
     299             :   }
     300             : 
     301          61 :   std::vector<bool> res((int)nchan, false);
     302             : 
     303         146 :   for (uInt j = 0; j < masklist.size(); j += 2) {
     304      346294 :     for (int i = masklist[j]; i <= min((Int)nchan-1, (Int)masklist[j+1]); ++i) {
     305      346185 :       res[i] = true;
     306             :     }
     307         109 :     if (masklist[j+1] == nchan-1) {
     308          24 :       break;
     309             :     }
     310             :   }
     311             : 
     312          61 :   return res;
     313             : }
     314             : }

Generated by: LCOV version 1.16