GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GWcsARC.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GWcsARC.cpp - Zenithal/azimuthal equidistant (ARC) projection class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2018-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 GWcsARC.cpp
23  * @brief Zenithal/azimuthal equidistant (ARC) projection class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GMath.hpp"
33 #include "GTools.hpp"
34 #include "GWcsARC.hpp"
35 #include "GWcsRegistry.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 #define G_PRJ_X2S "GWcsARC::prj_x2s(int, int, int, int, double*, double*,"\
39  " double*, double*, int*)"
40 #define G_PRJ_S2X "GWcsARC::prj_s2x(int, int, int, int, double*, double*,"\
41  " double*, double*, int*)"
42 
43 /* __ Macros _____________________________________________________________ */
44 
45 /* __ Coding definitions _________________________________________________ */
46 
47 /* __ Debug definitions __________________________________________________ */
48 
49 /* __ Local prototypes ___________________________________________________ */
50 
51 /* __ Constants __________________________________________________________ */
52 
53 /* __ Globals ____________________________________________________________ */
55 const GWcsRegistry g_wcs_arc_registry(&g_wcs_arc_seed);
56 
57 
58 /*==========================================================================
59  = =
60  = Constructors/destructors =
61  = =
62  ==========================================================================*/
63 
64 /***********************************************************************//**
65  * @brief Void constructor
66  ***************************************************************************/
68 {
69  // Initialise class members
70  init_members();
71 
72  // Return
73  return;
74 }
75 
76 
77 /***********************************************************************//**
78  * @brief Constructor
79  *
80  * @param[in] coords Coordinate system.
81  * @param[in] crval1 X value of reference pixel.
82  * @param[in] crval2 Y value of reference pixel.
83  * @param[in] crpix1 X index of reference pixel (starting from 1).
84  * @param[in] crpix2 Y index of reference pixel (starting from 1).
85  * @param[in] cdelt1 Increment in x direction at reference pixel [deg].
86  * @param[in] cdelt2 Increment in y direction at reference pixel [deg].
87  ***************************************************************************/
88 GWcsARC::GWcsARC(const std::string& coords,
89  const double& crval1, const double& crval2,
90  const double& crpix1, const double& crpix2,
91  const double& cdelt1, const double& cdelt2) :
92  GWcs(coords, crval1, crval2, crpix1, crpix2, cdelt1, cdelt2)
93 
94 {
95  // Initialise class members
96  init_members();
97 
98  // Setup WCS derived parameters
99  wcs_set();
100 
101  // Return
102  return;
103 }
104 
105 
106 /***********************************************************************//**
107  * @brief Copy constructor
108  *
109  * @param[in] wcs World Coordinate System.
110  ***************************************************************************/
111 GWcsARC::GWcsARC(const GWcsARC& wcs) : GWcs(wcs)
112 {
113  // Initialise class members for clean destruction
114  init_members();
115 
116  // Copy members
117  copy_members(wcs);
118 
119  // Return
120  return;
121 }
122 
123 
124 /***********************************************************************//**
125  * @brief Destructor
126  ***************************************************************************/
128 {
129  // Free members
130  free_members();
131 
132  // Return
133  return;
134 }
135 
136 
137 /*==========================================================================
138  = =
139  = Operators =
140  = =
141  ==========================================================================*/
142 
143 /***********************************************************************//**
144  * @brief Assignment operator
145  *
146  * @param[in] wcs World Coordinate System.
147  * @return World Coordinate System.
148  ***************************************************************************/
150 {
151  // Execute only if object is not identical
152  if (this != &wcs) {
153 
154  // Copy base class members
155  this->GWcs::operator=(wcs);
156 
157  // Free members
158  free_members();
159 
160  // Initialise private members for clean destruction
161  init_members();
162 
163  // Copy members
164  copy_members(wcs);
165 
166  } // endif: object was not identical
167 
168  // Return this object
169  return *this;
170 }
171 
172 
173 /*==========================================================================
174  = =
175  = Public methods =
176  = =
177  ==========================================================================*/
178 
179 /***********************************************************************//**
180  * @brief Clear instance
181  *
182  * This method properly resets the object to an initial state.
183  ***************************************************************************/
184 void GWcsARC::clear(void)
185 {
186  // Free class members (base and derived classes, derived class first)
187  free_members();
188  this->GWcs::free_members();
190 
191  // Initialise members
193  this->GWcs::init_members();
194  init_members();
195 
196  // Return
197  return;
198 }
199 
200 
201 /***********************************************************************//**
202  * @brief Clone instance
203  *
204  * @return Pointer to deep copy of Zenithal/azimuthal equidistant projection.
205  ***************************************************************************/
207 {
208  return new GWcsARC(*this);
209 }
210 
211 
212 
213 /***********************************************************************//**
214  * @brief Print WCS information
215  *
216  * @param[in] chatter Chattiness.
217  * @return String containing WCS information.
218  ***************************************************************************/
219 std::string GWcsARC::print(const GChatter& chatter) const
220 {
221  // Initialise result string
222  std::string result;
223 
224  // Continue only if chatter is not silent
225  if (chatter != SILENT) {
226 
227  // Append header
228  result.append("=== GWcsARC ===");
229 
230  // Append information
231  result.append(wcs_print(chatter));
232 
233  } // endif: chatter was not silent
234 
235  // Return result
236  return result;
237 }
238 
239 
240 /*==========================================================================
241  = =
242  = Private methods =
243  = =
244  ==========================================================================*/
245 
246 /***********************************************************************//**
247  * @brief Initialise class members
248  ***************************************************************************/
250 {
251  // Return
252  return;
253 }
254 
255 
256 /***********************************************************************//**
257  * @brief Copy class members
258  *
259  * @param[in] wcs World Coordinate System.
260  ***************************************************************************/
262 {
263  // Return
264  return;
265 }
266 
267 
268 /***********************************************************************//**
269  * @brief Delete class members
270  ***************************************************************************/
272 {
273  // Return
274  return;
275 }
276 
277 
278 /***********************************************************************//**
279  * @brief Setup of projection
280  *
281  * This method sets up the projection information. The method has been
282  * adapted from the wcslib function prj.c::arcset (version 7.2).
283  *
284  * Given and/or returned:
285  * m_r0 Reset to 180/pi if 0.
286  * m_phi0 Reset to 0.0 if undefined.
287  * m_theta0 Reset to 90.0 if undefined.
288  *
289  * Returned:
290  * m_x0 Fiducial offset in x.
291  * m_y0 Fiducial offset in y.
292  * m_w[0] r0*(pi/180)
293  * m_w[1] (180/pi)/r0
294  ***************************************************************************/
295 void GWcsARC::prj_set(void) const
296 {
297  // Signal that projection has been set (needs to be done before calling
298  // the prj_off() method to avoid an endless loop)
299  m_prjset = true;
300 
301  // Initialise projection parameters
302  m_w.assign(2, 0.0);
303 
304  // Set undefined parameters
305  if (m_r0 == 0.0) {
307  m_w[0] = 1.0;
308  m_w[1] = 1.0;
309  }
310  else {
311  m_w[0] = m_r0 * gammalib::deg2rad;
312  m_w[1] = 1.0/m_w[0];
313  }
314 
315  // Compute fiducial offset
316  prj_off(0.0, 90.0);
317 
318  // Return
319  return;
320 }
321 
322 
323 /***********************************************************************//**
324  * @brief Pixel-to-spherical deprojection
325  *
326  * @param[in] nx X vector length.
327  * @param[in] ny Y vector length (0=no replication).
328  * @param[in] sxy Input vector step.
329  * @param[in] spt Output vector step.
330  * @param[in] x Vector of projected x coordinates.
331  * @param[in] y Vector of projected y coordinates.
332  * @param[out] phi Longitude of the projected point in native spherical
333  * coordinates [deg].
334  * @param[out] theta Latitude of the projected point in native spherical
335  * coordinates [deg].
336  * @param[out] stat Status return value for each vector element (always 0)
337  *
338  * Deproject pixel (x,y) coordinates in the plane of projection to native
339  * spherical coordinates (phi,theta).
340  *
341  * This method has been adapted from the wcslib function prj.c::arcx2s().
342  * The interface follows very closely that of wcslib. In contrast to the
343  * wcslib routine, however, the method assumes that the projection has been
344  * setup previously (as this will be done by the constructor).
345  ***************************************************************************/
346 void GWcsARC::prj_x2s(int nx, int ny, int sxy, int spt,
347  const double* x, const double* y,
348  double* phi, double* theta, int* stat) const
349 {
350  // Initialize projection if required
351  if (!m_prjset) {
352  prj_set();
353  }
354 
355  // Set value replication length mx,my
356  int mx;
357  int my;
358  if (ny > 0) {
359  mx = nx;
360  my = ny;
361  }
362  else {
363  mx = 1;
364  my = 1;
365  ny = nx;
366  }
367 
368  // Do x dependence
369  const double* xp = x;
370  int rowoff = 0;
371  int rowlen = nx * spt;
372  for (int ix = 0; ix < nx; ++ix, rowoff += spt, xp += sxy) {
373  double xj = *xp + m_x0;
374  double* phip = phi + rowoff;
375  for (int iy = 0; iy < my; ++iy, phip += rowlen) {
376  *phip = xj;
377  }
378  }
379 
380  // Do y dependence
381  const double* yp = y;
382  double* phip = phi;
383  double* thetap = theta;
384  int* statp = stat;
385  for (int iy = 0; iy < ny; ++iy, yp += sxy) {
386  double yj = *yp + m_y0;
387  double yj2 = yj * yj;
388  for (int ix = 0; ix < mx; ++ix, phip += spt, thetap += spt) {
389  double xj = *phip;
390  double r = std::sqrt(xj*xj + yj2);
391  if (r == 0.0) {
392  *phip = 0.0;
393  *thetap = 90.0;
394  }
395  else {
396  *phip = gammalib::atan2d(xj, -yj);
397  *thetap = 90.0 - r * m_w[1];
398  }
399  *(statp++) = 0;
400  }
401  }
402 
403  // Do boundary checking
404  int status = prj_bchk(1.0e-13, nx, my, spt, phi, theta, stat);
405 
406  // Check status code
408 
409  // Return
410  return;
411 }
412 
413 
414 /***********************************************************************//**
415  * @brief Generic spherical-to-pixel projection
416  *
417  * @param[in] nphi Longitude vector length.
418  * @param[in] ntheta Latitude vector length (0=no replication).
419  * @param[in] spt Input vector step.
420  * @param[in] sxy Output vector step.
421  * @param[in] phi Longitude vector of the projected point in native spherical
422  * coordinates [deg].
423  * @param[in] theta Latitude vector of the projected point in native spherical
424  * coordinates [deg].
425  * @param[out] x Vector of projected x coordinates.
426  * @param[out] y Vector of projected y coordinates.
427  * @param[out] stat Status return value for each vector element (always 0)
428  *
429  * Project native spherical coordinates (phi,theta) to pixel (x,y)
430  * coordinates in the plane of projection.
431  *
432  * This method has been adapted from the wcslib function prj.c::arcs2x().
433  * The interface follows very closely that of wcslib. In contrast to the
434  * wcslib routine, however, the method assumes that the projection has been
435  * setup previously (as this will be done by the constructor).
436  ***************************************************************************/
437 void GWcsARC::prj_s2x(int nphi, int ntheta, int spt, int sxy,
438  const double* phi, const double* theta,
439  double* x, double* y, int* stat) const
440 {
441  // Initialize projection if required
442  if (!m_prjset) {
443  prj_set();
444  }
445 
446  // Set value replication length mphi,mtheta
447  int mphi;
448  int mtheta;
449  if (ntheta > 0) {
450  mphi = nphi;
451  mtheta = ntheta;
452  }
453  else {
454  mphi = 1;
455  mtheta = 1;
456  ntheta = nphi;
457  }
458 
459  // Initialise status code and statistics
460  //int status = 0;
461  //int n_invalid = 0;
462 
463  // Do phi dependence
464  const double* phip = phi;
465  int rowoff = 0;
466  int rowlen = nphi * sxy;
467  for (int iphi = 0; iphi < nphi; ++iphi, rowoff += sxy, phip += spt) {
468  double sinphi;
469  double cosphi;
470  gammalib::sincosd(*phip, &sinphi, &cosphi);
471  double* xp = x + rowoff;
472  double* yp = y + rowoff;
473  for (int itheta = 0; itheta < mtheta; ++itheta) {
474  *xp = sinphi;
475  *yp = cosphi;
476  xp += rowlen;
477  yp += rowlen;
478  }
479  }
480 
481  // Do theta dependence
482  const double* thetap = theta;
483  double* xp = x;
484  double* yp = y;
485  int* statp = stat;
486  for (int itheta = 0; itheta < ntheta; ++itheta, thetap += spt) {
487 
488  // Compute r
489  double r = m_w[0] * (90.0 - *thetap);
490 
491  // Do phi dependence
492  for (int iphi = 0; iphi < mphi; ++iphi, xp += sxy, yp += sxy) {
493  *xp = r * (*xp) - m_x0;
494  *yp = -r * (*yp) - m_y0;
495  *(statp++) = 0;
496  } // endfor: phi
497 
498  } // endfor: theta
499 
500  // Check status code
501  //gammalib::check_prj_s2x_status(G_PRJ_S2X, status, n_invalid);
502 
503  // Return
504  return;
505 }
GWcsARC(void)
Void constructor.
Definition: GWcsARC.cpp:67
void init_members(void)
Initialise class members.
Definition: GWcs.cpp:962
Interface definition for the WCS registry class.
Zenithal/azimuthal equidistant (ARC) projection class definition.
Definition: GWcsARC.hpp:43
void prj_set(void) const
Setup of projection.
Definition: GWcsARC.cpp:295
void prj_off(const double &phi0, const double &theta0) const
Compute fiducial offset to force (x,y) = (0,0) at (phi0,theta0)
Definition: GWcs.cpp:3232
GWcsARC & operator=(const GWcsARC &wcs)
Assignment operator.
Definition: GWcsARC.cpp:149
Zenithal/azimuthal equidistant (ARC) projection class definition.
virtual ~GWcsARC(void)
Destructor.
Definition: GWcsARC.cpp:127
Gammalib tools definition.
void free_members(void)
Delete class members.
Definition: GWcs.cpp:1066
virtual void clear(void)
Clear instance.
Definition: GWcsARC.cpp:184
void prj_x2s(int nx, int ny, int sxy, int spt, const double *x, const double *y, double *phi, double *theta, int *stat) const
Pixel-to-spherical deprojection.
Definition: GWcsARC.cpp:346
World Coordinate Projection registry class interface definition.
const double deg2rad
Definition: GMath.hpp:43
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition: GVector.cpp:1358
void wcs_set(void) const
Setup of World Coordinate System.
Definition: GWcs.cpp:1262
virtual GWcsARC * clone(void) const
Clone instance.
Definition: GWcsARC.cpp:206
void sincosd(const double &angle, double *s, double *c)
Compute sine and cosine of angle in degrees.
Definition: GMath.cpp:342
virtual GWcs & operator=(const GWcs &wcs)
Assignment operator.
Definition: GWcs.cpp:180
double m_x0
Fiducial x offset.
Definition: GWcs.hpp:215
GChatter
Definition: GTypemaps.hpp:33
virtual std::string print(const GChatter &chatter=NORMAL) const
Print WCS information.
Definition: GWcsARC.cpp:219
std::string wcs_print(const GChatter &chatter=NORMAL) const
Print WCS information.
Definition: GWcs.cpp:1642
void prj_s2x(int nphi, int ntheta, int spt, int sxy, const double *phi, const double *theta, double *x, double *y, int *stat) const
Generic spherical-to-pixel projection.
Definition: GWcsARC.cpp:437
void free_members(void)
Delete class members.
std::vector< double > m_w
Intermediate values.
Definition: GWcs.hpp:217
Abstract world coordinate system base class.
Definition: GWcs.hpp:51
int prj_bchk(const double &tol, const int &nphi, const int &ntheta, const int &spt, double *phi, double *theta, int *stat) const
Performs bounds checking on native spherical coordinates.
Definition: GWcs.cpp:3281
double m_y0
Fiducial y offset.
Definition: GWcs.hpp:216
double atan2d(const double &y, const double &x)
Compute arc tangens in degrees.
Definition: GMath.cpp:308
void check_prj_x2s_status(const std::string &origin, const int &status, const int &number)
Checks status of GWcs::prj_x2s method.
Definition: GException.cpp:401
Exception handler interface definition.
bool m_prjset
Projection is set.
Definition: GWcs.hpp:211
void init_members(void)
Initialise class members.
void init_members(void)
Initialise class members.
Definition: GWcsARC.cpp:249
const GWcsARC g_wcs_arc_seed
Definition: GWcsARC.cpp:54
const double rad2deg
Definition: GMath.hpp:44
void free_members(void)
Delete class members.
Definition: GWcsARC.cpp:271
#define G_PRJ_X2S
Definition: GWcsARC.cpp:38
void copy_members(const GWcsARC &wcs)
Copy class members.
Definition: GWcsARC.cpp:261
double m_r0
Radius of the generating sphere.
Definition: GWcs.hpp:212
Mathematical function definitions.