CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
FITSwrapper.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful,*
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  *******************************************************************/
20 
34 #include "FITSwrapper.h"
35 #include "CCfits/CCfits"
36 
37 using namespace std;
38 
39 using namespace cbl;
40 
41 
42 // ============================================================================
43 
44 
45 vector<vector<double>> cbl::wrapper::ccfits::read_table_fits (const std::string input_fits, const std::vector<std::string> column_names, const int next, const double fill_value)
46 {
47  ifstream check_input(input_fits); checkIO(check_input, input_fits); check_input.close();
48 
49  unique_ptr<CCfits::FITS> pInfile(new CCfits::FITS(input_fits, CCfits::Read, next));
50 
51  CCfits::ExtHDU& table = pInfile->currentExtension();
52 
53  const long nrows = table.rows();
54  size_t no_col = 0;
55 
56  if (nrows==0)
57  ErrorCBL("no rows in the selected table extension!", "read_table_fits", "FITSwrapper.cpp");
58 
59  vector<vector<double>> cc;
60 
61  for (size_t i=0; i<column_names.size(); i++) {
62  vector<double> vv;
63  try {
64  table.column(column_names[i]).read(vv, 0, nrows);
65  }
66  catch (CCfits::Table::NoSuchColumn) {
67  if (fill_value==par::defaultDouble)
68  ErrorCBL("no column "+column_names[i]+"!", "read_table_fits", "FITSwrapper.cpp");
69  else {
70  vv.erase(vv.begin(), vv.end());
71  vv.resize(nrows, fill_value);
72  no_col ++;
73  }
74  }
75  cc.push_back(vv);
76  }
77 
78  if (no_col==column_names.size())
79  ErrorCBL("no column found!", "read_table_fits", "FITSwrapper.cpp");
80 
81  return cc;
82 }
83 
84 
85 // ============================================================================
86 
87 
88 void cbl::wrapper::ccfits::write_table_fits (const std::string output_dir, const std::string file_fits, const std::vector<std::string> column_names, const std::vector<std::vector<double>> table, const std::vector<std::string> column_units)
89 {
90  const string file_name = output_dir+file_fits;
91 
92  if (system(("rm -rf "+file_name).c_str())) {}
93 
94  shared_ptr<CCfits::FITS> pFits(0);
95 
96  try
97  {
98  pFits.reset( new CCfits::FITS(file_name, CCfits::Write) );
99  }
100  catch(CCfits::FITS::CantOpen)
101  {
102  ErrorCBL("the file "+file_name+" cannot be opened!", "write_table_fits", "FITSwrapper.cpp");
103  }
104 
105  const size_t ncolumns = column_names.size();
106  size_t rows = table[0].size();
107  string hduName = "TABLE_BINARY";
108 
109  vector<string> colName = column_names;
110  vector<string> colUnit = (column_units.size()==0) ? vector<string>(ncolumns, "") : column_units;
111  vector<string> colForm(ncolumns, "1D");
112 
113  CCfits::Table* newTable = pFits->addTable(hduName, rows, colName, colForm, colUnit);
114 
115  // numbers is a scalar column
116  for (size_t i=0; i<ncolumns; i++)
117  newTable->column(colName[i]).write(table[i], 1);
118 
119  coutCBL << "I wrote the file: " << file_name << endl;
120 }
class FITSwrapper that wrap CCfits routines to manage FITS files
#define coutCBL
CBL print message.
Definition: Kernel.h:734
static const double defaultDouble
default double value
Definition: Constants.h:348
static const double cc
: the speed of light in vacuum (the value is exact) [km/sec]
Definition: Constants.h:221
std::vector< std::vector< double > > read_table_fits(const std::string input_fits, const std::vector< std::string > column_names, const int next=1, const double fill_value=cbl::par::defaultDouble)
function to read a table from a fits file
Definition: FITSwrapper.cpp:45
void write_table_fits(const std::string output_dir, const std::string file_fits, const std::vector< std::string > column_names, const std::vector< std::vector< double >> table, const std::vector< std::string > column_units={})
function that writes a table on a fits file
Definition: FITSwrapper.cpp:88
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780