]> git.mxchange.org Git - simgear.git/blob - simgear/magvar/magvar.cxx
Still tweaking output messages.
[simgear.git] / simgear / magvar / magvar.cxx
1 // magvar.cxx -- compute local magnetic variation given position,
2 //               altitude, and date
3 //
4 // This is an implementation of the NIMA (formerly DMA) WMM2000
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 // Put in some bullet-proofing to handle magnetic and geographic poles.
25 // 3/28/2000 EAW
26
27 //  The routine uses a spherical harmonic expansion of the magnetic
28 // potential up to twelfth order, together with its time variation, as
29 // described in Chapter 4 of "Geomagnetism, Vol 1, Ed. J.A.Jacobs,
30 // Academic Press (London 1987)". The program first converts geodetic
31 // coordinates (lat/long on elliptic earth and altitude) to spherical
32 // geocentric (spherical lat/long and radius) coordinates. Using this,
33 // the spherical (B_r, B_theta, B_phi) magnetic field components are
34 // computed from the model. These are finally referred to surface (X, Y,
35 // Z) coordinates.
36 //
37 //   Fields are accurate to better than 200nT, variation and dip to
38 // better than 0.5 degrees, with the exception of the declination near
39 // the magnetic poles (where it is ill-defined) where the error may reach
40 // 4 degrees or more.
41 //
42 //   Variation is undefined at both the geographic and  
43 // magnetic poles, even though the field itself is well-behaved. To
44 // avoid the routine blowing up, latitude entries corresponding to
45 // the geographic poles are slightly offset. At the magnetic poles,
46 // the routine returns zero variation.
47
48
49 //
50 // This program is free software; you can redistribute it and/or
51 // modify it under the terms of the GNU General Public License as
52 // published by the Free Software Foundation; either version 2 of the
53 // License, or (at your option) any later version.
54 //
55 // This program is distributed in the hope that it will be useful, but
56 // WITHOUT ANY WARRANTY; without even the implied warranty of
57 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
58 // General Public License for more details.
59 //
60 // You should have received a copy of the GNU General Public License
61 // along with this program; if not, write to the Free Software
62 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
63 //
64 // $Id$
65
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <math.h>
70
71 #include "magvar.hxx"
72
73
74 #define max(a,b)        (((a) > (b)) ? (a) : (b))
75
76 static const double pi = 3.14159265358979;
77 static const double a = 6378.16;        /* major radius (km) IAU66 ellipsoid */
78 static const double f = 1.0 / 298.25;   /* inverse flattening IAU66 ellipsoid */
79 static const double b = 6378.16 * (1.0 -1.0 / 298.25 );
80 /* minor radius b=a*(1-f) */
81 static const double r_0 = 6371.2;       /* "mean radius" for spherical harmonic expansion */
82
83 static double gnm_wmm2000[13][13] =
84 {
85     {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},
86     {-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},
87     {-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},
88     {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},
89     {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},
90     {-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},
91     {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},
92     {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},
93     {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},
94     {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},
95     {-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},
96     {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},
97     {-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},
98 };
99
100 static double hnm_wmm2000[13][13]=
101 {
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, 5194.5, 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, -2484.8, -467.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
105     {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},
106     {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},
107     {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},
108     {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},
109     {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},
110     {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},
111     {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},
112     {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},
113     {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},
114     {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},
115 };
116
117 static double gtnm_wmm2000[13][13]=
118 {
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     {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},
121     {-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},
122     {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},
123     {-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},
124     {-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},
125     {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},
126     {-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},
127     {-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},
128     {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},
129     {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},
130     {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},
131     {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},
132 };
133
134 static double htnm_wmm2000[13][13]=
135 {
136     {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},
137     {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},
138     {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},
139     {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},
140     {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},
141     {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},
142     {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},
143     {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},
144     {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},
145     {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},
146     {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},
147     {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},
148     {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},
149 };
150
151 static const int nmax = 12;
152
153 static double P[13][13];
154 static double DP[13][13];
155 static double gnm[13][13];
156 static double hnm[13][13];
157 static double sm[13];
158 static double cm[13];
159
160 static double root[13];
161 static double roots[13][13][2];
162
163 /* Convert date to Julian day    1950-2049 */
164 unsigned long int yymmdd_to_julian_days( int yy, int mm, int dd )
165 {
166     unsigned long jd;
167  
168     yy = (yy < 50) ? (2000 + yy) : (1900 + yy);
169     jd = dd - 32075L + 1461L * (yy + 4800L + (mm - 14) / 12 ) / 4;
170     jd = jd + 367L * (mm - 2 - (mm - 14) / 12*12) / 12;
171     jd = jd - 3 * ((yy + 4900L + (mm - 14) / 12) / 100) / 4;
172
173     /* printf("julian date = %d\n", jd ); */
174     return jd;
175
176
177
178 /* Convert degrees to radians */
179 double deg_to_rad( double deg )
180 {
181     return deg*pi/180.;
182 }
183
184
185 /* Convert radians to degrees */
186 double rad_to_deg( double rad )
187 {
188     return rad*180./pi;
189 }
190              
191
192 /*
193  * return variation (in radians) given geodetic latitude (radians),
194  * longitude(radians), height (km) and (Julian) date
195  * N and E lat and long are positive, S and W negative
196 */
197
198 double SGMagVar( double lat, double lon, double h, long dat, double* field )
199 {
200     /* output field B_r,B_th,B_phi,B_x,B_y,B_z */
201     int n,m;
202     /* reference dates */
203     long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
204
205     double yearfrac,sr,r,theta,c,s,psi,fn,fn_0,B_r,B_theta,B_phi,X,Y,Z;
206     double sinpsi, cospsi, inv_s;
207
208     static int been_here = 0;
209
210     double sinlat = sin(lat);
211     double coslat = cos(lat);
212
213     /* convert to geocentric coords: */
214     // sr = sqrt(pow(a*coslat,2.0)+pow(b*sinlat,2.0));
215     sr = sqrt(a*a*coslat*coslat + b*b*sinlat*sinlat);
216     /* sr is effective radius */
217     theta = atan2(coslat * (h*sr + a*a),
218                   sinlat * (h*sr + b*b));
219     /* theta is geocentric co-latitude */
220
221     r = h*h + 2.0*h * sr +
222         (a*a*a*a - ( a*a*a*a - b*b*b*b ) * sinlat*sinlat ) / 
223         (a*a - (a*a - b*b) * sinlat*sinlat );
224
225     r = sqrt(r);
226
227     /* r is geocentric radial distance */
228     c = cos(theta);
229     s = sin(theta);
230     /* protect against zero divide at geographic poles */
231     inv_s =  1.0 / (s + (s == 0.)*1.0e-8); 
232
233     /* zero out arrays */
234     for ( n = 0; n <= nmax; n++ ) {
235         for ( m = 0; m <= n; m++ ) {
236             P[n][m] = 0;
237             DP[n][m] = 0;
238         }
239     }
240
241     /* diagonal elements */
242     P[0][0] = 1;
243     P[1][1] = s;
244     DP[0][0] = 0;
245     DP[1][1] = c;
246     P[1][0] = c ;
247     DP[1][0] = -s;
248
249     // these values will not change for subsequent function calls
250     if( !been_here ) {
251         for ( n = 2; n <= nmax; n++ ) {
252             root[n] = sqrt((2.0*n-1) / (2.0*n));
253         }
254
255         for ( m = 0; m <= nmax; m++ ) {
256             double mm = m*m;
257             for ( n = max(m + 1, 2); n <= nmax; n++ ) {
258                 roots[m][n][0] = sqrt((n-1)*(n-1) - mm);
259                 roots[m][n][1] = 1.0 / sqrt( n*n - mm);
260             }
261         }
262         been_here = 1;
263     }
264
265     for ( n=2; n <= nmax; n++ ) {
266         // double root = sqrt((2.0*n-1) / (2.0*n));
267         P[n][n] = P[n-1][n-1] * s * root[n];
268         DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
269             root[n];
270     }
271
272     /* lower triangle */
273     for ( m = 0; m <= nmax; m++ ) {
274         // double mm = m*m;
275         for ( n = max(m + 1, 2); n <= nmax; n++ ) {
276             // double root1 = sqrt((n-1)*(n-1) - mm);
277             // double root2 = 1.0 / sqrt( n*n - mm);
278             P[n][m] = (P[n-1][m] * c * (2.0*n-1) -
279                        P[n-2][m] * roots[m][n][0]) *
280                 roots[m][n][1];
281
282             DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
283                         (2.0*n-1) - DP[n-2][m] * roots[m][n][0]) *
284                 roots[m][n][1];
285         }
286     }
287
288     /* compute gnm, hnm at dat */
289     /* WMM2000 */
290     yearfrac = (dat - date0_wmm2000) / 365.25;
291     for ( n = 1; n <= nmax; n++ ) {
292         for ( m = 0; m <= nmax; m++ ) {
293             gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
294             hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
295         }
296     }
297
298     /* compute sm (sin(m lon) and cm (cos(m lon)) */
299     for ( m = 0; m <= nmax; m++ ) {
300         sm[m] = sin(m * lon);
301         cm[m] = cos(m * lon);
302     }
303
304     /* compute B fields */
305     B_r = 0.0;
306     B_theta = 0.0;
307     B_phi = 0.0;
308     fn_0 = r_0/r;
309     fn = fn_0 * fn_0;
310
311     for ( n = 1; n <= nmax; n++ ) {
312         double c1_n=0;
313         double c2_n=0;
314         double c3_n=0;
315         for ( m = 0; m <= n; m++ ) {
316             double tmp = (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]); 
317             c1_n=c1_n + tmp * P[n][m];
318             c2_n=c2_n + tmp * DP[n][m];
319             c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
320         }
321         // fn=pow(r_0/r,n+2.0);
322         fn *= fn_0;
323         B_r = B_r + (n + 1) * c1_n * fn;
324         B_theta = B_theta - c2_n * fn;
325         B_phi = B_phi + c3_n * fn * inv_s;
326     }
327
328     /* Find geodetic field components: */
329     psi = theta - ((pi / 2.0) - lat);
330     sinpsi = sin(psi);
331     cospsi = cos(psi);
332     X = -B_theta * cospsi - B_r * sinpsi;
333     Y = B_phi;
334     Z = B_theta * sinpsi - B_r * cospsi;
335
336     field[0]=B_r;
337     field[1]=B_theta;
338     field[2]=B_phi;
339     field[3]=X;
340     field[4]=Y;
341     field[5]=Z;   /* output fields */
342
343     /* find variation in radians */
344     /* return zero variation at magnetic pole X=Y=0. */
345     /* E is positive */
346     return (X != 0. || Y != 0.) ? atan2(Y, X) : (double) 0.;  
347 }
348
349
350 #ifdef TEST_NHV_HACKS
351 double SGMagVarOrig( double lat, double lon, double h, long dat, double* field )
352 {
353     /* output field B_r,B_th,B_phi,B_x,B_y,B_z */
354     int n,m;
355     /* reference dates */
356     long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
357
358     double yearfrac,sr,r,theta,c,s,psi,fn,B_r,B_theta,B_phi,X,Y,Z;
359
360     /* convert to geocentric coords: */
361     sr = sqrt(pow(a*cos(lat),2.0)+pow(b*sin(lat),2.0));
362     /* sr is effective radius */
363     theta = atan2(cos(lat) * (h * sr + a * a),
364                   sin(lat) * (h * sr + b * b));
365     /* theta is geocentric co-latitude */
366
367     r = h * h + 2.0*h * sr +
368         (pow(a,4.0) - (pow(a,4.0) - pow(b,4.0)) * pow(sin(lat),2.0)) / 
369         (a * a - (a * a - b * b) * pow(sin(lat),2.0));
370
371     r = sqrt(r);
372
373     /* r is geocentric radial distance */
374     c = cos(theta);
375     s = sin(theta);
376
377     /* zero out arrays */
378     for ( n = 0; n <= nmax; n++ ) {
379         for ( m = 0; m <= n; m++ ) {
380             P[n][m] = 0;
381             DP[n][m] = 0;
382         }
383     }
384
385     /* diagonal elements */
386     P[0][0] = 1;
387     P[1][1] = s;
388     DP[0][0] = 0;
389     DP[1][1] = c;
390     P[1][0] = c ;
391     DP[1][0] = -s;
392
393     for ( n = 2; n <= nmax; n++ ) {
394         P[n][n] = P[n-1][n-1] * s * sqrt((2.0*n-1) / (2.0*n));
395         DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
396             sqrt((2.0*n-1) / (2.0*n));
397     }
398
399     /* lower triangle */
400     for ( m = 0; m <= nmax; m++ ) {
401         for ( n = max(m + 1, 2); n <= nmax; n++ ) {
402             P[n][m] = (P[n-1][m] * c * (2.0*n-1) - P[n-2][m] *
403                        sqrt(1.0*(n-1)*(n-1) - m * m)) /
404                 sqrt(1.0* n * n - m * m);
405             DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
406                         (2.0*n-1) - DP[n-2][m] *
407                         sqrt(1.0*(n-1) * (n-1) - m * m)) /
408                 sqrt(1.0* n * n - m * m);
409         }
410     }
411
412     /* compute gnm, hnm at dat */
413     /* WMM2000 */
414     yearfrac = (dat - date0_wmm2000) / 365.25;
415     for ( n = 1; n <= nmax; n++ ) {
416         for ( m = 0; m <= nmax; m++ ) {
417             gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
418             hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
419         }
420     }
421
422     /* compute sm (sin(m lon) and cm (cos(m lon)) */
423     for ( m = 0; m <= nmax; m++ ) {
424         sm[m] = sin(m * lon);
425         cm[m] = cos(m * lon);
426     }
427
428     /* compute B fields */
429     B_r = 0.0;
430     B_theta = 0.0;
431     B_phi = 0.0;
432
433     for ( n = 1; n <= nmax; n++ ) {
434         double c1_n=0;
435         double c2_n=0;
436         double c3_n=0;
437         for ( m = 0; m <= n; m++ ) {
438             c1_n=c1_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * P[n][m];
439             c2_n=c2_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * DP[n][m];
440             c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
441         }
442         fn=pow(r_0/r,n+2.0);
443         B_r = B_r + (n + 1) * c1_n * fn;
444         B_theta = B_theta - c2_n * fn;
445         B_phi = B_phi + c3_n * fn / s;
446     }
447
448     /* Find geodetic field components: */
449     psi = theta - (pi / 2.0 - lat);
450     X = -B_theta * cos(psi) - B_r * sin(psi);
451     Y = B_phi;
452     Z = B_theta * sin(psi) - B_r * cos(psi);
453
454     field[0]=B_r;
455     field[1]=B_theta;
456     field[2]=B_phi;
457     field[3]=X;
458     field[4]=Y;
459     field[5]=Z;   /* output fields */
460
461     /* find variation, leave in radians! */
462     return atan2(Y, X);  /* E is positive */
463 }
464 #endif // TEST_NHV_HACKS