LCOV - code coverage report
Current view: top level - imageanalysis/IO - ProfileFitterEstimatesFileParser.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 64 85 75.3 %
Date: 2024-12-11 20:54:31 Functions: 5 7 71.4 %

          Line data    Source code
       1             : //# Copyright (C) 1996,1997,1998,1999,2001,2002
       2             : //# Associated Universities, Inc. Washington DC, USA.
       3             : //#
       4             : //# This library is free software; you can redistribute it and/or modify it
       5             : //# under the terms of the GNU Library General Public License as published by
       6             : //# the Free Software Foundation; either version 2 of the License, or (at your
       7             : //# option) any later version.
       8             : //#
       9             : //# This library is distributed in the hope that it will be useful, but WITHOUT
      10             : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      11             : //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
      12             : //# License for more details.
      13             : //#
      14             : //# You should have received a copy of the GNU Library General Public License
      15             : //# along with this library; if not, write to the Free Software Foundation,
      16             : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
      17             : //#
      18             : //# Correspondence concerning AIPS++ should be addressed as follows:
      19             : //#        Internet email: casa-feedback@nrao.edu.
      20             : //#        Postal address: AIPS++ Project Office
      21             : //#                        National Radio Astronomy Observatory
      22             : //#                        520 Edgemont Road
      23             : //#                        Charlottesville, VA 22903-2475 USA
      24             : //#
      25             : //# $Id:  $
      26             : 
      27             : #include <imageanalysis/IO/ProfileFitterEstimatesFileParser.h>
      28             : 
      29             : #include <casacore/casa/aips.h>
      30             : #include <casacore/casa/IO/RegularFileIO.h>
      31             : #include <casacore/casa/Utilities/Regex.h>
      32             : #include <casacore/casa/Containers/Record.h>
      33             : #include <components/SpectralComponents/GaussianSpectralElement.h>
      34             : #include <casacore/images/Images/ImageStatistics.h>
      35             : 
      36             : 
      37             : using namespace casacore;
      38             : namespace casa { //# NAMESPACE CASA - BEGIN
      39             : 
      40             : const String ProfileFitterEstimatesFileParser::_class = "FitterEstimatesFileParser";
      41             : 
      42           5 : ProfileFitterEstimatesFileParser::ProfileFitterEstimatesFileParser (
      43             :         const String& filename
      44           5 : ) : _spectralList(), _fixedValues(0), _log(LogIO()), _peakValues(0),
      45           5 :         _centerValues(0), _fwhmValues(0), _contents("") {
      46           5 :         RegularFile myFile(filename);
      47           5 :         _log << LogOrigin(_class, __FUNCTION__);
      48             : 
      49           5 :         if (! myFile.exists()) {
      50           0 :                 _log << "Estimates file " << filename << " does not exist"
      51           0 :                         << LogIO::EXCEPTION;
      52             :         }
      53           5 :         if (! myFile.isReadable()) {
      54           0 :                 _log << "Estimates file " << filename << " is not readable"
      55           0 :                         << LogIO::EXCEPTION;
      56             :         }
      57           5 :         _parseFile(myFile);
      58           5 :         _createSpectralList();
      59             : 
      60           5 : }
      61             : 
      62           5 : ProfileFitterEstimatesFileParser::~ProfileFitterEstimatesFileParser() {}
      63             : 
      64           5 : SpectralList ProfileFitterEstimatesFileParser::getEstimates() const {
      65           5 :         return _spectralList;
      66             : }
      67             : 
      68           0 : vector<String> ProfileFitterEstimatesFileParser::getFixed() const {
      69           0 :         return _fixedValues;
      70             : }
      71             : 
      72           0 : String ProfileFitterEstimatesFileParser::getContents() const {
      73           0 :         return _contents;
      74             : }
      75             : 
      76           5 : void ProfileFitterEstimatesFileParser::_parseFile(
      77             :         const RegularFile& myFile
      78             : ) {
      79           5 :         _log << LogOrigin(_class, __FUNCTION__);
      80             : 
      81           5 :         RegularFileIO fileIO(myFile);
      82             :         // I doubt a genuine estimates file will ever have this many characters
      83           5 :         Int bufSize = 4096;
      84           5 :         char *buffer = new char[bufSize];
      85             :         int nRead;
      86             : 
      87           5 :         while ((nRead = fileIO.read(bufSize, buffer, false)) == bufSize) {
      88           0 :                 _log << LogIO::NORMAL << "read: " << nRead << LogIO::POST;
      89           0 :                 String chunk(buffer, bufSize);
      90             : 
      91           0 :                 _contents += chunk;
      92           0 :         }
      93             :         // get the last chunk
      94           5 :         String chunk(buffer, nRead);
      95           5 :         _contents += chunk;
      96             : 
      97           5 :         Vector<String> lines = stringToVector(_contents, '\n');
      98           5 :         Regex blankLine("^[ \n\t\r\v\f]+$",1000);
      99          22 :         for(Vector<String>::iterator iter=lines.begin(); iter!=lines.end(); iter++) {
     100          17 :                 if (iter->empty() || iter->firstchar() == '#' ||  iter->matches(blankLine)) {
     101             :                         // ignore comments and blank lines
     102          10 :                         continue;
     103             :                 }
     104           7 :                 uInt commaCount = iter->freq(',');
     105           7 :                 if (commaCount < 2 || commaCount > 3) {
     106           0 :                         _log << "bad format for line " << *iter << LogIO::EXCEPTION;
     107             :                 }
     108           7 :                 Vector<String> parts = stringToVector(*iter);
     109          29 :                 for (Vector<String>::iterator viter = parts.begin(); viter != parts.end(); viter++) {
     110          22 :                         viter->trim();
     111           7 :                 }
     112          14 :                 String filename = myFile.path().dirName() + "/" + myFile.path().baseName();
     113           7 :                 String peak = parts[0];
     114           7 :                 String center = parts[1];
     115           7 :                 String fwhm = parts[2];
     116             : 
     117           7 :                 String fixedMask;
     118             : 
     119           7 :                 if (! peak.matches(RXdouble) ) {
     120           0 :                         _log << "File " << filename << ", line " << *iter
     121             :                                 << ": peak value " << peak << " is not numeric"
     122           0 :                                 << LogIO::EXCEPTION;
     123             :                 }
     124           7 :                 _peakValues.push_back(String::toDouble(peak));
     125             : 
     126           7 :                 if (! center.matches(RXdouble) ) {
     127           0 :                         _log << "File " << filename << ", line " << *iter
     128             :                                 << ": x position value " << center << " is not numeric"
     129           0 :                                 << LogIO::EXCEPTION;
     130             :                 }
     131           7 :                 _centerValues.push_back(String::toDouble(center));
     132             : 
     133           7 :                 if (! fwhm.matches(RXdouble)) {
     134           0 :                         _log << "File " << filename << ", line " << *iter
     135             :                                 << ": Major axis value " << fwhm << " is not numeric"
     136           0 :                                 << LogIO::EXCEPTION;
     137             :                 }
     138           7 :                 _fwhmValues.push_back(String::toDouble(fwhm));
     139             : 
     140           7 :                 if (parts.size() == 4) {
     141           1 :                         fixedMask = parts[3];
     142           1 :                         for (
     143           1 :                                 String::iterator siter = fixedMask.begin();
     144           2 :                                 siter != fixedMask.end(); siter++
     145             :                         ) {
     146           1 :                                 if (
     147           1 :                                         *siter != 'c' && *siter != 'f' && *siter != 'p'
     148             :                                 ) {
     149           0 :                                         _log << "fixed parameter ID " << String(*siter) << " is not recognized"
     150           0 :                                                 << LogIO::EXCEPTION;
     151             :                                 }
     152             :                         }
     153             :                 }
     154           7 :                 _fixedValues.push_back(fixedMask);
     155          12 :         }
     156           5 : }
     157             : 
     158           5 : void ProfileFitterEstimatesFileParser::_createSpectralList() {
     159          12 :         for (uInt i=0; i<_peakValues.size(); i++) {
     160             :                 GaussianSpectralElement se(
     161           7 :                         _peakValues[i],
     162           7 :                         _centerValues[i],
     163           7 :                         GaussianSpectralElement::sigmaFromFWHM(_fwhmValues[i])
     164           7 :                 );
     165           7 :                 if (! _fixedValues[i].empty()) {
     166           1 :                         se.fixByString(_fixedValues[i]);
     167             :                 }
     168           7 :                 _spectralList.add(se);
     169           7 :         }
     170             : 
     171           5 : }
     172             : } //# NAMESPACE CASA - END
     173             : 

Generated by: LCOV version 1.16