LCOV - code coverage report
Current view: top level - singledish/SingleDish - BaselineTable.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 142 166 85.5 %
Date: 2024-12-11 20:54:31 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: casa-feedback@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          75 : BaselineTable::BaselineTable(const MeasurementSet& parent)
      56             : {
      57         150 :   TableDesc td("", "1", TableDesc::Scratch);
      58          75 :   td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
      59          75 :   td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
      60          75 :   td.addColumn(ScalarColumnDesc<uInt>("ANTNO"));
      61          75 :   td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
      62          75 :   td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
      63          75 :   td.addColumn(ScalarColumnDesc<Double>("TIME"));
      64          75 :   TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
      65          75 :   TableMeasValueDesc measVal(td, "TIME");
      66          75 :   TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
      67          75 :   mepochCol.write(td);
      68          75 :   String tabname = parent.tableName()+"/"+name_;
      69          75 :   SetupNewTable aNewTab(tabname, td, Table::Scratch);
      70          75 :   table_ = Table(aNewTab, Table::Memory);
      71          75 :   attachBaseColumns();
      72             : 
      73          75 :   table_.rwKeywordSet().define("VERSION", 1);
      74          75 :   table_.rwKeywordSet().define("MSName", parent.tableName());
      75          75 :   table_.rwKeywordSet().define("ApplyType", "NONE");
      76          75 :   table_.rwKeywordSet().defineTable("FREQUENCIES", parent.spectralWindow());
      77          75 :   table_.tableInfo().setType("ApplyTable");
      78          75 :   originaltable_ = table_;
      79             : 
      80          75 :   setup();
      81          75 : }
      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         170 : BaselineTable::~BaselineTable()
      92             : {
      93         170 : }
      94             : 
      95           0 : void BaselineTable::attach()
      96             : {
      97           0 :   attachBaseColumns();
      98           0 :   attachOptionalColumns();
      99           0 : }
     100             : 
     101          85 : void BaselineTable::attachBaseColumns()
     102             : {
     103          85 :   scanCol_.attach(table_, "SCANNO");
     104          85 :   beamCol_.attach(table_, "BEAMNO");
     105          85 :   antCol_.attach(table_, "ANTNO");
     106          85 :   ifCol_.attach(table_, "IFNO");
     107          85 :   timeCol_.attach(table_, "TIME");
     108          85 :   timeMeasCol_.attach(table_, "TIME");
     109          85 :   freqidCol_.attach(table_, "FREQ_ID");
     110          85 : }
     111          75 : void BaselineTable::setup()
     112             : {
     113          75 :   table_.addColumn(ArrayColumnDesc<Bool>("APPLY"));
     114          75 :   table_.addColumn(ArrayColumnDesc<uInt>("FUNC_TYPE"));
     115          75 :   table_.addColumn(ArrayColumnDesc<Int>("FUNC_PARAM"));
     116          75 :   table_.addColumn(ArrayColumnDesc<Float>("FUNC_FPARAM"));
     117          75 :   table_.addColumn(ArrayColumnDesc<uInt>("MASKLIST"));
     118          75 :   table_.addColumn(ArrayColumnDesc<Float>("RESULT"));
     119          75 :   table_.addColumn(ArrayColumnDesc<Float>("RMS"));
     120          75 :   table_.addColumn(ScalarColumnDesc<uInt>("NCHAN"));
     121          75 :   table_.addColumn(ArrayColumnDesc<Float>("CLIP_THRESHOLD"));
     122          75 :   table_.addColumn(ArrayColumnDesc<uInt>("CLIP_ITERATION"));
     123          75 :   table_.addColumn(ArrayColumnDesc<Bool>("USE_LF"));
     124          75 :   table_.addColumn(ArrayColumnDesc<Float>("LF_THRESHOLD"));
     125          75 :   table_.addColumn(ArrayColumnDesc<uInt>("LF_AVERAGE"));
     126          75 :   table_.addColumn(ArrayColumnDesc<uInt>("LF_EDGE"));
     127             : 
     128          75 :   table_.rwKeywordSet().define("ApplyType", "BASELINE");
     129             : 
     130          75 :   attachOptionalColumns();
     131          75 : }
     132             : 
     133          85 : void BaselineTable::attachOptionalColumns()
     134             : {
     135          85 :   applyCol_.attach(table_, "APPLY");
     136          85 :   ftypeCol_.attach(table_, "FUNC_TYPE");
     137          85 :   fparCol_.attach(table_, "FUNC_PARAM");
     138          85 :   ffparCol_.attach(table_, "FUNC_FPARAM");
     139          85 :   maskCol_.attach(table_, "MASKLIST");
     140          85 :   resCol_.attach(table_, "RESULT");
     141          85 :   rmsCol_.attach(table_, "RMS");
     142          85 :   nchanCol_.attach(table_, "NCHAN");
     143          85 :   cthresCol_.attach(table_, "CLIP_THRESHOLD");
     144          85 :   citerCol_.attach(table_, "CLIP_ITERATION");
     145          85 :   uselfCol_.attach(table_, "USE_LF");
     146          85 :   lfthresCol_.attach(table_, "LF_THRESHOLD");
     147          85 :   lfavgCol_.attach(table_, "LF_AVERAGE");
     148          85 :   lfedgeCol_.attach(table_, "LF_EDGE");
     149             : 
     150          85 :   ftypeCol_.rwKeywordSet().define("Polynomial",   BaselineType_kPolynomial);
     151          85 :   ftypeCol_.rwKeywordSet().define("Chebyshev",    BaselineType_kChebyshev);
     152          85 :   ftypeCol_.rwKeywordSet().define("Cubic Spline", BaselineType_kCubicSpline);
     153          85 :   ftypeCol_.rwKeywordSet().define("Sinusoid",     BaselineType_kSinusoid);
     154          85 : }
     155             : 
     156          75 : void BaselineTable::save(const std::string &filename)
     157             : {
     158          75 :   String inname(filename);
     159          75 :   Path path(inname);
     160          75 :   inname = path.expandedName();
     161          75 :   table_.deepCopy(inname, Table::New);
     162          75 : }
     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         124 : }
     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         183 :   return getMaskFromMaskList(nchan, masklist.row(ipol));
     175          61 : }
     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         122 : }
     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          61 : }
     188             : 
     189         251 : void BaselineTable::setbasedata(uInt irow, uInt scanno, uInt beamno, uInt antno, 
     190             :                                 uInt ifno, uInt freqid, Double time)
     191             : {
     192         251 :   scanCol_.put(irow, scanno);
     193         251 :   beamCol_.put(irow, beamno);
     194         251 :   antCol_.put(irow, antno);
     195         251 :   ifCol_.put(irow, ifno);
     196         251 :   timeCol_.put(irow, time);
     197         251 :   freqidCol_.put(irow, freqid);
     198         251 : }
     199             : 
     200         251 : 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         251 :   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           0 :   }
     223             : 
     224         251 :   setbasedata(irow, scanno, beamno, antno, ifno, freqid, time);
     225         251 :   applyCol_.put(irow, apply);
     226         251 :   ftypeCol_.put(irow, ftype);
     227         251 :   fparCol_.put(irow, fpar);
     228         251 :   ffparCol_.put(irow, ffpar);
     229         251 :   maskCol_.put(irow, mask);
     230         251 :   resCol_.put(irow, res);
     231         251 :   rmsCol_.put(irow, rms);
     232         251 :   nchanCol_.put(irow, nchan);
     233         251 :   cthresCol_.put(irow, cthres);
     234         251 :   citerCol_.put(irow, citer);
     235         251 :   uselfCol_.put(irow, uself);
     236         251 :   lfthresCol_.put(irow, lfthres);
     237         251 :   lfavgCol_.put(irow, lfavg);
     238         251 :   lfedgeCol_.put(irow, lfedge);
     239         251 : }
     240             : 
     241         251 : 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         251 :   uInt irow = nrow();
     260         251 :   table_.addRow(1, true);
     261         251 :   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         251 : }
     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           0 : }
     314             : }

Generated by: LCOV version 1.16