LCOV - code coverage report
Current view: top level - singledish/SingleDish - BaselineTable.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 184 0.0 %
Date: 2025-08-21 08:01:32 Functions: 0 22 0.0 %

          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           0 : BaselineTable::BaselineTable(const MeasurementSet& parent)
      56             : {
      57           0 :   TableDesc td("", "1", TableDesc::Scratch);
      58           0 :   td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
      59           0 :   td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
      60           0 :   td.addColumn(ScalarColumnDesc<uInt>("ANTNO"));
      61           0 :   td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
      62           0 :   td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
      63           0 :   td.addColumn(ScalarColumnDesc<Double>("TIME"));
      64           0 :   TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
      65           0 :   TableMeasValueDesc measVal(td, "TIME");
      66           0 :   TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
      67           0 :   mepochCol.write(td);
      68           0 :   String tabname = parent.tableName()+"/"+name_;
      69           0 :   SetupNewTable aNewTab(tabname, td, Table::Scratch);
      70           0 :   table_ = Table(aNewTab, Table::Memory);
      71           0 :   attachBaseColumns();
      72             : 
      73           0 :   table_.rwKeywordSet().define("VERSION", 1);
      74           0 :   table_.rwKeywordSet().define("MSName", parent.tableName());
      75           0 :   table_.rwKeywordSet().define("ApplyType", "NONE");
      76           0 :   table_.rwKeywordSet().defineTable("FREQUENCIES", parent.spectralWindow());
      77           0 :   table_.tableInfo().setType("ApplyTable");
      78           0 :   originaltable_ = table_;
      79             : 
      80           0 :   setup();
      81           0 : }
      82             : 
      83           0 : BaselineTable::BaselineTable(const String &name)
      84             : {
      85           0 :   table_ = Table(name, Table::Update);
      86           0 :   attachBaseColumns();
      87           0 :   originaltable_ = table_;
      88           0 :   attachOptionalColumns();
      89           0 : }
      90             : 
      91           0 : BaselineTable::~BaselineTable()
      92             : {
      93           0 : }
      94             : 
      95           0 : void BaselineTable::attach()
      96             : {
      97           0 :   attachBaseColumns();
      98           0 :   attachOptionalColumns();
      99           0 : }
     100             : 
     101           0 : void BaselineTable::attachBaseColumns()
     102             : {
     103           0 :   scanCol_.attach(table_, "SCANNO");
     104           0 :   beamCol_.attach(table_, "BEAMNO");
     105           0 :   antCol_.attach(table_, "ANTNO");
     106           0 :   ifCol_.attach(table_, "IFNO");
     107           0 :   timeCol_.attach(table_, "TIME");
     108           0 :   timeMeasCol_.attach(table_, "TIME");
     109           0 :   freqidCol_.attach(table_, "FREQ_ID");
     110           0 : }
     111           0 : void BaselineTable::setup()
     112             : {
     113           0 :   table_.addColumn(ArrayColumnDesc<Bool>("APPLY"));
     114           0 :   table_.addColumn(ArrayColumnDesc<uInt>("FUNC_TYPE"));
     115           0 :   table_.addColumn(ArrayColumnDesc<Int>("FUNC_PARAM"));
     116           0 :   table_.addColumn(ArrayColumnDesc<Float>("FUNC_FPARAM"));
     117           0 :   table_.addColumn(ArrayColumnDesc<uInt>("MASKLIST"));
     118           0 :   table_.addColumn(ArrayColumnDesc<Float>("RESULT"));
     119           0 :   table_.addColumn(ArrayColumnDesc<Float>("RMS"));
     120           0 :   table_.addColumn(ScalarColumnDesc<uInt>("NCHAN"));
     121           0 :   table_.addColumn(ArrayColumnDesc<Float>("CLIP_THRESHOLD"));
     122           0 :   table_.addColumn(ArrayColumnDesc<uInt>("CLIP_ITERATION"));
     123           0 :   table_.addColumn(ArrayColumnDesc<Bool>("USE_LF"));
     124           0 :   table_.addColumn(ArrayColumnDesc<Float>("LF_THRESHOLD"));
     125           0 :   table_.addColumn(ArrayColumnDesc<uInt>("LF_AVERAGE"));
     126           0 :   table_.addColumn(ArrayColumnDesc<uInt>("LF_EDGE"));
     127             : 
     128           0 :   table_.rwKeywordSet().define("ApplyType", "BASELINE");
     129             : 
     130           0 :   attachOptionalColumns();
     131           0 : }
     132             : 
     133           0 : void BaselineTable::attachOptionalColumns()
     134             : {
     135           0 :   applyCol_.attach(table_, "APPLY");
     136           0 :   ftypeCol_.attach(table_, "FUNC_TYPE");
     137           0 :   fparCol_.attach(table_, "FUNC_PARAM");
     138           0 :   ffparCol_.attach(table_, "FUNC_FPARAM");
     139           0 :   maskCol_.attach(table_, "MASKLIST");
     140           0 :   resCol_.attach(table_, "RESULT");
     141           0 :   rmsCol_.attach(table_, "RMS");
     142           0 :   nchanCol_.attach(table_, "NCHAN");
     143           0 :   cthresCol_.attach(table_, "CLIP_THRESHOLD");
     144           0 :   citerCol_.attach(table_, "CLIP_ITERATION");
     145           0 :   uselfCol_.attach(table_, "USE_LF");
     146           0 :   lfthresCol_.attach(table_, "LF_THRESHOLD");
     147           0 :   lfavgCol_.attach(table_, "LF_AVERAGE");
     148           0 :   lfedgeCol_.attach(table_, "LF_EDGE");
     149             : 
     150           0 :   ftypeCol_.rwKeywordSet().define("Polynomial",   BaselineType_kPolynomial);
     151           0 :   ftypeCol_.rwKeywordSet().define("Chebyshev",    BaselineType_kChebyshev);
     152           0 :   ftypeCol_.rwKeywordSet().define("Cubic Spline", BaselineType_kCubicSpline);
     153           0 :   ftypeCol_.rwKeywordSet().define("Sinusoid",     BaselineType_kSinusoid);
     154           0 : }
     155             : 
     156           0 : void BaselineTable::save(const std::string &filename)
     157             : {
     158           0 :   String inname(filename);
     159           0 :   Path path(inname);
     160           0 :   inname = path.expandedName();
     161           0 :   table_.deepCopy(inname, Table::New);
     162           0 : }
     163             : 
     164           0 : bool BaselineTable::getApply(uInt irow, uInt ipol) const
     165             : {
     166           0 :   Vector<Bool> apply = applyCol_.get(irow);
     167           0 :   return static_cast<bool>(apply[ipol]);
     168           0 : }
     169             : 
     170           0 : std::vector<bool> BaselineTable::getMask(uInt irow, uInt ipol)
     171             : {
     172           0 :   uInt nchan = nchanCol_.get(irow);
     173           0 :   Matrix<uInt> masklist = maskCol_.get(irow);
     174           0 :   return getMaskFromMaskList(nchan, masklist.row(ipol));
     175           0 : }
     176             : 
     177           0 : uint BaselineTable::getBaselineType(uInt irow, uInt ipol) const
     178             : {
     179           0 :   Vector<uInt> ftype = ftypeCol_.get(irow);
     180           0 :   return static_cast<uint>(ftype[ipol]);
     181           0 : }
     182             : 
     183           0 : int BaselineTable::getFPar(uInt irow, uInt ipol, uInt bltype) const
     184             : {
     185           0 :   auto fpar = fparCol_.get(irow);  // Get the stored data
     186             : 
     187           0 :   assert(fpar.ndim() == 1 || fpar.ndim() == 2);
     188           0 :   if (fpar.ndim() == 2) {  // If it is a matrix
     189           0 :     Matrix<Int> fpar_data = fpar;
     190             : 
     191           0 :     if (bltype == BaselineType_kSinusoid)
     192           0 :       return static_cast<int>(max(fpar_data.row(ipol)));
     193             :     
     194           0 :     auto res = static_cast<int>(fpar_data.row(ipol)[0]);
     195           0 :     return res;
     196           0 :   } 
     197             :   else {  // If it is a vector
     198           0 :     Vector<Int> fpar_data = fpar;
     199           0 :     return static_cast<int>(fpar_data[ipol]);
     200           0 :   }
     201           0 : }
     202             : 
     203           0 : std::vector<size_t> BaselineTable::getFParSinusoid(uInt irow, uInt ipol) const
     204             : {
     205             :   // To retrieve number of waves array
     206           0 :   Matrix<Int> fparsin = fparCol_.get(irow);
     207           0 :   Vector<Int> vector_nwave = fparsin.row(ipol);
     208           0 :   std::vector<size_t>preprocess(vector_nwave.begin(), vector_nwave.end());
     209             :   // Due to the use of Matrix in bltable creation, the shape must be rectangular
     210             :   // By the default initialized by 0s, therefore the following situation can occur
     211             :   // 0,0 ->[0,1,3,5,12]  0,1 ->[0,1,3,0,0]
     212             :   // Check all numbers except the first element which can be 0
     213           0 :   auto it = std::find(preprocess.begin() + 1, preprocess.end(), 0);
     214             : 
     215             :   // No 0s found return vector as it is
     216           0 :   if (it == preprocess.end()) return preprocess;
     217             : 
     218             :   // Return the subvector up to (but not including) the first 0
     219           0 :   return std::vector<size_t>(preprocess.begin(), it);
     220           0 : }
     221             : 
     222           0 : void BaselineTable::setbasedata(uInt irow, uInt scanno, uInt beamno, uInt antno, 
     223             :                                 uInt ifno, uInt freqid, Double time)
     224             : {
     225           0 :   scanCol_.put(irow, scanno);
     226           0 :   beamCol_.put(irow, beamno);
     227           0 :   antCol_.put(irow, antno);
     228           0 :   ifCol_.put(irow, ifno);
     229           0 :   timeCol_.put(irow, time);
     230           0 :   freqidCol_.put(irow, freqid);
     231           0 : }
     232             : 
     233           0 : void BaselineTable::setdata(uInt irow, uInt scanno, 
     234             :                             uInt beamno, uInt antno, uInt ifno, 
     235             :                             uInt freqid, Double time, 
     236             :                             Array<Bool> apply, 
     237             :                             Array<uInt> ftype,
     238             :                             Array<Int> fpar, 
     239             :                             Array<Float> ffpar, 
     240             :                             Array<uInt> mask,
     241             :                             Array<Float> res,
     242             :                             Array<Float> rms, 
     243             :                             uInt nchan, 
     244             :                             Array<Float> cthres,
     245             :                             Array<uInt> citer, 
     246             :                             Array<Bool> uself, 
     247             :                             Array<Float> lfthres, 
     248             :                             Array<uInt> lfavg, 
     249             :                             Array<uInt> lfedge)
     250             : {
     251           0 :   if (irow >= (uInt)nrow()) {
     252           0 :     stringstream ss;
     253           0 :     ss << "row index out of range[irow=" << irow << "][nrow=" << nrow() << "]";
     254           0 :     throw AipsError(ss.str());
     255           0 :   }
     256             : 
     257           0 :   setbasedata(irow, scanno, beamno, antno, ifno, freqid, time);
     258           0 :   applyCol_.put(irow, apply);
     259           0 :   ftypeCol_.put(irow, ftype);
     260           0 :   fparCol_.put(irow, fpar);
     261           0 :   ffparCol_.put(irow, ffpar);
     262           0 :   maskCol_.put(irow, mask);
     263           0 :   resCol_.put(irow, res);
     264           0 :   rmsCol_.put(irow, rms);
     265           0 :   nchanCol_.put(irow, nchan);
     266           0 :   cthresCol_.put(irow, cthres);
     267           0 :   citerCol_.put(irow, citer);
     268           0 :   uselfCol_.put(irow, uself);
     269           0 :   lfthresCol_.put(irow, lfthres);
     270           0 :   lfavgCol_.put(irow, lfavg);
     271           0 :   lfedgeCol_.put(irow, lfedge);
     272           0 : }
     273             : 
     274           0 : void BaselineTable::appenddata(uInt scanno, uInt beamno, 
     275             :                                uInt antno, uInt ifno, 
     276             :                                uInt freqid, Double time, 
     277             :                                Array<Bool> apply, 
     278             :                                Array<uInt> ftype, 
     279             :                                Array<Int> fpar, 
     280             :                                Array<Float> ffpar, 
     281             :                                Array<uInt> mask,
     282             :                                Array<Float> res,
     283             :                                Array<Float> rms,
     284             :                                uInt nchan, 
     285             :                                Array<Float> cthres,
     286             :                                Array<uInt> citer, 
     287             :                                Array<Bool> uself, 
     288             :                                Array<Float> lfthres, 
     289             :                                Array<uInt> lfavg, 
     290             :                                Array<uInt> lfedge)
     291             : {
     292           0 :   uInt irow = nrow();
     293           0 :   table_.addRow(1, true);
     294           0 :   setdata(irow, scanno, beamno, antno, ifno, freqid, time, 
     295             :           apply, ftype, fpar, ffpar, mask, res, rms, 
     296             :           nchan, cthres, citer, uself, lfthres, lfavg, lfedge);
     297           0 : }
     298             : 
     299           0 : void BaselineTable::appendbasedata(int scanno, int beamno, 
     300             :                                    int antno, int ifno, 
     301             :                                    int freqid, Double time)
     302             : {
     303           0 :   uInt irow = nrow();
     304           0 :   table_.addRow(1, true);
     305           0 :   setbasedata(irow, uInt(scanno), uInt(beamno), uInt(antno), 
     306             :               uInt(ifno), uInt(freqid), time);
     307           0 : }
     308             : 
     309           0 : void BaselineTable::setresult(uInt irow, 
     310             :                               Vector<Float> res, 
     311             :                               Array<Float> rms)
     312             : {
     313           0 :   resCol_.put(irow, res);
     314           0 :   rmsCol_.put(irow, rms);
     315           0 : }
     316             : 
     317           0 : uInt BaselineTable::getNChan(int irow)
     318             : {
     319           0 :   return nchanCol_.get(irow);
     320             : }
     321             : 
     322           0 : uInt BaselineTable::nchan(uInt ifno)
     323             : {
     324           0 :   uInt tmp = ifno;
     325           0 :   return tmp;
     326             : }
     327             : 
     328           0 : std::vector<bool> BaselineTable::getMaskFromMaskList(uInt const nchan, Vector<uInt> const& masklist)
     329             : {
     330           0 :   if (masklist.size() % 2 != 0) {
     331           0 :     throw(AipsError("masklist must have even number of elements."));
     332             :   }
     333             : 
     334           0 :   std::vector<bool> res((int)nchan, false);
     335             : 
     336           0 :   for (uInt j = 0; j < masklist.size(); j += 2) {
     337           0 :     for (int i = masklist[j]; i <= min((Int)nchan-1, (Int)masklist[j+1]); ++i) {
     338           0 :       res[i] = true;
     339             :     }
     340           0 :     if (masklist[j+1] == nchan-1) {
     341           0 :       break;
     342             :     }
     343             :   }
     344             : 
     345           0 :   return res;
     346           0 : }
     347             : }

Generated by: LCOV version 1.16