cphot 0.1
A C++ tool for computing photometry from spectra.
io.hpp
Go to the documentation of this file.
1 /**
2  * @defgroup IO Input/Output
3  * @brief Tools to read and write filter libraries
4  * @version 0.1
5  *
6  */
7 #pragma once
8 #include "filter.hpp"
9 #include "votable.hpp"
10 #include "rquantities.hpp"
11 #include <cpr/cpr.h>
12 
13 
14 namespace cphot {
15 
16 /**
17  * @ingroup IO
18  * @brief Get the filter object from VOTable object
19  *
20  * @param vot votable document
21  * @return Filter object
22  */
24  // Extract name
25  std::string filter_name = std::regex_replace(
26  vot.params["filterID"].value,
27  std::regex("/"),
28  "_"
29  );
30  //extract detector type
31  std::string detector_type = (parseString<int>(vot.params["DetectorType"].value) == 0) ?
32  "energy" : "photon";
33 
34  // extract wavelength and units
35  const std::vector<std::string> AA_str {"Angstrom", "AA", "angstrom"};
36  const std::vector<std::string> nm_str {"Nanometer", "nanometer", "nm"};
37  const auto & wave = vot.get<double>("Wavelength");
38  QLength wavelength_unit;
39  if (contains(AA_str, wave.unit)) { wavelength_unit = angstrom;}
40  else if (contains(nm_str, wave.unit)) { wavelength_unit = nm;}
41 
42  // extract transmission
43  const auto & transmit = vot.get<double>("Transmission");
44 
45  // convert to Filter inputs
46  std::vector<std::size_t> shape = { wave.data.size() };
47  DMatrix xt_wave = xt::adapt(wave.data, shape);
48  DMatrix xt_transmit = xt::adapt(transmit.data, shape);
49 
50  return Filter(xt_wave, xt_transmit,
51  wavelength_unit, detector_type,
52  filter_name);
53 }
54 
55 /**
56  * @ingroup IO
57  * @brief Get the filter object from a VOTable file
58  *
59  * @param vot_filename path to the xml file
60  * @return Filter object
61  */
62 Filter get_filter(const std::string& vot_filename){
63  // Read VOTable
64  votable::VOTable vot(vot_filename);
65  return get_filter(vot);
66 }
67 
68 /**
69  * @ingroup IO
70  * @brief main interface to SVO data requests
71  *
72  * Query the <a link=http://svo2.cab.inta-csic.es/theory/fps/>SVO filter profile
73  * service</a> and return the filter object
74  * (http://svo2.cab.inta-csic.es/theory/fps)
75  *
76  * @param id passband id
77  * @return std::string data content
78  */
79 Filter download_svo_filter(const std::string & id){
80  cpr::Response r = cpr::Get(cpr::Url{"http://svo2.cab.inta-csic.es/theory/fps/fps.php"},
81  cpr::Parameters{{"ID", id}});
82  votable::VOTable vot;
83  vot.from_content(r.text);
84  return get_filter(vot);
85 }
86 
87 /**
88  * @ingroup IO
89  * @brief Download the filter library from pyphot repository
90  *
91  * The url to pyphot's file is hard-coded in the function for:
92  * "https://raw.githubusercontent.com/mfouesneau/pyphot/master/pyphot/libs/new_filters.hd5"
93  *
94  * @param where where to store the hdf5 file
95  * @return std::string path to the hdf5 file
96  */
97 std::string download_pyphot_hdf5library(const std::string & where="./pyphot_library.hdf5"){
98  const std::string pyphot_url = "https://raw.githubusercontent.com/mfouesneau/pyphot/master/pyphot/libs/new_filters.hd5";
99  cpr::Response r = cpr::Get(cpr::Url{pyphot_url});
100  std::ofstream libout(where);
101  libout << r.text;
102  libout.close();
103  return where;
104 }
105 
106 }; // namespace cphot
contains
bool contains(const std::vector< std::string > &v, const std::string &other)
Check if a vector contains an item.
Definition: helpers.hpp:45
filter.hpp
votable::VOTable
Class defining a very simple parser of XML VOTable.
Definition: votable.hpp:162
rquantities.hpp
cphot::DMatrix
xt::xarray< double, xt::layout_type::row_major > DMatrix
Definition: filter.hpp:32
cphot::Filter
Unit Aware Filter. input spectra and output values have units to avoid mis-interpretation.
Definition: filter.hpp:43
angstrom
constexpr QLength angstrom
Definition: rquantities.hpp:274
cphot::download_pyphot_hdf5library
std::string download_pyphot_hdf5library(const std::string &where="./pyphot_library.hdf5")
Download the filter library from pyphot repository.
Definition: io.hpp:97
cphot::download_svo_filter
Filter download_svo_filter(const std::string &id)
main interface to SVO data requests
Definition: io.hpp:79
cphot
Definition: filter.hpp:30
cphot::get_filter
Filter get_filter(votable::VOTable &vot)
Get the filter object from VOTable object.
Definition: io.hpp:23
nm
constexpr QLength nm
Definition: rquantities.hpp:273
cphot_sun_theoretical::wavelength_unit
const QLength wavelength_unit
Definition: sun_data.hpp:19
votable::VOTable::params
std::map< std::string, Param > params
Table parameters.
Definition: votable.hpp:165
votable::VOTable::from_content
void from_content(const std::string &content)
Constructor.
Definition: votable.hpp:208
votable::VOTable::get
VOField< T > get(std::string field_name)
Retrieve field data.
Definition: votable.hpp:330
votable.hpp