]> git.mxchange.org Git - simgear.git/blob - simgear/magvar/magvar.cxx
Added gdbm to SimGear. Many systems will already have gdbm installed so
[simgear.git] / simgear / magvar / magvar.cxx
1 // magvar.cxx -- compute local magnetic variation given position,
2 //               altitude, and date
3 //
4 // This is an implimentation of the NIMA WMM 2000
5 //
6 //    http://www.nima.mil/GandG/ngdc-wmm2000.html
7 //
8 // Copyright (C) 2000  Edward A Williams <Ed_Williams@compuserve.com>
9 //
10 // Adapted from Excel 3.0 version 3/27/94 EAW
11 // Recoded in C++ by Starry Chan
12 // WMM95 added and rearranged in ANSI-C EAW 7/9/95
13 // Put shell around program and made Borland & GCC compatible EAW 11/22/95
14 // IGRF95 added 2/96 EAW
15 // WMM2000 IGR2000 added 2/00 EAW
16 // Released under GPL 3/26/00 EAW
17 // Adaptions and modifications for the SimGear project  3/27/2000 CLO
18 //
19 // Removed all pow() calls and made static roots[][] arrays to
20 // save many sqrt() calls on subsequent invocations
21 // left old code as SGMagVarOrig() for testing purposes
22 // 3/28/2000  Norman Vine -- nhv@yahoo.com
23 //
24 // This program is free software; you can redistribute it and/or
25 // modify it under the terms of the GNU General Public License as
26 // published by the Free Software Foundation; either version 2 of the
27 // License, or (at your option) any later version.
28 //
29 // This program is distributed in the hope that it will be useful, but
30 // WITHOUT ANY WARRANTY; without even the implied warranty of
31 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32 // General Public License for more details.
33 //
34 // You should have received a copy of the GNU General Public License
35 // along with this program; if not, write to the Free Software
36 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 //
38 // $Id$
39
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <math.h>
44
45 #include "magvar.hxx"
46
47
48 #define max(a,b)        (((a) > (b)) ? (a) : (b))
49
50 static const double pi = 3.14159265358979;
51 static const double a = 6378.16;        /* major radius (km) IAU66 ellipsoid */
52 static const double f = 1.0 / 298.25;   /* inverse flattening IAU66 ellipsoid */
53 static const double b = 6378.16 * (1.0 -1.0 / 298.25 );
54 /* minor radius b=a*(1-f) */
55 static const double r_0 = 6371.2;       /* "mean radius" for spherical harmonic expansion */
56
57 static double gnm_wmm2000[13][13] =
58 {
59     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
60     {-29616.0, -1722.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
61     {-2266.7, 3070.2, 1677.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
62     {1322.4, -2291.5, 1255.9, 724.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
63     {932.1, 786.3, 250.6, -401.5, 106.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
64     {-211.9, 351.6, 220.8, -134.5, -168.8, -13.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
65     {73.8, 68.2, 74.1, -163.5, -3.8, 17.1, -85.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
66     {77.4, -73.9, 2.2, 35.7, 7.3, 5.2, 8.4, -1.5, 0.0, 0.0, 0.0, 0.0, 0.0},
67     {23.3, 7.3, -8.5, -6.6, -16.9, 8.6, 4.9, -7.8, -7.6, 0.0, 0.0, 0.0, 0.0},
68     {5.7, 8.5, 2.0, -9.8, 7.6, -7.0, -2.0, 9.2, -2.2, -6.6, 0.0, 0.0, 0.0},
69     {-2.2, -5.7, 1.6, -3.7, -0.6, 4.1, 2.2, 2.2, 4.6, 2.3, 0.1, 0.0, 0.0},
70     {3.3, -1.1, -2.4, 2.6, -1.3, -1.7, -0.6, 0.4, 0.7, -0.3, 2.3, 4.2, 0.0},
71     {-1.5, -0.2, -0.3, 0.5, 0.2, 0.9, -1.4, 0.6, -0.6, -1.0, -0.3, 0.3, 0.4},
72 };
73
74 static double hnm_wmm2000[13][13]=
75 {
76     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
77     {0.0, 5194.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
78     {0.0, -2484.8, -467.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
79     {0.0, -224.7, 293.0, -486.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
80     {0.0, 273.3, -227.9, 120.9, -302.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
81     {0.0, 42.0, 173.8, -135.0, -38.6, 105.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
82     {0.0, -17.4, 61.2, 63.2, -62.9, 0.2, 43.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
83     {0.0, -62.3, -24.5, 8.9, 23.4, 15.0, -27.6, -7.8, 0.0, 0.0, 0.0, 0.0, 0.0},
84     {0.0, 12.4, -20.8, 8.4, -21.2, 15.5, 9.1, -15.5, -5.4, 0.0, 0.0, 0.0, 0.0},
85     {0.0, -20.4, 13.9, 12.0, -6.2, -8.6, 9.4, 5.0, -8.4, 3.2, 0.0, 0.0, 0.0},
86     {0.0, 0.9, -0.7, 3.9, 4.8, -5.3, -1.0, -2.4, 1.3, -2.3, -6.4, 0.0, 0.0},
87     {0.0, -1.5, 0.7, -1.1, -2.3, 1.3, -0.6, -2.8, -1.6, -0.1, -1.9, 1.4, 0.0},
88     {0.0, -1.0, 0.7, 2.2, -2.5, -0.2, 0.0, -0.2, 0.0, 0.2, -0.9, -0.2, 1.0},
89 };
90
91 static double gtnm_wmm2000[13][13]=
92 {
93     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
94     {14.7, 11.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
95     {-13.6, -0.7, -1.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
96     {0.3, -4.3, 0.9, -8.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
97     {-1.6, 0.9, -7.6, 2.2, -3.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
98     {-0.9, -0.2, -2.5, -2.7, -0.9, 1.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
99     {1.2, 0.2, 1.7, 1.6, -0.1, -0.3, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
100     {-0.4, -0.8, -0.2, 1.1, 0.4, 0.0, -0.2, -0.2, 0.0, 0.0, 0.0, 0.0, 0.0},
101     {-0.3, 0.6, -0.8, 0.3, -0.2, 0.5, 0.0, -0.6, 0.1, 0.0, 0.0, 0.0, 0.0},
102     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
103     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
104     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
105     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
106 };
107
108 static double htnm_wmm2000[13][13]=
109 {
110     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
111     {0.0, -20.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
112     {0.0, -21.5, -9.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
113     {0.0, 6.4, -1.3, -13.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
114     {0.0, 2.3, 0.7, 3.7, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
115     {0.0, 0.0, 2.1, 2.3, 3.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
116     {0.0, -0.3, -1.7, -0.9, -1.0, -0.1, 1.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
117     {0.0, 1.4, 0.2, 0.7, 0.4, -0.3, -0.8, -0.1, 0.0, 0.0, 0.0, 0.0, 0.0},
118     {0.0, -0.5, 0.1, -0.2, 0.0, 0.1, -0.1, 0.3, 0.2, 0.0, 0.0, 0.0, 0.0},
119     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
120     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
121     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
122     {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
123 };
124
125 static const int nmax = 12;
126
127 static double P[13][13];
128 static double DP[13][13];
129 static double gnm[13][13];
130 static double hnm[13][13];
131 static double sm[13];
132 static double cm[13];
133
134 static double root[13];
135 static double roots[13][13][2];
136
137 /* Convert date to Julian day    1950-2049 */
138 unsigned long int yymmdd_to_julian_days( int yy, int mm, int dd )
139 {
140     unsigned long jd;
141  
142     yy = (yy < 50) ? (2000 + yy) : (1900 + yy);
143     jd = dd - 32075L + 1461L * (yy + 4800L + (mm - 14) / 12 ) / 4;
144     jd = jd + 367L * (mm - 2 - (mm - 14) / 12*12) / 12;
145     jd = jd - 3 * ((yy + 4900L + (mm - 14) / 12) / 100) / 4;
146
147     /* printf("julian date = %d\n", jd ); */
148     return jd;
149
150
151
152 /* Convert degrees to radians */
153 double deg_to_rad( double deg )
154 {
155     return deg*pi/180.;
156 }
157
158
159 /* Convert radians to degrees */
160 double rad_to_deg( double rad )
161 {
162     return rad*180./pi;
163 }
164              
165
166 /*
167  * return variation (in radians) given geodetic latitude (radians),
168  * longitude(radians), height (km) and (Julian) date
169  * N and E lat and long are positive, S and W negative
170 */
171
172 double SGMagVar( double lat, double lon, double h, long dat, double* field )
173 {
174     /* output field B_r,B_th,B_phi,B_x,B_y,B_z */
175     int n,m;
176     /* reference dates */
177     long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
178
179     double yearfrac,sr,r,theta,c,s,psi,fn,fn_0,B_r,B_theta,B_phi,X,Y,Z;
180     double sinpsi, cospsi, inv_s;
181
182     static int been_here = 0;
183         
184     double sinlat = sin(lat);
185     double coslat = cos(lat);
186
187     /* convert to geocentric coords: */
188     // sr = sqrt(pow(a*coslat,2.0)+pow(b*sinlat,2.0));
189     sr = sqrt(a*a*coslat*coslat + b*b*sinlat*sinlat);
190     /* sr is effective radius */
191     theta = atan2(coslat * (h*sr + a*a),
192                   sinlat * (h*sr + b*b));
193     /* theta is geocentric co-latitude */
194
195     r = h*h + 2.0*h * sr +
196         (a*a*a*a - ( a*a*a*a - b*b*b*b ) * sinlat*sinlat ) / 
197         (a*a - (a*a - b*b) * sinlat*sinlat );
198
199     r = sqrt(r);
200
201     /* r is geocentric radial distance */
202     c = cos(theta);
203     s = sin(theta);
204     inv_s = 1.0 / s;
205
206     /*zero out arrays */
207     for ( n = 0; n <= nmax; n++ ) {
208         for ( m = 0; m <= n; m++ ) {
209             P[n][m] = 0;
210             DP[n][m] = 0;
211         }
212     }
213
214     /* diagonal elements */
215     P[0][0] = 1;
216     P[1][1] = s;
217     DP[0][0] = 0;
218     DP[1][1] = c;
219     P[1][0] = c ;
220     DP[1][0] = -s;
221
222     // these values will not change for subsequent function calls
223     if( !been_here ) {
224         for ( n = 2; n <= nmax; n++ ) {
225             root[n] = sqrt((2.0*n-1) / (2.0*n));
226         }
227         
228         for ( m = 0; m <= nmax; m++ ) {
229             double mm = m*m;
230             for ( n = max(m + 1, 2); n <= nmax; n++ ) {
231                 roots[m][n][0] = sqrt((n-1)*(n-1) - mm);
232                 roots[m][n][1] = 1.0 / sqrt( n*n - mm);
233             }
234         }
235         been_here = 1;
236     }
237         
238     for ( n=2; n <= nmax; n++ ) {
239         // double root = sqrt((2.0*n-1) / (2.0*n));
240         P[n][n] = P[n-1][n-1] * s * root[n];
241         DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
242             root[n];
243     }
244
245     /* lower triangle */
246     for ( m = 0; m <= nmax; m++ ) {
247         // double mm = m*m;
248         for ( n = max(m + 1, 2); n <= nmax; n++ ) {
249             // double root1 = sqrt((n-1)*(n-1) - mm);
250             // double root2 = 1.0 / sqrt( n*n - mm);
251             P[n][m] = (P[n-1][m] * c * (2.0*n-1) -
252                        P[n-2][m] * roots[m][n][0]) *
253                 roots[m][n][1];
254                                   
255             DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
256                         (2.0*n-1) - DP[n-2][m] * roots[m][n][0]) *
257                 roots[m][n][1];
258         }
259     }
260
261     /* compute gnm, hnm at dat */
262     /* WMM2000 */
263     yearfrac = (dat - date0_wmm2000) / 365.25;
264     for ( n = 1; n <= nmax; n++ ) {
265         for ( m = 0; m <= nmax; m++ ) {
266             gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
267             hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
268         }
269     }
270
271     /* compute sm (sin(m lon) and cm (cos(m lon)) */
272     for ( m = 0; m <= nmax; m++ ) {
273         sm[m] = sin(m * lon);
274         cm[m] = cos(m * lon);
275     }
276
277     /* compute B fields */
278     B_r = 0.0;
279     B_theta = 0.0;
280     B_phi = 0.0;
281     fn_0 = r_0/r;
282     fn = fn_0 * fn_0;
283         
284     for ( n = 1; n <= nmax; n++ ) {
285         double c1_n=0;
286         double c2_n=0;
287         double c3_n=0;
288         for ( m = 0; m <= n; m++ ) {
289             double tmp = (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]); 
290             c1_n=c1_n + tmp * P[n][m];
291             c2_n=c2_n + tmp * DP[n][m];
292             c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
293         }
294         // fn=pow(r_0/r,n+2.0);
295         fn *= fn_0;
296         B_r = B_r + (n + 1) * c1_n * fn;
297         B_theta = B_theta - c2_n * fn;
298         B_phi = B_phi + c3_n * fn * inv_s;
299     }
300
301     /* Find geodetic field components: */
302     psi = theta - ((pi / 2.0) - lat);
303     sinpsi = sin(psi);
304     cospsi = cos(psi);
305     X = -B_theta * cospsi - B_r * sinpsi;
306     Y = B_phi;
307     Z = B_theta * sinpsi - B_r * cospsi;
308
309     field[0]=B_r;
310     field[1]=B_theta;
311     field[2]=B_phi;
312     field[3]=X;
313     field[4]=Y;
314     field[5]=Z;   /* output fields */
315
316     /* find variation, leave in radians! */
317     return atan2(Y, X);  /* E is positive */
318 }
319
320
321 #ifdef TEST_NHV_HACKS
322 double SGMagVarOrig( double lat, double lon, double h, long dat, double* field )
323 {
324     /* output field B_r,B_th,B_phi,B_x,B_y,B_z */
325     int n,m;
326     /* reference dates */
327     long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
328
329     double yearfrac,sr,r,theta,c,s,psi,fn,B_r,B_theta,B_phi,X,Y,Z;
330
331     /* convert to geocentric coords: */
332     sr = sqrt(pow(a*cos(lat),2.0)+pow(b*sin(lat),2.0));
333     /* sr is effective radius */
334     theta = atan2(cos(lat) * (h * sr + a * a),
335                   sin(lat) * (h * sr + b * b));
336     /* theta is geocentric co-latitude */
337
338     r = h * h + 2.0*h * sr +
339         (pow(a,4.0) - (pow(a,4.0) - pow(b,4.0)) * pow(sin(lat),2.0)) / 
340         (a * a - (a * a - b * b) * pow(sin(lat),2.0));
341
342     r = sqrt(r);
343
344         /* r is geocentric radial distance */
345     c = cos(theta);
346     s = sin(theta);
347
348         /*zero out arrays */
349     for ( n = 0; n <= nmax; n++ ) {
350         for ( m = 0; m <= n; m++ ) {
351             P[n][m] = 0;
352             DP[n][m] = 0;
353         }
354     }
355
356     /* diagonal elements */
357     P[0][0] = 1;
358     P[1][1] = s;
359     DP[0][0] = 0;
360     DP[1][1] = c;
361     P[1][0] = c ;
362     DP[1][0] = -s;
363
364     for ( n = 2; n <= nmax; n++ ) {
365         P[n][n] = P[n-1][n-1] * s * sqrt((2.0*n-1) / (2.0*n));
366         DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
367             sqrt((2.0*n-1) / (2.0*n));
368     }
369
370     /* lower triangle */
371     for ( m = 0; m <= nmax; m++ ) {
372         for ( n = max(m + 1, 2); n <= nmax; n++ ) {
373             P[n][m] = (P[n-1][m] * c * (2.0*n-1) - P[n-2][m] *
374                        sqrt(1.0*(n-1)*(n-1) - m * m)) /
375                 sqrt(1.0* n * n - m * m);
376             DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
377                         (2.0*n-1) - DP[n-2][m] *
378                         sqrt(1.0*(n-1) * (n-1) - m * m)) /
379                 sqrt(1.0* n * n - m * m);
380         }
381     }
382
383     /* compute gnm, hnm at dat */
384     /* WMM2000 */
385     yearfrac = (dat - date0_wmm2000) / 365.25;
386     for ( n = 1; n <= nmax; n++ ) {
387         for ( m = 0; m <= nmax; m++ ) {
388             gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
389             hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
390         }
391     }
392
393     /* compute sm (sin(m lon) and cm (cos(m lon)) */
394     for ( m = 0; m <= nmax; m++ ) {
395         sm[m] = sin(m * lon);
396         cm[m] = cos(m * lon);
397     }
398
399     /* compute B fields */
400     B_r = 0.0;
401     B_theta = 0.0;
402     B_phi = 0.0;
403
404     for ( n = 1; n <= nmax; n++ ) {
405         double c1_n=0;
406         double c2_n=0;
407         double c3_n=0;
408         for ( m = 0; m <= n; m++ ) {
409             c1_n=c1_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * P[n][m];
410             c2_n=c2_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * DP[n][m];
411             c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
412         }
413         fn=pow(r_0/r,n+2.0);
414         B_r = B_r + (n + 1) * c1_n * fn;
415         B_theta = B_theta - c2_n * fn;
416         B_phi = B_phi + c3_n * fn / s;
417     }
418
419     /* Find geodetic field components: */
420     psi = theta - (pi / 2.0 - lat);
421     X = -B_theta * cos(psi) - B_r * sin(psi);
422     Y = B_phi;
423     Z = B_theta * sin(psi) - B_r * cos(psi);
424
425     field[0]=B_r;
426     field[1]=B_theta;
427     field[2]=B_phi;
428     field[3]=X;
429     field[4]=Y;
430     field[5]=Z;   /* output fields */
431
432         /* find variation, leave in radians! */
433     return atan2(Y, X);  /* E is positive */
434 }
435 #endif // TEST_NHV_HACKS