Commit e67b4e7d6f96a1d837fbcd9e8e5825443b107a00

Authored by Domenico Tiziani
1 parent adc6fa6c

Add output support for csv format, change default to csv

include/GObservations.hpp
... ... @@ -158,7 +158,7 @@ public:
158 158 double npred(void) const;
159 159 GMatrixSparse hessian(const GOptimizerPars& pars);
160 160 GMatrixSparse covariance(void) const;
161   - void save(const GFilename& filename) const;
  161 + void save(const GFilename& filename, bool csv = true) const;
162 162  
163 163 protected:
164 164 // Protected methods
... ...
src/obs/GObservations_likelihood.cpp
... ... @@ -38,6 +38,7 @@
38 38 #include "GFitsBinTable.hpp"
39 39 #include "GFitsTableStringCol.hpp"
40 40 #include "GFitsTableDoubleCol.hpp"
  41 +#include "GCsv.hpp"
41 42  
42 43 /* __ OpenMP section _____________________________________________________ */
43 44 #ifdef _OPENMP
... ... @@ -627,7 +628,7 @@ GMatrixSparse GObservations::likelihood::covariance(void) const
627 628 * Saves the likelihood fit results into a FITS file. For the moment the
628 629 * method only writes the covariance matrix.
629 630 ***************************************************************************/
630   -void GObservations::likelihood::save(const GFilename& filename) const
  631 +void GObservations::likelihood::save(const GFilename& filename, bool csv) const
631 632 {
632 633 // Get covariance matrix
633 634 GMatrixSparse covmat = covariance();
... ... @@ -643,44 +644,65 @@ void GObservations::likelihood::save(const GFilename& filename) const
643 644  
644 645 // Create binary table and columns
645 646 int size = covmat_entries.size();
646   - GFitsBinTable covmat_table;
647   - GFitsTableStringCol par("Parameters", 1, 50, size);
648   - GFitsTableDoubleCol cov("Covariance", 1, size*size);
649   -
650   - // Fill tables
651   - int counter = 0;
652   - for (int i = 0; i < size; ++i) {
653   - par(0, i) = covmat_entries[i];
654   - for (int j = 0; j < size; ++j) {
655   - cov(0, counter) = covmat(i,j);
656   - ++counter;
  647 +
  648 + if (csv) {
  649 + GCsv table(size + 1, size);
  650 +
  651 + // Fill CSV header
  652 + for (int i = 0; i < size; ++i) {
  653 + table.string(0, i, covmat_entries[i]);
  654 + }
  655 +
  656 + // Fill CSV table
  657 + for (int i = 0; i < size; ++i) {
  658 + for (int j = 0; j < size; j ++) {
  659 + table.real(i+1, j, covmat(i,j));
  660 + }
657 661 }
658   - }
659 662  
660   - // Set dimension for covariance matrix column
661   - std::vector<int> dim;
662   - dim.push_back(size);
663   - dim.push_back(size);
664   - cov.dim(dim);
  663 + // Save CSV table
  664 + table.save(filename, " ", true);
  665 + } else {
  666 +
  667 + GFitsBinTable covmat_table;
  668 + GFitsTableStringCol par("Parameters", 1, 50, size);
  669 + GFitsTableDoubleCol cov("Covariance", 1, size*size);
  670 +
  671 + // Fill tables
  672 + int counter = 0;
  673 + for (int i = 0; i < size; ++i) {
  674 + par(0, i) = covmat_entries[i];
  675 + for (int j = 0; j < size; ++j) {
  676 + cov(0, counter) = covmat(i,j);
  677 + ++counter;
  678 + }
  679 + }
665 680  
666   - // Append columns to table
667   - covmat_table.append(par);
668   - covmat_table.append(cov);
  681 + // Set dimension for covariance matrix column
  682 + std::vector<int> dim;
  683 + dim.push_back(size);
  684 + dim.push_back(size);
  685 + cov.dim(dim);
669 686  
670   - // Set extension name
671   - covmat_table.extname("Covariance Matrix");
  687 + // Append columns to table
  688 + covmat_table.append(par);
  689 + covmat_table.append(cov);
672 690  
673   - // Allocate FITS object
674   - GFits fits;
  691 + // Set extension name
  692 + covmat_table.extname("Covariance Matrix");
675 693  
676   - // Append covariance matrix table to FITS object
677   - fits.append(covmat_table);
  694 + // Allocate FITS object
  695 + GFits fits;
678 696  
679   - // Save FITS file to disk
680   - fits.saveto(filename, true);
  697 + // Append covariance matrix table to FITS object
  698 + fits.append(covmat_table);
681 699  
682   - // Close FITS object
683   - fits.close();
  700 + // Save FITS file to disk
  701 + fits.saveto(filename, true);
  702 +
  703 + // Close FITS object
  704 + fits.close();
  705 + }
684 706  
685 707 // Return
686 708 return;
... ...