00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 #include <stdio.h>
00081 #include <stdlib.h>
00082 #include <math.h>
00083
00084 #include <simgear/constants.h>
00085 #include <simgear/sg_inlines.h>
00086
00087 #include "coremag.hxx"
00088
00089 static const double a = 6378.137;
00090 static const double b = 6356.7523142;
00091 static const double r_0 = 6371.2;
00092
00093 static double gnm_wmm2005[13][13] =
00094 {
00095 {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},
00096 {-29556.8, -1671.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00097 {-2340.6, 3046.9, 1657.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00098 {1335.4, -2305.1, 1246.7, 674.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00099 {919.8, 798.1, 211.3, -379.4, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00100 {-227.4, 354.6, 208.7, -136.5, -168.3, -14.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00101 {73.2, 69.7, 76.7, -151.2, -14.9, 14.6, -86.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00102 {80.1, -74.5, -1.4, 38.5, 12.4, 9.5, 5.7, 1.8, 0.0, 0.0, 0.0, 0.0, 0.0},
00103 {24.9, 7.7, -11.6, -6.9, -18.2, 10.0, 9.2, -11.6, -5.2, 0.0, 0.0, 0.0, 0.0},
00104 {5.6, 9.9, 3.5, -7.0, 5.1, -10.8, -1.3, 8.8, -6.7, -9.1, 0.0, 0.0, 0.0},
00105 {-2.3, -6.3, 1.6, -2.6, 0.0, 3.1, 0.4, 2.1, 3.9, -0.1, -2.3, 0.0, 0.0},
00106 {2.8, -1.6, -1.7, 1.7, -0.1, 0.1, -0.7, 0.7, 1.8, 0.0, 1.1, 4.1, 0.0},
00107 {-2.4, -0.4, 0.2, 0.8, -0.3, 1.1, -0.5, 0.4, -0.3, -0.3, -0.1, -0.3, -0.1},
00108 };
00109
00110 static double hnm_wmm2005[13][13]=
00111 {
00112 {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},
00113 {0.0, 5079.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00114 {0.0, -2594.7, -516.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00115 {0.0, -199.9, 269.3, -524.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00116 {0.0, 281.5, -226.0, 145.8, -304.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00117 {0.0, 42.4, 179.8, -123.0, -19.5, 103.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00118 {0.0, -20.3, 54.7, 63.6, -63.4, -0.1, 50.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00119 {0.0, -61.5, -22.4, 7.2, 25.4, 11.0, -26.4, -5.1, 0.0, 0.0, 0.0, 0.0, 0.0},
00120 {0.0, 11.2, -21.0, 9.6, -19.8, 16.1, 7.7, -12.9, -0.2, 0.0, 0.0, 0.0, 0.0},
00121 {0.0, -20.1, 12.9, 12.6, -6.7, -8.1, 8.0, 2.9, -7.9, 6.0, 0.0, 0.0, 0.0},
00122 {0.0, 2.4, 0.2, 4.4, 4.8, -6.5, -1.1, -3.4, -0.8, -2.3, -7.9, 0.0, 0.0},
00123 {0.0, 0.3, 1.2, -0.8, -2.5, 0.9, -0.6, -2.7, -0.9, -1.3, -2.0, -1.2, 0.0},
00124 {0.0, -0.4, 0.3, 2.4, -2.6, 0.6, 0.3, 0.0, 0.0, 0.3, -0.9, -0.4, 0.8},
00125 };
00126
00127 static double gtnm_wmm2005[13][13]=
00128 {
00129 {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},
00130 {8.0, 10.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00131 {-15.1, -7.8, -0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00132 {0.4, -2.6, -1.2, -6.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00133 {-2.5, 2.8, -7.0, 6.2, -3.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00134 {-2.8, 0.7, -3.2, -1.1, 0.1, -0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00135 {-0.7, 0.4, -0.3, 2.3, -2.1, -0.6, 1.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00136 {0.2, -0.1, -0.3, 1.1, 0.6, 0.5, -0.4, 0.6, 0.0, 0.0, 0.0, 0.0, 0.0},
00137 {0.1, 0.3, -0.4, 0.3, -0.3, 0.2, 0.4, -0.7, 0.4, 0.0, 0.0, 0.0, 0.0},
00138 {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},
00139 {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},
00140 {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},
00141 {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},
00142 };
00143
00144 static double htnm_wmm2005[13][13]=
00145 {
00146 {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},
00147 {0.0, -20.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00148 {0.0, -23.2, -14.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00149 {0.0, 5.0, -7.0, -0.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00150 {0.0, 2.2, 1.6, 5.8, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00151 {0.0, 0.0, 1.7, 2.1, 4.8, -1.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00152 {0.0, -0.6, -1.9, -0.4, -0.5, -0.3, 0.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
00153 {0.0, 0.6, 0.4, 0.2, 0.3, -0.8, -0.2, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0},
00154 {0.0, -0.2, 0.1, 0.3, 0.4, 0.1, -0.2, 0.4, 0.4, 0.0, 0.0, 0.0, 0.0},
00155 {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},
00156 {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},
00157 {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},
00158 {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},
00159 };
00160
00161 static const int nmax = 12;
00162
00163 static double P[13][13];
00164 static double DP[13][13];
00165 static double gnm[13][13];
00166 static double hnm[13][13];
00167 static double sm[13];
00168 static double cm[13];
00169
00170 static double root[13];
00171 static double roots[13][13][2];
00172
00173
00174 unsigned long int yymmdd_to_julian_days( int yy, int mm, int dd )
00175 {
00176 unsigned long jd;
00177
00178 yy = (yy < 50) ? (2000 + yy) : (1900 + yy);
00179 jd = dd - 32075L + 1461L * (yy + 4800L + (mm - 14) / 12 ) / 4;
00180 jd = jd + 367L * (mm - 2 - (mm - 14) / 12*12) / 12;
00181 jd = jd - 3 * ((yy + 4900L + (mm - 14) / 12) / 100) / 4;
00182
00183
00184 return jd;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194 double calc_magvar( double lat, double lon, double h, long dat, double* field )
00195 {
00196
00197 int n,m;
00198
00199 long date0_wmm2005 = yymmdd_to_julian_days(5,1,1);
00200
00201 double yearfrac,sr,r,theta,c,s,psi,fn,fn_0,B_r,B_theta,B_phi,X,Y,Z;
00202 double sinpsi, cospsi, inv_s;
00203
00204 static int been_here = 0;
00205
00206 double sinlat = sin(lat);
00207 double coslat = cos(lat);
00208
00209
00210
00211 sr = sqrt(a*a*coslat*coslat + b*b*sinlat*sinlat);
00212
00213 theta = atan2(coslat * (h*sr + a*a),
00214 sinlat * (h*sr + b*b));
00215
00216
00217 r = h*h + 2.0*h * sr +
00218 (a*a*a*a - ( a*a*a*a - b*b*b*b ) * sinlat*sinlat ) /
00219 (a*a - (a*a - b*b) * sinlat*sinlat );
00220
00221 r = sqrt(r);
00222
00223
00224 c = cos(theta);
00225 s = sin(theta);
00226
00227 inv_s = 1.0 / (s + (s == 0.)*1.0e-8);
00228
00229
00230 for ( n = 0; n <= nmax; n++ ) {
00231 for ( m = 0; m <= n; m++ ) {
00232 P[n][m] = 0;
00233 DP[n][m] = 0;
00234 }
00235 }
00236
00237
00238 P[0][0] = 1;
00239 P[1][1] = s;
00240 DP[0][0] = 0;
00241 DP[1][1] = c;
00242 P[1][0] = c ;
00243 DP[1][0] = -s;
00244
00245
00246 if( !been_here ) {
00247 for ( n = 2; n <= nmax; n++ ) {
00248 root[n] = sqrt((2.0*n-1) / (2.0*n));
00249 }
00250
00251 for ( m = 0; m <= nmax; m++ ) {
00252 double mm = m*m;
00253 for ( n = SG_MAX2(m + 1, 2); n <= nmax; n++ ) {
00254 roots[m][n][0] = sqrt((n-1)*(n-1) - mm);
00255 roots[m][n][1] = 1.0 / sqrt( n*n - mm);
00256 }
00257 }
00258 been_here = 1;
00259 }
00260
00261 for ( n=2; n <= nmax; n++ ) {
00262
00263 P[n][n] = P[n-1][n-1] * s * root[n];
00264 DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
00265 root[n];
00266 }
00267
00268
00269 for ( m = 0; m <= nmax; m++ ) {
00270
00271 for ( n = SG_MAX2(m + 1, 2); n <= nmax; n++ ) {
00272
00273
00274 P[n][m] = (P[n-1][m] * c * (2.0*n-1) -
00275 P[n-2][m] * roots[m][n][0]) *
00276 roots[m][n][1];
00277
00278 DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
00279 (2.0*n-1) - DP[n-2][m] * roots[m][n][0]) *
00280 roots[m][n][1];
00281 }
00282 }
00283
00284
00285
00286
00287 yearfrac = (dat - date0_wmm2005) / 365.25;
00288 for ( n = 1; n <= nmax; n++ ) {
00289 for ( m = 0; m <= nmax; m++ ) {
00290 gnm[n][m] = gnm_wmm2005[n][m] + yearfrac * gtnm_wmm2005[n][m];
00291 hnm[n][m] = hnm_wmm2005[n][m] + yearfrac * htnm_wmm2005[n][m];
00292 }
00293 }
00294
00295
00296 for ( m = 0; m <= nmax; m++ ) {
00297 sm[m] = sin(m * lon);
00298 cm[m] = cos(m * lon);
00299 }
00300
00301
00302 B_r = 0.0;
00303 B_theta = 0.0;
00304 B_phi = 0.0;
00305 fn_0 = r_0/r;
00306 fn = fn_0 * fn_0;
00307
00308 for ( n = 1; n <= nmax; n++ ) {
00309 double c1_n=0;
00310 double c2_n=0;
00311 double c3_n=0;
00312 for ( m = 0; m <= n; m++ ) {
00313 double tmp = (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]);
00314 c1_n=c1_n + tmp * P[n][m];
00315 c2_n=c2_n + tmp * DP[n][m];
00316 c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
00317 }
00318
00319 fn *= fn_0;
00320 B_r = B_r + (n + 1) * c1_n * fn;
00321 B_theta = B_theta - c2_n * fn;
00322 B_phi = B_phi + c3_n * fn * inv_s;
00323 }
00324
00325
00326 psi = theta - ((M_PI / 2.0) - lat);
00327 sinpsi = sin(psi);
00328 cospsi = cos(psi);
00329 X = -B_theta * cospsi - B_r * sinpsi;
00330 Y = B_phi;
00331 Z = B_theta * sinpsi - B_r * cospsi;
00332
00333 field[0]=B_r;
00334 field[1]=B_theta;
00335 field[2]=B_phi;
00336 field[3]=X;
00337 field[4]=Y;
00338 field[5]=Z;
00339
00340
00341
00342
00343 return (X != 0. || Y != 0.) ? atan2(Y, X) : (double) 0.;
00344 }
00345
00346
00347 #ifdef TEST_NHV_HACKS
00348 double SGMagVarOrig( double lat, double lon, double h, long dat, double* field )
00349 {
00350
00351 int n,m;
00352
00353 long date0_wmm2005 = yymmdd_to_julian_days(5,1,1);
00354
00355 double yearfrac,sr,r,theta,c,s,psi,fn,B_r,B_theta,B_phi,X,Y,Z;
00356
00357
00358 sr = sqrt(pow(a*cos(lat),2.0)+pow(b*sin(lat),2.0));
00359
00360 theta = atan2(cos(lat) * (h * sr + a * a),
00361 sin(lat) * (h * sr + b * b));
00362
00363
00364 r = h * h + 2.0*h * sr +
00365 (pow(a,4.0) - (pow(a,4.0) - pow(b,4.0)) * pow(sin(lat),2.0)) /
00366 (a * a - (a * a - b * b) * pow(sin(lat),2.0));
00367
00368 r = sqrt(r);
00369
00370
00371 c = cos(theta);
00372 s = sin(theta);
00373
00374
00375 for ( n = 0; n <= nmax; n++ ) {
00376 for ( m = 0; m <= n; m++ ) {
00377 P[n][m] = 0;
00378 DP[n][m] = 0;
00379 }
00380 }
00381
00382
00383 P[0][0] = 1;
00384 P[1][1] = s;
00385 DP[0][0] = 0;
00386 DP[1][1] = c;
00387 P[1][0] = c ;
00388 DP[1][0] = -s;
00389
00390 for ( n = 2; n <= nmax; n++ ) {
00391 P[n][n] = P[n-1][n-1] * s * sqrt((2.0*n-1) / (2.0*n));
00392 DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
00393 sqrt((2.0*n-1) / (2.0*n));
00394 }
00395
00396
00397 for ( m = 0; m <= nmax; m++ ) {
00398 for ( n = SG_MAX2(m + 1, 2); n <= nmax; n++ ) {
00399 P[n][m] = (P[n-1][m] * c * (2.0*n-1) - P[n-2][m] *
00400 sqrt(1.0*(n-1)*(n-1) - m * m)) /
00401 sqrt(1.0* n * n - m * m);
00402 DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
00403 (2.0*n-1) - DP[n-2][m] *
00404 sqrt(1.0*(n-1) * (n-1) - m * m)) /
00405 sqrt(1.0* n * n - m * m);
00406 }
00407 }
00408
00409
00410
00411 yearfrac = (dat - date0_wmm2005) / 365.25;
00412 for ( n = 1; n <= nmax; n++ ) {
00413 for ( m = 0; m <= nmax; m++ ) {
00414 gnm[n][m] = gnm_wmm2005[n][m] + yearfrac * gtnm_wmm2005[n][m];
00415 hnm[n][m] = hnm_wmm2005[n][m] + yearfrac * htnm_wmm2005[n][m];
00416 }
00417 }
00418
00419
00420 for ( m = 0; m <= nmax; m++ ) {
00421 sm[m] = sin(m * lon);
00422 cm[m] = cos(m * lon);
00423 }
00424
00425
00426 B_r = 0.0;
00427 B_theta = 0.0;
00428 B_phi = 0.0;
00429
00430 for ( n = 1; n <= nmax; n++ ) {
00431 double c1_n=0;
00432 double c2_n=0;
00433 double c3_n=0;
00434 for ( m = 0; m <= n; m++ ) {
00435 c1_n=c1_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * P[n][m];
00436 c2_n=c2_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * DP[n][m];
00437 c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
00438 }
00439 fn=pow(r_0/r,n+2.0);
00440 B_r = B_r + (n + 1) * c1_n * fn;
00441 B_theta = B_theta - c2_n * fn;
00442 B_phi = B_phi + c3_n * fn / s;
00443 }
00444
00445
00446 psi = theta - (pi / 2.0 - lat);
00447 X = -B_theta * cos(psi) - B_r * sin(psi);
00448 Y = B_phi;
00449 Z = B_theta * sin(psi) - B_r * cos(psi);
00450
00451 field[0]=B_r;
00452 field[1]=B_theta;
00453 field[2]=B_phi;
00454 field[3]=X;
00455 field[4]=Y;
00456 field[5]=Z;
00457
00458
00459 return atan2(Y, X);
00460 }
00461 #endif // TEST_NHV_HACKS