-
Notifications
You must be signed in to change notification settings - Fork 2
/
igrf.c
285 lines (266 loc) · 12.8 KB
/
igrf.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/****************************************************************************/
/* */
/* NGDC's Geomagnetic Field Modeling software for the IGRF and WMM */
/* */
/****************************************************************************/
/* */
/* Disclaimer: This program has undergone limited testing. It is */
/* being distributed unoffically. The National Geophysical Data */
/* Center does not guarantee it's correctness. */
/* */
/****************************************************************************/
/* */
/* Version 7.0: */
/* - input file format changed to */
/* -- accept new DGRF2005 coeffs with 0.01 nT precision */
/* -- make sure all values are separated by blanks */
/* -- swapped n and m: first is degree, second is order */
/* - corrected feet to km conversion factor */
/* Thanks to all who provided bug reports and suggested fixes */
/* */
/* Stefan Maus Jan-25-2010 */
/* */
/****************************************************************************/
/* */
/* Version 6.1: */
/* Included option to read coordinates from a file and output the */
/* results to a new file, repeating the input and adding columns */
/* for the output */
/* Stefan Maus Jan-31-2008 */
/* */
/****************************************************************************/
/* */
/* Version 6.0: */
/* Bug fixes for the interpolation between models. Also added warnings */
/* for declination at low H and corrected behaviour at geogr. poles. */
/* Placed print-out commands into separate routines to facilitate */
/* fine-tuning of the tables */
/* Stefan Maus Aug-24-2004 */
/* */
/****************************************************************************/
#include <math.h>
#include <float.h>
#include "vector.h"
#include "igrfCoeffs.h"
#define PI 3.14159265358979323846
#define MAXDEG 13
#define MAXCOEFF (MAXDEG*(MAXDEG+2))
float mag_coeff[MAXCOEFF]; //Computed coefficients
/****************************************************************************/
/* */
/* Subroutine extrapsh */
/* */
/****************************************************************************/
/* */
/* Extrapolates linearly a spherical harmonic model with a */
/* rate-of-change model. */
/* */
/* Input: */
/* date - date of resulting model (in decimal year) */
/* igrf_date - date of base model */
/* igrf_ord - maximum degree and order of base model */
/* igrf_coeffs - Schmidt quasi-normal internal spherical */
/* harmonic coefficients of base model */
/* sv_ord - maximum degree and order of rate-of-change model */
/* igrf_sv - Schmidt quasi-normal internal spherical */
/* harmonic coefficients of rate-of-change model */
/* */
/* Output: */
/* mag_coeff - Schmidt quasi-normal internal spherical */
/* harmonic coefficients */
/* nmax - maximum degree and order of resulting model */
/* */
/* FORTRAN */
/* A. Zunde */
/* USGS, MS 964, box 25046 Federal Center, Denver, CO. 80225 */
/* */
/* C */
/* C. H. Shaffer */
/* Lockheed Missiles and Space Company, Sunnyvale CA */
/* August 16, 1988 */
/* */
/****************************************************************************/
int extrapsh(float date){
int nmax;
int k, l;
int i;
int igo=igrf_ord,svo=sv_ord;
float factor;
//# of years to extrapolate
factor = date - igrf_date;
//make shure that degree is smaller then MAXDEG
if(igo>MAXDEG){
igo=MAXDEG;
}
if(svo>MAXDEG){
svo=MAXDEG;
}
//check for equal degree
if (igo == svo){
k = igo * (igo + 2);
nmax = igo;
}else{
//check if reference is bigger
if (igo > svo){
k = svo * (svo + 2);
l = igo * (igo + 2);
//copy extra elements unchanged
for ( i = k ; i < l; ++i){
mag_coeff[i] = igrf_coeffs[i];
}
//maximum degree of model
nmax = igo;
}else{
k = igo * (igo + 2);
l = svo * (svo + 2);
//put in change for extra elements?
for(i=k;i<l;++i){
mag_coeff[i] = factor * igrf_sv[i];
}
nmax = svo;
}
}
//apply secular variations to model
for ( i = 0; i < k; ++i){
mag_coeff[i] = igrf_coeffs[i] + factor * igrf_sv[i];
}
//return maximum degree of model and secular variations
return nmax;
}
/****************************************************************************/
/* */
/* Subroutine shval3 */
/* */
/****************************************************************************/
/* */
/* Calculates field components from spherical harmonic (sh) */
/* models. */
/* */
/* Input: */
/* latitude - north latitude, in radians */
/* longitude - east longitude, in radians */
/* elev - radial distance from earth's center */
/* nmax - maximum degree and order of coefficients */
/* */
/* Output: */
/* x - northward component */
/* y - eastward component */
/* z - vertically-downward component */
/* */
/* based on subroutine 'igrf' by D. R. Barraclough and S. R. C. Malin, */
/* report no. 71/1, institute of geological sciences, U.K. */
/* */
/* FORTRAN */
/* Norman W. Peddie */
/* USGS, MS 964, box 25046 Federal Center, Denver, CO. 80225 */
/* */
/* C */
/* C. H. Shaffer */
/* Lockheed Missiles and Space Company, Sunnyvale CA */
/* August 17, 1988 */
/* */
/****************************************************************************/
//define buffer size for q and q
#define PQ_BUFFSIZE 32
int shval3(float flat,float flon,float elev,int nmax,VEC *dest){
const float earths_radius = 6371.2;
float slat;
float clat;
float ratio;
float aa, bb, cc;
float rr;
float fm,fn;
float sl[MAXDEG];
float cl[MAXDEG];
float p[PQ_BUFFSIZE];
float q[PQ_BUFFSIZE];
int i,j,k,l,m,n;
int kw;
int npq;
float x,y,z;
//calculate sin and cos of latitude
slat = sin(flat);
clat = cos(flat);
//prevent divide by zero
if(clat==0){
clat=DBL_EPSILON;
}
//calculate sin and cos of longitude
sl[0] = sin(flon);
cl[0] = cos(flon);
//initialize coordinates
x = 0;
y = 0;
z = 0;
//calculate loop iterations
npq = (nmax * (nmax + 3)) / 2;
//calculate ratio of earths radius to elevation
ratio = earths_radius / elev;
aa = sqrt(3.0);
//set initial values of p
p[0] = 2.0 * slat;
p[1] = 2.0 * clat;
p[2] = 4.5 * slat * slat - 1.5;
p[3] = 3.0 * aa * clat * slat;
//Set initial values of q
q[0] = -clat;
q[1] = slat;
q[2] = -3.0 * clat * slat;
q[3] = aa * (slat * slat - clat * clat);
for(k=0,l=1,n=0,m=0,rr=ratio*ratio; k < npq;k++,m++){
//testing get wrapped idx
kw=k%PQ_BUFFSIZE;
if (n <= m){
m = -1;
n+= 1;
//rr = pow(ratio,n+2);
rr*=ratio;
fn = n;
}
fm = m+1;
if (k >= 4){
j = k - n ;
//wrap j for smaller array
j=j%PQ_BUFFSIZE;
if (m+1 == n){
aa = sqrt(1.0 - 0.5/fm);
p[kw] = (1.0 + 1.0/fm) * aa * clat * p[j-1];
q[kw] = aa * (clat * q[j-1] + slat/fm * p[j-1]);
sl[m] = sl[m-1] * cl[0] + cl[m-1] * sl[0];
cl[m] = cl[m-1] * cl[0] - sl[m-1] * sl[0];
}else{
aa = sqrt(fn*fn - fm*fm);
bb = sqrt(((fn - 1.0)*(fn-1.0)) - (fm * fm))/aa;
cc = (2.0 * fn - 1.0)/aa;
i = k - 2 * n + 1;
//wrap i for smaller array
i=i%PQ_BUFFSIZE;
p[kw] = (fn + 1.0) * (cc * slat/fn * p[j] - bb/(fn - 1.0) * p[i]);
q[kw] = cc * (slat * q[j] - clat/fn * p[j]) - bb * q[i];
}
}
aa = rr * mag_coeff[l-1];
if (m == -1){
x = x + aa * q[kw];
z = z - aa * p[kw];
l+= 1;
}else{
bb = rr * mag_coeff[l];
cc = aa * cl[m] + bb * sl[m];
x = x + cc * q[kw];
z = z - cc * p[kw];
if (clat > 0){
y = y + (aa * sl[m] - bb * cl[m]) *fm * p[kw]/((fn + 1.0) * clat);
}else{
y = y + (aa * sl[m] - bb * cl[m]) * q[kw] * slat;
}
l+= 2;
}
}
//set destination values
dest->c.x=x;
dest->c.y=y;
dest->c.z=z;
//always returns zero
return 0;
}