GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GSkyDir.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GSkyDir.hpp - Sky direction class *
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 GSkyDir.hpp
23 * @brief Sky direction class interface definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GSKYDIR_HPP
28#define GSKYDIR_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <string>
32#include "GBase.hpp"
33#include "GVector.hpp"
34#include "GMath.hpp"
35
36/* __ Forward declarations _______________________________________________ */
37class GTime;
38
39/* __ Compile options ____________________________________________________ */
40#define G_SINCOS_CACHE
41
42/* __ Kluge for compilation on Sun _______________________________________ */
43#ifdef sun
44#undef sun
45#endif
46
47
48/***********************************************************************//**
49 * @class GSkyDir
50 *
51 * @brief Sky direction class
52 *
53 * This class implements a spherical coordinate on the sky. Two coordinate
54 * systems are supported: celestial (or equatorial), defined by Right
55 * Ascension and Declination, and Galactic, defined by galactic longitude
56 * and galactic latitude. The class provides automatic conversion between
57 * both systems if required. Coordinates are stored in either of the
58 * systems (in units of radians), and conversion is performed (and stored)
59 * if requested. Coordinates can be given and returned in radians or in
60 * degrees. Note that the epoch for celestial coordinates is fixed to J2000.
61 ***************************************************************************/
62class GSkyDir : public GBase {
63
64 // Operator friends
65 friend bool operator==(const GSkyDir &a, const GSkyDir &b);
66 friend bool operator!=(const GSkyDir &a, const GSkyDir &b);
67
68public:
69 // Constructors and destructors
70 GSkyDir(void);
71 explicit GSkyDir(const std::string& coord);
72 explicit GSkyDir(const GVector& vector);
73 GSkyDir(const GSkyDir& dir);
74 virtual ~GSkyDir(void);
75
76 // Operators
77 GSkyDir& operator=(const GSkyDir& dir);
78
79 // Methods
80 void clear(void);
81 GSkyDir* clone(void) const;
82 std::string classname(void) const;
83 void radec(const double& ra, const double& dec);
84 void radec_deg(const double& ra, const double& dec);
85 void lb(const double& l, const double& b);
86 void lb_deg(const double& l, const double& b);
87 void set(const std::string& coord);
88 void celvector(const GVector& vector);
89 void galvector(const GVector& vector);
90 void rotate(const double& phi, const double& theta);
91 void rotate_deg(const double& phi, const double& theta);
92 void precess(const double& from_epoch, const double& to_epoch);
93 void sun(const GTime& time, const double& epoch = 2000.0);
94 void moon(const GTime& time, const double& epoch = 2000.0);
95 const double& l(void) const;
96 const double& b(void) const;
97 const double& ra(void) const;
98 const double& dec(void) const;
99 double l_deg(void) const;
100 double b_deg(void) const;
101 double ra_deg(void) const;
102 double dec_deg(void) const;
103 GVector celvector(void) const;
104 GVector galvector(void) const;
105 double cos_dist(const GSkyDir& dir) const;
106 double dist(const GSkyDir& dir) const;
107 double dist_deg(const GSkyDir& dir) const;
108 double posang(const GSkyDir& dir,
109 const std::string& coordsys = "CEL") const;
110 double posang_deg(const GSkyDir& dir,
111 const std::string& coordsys = "CEL") const;
112 std::string print(const GChatter& chatter = NORMAL) const;
113
114private:
115 // Private methods
116 void init_members(void);
117 void copy_members(const GSkyDir& dir);
118 void free_members(void);
119 void equ2gal(void) const;
120 void gal2equ(void) const;
121 void euler(const int& type, const double& xin, const double &yin,
122 double* xout, double *yout) const;
123
124 // Private members
125 mutable bool m_has_lb; //!< Has galactic coordinates
126 mutable bool m_has_radec; //!< Has equatorial coordinates
127 double m_l; //!< Galactic longitude in radians
128 double m_b; //!< Galactic latitude in radians
129 double m_ra; //!< Right Ascension in radians
130 double m_dec; //!< Declination in radians
131
132 // Sincos cache
133 #if defined(G_SINCOS_CACHE)
134 mutable bool m_has_lb_cache;
135 mutable bool m_has_radec_cache;
136 mutable double m_sin_b;
137 mutable double m_cos_b;
138 mutable double m_sin_dec;
139 mutable double m_cos_dec;
140 #endif
141};
142
143
144/***********************************************************************//**
145 * @brief Return class name
146 *
147 * @return String containing the class name ("GSkyDir").
148 ***************************************************************************/
149inline
150std::string GSkyDir::classname(void) const
151{
152 return ("GSkyDir");
153}
154
155
156/***********************************************************************//**
157 * @brief Return galactic longitude in radians
158 *
159 * @return Galactic longitude in radians.
160 ***************************************************************************/
161inline
162const double& GSkyDir::l(void) const
163{
164 if (!m_has_lb && m_has_radec) {
165 equ2gal();
166 }
167 return m_l;
168}
169
170
171/***********************************************************************//**
172 * @brief Return galactic longitude in degrees
173 *
174 * @return Galactic longitude in degrees.
175 ***************************************************************************/
176inline
177double GSkyDir::l_deg(void) const
178{
179 return (l() * gammalib::rad2deg);
180}
181
182
183/***********************************************************************//**
184 * @brief Return galactic latitude in radians
185 *
186 * @return Galactic latitude in radians.
187 ***************************************************************************/
188inline
189const double& GSkyDir::b(void) const
190{
191 if (!m_has_lb && m_has_radec) {
192 equ2gal();
193 }
194 return m_b;
195}
196
197
198/***********************************************************************//**
199 * @brief Returns galactic latitude in degrees
200 *
201 * @return Galactic latitude in degrees.
202 ***************************************************************************/
203inline
204double GSkyDir::b_deg(void) const
205{
206 return (b() * gammalib::rad2deg);
207}
208
209
210/***********************************************************************//**
211 * @brief Return Right Ascension in radians
212 *
213 * @return Right Ascension in radians.
214 ***************************************************************************/
215inline
216const double& GSkyDir::ra(void) const
217{
218 if (!m_has_radec && m_has_lb) {
219 gal2equ();
220 }
221 return m_ra;
222}
223
224
225/***********************************************************************//**
226 * @brief Returns Right Ascension in degrees
227 *
228 * @return Right Ascension in degrees.
229 ***************************************************************************/
230inline
231double GSkyDir::ra_deg(void) const
232{
233 return (ra() * gammalib::rad2deg);
234}
235
236
237/***********************************************************************//**
238 * @brief Return Declination in radians
239 *
240 * @return Declination in radians.
241 ***************************************************************************/
242inline
243const double& GSkyDir::dec(void) const
244{
245 if (!m_has_radec && m_has_lb) {
246 gal2equ();
247 }
248 return m_dec;
249}
250
251
252/***********************************************************************//**
253 * @brief Returns Declination in degrees
254 *
255 * @return Declination in degrees.
256 ***************************************************************************/
257inline
258double GSkyDir::dec_deg(void) const
259{
260 return (dec() * gammalib::rad2deg);
261}
262
263
264/***********************************************************************//**
265 * @brief Compute angular distance between sky directions in radians
266 *
267 * @param[in] dir Sky direction to which distance is to be computed.
268 * @return Angular distance (radians).
269 *
270 * Computes the angular distance between two sky directions in radians.
271 ***************************************************************************/
272inline
273double GSkyDir::dist(const GSkyDir& dir) const
274{
275 return (gammalib::acos(cos_dist(dir)));
276}
277
278
279/***********************************************************************//**
280 * @brief Compute angular distance between sky directions in degrees
281 *
282 * @param[in] dir Sky direction to which distance is to be computed.
283 * @return Angular distance (degrees).
284 *
285 * Computes the angular distance between two sky directions in degrees.
286 ***************************************************************************/
287inline
288double GSkyDir::dist_deg(const GSkyDir& dir) const
289{
290 return (dist(dir) * gammalib::rad2deg);
291}
292
293
294/***********************************************************************//**
295 * @brief Compute position angle between sky directions in degrees
296 *
297 * @param[in] dir Sky direction.
298 * @param[in] coordsys Coordinate system ("CEL" or "GAL")
299 * @return Position angle (deg).
300 *
301 * Computes the position angle of a specified sky direction with respect to
302 * the sky direction of the instance in degrees. If "CEL" is specified as
303 * @p coordsys the position angle is counted counterclockwise from celestial
304 * North, if "GAL" is specified the position angle is counted
305 * counterclockwise from Galactic North.
306 *
307 * See the posang() method for more information about the computation of the
308 * position angle.
309 ***************************************************************************/
310inline
311double GSkyDir::posang_deg(const GSkyDir& dir,
312 const std::string& coordsys) const
313{
314 return (posang(dir, coordsys) * gammalib::rad2deg);
315}
316
317#endif /* GSKYDIR_HPP */
Definition of interface for all GammaLib classes.
Mathematical function definitions.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Vector class interface definition.
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Sky direction class.
Definition GSkyDir.hpp:62
double m_cos_b
Definition GSkyDir.hpp:137
void radec(const double &ra, const double &dec)
Set equatorial sky direction (radians)
Definition GSkyDir.cpp:218
void rotate_deg(const double &phi, const double &theta)
Rotate sky direction by zenith and azimuth angle.
Definition GSkyDir.cpp:573
void lb_deg(const double &l, const double &b)
Set galactic sky direction (degrees)
Definition GSkyDir.cpp:299
virtual ~GSkyDir(void)
Destructor.
Definition GSkyDir.cpp:133
double dec_deg(void) const
Returns Declination in degrees.
Definition GSkyDir.hpp:258
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition GSkyDir.hpp:231
bool m_has_lb
Has galactic coordinates.
Definition GSkyDir.hpp:125
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition GSkyDir.cpp:245
double b_deg(void) const
Returns galactic latitude in degrees.
Definition GSkyDir.hpp:204
void free_members(void)
Delete class members.
Definition GSkyDir.cpp:1471
double dist_deg(const GSkyDir &dir) const
Compute angular distance between sky directions in degrees.
Definition GSkyDir.hpp:288
void rotate(const double &phi, const double &theta)
Rotate sky direction by zenith and azimuth angle.
Definition GSkyDir.cpp:527
const double & dec(void) const
Return Declination in radians.
Definition GSkyDir.hpp:243
GSkyDir * clone(void) const
Clone sky direction.
Definition GSkyDir.cpp:203
GVector galvector(void) const
Return sky direction as 3D vector in galactic coordinates.
Definition GSkyDir.cpp:1139
double cos_dist(const GSkyDir &dir) const
Compute cosine of angular distance between sky directions.
Definition GSkyDir.cpp:1176
double m_sin_b
Definition GSkyDir.hpp:136
double m_sin_dec
Definition GSkyDir.hpp:138
void init_members(void)
Initialise class members.
Definition GSkyDir.cpp:1413
const double & l(void) const
Return galactic longitude in radians.
Definition GSkyDir.hpp:162
void lb(const double &l, const double &b)
Set galactic sky direction (radians)
Definition GSkyDir.cpp:272
void clear(void)
Clear sky direction.
Definition GSkyDir.cpp:185
void euler(const int &type, const double &xin, const double &yin, double *xout, double *yout) const
General coordinate transformation routine for J2000.
Definition GSkyDir.cpp:1531
void set(const std::string &coord)
Definition GSkyDir.cpp:333
double posang_deg(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in degrees.
Definition GSkyDir.hpp:311
bool m_has_radec_cache
Definition GSkyDir.hpp:135
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition GSkyDir.hpp:273
GSkyDir & operator=(const GSkyDir &dir)
Assignment operator.
Definition GSkyDir.cpp:155
void equ2gal(void) const
Convert equatorial to galactic coordinates.
Definition GSkyDir.cpp:1481
void precess(const double &from_epoch, const double &to_epoch)
Precess sky direction.
Definition GSkyDir.cpp:600
std::string classname(void) const
Return class name.
Definition GSkyDir.hpp:150
friend bool operator!=(const GSkyDir &a, const GSkyDir &b)
Non equality operator.
Definition GSkyDir.cpp:1644
std::string print(const GChatter &chatter=NORMAL) const
Print sky direction information.
Definition GSkyDir.cpp:1376
void copy_members(const GSkyDir &dir)
Copy class members.
Definition GSkyDir.cpp:1443
GVector celvector(void) const
Return sky direction as 3D vector in celestial coordinates.
Definition GSkyDir.cpp:1106
void moon(const GTime &time, const double &epoch=2000.0)
Set sky direction to direction of Moon.
Definition GSkyDir.cpp:747
const double & ra(void) const
Return Right Ascension in radians.
Definition GSkyDir.hpp:216
double m_dec
Declination in radians.
Definition GSkyDir.hpp:130
const double & b(void) const
Return galactic latitude in radians.
Definition GSkyDir.hpp:189
GSkyDir(void)
Constructor.
Definition GSkyDir.cpp:60
bool m_has_radec
Has equatorial coordinates.
Definition GSkyDir.hpp:126
double m_cos_dec
Definition GSkyDir.hpp:139
double m_l
Galactic longitude in radians.
Definition GSkyDir.hpp:127
double l_deg(void) const
Return galactic longitude in degrees.
Definition GSkyDir.hpp:177
void gal2equ(void) const
Convert galactic to equatorial coordinates.
Definition GSkyDir.cpp:1503
friend bool operator==(const GSkyDir &a, const GSkyDir &b)
Equality operator.
Definition GSkyDir.cpp:1580
double m_b
Galactic latitude in radians.
Definition GSkyDir.hpp:128
bool m_has_lb_cache
Definition GSkyDir.hpp:134
void sun(const GTime &time, const double &epoch=2000.0)
Set sky direction to direction of Sun.
Definition GSkyDir.cpp:677
double m_ra
Right Ascension in radians.
Definition GSkyDir.hpp:129
double posang(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in radians.
Definition GSkyDir.cpp:1300
Time class.
Definition GTime.hpp:55
Vector class.
Definition GVector.hpp:46
double acos(const double &arg)
Computes acos by avoiding NaN due to rounding errors.
Definition GMath.cpp:69
const double rad2deg
Definition GMath.hpp:44