File: wcs.c

package info (click to toggle)
fitsh 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,768 kB
  • ctags: 4,050
  • sloc: ansic: 53,352; makefile: 1,120; sh: 25
file content (151 lines) | stat: -rw-r--r-- 3,362 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*****************************************************************************/
/* wcs.c								     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Some functions related to sky coordinate conversions & projections	     */
/*****************************************************************************/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include "wcs.h"

/*****************************************************************************/

int wcs_get_projection_matrix(double ra0,double de0,matrix mproj)
{
 double	sr0,sd0,cr0,cd0;

 ra0=M_D2R*ra0;
 de0=M_D2R*de0;

 sr0=sin(ra0);
 cr0=cos(ra0);
 sd0=sin(de0);
 cd0=cos(de0);

 mproj[0][0]=+sr0    ,mproj[0][1]=-cr0    ,mproj[0][2]=0.0;
 mproj[1][0]=-sd0*cr0,mproj[1][1]=-sd0*sr0,mproj[1][2]=+cd0;
 mproj[2][0]=-cd0*cr0,mproj[2][1]=-cd0*sr0,mproj[2][2]=-sd0;

 return(0);
}

int wcs_get_projected_coords_matrix(matrix mproj,double ra,double de,double *rx,double *ry,double *rz)
{
 double	sr,sd,cr,cd,x,y,z;

 ra=M_D2R*ra;
 de=M_D2R*de;

 sr=sin(ra);
 cr=cos(ra);
 sd=sin(de);
 cd=cos(de);

 x=cd*cr;
 y=cd*sr;
 z=sd;

 *rx=mproj[0][0]*x+mproj[0][1]*y+mproj[0][2]*z;
 *ry=mproj[1][0]*x+mproj[1][1]*y+mproj[1][2]*z;
 *rz=mproj[2][0]*x+mproj[2][1]*y+mproj[2][2]*z;
 
 return(0);
}

int wcs_invert_projected_coords_matrix(matrix mproj,double x,double y,double *rra,double *rde)
{
 double	z,px,py,pz;

 z=1.0-x*x-y*y;
 if ( z<0.0 )	return(1);
 else		z=-sqrt(z);

 px=mproj[0][0]*x+mproj[1][0]*y+mproj[2][0]*z;
 py=mproj[0][1]*x+mproj[1][1]*y+mproj[2][1]*z;
 pz=mproj[0][2]*x+mproj[1][2]*y+mproj[2][2]*z;

 *rde=asin(pz)*M_R2D;
 *rra=atan2(py,px)*M_R2D;
 if ( *rra<0.0 )	*rra+=360.0;
 
 return(0);
}

int wcs_project_distort(int type,double *rx,double *ry,double *rz)
{
 double	d,m;
 switch ( type )
  {   case WCS_TAN:	/* gnomonic projection				*/
 	m=1.0/sqrt(1-(*rx)*(*rx)-(*ry)*(*ry));
	*rx=(*rx)*m;
	*ry=(*ry)*m;
	break;
     case WCS_ARC:	/* arc projection				*/
	d=sqrt((*rx)*(*rx)+(*ry)*(*ry));
	if ( d>0.0 && *rz < 0.0 )	m=asin(d)/d;
	else if ( d>0.0 )		m=(M_PI-asin(d))/d;
	else				m=1.0;
	*rx=(*rx)*m;
	*ry=(*ry)*m;
	break;
     case WCS_SIN:	/* orthographic: do nothing, successfully	*/
	break;
     default:		/* unknown projection 				*/
	return(1);
	break;
 }
 return(0);
}

int wcs_invert_project_distort(int type,double *rx,double *ry,double *rz)
{
 double	d,m;
 switch ( type )
  {   case WCS_TAN:	/* gnomonic projection				*/
 	m=1.0/sqrt(1+(*rx)*(*rx)+(*ry)*(*ry));
	*rx=(*rx)*m;
	*ry=(*ry)*m;
	break;
     case WCS_ARC:	/* arc projection				*/
	d=sqrt((*rx)*(*rx)+(*ry)*(*ry));
	if ( d>0.0 )	m=sin(d)/d;
	else		m=1.0;
	*rx=(*rx)*m;
	*ry=(*ry)*m;
	break;
     case WCS_SIN:	/* orthographic: do nothing, successfully	*/
	break;
     default:		/* unknown projection 				*/
	return(1);
	break;
 }
 return(0);
}

int wcs_get_projected_coords(double ra,double de,double ra0,double de0,double *rx,double *ry,double *rz)
{
 double	sinda,cosda,sind0,sind,cosd0,cosd;

 ra =M_D2R*(ra-ra0);
 de0=M_D2R*de0;
 de =M_D2R*de;

 sinda=sin(ra);
 cosda=cos(ra);
 sind0=sin(de0);
 cosd0=cos(de0);
 sind =sin(de);
 cosd =cos(de);

 *rx=+cosd*sinda;
 *ry=-sind0*cosd*cosda+cosd0*sind;
 *rz=+cosd0*cosd*cosda+sind0*sind;

 return(0);
}

/*****************************************************************************/