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