| 1 |
/* |
| 2 |
* The MIT License |
| 3 |
|
| 4 |
Conograph (powder auto-indexing program) |
| 5 |
|
| 6 |
Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in |
| 16 |
all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
THE SOFTWARE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
#include <fstream> |
| 28 |
#include <sstream> |
| 29 |
#include "utility_data_structure/range.hh" |
| 30 |
#include "utility_func/zstring.hh" |
| 31 |
#include "utility_func/zmath.hh" |
| 32 |
#include "zlog/zlog.hh" |
| 33 |
#include "PeakPosData.hh" |
| 34 |
|
| 35 |
using namespace std; |
| 36 |
|
| 37 |
PeakPosData::PeakPosData() |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
PeakPosData::~PeakPosData() |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
ZErrorMessageReadingFile PeakPosData::readFile(const string& filename) |
| 46 |
{ |
| 47 |
m_XWave.clear(); |
| 48 |
m_YInt.clear(); |
| 49 |
m_YErr.clear(); |
| 50 |
PeakPosX.clear(); |
| 51 |
PeakPosY.clear(); |
| 52 |
PeakWidth.clear(); |
| 53 |
toUseFlag.clear(); |
| 54 |
|
| 55 |
ifstream ifs( filename.c_str() ); |
| 56 |
|
| 57 |
if (!ifs){ |
| 58 |
return ZErrorMessageReadingFile(filename, |
| 59 |
ZErrorMessage( ZErrorFileNotFound, __FILE__ , __LINE__, __FUNCTION__)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
int count = 0; |
| 64 |
string s; |
| 65 |
|
| 66 |
try{ |
| 67 |
// read headers while count <= 0. |
| 68 |
string header; |
| 69 |
while( count <= 0 && getnewline(ifs, s) == ZErrorNoError ) |
| 70 |
{ |
| 71 |
if(is_blank(s)) continue; |
| 72 |
replace(s.begin(),s.end(),',',' '); |
| 73 |
istringstream iss(s); |
| 74 |
iss >> header; |
| 75 |
|
| 76 |
if(count == 0) |
| 77 |
{ |
| 78 |
if (header != "IGOR") throw ZErrorNotIGORFile; |
| 79 |
count = -1; |
| 80 |
} |
| 81 |
else if (header.substr(0,5) == "WAVES") |
| 82 |
{ |
| 83 |
iss >> m_XWave_title; |
| 84 |
if( iss.fail() ) throw 1; |
| 85 |
iss >> m_YInt_title; |
| 86 |
if( iss.fail() ) throw 1; |
| 87 |
iss >> m_YErr_title; |
| 88 |
if( iss.fail() ) throw ZErrorErrorsAreNotContained; |
| 89 |
|
| 90 |
count = 1; |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
count = 0; |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
if( count <= 0 ) |
| 99 |
{ |
| 100 |
return ZErrorMessageReadingFile(filename, |
| 101 |
ZErrorMessage("There is no headers", __FILE__, __LINE__, __FUNCTION__)); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
//read Profile Data. |
| 106 |
Double t; |
| 107 |
while (getnewline(ifs, s) == ZErrorNoError) |
| 108 |
{ |
| 109 |
if(is_blank(s)) continue; |
| 110 |
if(s.substr(0,5) == "BEGIN") continue; |
| 111 |
if(s.substr(0,3) == "END" ) break; |
| 112 |
|
| 113 |
istringstream iss(s); |
| 114 |
|
| 115 |
iss >> t; |
| 116 |
if( iss.fail() ) throw 1; |
| 117 |
m_XWave.push_back(t); |
| 118 |
|
| 119 |
iss >> t; |
| 120 |
if( iss.fail() ) throw 1; |
| 121 |
m_YInt.push_back(t); |
| 122 |
|
| 123 |
iss >> t; |
| 124 |
if( iss.fail() ) throw 1; |
| 125 |
m_YErr.push_back(t); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
bool flag = false; |
| 130 |
while( getnewline(ifs, s) == ZErrorNoError ) |
| 131 |
{ |
| 132 |
if(is_blank(s)) continue; |
| 133 |
replace(s.begin(),s.end(),',',' '); |
| 134 |
istringstream iss(s); |
| 135 |
iss >> header; |
| 136 |
|
| 137 |
if (header.substr(0,5) == "WAVES") |
| 138 |
{ |
| 139 |
flag = true; |
| 140 |
} |
| 141 |
break; |
| 142 |
} |
| 143 |
if( !flag ) |
| 144 |
{ |
| 145 |
return ZErrorMessageReadingFile(filename, |
| 146 |
ZErrorMessage(ZErrorPeakPositionsAreNotContained, "There is no headers for peak positions", __FILE__, __LINE__, __FUNCTION__)); |
| 147 |
} |
| 148 |
|
| 149 |
while (getnewline(ifs, s) == ZErrorNoError) |
| 150 |
{ |
| 151 |
if(is_blank(s)) continue; |
| 152 |
if(s.substr(0,5) == "BEGIN") continue; |
| 153 |
if(s.substr(0,3) == "END" ) break; |
| 154 |
|
| 155 |
istringstream iss(s); |
| 156 |
|
| 157 |
iss >> count; |
| 158 |
if( iss.fail() ) |
| 159 |
{ |
| 160 |
return ZErrorMessageReadingFile(filename, |
| 161 |
ZErrorMessage(ZErrorFileFormatBroken, __FILE__, __LINE__, __FUNCTION__)); |
| 162 |
} |
| 163 |
|
| 164 |
iss >> t; |
| 165 |
if( iss.fail() ) |
| 166 |
{ |
| 167 |
return ZErrorMessageReadingFile(filename, |
| 168 |
ZErrorMessage(ZErrorFileFormatBroken, __FILE__, __LINE__, __FUNCTION__)); |
| 169 |
} |
| 170 |
PeakPosX.push_back(t); |
| 171 |
|
| 172 |
iss >> t; |
| 173 |
if( iss.fail() ) |
| 174 |
{ |
| 175 |
return ZErrorMessageReadingFile(filename, |
| 176 |
ZErrorMessage(ZErrorFileFormatBroken, __FILE__, __LINE__, __FUNCTION__)); |
| 177 |
} |
| 178 |
PeakPosY.push_back(t); |
| 179 |
|
| 180 |
iss >> t; |
| 181 |
if( iss.fail() ) |
| 182 |
{ |
| 183 |
return ZErrorMessageReadingFile(filename, |
| 184 |
ZErrorMessage(ZErrorFileFormatBroken, __FILE__, __LINE__, __FUNCTION__)); |
| 185 |
} |
| 186 |
PeakWidth.push_back(t); |
| 187 |
|
| 188 |
iss >> flag; |
| 189 |
if( iss.fail() ) |
| 190 |
{ |
| 191 |
return ZErrorMessageReadingFile(filename, |
| 192 |
ZErrorMessage(ZErrorFileFormatBroken, __FILE__, __LINE__, __FUNCTION__)); |
| 193 |
} |
| 194 |
toUseFlag.push_back(flag); |
| 195 |
|
| 196 |
if( *(PeakWidth.rbegin()) <= 0.0 ) |
| 197 |
{ |
| 198 |
*( PeakWidth.rbegin() ) = 0; |
| 199 |
*( toUseFlag.rbegin() ) = false; |
| 200 |
ZLOG_WARN( "Peak position with non-positive FWHM is ignored : " |
| 201 |
+ num2str( *( PeakPosX.rbegin() ) ) + " " |
| 202 |
+ num2str( *( PeakPosY.rbegin() ) ) + " " |
| 203 |
+ num2str( *( PeakWidth.rbegin() ) ) + " " |
| 204 |
+ num2str( *( toUseFlag.rbegin() ) ) + "\n" ); |
| 205 |
} |
| 206 |
} |
| 207 |
if( PeakPosX.empty() ) |
| 208 |
{ |
| 209 |
return ZErrorMessageReadingFile(filename, |
| 210 |
ZErrorMessage(ZErrorPeakPositionsAreNotContained, "There is no data for peak positions", __FILE__, __LINE__, __FUNCTION__)); |
| 211 |
} |
| 212 |
} |
| 213 |
catch(const ZErrorType& num) |
| 214 |
{ |
| 215 |
if(num == ZErrorErrorsAreNotContained) |
| 216 |
{ |
| 217 |
return ZErrorMessageReadingFile(filename, ZErrorMessage(ZErrorErrorsAreNotContained, "The number of columns is less than "+ num2str(NumContents), __FILE__, __LINE__, __FUNCTION__)); |
| 218 |
} |
| 219 |
else // num == ZErrorNotIGORFile |
| 220 |
{ |
| 221 |
return ZErrorMessageReadingFile(filename, ZErrorMessage(ZErrorNotIGORFile, "Not Igor File", __FILE__, __LINE__, __FUNCTION__)); |
| 222 |
} |
| 223 |
} |
| 224 |
catch(const Int4& num) |
| 225 |
{ |
| 226 |
if(num == 1) |
| 227 |
{ |
| 228 |
return ZErrorMessageReadingFile(filename, |
| 229 |
ZErrorMessage("The number of columns is less than "+num2str<Int4>(NumContents), __FILE__, __LINE__, __FUNCTION__)); |
| 230 |
} |
| 231 |
else // num == 3. |
| 232 |
{ |
| 233 |
return ZErrorMessageReadingFile(filename, |
| 234 |
ZErrorMessage("Not Igor File", __FILE__, __LINE__, __FUNCTION__)); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return ZErrorMessageReadingFile(); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
void PeakPosData::printData(ostream *os) const |
| 244 |
{ |
| 245 |
*os << m_XWave_title << ", " << m_YInt_title << endl; |
| 246 |
*os << "BEGIN" << endl; |
| 247 |
os->setf(ios::right); |
| 248 |
os->setf(ios::uppercase); |
| 249 |
os->setf(ios::showpoint); |
| 250 |
|
| 251 |
os->precision(6); |
| 252 |
for (UInt4 i=0; i<m_XWave.size(); i++) |
| 253 |
{ |
| 254 |
os->unsetf(ios::scientific); |
| 255 |
os->width(10); |
| 256 |
*os << m_XWave[i]; |
| 257 |
|
| 258 |
os->setf(ios::scientific); |
| 259 |
os->width(15); |
| 260 |
*os << m_YInt[i]; |
| 261 |
|
| 262 |
// os->width(15); |
| 263 |
// *os << m_YErr[i]; |
| 264 |
*os << endl; |
| 265 |
} |
| 266 |
*os << "END" << endl; |
| 267 |
|
| 268 |
*os << "WAVES/O peak, peakpos, height, FWHM, Flag" << endl; |
| 269 |
*os << "BEGIN" << endl; |
| 270 |
for (UInt4 j = 0; j < PeakPosX.size(); j++) |
| 271 |
{ |
| 272 |
// OUTPUT |
| 273 |
os->precision(); |
| 274 |
os->width(5); |
| 275 |
*os << j + 1; |
| 276 |
|
| 277 |
os->width(15); |
| 278 |
*os << PeakPosX[j]; |
| 279 |
os->width(15); |
| 280 |
*os << PeakPosY[j]; |
| 281 |
os->width(15); |
| 282 |
*os << PeakWidth[j]; |
| 283 |
|
| 284 |
os->precision(); |
| 285 |
os->width(5); |
| 286 |
*os << toUseFlag[j] << endl; |
| 287 |
} |
| 288 |
*os << "END" << endl; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
Double PeakPosData::putAveragePeakHeightOfFirst20() const |
| 293 |
{ |
| 294 |
Double ans = 0.0; |
| 295 |
const Int4 iend = min(20, (Int4)PeakPosX.size()); |
| 296 |
if( iend <= 0 ) return ans; |
| 297 |
|
| 298 |
for(Int4 i=0; i<iend; i++) |
| 299 |
{ |
| 300 |
ans += PeakPosY[i]; |
| 301 |
} |
| 302 |
return ans / iend; |
| 303 |
} |