GammaLib 2.0.0
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-2022 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 GVector& vector);
72 GSkyDir(const GSkyDir& dir);
73 virtual ~GSkyDir(void);
74
75 // Operators
76 GSkyDir& operator=(const GSkyDir& dir);
77
78 // Methods
79 void clear(void);
80 GSkyDir* clone(void) const;
81 std::string classname(void) const;
82 void radec(const double& ra, const double& dec);
83 void radec_deg(const double& ra, const double& dec);
84 void lb(const double& l, const double& b);
85 void lb_deg(const double& l, const double& b);
86 void celvector(const GVector& vector);
87 void galvector(const GVector& vector);
88 void rotate(const double& phi, const double& theta);
89 void rotate_deg(const double& phi, const double& theta);
90 void precess(const double& from_epoch, const double& to_epoch);
91 void sun(const GTime& time, const double& epoch = 2000.0);
92 void moon(const GTime& time, const double& epoch = 2000.0);
93 const double& l(void) const;
94 const double& b(void) const;
95 const double& ra(void) const;
96 const double& dec(void) const;
97 double l_deg(void) const;
98 double b_deg(void) const;
99 double ra_deg(void) const;
100 double dec_deg(void) const;
101 GVector celvector(void) const;
102 GVector galvector(void) const;
103 double cos_dist(const GSkyDir& dir) const;
104 double dist(const GSkyDir& dir) const;
105 double dist_deg(const GSkyDir& dir) const;
106 double posang(const GSkyDir& dir,
107 const std::string& coordsys = "CEL") const;
108 double posang_deg(const GSkyDir& dir,
109 const std::string& coordsys = "CEL") const;
110 std::string print(const GChatter& chatter = NORMAL) const;
111
112private:
113 // Private methods
114 void init_members(void);
115 void copy_members(const GSkyDir& dir);
116 void free_members(void);
117 void equ2gal(void) const;
118 void gal2equ(void) const;
119 void euler(const int& type, const double& xin, const double &yin,
120 double* xout, double *yout) const;
121
122 // Private members
123 mutable bool m_has_lb; //!< Has galactic coordinates
124 mutable bool m_has_radec; //!< Has equatorial coordinates
125 double m_l; //!< Galactic longitude in radians
126 double m_b; //!< Galactic latitude in radians
127 double m_ra; //!< Right Ascension in radians
128 double m_dec; //!< Declination in radians
129
130 // Sincos cache
131 #if defined(G_SINCOS_CACHE)
132 mutable bool m_has_lb_cache;
133 mutable bool m_has_radec_cache;
134 mutable double m_sin_b;
135 mutable double m_cos_b;
136 mutable double m_sin_dec;
137 mutable double m_cos_dec;
138 #endif
139};
140
141
142/***********************************************************************//**
143 * @brief Return class name
144 *
145 * @return String containing the class name ("GSkyDir").
146 ***************************************************************************/
147inline
148std::string GSkyDir::classname(void) const
149{
150 return ("GSkyDir");
151}
152
153
154/***********************************************************************//**
155 * @brief Return galactic longitude in radians
156 *
157 * @return Galactic longitude in radians.
158 ***************************************************************************/
159inline
160const double& GSkyDir::l(void) const
161{
162 if (!m_has_lb && m_has_radec) {
163 equ2gal();
164 }
165 return m_l;
166}
167
168
169/***********************************************************************//**
170 * @brief Return galactic longitude in degrees
171 *
172 * @return Galactic longitude in degrees.
173 ***************************************************************************/
174inline
175double GSkyDir::l_deg(void) const
176{
177 return (l() * gammalib::rad2deg);
178}
179
180
181/***********************************************************************//**
182 * @brief Return galactic latitude in radians
183 *
184 * @return Galactic latitude in radians.
185 ***************************************************************************/
186inline
187const double& GSkyDir::b(void) const
188{
189 if (!m_has_lb && m_has_radec) {
190 equ2gal();
191 }
192 return m_b;
193}
194
195
196/***********************************************************************//**
197 * @brief Returns galactic latitude in degrees
198 *
199 * @return Galactic latitude in degrees.
200 ***************************************************************************/
201inline
202double GSkyDir::b_deg(void) const
203{
204 return (b() * gammalib::rad2deg);
205}
206
207
208/***********************************************************************//**
209 * @brief Return Right Ascension in radians
210 *
211 * @return Right Ascension in radians.
212 ***************************************************************************/
213inline
214const double& GSkyDir::ra(void) const
215{
216 if (!m_has_radec && m_has_lb) {
217 gal2equ();
218 }
219 return m_ra;
220}
221
222
223/***********************************************************************//**
224 * @brief Returns Right Ascension in degrees
225 *
226 * @return Right Ascension in degrees.
227 ***************************************************************************/
228inline
229double GSkyDir::ra_deg(void) const
230{
231 return (ra() * gammalib::rad2deg);
232}
233
234
235/***********************************************************************//**
236 * @brief Return Declination in radians
237 *
238 * @return Declination in radians.
239 ***************************************************************************/
240inline
241const double& GSkyDir::dec(void) const
242{
243 if (!m_has_radec && m_has_lb) {
244 gal2equ();
245 }
246 return m_dec;
247}
248
249
250/***********************************************************************//**
251 * @brief Returns Declination in degrees
252 *
253 * @return Declination in degrees.
254 ***************************************************************************/
255inline
256double GSkyDir::dec_deg(void) const
257{
258 return (dec() * gammalib::rad2deg);
259}
260
261
262/***********************************************************************//**
263 * @brief Compute angular distance between sky directions in radians
264 *
265 * @param[in] dir Sky direction to which distance is to be computed.
266 * @return Angular distance (radians).
267 *
268 * Computes the angular distance between two sky directions in radians.
269 ***************************************************************************/
270inline
271double GSkyDir::dist(const GSkyDir& dir) const
272{
273 return (gammalib::acos(cos_dist(dir)));
274}
275
276
277/***********************************************************************//**
278 * @brief Compute angular distance between sky directions in degrees
279 *
280 * @param[in] dir Sky direction to which distance is to be computed.
281 * @return Angular distance (degrees).
282 *
283 * Computes the angular distance between two sky directions in degrees.
284 ***************************************************************************/
285inline
286double GSkyDir::dist_deg(const GSkyDir& dir) const
287{
288 return (dist(dir) * gammalib::rad2deg);
289}
290
291
292/***********************************************************************//**
293 * @brief Compute position angle between sky directions in degrees
294 *
295 * @param[in] dir Sky direction.
296 * @param[in] coordsys Coordinate system ("CEL" or "GAL")
297 * @return Position angle (deg).
298 *
299 * Computes the position angle of a specified sky direction with respect to
300 * the sky direction of the instance in degrees. If "CEL" is specified as
301 * @p coordsys the position angle is counted counterclockwise from celestial
302 * North, if "GAL" is specified the position angle is counted
303 * counterclockwise from Galactic North.
304 *
305 * See the posang() method for more information about the computation of the
306 * position angle.
307 ***************************************************************************/
308inline
309double GSkyDir::posang_deg(const GSkyDir& dir,
310 const std::string& coordsys) const
311{
312 return (posang(dir, coordsys) * gammalib::rad2deg);
313}
314
315#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:135
void radec(const double &ra, const double &dec)
Set equatorial sky direction (radians)
Definition GSkyDir.cpp:197
void rotate_deg(const double &phi, const double &theta)
Rotate sky direction by zenith and azimuth angle.
Definition GSkyDir.cpp:424
void lb_deg(const double &l, const double &b)
Set galactic sky direction (degrees)
Definition GSkyDir.cpp:278
virtual ~GSkyDir(void)
Destructor.
Definition GSkyDir.cpp:112
double dec_deg(void) const
Returns Declination in degrees.
Definition GSkyDir.hpp:256
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition GSkyDir.hpp:229
bool m_has_lb
Has galactic coordinates.
Definition GSkyDir.hpp:123
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition GSkyDir.cpp:224
double b_deg(void) const
Returns galactic latitude in degrees.
Definition GSkyDir.hpp:202
void free_members(void)
Delete class members.
Definition GSkyDir.cpp:1322
double dist_deg(const GSkyDir &dir) const
Compute angular distance between sky directions in degrees.
Definition GSkyDir.hpp:286
void rotate(const double &phi, const double &theta)
Rotate sky direction by zenith and azimuth angle.
Definition GSkyDir.cpp:378
const double & dec(void) const
Return Declination in radians.
Definition GSkyDir.hpp:241
GSkyDir * clone(void) const
Clone sky direction.
Definition GSkyDir.cpp:182
GVector galvector(void) const
Return sky direction as 3D vector in galactic coordinates.
Definition GSkyDir.cpp:990
double cos_dist(const GSkyDir &dir) const
Compute cosine of angular distance between sky directions.
Definition GSkyDir.cpp:1027
double m_sin_b
Definition GSkyDir.hpp:134
double m_sin_dec
Definition GSkyDir.hpp:136
void init_members(void)
Initialise class members.
Definition GSkyDir.cpp:1264
const double & l(void) const
Return galactic longitude in radians.
Definition GSkyDir.hpp:160
void lb(const double &l, const double &b)
Set galactic sky direction (radians)
Definition GSkyDir.cpp:251
void clear(void)
Clear sky direction.
Definition GSkyDir.cpp:164
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:1382
double posang_deg(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in degrees.
Definition GSkyDir.hpp:309
bool m_has_radec_cache
Definition GSkyDir.hpp:133
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition GSkyDir.hpp:271
GSkyDir & operator=(const GSkyDir &dir)
Assignment operator.
Definition GSkyDir.cpp:134
void equ2gal(void) const
Convert equatorial to galactic coordinates.
Definition GSkyDir.cpp:1332
void precess(const double &from_epoch, const double &to_epoch)
Precess sky direction.
Definition GSkyDir.cpp:451
std::string classname(void) const
Return class name.
Definition GSkyDir.hpp:148
friend bool operator!=(const GSkyDir &a, const GSkyDir &b)
Non equality operator.
Definition GSkyDir.cpp:1495
std::string print(const GChatter &chatter=NORMAL) const
Print sky direction information.
Definition GSkyDir.cpp:1227
void copy_members(const GSkyDir &dir)
Copy class members.
Definition GSkyDir.cpp:1294
GVector celvector(void) const
Return sky direction as 3D vector in celestial coordinates.
Definition GSkyDir.cpp:957
void moon(const GTime &time, const double &epoch=2000.0)
Set sky direction to direction of Moon.
Definition GSkyDir.cpp:598
const double & ra(void) const
Return Right Ascension in radians.
Definition GSkyDir.hpp:214
double m_dec
Declination in radians.
Definition GSkyDir.hpp:128
const double & b(void) const
Return galactic latitude in radians.
Definition GSkyDir.hpp:187
GSkyDir(void)
Constructor.
Definition GSkyDir.cpp:60
bool m_has_radec
Has equatorial coordinates.
Definition GSkyDir.hpp:124
double m_cos_dec
Definition GSkyDir.hpp:137
double m_l
Galactic longitude in radians.
Definition GSkyDir.hpp:125
double l_deg(void) const
Return galactic longitude in degrees.
Definition GSkyDir.hpp:175
void gal2equ(void) const
Convert galactic to equatorial coordinates.
Definition GSkyDir.cpp:1354
friend bool operator==(const GSkyDir &a, const GSkyDir &b)
Equality operator.
Definition GSkyDir.cpp:1431
double m_b
Galactic latitude in radians.
Definition GSkyDir.hpp:126
bool m_has_lb_cache
Definition GSkyDir.hpp:132
void sun(const GTime &time, const double &epoch=2000.0)
Set sky direction to direction of Sun.
Definition GSkyDir.cpp:528
double m_ra
Right Ascension in radians.
Definition GSkyDir.hpp:127
double posang(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in radians.
Definition GSkyDir.cpp:1151
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