-
Notifications
You must be signed in to change notification settings - Fork 4
/
Class_IGRF11.cpp
1319 lines (1213 loc) · 48.1 KB
/
Class_IGRF11.cpp
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------
Purpose: C++ Class to compute geomagnetic components (X, Y, Z) from IGRF 11 model
-------------------------------------------------------------------
Author: Haroldo Antonio Marques
Programa de Pos-Graduacao em Ciencias Cartograficas
FCT/UNESP - Presidente Prudente - SP, Brazil
FAPESP PROCESS: 05/03522-1
Date: August of 2010
-------------------------------------------------------------------
Observation: This class was developed to suport the source code available at:
http://www.ngdc.noaa.gov/IAGA/vmod/igrf.html
-------------------------------------------------------------------*/
#include "Class_IGRF11.h"
namespace IGRF11{
//------------------------------------------------------------------------------------
Class_IGRF11::Class_IGRF11()
{/*-------------------------------------------------------------------
Purpose: Class constructor
-------------------------------------------------------------------
Input:
Output:
-------------------------------------------------------------------
Authors: Haroldo Antonio Marques
Programa de Pos-Graduacao em Ciencias Cartograficas
FCT/UNESP - Presidente Prudente - SP
Date: January of 2010
-------------------------------------------------------------------
Observation:
-------------------------------------------------------------------*/
stream = NULL;
x=0;
y=0;
z=0;
d=0;
f=0;
h=0;
i=0;
need_to_read_model = 1;
need_to_interp =1;
/* Control variables */
again = 1;
decyears = 3;
units = 4;
decdeg = 3;
range = -1;
counter = 0;
igdgc=3;
isyear=-1;
ismonth=-1;
isday=-1;
ieyear=-1;
iemonth=-1;
ieday=-1;
ilat_deg=200;
ilat_min=200;
ilat_sec=200;
ilon_deg=200;
ilon_min=200;
ilon_sec=200;
coords_from_file = 0;
arg_err = 0;
iline=0;
alt=-999999;
sdate=-1;
step=-1;
edate=-1;
}
//------------------------------------------------------------------------------------
Class_IGRF11::~Class_IGRF11()//destructor
{ /*-------------------------------------------------------------------
Purpose: Class Destructor
-------------------------------------------------------------------
Input:
Output:
-------------------------------------------------------------------
Author: Haroldo Antonio Marques
Programa de Pos-Graduacao em Ciencias Cartograficas
FCT/UNESP - Presidente Prudente - SP, Brazil
FAPESP PROCESS: 05/03522-1
Date: July of 2007
-------------------------------------------------------------------
Observation:
-------------------------------------------------------------------*/
//Insert code here
}
//------------------------------------------------------------------------------------
void Class_IGRF11::Set_X(double value)
{
x = value;
}
//------------------------------------------------------------------------------------
void Class_IGRF11::Set_Y(double value)
{
y = value;
}
//------------------------------------------------------------------------------------
void Class_IGRF11::Set_Z(double value)
{
z = value;
}
//------------------------------------------------------------------------------------
void Class_IGRF11::Set_d(double value)
{
d = value;
}
//------------------------------------------------------------------------------------
void Class_IGRF11::Set_f(double value)
{
f = value;
}
//------------------------------------------------------------------------------------
void Class_IGRF11::Set_h(double value)
{
h = value;
}
//------------------------------------------------------------------------------------
void Class_IGRF11::Set_i(double value)
{
i = value;
}
//------------------------------------------------------------------------------------
double Class_IGRF11::Get_X()
{
return x;
}
//------------------------------------------------------------------------------------
double Class_IGRF11::Get_Y()
{
return y;
}
//------------------------------------------------------------------------------------
double Class_IGRF11::Get_Z()
{
return z;
}
//------------------------------------------------------------------------------------
double Class_IGRF11::Get_d()
{
return d;
}
//------------------------------------------------------------------------------------
double Class_IGRF11::Get_f()
{
return f;
}
//------------------------------------------------------------------------------------
double Class_IGRF11::Get_h()
{
return h;
}
//------------------------------------------------------------------------------------
double Class_IGRF11::Get_i()
{
return i;
}
//------------------------------------------------------------------------------------
int Class_IGRF11::IGRF11(int year, int month, int day, int hour, int min, double sec,
double latitude, double longitude, double h,char Name_IGRF_Model[])
{
//Original source code available at: http://www.ngdc.noaa.gov/IAGA/vmod/igrf.html
//Adapted by Marques, H. A., 2010
/* Variable declaration */
warn_H = 0;
warn_H_val = 99999.0;
warn_H_strong = 0;
warn_H_strong_val = 99999.0;
warn_P = 0;
//strcpy(mdfile,".\\IGRF11\\IGRF11.COF");
//Obtain the desired model file and read the data */
if(need_to_read_model)
if(Read_IGRF_Model(Name_IGRF_Model,minyr,maxyr,modelI,nmodel,yrmax,altmin,
altmax,max1,max2,max3,irec_pos, model,epoch,yrmin) )
{
need_to_read_model = 0;
}
else
return 0;
// if date specified in command line then warn if past end of validity
if ((sdate>maxyr)&&(sdate<maxyr+1))
{
printf("\nWarning: The date %4.2f is out of range,\n", sdate);
printf(" but still within one year of model expiration date.\n");
printf(" An updated model file is available before 1.1.%4.0f\n",maxyr);
}
//----------------------------------------------------------------
// printf("\nHow would you like to enter the date?\n");
// printf(" 1) In decimal years.\n");
// printf(" 2) In year, month, and day.\n");
decyears = 1;
//----------------------------------------------------------------
// printf("\nWould you like output for a single date or for a range of dates?\n");
//printf(" 1) A single date.\n");
//printf(" 2) A range of dates.\n");
range = 1;
//----------------------------------------------------------------
//if (decyears==1)
//{
// printf("\nEnter the decimal date (%4.2f to %4.0f): ",minyr, maxyr);
sdate = julday(month,day,year);
sdate += double(hour + min/60.0+sec/3600.0)/24.0;
//}
if ((sdate<minyr)||(sdate>=maxyr+1))
{
ismonth=isday=isyear=0;
printf("\nError: The date %4.2f is out of range.\n", sdate);
}
if ((sdate>maxyr)&&(sdate<maxyr+1))
{
printf("\nWarning: The date %4.2f is out of range,\n", sdate);
printf(" but still within one year of model expiration date.\n");
printf(" An updated model file is available before 1.1.%4.0f\n",maxyr);
}
// Pick model */
for (modelI=0; modelI<nmodel; modelI++)
if (sdate<yrmax[modelI]) break;
if (modelI == nmodel) modelI--; // if beyond end of last model use last model */
// Get altitude min and max for selected model. */
minalt=altmin[modelI];
maxalt=altmax[modelI];
//printf("\n\nEnter Coordinate Preferences:");
//printf("\n 1) Geodetic (WGS84 latitude and altitude above mean sea level)");
//printf("\n 2) Geocentric (spherical, altitude relative to Earth's center)\n");
igdgc = 2;
// If needed modify ranges to reflect coords. */
if (igdgc==2)
{
minalt+=6371.2; // Add radius to ranges. */
maxalt+=6371.2;
}
// Get unit prefs */
if (igdgc==1)
{
//while ((units>3)||(units<1))
//{
// printf("\n\nEnter Unit Preferences:");
// printf("\n 1) Kilometers");
// printf("\n 2) Meters");
// printf("\n 3) Feet\n");
// printf("\n ==> ");
units = 2;
//}
}
else units = 1; // geocentric always in km */
// Do unit conversions if neccessary */
if (units==2)
{
minalt*=1000.0;
maxalt*=1000.0;
}
else if (units==3)
{
minalt*=FT2KM;
maxalt*=FT2KM;
}
//if (igdgc==2) printf("\n\nEnter geocentric altitude in km (%.2f to %.2f): ", minalt, maxalt);
//if (igdgc==1 && units==1) printf("\n\nEnter geodetic altitude above mean sea level in km (%.2f to %.2f): ", minalt, maxalt);
//if (igdgc==1 && units==2) printf("\n\nEnter geodetic altitude above mean sea level in meters (%.2f to %.2f): ", minalt, maxalt);
//if (igdgc==1 && units==3) printf("\n\nEnter geodetic altitude above mean sea level in feet (%.2f to %.2f): ", minalt, maxalt);
//alt = 6378.137+450.000;
//alt = 6378.137+(51.2408/1000);
alt = 6378.137+h;
/* Convert altitude to km */
if (units==2)
{
alt *= 0.001;
}
else if (units==3)
{
alt /= FT2KM;
}
//printf("\n\nHow would you like to enter the latitude and longitude?:");
//printf("\n 1) In decimal degrees.");
//printf("\n 2) In degrees, minutes, and seconds.\n");
decdeg = 1;
if (decdeg==1)
{
//printf("\n\nEnter the decimal latitude (-90 to 90) (- for Southern hemisphere).\n");
//printf("\n\nEnter the decimal longitude (-180 to 180) (- for Western hemisphere).\n");
} /* if (decdeg==1) */
if(need_to_interp)
/** This will compute everything needed for 1 point in time. **/
if (max2[modelI] == 0)
{
getshc(Name_IGRF_Model, 1, irec_pos[modelI], max1[modelI], 1);
getshc(Name_IGRF_Model, 1, irec_pos[modelI+1], max1[modelI+1], 2);
nmax = interpsh(sdate, yrmin[modelI], max1[modelI],
yrmin[modelI+1], max1[modelI+1], 3);
nmax = interpsh(sdate+1, yrmin[modelI] , max1[modelI],
yrmin[modelI+1], max1[modelI+1],4);
need_to_interp=0;
}
else
{
getshc(Name_IGRF_Model, 1, irec_pos[modelI], max1[modelI], 1);
getshc(Name_IGRF_Model, 0, irec_pos[modelI], max2[modelI], 2);
nmax = extrapsh(sdate, epoch[modelI], max1[modelI], max2[modelI], 3);
nmax = extrapsh(sdate+1, epoch[modelI], max1[modelI], max2[modelI], 4);
need_to_interp=0;
}
/* Do the first calculations */
shval3(igdgc, latitude, longitude, alt, nmax, 3,
IEXT, EXT_COEFF1, EXT_COEFF2, EXT_COEFF3);
dihf(3);
shval3(igdgc, latitude, longitude, alt, nmax, 4,
IEXT, EXT_COEFF1, EXT_COEFF2, EXT_COEFF3);
dihf(4);
ddot = ((dtemp - d)*RAD2DEG);
if (ddot > 180.0) ddot -= 360.0;
if (ddot <= -180.0) ddot += 360.0;
ddot *= 60.0;
idot = ((itemp - i)*RAD2DEG)*60;
d = d*(RAD2DEG); i = i*(RAD2DEG);
hdot = htemp - h; xdot = xtemp - x;
ydot = ytemp - y; zdot = ztemp - z;
fdot = ftemp - f;
/* deal with geographic and magnetic poles */
if (h < 100.0) /* at magnetic poles */
{
d = NaN;
ddot = NaN;
/* while rest is ok */
}
if (h < 1000.0)
{
warn_H = 0;
warn_H_strong = 1;
if (h<warn_H_strong_val) warn_H_strong_val = h;
}
else if (h < 5000.0 && !warn_H_strong)
{
warn_H = 1;
if (h<warn_H_val) warn_H_val = h;
}
if (90.0-fabs(latitude) <= 0.001) /* at geographic poles */
{
x = NaN;
y = NaN;
d = NaN;
xdot = NaN;
ydot = NaN;
ddot = NaN;
warn_P = 1;
warn_H = 0;
warn_H_strong = 0;
/* while rest is ok */
}
/** Above will compute everything for 1 point in time. **/
//printf("\n\n\n Model: %s \n", model[modelI]);
// if (decdeg==1)
// {
// printf(" Latitude: %4.2f deg\n", latitude);
// printf(" Longitude: %4.2f deg\n", longitude);
// }
// else
// {
// printf(" Latitude: %d deg, %d min, %d sec\n",
// ilat_deg,ilat_min, ilat_sec);
// printf(" Longitude: %d deg, %d min, %d sec\n",
// ilon_deg, ilon_min, ilon_sec);
// }
// printf(" Altitude: ");
// if (units==1)
// printf("%.2f km\n", alt);
// else if (units==2)
// printf("%.2f meters\n", alt*1000.0);
// else
// printf("%.2f ft\n", (alt*FT2KM));
//if (range==1)
//{
// printf(" Date of Interest: ");
// if (decyears==1)
// printf(" %4.2f\n\n", sdate);
// else
// printf("%d-%d-%d (yyyy-mm-dd)\n\n", isyear, ismonth, isday);
// printf(" Date of Interest: ");
// if (decyears==1)
// printf(" %4.2f\n\n", sdate);
// else
// printf("%d-%d-%d (yyyy-mm-dd)\n\n", isyear, ismonth, isday);
//print_header();
//print_result(sdate,d, i, h, x, y, z, f);
//print_long_dashed_line();
//print_header_sv();
//print_result_sv(sdate,ddot,idot,hdot,xdot,ydot,zdot,fdot);
//print_dashed_line();
//} /* if range == 1 */
//system("pause");
return 1;
}
//------------------------------------------------------------------------------------
int Class_IGRF11::Read_IGRF_Model(char Name_IGRF_Model[],
double &minyr,
double &maxyr,
int &modelI, /* Which model (Index) */
int &nmodel, /* Number of models in file */
double yrmax[MAXMOD],
double altmin[MAXMOD],
double altmax[MAXMOD],
int max1[MAXMOD],
int max2[MAXMOD],
int max3[MAXMOD],
long irec_pos[MAXMOD],
char model[MAXMOD][9],
double epoch[MAXMOD],
double yrmin[MAXMOD])
{
//Original source code available at: http://www.ngdc.noaa.gov/IAGA/vmod/igrf.html
//Adapted by Marques, H. A., 2010
char inbuff[MAXINBUFF];
int fileline;
/* Initializations. */
inbuff[MAXREAD+1]='\0'; /* Just to protect mem. */
inbuff[MAXINBUFF-1]='\0'; /* Just to protect mem. */
if (!(stream = fopen(Name_IGRF_Model, "rt")))
{ printf("\nError opening file %s.", Name_IGRF_Model);
exit;
}
rewind(stream);
fileline = 0; /* First line will be 1 */
modelI = -1;
while (fgets(inbuff,MAXREAD,stream)) /* While not end of file
* read to end of line or buffer */
{
fileline++; /* On new line */
if (strlen(inbuff) != RECL) /* IF incorrect record size */
{
printf("Corrupt record in file %s on line %d.\n", Name_IGRF_Model, fileline);
fclose(stream);
exit(5);
}
/* old statement Dec 1999 */
/* if (!strncmp(inbuff," ",4)){ /* If 1st 4 chars are spaces */
/* New statement Dec 1999 changed by wmd required by year 2000 models */
if (!strncmp(inbuff," ",3)) /* If 1st 3 chars are spaces */
{
modelI++; /* New model */
if (modelI > MAXMOD) /* If too many headers */
{
printf("Too many models in file %s on line %d.", Name_IGRF_Model, fileline);
fclose(stream);
exit(6);
}
irec_pos[modelI]=ftell(stream);
/* Get fields from buffer into individual vars. */
sscanf(inbuff, "%s%lg%d%d%d%lg%lg%lg%lg", model[modelI], &epoch[modelI],
&max1[modelI], &max2[modelI], &max3[modelI], &yrmin[modelI],
&yrmax[modelI], &altmin[modelI], &altmax[modelI]);
/* Compute date range for all models */
if (modelI == 0) /*If first model */
{
minyr=yrmin[0];
maxyr=yrmax[0];
}
else
{
if (yrmin[modelI]<minyr)
{
minyr=yrmin[modelI];
}
if (yrmax[modelI]>maxyr){
maxyr=yrmax[modelI];
}
} /* if modelI != 0 */
} /* If 1st 3 chars are spaces */
} /* While not end of model file */
nmodel = modelI + 1;
fclose(stream);
return 1;
}
//------------------------------------------------------------------------------------
/****************************************************************************/
/* */
/* Subroutine getshc */
/* */
/****************************************************************************/
/* */
/* Reads spherical harmonic coefficients from the specified */
/* model into an array. */
/* */
/* Input: */
/* stream - Logical unit number */
/* iflag - Flag for SV equal to ) or not equal to 0 */
/* for designated read statements */
/* strec - Starting record number to read from model */
/* nmax_of_gh - Maximum degree and order of model */
/* */
/* Output: */
/* gh1 or 2 - Schmidt quasi-normal internal spherical */
/* harmonic coefficients */
/* */
/* FORTRAN */
/* Bill Flanagan */
/* NOAA CORPS, DESDIS, NGDC, 325 Broadway, Boulder CO. 80301 */
/* */
/* C */
/* C. H. Shaffer */
/* Lockheed Missiles and Space Company, Sunnyvale CA */
/* August 15, 1988 */
/* */
/****************************************************************************/
int Class_IGRF11::getshc(char file[PATH],int iflag,long int strec,int nmax_of_gh,int gh)
{
char inbuff[MAXINBUFF];
char irat[9];
int ii,m,n,mm,nn;
int ios;
int line_num;
double g,hh;
double trash;
stream = fopen(file, "rt");
if (stream == NULL)
{
printf("\nError on opening file %s", file);
}
else
{
ii = 0;
ios = 0;
fseek(stream,strec,SEEK_SET);
for ( nn = 1; nn <= nmax_of_gh; ++nn)
{
for (mm = 0; mm <= nn; ++mm)
{
if (iflag == 1)
{
fgets(inbuff, MAXREAD, stream);
sscanf(inbuff, "%d%d%lg%lg%lg%lg%s%d",
&n, &m, &g, &hh, &trash, &trash, irat, &line_num);
}
else
{
fgets(inbuff, MAXREAD, stream);
sscanf(inbuff, "%d%d%lg%lg%lg%lg%s%d",
&n, &m, &trash, &trash, &g, &hh, irat, &line_num);
}
if ((nn != n) || (mm != m))
{
ios = -2;
fclose(stream);
return(ios);
}
ii = ii + 1;
switch(gh)
{
case 1: gh1[ii] = g;
break;
case 2: gh2[ii] = g;
break;
default: printf("\nError in subroutine getshc");
break;
}
if (m != 0)
{
ii = ii+ 1;
switch(gh)
{
case 1: gh1[ii] = hh;
break;
case 2: gh2[ii] = hh;
break;
default: printf("\nError in subroutine getshc");
break;
}
}
}
}
}
fclose(stream);
return(ios);
}
//------------------------------------------------------------------------------------
/****************************************************************************/
/* */
/* Subroutine extrapsh */
/* */
/****************************************************************************/
/* */
/* Extrapolates linearly a spherical harmonic model with a */
/* rate-of-change model. */
/* */
/* Input: */
/* date - date of resulting model (in decimal year) */
/* dte1 - date of base model */
/* nmax1 - maximum degree and order of base model */
/* gh1 - Schmidt quasi-normal internal spherical */
/* harmonic coefficients of base model */
/* nmax2 - maximum degree and order of rate-of-change model */
/* gh2 - Schmidt quasi-normal internal spherical */
/* harmonic coefficients of rate-of-change model */
/* */
/* Output: */
/* gha or b - 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 Class_IGRF11::extrapsh(double date, double dte1, int nmax1, int nmax2, int gh)
{
int nmax;
int k, l;
int ii;
double factor;
factor = date - dte1;
if (nmax1 == nmax2)
{
k = nmax1 * (nmax1 + 2);
nmax = nmax1;
}
else
{
if (nmax1 > nmax2)
{
k = nmax2 * (nmax2 + 2);
l = nmax1 * (nmax1 + 2);
switch(gh)
{
case 3: for ( ii = k + 1; ii <= l; ++ii)
{
gha[ii] = gh1[ii];
}
break;
case 4: for ( ii = k + 1; ii <= l; ++ii)
{
ghb[ii] = gh1[ii];
}
break;
default: printf("\nError in subroutine extrapsh");
break;
}
nmax = nmax1;
}
else
{
k = nmax1 * (nmax1 + 2);
l = nmax2 * (nmax2 + 2);
switch(gh)
{
case 3: for ( ii = k + 1; ii <= l; ++ii)
{
gha[ii] = factor * gh2[ii];
}
break;
case 4: for ( ii = k + 1; ii <= l; ++ii)
{
ghb[ii] = factor * gh2[ii];
}
break;
default: printf("\nError in subroutine extrapsh");
break;
}
nmax = nmax2;
}
}
switch(gh)
{
case 3: for ( ii = 1; ii <= k; ++ii)
{
gha[ii] = gh1[ii] + factor * gh2[ii];
}
break;
case 4: for ( ii = 1; ii <= k; ++ii)
{
ghb[ii] = gh1[ii] + factor * gh2[ii];
}
break;
default: printf("\nError in subroutine extrapsh");
break;
}
return(nmax);
}
//------------------------------------------------------------------------------------
/****************************************************************************/
/* */
/* Subroutine interpsh */
/* */
/****************************************************************************/
/* */
/* Interpolates linearly, in time, between two spherical harmonic */
/* models. */
/* */
/* Input: */
/* date - date of resulting model (in decimal year) */
/* dte1 - date of earlier model */
/* nmax1 - maximum degree and order of earlier model */
/* gh1 - Schmidt quasi-normal internal spherical */
/* harmonic coefficients of earlier model */
/* dte2 - date of later model */
/* nmax2 - maximum degree and order of later model */
/* gh2 - Schmidt quasi-normal internal spherical */
/* harmonic coefficients of internal model */
/* */
/* Output: */
/* gha or b - coefficients of resulting model */
/* 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 17, 1988 */
/* */
/****************************************************************************/
int Class_IGRF11::interpsh(double date, double dte1,int nmax1, double dte2,int nmax2,int gh)
{
int nmax;
int k, l;
int ii;
double factor;
factor = (date - dte1) / (dte2 - dte1);
if (nmax1 == nmax2)
{
k = nmax1 * (nmax1 + 2);
nmax = nmax1;
}
else
{
if (nmax1 > nmax2)
{
k = nmax2 * (nmax2 + 2);
l = nmax1 * (nmax1 + 2);
switch(gh)
{
case 3: for ( ii = k + 1; ii <= l; ++ii)
{
gha[ii] = gh1[ii] + factor * (-gh1[ii]);
}
break;
case 4: for ( ii = k + 1; ii <= l; ++ii)
{
ghb[ii] = gh1[ii] + factor * (-gh1[ii]);
}
break;
default: printf("\nError in subroutine extrapsh");
break;
}
nmax = nmax1;
}
else
{
k = nmax1 * (nmax1 + 2);
l = nmax2 * (nmax2 + 2);
switch(gh)
{
case 3: for ( ii = k + 1; ii <= l; ++ii)
{
gha[ii] = factor * gh2[ii];
}
break;
case 4: for ( ii = k + 1; ii <= l; ++ii)
{
ghb[ii] = factor * gh2[ii];
}
break;
default: printf("\nError in subroutine extrapsh");
break;
}
nmax = nmax2;
}
}
switch(gh)
{
case 3: for ( ii = 1; ii <= k; ++ii)
{
gha[ii] = gh1[ii] + factor * (gh2[ii] - gh1[ii]);
}
break;
case 4: for ( ii = 1; ii <= k; ++ii)
{
ghb[ii] = gh1[ii] + factor * (gh2[ii] - gh1[ii]);
}
break;
default: printf("\nError in subroutine extrapsh");
break;
}
return(nmax);
}
//------------------------------------------------------------------------------------
/****************************************************************************/
/* */
/* Subroutine shval3 */
/* */
/****************************************************************************/
/* */
/* Calculates field components from spherical harmonic (sh) */
/* models. */
/* */
/* Input: */
/* igdgc - indicates coordinate system used; set equal */
/* to 1 if geodetic, 2 if geocentric */
/* latitude - north latitude, in degrees */
/* longitude - east longitude, in degrees */
/* elev - WGS84 altitude above ellipsoid (igdgc=1), or */
/* radial distance from earth's center (igdgc=2) */
/* a2,b2 - squares of semi-major and semi-minor axes of */
/* the reference spheroid used for transforming */
/* between geodetic and geocentric coordinates */
/* or components */
/* nmax - maximum degree and order of coefficients */
/* iext - external coefficients flag (=0 if none) */
/* ext1,2,3 - 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. */
/* */
/* 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 */
/* */
/****************************************************************************/
int Class_IGRF11::shval3( int igdgc,double flat,double flon,double elev,int nmax,
int gh,int iext,double ext1,double ext2,double ext3)
{
double earths_radius = 6371.2;
double dtr = 0.01745329;
double slat;
double clat;
double ratio;
double aa, bb, cc, dd;
double sd;
double cd;
double r;
double a2;
double b2;
double rr;
double fm,fn;
double sl[14];
double cl[14];
double p[119];
double q[119];
int ii,j,k,l,m,n;
int npq;
int ios;
double argument;
double power;
a2 = 40680631.59; /* WGS84 */
b2 = 40408299.98; /* WGS84 */
ios = 0;
r = elev;
argument = flat * dtr;
slat = sin( argument );
if ((90.0 - flat) < 0.001)
{
aa = 89.999; /* 300 ft. from North pole */
}
else
{
if ((90.0 + flat) < 0.001)
{
aa = -89.999; /* 300 ft. from South pole */
}
else
{
aa = flat;
}
}
argument = aa * dtr;
clat = cos( argument );
argument = flon * dtr;
sl[1] = sin( argument );
cl[1] = cos( argument );
switch(gh)
{
case 3: x = 0;
y = 0;
z = 0;
break;
case 4: xtemp = 0;
ytemp = 0;
ztemp = 0;
break;
default: printf("\nError in subroutine shval3");
break;
}
sd = 0.0;
cd = 1.0;
l = 1;
n = 0;
m = 1;
npq = (nmax * (nmax + 3)) / 2;
if (igdgc == 1)
{