forked from athurnherr/LDEO_IX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magdev.m
304 lines (267 loc) · 6.92 KB
/
magdev.m
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
function [dev,d,h,i,f,x,y,z]=magdev(flat,flon,elevkm,year);
% function [dev,d,h,i,f,x,y,z]=magdev(flat,flon,elevkm,year);
%
% compute magnetic deviation
%
% based on IGRF12 model (observed until end of 2014, predicted until end of 2019)
%
% input: flat - latitude (degree)
% flon - longitude (degree)
% elevkm [0] - elevation above mean geoid (km)
% year - decimal year
%
% output: dev - mag. deviation (degree)
%
%
% based of FORTRAN ROUTINE GEOMAG.FOR
% more info under http://www.ngdc.noaa.gov/IAGA/vmod/igrf.html
%
%
% version 0.5 last change 13.07.2015
% M. Visbeck, LDEO FEB 2000
% rewritten for other epochs, G. Krahmann, IFM-GEOMAR Mar 2007
%
% modified for >=R14 GK, Jun 2007 0.1-->0.2
% switch to IGRF11 GK, 08.11.2010 0.2-->0.3
% replaced sind/cosd with sind/cosd GK, 31.05.2011 0.3-->0.4
% switch to IGRF12 GK, 12.07.2015 0.4-->0.5
%======================================================================
% M A G D E V . M
% doc: Wed Sep 4 18:17:31 2019
% dlm: Mon May 22 10:53:30 2023
% (c) 2019 G. Krahmann
% uE-Info: 47 0 NIL 0 0 72 0 2 8 NIL ofnI
%======================================================================
% CHANGES BY ANT:
% Sep 4, 2019: - changed sin/cos_d to sin/cosd
% May 22, 2023: - adapted to IGRF13
%
% read the coefficients
%
fname = 'IGRF13coeffs.xlsx';
gh = xlsread(fname);
%
% determine the maximum order of polynomials
%
% this might need modification in future versions of the file
%
if year<2000
nmax = 10;
else
nmax = 13;
end
%
% interpolate the coefficients in time
%
warning off % another workaround for annoying >=R14 warnings
if year <= gh(1,end-1)
gh2 = interp1( gh(1,3:end-1),gh(2:end,3:end-1)',year)';
else
gh2 = interp1( gh(1,3:end-1),gh(2:end,3:end-1)',gh(1,end-1))';
ghc = gh2*0;
dummy = gh(2:end,end);
good = find(~isnan(dummy));
ghc(good) = dummy(good);
gh2 = gh2+ghc*(year-gh(1,end-1));
end
warning on
%
% set default elevation
%
if nargin<3
elevkm=0;
end
%
% calculate field
%
[x,y,z] = shval3(flat,flon,elevkm,nmax,gh2);
[d,i,h,f]=dihf (x,y,z);
%
% convert units
%
dev=d*180/pi;
%
% more detailed screen output, if output is not stored
%
if nargout<1
disp([' model ',fname,' harmonics: ',int2str(nmax)])
disp([' lat: ',num2str(flat)])
disp([' lon: ',num2str(flon)])
disp([' tim: ',num2str(year)])
disp([' mag dev [deg]: ',num2str(d*180/pi)])
end
%=====================================================================
function [x,y,z] = shval3(flat,flon,elevkm,nmax,gh)
% function [x,y,z] = shval3(flat,flon,elevkm,nmax,gh)
% ================================================================
%
% version 1.01
%
% calculates field components from spherical harmonic (sh)
% models.
%
% input:
% flat - north latitude, in degrees
% flon - east longitude, in degrees
% elevkm - elevation above mean sea level
% nmax - maximum degree and order of coefficients
% gh - schmidt quasi-normal internal spherical
% harmonic coefficients
% iext - external coefficients flag (= 0 if none)
% ext - the three 1st-degree external coefficients
% (not used if iext = 0)
%
% 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.
%
% norman w. peddie, u.s. geological survey, mail stop 964,
% federal center, box 25046, denver, colorado 80225
%
% ================================================================
% the required sizes of the arrays used in this subroutine
% depend on the value of nmax. the minimum dimensions
% needed are indicated in the table below. (note that this
% version is dimensioned for nmax of 14 or less).
%
% ================================================================
r=elevkm;
erad=6371.2;
a2=40680925.;
b2=40408588.;
slat = sind(flat);
aa = min(89.999,max(-89.999,flat));
clat = cosd(aa);
sl(1) = sind(flon);
cl(1) = cosd(flon);
x=0.0;
y=0.0;
z=0.0;
sd = 0.0;
cd = 1.0;
n=0;
l=1;
m=1;
npq = (nmax*(nmax+3))/2;
aa = a2*clat*clat;
bb = b2*slat*slat;
cc = aa+bb;
dd = sqrt(cc);
r=sqrt(elevkm*(elevkm+2.0*dd)+(a2*aa+b2*bb)/cc);
cd = (elevkm+dd)/r;
sd = (a2-b2)/dd*slat*clat/r;
aa = slat;
slat = slat*cd-clat*sd;
clat = clat*cd+aa*sd;
ratio = erad/r;
aa = sqrt(3.0);
p(1) = 2.0*slat;
p(2) = 2.0*clat;
p(3) = 4.5*slat*slat-1.5;
p(4) = 3.0*aa*clat*slat;
q(1) = -clat;
q(2) = slat;
q(3) = -3.0*clat*slat;
q(4) = aa*(slat*slat-clat*clat);
for k = 1: npq
if (n<m)
m=0;
n=n+1;
rr = ratio^(n+2);
fn=n;
end;
fm=m;
if (k>=5)
if (m==n)
aa = sqrt(1.0-.5/fm);
j=k-n-1;
p(k) = (1.0+1.0/fm)*aa*clat*p(j);
q(k) = aa*(clat*q(j)+slat/fm*p(j));
sl(m) = sl(m-1)*cl(1)+cl(m-1)*sl(1);
cl(m) = cl(m-1)*cl(1)-sl(m-1)*sl(1);
else
aa = sqrt(fn*fn-fm*fm);
bb = sqrt((fn-1.0)^2-fm*fm)/aa;
cc = (2.0*fn-1.0)/aa;
i=k-n;
j=k-2*n+1;
p(k) = (fn+1.0)*(cc*slat/fn*p(i)-bb/(fn-1.0)*p(j));
q(k) = cc*(slat*q(i)-clat/fn*p(i))-bb*q(j);
end;
end;
aa = rr*gh(l);
if (m==0)
x=x+aa*q(k);
z=z-aa*p(k);
l=l+1;
else
bb = rr*gh(l+1);
cc = aa*cl(m)+bb*sl(m);
x=x+cc*q(k);
z=z-cc*p(k);
if (clat>0.0)
y=y+(aa*sl(m)-bb*cl(m))*fm*p(k)/((fn+1.0)*clat);
else
y=y+(aa*sl(m)-bb*cl(m))*q(k)*slat;
end;
l=l+2;
end;
m=m+1;
end;
aa=x;
x=x*cd+z*sd;
z=z*cd-aa*sd;
return;
%==================================================================
function [d,i,h,f] = dihf(x,y,z)
% function [d,i,h,f] = dihf(x,y,z)
% ===============================================================
%
% version 1.01
%
% computes the geomagnetic elements d, i, h, and f from
% x, y, and z.
%
% input:
% x - northward component
% y - eastward component
% z - vertically-downward component
%
% output:
% d - declination
% i - inclination
% h - horizontal intensity
% f - total intensity
%
% a. zunde
% usgs, ms 964, box 25046 federal center, denver, co 80225
%
% ===============================================================
sn=0.0001;
% ---------------------------------------------------------------
% if d and i cannot be determined, set equal to NaN
% ---------------------------------------------------------------
h=sqrt(x*x+y*y);
f=sqrt(x*x+y*y+z*z);
if (f<sn)
d=NaN;
i=NaN;
else
i=atan2(z,h);
if (h<sn)
d=NaN;
else
hpx = h+x;
if (hpx<200)
d=pi;
else
d=2.0*atan2(y,hpx);
end;
end;
end;
return;