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/FitterEstimatesFileParser.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/ComponentModels/ConstantSpectrum.h>
34 : #include <components/ComponentModels/Flux.h>
35 : #include <components/ComponentModels/GaussianShape.h>
36 : #include <casacore/coordinates/Coordinates/CoordinateUtil.h>
37 : #include <casacore/coordinates/Coordinates/DirectionCoordinate.h>
38 : #include <casacore/images/Images/ImageStatistics.h>
39 :
40 :
41 : using namespace casacore;
42 : namespace casa {
43 :
44 0 : FitterEstimatesFileParser::~FitterEstimatesFileParser() {}
45 :
46 0 : ComponentList FitterEstimatesFileParser::getEstimates() const {
47 0 : return _componentList;
48 : }
49 :
50 0 : Vector<String> FitterEstimatesFileParser::getFixed() const {
51 0 : return _fixedValues;
52 : }
53 :
54 0 : String FitterEstimatesFileParser::getContents() const {
55 0 : return _contents;
56 : }
57 :
58 0 : void FitterEstimatesFileParser::_parseFile(
59 : const RegularFile& myFile
60 : ) {
61 0 : _log->origin(LogOrigin("FitterEstimatesFileParser",__func__));
62 :
63 0 : RegularFileIO fileIO(myFile);
64 : // I doubt a genuine estimates file will ever have this many characters
65 0 : Int bufSize = 4096;
66 0 : char *buffer = new char[bufSize];
67 : int nRead;
68 :
69 0 : while ((nRead = fileIO.read(bufSize, buffer, false)) == bufSize) {
70 0 : *_log << LogIO::NORMAL << "read: " << nRead << LogIO::POST;
71 0 : String chunk(buffer, bufSize);
72 :
73 0 : _contents += chunk;
74 0 : }
75 : // get the last chunk
76 0 : String chunk(buffer, nRead);
77 0 : _contents += chunk;
78 :
79 0 : Vector<String> lines = stringToVector(_contents, '\n');
80 0 : Regex blankLine("^[ \n\t\r\v\f]+$",1000);
81 0 : uInt componentIndex = 0;
82 0 : Vector<String>::iterator end = lines.end();
83 0 : String filename = myFile.path().dirName() + "/" + myFile.path().baseName();
84 0 : for(Vector<String>::iterator iter=lines.begin(); iter!=end; iter++) {
85 0 : if (iter->empty() || iter->firstchar() == '#' || iter->matches(blankLine)) {
86 : // ignore comments and blank lines
87 0 : continue;
88 : }
89 0 : uInt commaCount = iter->freq(',');
90 0 : ThrowIf(
91 : commaCount < 5 || commaCount > 6,
92 : "bad format for line " + *iter
93 : );
94 0 : Vector<String> parts = stringToVector(*iter);
95 0 : for (Vector<String>::iterator viter = parts.begin(); viter != parts.end(); viter++) {
96 0 : viter->trim();
97 0 : }
98 0 : String peak = parts[0];
99 0 : String xpos = parts[1];
100 0 : String ypos = parts[2];
101 0 : String maj = parts[3];
102 0 : String min = parts[4];
103 0 : String pa = parts[5];
104 :
105 0 : String fixedMask;
106 0 : _peakValues.resize(componentIndex + 1, true);
107 0 : _xposValues.resize(componentIndex + 1, true);
108 0 : _yposValues.resize(componentIndex + 1, true);
109 0 : _majValues.resize(componentIndex + 1, true);
110 0 : _minValues.resize(componentIndex + 1, true);
111 0 : _paValues.resize(componentIndex + 1, true);
112 0 : _fixedValues.resize(componentIndex + 1, true);
113 :
114 0 : ThrowIf(
115 : ! peak.matches(RXdouble),
116 : "File " + filename + ", line " + *iter
117 : + ": peak value " + peak + " is not numeric"
118 : );
119 0 : _peakValues(componentIndex) = String::toDouble(peak);
120 :
121 0 : if (! xpos.matches(RXdouble) ) {
122 0 : *_log << "File " << filename << ", line " << *iter
123 : << ": x position value " << xpos << " is not numeric"
124 0 : << LogIO::EXCEPTION;
125 : }
126 0 : _xposValues(componentIndex) = String::toDouble(xpos);
127 :
128 0 : if (! ypos.matches(RXdouble) ) {
129 0 : *_log << "File " << filename << ", line " << *iter
130 : << ": y position value " << ypos << " is not numeric"
131 0 : << LogIO::EXCEPTION;
132 : }
133 0 : _yposValues(componentIndex) = String::toDouble(ypos);
134 :
135 0 : Quantity majQuantity;
136 0 : ThrowIf(
137 : ! readQuantity(majQuantity, maj),
138 : "File " + filename + ", line " + *iter
139 : + ": Major axis value " + maj + " is not a quantity"
140 : );
141 0 : _majValues(componentIndex) = majQuantity;
142 :
143 0 : Quantity minQuantity;
144 0 : ThrowIf(
145 : ! readQuantity(minQuantity, min),
146 : "File " + filename + ", line " + *iter
147 : + ": Major axis value " + min + " is not a quantity"
148 : );
149 0 : _minValues(componentIndex) = minQuantity;
150 :
151 0 : Quantity paQuantity;
152 0 : ThrowIf(
153 : ! readQuantity(paQuantity, pa),
154 : "File " + filename + ", line " + *iter
155 : + ": Position angle value " + pa + " is not a quantity"
156 : );
157 0 : _paValues(componentIndex) = paQuantity;
158 :
159 0 : if (parts.size() == 7) {
160 0 : fixedMask = parts[6];
161 0 : for (
162 0 : String::iterator siter = fixedMask.begin();
163 0 : siter != fixedMask.end(); siter++
164 : ) {
165 0 : if (
166 0 : *siter != 'a' && *siter != 'b' && *siter != 'f'
167 0 : && *siter != 'p' && *siter != 'x' && *siter != 'y'
168 : ) {
169 0 : *_log << "fixed parameter ID " << String(*siter) << " is not recognized"
170 0 : << LogIO::EXCEPTION;
171 : }
172 : }
173 0 : _fixedValues(componentIndex) = fixedMask;
174 : }
175 0 : _fixedValues(componentIndex) = fixedMask;
176 0 : componentIndex++;
177 0 : }
178 0 : ThrowIf(componentIndex == 0, "No valid estmates were found in file " + filename);
179 0 : }
180 :
181 : }
|