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 0 : ProfileFitterEstimatesFileParser::ProfileFitterEstimatesFileParser ( 43 : const String& filename 44 0 : ) : _spectralList(), _fixedValues(0), _log(LogIO()), _peakValues(0), 45 0 : _centerValues(0), _fwhmValues(0), _contents("") { 46 0 : RegularFile myFile(filename); 47 0 : _log << LogOrigin(_class, __FUNCTION__); 48 : 49 0 : if (! myFile.exists()) { 50 0 : _log << "Estimates file " << filename << " does not exist" 51 0 : << LogIO::EXCEPTION; 52 : } 53 0 : if (! myFile.isReadable()) { 54 0 : _log << "Estimates file " << filename << " is not readable" 55 0 : << LogIO::EXCEPTION; 56 : } 57 0 : _parseFile(myFile); 58 0 : _createSpectralList(); 59 : 60 0 : } 61 : 62 0 : ProfileFitterEstimatesFileParser::~ProfileFitterEstimatesFileParser() {} 63 : 64 0 : SpectralList ProfileFitterEstimatesFileParser::getEstimates() const { 65 0 : 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 0 : void ProfileFitterEstimatesFileParser::_parseFile( 77 : const RegularFile& myFile 78 : ) { 79 0 : _log << LogOrigin(_class, __FUNCTION__); 80 : 81 0 : RegularFileIO fileIO(myFile); 82 : // I doubt a genuine estimates file will ever have this many characters 83 0 : Int bufSize = 4096; 84 0 : char *buffer = new char[bufSize]; 85 : int nRead; 86 : 87 0 : 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 0 : String chunk(buffer, nRead); 95 0 : _contents += chunk; 96 : 97 0 : Vector<String> lines = stringToVector(_contents, '\n'); 98 0 : Regex blankLine("^[ \n\t\r\v\f]+$",1000); 99 0 : for(Vector<String>::iterator iter=lines.begin(); iter!=lines.end(); iter++) { 100 0 : if (iter->empty() || iter->firstchar() == '#' || iter->matches(blankLine)) { 101 : // ignore comments and blank lines 102 0 : continue; 103 : } 104 0 : uInt commaCount = iter->freq(','); 105 0 : if (commaCount < 2 || commaCount > 3) { 106 0 : _log << "bad format for line " << *iter << LogIO::EXCEPTION; 107 : } 108 0 : Vector<String> parts = stringToVector(*iter); 109 0 : for (Vector<String>::iterator viter = parts.begin(); viter != parts.end(); viter++) { 110 0 : viter->trim(); 111 0 : } 112 0 : String filename = myFile.path().dirName() + "/" + myFile.path().baseName(); 113 0 : String peak = parts[0]; 114 0 : String center = parts[1]; 115 0 : String fwhm = parts[2]; 116 : 117 0 : String fixedMask; 118 : 119 0 : if (! peak.matches(RXdouble) ) { 120 0 : _log << "File " << filename << ", line " << *iter 121 : << ": peak value " << peak << " is not numeric" 122 0 : << LogIO::EXCEPTION; 123 : } 124 0 : _peakValues.push_back(String::toDouble(peak)); 125 : 126 0 : 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 0 : _centerValues.push_back(String::toDouble(center)); 132 : 133 0 : 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 0 : _fwhmValues.push_back(String::toDouble(fwhm)); 139 : 140 0 : if (parts.size() == 4) { 141 0 : fixedMask = parts[3]; 142 0 : for ( 143 0 : String::iterator siter = fixedMask.begin(); 144 0 : siter != fixedMask.end(); siter++ 145 : ) { 146 0 : if ( 147 0 : *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 0 : _fixedValues.push_back(fixedMask); 155 0 : } 156 0 : } 157 : 158 0 : void ProfileFitterEstimatesFileParser::_createSpectralList() { 159 0 : for (uInt i=0; i<_peakValues.size(); i++) { 160 : GaussianSpectralElement se( 161 0 : _peakValues[i], 162 0 : _centerValues[i], 163 0 : GaussianSpectralElement::sigmaFromFWHM(_fwhmValues[i]) 164 0 : ); 165 0 : if (! _fixedValues[i].empty()) { 166 0 : se.fixByString(_fixedValues[i]); 167 : } 168 0 : _spectralList.add(se); 169 0 : } 170 : 171 0 : } 172 : } //# NAMESPACE CASA - END 173 :