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 22 : FitterEstimatesFileParser::~FitterEstimatesFileParser() {}
45 :
46 22 : ComponentList FitterEstimatesFileParser::getEstimates() const {
47 22 : return _componentList;
48 : }
49 :
50 22 : Vector<String> FitterEstimatesFileParser::getFixed() const {
51 22 : return _fixedValues;
52 : }
53 :
54 22 : String FitterEstimatesFileParser::getContents() const {
55 22 : return _contents;
56 : }
57 :
58 23 : void FitterEstimatesFileParser::_parseFile(
59 : const RegularFile& myFile
60 : ) {
61 23 : _log->origin(LogOrigin("FitterEstimatesFileParser",__func__));
62 :
63 23 : RegularFileIO fileIO(myFile);
64 : // I doubt a genuine estimates file will ever have this many characters
65 23 : Int bufSize = 4096;
66 23 : char *buffer = new char[bufSize];
67 : int nRead;
68 :
69 23 : 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 23 : String chunk(buffer, nRead);
77 23 : _contents += chunk;
78 :
79 23 : Vector<String> lines = stringToVector(_contents, '\n');
80 23 : Regex blankLine("^[ \n\t\r\v\f]+$",1000);
81 23 : uInt componentIndex = 0;
82 23 : Vector<String>::iterator end = lines.end();
83 46 : String filename = myFile.path().dirName() + "/" + myFile.path().baseName();
84 121 : for(Vector<String>::iterator iter=lines.begin(); iter!=end; iter++) {
85 98 : if (iter->empty() || iter->firstchar() == '#' || iter->matches(blankLine)) {
86 : // ignore comments and blank lines
87 56 : continue;
88 : }
89 42 : uInt commaCount = iter->freq(',');
90 42 : ThrowIf(
91 : commaCount < 5 || commaCount > 6,
92 : "bad format for line " + *iter
93 : );
94 42 : Vector<String> parts = stringToVector(*iter);
95 310 : for (Vector<String>::iterator viter = parts.begin(); viter != parts.end(); viter++) {
96 268 : viter->trim();
97 42 : }
98 42 : String peak = parts[0];
99 42 : String xpos = parts[1];
100 42 : String ypos = parts[2];
101 42 : String maj = parts[3];
102 42 : String min = parts[4];
103 42 : String pa = parts[5];
104 :
105 42 : String fixedMask;
106 42 : _peakValues.resize(componentIndex + 1, true);
107 42 : _xposValues.resize(componentIndex + 1, true);
108 42 : _yposValues.resize(componentIndex + 1, true);
109 42 : _majValues.resize(componentIndex + 1, true);
110 42 : _minValues.resize(componentIndex + 1, true);
111 42 : _paValues.resize(componentIndex + 1, true);
112 42 : _fixedValues.resize(componentIndex + 1, true);
113 :
114 42 : ThrowIf(
115 : ! peak.matches(RXdouble),
116 : "File " + filename + ", line " + *iter
117 : + ": peak value " + peak + " is not numeric"
118 : );
119 42 : _peakValues(componentIndex) = String::toDouble(peak);
120 :
121 42 : 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 42 : _xposValues(componentIndex) = String::toDouble(xpos);
127 :
128 42 : 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 42 : _yposValues(componentIndex) = String::toDouble(ypos);
134 :
135 42 : Quantity majQuantity;
136 42 : ThrowIf(
137 : ! readQuantity(majQuantity, maj),
138 : "File " + filename + ", line " + *iter
139 : + ": Major axis value " + maj + " is not a quantity"
140 : );
141 42 : _majValues(componentIndex) = majQuantity;
142 :
143 42 : Quantity minQuantity;
144 42 : ThrowIf(
145 : ! readQuantity(minQuantity, min),
146 : "File " + filename + ", line " + *iter
147 : + ": Major axis value " + min + " is not a quantity"
148 : );
149 42 : _minValues(componentIndex) = minQuantity;
150 :
151 42 : Quantity paQuantity;
152 42 : ThrowIf(
153 : ! readQuantity(paQuantity, pa),
154 : "File " + filename + ", line " + *iter
155 : + ": Position angle value " + pa + " is not a quantity"
156 : );
157 42 : _paValues(componentIndex) = paQuantity;
158 :
159 42 : if (parts.size() == 7) {
160 16 : fixedMask = parts[6];
161 16 : for (
162 16 : String::iterator siter = fixedMask.begin();
163 18 : siter != fixedMask.end(); siter++
164 : ) {
165 2 : if (
166 4 : *siter != 'a' && *siter != 'b' && *siter != 'f'
167 4 : && *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 16 : _fixedValues(componentIndex) = fixedMask;
174 : }
175 42 : _fixedValues(componentIndex) = fixedMask;
176 42 : componentIndex++;
177 65 : }
178 24 : ThrowIf(componentIndex == 0, "No valid estmates were found in file " + filename);
179 28 : }
180 :
181 : }
|