LCOV - code coverage report
Current view: top level - msvis/MSVis/test - tHanningSmooth.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 53 60 88.3 %
Date: 2024-10-29 13:38:20 Functions: 1 1 100.0 %

          Line data    Source code
       1             : //# tHanningSmooth.cc: Tests Hanning smoothing of spectral channels
       2             : //# Copyright (C) 1995,1999,2000,2001
       3             : //# Associated Universities, Inc. Washington DC, USA.
       4             : //#
       5             : //# This program is free software; you can redistribute it and/or modify it
       6             : //# under the terms of the GNU General Public License as published by the Free
       7             : //# Software Foundation; either version 2 of the License, or (at your option)
       8             : //# any later version.
       9             : //#
      10             : //# This program 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 General Public License for
      13             : //# more details.
      14             : //#
      15             : //# You should have received a copy of the GNU General Public License along
      16             : //# with this program; if not, write to the Free Software Foundation, Inc.,
      17             : //# 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             : 
      28             : #include <casacore/casa/aips.h>
      29             : #include <casacore/casa/Exceptions/Error.h>
      30             : #include <casacore/casa/iostream.h>
      31             : #include <msvis/MSVis/VisSet.h>
      32             : #include <msvis/MSVis/VisBuffer.h>
      33             : #include <msvis/MSVis/VisibilityIterator.h>
      34             : #include <casacore/casa/OS/Timer.h>
      35             : #include <casacore/casa/iomanip.h>
      36             : #include <casacore/casa/namespace.h>
      37             : #include <casacore/casa/OS/EnvVar.h>
      38             : 
      39             : using namespace std;
      40             : using namespace casa;
      41             : 
      42           1 : int main(int argc, char **argv) {
      43             : 
      44           1 :   std::string copy_input_ms;
      45             :   try {
      46           1 :     std::string source_input_ms;
      47           1 :     if (argc < 2)
      48             :     {
      49           1 :       String casadata = EnvironmentVariable::get("CASADATA");
      50           1 :       if (casadata.empty()) {
      51           0 :           throw(AipsError("CASADATA env variable not defined and no file given in command line"));
      52             :       }
      53           1 :       source_input_ms = casadata + "/unittest/hanningsmooth/ngc5921_ut.ms";
      54           1 :     }
      55             :     else
      56             :     {
      57           0 :       source_input_ms = argv[1];
      58             :     }
      59           1 :     copy_input_ms = "ngc5921_ut.ms";
      60             : 
      61             :     // Make a copy of the MS in the working directory
      62             :     // TODO: Use C++17 filesystem copy https://en.cppreference.com/w/cpp/filesystem/copy
      63           2 :     String cp_command = std::string("cp -RL ") + source_input_ms + " .";
      64           1 :     auto cmd_ecode = system(cp_command.c_str());
      65           1 :     if (!cmd_ecode)
      66           1 :         std::cerr << "Failed to copy " << copy_input_ms << " to current directory" <<std::endl;
      67             : 
      68           1 :     MS ms(copy_input_ms, Table::Update);
      69             : 
      70           1 :     Block<int> sort(4);
      71           1 :     sort[2] = MS::FIELD_ID;
      72           1 :     sort[3] = MS::ARRAY_ID;
      73           1 :     sort[1] = MS::DATA_DESC_ID;
      74           1 :     sort[0] = MS::TIME;
      75           1 :     Matrix<Int> allChannels;
      76             : 
      77           1 :     VisSet vs(ms,sort,allChannels, 0.0);
      78           1 :     VisIter& vi(vs.iter());
      79           1 :     VisBuffer vb(vi);
      80             : 
      81             :     Int row, chn, pol;
      82             : 
      83           7 :     for (vi.originChunks();vi.moreChunks();vi.nextChunk()) {
      84          66 :       for (vi.origin();vi.more();vi++) {
      85             : 
      86          60 :         Cube<Complex>& vc= vb.correctedVisCube();
      87          60 :         Cube<Bool>& fc= vb.flagCube();
      88             : 
      89          60 :         Int nRow=vb.nRow();
      90          60 :         Int nChan=vb.nChannel();
      91          60 :         Int nPol=vi.visibilityShape()(0);
      92          60 :         Cube<Complex> smoothedData(nPol,nChan,nRow);
      93             :         //smoothedData.reference(vb.correctedVisCube());
      94          60 :         Cube<Bool> newFlag(nPol,nChan,nRow);
      95             :         //newFlag.reference(vb.flagCube());
      96       22713 :         for (row=0; row<nRow; row++) {
      97       67959 :           for (pol=0; pol<nPol; pol++) {
      98       45306 :             smoothedData(pol,0,row) = vc(pol,0,row); // not really necessary
      99       45306 :             newFlag(pol,0,row) = true;    // since we must flag first channel
     100     2808972 :             for (chn=1; chn<nChan-1; chn++) {
     101     2763666 :               smoothedData(pol,chn,row) =
     102     2763666 :                 vc(pol,chn-1,row)*0.25 + vc(pol,chn,row)*0.50 +
     103     5527332 :                 vc(pol,chn+1,row)*0.25;
     104     2763666 :               newFlag(pol,chn,row) =
     105     2763666 :                 fc(pol,chn-1,row)||fc(pol,chn,row)||fc(pol,chn+1,row);
     106             :             }
     107       45306 :             smoothedData(pol,nChan-1,row) = vc(pol,nChan-1,row);
     108       45306 :             newFlag(pol,nChan-1,row) = true;  // flag last channel
     109             :           }
     110             :         }
     111          60 :         vi.setVisAndFlag(smoothedData,newFlag,VisibilityIterator::Corrected);
     112          60 :       }
     113             :     }
     114           1 :   }
     115           0 :   catch (const AipsError &x) {
     116           0 :     cerr << "***Exception:" << endl;
     117           0 :     cerr << x.getMesg() << endl;
     118           0 :     return 1;
     119           0 :   }
     120             :   
     121             :   //TODO: Use C++ remove_all https://en.cppreference.com/w/cpp/filesystem/remove
     122           2 :   String rm_command = std::string("rm -rf ") + copy_input_ms;
     123           1 :   auto cmd_ecode = system(rm_command.c_str());
     124           1 :   if (!cmd_ecode)
     125           1 :       std::cerr << "Failed to remove temporary copy " << copy_input_ms <<std::endl;
     126             : 
     127           1 :   return 0;
     128           1 : }
     129             : 

Generated by: LCOV version 1.16