GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCTAPointing.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCTAPointing.cpp - CTA pointing class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2021 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 GCTAPointing.cpp
23  * @brief CTA pointing class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GFilename.hpp"
33 #include "GMatrix.hpp"
34 #include "GSkyDir.hpp"
35 #include "GXmlElement.hpp"
36 #include "GCTAPointing.hpp"
37 #include "GCTAInstDir.hpp"
38 
39 /* __ Method name definitions ____________________________________________ */
40 #define G_READ_XML "GCTAPointing::read(GXmlElement&)"
41 #define G_WRITE_XML "GCTAPointing::write(GXmlElement&)"
42 
43 /* __ Macros _____________________________________________________________ */
44 
45 /* __ Coding definitions _________________________________________________ */
46 //#define G_USE_VECTORS
47 
48 /* __ Debug definitions __________________________________________________ */
49 
50 
51 
52 /*==========================================================================
53  = =
54  = Constructors/destructors =
55  = =
56  ==========================================================================*/
57 
58 /***********************************************************************//**
59  * @brief Void constructor
60  ***************************************************************************/
62 {
63  // Initialise members
64  init_members();
65 
66  // Return
67  return;
68 }
69 
70 
71 /***********************************************************************//**
72  * @brief Sky direction constructor
73  *
74  * @param[in] dir Sky direction.
75  *
76  * Construct CTA pointing from sky direction.
77  ***************************************************************************/
79 {
80  // Initialise members
81  init_members();
82 
83  // Assign sky direction
84  this->dir(dir);
85 
86  // Return
87  return;
88 }
89 
90 
91 /***********************************************************************//**
92  * @brief XML constructor
93  *
94  * @param[in] xml XML element.
95  *
96  * Construct CTA pointing from XML element.
97  ***************************************************************************/
99 {
100  // Initialise members
101  init_members();
102 
103  // Read information from XML element
104  read(xml);
105 
106  // Return
107  return;
108 }
109 
110 
111 /***********************************************************************//**
112  * @brief Copy constructor
113  *
114  * @param[in] pnt CTA pointing.
115  ***************************************************************************/
117 {
118  // Initialise members
119  init_members();
120 
121  // Copy members
122  copy_members(pnt);
123 
124  // Return
125  return;
126 }
127 
128 
129 /***********************************************************************//**
130  * @brief Destructor
131  ***************************************************************************/
133 {
134  // Free members
135  free_members();
136 
137  // Return
138  return;
139 }
140 
141 
142 /*==========================================================================
143  = =
144  = Operators =
145  = =
146  ==========================================================================*/
147 
148 /***********************************************************************//**
149  * @brief Assignment operator
150  *
151  * @param[in] pnt CTA pointing.
152  * @return CTA pointing.
153  ***************************************************************************/
155 {
156  // Execute only if object is not identical
157  if (this != &pnt) {
158 
159  // Free members
160  free_members();
161 
162  // Initialise members
163  init_members();
164 
165  // Copy members
166  copy_members(pnt);
167 
168  } // endif: object was not identical
169 
170  // Return this object
171  return *this;
172 }
173 
174 
175 /*==========================================================================
176  = =
177  = Public methods =
178  = =
179  ==========================================================================*/
180 
181 /***********************************************************************//**
182  * @brief Clear CTA pointing
183  ***************************************************************************/
185 {
186  // Free members
187  free_members();
188 
189  // Initialise private members
190  init_members();
191 
192  // Return
193  return;
194 }
195 
196 
197 /***********************************************************************//**
198  * @brief Clone CTA pointing
199  *
200  * @return Poiter to deep copy of CTA pointing.
201  ***************************************************************************/
203 {
204  return new GCTAPointing(*this);
205 }
206 
207 
208 /***********************************************************************//**
209  * @brief Set pointing direction
210  *
211  * @param[in] dir Sky direction of pointing.
212  *
213  * Set the pointing direction to the specified @p sky direction.
214  ***************************************************************************/
215 void GCTAPointing::dir(const GSkyDir& dir)
216 {
217  // Set sky direction
218  m_dir = dir;
219 
220  // Invalidate cache
221  m_has_cache = false;
222 
223  // Signal that pointing is valid
224  m_valid = true;
225 
226  // Return
227  return;
228 }
229 
230 
231 /***********************************************************************//**
232  * @brief Get instrument direction from sky direction
233  *
234  * @param[in] skydir Sky direction.
235  * @return CTA instrument direction.
236  *
237  * Returns instrument direction as function of a sky direction
238  ***************************************************************************/
240 {
241  #if defined(G_USE_VECTORS)
242  // Compute rotation matrix
243  update();
244 
245  // Get celestial vector from sky coordinate
246  GVector celvector = skydir.celvector();
247 
248  // Transform to instrument system
249  GVector inst = m_Rback.transpose() * celvector;
250 
251  // Initialise instrument coordinates
252  double detx(0.0);
253  double dety(0.0);
254 
255  // Get offset from cel vector, i.e. distance from camera center
256  double theta = std::acos(inst[2]);
257 
258  // Check if theta and phi are defined
259  if (theta > 0.0 ) {
260  double phi = std::asin(inst[1] / std::sin(theta));
261  detx = theta * std::cos(phi);
262  dety = theta * std::sin(phi);
263  }
264  #else
265  double theta = m_dir.dist(skydir);
266  double phi = m_dir.posang(skydir); // Celestial system
267  double detx(0.0);
268  double dety(0.0);
269  if (theta > 0.0 ) {
270  detx = theta * std::cos(phi);
271  dety = theta * std::sin(phi);
272  }
273  #endif
274 
275  // Initialise instrument direction
276  GCTAInstDir inst_direction(skydir);
277  inst_direction.detx(detx);
278  inst_direction.dety(dety);
279 
280  // Return
281  return inst_direction;
282 }
283 
284 
285 /***********************************************************************//**
286  * @brief Get sky direction direction from instrument direction
287  *
288  * @param[in] instdir Instrument direction.
289  * @return Sky direction.
290  *
291  * Returns sky direction as function of an instrument direction
292  ***************************************************************************/
294 {
295  // Compute rotation matrix
296  update();
297 
298  // Retrieve instrument coordinates
299  double inst_x = instdir.detx();
300  double inst_y = instdir.dety();
301 
302  // Convert to polar coordinates
303  double theta = std::sqrt(inst_x * inst_x + inst_y * inst_y);
304  double phi = std::atan2(inst_y, inst_x);
305  double sin_phi = std::sin(phi);
306  double cos_phi = std::cos(phi);
307  double sin_theta = std::sin(theta);
308  double cos_theta = std::cos(theta);
309 
310  // Build vector from polar coordinates
311  GVector native(-cos_phi*sin_theta, sin_phi*sin_theta, cos_theta);
312 
313  // Rotate from instrument system into sky system
314  GVector skyvector = m_Rback * native;
315  GSkyDir sky;
316  sky.celvector(skyvector);
317 
318  // Return
319  return sky;
320 
321 }
322 
323 
324 /***********************************************************************//**
325  * @brief Return rotation matrix
326  *
327  * @return Rotation matrix.
328  ***************************************************************************/
329 const GMatrix& GCTAPointing::rot(void) const
330 {
331  // Update cache
332  update();
333 
334  // Return rotation matrix
335  return m_Rback;
336 }
337 
338 
339 /***********************************************************************//**
340  * @brief Read pointing from XML element
341  *
342  * @param[in] xml XML element.
343  *
344  * @exception GException::invalid_value
345  * Invalid XML format encountered.
346  *
347  * Read pointing parameter from an XML element. The format of the pointing
348  * parameter is
349  *
350  * <parameter name="Pointing" ra="83.0" dec="22.1"/>
351  *
352  ***************************************************************************/
354 {
355  // Clear pointing
356  clear();
357 
358  // Get pointing parameter
359  const GXmlElement* par = gammalib::xml_get_par(G_READ_XML, xml, "Pointing");
360 
361  // Extract position attributes
362  if (par->has_attribute("ra") && par->has_attribute("dec")) {
363  double ra = gammalib::todouble(par->attribute("ra"));
364  double dec = gammalib::todouble(par->attribute("dec"));
365  m_dir.radec_deg(ra,dec);
366  }
367  else if (par->has_attribute("lon") && par->has_attribute("lat")) {
368  double lon = gammalib::todouble(par->attribute("lon"));
369  double lat = gammalib::todouble(par->attribute("lat"));
370  m_dir.lb_deg(lon,lat);
371  }
372  else {
373  std::string msg = "Attributes \"ra\" and \"dec\" or \"lon\" and"
374  " \"lat\"not found in XML parameter \"Pointing\"."
375  " Please verify the XML format.";
377  }
378 
379  // Signal that pointing is valid
380  m_valid = true;
381 
382  // Return
383  return;
384 }
385 
386 
387 /***********************************************************************//**
388  * @brief Write pointing information into XML element
389  *
390  * @param[in] xml XML element.
391  *
392  * Writes pointing parameter into an XML element. The format of the pointing
393  * parameter is
394  *
395  * <parameter name="Pointing" ra="83.0" dec="22.1"/>
396  *
397  ***************************************************************************/
399 {
400  // Get parameter
401  GXmlElement* par = gammalib::xml_need_par(G_WRITE_XML, xml, "Pointing");
402 
403  // Write attributes
404  par->attribute("ra", gammalib::str(m_dir.ra_deg()));
405  par->attribute("dec", gammalib::str(m_dir.dec_deg()));
406 
407  // Return
408  return;
409 }
410 
411 
412 /***********************************************************************//**
413  * @brief Print CTA pointing information
414  *
415  * @param[in] chatter Chattiness.
416  * @return String containing pointing information.
417  ***************************************************************************/
418 std::string GCTAPointing::print(const GChatter& chatter) const
419 {
420  // Initialise result string
421  std::string result;
422 
423  // Continue only if chatter is not silent
424  if (chatter != SILENT) {
425 
426  // Append header
427  result.append("=== GCTAPointing ===");
428 
429  // Append information
430  result.append("\n"+gammalib::parformat("Pointing direction"));
431  result.append(this->dir().print());
432 
433  } // endif: chatter was not silent
434 
435  // Return result
436  return result;
437 }
438 
439 
440 /*==========================================================================
441  = =
442  = Private methods =
443  = =
444  ==========================================================================*/
445 
446 /***********************************************************************//**
447  * @brief Initialise class members
448  ***************************************************************************/
450 {
451  // Initialise members
452  m_dir.clear();
453  m_valid = false;
454  m_zenith = 0.0;
455  m_azimuth = 0.0;
456 
457  // Initialise cache
458  m_has_cache = false;
459  m_Rback.clear();
460 
461  // Return
462  return;
463 }
464 
465 
466 /***********************************************************************//**
467  * @brief Copy class members
468  *
469  * @param[in] pnt CTA pointing.
470  ***************************************************************************/
472 {
473  // Copy members
474  m_dir = pnt.m_dir;
475  m_valid = pnt.m_valid;
476  m_zenith = pnt.m_zenith;
477  m_azimuth = pnt.m_azimuth;
478 
479  // Copy cache
480  m_has_cache = pnt.m_has_cache;
481  m_Rback = pnt.m_Rback;
482 
483  // Return
484  return;
485 }
486 
487 
488 /***********************************************************************//**
489  * @brief Delete class members
490  ***************************************************************************/
492 {
493  // Return
494  return;
495 }
496 
497 
498 /***********************************************************************//**
499  * @brief Update coordinate transformation cache
500  ***************************************************************************/
501 void GCTAPointing::update(void) const
502 {
503  // Update coordinate transformation cache only if required
504  if (!m_has_cache) {
505 
506  // Set up Euler matrices
507  GMatrix Ry;
508  GMatrix Rz;
509  Ry.eulery(m_dir.dec_deg() - 90.0);
510  Rz.eulerz(-m_dir.ra_deg());
511 
512  // Compute rotation matrix
513  m_Rback = (Ry * Rz).transpose();
514 
515  // Signal that we have a valid transformation cache
516  m_has_cache = true;
517 
518  } // endif: Update of cache was required
519 
520  // Return
521  return;
522 }
void clear(void)
Clear CTA pointing.
GMatrix m_Rback
Rotation matrix.
double dec_deg(void) const
Returns Declination in degrees.
Definition: GSkyDir.hpp:256
void detx(const double &x)
Set DETX coordinate (in radians)
XML element node class interface definition.
GSkyDir skydir(const GCTAInstDir &instdir) const
Get sky direction direction from instrument direction.
Sky direction class interface definition.
GVector cos(const GVector &vector)
Computes cosine of vector elements.
Definition: GVector.cpp:1190
Generic matrix class definition.
XML element node class.
Definition: GXmlElement.hpp:48
CTA pointing class interface definition.
virtual ~GCTAPointing(void)
Destructor.
double acos(const double &arg)
Computes acos by avoiding NaN due to rounding errors.
Definition: GMath.cpp:69
Gammalib tools definition.
void free_members(void)
Delete class members.
void read(const GXmlElement &xml)
Read pointing from XML element.
void write(GXmlElement &xml) const
Write pointing information into XML element.
#define G_READ_XML
void eulery(const double &angle)
Set Euler rotation matrix around y axis.
Definition: GMatrix.cpp:1157
virtual void clear(void)
Clear matrix.
Definition: GMatrix.cpp:541
bool m_has_cache
Has transformation cache.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition: GVector.cpp:1358
double m_zenith
Pointing zenith angle (deg)
const GMatrix & rot(void) const
Return rotation matrix.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
GMatrix transpose(void) const
Return transposed matrix.
Definition: GMatrix.cpp:903
double m_azimuth
Pointing azimuth angle (deg)
void eulerz(const double &angle)
Set Euler rotation matrix around z axis.
Definition: GMatrix.cpp:1194
bool has_attribute(const std::string &name) const
Check if element has a given attribute.
void celvector(const GVector &vector)
Set sky direction from 3D vector in celestial coordinates.
Definition: GSkyDir.cpp:313
GCTAInstDir instdir(const GSkyDir &skydir) const
Get instrument direction from sky direction.
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:1637
CTA pointing class.
GCTAPointing * clone(void) const
Clone CTA pointing.
CTA instrument direction class interface definition.
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition: GSkyDir.hpp:229
GChatter
Definition: GTypemaps.hpp:33
virtual GCTAPointing & operator=(const GCTAPointing &pnt)
Assignment operator.
#define G_WRITE_XML
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition: GSkyDir.cpp:224
GVector asin(const GVector &vector)
Computes arcsin of vector elements.
Definition: GVector.cpp:1106
double posang(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in radians.
Definition: GSkyDir.cpp:1151
void clear(void)
Clear sky direction.
Definition: GSkyDir.cpp:164
bool m_valid
Validity flag.
std::string print(const GChatter &chatter=NORMAL) const
Print CTA pointing information.
void lb_deg(const double &l, const double &b)
Set galactic sky direction (degrees)
Definition: GSkyDir.cpp:278
GVector sin(const GVector &vector)
Computes sine of vector elements.
Definition: GVector.cpp:1316
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition: GSkyDir.hpp:271
double atan2(const double &y, const double &x)
Compute arc tangens in radians.
Definition: GMath.cpp:102
void copy_members(const GCTAPointing &pnt)
Copy class members.
CTA instrument direction class.
Definition: GCTAInstDir.hpp:63
const GSkyDir & dir(void) const
Return pointing sky direction.
void dety(const double &y)
Set DETY coordinate (in radians)
Generic matrix class definition.
Definition: GMatrix.hpp:79
Vector class.
Definition: GVector.hpp:46
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
Sky direction class.
Definition: GSkyDir.hpp:62
void update(void) const
Update coordinate transformation cache.
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:1689
Filename class interface definition.
double todouble(const std::string &arg)
Convert string into double precision value.
Definition: GTools.cpp:926
GSkyDir m_dir
Pointing direction in sky coordinates.
GCTAPointing(void)
Void constructor.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489
void init_members(void)
Initialise class members.