CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
FITSCatalogue.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2018 by Federico Marulli and Alfonso Veropalumbo *
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 
35 #include "FITSwrapper.h"
36 #include "Catalogue.h"
37 
38 using namespace std;
39 
40 using namespace cbl;
41 
42 
43 // ============================================================================
44 
45 
46 cbl::catalogue::Catalogue::Catalogue (const ObjectType objectType, const CoordinateType coordinateType, const std::vector<std::string> file, const std::vector<std::string> column_names, const bool read_weights, const bool read_regions, const double nSub, const double fact, const cosmology::Cosmology &cosm, const CoordinateUnits inputUnits, const int seed)
47 {
48  // parameters for random numbers used in case nSub!=1
49  random::UniformRandomNumbers ran(0., 1., seed);
50 
51 
52  // read the input catalogue files
53 
54  for (size_t dd=0; dd<file.size(); ++dd) {
55 
56  coutCBL << "Reading the catalogue: " << file[dd] << endl;
57 
58  // read the columns from the table searching by names
59  vector<vector<double>> table = wrapper::ccfits::read_table_fits(file[dd], column_names, 1, 1.);
60 
61  if (((read_weights || read_regions) && table.size()<3) || ((read_weights && read_regions) && table.size()<4))
62  ErrorCBL("the number of columns in the input FITS file is wrong!", "Catalogue", "FITSCatalogue.cpp");
63 
64  // include the objects in the catalogue
65 
66  for (size_t i=0; i<table[0].size(); ++i) {
67 
68  if (ran()<nSub) { // extract a subsample
69 
70  if (coordinateType==CoordinateType::_comoving_) { // comoving coordinates (x, y, z)
71  comovingCoordinates coord = {table[0][i]*fact, table[1][i]*fact, table[2][i]*fact};
72  m_object.push_back(move(Object::Create(objectType, coord, (read_weights) ? table[3][i] : 1., (read_regions) ? (long)table[(read_weights) ? 4 : 3][i] : 1)));
73  }
74 
75  else if (coordinateType==CoordinateType::_observed_) { // observed coordinates (R.A., Dec, redshift)
76  if (table[2][i]*fact>0) {
77  observedCoordinates coord = {table[0][i]*fact, table[1][i]*fact, table[2][i]*fact};
78  m_object.push_back(move(Object::Create(objectType, coord, inputUnits, cosm, (read_weights) ? table[3][i] : 1., (read_regions) ? (long)table[(read_weights) ? 4 : 3][i] : 1)));
79  }
80  else WarningMsgCBL("the object "+conv(i, par::fINT)+" has z = "+conv(table[2][i]*fact, par::fDP2)+", and it will be not included in the catalogue!", "Catalogue", "FITSCatalogue.cpp");
81  }
82 
83  else ErrorCBL("coordinateType is not valid!", "Catalogue", "FITSCatalogue.cpp");
84 
85  }
86 
87  }
88  }
89 }
90 
91 // ============================================================================
92 
93 
94 cbl::catalogue::Catalogue::Catalogue (const ObjectType objectType, const CoordinateType coordinateType, const std::vector<std::string> file, const std::vector<std::string> column_names, const std::vector<Var> attribute, const double nSub, const double fact, const cosmology::Cosmology &cosm, const CoordinateUnits inputUnits, const int seed)
95 {
96  // preliminary check on vector sizes
97  size_t nvar;
98  if (attribute.size()==column_names.size()) nvar = attribute.size();
99  else ErrorCBL("Column_names vector and attribute vector must have equal size!", "Catalogue", "FITSCatalogue.cpp");
100 
101  const int num_threads = (nvar>size_t(omp_get_max_threads())) ? omp_get_max_threads() : nvar;
102 
103  unordered_map<int, Var> varMap;
104  for (size_t ii=0; ii<nvar; ii++)
105  varMap.insert({ii, attribute[ii]});
106 
107  // parameters for random numbers used in case nSub!=1
108  random::UniformRandomNumbers ran(0., 1., seed);
109 
110  // read the input catalogue files
111 
112  for (size_t dd=0; dd<file.size(); ++dd) {
113 
114  coutCBL << "Reading the catalogue: " << file[dd] << endl;
115 
116  // read the columns from the table searching by names
117  vector<vector<double>> table = wrapper::ccfits::read_table_fits(file[dd], column_names, 1, 1.);
118 
119  // prepare default coordinates
120  comovingCoordinates defaultComovingCoord = {par::defaultDouble, par::defaultDouble, par::defaultDouble};
121  observedCoordinates defaultObservedCoord = {par::defaultDouble, -1., 0.1};
122 
123  // include the objects in the catalogue
124 
125  for (size_t i=0; i<table[0].size(); ++i) {
126 
127  size_t prev_nObj = nObjects();
128 
129  if (ran()<nSub) { // extract a subsample
130 
131  if (coordinateType==cbl::CoordinateType::_comoving_)
132  m_object.push_back(move(Object::Create(objectType, defaultComovingCoord, 1.)));
133 
134  else if (coordinateType==cbl::CoordinateType::_observed_)
135  m_object.push_back(move(Object::Create(objectType, defaultObservedCoord, inputUnits, cosm, 1.)));
136 
137 
138 #pragma omp parallel num_threads(num_threads)
139  {
140 #pragma omp for schedule(dynamic)
141  for (size_t i=0; i<nvar; ++i) {
142 
143  for (size_t ii=prev_nObj; ii<nObjects(); ii++) {
144  double temp = ((varMap[i]==Var::_RA_) || (varMap[i]==Var::_Dec_)) ? radians(table[i][ii], inputUnits) : table[i][ii];
145  set_var(ii, varMap[i], ((varMap[i]==Var::_X_) || (varMap[i]==Var::_Y_) || (varMap[i]==Var::_Z_)) ? temp*fact : temp, cosm);
146  }
147 
148  }
149  }
150 
151 
152  }
153  }
154  }
155 }
The class Catalogue
class FITSwrapper that wrap CCfits routines to manage FITS files
#define coutCBL
CBL print message.
Definition: Kernel.h:734
Catalogue()=default
default constructor
The class Cosmology.
Definition: Cosmology.h:277
The class UniformRandomNumbers.
static const char fDP2[]
conversion symbol for: double -> std::string
Definition: Constants.h:133
static const char fINT[]
conversion symbol for: int -> std::string
Definition: Constants.h:121
static const double defaultDouble
default double value
Definition: Constants.h:348
ObjectType
the object types
Definition: Object.h:51
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
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
CoordinateType
the coordinate type
Definition: Kernel.h:624
@ _observed_
observed coordinates (R.A., Dec, redshift)
@ _comoving_
comoving coordinates (x, y, z)
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
double radians(const double angle, const CoordinateUnits inputUnits=CoordinateUnits::_degrees_)
conversion to radians
Definition: Func.cpp:126
CoordinateUnits
the coordinate units
Definition: Kernel.h:562
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message
Definition: Kernel.h:747