GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GTools.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GTools.hpp - GammaLib tools *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2008-2024 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GTools.hpp
23 * @brief Gammalib tools definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GTOOLS_HPP
28#define GTOOLS_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <vector>
32#include <string>
33#include <complex>
34#include <cmath>
35#include <cfloat>
36
37/* __ Forward declarations _______________________________________________ */
38class GEnergy;
39class GFilename;
40class GXmlElement;
41class GXmlNode;
42
43/* __ Constants __________________________________________________________ */
44namespace gammalib {
45 const double MeV2erg = 1.6021765e-6; // MeV => erg
46 const double erg2MeV = 624150.96; // erg => MeV
47 const double MeV2Angstrom = 1.239841875e-2; // MeV => Angstrom
48 const double pc2cm = 3.08568025e18; // pc => cm
49 const double sec_in_day = 86400.0; // seconds per day
50 const double sec2day = 1.0 / sec_in_day; // seconds => days
51 const double tai2tt = 32.184; // TAI => TT time system
52 const double mec2 = 0.5109989461; // Electron rest mass (MeV)
53 const double speed_of_light = 299792458.0; // Vacuum speed of light (m/s)
54}
55
56/* __ Prototypes ________________________________________________________ */
57namespace gammalib {
58 std::string strip_whitespace(const std::string& arg);
59 std::string strip_chars(const std::string& arg,
60 const std::string& chars);
61 std::string rstrip_chars(const std::string& arg,
62 const std::string& chars);
63 std::string replace_segment(const std::string& arg,
64 const std::string& segment,
65 const std::string& replacement);
66 std::string expand_env(const std::string& arg);
67 std::string filepath(const std::string& pathname,
68 const std::string& filename);
69 std::string tmpnam(void);
70 std::string getenv(const std::string& arg);
71 std::string str(const unsigned short int& value);
72 std::string str(const unsigned int& value);
73 std::string str(const unsigned long int& value);
74 std::string str(const unsigned long long int& value);
75 std::string str(const short int& value);
76 std::string str(const int& value, const std::string& fmt = "%d");
77 std::string str(const long int& value);
78 std::string str(const long long int& value);
79 std::string str(const float& value, const int& precision = 0);
80 std::string str(const double& value, const int& precision = 0);
81 std::string str(const std::complex<double>& value,
82 const int& precision = 0);
83 std::string strdate(void);
84 char* tochar(const std::string& arg);
85 short toshort(const std::string& arg);
86 unsigned short toushort(const std::string& arg);
87 int toint(const std::string& arg);
88 unsigned int touint(const std::string& arg);
89 long tolong(const std::string& arg);
90 unsigned long toulong(const std::string& arg);
91 long long tolonglong(const std::string& arg);
92 unsigned long long toulonglong(const std::string& arg);
93 float tofloat(const std::string& arg);
94 double todouble(const std::string& arg);
95 std::string toupper(const std::string& s);
96 std::string tolower(const std::string& s);
97 std::vector<std::string> split(const std::string& s, const std::string& sep);
98 std::string fill(const std::string& s, const int& n);
99 std::string left(const std::string& s, const int& n,
100 const char& c = ' ');
101 std::string right(const std::string& s, const int& n,
102 const char& c = ' ');
103 std::string centre(const std::string& s, const int& n,
104 const char& c = ' ');
105 std::string parformat(const std::string& s, const int& indent = 0);
106 std::string number(const std::string& noun, const int& number);
107 double plaw_photon_flux(const double& emin,
108 const double& emax,
109 const double& epivot,
110 const double& gamma);
111 double plaw_energy_flux(const double& emin,
112 const double& emax,
113 const double& epivot,
114 const double& gamma);
115 GEnergy elogmean(const GEnergy& a, const GEnergy& b);
116 bool dir_exists(const std::string& dirname);
117 bool is_infinite(const double& x);
118 bool is_notanumber(const double& x);
119 bool contains(const std::string& str,
120 const std::string& substring);
121 bool contains(const std::vector<std::string>& strings,
122 const std::string& string);
123 void warning(const std::string& origin,
124 const std::string& message);
125 std::string xml2str(const std::string& arg);
126 std::string str2xml(const std::string& arg);
127 bool xml_has_par(const GXmlElement& xml,
128 const std::string& name);
129 GXmlElement* xml_need_par(const std::string& origin,
130 GXmlElement& xml,
131 const std::string& name);
132 const GXmlElement* xml_get_par(const std::string& origin,
133 const GXmlElement& xml,
134 const std::string& name);
135 std::string xml_get_attr(const std::string& origin,
136 const GXmlElement& xml,
137 const std::string& name,
138 const std::string& attribute);
139 void xml_check_parnum(const std::string& origin,
140 const GXmlElement& xml,
141 const int& number);
142 void xml_check_type(const std::string& origin,
143 GXmlElement& xml,
144 const std::string& type);
145 void xml_check_par(const std::string& origin,
146 const std::string& name,
147 const int& number);
149 const std::string& filename);
151 const std::string& filename);
152 void xml_get_name_value_pair(const GXmlNode* node,
153 std::string& name,
154 std::string& value);
155 int recv(int fd, char *buffer, int len, int flags,
156 int timeout);
157 double roi_arclength(const double& rad,
158 const double& dist,
159 const double& cosdist,
160 const double& sindist,
161 const double& roi,
162 const double& cosroi);
163 bool compare(const double& a,
164 const double& b,
165 const double& tol);
166 std::string http_query(const std::string& host,
167 const std::string& query);
168 std::string host_country(const bool& force_query = false);
169 GFilename gamma_filename(const std::string& name);
170 size_t get_current_rss(void);
171 double get_current_clock(void);
172}
173
174
175/***********************************************************************//**
176 * @brief Signal if argument is infinite
177 *
178 * @param[in] x Argument.
179 * @return True if argument @p x is infinite, false otherwise.
180 *
181 * Signals if the argument @p x is infinite.
182 *
183 * This function has been copied from gnulib.
184 ***************************************************************************/
185inline
186bool gammalib::is_infinite(const double& x)
187{
188 return (x < -DBL_MAX || x > DBL_MAX);
189}
190
191
192/***********************************************************************//**
193 * @brief Signal if argument is not a number
194 *
195 * @param[in] x Argument.
196 * @return True if argument @p x is not a number, false otherwise.
197 *
198 * Signals if the argument @p x is not a number.
199 *
200 * This function is a very simple kluge. It may not work on all systems.
201 ***************************************************************************/
202inline
203bool gammalib::is_notanumber(const double& x)
204{
205 return (x != x);
206}
207
208#endif /* GTOOLS_HPP */
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
Filename class.
Definition GFilename.hpp:62
XML element node class.
Abstract XML node base class.
Definition GXmlNode.hpp:57
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1162
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition GTools.cpp:1186
GFilename gamma_filename(const std::string &name)
Returns filename in .gamma directory.
Definition GTools.cpp:2503
bool is_infinite(const double &x)
Signal if argument is infinite.
Definition GTools.hpp:186
double plaw_photon_flux(const double &emin, const double &emax, const double &epivot, const double &gamma)
Compute photon flux between two energies for a power law.
Definition GTools.cpp:1219
std::string strdate(void)
Return current date.
Definition GTools.cpp:746
const double erg2MeV
Definition GTools.hpp:46
const double pc2cm
Definition GTools.hpp:48
std::string http_query(const std::string &host, const std::string &query)
Return response to a HTTP query.
Definition GTools.cpp:2249
bool is_notanumber(const double &x)
Signal if argument is not a number.
Definition GTools.hpp:203
GEnergy elogmean(const GEnergy &a, const GEnergy &b)
Computes log mean energy.
Definition GTools.cpp:1309
char * tochar(const std::string &arg)
Convert string to C string.
Definition GTools.cpp:786
long tolong(const std::string &arg)
Convert string into long value.
Definition GTools.cpp:870
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508
const GXmlElement * xml_get_par(const std::string &origin, const GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition GTools.cpp:1708
double todouble(const std::string &arg)
Convert string into double precision value.
Definition GTools.cpp:945
std::string xml_get_attr(const std::string &origin, const GXmlElement &xml, const std::string &name, const std::string &attribute)
Return attribute value for a given parameter in XML element.
Definition GTools.cpp:1757
std::string left(const std::string &s, const int &n, const char &c=' ')
Left justify string to achieve a length of n characters.
Definition GTools.cpp:1089
bool compare(const double &a, const double &b, const double &tol)
Compare two floating point values with tolerance.
Definition GTools.cpp:2209
std::string right(const std::string &s, const int &n, const char &c=' ')
Right justify string to achieve a length of n characters.
Definition GTools.cpp:1113
std::string replace_segment(const std::string &arg, const std::string &segment, const std::string &replacement)
Replace string segment in string.
Definition GTools.cpp:187
std::string filepath(const std::string &pathname, const std::string &filename)
Build file path from path name and file name.
Definition GTools.cpp:412
std::string tolower(const std::string &s)
Convert string to lower case.
Definition GTools.cpp:974
unsigned long long toulonglong(const std::string &arg)
Convert string into unsigned long long value.
Definition GTools.cpp:915
int toint(const std::string &arg)
Convert string into integer value.
Definition GTools.cpp:840
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition GTools.cpp:1656
std::string rstrip_chars(const std::string &arg, const std::string &chars)
Strip trailing character from string.
Definition GTools.cpp:150
std::string xml2str(const std::string &arg)
Convert XML character references in string to characters.
Definition GTools.cpp:1435
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
Definition GTools.cpp:1965
const double sec2day
Definition GTools.hpp:50
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition GTools.cpp:99
std::string str2xml(const std::string &arg)
Convert special characters in string to XML character references.
Definition GTools.cpp:1568
double roi_arclength(const double &rad, const double &dist, const double &cosdist, const double &sindist, const double &roi, const double &cosroi)
Returns length of circular arc within circular ROI.
Definition GTools.cpp:2126
float tofloat(const std::string &arg)
Convert string into single precision value.
Definition GTools.cpp:930
std::string expand_env(const std::string &arg)
Expand environment variables in string.
Definition GTools.cpp:233
unsigned long toulong(const std::string &arg)
Convert string into unsigned long value.
Definition GTools.cpp:885
const double MeV2Angstrom
Definition GTools.hpp:47
bool dir_exists(const std::string &dirname)
Checks if directory exists.
Definition GTools.cpp:1331
unsigned int touint(const std::string &arg)
Convert string into unsigned integer value.
Definition GTools.cpp:855
bool contains(const std::string &str, const std::string &substring)
Checks if a substring is in a string.
Definition GTools.cpp:1361
std::string host_country(const bool &force_query=false)
Return two-digit host country code.
Definition GTools.cpp:2345
short toshort(const std::string &arg)
Convert string into short value.
Definition GTools.cpp:810
double get_current_clock(void)
Get current clock in seconds.
Definition GTools.cpp:2587
std::string centre(const std::string &s, const int &n, const char &c=' ')
Centre string to achieve a length of n characters.
Definition GTools.cpp:1137
void xml_check_parnum(const std::string &origin, const GXmlElement &xml, const int &number)
Checks number of parameters.
Definition GTools.cpp:1796
std::string tmpnam(void)
Return temporary file name.
Definition GTools.cpp:440
const double tai2tt
Definition GTools.hpp:51
void xml_get_name_value_pair(const GXmlNode *node, std::string &name, std::string &value)
Extract name / value pair from XML node.
Definition GTools.cpp:2007
void warning(const std::string &origin, const std::string &message)
Emits warning.
Definition GTools.cpp:1405
unsigned short toushort(const std::string &arg)
Convert string into unsigned short value.
Definition GTools.cpp:825
const double speed_of_light
Definition GTools.hpp:53
double plaw_energy_flux(const double &emin, const double &emax, const double &epivot, const double &gamma)
Compute energy flux between two energies for a power law.
Definition GTools.cpp:1267
const double mec2
Definition GTools.hpp:52
std::string fill(const std::string &s, const int &n)
Fill string with n strings of same type.
Definition GTools.cpp:1063
const double sec_in_day
Definition GTools.hpp:49
std::string getenv(const std::string &arg)
Return value of environment variable.
Definition GTools.cpp:484
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition GTools.cpp:1615
void xml_check_par(const std::string &origin, const std::string &name, const int &number)
Checks whether a parameter has occured once.
Definition GTools.cpp:1874
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split string.
Definition GTools.cpp:1002
int recv(int fd, char *buffer, int len, int flags, int timeout)
Checks whether a parameter has occured once.
Definition GTools.cpp:2055
std::string toupper(const std::string &s)
Convert string to upper case.
Definition GTools.cpp:960
const double MeV2erg
Definition GTools.hpp:45
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
Definition GTools.cpp:1908
long long tolonglong(const std::string &arg)
Convert string into long long value.
Definition GTools.cpp:900
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition GTools.cpp:1838
std::string strip_chars(const std::string &arg, const std::string &chars)
Strip leading and trailing character from string.
Definition GTools.cpp:113
size_t get_current_rss(void)
Get current resident set size (physical memory use) in Bytes.
Definition GTools.cpp:2540