Commit 3d17138027cee19fe3591ab56be3b2192566fcf3
1 parent
9d50309d
Use new GCsv table constructor, set methods and save method.
Showing
1 changed file
with
11 additions
and
32 deletions
src/ctbutterfly/ctbutterfly.cpp
| ... | ... | @@ -353,12 +353,10 @@ void ctbutterfly::run(void) |
| 353 | 353 | |
| 354 | 354 | |
| 355 | 355 | /***********************************************************************//** |
| 356 | - * @brief Save butterfly | |
| 356 | + * @brief Save butterfly diagram | |
| 357 | 357 | * |
| 358 | - * @exception GException::invalid_value | |
| 359 | - * File exists already and clobber is false | |
| 360 | - * | |
| 361 | - * This method saves the butterfly into an ascii file | |
| 358 | + * Saves the butterfly diagram into an ascii file using a column separated | |
| 359 | + * value (CSV) format with blanks as separators. | |
| 362 | 360 | ***************************************************************************/ |
| 363 | 361 | void ctbutterfly::save(void) |
| 364 | 362 | { |
| ... | ... | @@ -371,37 +369,18 @@ void ctbutterfly::save(void) |
| 371 | 369 | // Get output filename |
| 372 | 370 | m_outfile = (*this)["outfile"].filename(); |
| 373 | 371 | |
| 374 | - // If file exists already then delete it in case the clobber parameter | |
| 375 | - // is true | |
| 376 | - if (gammalib::file_exists(m_outfile)) { | |
| 377 | - if ((*this)["clobber"].boolean()) { | |
| 378 | - std::remove(m_outfile.c_str()); | |
| 379 | - } | |
| 380 | - else { | |
| 381 | - std::string msg = "Output file \""+m_outfile+"\" already exists " | |
| 382 | - "and clobber parameters is set to \"false\"."; | |
| 383 | - throw GException::invalid_value(G_SAVE, msg); | |
| 384 | - } | |
| 385 | - } | |
| 386 | - | |
| 387 | - // Create file instance | |
| 388 | - std::fstream file; | |
| 389 | - | |
| 390 | - // Open file | |
| 391 | - file.open(m_outfile.c_str(), std::fstream::out | std::fstream::app); | |
| 392 | - | |
| 393 | - // Write short header | |
| 394 | - file << "# Energy [MeV] | flux [ph/cm2/s/MeV] | e_flux [ph/cm2/s/MeV]"; | |
| 395 | - file << std::endl; | |
| 372 | + // Create CSV table with 3 columns | |
| 373 | + GCsv table(m_fluxes.size(), 3); | |
| 396 | 374 | |
| 397 | - // Write values to file | |
| 375 | + // Fill CSV table | |
| 398 | 376 | for (int i = 0; i < m_fluxes.size(); ++i) { |
| 399 | - file << m_energies[i] << " " << m_fluxes[i] << " " << m_errors[i]; | |
| 400 | - file << std::endl; | |
| 377 | + table.real(i, 0, m_energies[i]); | |
| 378 | + table.real(i, 1, m_fluxes[i]); | |
| 379 | + table.real(i, 2, m_errors[i]); | |
| 401 | 380 | } |
| 402 | 381 | |
| 403 | - // Close file | |
| 404 | - file.close(); | |
| 382 | + // Save CSV table | |
| 383 | + table.save(m_outfile, " ", clobber()); | |
| 405 | 384 | |
| 406 | 385 | // Return |
| 407 | 386 | return; | ... | ... |