5
|
1 |
/*
|
|
2 |
The core calculation subroutines are here.
|
|
3 |
The first set were added be Eric Firing to modernize
|
|
4 |
the reading and handling of the model data, but are
|
|
5 |
otherwise based on code from the original geomag61.c.
|
|
6 |
The second set of subroutines are identical to the originals,
|
|
7 |
or are lightly modified to use the new model data structure
|
|
8 |
and to avoid the use of global variables.
|
|
9 |
|
|
10 |
*/
|
|
11 |
|
|
12 |
#include "geomag.h"
|
|
13 |
#include "igrf11.h" /* initializes model_lines */
|
|
14 |
|
|
15 |
#define LINEBUFSIZE 90 /* leave some slop */
|
|
16 |
|
|
17 |
int models_from_lines(struct model_t ***model_array)
|
|
18 |
/* May want to change this to return the array of pointers and use
|
|
19 |
a pointer to an int to return the length or error code; as it
|
|
20 |
is, triple dereferencing is getting confusing.
|
|
21 |
*/
|
|
22 |
|
|
23 |
{
|
|
24 |
struct model_t model;
|
|
25 |
struct model_t *mod_ptr, *prev_mod_ptr, *first_mod_ptr;
|
|
26 |
struct model_t **models;
|
|
27 |
char buf[LINEBUFSIZE];
|
|
28 |
char *c;
|
|
29 |
int nmodels = 0;
|
|
30 |
int ret; /* used for error return cases of "goto error;" */
|
|
31 |
int nscan;
|
|
32 |
int ii;
|
|
33 |
int iline=0;
|
|
34 |
|
|
35 |
|
|
36 |
while (1)
|
|
37 |
{
|
|
38 |
strcpy(buf, model_lines[iline]);
|
|
39 |
iline++;
|
|
40 |
if (strlen(buf) != RECLEN) break;
|
|
41 |
|
|
42 |
if (!strncmp(buf," ", 3))
|
|
43 |
{
|
|
44 |
if (nmodels)
|
|
45 |
{
|
|
46 |
prev_mod_ptr = mod_ptr;
|
|
47 |
}
|
|
48 |
mod_ptr = malloc(sizeof(struct model_t));
|
|
49 |
if (mod_ptr == NULL)
|
|
50 |
{
|
|
51 |
ret = -2;
|
|
52 |
goto error;
|
|
53 |
}
|
|
54 |
mod_ptr->next = NULL;
|
|
55 |
if (nmodels)
|
|
56 |
{
|
|
57 |
prev_mod_ptr->next = mod_ptr;
|
|
58 |
}
|
|
59 |
else
|
|
60 |
{
|
|
61 |
first_mod_ptr = mod_ptr;
|
|
62 |
}
|
|
63 |
nmodels++; /* Now it can be used for deallocation on error */
|
|
64 |
nscan = sscanf(buf, "%s%lf%d%d%d%lf%lf%lf%lf",
|
|
65 |
mod_ptr->name, &mod_ptr->epoch,
|
|
66 |
&mod_ptr->max1, &mod_ptr->max2,
|
|
67 |
&mod_ptr->max3, &mod_ptr->yrmin,
|
|
68 |
&mod_ptr->yrmax, &mod_ptr->altmin,
|
|
69 |
&mod_ptr->altmax);
|
|
70 |
if (nscan != 9)
|
|
71 |
{
|
|
72 |
ret = -3;
|
|
73 |
goto error;
|
|
74 |
}
|
|
75 |
ii = 0; /* initialized for reading coefficients */
|
|
76 |
}
|
|
77 |
else
|
|
78 |
{
|
|
79 |
int mm, nn;
|
|
80 |
|
|
81 |
if (nmodels < 1)
|
|
82 |
{
|
|
83 |
ret = -4;
|
|
84 |
goto error;
|
|
85 |
}
|
|
86 |
|
|
87 |
for ( nn = 1; nn <= mod_ptr->max1; ++nn)
|
|
88 |
{
|
|
89 |
for (mm = 0; mm <= nn; ++mm)
|
|
90 |
{
|
|
91 |
int m, n;
|
|
92 |
double f1, f2, f3, f4;
|
|
93 |
int nscan2, nscan3;
|
|
94 |
|
|
95 |
/* Read a new line only if we have already used the line. */
|
|
96 |
if (buf[0] == '\0')
|
|
97 |
{
|
|
98 |
strcpy(buf, model_lines[iline]);
|
|
99 |
iline++;
|
|
100 |
}
|
|
101 |
|
|
102 |
nscan = sscanf(buf, "%d %d %lf %lf %lf %lf", /* %8s%d", */
|
|
103 |
&n, &m, &f1, &f2, &f3, &f4);
|
|
104 |
/*, irat, &line_num); */
|
|
105 |
|
|
106 |
if (nscan != 6)
|
|
107 |
{
|
|
108 |
ret = -5;
|
|
109 |
// printf("nscan=%d\n", nscan);
|
|
110 |
// printf("%s\n", buf);
|
|
111 |
goto error;
|
|
112 |
}
|
|
113 |
if ((nn != n) || (mm != m))
|
|
114 |
{
|
|
115 |
ret = -6;
|
|
116 |
// printf("nn, n, mm, m: %d %d %d %d\n", nn, n, mm, m);
|
|
117 |
// printf("%s\n", buf);
|
|
118 |
goto error;
|
|
119 |
}
|
|
120 |
ii++; /* 1-based arrays */
|
|
121 |
mod_ptr->gh[ii] = f1;
|
|
122 |
mod_ptr->ghr[ii] = f3;
|
|
123 |
if (m != 0)
|
|
124 |
{
|
|
125 |
ii++;
|
|
126 |
mod_ptr->gh[ii] = f2;
|
|
127 |
mod_ptr->ghr[ii] = f4;
|
|
128 |
}
|
|
129 |
buf[0] = '\0'; /* flag: read a new line */
|
|
130 |
}
|
|
131 |
}
|
|
132 |
}
|
|
133 |
}
|
|
134 |
models = (struct model_t **)malloc(nmodels * sizeof(void *));
|
|
135 |
if (models == NULL)
|
|
136 |
{
|
|
137 |
return(-6);
|
|
138 |
}
|
|
139 |
mod_ptr = first_mod_ptr;
|
|
140 |
for (ii=0; ii<nmodels; ii++)
|
|
141 |
{
|
|
142 |
models[ii] = mod_ptr;
|
|
143 |
mod_ptr = mod_ptr->next;
|
|
144 |
}
|
|
145 |
*model_array = models;
|
|
146 |
return nmodels;
|
|
147 |
|
|
148 |
error:
|
|
149 |
/* Clean up by working forward through the linked list. */
|
|
150 |
for (ii=0; ii<nmodels; ii++)
|
|
151 |
{
|
|
152 |
mod_ptr = first_mod_ptr->next;
|
|
153 |
if (first_mod_ptr) free(first_mod_ptr);
|
|
154 |
first_mod_ptr = mod_ptr;
|
|
155 |
}
|
|
156 |
return ret;
|
|
157 |
}
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
int models_from_file(char *filename, struct model_t ***model_array)
|
|
162 |
/* May want to change this to return the array of pointers and use
|
|
163 |
a pointer to an int to return the length or error code; as it
|
|
164 |
is, triple dereferencing is getting confusing.
|
|
165 |
*/
|
|
166 |
|
|
167 |
{
|
|
168 |
struct model_t model;
|
|
169 |
struct model_t *mod_ptr, *prev_mod_ptr, *first_mod_ptr;
|
|
170 |
struct model_t **models;
|
|
171 |
FILE *modfile;
|
|
172 |
char buf[LINEBUFSIZE];
|
|
173 |
char *c;
|
|
174 |
int nmodels = 0;
|
|
175 |
int ret; /* used for error return cases of "goto error;" */
|
|
176 |
int nscan;
|
|
177 |
int ii;
|
|
178 |
|
|
179 |
|
|
180 |
modfile = fopen(filename, "rb"); /* handle line endings ourselves */
|
|
181 |
if (modfile == NULL) return(-1);
|
|
182 |
while (!feof(modfile))
|
|
183 |
{
|
|
184 |
fgets(buf, LINEBUFSIZE, modfile);
|
|
185 |
for (c = buf; *c; c++)
|
|
186 |
{
|
|
187 |
if (*c == '\n' || *c == '\r')
|
|
188 |
{
|
|
189 |
*c = '\0';
|
|
190 |
break;
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
if (strlen(buf) != RECLEN) continue;
|
|
195 |
|
|
196 |
if (!strncmp(buf," ", 3))
|
|
197 |
{
|
|
198 |
if (nmodels)
|
|
199 |
{
|
|
200 |
prev_mod_ptr = mod_ptr;
|
|
201 |
}
|
|
202 |
mod_ptr = malloc(sizeof(struct model_t));
|
|
203 |
if (mod_ptr == NULL)
|
|
204 |
{
|
|
205 |
ret = -2;
|
|
206 |
goto error;
|
|
207 |
}
|
|
208 |
mod_ptr->next = NULL;
|
|
209 |
if (nmodels)
|
|
210 |
{
|
|
211 |
prev_mod_ptr->next = mod_ptr;
|
|
212 |
}
|
|
213 |
else
|
|
214 |
{
|
|
215 |
first_mod_ptr = mod_ptr;
|
|
216 |
}
|
|
217 |
nmodels++; /* Now it can be used for deallocation on error */
|
|
218 |
nscan = sscanf(buf, "%s%lf%d%d%d%lf%lf%lf%lf",
|
|
219 |
mod_ptr->name, &mod_ptr->epoch,
|
|
220 |
&mod_ptr->max1, &mod_ptr->max2,
|
|
221 |
&mod_ptr->max3, &mod_ptr->yrmin,
|
|
222 |
&mod_ptr->yrmax, &mod_ptr->altmin,
|
|
223 |
&mod_ptr->altmax);
|
|
224 |
if (nscan != 9)
|
|
225 |
{
|
|
226 |
ret = -3;
|
|
227 |
goto error;
|
|
228 |
}
|
|
229 |
ii = 0; /* initialized for reading coefficients */
|
|
230 |
}
|
|
231 |
else
|
|
232 |
{
|
|
233 |
int mm, nn;
|
|
234 |
|
|
235 |
if (nmodels < 1)
|
|
236 |
{
|
|
237 |
ret = -4;
|
|
238 |
goto error;
|
|
239 |
}
|
|
240 |
|
|
241 |
for ( nn = 1; nn <= mod_ptr->max1; ++nn)
|
|
242 |
{
|
|
243 |
for (mm = 0; mm <= nn; ++mm)
|
|
244 |
{
|
|
245 |
int m, n;
|
|
246 |
double f1, f2, f3, f4;
|
|
247 |
int nscan2, nscan3;
|
|
248 |
|
|
249 |
/* Read a new line only if we have already used the line. */
|
|
250 |
if (buf[0] == '\0')
|
|
251 |
{
|
|
252 |
/* Read a line and chop off the line ending. */
|
|
253 |
fgets(buf, LINEBUFSIZE, modfile);
|
|
254 |
for (c = buf; *c; c++)
|
|
255 |
{
|
|
256 |
if (*c == '\n' || *c == '\r')
|
|
257 |
{
|
|
258 |
*c = '\0';
|
|
259 |
break;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
}
|
|
263 |
|
|
264 |
nscan = sscanf(buf, "%d %d %lf %lf %lf %lf", /* %8s%d", */
|
|
265 |
&n, &m, &f1, &f2, &f3, &f4);
|
|
266 |
/*, irat, &line_num); */
|
|
267 |
|
|
268 |
if (nscan != 6)
|
|
269 |
{
|
|
270 |
ret = -5;
|
|
271 |
// printf("nscan=%d\n", nscan);
|
|
272 |
// printf("%s\n", buf);
|
|
273 |
goto error;
|
|
274 |
}
|
|
275 |
if ((nn != n) || (mm != m))
|
|
276 |
{
|
|
277 |
ret = -6;
|
|
278 |
// printf("nn, n, mm, m: %d %d %d %d\n", nn, n, mm, m);
|
|
279 |
// printf("%s\n", buf);
|
|
280 |
goto error;
|
|
281 |
}
|
|
282 |
ii++; /* 1-based arrays */
|
|
283 |
mod_ptr->gh[ii] = f1;
|
|
284 |
mod_ptr->ghr[ii] = f3;
|
|
285 |
if (m != 0)
|
|
286 |
{
|
|
287 |
ii++;
|
|
288 |
mod_ptr->gh[ii] = f2;
|
|
289 |
mod_ptr->ghr[ii] = f4;
|
|
290 |
}
|
|
291 |
buf[0] = '\0'; /* flag: read a new line */
|
|
292 |
}
|
|
293 |
}
|
|
294 |
}
|
|
295 |
}
|
|
296 |
fclose(modfile);
|
|
297 |
models = (struct model_t **)malloc(nmodels * sizeof(void *));
|
|
298 |
if (models == NULL)
|
|
299 |
{
|
|
300 |
return(-6);
|
|
301 |
}
|
|
302 |
mod_ptr = first_mod_ptr;
|
|
303 |
for (ii=0; ii<nmodels; ii++)
|
|
304 |
{
|
|
305 |
models[ii] = mod_ptr;
|
|
306 |
mod_ptr = mod_ptr->next;
|
|
307 |
}
|
|
308 |
*model_array = models;
|
|
309 |
return nmodels;
|
|
310 |
|
|
311 |
error:
|
|
312 |
fclose(modfile);
|
|
313 |
/* Clean up by working forward through the linked list. */
|
|
314 |
for (ii=0; ii<nmodels; ii++)
|
|
315 |
{
|
|
316 |
mod_ptr = first_mod_ptr->next;
|
|
317 |
if (first_mod_ptr) free(first_mod_ptr);
|
|
318 |
first_mod_ptr = mod_ptr;
|
|
319 |
}
|
|
320 |
return ret;
|
|
321 |
}
|
|
322 |
|
|
323 |
void free_models(struct model_t **model_array, int nmodels)
|
|
324 |
{
|
|
325 |
int i;
|
|
326 |
|
|
327 |
for (i=0; i<nmodels; i++)
|
|
328 |
{
|
|
329 |
if (model_array[i]) free(model_array[i]);
|
|
330 |
}
|
|
331 |
if (model_array) free(model_array);
|
|
332 |
}
|
|
333 |
|
|
334 |
int dihf_from_models(struct model_t **model_array, int nmodels,
|
|
335 |
double yr, double lon, double lat,
|
|
336 |
double *d_ptr, double *i_ptr, double *h_ptr,
|
|
337 |
double *f_ptr)
|
|
338 |
{
|
|
339 |
int ret = 0; /* return code */
|
|
340 |
int modelI;
|
|
341 |
int nmax;
|
|
342 |
double gha[MAXCOEFF];
|
|
343 |
double x, y, z;
|
|
344 |
double d, i, h, f;
|
|
345 |
struct model_t *modI, *modIp;
|
|
346 |
|
|
347 |
/* might not want to keep these */
|
|
348 |
int warn_H = 0;
|
|
349 |
int warn_H_strong = 0;
|
|
350 |
double warn_H_strong_val;
|
|
351 |
|
|
352 |
|
|
353 |
for (modelI=0; modelI<nmodels; modelI++)
|
|
354 |
{
|
|
355 |
if (yr < model_array[modelI]->yrmax)
|
|
356 |
break;
|
|
357 |
}
|
|
358 |
if (modelI == nmodels) modelI--;
|
|
359 |
|
|
360 |
modI = model_array[modelI];
|
|
361 |
if (modI->max2 == 0)
|
|
362 |
{
|
|
363 |
modIp = model_array[modelI+1];
|
|
364 |
nmax = interpsh(yr, modI->yrmin, modI->max1,
|
|
365 |
modIp->yrmin, modIp->max1,
|
|
366 |
modI->gh, modIp->gh, gha);
|
|
367 |
}
|
|
368 |
else
|
|
369 |
{
|
|
370 |
nmax = extrapsh(yr, modI->epoch, modI->max1, modI->max2,
|
|
371 |
modI->gh, modI->ghr, gha);
|
|
372 |
}
|
|
373 |
|
|
374 |
shval3(1, lat, lon, 0.0, nmax, gha,
|
|
375 |
IEXT, EXT_COEFF1, EXT_COEFF2, EXT_COEFF3,
|
|
376 |
&x, &y, &z);
|
|
377 |
dihf(x, y, z, &d, &i, &h, &f);
|
|
378 |
|
|
379 |
if (h < 100.0) /* at magnetic poles */
|
|
380 |
{
|
|
381 |
d = NaN;
|
|
382 |
/* while rest is ok */
|
|
383 |
}
|
|
384 |
|
|
385 |
if (90.0-fabs(lat) <= 0.001) /* at geographic poles */
|
|
386 |
{
|
|
387 |
x = NaN;
|
|
388 |
y = NaN;
|
|
389 |
d = NaN;
|
|
390 |
/* while rest is ok */
|
|
391 |
}
|
|
392 |
|
|
393 |
*d_ptr = d * (180.0/M_PI);
|
|
394 |
*i_ptr = i * (180.0/M_PI);
|
|
395 |
*h_ptr = h;
|
|
396 |
*f_ptr = f;
|
|
397 |
return ret; /* always 0 at present */
|
|
398 |
}
|
|
399 |
|
|
400 |
|
|
401 |
|
|
402 |
/* subroutines below are modifications of the routines from geomag61.c */
|
|
403 |
|
|
404 |
|
|
405 |
/****************************************************************************/
|
|
406 |
/* */
|
|
407 |
/* Subroutine julday */
|
|
408 |
/* */
|
|
409 |
/****************************************************************************/
|
|
410 |
/* */
|
|
411 |
/* Computes the decimal day of year from month, day, year. */
|
|
412 |
/* Leap years accounted for 1900 and 2000 are not leap years. */
|
|
413 |
/* */
|
|
414 |
/* Input: */
|
|
415 |
/* year - Integer year of interest */
|
|
416 |
/* month - Integer month of interest */
|
|
417 |
/* day - Integer day of interest */
|
|
418 |
/* */
|
|
419 |
/* Output: */
|
|
420 |
/* date - Julian date to thousandth of year */
|
|
421 |
/* */
|
|
422 |
/* FORTRAN */
|
|
423 |
/* S. McLean */
|
|
424 |
/* NGDC, NOAA egc1, 325 Broadway, Boulder CO. 80301 */
|
|
425 |
/* */
|
|
426 |
/* C */
|
|
427 |
/* C. H. Shaffer */
|
|
428 |
/* Lockheed Missiles and Space Company, Sunnyvale CA */
|
|
429 |
/* August 12, 1988 */
|
|
430 |
/* */
|
|
431 |
/* Julday Bug Fix */
|
|
432 |
/* Thanks to Rob Raper */
|
|
433 |
/****************************************************************************/
|
|
434 |
|
|
435 |
|
|
436 |
double julday(int i_month, int i_day, int i_year)
|
|
437 |
{
|
|
438 |
int aggregate_first_day_of_month[13];
|
|
439 |
int leap_year = 0;
|
|
440 |
int truncated_dividend;
|
|
441 |
double year;
|
|
442 |
double day;
|
|
443 |
double decimal_date;
|
|
444 |
double remainder = 0.0;
|
|
445 |
double divisor = 4.0;
|
|
446 |
double dividend;
|
|
447 |
double left_over;
|
|
448 |
|
|
449 |
aggregate_first_day_of_month[1] = 1;
|
|
450 |
aggregate_first_day_of_month[2] = 32;
|
|
451 |
aggregate_first_day_of_month[3] = 60;
|
|
452 |
aggregate_first_day_of_month[4] = 91;
|
|
453 |
aggregate_first_day_of_month[5] = 121;
|
|
454 |
aggregate_first_day_of_month[6] = 152;
|
|
455 |
aggregate_first_day_of_month[7] = 182;
|
|
456 |
aggregate_first_day_of_month[8] = 213;
|
|
457 |
aggregate_first_day_of_month[9] = 244;
|
|
458 |
aggregate_first_day_of_month[10] = 274;
|
|
459 |
aggregate_first_day_of_month[11] = 305;
|
|
460 |
aggregate_first_day_of_month[12] = 335;
|
|
461 |
|
|
462 |
/* Test for leap year. If true add one to day. */
|
|
463 |
|
|
464 |
year = i_year; /* Century Years not */
|
|
465 |
if ((i_year != 1900) && (i_year != 2100)) /* divisible by 400 are */
|
|
466 |
{ /* NOT leap years */
|
|
467 |
dividend = year/divisor;
|
|
468 |
truncated_dividend = dividend;
|
|
469 |
left_over = dividend - truncated_dividend;
|
|
470 |
remainder = left_over*divisor;
|
|
471 |
if ((remainder > 0.0) && (i_month > 2))
|
|
472 |
{
|
|
473 |
leap_year = 1;
|
|
474 |
}
|
|
475 |
else
|
|
476 |
{
|
|
477 |
leap_year = 0;
|
|
478 |
}
|
|
479 |
}
|
|
480 |
day = aggregate_first_day_of_month[i_month] + i_day - 1 + leap_year;
|
|
481 |
if (leap_year)
|
|
482 |
{
|
|
483 |
decimal_date = year + (day/366.0); /*In version 3.0 this was incorrect*/
|
|
484 |
}
|
|
485 |
else
|
|
486 |
{
|
|
487 |
decimal_date = year + (day/365.0); /*In version 3.0 this was incorrect*/
|
|
488 |
}
|
|
489 |
return(decimal_date);
|
|
490 |
}
|
|
491 |
|
|
492 |
|
|
493 |
/****************************************************************************/
|
|
494 |
/* */
|
|
495 |
/* Subroutine extrapsh */
|
|
496 |
/* */
|
|
497 |
/****************************************************************************/
|
|
498 |
/* */
|
|
499 |
/* Extrapolates linearly a spherical harmonic model with a */
|
|
500 |
/* rate-of-change model. */
|
|
501 |
/* */
|
|
502 |
/* Input: */
|
|
503 |
/* date - date of resulting model (in decimal year) */
|
|
504 |
/* dte1 - date of base model */
|
|
505 |
/* nmax1 - maximum degree and order of base model */
|
|
506 |
/* gh1 - Schmidt quasi-normal internal spherical */
|
|
507 |
/* harmonic coefficients of base model */
|
|
508 |
/* nmax2 - maximum degree and order of rate-of-change model */
|
|
509 |
/* gh2 - Schmidt quasi-normal internal spherical */
|
|
510 |
/* harmonic coefficients of rate-of-change model */
|
|
511 |
/* */
|
|
512 |
/* Output: */
|
|
513 |
/* gha or b - Schmidt quasi-normal internal spherical */
|
|
514 |
/* harmonic coefficients */
|
|
515 |
/* nmax - maximum degree and order of resulting model */
|
|
516 |
/* */
|
|
517 |
/* FORTRAN */
|
|
518 |
/* A. Zunde */
|
|
519 |
/* USGS, MS 964, box 25046 Federal Center, Denver, CO. 80225 */
|
|
520 |
/* */
|
|
521 |
/* C */
|
|
522 |
/* C. H. Shaffer */
|
|
523 |
/* Lockheed Missiles and Space Company, Sunnyvale CA */
|
|
524 |
/* August 16, 1988 */
|
|
525 |
/* */
|
|
526 |
/****************************************************************************/
|
|
527 |
|
|
528 |
/* gh_Schmidt[12] are input arrays of Schmidt coefficients: gh1, gh2 */
|
|
529 |
/* gh_model is an output array of model coefficients: gha or ghb */
|
|
530 |
|
|
531 |
int extrapsh(double date, double dte1, int nmax1, int nmax2,
|
|
532 |
double *gh_Schmidt1, double *gh_Schmidt2, double *gh_model)
|
|
533 |
{
|
|
534 |
int nmax;
|
|
535 |
int k, l;
|
|
536 |
int ii;
|
|
537 |
double factor;
|
|
538 |
|
|
539 |
factor = date - dte1;
|
|
540 |
if (nmax1 == nmax2)
|
|
541 |
{
|
|
542 |
k = nmax1 * (nmax1 + 2);
|
|
543 |
nmax = nmax1;
|
|
544 |
}
|
|
545 |
else
|
|
546 |
{
|
|
547 |
if (nmax1 > nmax2)
|
|
548 |
{
|
|
549 |
k = nmax2 * (nmax2 + 2);
|
|
550 |
l = nmax1 * (nmax1 + 2);
|
|
551 |
for ( ii = k + 1; ii <= l; ++ii)
|
|
552 |
{
|
|
553 |
gh_model[ii] = gh_Schmidt1[ii];
|
|
554 |
}
|
|
555 |
nmax = nmax1;
|
|
556 |
}
|
|
557 |
else
|
|
558 |
{
|
|
559 |
k = nmax1 * (nmax1 + 2);
|
|
560 |
l = nmax2 * (nmax2 + 2);
|
|
561 |
for ( ii = k + 1; ii <= l; ++ii)
|
|
562 |
{
|
|
563 |
gh_model[ii] = factor * gh_Schmidt2[ii];
|
|
564 |
}
|
|
565 |
nmax = nmax2;
|
|
566 |
}
|
|
567 |
}
|
|
568 |
for ( ii = 1; ii <= k; ++ii)
|
|
569 |
{
|
|
570 |
gh_model[ii] = gh_Schmidt1[ii] + factor * gh_Schmidt2[ii];
|
|
571 |
}
|
|
572 |
return(nmax);
|
|
573 |
}
|
|
574 |
|
|
575 |
/****************************************************************************/
|
|
576 |
/* */
|
|
577 |
/* Subroutine interpsh */
|
|
578 |
/* */
|
|
579 |
/****************************************************************************/
|
|
580 |
/* */
|
|
581 |
/* Interpolates linearly, in time, between two spherical harmonic */
|
|
582 |
/* models. */
|
|
583 |
/* */
|
|
584 |
/* Input: */
|
|
585 |
/* date - date of resulting model (in decimal year) */
|
|
586 |
/* dte1 - date of earlier model */
|
|
587 |
/* nmax1 - maximum degree and order of earlier model */
|
|
588 |
/* gh1 - Schmidt quasi-normal internal spherical */
|
|
589 |
/* harmonic coefficients of earlier model */
|
|
590 |
/* dte2 - date of later model */
|
|
591 |
/* nmax2 - maximum degree and order of later model */
|
|
592 |
/* gh2 - Schmidt quasi-normal internal spherical */
|
|
593 |
/* harmonic coefficients of internal model */
|
|
594 |
/* */
|
|
595 |
/* Output: */
|
|
596 |
/* gha or b - coefficients of resulting model */
|
|
597 |
/* nmax - maximum degree and order of resulting model */
|
|
598 |
/* */
|
|
599 |
/* FORTRAN */
|
|
600 |
/* A. Zunde */
|
|
601 |
/* USGS, MS 964, box 25046 Federal Center, Denver, CO. 80225 */
|
|
602 |
/* */
|
|
603 |
/* C */
|
|
604 |
/* C. H. Shaffer */
|
|
605 |
/* Lockheed Missiles and Space Company, Sunnyvale CA */
|
|
606 |
/* August 17, 1988 */
|
|
607 |
/* */
|
|
608 |
/****************************************************************************/
|
|
609 |
|
|
610 |
/* gh_Schmidt[12] are input arrays of Schmidt coefficients: gh1, gh2 */
|
|
611 |
/* gh_model is an output array of model coefficients: gha or ghb */
|
|
612 |
|
|
613 |
int interpsh(double date, double dte1, int nmax1, double dte2, double nmax2,
|
|
614 |
double *gh_Schmidt1, double *gh_Schmidt2, double *gh_model)
|
|
615 |
{
|
|
616 |
int nmax;
|
|
617 |
int k, l;
|
|
618 |
int ii;
|
|
619 |
double factor;
|
|
620 |
|
|
621 |
factor = (date - dte1) / (dte2 - dte1);
|
|
622 |
if (nmax1 == nmax2)
|
|
623 |
{
|
|
624 |
k = nmax1 * (nmax1 + 2);
|
|
625 |
nmax = nmax1;
|
|
626 |
}
|
|
627 |
else
|
|
628 |
{
|
|
629 |
if (nmax1 > nmax2)
|
|
630 |
{
|
|
631 |
k = nmax2 * (nmax2 + 2);
|
|
632 |
l = nmax1 * (nmax1 + 2);
|
|
633 |
for ( ii = k + 1; ii <= l; ++ii)
|
|
634 |
{
|
|
635 |
gh_model[ii] = gh_Schmidt1[ii] * (1 - factor);
|
|
636 |
}
|
|
637 |
nmax = nmax1;
|
|
638 |
}
|
|
639 |
else
|
|
640 |
{
|
|
641 |
k = nmax1 * (nmax1 + 2);
|
|
642 |
l = nmax2 * (nmax2 + 2);
|
|
643 |
for ( ii = k + 1; ii <= l; ++ii)
|
|
644 |
{
|
|
645 |
gh_model[ii] = factor * gh_Schmidt2[ii];
|
|
646 |
}
|
|
647 |
nmax = nmax2;
|
|
648 |
}
|
|
649 |
}
|
|
650 |
for ( ii = 1; ii <= k; ++ii)
|
|
651 |
{
|
|
652 |
gh_model[ii] = gh_Schmidt1[ii] +
|
|
653 |
factor * (gh_Schmidt2[ii] - gh_Schmidt1[ii]);
|
|
654 |
}
|
|
655 |
return(nmax);
|
|
656 |
}
|
|
657 |
|
|
658 |
|
|
659 |
|
|
660 |
|
|
661 |
|
|
662 |
/****************************************************************************/
|
|
663 |
/* */
|
|
664 |
/* Subroutine shval3 */
|
|
665 |
/* */
|
|
666 |
/****************************************************************************/
|
|
667 |
/* */
|
|
668 |
/* Calculates field components from spherical harmonic (sh) */
|
|
669 |
/* models. */
|
|
670 |
/* */
|
|
671 |
/* Input: */
|
|
672 |
/* igdgc - indicates coordinate system used; set equal */
|
|
673 |
/* to 1 if geodetic, 2 if geocentric */
|
|
674 |
/* latitude - north latitude, in degrees */
|
|
675 |
/* longitude - east longitude, in degrees */
|
|
676 |
/* elev - WGS84 altitude above mean sea level (igdgc=1), or */
|
|
677 |
/* radial distance from earth's center (igdgc=2) */
|
|
678 |
/* nmax - maximum degree and order of coefficients */
|
|
679 |
/* iext - external coefficients flag (=0 if none) */
|
|
680 |
/* ext1,2,3 - the three 1st-degree external coefficients */
|
|
681 |
/* (not used if iext = 0) */
|
|
682 |
/* */
|
|
683 |
/* Output: */
|
|
684 |
/* x - northward component */
|
|
685 |
/* y - eastward component */
|
|
686 |
/* z - vertically-downward component */
|
|
687 |
/* */
|
|
688 |
/* based on subroutine 'igrf' by D. R. Barraclough and S. R. C. Malin, */
|
|
689 |
/* report no. 71/1, institute of geological sciences, U.K. */
|
|
690 |
/* */
|
|
691 |
/* FORTRAN */
|
|
692 |
/* Norman W. Peddie */
|
|
693 |
/* USGS, MS 964, box 25046 Federal Center, Denver, CO. 80225 */
|
|
694 |
/* */
|
|
695 |
/* C */
|
|
696 |
/* C. H. Shaffer */
|
|
697 |
/* Lockheed Missiles and Space Company, Sunnyvale CA */
|
|
698 |
/* August 17, 1988 */
|
|
699 |
/* */
|
|
700 |
/****************************************************************************/
|
|
701 |
|
|
702 |
|
|
703 |
/* gh is the array of model coefficients--gha or ghb */
|
|
704 |
|
|
705 |
void shval3(int igdgc, double flat, double flon, double elev,
|
|
706 |
int nmax, double *gh, int iext,
|
|
707 |
double ext1, double ext2, double ext3,
|
|
708 |
double *x_ptr, double *y_ptr, double *z_ptr)
|
|
709 |
{
|
|
710 |
double earths_radius = 6371.2;
|
|
711 |
double dtr = 0.01745329;
|
|
712 |
double slat;
|
|
713 |
double clat;
|
|
714 |
double ratio;
|
|
715 |
double aa, bb, cc, dd;
|
|
716 |
double sd;
|
|
717 |
double cd;
|
|
718 |
double r;
|
|
719 |
double a2;
|
|
720 |
double b2;
|
|
721 |
double rr;
|
|
722 |
double fm,fn;
|
|
723 |
double sl[14];
|
|
724 |
double cl[14];
|
|
725 |
double p[119];
|
|
726 |
double q[119];
|
|
727 |
int ii,j,k,l,m,n;
|
|
728 |
int npq;
|
|
729 |
double power;
|
|
730 |
|
|
731 |
double x, y, z;
|
|
732 |
|
|
733 |
/* a2,b2 - squares of semi-major and semi-minor axes of */
|
|
734 |
/* the reference spheroid used for transforming */
|
|
735 |
/* between geodetic and geocentric coordinates */
|
|
736 |
/* or components */
|
|
737 |
a2 = 40680631.59; /* WGS84 */
|
|
738 |
b2 = 40408299.98; /* WGS84 */
|
|
739 |
|
|
740 |
r = elev;
|
|
741 |
slat = sin(flat * dtr );
|
|
742 |
if ((90.0 - flat) < 0.001)
|
|
743 |
{
|
|
744 |
aa = 89.999; /* 300 ft. from North pole */
|
|
745 |
}
|
|
746 |
else
|
|
747 |
{
|
|
748 |
if ((90.0 + flat) < 0.001)
|
|
749 |
{
|
|
750 |
aa = -89.999; /* 300 ft. from South pole */
|
|
751 |
}
|
|
752 |
else
|
|
753 |
{
|
|
754 |
aa = flat;
|
|
755 |
}
|
|
756 |
}
|
|
757 |
clat = cos(aa * dtr);
|
|
758 |
sl[1] = sin(flon*dtr);
|
|
759 |
cl[1] = cos(flon*dtr);
|
|
760 |
|
|
761 |
x = y = z = 0.0;
|
|
762 |
|
|
763 |
sd = 0.0;
|
|
764 |
cd = 1.0;
|
|
765 |
|
|
766 |
l = 1;
|
|
767 |
n = 0;
|
|
768 |
m = 1;
|
|
769 |
|
|
770 |
npq = (nmax * (nmax + 3)) / 2;
|
|
771 |
|
|
772 |
if (igdgc == 1)
|
|
773 |
{
|
|
774 |
aa = a2 * clat * clat;
|
|
775 |
bb = b2 * slat * slat;
|
|
776 |
cc = aa + bb;
|
|
777 |
dd = sqrt(cc);
|
|
778 |
r = sqrt(elev * (elev + 2.0 * dd) + (a2 * aa + b2 * bb) / cc);
|
|
779 |
cd = (elev + dd) / r;
|
|
780 |
sd = (a2 - b2) / dd * slat * clat / r;
|
|
781 |
aa = slat;
|
|
782 |
slat = slat * cd - clat * sd;
|
|
783 |
clat = clat * cd + aa * sd;
|
|
784 |
}
|
|
785 |
ratio = earths_radius / r;
|
|
786 |
|
|
787 |
aa = sqrt(3.0);
|
|
788 |
p[1] = 2.0 * slat;
|
|
789 |
p[2] = 2.0 * clat;
|
|
790 |
p[3] = 4.5 * slat * slat - 1.5;
|
|
791 |
p[4] = 3.0 * aa * clat * slat;
|
|
792 |
q[1] = -clat;
|
|
793 |
q[2] = slat;
|
|
794 |
q[3] = -3.0 * clat * slat;
|
|
795 |
q[4] = aa * (slat * slat - clat * clat);
|
|
796 |
|
|
797 |
for ( k = 1; k <= npq; ++k)
|
|
798 |
{
|
|
799 |
if (n < m)
|
|
800 |
{
|
|
801 |
m = 0;
|
|
802 |
n = n + 1;
|
|
803 |
power = n + 2;
|
|
804 |
rr = pow(ratio,power);
|
|
805 |
fn = n;
|
|
806 |
}
|
|
807 |
fm = m;
|
|
808 |
if (k >= 5)
|
|
809 |
{
|
|
810 |
if (m == n)
|
|
811 |
{
|
|
812 |
aa = sqrt(1.0 - 0.5/fm);
|
|
813 |
j = k - n - 1;
|
|
814 |
p[k] = (1.0 + 1.0/fm) * aa * clat * p[j];
|
|
815 |
q[k] = aa * (clat * q[j] + slat/fm * p[j]);
|
|
816 |
sl[m] = sl[m-1] * cl[1] + cl[m-1] * sl[1];
|
|
817 |
cl[m] = cl[m-1] * cl[1] - sl[m-1] * sl[1];
|
|
818 |
}
|
|
819 |
else
|
|
820 |
{
|
|
821 |
aa = sqrt(fn*fn - fm*fm);
|
|
822 |
bb = sqrt(((fn - 1.0)*(fn-1.0)) - (fm * fm)) / aa;
|
|
823 |
cc = (2.0 * fn - 1.0)/aa;
|
|
824 |
ii = k - n;
|
|
825 |
j = k - 2 * n + 1;
|
|
826 |
p[k] = (fn + 1.0) * (cc * slat/fn * p[ii] - bb/(fn - 1.0) * p[j]);
|
|
827 |
q[k] = cc * (slat * q[ii] - clat/fn * p[ii]) - bb * q[j];
|
|
828 |
}
|
|
829 |
}
|
|
830 |
aa = rr * gh[l];
|
|
831 |
if (m == 0)
|
|
832 |
{
|
|
833 |
x = x + aa * q[k];
|
|
834 |
z = z - aa * p[k];
|
|
835 |
l = l + 1;
|
|
836 |
}
|
|
837 |
else
|
|
838 |
{
|
|
839 |
bb = rr * gh[l+1];
|
|
840 |
cc = aa * cl[m] + bb * sl[m];
|
|
841 |
x = x + cc * q[k];
|
|
842 |
z = z - cc * p[k];
|
|
843 |
if (clat > 0)
|
|
844 |
{
|
|
845 |
y = y + (aa * sl[m] - bb * cl[m]) *
|
|
846 |
fm * p[k]/((fn + 1.0) * clat);
|
|
847 |
}
|
|
848 |
else
|
|
849 |
{
|
|
850 |
y = y + (aa * sl[m] - bb * cl[m]) * q[k] * slat;
|
|
851 |
}
|
|
852 |
l = l + 2;
|
|
853 |
}
|
|
854 |
m = m + 1;
|
|
855 |
}
|
|
856 |
if (iext != 0)
|
|
857 |
{
|
|
858 |
aa = ext2 * cl[1] + ext3 * sl[1];
|
|
859 |
x = x - ext1 * clat + aa * slat;
|
|
860 |
y = y + ext2 * sl[1] - ext3 * cl[1];
|
|
861 |
z = z + ext1 * slat + aa * clat;
|
|
862 |
}
|
|
863 |
aa = x;
|
|
864 |
x = x * cd + z * sd;
|
|
865 |
z = z * cd - aa * sd;
|
|
866 |
|
|
867 |
*x_ptr = x;
|
|
868 |
*y_ptr = y;
|
|
869 |
*z_ptr = z;
|
|
870 |
}
|
|
871 |
|
|
872 |
|
|
873 |
/****************************************************************************/
|
|
874 |
/* */
|
|
875 |
/* Subroutine dihf */
|
|
876 |
/* */
|
|
877 |
/****************************************************************************/
|
|
878 |
/* */
|
|
879 |
/* Computes the geomagnetic d, i, h, and f from x, y, and z. */
|
|
880 |
/* */
|
|
881 |
/* Input: */
|
|
882 |
/* x - northward component */
|
|
883 |
/* y - eastward component */
|
|
884 |
/* z - vertically-downward component */
|
|
885 |
/* */
|
|
886 |
/* Output: */
|
|
887 |
/* d - declination */
|
|
888 |
/* i - inclination */
|
|
889 |
/* h - horizontal intensity */
|
|
890 |
/* f - total intensity */
|
|
891 |
/* */
|
|
892 |
/* FORTRAN */
|
|
893 |
/* A. Zunde */
|
|
894 |
/* USGS, MS 964, box 25046 Federal Center, Denver, CO. 80225 */
|
|
895 |
/* */
|
|
896 |
/* C */
|
|
897 |
/* C. H. Shaffer */
|
|
898 |
/* Lockheed Missiles and Space Company, Sunnyvale CA */
|
|
899 |
/* August 22, 1988 */
|
|
900 |
/* */
|
|
901 |
/****************************************************************************/
|
|
902 |
|
|
903 |
void dihf(double x, double y, double z,
|
|
904 |
double *d_ptr, double *i_ptr, double *h_ptr, double *f_ptr)
|
|
905 |
{
|
|
906 |
double d, i, h, f;
|
|
907 |
double h2;
|
|
908 |
double hpx;
|
|
909 |
double sn = 0.0001; /* constant threshold */
|
|
910 |
|
|
911 |
h2 = x*x + y*y;
|
|
912 |
h = sqrt(h2); /* calculate horizontal intensity */
|
|
913 |
f = sqrt(h2 + z*z); /* calculate total intensity */
|
|
914 |
if (f < sn)
|
|
915 |
{
|
|
916 |
d = NaN; /* If d and i cannot be determined, */
|
|
917 |
i = NaN; /* set equal to NaN */
|
|
918 |
}
|
|
919 |
else
|
|
920 |
{
|
|
921 |
i = atan2(z, h);
|
|
922 |
if (h < sn)
|
|
923 |
{
|
|
924 |
d = NaN;
|
|
925 |
}
|
|
926 |
else
|
|
927 |
{
|
|
928 |
hpx = h + x;
|
|
929 |
if (hpx < sn)
|
|
930 |
{
|
|
931 |
d = M_PI;
|
|
932 |
}
|
|
933 |
else
|
|
934 |
{
|
|
935 |
d = 2.0 * atan2(y, hpx);
|
|
936 |
}
|
|
937 |
}
|
|
938 |
}
|
|
939 |
*d_ptr = d;
|
|
940 |
*i_ptr = i;
|
|
941 |
*h_ptr = h;
|
|
942 |
*f_ptr = f;
|
|
943 |
}
|
|
944 |
|