source: Sophya/trunk/SophyaExt/XAstroPack/mollweide.cc@ 1812

Last change on this file since 1812 was 1811, checked in by cmv, 24 years ago

projection mollweide cmv 13/12/01

File size: 6.1 KB
RevLine 
[1811]1//#define TEST_MOLLWEIDE
2#include <stdlib.h>
3#include <stdio.h>
4#include <math.h>
5#include "mollweide.h"
6
7#if defined(TEST_MOLLWEIDE)
8#include "timing.h"
9double _t;
10#endif
11
12/*! \ingroup XAstroPack
13 \brief La vraie projection Mollweide (longitude,latitude) -> (xmoll,ymoll)
14 \param input: longitude [0,2PI[ en radians
15 \param input: latitude [-Pi/2,Pi/2] en radians
16 \param output: xmoll dans [-2*sqrt(2),2*sqrt(2)]
17 \param output: ymoll dans [-sqrt(2),sqrt(2)]
18 \warning Probleme de precision pour t pres de Pi/2 (latitude pres de -Pi/2 ou Pi/2)
19 \return t
20 \verbatim
21 - La vraie MollWeide:
22 Equation implicite en t: 2*t+sin(2*t) = Pi*sin(lat) = a
23 | xmoll = 2*sqrt(2)*(lon-Pi)/Pi*cos(t)
24 | ymoll = sqrt(2)*sin(t)
25 --------------------------------------------------------
26 INPUT: longitude [0,2PI[ en radians
27 latitude [-Pi/2,Pi/2] en radians
28 OUTPUT: xmoll dans [-2*sqrt(2),2*sqrt(2)]
29 ymoll dans [-sqrt(2),sqrt(2)]
30 RETURN: t
31 --------------------------------------------------------
32 REMARQUES:
33 - xmoll varie [-2*sqrt(2),2*sqrt(2)]
34 centre galactique au milieu, anti-centre au bord
35 pour centre galactique au bord, anti-centre au milieu:
36 xmoll' = 2*sqrt(2)*longitude/Pi*cos(t)
37 xmoll' varie [0,4*sqrt(2)]
38 - ymoll varie [-sqrt(2),sqrt(2)] plan gal en 0
39 - t varie [-Pi/2,Pi/2]
40 - a varie [-Pi,Pi] (a <-> -a <==> t <-> -t)
41 - Pour latitude --> -latitude alors t --> -t
42 latitude=90 --> t=Pi/2
43 latitude=-90 --> t=-Pi/2
44 RAPIDITE: 10^6 appels a MollWeide_XY en 5.2 seconde (PC Linux 500Mhz)
45 PRECISION: eps=1e-16 -> |t-tvrai|<1e-6 toujours
46 |t-tvrai|<1e-10 pour t<1.57
47 |t-tvrai|<1e-10 pour t<1.57
48 |t-tvrai|<3e-14 pour t<1.50
49 |t-tvrai|<5e-15 pour t<1.40
50 \endverbatim
51*/
52double MollWeide_XY(double longitude,double latitude,double* xmoll,double*ymoll)
53{
54 int i,niter=2;
55 double tini,t0,f0,f1,f2,e1,e2,dt,a,asgn,eps=1.e-16;
56
57 a = M_PI*sin(latitude);
58 if(a<0.) {a*=-1.; asgn=-1.;} else asgn=1.;
59 // premier guess t1 ok pour t->0 (droite)
60 f1 = a/4.;
61 // second guess t2 ok pour t->Pi/2 (DL en Pi/2, type a+b*x^3)
62 f2 = 0.75*(M_PI-a);
63 if(f2>0.) f2=M_PI_2-pow(f2,0.33333333333333333); else f2=M_PI_2;
64 e1 = 2.*f1+sin(2.*f1)-a; if(e1<0.) e1 *= -1.;
65 e2 = 2.*f2+sin(2.*f2)-a; if(e2<0.) e2 *= -1.;
66 tini = (f1*e2+f2*e1)/(e1+e2);
67 //printf("---> a=%e tini=%e f1=%e f2=%e\n",a,tini,f1,f2);
68 if(tini<0) tini=0.; else if(tini>M_PI_2) tini=M_PI_2;
69 // L'approximation par le DL au 2sd ordre, on resoud:
70 // a = f(x0) + f'(x0)*(x-x0) + f''(x0)*(x-x0)^2/2
71 t0 = tini;
72 for(i=0;i<niter;i++) {
73 e1 = sin(2.*t0);
74 f0 = 2.*t0+e1; // (f(x0)) >=0 sur 0<t<Pi/2
75 //printf(".%d a-f0=%e\n",i,a-f0);
76 if(a-f0<eps && a-f0>-eps) break;
77 f1 = 2.*(1.+cos(2.*t0)); // (f'(x0)) >=0 sur 0<t<Pi/2
78 f2 = -2.*e1; // (f''(x0)/2) <=0 sur 0<t<Pi/2
79 dt = f1*f1 - 4.*(f0-a)*f2;
80 //printf(".%d f0=%e f1=%e f2=%e disc=%e\n",i,f0,f1,f2,dt);
81 if(dt>=0.) dt=-f1+sqrt(dt); else dt=0.;
82 ////printf(".%d -b+sqrt(disc)=%e\n",i,dt);
83 if(f2<-eps && (dt>eps || dt<-eps)) { // approx parabolique
84 t0 += dt/(2.*f2);
85 //printf(".%d parab t0=%e\n",i,t0);
86 } else if(f1>eps) {
87 t0 += (a-f0)/f1; // approx lineaire
88 //printf(".%d lin t0=%e\n",i,t0);
89 }
90 if(t0<0) t0=0.; else if(t0>M_PI_2) t0=M_PI_2;
91 }
92
93 t0 *= asgn;
94 *xmoll = 2.*M_SQRT2*(longitude-M_PI)/M_PI*cos(t0);
95 *ymoll = M_SQRT2*sin(t0);
96
97 #if defined(TEST_MOLLWEIDE)
98 _t = tini;
99 #endif
100
101 return t0;
102}
103
104/*! \ingroup XAstroPack
105 \brief La vraie projection Mollweide (xmoll,ymoll) -> (longitude,latitude)
106 \param input: xmoll dans [-2*sqrt(2),2*sqrt(2)]
107 \param input: ymoll dans [-sqrt(2),sqrt(2)]
108 \param output: longitude [0,2PI[ en radians
109 \param output: latitude [-Pi/2,Pi/2] en radians
110 \return t
111 \warning Probleme de precision pour t pres de Pi/2 (latitude pres de -Pi/2 ou Pi/2)
112 \verbatim
113 --------------------------------------------------------
114 INPUT: xmoll dans [-2*sqrt(2),2*sqrt(2)]
115 ymoll dans [-sqrt(2),sqrt(2)]
116 OUTPUT: longitude [0,2PI[ en radians
117 latitude [-Pi/2,Pi/2] en radians
118 RETURN: t if OK
119 -991 : xmoll hors limites
120 -992 : ymoll hors limites
121 -993 : non inversible (poles de la sphere)
122 --------------------------------------------------------
123 \endverbatim
124*/
125double MollWeide_LL(double xmoll,double ymoll,double* longitude,double* latitude)
126{
127 double t,a,eps=1.e-20;
128
129 *longitude = *latitude = -999.;
130 if(xmoll<-2*M_SQRT2 || xmoll>2*M_SQRT2) return(-901.);
131 if(ymoll<-M_SQRT2 || ymoll>M_SQRT2) return(-902.);
132
133 t = ymoll/M_SQRT2; if(t<-1.) t=-1.; else if(t>1.) t=1.;
134 t = asin(t);
135
136 a = (2.*t+sin(2.*t))/M_PI; if(a<-1.) a=-1.; else if(a>1.) a=1.;
137 *latitude = asin(a);
138
139 a= cos(t);
140 if(fabs(a)<eps) return(-903.);
141 *longitude = M_PI*(xmoll/(2.*M_SQRT2*a)+1.);
142
143 return(t);
144}
145
146///////////////////////////////////////
147////////// Programme de test //////////
148///////////////////////////////////////
149#if defined(TEST_MOLLWEIDE)
150int main()
151{
152 FILE *file = fopen("mollw.data","w");
153 FILE *file2 = fopen("mollw2.data","w");
154 int i,npoints=250000;
155 double tvrai,lon,lat,x,y,tcalc,a;
156 double longitude,latitude,tll,txy;
157 InitTim();
158
159 tll=txy=0.;
160 for(i=0;i<=npoints;i++) {
161 //tvrai = 1.57078;
162 tvrai = (double)i*(M_PI_2) /(double)npoints;
163 a = 2.*tvrai+sin(2.*tvrai);
164 lat = asin(a/M_PI);
165 tcalc = MollWeide_XY(0.,lat,&x,&y);
166 if(fabs(tcalc-tvrai)>tll) {tll = fabs(tcalc-tvrai); txy=tvrai;}
167 if(file) fprintf(file,"%e %e %e %e %e\n",tvrai,tcalc,_t,a,tcalc-tvrai);
168 }
169 printf("Maximum difference: %e for t=%.10e=Pi/2-%.10e\n",tll,txy,M_PI_2-txy);
170 if(file) fclose(file);
171 PrtTim("--- End of first testing ---");
172
173 for(lon=0.;lon<360.;lon+=2.5) for(lat=-90.;lat<=90.1;lat+=2.5) {
174 txy = MollWeide_XY(lon*M_PI/180.,lat*M_PI/180.,&x,&y);
175 tll = MollWeide_LL(x,y,&longitude,&latitude);
176 longitude *= 180./M_PI;
177 latitude *= 180./M_PI;
178 if(file2) fprintf(file2,"%e %e %e %e %e %e %e %e %e %e\n"
179 ,lon,lat,x,y,longitude,latitude,txy,tll,longitude-lon,latitude-lat);
180 }
181 if(file2) fclose(file2);
182 PrtTim("--- End of second testing ---");
183 return 0;
184}
185#endif
Note: See TracBrowser for help on using the repository browser.