-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwmm2020.c
3186 lines (2713 loc) · 125 KB
/
wmm2020.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
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include "WMM2020.h"
/* $Id: GeomagnetismLibrary.c 1521 2017-01-24 17:52:41Z awoods $
*
* ABSTRACT
*
* The purpose of Geomagnetism Library is primarily to support the World Magnetic Model (WMM) 2015-2020.
* It however is built to be used for spherical harmonic models of the Earth's magnetic field
* generally and supports models even with a large (>>12) number of degrees. It is also used in many
* other geomagnetic models distributed by NCEI.
*
* REUSE NOTES
*
* Geomagnetism Library is intended for reuse by any application that requires
* Computation of Geomagnetic field from a spherical harmonic model.
*
* REFERENCES
*
* Further information on Geoid can be found in the WMM Technical Documents.
*
*
* LICENSES
*
* The WMM source code is in the public domain and not licensed or under copyright.
* The information and software may be used freely by the public. As required by 17 U.S.C. 403,
* third parties producing copyrighted works consisting predominantly of the material produced by
* U.S. government agencies must provide notice with such work(s) identifying the U.S. Government material
* incorporated and stating that such material is not subject to copyright protection.
*
* RESTRICTIONS
*
* Geomagnetism library has no restrictions.
*
* ENVIRONMENT
*
* Geomagnetism library was tested in the following environments
*
* 1. Red Hat Linux with GCC Compiler
* 2. MS Windows 7 with MinGW compiler
* 3. Sun Solaris with GCC Compiler
*
*
* National Centers for Environmental Information
* NOAA E/NE42, 325 Broadway
* Boulder, CO 80305 USA
* Attn: Arnaud Chulliat
* Phone: (303) 497-6522
* Email: [email protected]
* Software and Model Support
* National Centers for Environmental Information
* NOAA E/NE42
* 325 Broadway
* Boulder, CO 80305 USA
* Attn: Adam Woods or Manoj Nair
* Phone: (303) 497-6640 or -4642
* Email: [email protected]
* URL: http://www.ngdc.noaa.gov/Geomagnetic/WMM/DoDWMM.shtml
* For more details on the subroutines, please consult the WMM
* Technical Documentations at
* http://www.ngdc.noaa.gov/Geomagnetic/WMM/DoDWMM.shtml
* Nov 23, 2009
* Written by Manoj C Nair and Adam Woods
*/
double WMM_COF[90][6] =
{
{1, 0, -29404.5, 0.0, 6.7, 0.0},
{1, 1, -1450.7, 4652.9, 7.7, -25.1},
{2, 0, -2500.0, 0.0, -11.5, 0.0},
{2, 1, 2982.0, -2991.6, -7.1, -30.2},
{2, 2, 1676.8, -734.8, -2.2, -23.9},
{3, 0, 1363.9, 0.0, 2.8, 0.0},
{3, 1, -2381.0, -82.2, -6.2, 5.7},
{3, 2, 1236.2, 241.8, 3.4, -1.0},
{3, 3, 525.7, -542.9, -12.2, 1.1},
{4, 0, 903.1, 0.0, -1.1, 0.0},
{4, 1, 809.4, 282.0, -1.6, 0.2},
{4, 2, 86.2, -158.4, -6.0, 6.9},
{4, 3, -309.4, 199.8, 5.4, 3.7},
{4, 4, 47.9, -350.1, -5.5, -5.6},
{5, 0, -234.4, 0.0, -0.3, 0.0},
{5, 1, 363.1, 47.7, 0.6, 0.1},
{5, 2, 187.8, 208.4, -0.7, 2.5},
{5, 3, -140.7, -121.3, 0.1, -0.9},
{5, 4, -151.2, 32.2, 1.2, 3.0},
{5, 5, 13.7, 99.1, 1.0, 0.5},
{6, 0, 65.9, 0.0, -0.6, 0.0},
{6, 1, 65.6, -19.1, -0.4, 0.1},
{6, 2, 73.0, 25.0, 0.5, -1.8},
{6, 3, -121.5, 52.7, 1.4, -1.4},
{6, 4, -36.2, -64.4, -1.4, 0.9},
{6, 5, 13.5, 9.0, -0.0, 0.1},
{6, 6, -64.7, 68.1, 0.8, 1.0},
{7, 0, 80.6, 0.0, -0.1, 0.0},
{7, 1, -76.8, -51.4, -0.3, 0.5},
{7, 2, -8.3, -16.8, -0.1, 0.6},
{7, 3, 56.5, 2.3, 0.7, -0.7},
{7, 4, 15.8, 23.5, 0.2, -0.2},
{7, 5, 6.4, -2.2, -0.5, -1.2},
{7, 6, -7.2, -27.2, -0.8, 0.2},
{7, 7, 9.8, -1.9, 1.0, 0.3},
{8, 0, 23.6, 0.0, -0.1, 0.0},
{8, 1, 9.8, 8.4, 0.1, -0.3},
{8, 2, -17.5, -15.3, -0.1, 0.7},
{8, 3, -0.4, 12.8, 0.5, -0.2},
{8, 4, -21.1, -11.8, -0.1, 0.5},
{8, 5, 15.3, 14.9, 0.4, -0.3},
{8, 6, 13.7, 3.6, 0.5, -0.5},
{8, 7, -16.5, -6.9, 0.0, 0.4},
{8, 8, -0.3, 2.8, 0.4, 0.1},
{9, 0, 5.0, 0.0, -0.1, 0.0},
{9, 1, 8.2, -23.3, -0.2, -0.3},
{9, 2, 2.9, 11.1, -0.0, 0.2},
{9, 3, -1.4, 9.8, 0.4, -0.4},
{9, 4, -1.1, -5.1, -0.3, 0.4},
{9, 5, -13.3, -6.2, -0.0, 0.1},
{9, 6, 1.1, 7.8, 0.3, -0.0},
{9, 7, 8.9, 0.4, -0.0, -0.2},
{9, 8, -9.3, -1.5, -0.0, 0.5},
{9, 9, -11.9, 9.7, -0.4, 0.2},
{10, 0, -1.9, 0.0, 0.0, 0.0},
{10, 1, -6.2, 3.4, -0.0, -0.0},
{10, 2, -0.1, -0.2, -0.0, 0.1},
{10, 3, 1.7, 3.5, 0.2, -0.3},
{10, 4, -0.9, 4.8, -0.1, 0.1},
{10, 5, 0.6, -8.6, -0.2, -0.2},
{10, 6, -0.9, -0.1, -0.0, 0.1},
{10, 7, 1.9, -4.2, -0.1, -0.0},
{10, 8, 1.4, -3.4, -0.2, -0.1},
{10, 9, -2.4, -0.1, -0.1, 0.2},
{10, 10, -3.9, -8.8, -0.0, -0.0},
{11, 0, 3.0, 0.0, -0.0, 0.0},
{11, 1, -1.4, -0.0, -0.1, -0.0},
{11, 2, -2.5, 2.6, -0.0, 0.1},
{11, 3, 2.4, -0.5, 0.0, 0.0},
{11, 4, -0.9, -0.4, -0.0, 0.2},
{11, 5, 0.3, 0.6, -0.1, -0.0},
{11, 6, -0.7, -0.2, 0.0, 0.0},
{11, 7, -0.1, -1.7, -0.0, 0.1},
{11, 8, 1.4, -1.6, -0.1, -0.0},
{11, 9, -0.6, -3.0, -0.1, -0.1},
{11, 10, 0.2, -2.0, -0.1, 0.0},
{11, 11, 3.1, -2.6, -0.1, -0.0},
{12, 0, -2.0, 0.0, 0.0, 0.0},
{12, 1, -0.1, -1.2, -0.0, -0.0},
{12, 2, 0.5, 0.5, -0.0, 0.0},
{12, 3, 1.3, 1.3, 0.0, -0.1},
{12, 4, -1.2, -1.8, -0.0, 0.1},
{12, 5, 0.7, 0.1, -0.0, -0.0},
{12, 6, 0.3, 0.7, 0.0, 0.0},
{12, 7, 0.5, -0.1, -0.0, -0.0},
{12, 8, -0.2, 0.6, 0.0, 0.1},
{12, 9, -0.5, 0.2, -0.0, -0.0},
{12, 10, 0.1, -0.9, -0.0, -0.0},
{12, 11, -1.1, -0.0, -0.0, 0.0},
{12, 12, -0.3, 0.5, -0.1, -0.1}
};
/******************************************************************************
************************************Wrapper***********************************
* This grouping consists of functions call groups of other functions to do a
* complete calculation of some sort. For example, the MAG_Geomag function
* does everything necessary to compute the geomagnetic elements from a given
* geodetic point in space and magnetic model adjusted for the appropriate
* date. These functions are the external functions necessary to create a
* program that uses or calculates the magnetic field.
******************************************************************************
******************************************************************************/
int MAG_Geomag(MAGtype_Ellipsoid Ellip, MAGtype_CoordSpherical CoordSpherical, MAGtype_CoordGeodetic CoordGeodetic,
MAGtype_MagneticModel *TimedMagneticModel, MAGtype_GeoMagneticElements *GeoMagneticElements)
/*
The main subroutine that calls a sequence of WMM sub-functions to calculate the magnetic field elements for a single point.
The function expects the model coefficients and point coordinates as input and returns the magnetic field elements and
their rate of change. Though, this subroutine can be called successively to calculate a time series, profile or grid
of magnetic field, these are better achieved by the subroutine MAG_Grid.
INPUT: Ellip
CoordSpherical
CoordGeodetic
TimedMagneticModel
OUTPUT : GeoMagneticElements
CALLS: MAG_AllocateLegendreFunctionMemory(NumTerms); ( For storing the ALF functions )
MAG_ComputeSphericalHarmonicVariables( Ellip, CoordSpherical, TimedMagneticModel->nMax, &SphVariables); (Compute Spherical Harmonic variables )
MAG_AssociatedLegendreFunction(CoordSpherical, TimedMagneticModel->nMax, LegendreFunction); Compute ALF
MAG_Summation(LegendreFunction, TimedMagneticModel, SphVariables, CoordSpherical, &MagneticResultsSph); Accumulate the spherical harmonic coefficients
MAG_SecVarSummation(LegendreFunction, TimedMagneticModel, SphVariables, CoordSpherical, &MagneticResultsSphVar); Sum the Secular Variation Coefficients
MAG_RotateMagneticVector(CoordSpherical, CoordGeodetic, MagneticResultsSph, &MagneticResultsGeo); Map the computed Magnetic fields to Geodetic coordinates
MAG_CalculateGeoMagneticElements(&MagneticResultsGeo, GeoMagneticElements); Calculate the Geomagnetic elements
MAG_CalculateSecularVariationElements(MagneticResultsGeoVar, GeoMagneticElements); Calculate the secular variation of each of the Geomagnetic elements
*/
{
MAGtype_LegendreFunction *LegendreFunction;
MAGtype_SphericalHarmonicVariables *SphVariables;
int NumTerms;
MAGtype_MagneticResults MagneticResultsSph, MagneticResultsGeo, MagneticResultsSphVar, MagneticResultsGeoVar;
NumTerms = ((TimedMagneticModel->nMax + 1) * (TimedMagneticModel->nMax + 2) / 2);
LegendreFunction = MAG_AllocateLegendreFunctionMemory(NumTerms); /* For storing the ALF functions */
SphVariables = MAG_AllocateSphVarMemory(TimedMagneticModel->nMax);
MAG_ComputeSphericalHarmonicVariables(Ellip, CoordSpherical, TimedMagneticModel->nMax, SphVariables); /* Compute Spherical Harmonic variables */
MAG_AssociatedLegendreFunction(CoordSpherical, TimedMagneticModel->nMax, LegendreFunction); /* Compute ALF */
MAG_Summation(LegendreFunction, TimedMagneticModel, *SphVariables, CoordSpherical, &MagneticResultsSph); /* Accumulate the spherical harmonic coefficients*/
MAG_SecVarSummation(LegendreFunction, TimedMagneticModel, *SphVariables, CoordSpherical, &MagneticResultsSphVar); /*Sum the Secular Variation Coefficients */
MAG_RotateMagneticVector(CoordSpherical, CoordGeodetic, MagneticResultsSph, &MagneticResultsGeo); /* Map the computed Magnetic fields to Geodeitic coordinates */
MAG_RotateMagneticVector(CoordSpherical, CoordGeodetic, MagneticResultsSphVar, &MagneticResultsGeoVar); /* Map the secular variation field components to Geodetic coordinates*/
MAG_CalculateGeoMagneticElements(&MagneticResultsGeo, GeoMagneticElements); /* Calculate the Geomagnetic elements, Equation 19 , WMM Technical report */
MAG_CalculateSecularVariationElements(MagneticResultsGeoVar, GeoMagneticElements); /*Calculate the secular variation of each of the Geomagnetic elements*/
MAG_FreeLegendreMemory(LegendreFunction);
MAG_FreeSphVarMemory(SphVariables);
return TRUE;
} /*MAG_Geomag*/
void MAG_Gradient(MAGtype_Ellipsoid Ellip, MAGtype_CoordGeodetic CoordGeodetic, MAGtype_MagneticModel *TimedMagneticModel, MAGtype_Gradient *Gradient)
{
/*It should be noted that the x[2], y[2], and z[2] variables are NOT the same
coordinate system as the directions in which the gradients are taken. These
variables represent a Cartesian coordinate system where the Earth's center is
the origin, 'z' points up toward the North (rotational) pole and 'x' points toward
the prime meridian. 'y' points toward longitude = 90 degrees East.
The gradient is preformed along a local Cartesian coordinate system with the
origin at CoordGeodetic. 'z' points down toward the Earth's core, x points
North, tangent to the local longitude line, and 'y' points East, tangent to
the local latitude line.*/
double phiDelta = 0.01, /*DeltaY = 0.01,*/ hDelta = -1, x[2], y[2], z[2], distance;
MAGtype_CoordSpherical AdjCoordSpherical;
MAGtype_CoordGeodetic AdjCoordGeodetic;
MAGtype_GeoMagneticElements GeomagneticElements, AdjGeoMagneticElements[2];
/*Initialization*/
MAG_GeodeticToSpherical(Ellip, CoordGeodetic, &AdjCoordSpherical);
MAG_Geomag(Ellip, AdjCoordSpherical, CoordGeodetic, TimedMagneticModel, &GeomagneticElements);
AdjCoordGeodetic = MAG_CoordGeodeticAssign(CoordGeodetic);
/*Gradient along x*/
AdjCoordGeodetic.phi = CoordGeodetic.phi + phiDelta;
MAG_GeodeticToSpherical(Ellip, AdjCoordGeodetic, &AdjCoordSpherical);
MAG_Geomag(Ellip, AdjCoordSpherical, AdjCoordGeodetic, TimedMagneticModel, &AdjGeoMagneticElements[0]);
MAG_SphericalToCartesian(AdjCoordSpherical, &x[0], &y[0], &z[0]);
AdjCoordGeodetic.phi = CoordGeodetic.phi - phiDelta;
MAG_GeodeticToSpherical(Ellip, AdjCoordGeodetic, &AdjCoordSpherical);
MAG_Geomag(Ellip, AdjCoordSpherical, AdjCoordGeodetic, TimedMagneticModel, &AdjGeoMagneticElements[1]);
MAG_SphericalToCartesian(AdjCoordSpherical, &x[1], &y[1], &z[1]);
distance = sqrt((x[0] - x[1])*(x[0] - x[1])+(y[0] - y[1])*(y[0] - y[1])+(z[0] - z[1])*(z[0] - z[1]));
Gradient->GradPhi = MAG_GeoMagneticElementsSubtract(AdjGeoMagneticElements[0], AdjGeoMagneticElements[1]);
Gradient->GradPhi = MAG_GeoMagneticElementsScale(Gradient->GradPhi, 1 / distance);
AdjCoordGeodetic = MAG_CoordGeodeticAssign(CoordGeodetic);
/*Gradient along y*/
/*It is perhaps noticeable that the method here for calculation is substantially
different than that for the gradient along x. As we near the North pole
the longitude lines approach each other, and the calculation that works well
for latitude lines becomes unstable when 0.01 degrees represents sufficiently
small numbers, and fails to function correctly at all at the North Pole*/
MAG_GeodeticToSpherical(Ellip, CoordGeodetic, &AdjCoordSpherical);
MAG_GradY(Ellip, AdjCoordSpherical, CoordGeodetic, TimedMagneticModel, GeomagneticElements, &(Gradient->GradLambda));
/*Gradient along z*/
AdjCoordGeodetic.HeightAboveEllipsoid = CoordGeodetic.HeightAboveEllipsoid + hDelta;
AdjCoordGeodetic.HeightAboveGeoid = CoordGeodetic.HeightAboveGeoid + hDelta;
MAG_GeodeticToSpherical(Ellip, AdjCoordGeodetic, &AdjCoordSpherical);
MAG_Geomag(Ellip, AdjCoordSpherical, AdjCoordGeodetic, TimedMagneticModel, &AdjGeoMagneticElements[0]);
MAG_SphericalToCartesian(AdjCoordSpherical, &x[0], &y[0], &z[0]);
AdjCoordGeodetic.HeightAboveEllipsoid = CoordGeodetic.HeightAboveEllipsoid - hDelta;
AdjCoordGeodetic.HeightAboveGeoid = CoordGeodetic.HeightAboveGeoid - hDelta;
MAG_GeodeticToSpherical(Ellip, AdjCoordGeodetic, &AdjCoordSpherical);
MAG_Geomag(Ellip, AdjCoordSpherical, AdjCoordGeodetic, TimedMagneticModel, &AdjGeoMagneticElements[1]);
MAG_SphericalToCartesian(AdjCoordSpherical, &x[1], &y[1], &z[1]);
distance = sqrt((x[0] - x[1])*(x[0] - x[1])+(y[0] - y[1])*(y[0] - y[1])+(z[0] - z[1])*(z[0] - z[1]));
Gradient->GradZ = MAG_GeoMagneticElementsSubtract(AdjGeoMagneticElements[0], AdjGeoMagneticElements[1]);
Gradient->GradZ = MAG_GeoMagneticElementsScale(Gradient->GradZ, 1/distance);
AdjCoordGeodetic = MAG_CoordGeodeticAssign(CoordGeodetic);
}
int MAG_SetDefaults(MAGtype_Ellipsoid *Ellip, MAGtype_Geoid *Geoid)
/*
Sets default values for WMM subroutines.
UPDATES : Ellip
Geoid
CALLS : none
*/
{
/* Sets WGS-84 parameters */
Ellip->a = 6378.137; /*semi-major axis of the ellipsoid in */
Ellip->b = 6356.7523142; /*semi-minor axis of the ellipsoid in */
Ellip->fla = 1 / 298.257223563; /* flattening */
Ellip->eps = sqrt(1 - (Ellip->b * Ellip->b) / (Ellip->a * Ellip->a)); /*first eccentricity */
Ellip->epssq = (Ellip->eps * Ellip->eps); /*first eccentricity squared */
Ellip->re = 6371.2; /* Earth's radius */
/* Sets EGM-96 model file parameters */
Geoid->NumbGeoidCols = 1441; /* 360 degrees of longitude at 15 minute spacing */
Geoid->NumbGeoidRows = 721; /* 180 degrees of latitude at 15 minute spacing */
Geoid->NumbHeaderItems = 6; /* min, max lat, min, max long, lat, long spacing*/
Geoid->ScaleFactor = 4; /* 4 grid cells per degree at 15 minute spacing */
Geoid->NumbGeoidElevs = Geoid->NumbGeoidCols * Geoid->NumbGeoidRows;
Geoid->Geoid_Initialized = 0; /* Geoid will be initialized only if this is set to zero */
Geoid->UseGeoid = MAG_USE_GEOID;
return TRUE;
} /*MAG_SetDefaults */
/*End of Wrapper Functions*/
/******************************************************************************
********************************User Interface********************************
* This grouping consists of functions which interact with the directly with
* the user and are generally specific to the XXX_point.c, XXX_grid.c, and
* XXX_file.c programs. They deal with input from and output to the user.
******************************************************************************/
void MAG_Error(int control)
/*This prints WMM errors.
INPUT control Error look up number
OUTPUT none
CALLS : none
*/
{
switch(control) {
case 1:
printf("\nError allocating in MAG_LegendreFunctionMemory.\n");
break;
case 2:
printf("\nError allocating in MAG_AllocateModelMemory.\n");
break;
case 3:
printf("\nError allocating in MAG_InitializeGeoid\n");
break;
case 4:
printf("\nError in setting default values.\n");
break;
case 5:
printf("\nError initializing Geoid.\n");
break;
case 6:
printf("\nError opening WMM.COF\n.");
break;
case 7:
printf("\nError opening WMMSV.COF\n.");
break;
case 8:
printf("\nError reading Magnetic Model.\n");
break;
case 9:
printf("\nError printing Command Prompt introduction.\n");
break;
case 10:
printf("\nError converting from geodetic co-ordinates to spherical co-ordinates.\n");
break;
case 11:
printf("\nError in time modifying the Magnetic model\n");
break;
case 12:
printf("\nError in Geomagnetic\n");
break;
case 13:
printf("\nError printing user data\n");\
break;
case 14:
printf("\nError allocating in MAG_SummationSpecial\n");
break;
case 15:
printf("\nError allocating in MAG_SecVarSummationSpecial\n");
break;
case 16:
printf("\nError in opening EGM9615.BIN file\n");
break;
case 17:
printf("\nError: Latitude OR Longitude out of range in MAG_GetGeoidHeight\n");
break;
case 18:
printf("\nError allocating in MAG_PcupHigh\n");
break;
case 19:
printf("\nError allocating in MAG_PcupLow\n");
break;
case 20:
printf("\nError opening coefficient file\n");
break;
case 21:
printf("\nError: UnitDepth too large\n");
break;
case 22:
printf("\nYour system needs Big endian version of EGM9615.BIN. \n");
printf("Please download this file from http://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml. \n");
printf("Replace the existing EGM9615.BIN file with the downloaded one\n");
break;
}
} /*MAG_Error*/
int MAG_ValidateDMSstring(char *input, int min, int max, char *Error)
/* Validates a latitude DMS string, and returns 1 for a success and returns 0 for a failure.
It copies an error message to the Error string in the event of a failure.
INPUT : input (DMS string)
OUTPUT : Error : Error string
CALLS : none
*/
{
int degree, minute, second, j = 0, n, max_minute = 60, max_second = 60;
int i;
degree = -1000;
minute = -1;
second = -1;
n = (int) strlen(input);
for(i = 0; i <= n - 1; i++) /*tests for legal characters*/
{
if((input[i] < '0' || input[i] > '9') && (input[i] != ',' && input[i] != ' ' && input[i] != '-' && input[i] != '\0' && input[i] != '\n'))
{
strcpy(Error, "\nError: Input contains an illegal character, legal characters for Degree, Minute, Second format are:\n '0-9' ',' '-' '[space]' '[Enter]'\n");
return FALSE;
}
if(input[i] == ',')
j++;
}
if(j == 2)
j = sscanf(input, "%d, %d, %d", °ree, &minute, &second); /*tests for legal formatting and range*/
else
j = sscanf(input, "%d %d %d", °ree, &minute, &second);
if(j == 1)
{
minute = 0;
second = 0;
j = 3;
}
if(j != 3)
{
strcpy(Error, "\nError: Not enough numbers used for Degrees, Minutes, Seconds format\n or they were incorrectly formatted\n The legal format is DD,MM,SS or DD MM SS\n");
return FALSE;
}
if(degree > max || degree < min)
{
sprintf(Error, "\nError: Degree input is outside legal range\n The legal range is from %d to %d\n", min, max);
return FALSE;
}
if(degree == max || degree == min)
max_minute = 0;
if(minute > max_minute || minute < 0)
{
strcpy(Error, "\nError: Minute input is outside legal range\n The legal minute range is from 0 to 60\n");
return FALSE;
}
if(minute == max_minute)
max_second = 0;
if(second > max_second || second < 0)
{
strcpy(Error, "\nError: Second input is outside legal range\n The legal second range is from 0 to 60\n");
return FALSE;
}
return TRUE;
} /*MAG_ValidateDMSstring*/
int MAG_Warnings(int control, double value, MAGtype_MagneticModel *MagneticModel)
/*Return value 0 means end program, Return value 1 means get new data, Return value 2 means continue.
This prints a warning to the screen determined by the control integer. It also takes the value of the parameter causing the warning as a double. This is unnecessary for some warnings.
It requires the MagneticModel to determine the current epoch.
INPUT control :int : (Warning number)
value : double: Magnetic field strength
MagneticModel
OUTPUT : none
CALLS : none
*/
{
char ans[20];
strcpy(ans, "");
switch(control) {
case 1:/* Horizontal Field strength low */
do {
printf("\nCaution: location is approaching the blackout zone around the magnetic pole as\n");
printf(" defined by the WMM military specification \n");
printf(" (https://www.ngdc.noaa.gov/geomag/WMM/data/MIL-PRF-89500B.pdf). Compass\n");
printf(" accuracy may be degraded in this region.\n");
printf("Press enter to continue...\n");
} while(NULL == fgets(ans, 20, stdin));
break;
case 2:/* Horizontal Field strength very low */
do {
printf("\nWarning: location is in the blackout zone around the magnetic pole as defined\n");
printf(" by the WMM military specification \n");
printf(" (https://www.ngdc.noaa.gov/geomag/WMM/data/MIL-PRF-89500B.pdf). Compass\n");
printf(" accuracy is highly degraded in this region.\n");
} while(NULL == fgets(ans, 20, stdin));
break;
case 3:/* Elevation outside the recommended range */
printf("\nWarning: The value you have entered of %.1f km for the elevation is outside of the recommended range.\n Elevations above -10.0 km are recommended for accurate results. \n", value);
while(1)
{
printf("\nPlease press 'C' to continue, 'G' to get new data or 'X' to exit...\n");
while( NULL == fgets(ans, 20, stdin)) {
printf("\nInvalid input\n");
}
switch(ans[0]) {
case 'X':
case 'x':
return 0;
case 'G':
case 'g':
return 1;
case 'C':
case 'c':
return 2;
default:
printf("\nInvalid input %c\n", ans[0]);
break;
}
}
break;
case 4:/*Date outside the recommended range*/
printf("\nWARNING - TIME EXTENDS BEYOND INTENDED USAGE RANGE\n CONTACT NCEI FOR PRODUCT UPDATES:\n");
printf(" National Centers for Environmental Information\n");
printf(" NOAA E/NE42\n");
printf(" 325 Broadway\n");
printf("\n Boulder, CO 80305 USA");
printf(" Attn: Manoj Nair or Arnaud Chulliat\n");
printf(" Phone: (303) 497-4642 or -6522\n");
printf(" Email: [email protected]\n");
printf(" Web: http://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml\n");
printf("\n VALID RANGE = %d - %d\n", (int) MagneticModel->epoch, (int) MagneticModel->CoefficientFileEndDate);
printf(" TIME = %f\n", value);
while(1)
{
printf("\nPlease press 'C' to continue, 'N' to enter new data or 'X' to exit...\n");
while (NULL ==fgets(ans, 20, stdin)){
printf("\nInvalid input\n");
}
switch(ans[0]) {
case 'X':
case 'x':
return 0;
case 'N':
case 'n':
return 1;
case 'C':
case 'c':
return 2;
default:
printf("\nInvalid input %c\n", ans[0]);
break;
}
}
break;
case 5:/*Elevation outside the allowable range*/
printf("\nError: The value you have entered of %f km for the elevation is outside of the recommended range.\n Elevations above -10.0 km are recommended for accurate results. \n", value);
while(1)
{
printf("\nPlease press 'C' to continue, 'G' to get new data or 'X' to exit...\n");
while (NULL ==fgets(ans, 20, stdin)){
printf("\nInvalid input\n");
}
switch(ans[0]) {
case 'X':
case 'x':
return 0;
case 'G':
case 'g':
return 1;
case 'C':
case 'c':
return 2;
default:
printf("\nInvalid input %c\n", ans[0]);
break;
}
}
break;
}
return 2;
} /*MAG_Warnings*/
/*End of User Interface functions*/
/******************************************************************************
********************************Memory and File Processing********************
* This grouping consists of functions that read coefficient files into the
* memory, allocate memory, free memory or print models into coefficient files.
******************************************************************************/
MAGtype_LegendreFunction *MAG_AllocateLegendreFunctionMemory(int NumTerms)
/* Allocate memory for Associated Legendre Function data types.
Should be called before computing Associated Legendre Functions.
INPUT: NumTerms : int : Total number of spherical harmonic coefficients in the model
OUTPUT: Pointer to data structure MAGtype_LegendreFunction with the following elements
double *Pcup; ( pointer to store Legendre Function )
double *dPcup; ( pointer to store Derivative of Legendre function )
FALSE: Failed to allocate memory
CALLS : none
*/
{
MAGtype_LegendreFunction *LegendreFunction;
LegendreFunction = (MAGtype_LegendreFunction *) calloc(1, sizeof (MAGtype_LegendreFunction));
if(!LegendreFunction)
{
MAG_Error(1);
return NULL;
}
LegendreFunction->Pcup = (double *) malloc((NumTerms + 1) * sizeof ( double));
if(LegendreFunction->Pcup == 0)
{
MAG_Error(1);
return NULL;
}
LegendreFunction->dPcup = (double *) malloc((NumTerms + 1) * sizeof ( double));
if(LegendreFunction->dPcup == 0)
{
MAG_Error(1);
return NULL;
}
return LegendreFunction;
} /*MAGtype_LegendreFunction*/
MAGtype_MagneticModel *MAG_AllocateModelMemory(int NumTerms)
/* Allocate memory for WMM Coefficients
* Should be called before reading the model file *
INPUT: NumTerms : int : Total number of spherical harmonic coefficients in the model
OUTPUT: Pointer to data structure MAGtype_MagneticModel with the following elements
double EditionDate;
double epoch; Base time of Geomagnetic model epoch (yrs)
char ModelName[20];
double *Main_Field_Coeff_G; C - Gauss coefficients of main geomagnetic model (nT)
double *Main_Field_Coeff_H; C - Gauss coefficients of main geomagnetic model (nT)
double *Secular_Var_Coeff_G; CD - Gauss coefficients of secular geomagnetic model (nT/yr)
double *Secular_Var_Coeff_H; CD - Gauss coefficients of secular geomagnetic model (nT/yr)
int nMax; Maximum degree of spherical harmonic model
int nMaxSecVar; Maxumum degree of spherical harmonic secular model
int SecularVariationUsed; Whether or not the magnetic secular variation vector will be needed by program
FALSE: Failed to allocate memory
CALLS : none
*/
{
MAGtype_MagneticModel *MagneticModel;
int i;
MagneticModel = (MAGtype_MagneticModel *) calloc(1, sizeof (MAGtype_MagneticModel));
if(MagneticModel == NULL)
{
MAG_Error(2);
return NULL;
}
MagneticModel->Main_Field_Coeff_G = (double *) malloc((NumTerms + 1) * sizeof ( double));
if(MagneticModel->Main_Field_Coeff_G == NULL)
{
MAG_Error(2);
return NULL;
}
MagneticModel->Main_Field_Coeff_H = (double *) malloc((NumTerms + 1) * sizeof ( double));
if(MagneticModel->Main_Field_Coeff_H == NULL)
{
MAG_Error(2);
return NULL;
}
MagneticModel->Secular_Var_Coeff_G = (double *) malloc((NumTerms + 1) * sizeof ( double));
if(MagneticModel->Secular_Var_Coeff_G == NULL)
{
MAG_Error(2);
return NULL;
}
MagneticModel->Secular_Var_Coeff_H = (double *) malloc((NumTerms + 1) * sizeof ( double));
if(MagneticModel->Secular_Var_Coeff_H == NULL)
{
MAG_Error(2);
return NULL;
}
MagneticModel->CoefficientFileEndDate = 0;
MagneticModel->EditionDate = 0;
strcpy(MagneticModel->ModelName, "");
MagneticModel->SecularVariationUsed = 0;
MagneticModel->epoch = 0;
MagneticModel->nMax = 0;
MagneticModel->nMaxSecVar = 0;
for(i=0; i<NumTerms; i++) {
MagneticModel->Main_Field_Coeff_G[i] = 0;
MagneticModel->Main_Field_Coeff_H[i] = 0;
MagneticModel->Secular_Var_Coeff_G[i] = 0;
MagneticModel->Secular_Var_Coeff_H[i] = 0;
}
return MagneticModel;
} /*MAG_AllocateModelMemory*/
MAGtype_SphericalHarmonicVariables* MAG_AllocateSphVarMemory(int nMax)
{
MAGtype_SphericalHarmonicVariables* SphVariables;
SphVariables = (MAGtype_SphericalHarmonicVariables*) calloc(1, sizeof(MAGtype_SphericalHarmonicVariables));
SphVariables->RelativeRadiusPower = (double *) malloc((nMax + 1) * sizeof ( double));
SphVariables->cos_mlambda = (double *) malloc((nMax + 1) * sizeof (double));
SphVariables->sin_mlambda = (double *) malloc((nMax + 1) * sizeof (double));
return SphVariables;
} /*MAG_AllocateSphVarMemory*/
void MAG_AssignHeaderValues(MAGtype_MagneticModel *model, char values[][MAXLINELENGTH])
{
/* MAGtype_Date releasedate; */
strcpy(model->ModelName, values[MODELNAME]);
/* releasedate.Year = 0;
releasedate.Day = 0;
releasedate.Month = 0;
releasedate.DecimalYear = 0;
sscanf(values[RELEASEDATE],"%d-%d-%d",&releasedate.Year,&releasedate.Month,&releasedate.Day);
if(MAG_DateToYear (&releasedate, NULL))
model->EditionDate = releasedate.DecimalYear;*/
model->epoch = atof(values[MODELSTARTYEAR]);
model->nMax = atoi(values[INTSTATICDEG]);
model->nMaxSecVar = atoi(values[INTSECVARDEG]);
model->CoefficientFileEndDate = atof(values[MODELENDYEAR]);
if(model->nMaxSecVar > 0)
model->SecularVariationUsed = 1;
else
model->SecularVariationUsed = 0;
}
void MAG_AssignMagneticModelCoeffs(MAGtype_MagneticModel *Assignee, MAGtype_MagneticModel *Source, int nMax, int nMaxSecVar)
/* This function assigns the first nMax degrees of the Source model to the Assignee model, leaving the other coefficients
untouched*/
{
int n, m, index;
assert(nMax <= Source->nMax);
assert(nMax <= Assignee->nMax);
assert(nMaxSecVar <= Source->nMaxSecVar);
assert(nMaxSecVar <= Assignee->nMaxSecVar);
for(n = 1; n <= nMaxSecVar; n++)
{
for(m = 0; m <= n; m++)
{
index = (n * (n + 1) / 2 + m);
Assignee->Main_Field_Coeff_G[index] = Source->Main_Field_Coeff_G[index];
Assignee->Main_Field_Coeff_H[index] = Source->Main_Field_Coeff_H[index];
Assignee->Secular_Var_Coeff_G[index] = Source->Secular_Var_Coeff_G[index];
Assignee->Secular_Var_Coeff_H[index] = Source->Secular_Var_Coeff_H[index];
}
}
for(n = nMaxSecVar + 1; n <= nMax; n++)
{
for(m = 0; m <= n; m++)
{
index = (n * (n + 1) / 2 + m);
Assignee->Main_Field_Coeff_G[index] = Source->Main_Field_Coeff_G[index];
Assignee->Main_Field_Coeff_H[index] = Source->Main_Field_Coeff_H[index];
}
}
return;
} /*MAG_AssignMagneticModelCoeffs*/
int MAG_FreeMemory(MAGtype_MagneticModel *MagneticModel, MAGtype_MagneticModel *TimedMagneticModel, MAGtype_LegendreFunction *LegendreFunction)
/* Free memory used by WMM functions. Only to be called at the end of the main function.
INPUT : MagneticModel pointer to data structure with the following elements
double EditionDate;
double epoch; Base time of Geomagnetic model epoch (yrs)
char ModelName[20];
double *Main_Field_Coeff_G; C - Gauss coefficients of main geomagnetic model (nT)
double *Main_Field_Coeff_H; C - Gauss coefficients of main geomagnetic model (nT)
double *Secular_Var_Coeff_G; CD - Gauss coefficients of secular geomagnetic model (nT/yr)
double *Secular_Var_Coeff_H; CD - Gauss coefficients of secular geomagnetic model (nT/yr)
int nMax; Maximum degree of spherical harmonic model
int nMaxSecVar; Maxumum degree of spherical harmonic secular model
int SecularVariationUsed; Whether or not the magnetic secular variation vector will be needed by program
TimedMagneticModel Pointer to data structure similar to the first input.
LegendreFunction Pointer to data structure with the following elements
double *Pcup; ( pointer to store Legendre Function )
double *dPcup; ( pointer to store Derivative of Lagendre function )
OUTPUT none
CALLS : none
*/
{
if(MagneticModel->Main_Field_Coeff_G)
{
free(MagneticModel->Main_Field_Coeff_G);
MagneticModel->Main_Field_Coeff_G = NULL;
}
if(MagneticModel->Main_Field_Coeff_H)
{
free(MagneticModel->Main_Field_Coeff_H);
MagneticModel->Main_Field_Coeff_H = NULL;
}
if(MagneticModel->Secular_Var_Coeff_G)
{
free(MagneticModel->Secular_Var_Coeff_G);
MagneticModel->Secular_Var_Coeff_G = NULL;
}
if(MagneticModel->Secular_Var_Coeff_H)
{
free(MagneticModel->Secular_Var_Coeff_H);
MagneticModel->Secular_Var_Coeff_H = NULL;
}
if(MagneticModel)
{
free(MagneticModel);
MagneticModel = NULL;
}
if(TimedMagneticModel->Main_Field_Coeff_G)
{
free(TimedMagneticModel->Main_Field_Coeff_G);
TimedMagneticModel->Main_Field_Coeff_G = NULL;
}
if(TimedMagneticModel->Main_Field_Coeff_H)
{
free(TimedMagneticModel->Main_Field_Coeff_H);
TimedMagneticModel->Main_Field_Coeff_H = NULL;
}
if(TimedMagneticModel->Secular_Var_Coeff_G)
{
free(TimedMagneticModel->Secular_Var_Coeff_G);
TimedMagneticModel->Secular_Var_Coeff_G = NULL;
}
if(TimedMagneticModel->Secular_Var_Coeff_H)
{
free(TimedMagneticModel->Secular_Var_Coeff_H);
TimedMagneticModel->Secular_Var_Coeff_H = NULL;
}
if(TimedMagneticModel)
{
free(TimedMagneticModel);
TimedMagneticModel = NULL;
}
if(LegendreFunction->Pcup)
{
free(LegendreFunction->Pcup);
LegendreFunction->Pcup = NULL;
}
if(LegendreFunction->dPcup)
{
free(LegendreFunction->dPcup);
LegendreFunction->dPcup = NULL;
}
if(LegendreFunction)
{
free(LegendreFunction);
LegendreFunction = NULL;
}
return TRUE;
} /*MAG_FreeMemory */
int MAG_FreeMagneticModelMemory(MAGtype_MagneticModel *MagneticModel)
/* Free the magnetic model memory used by WMM functions.
INPUT : MagneticModel pointer to data structure with the following elements
double EditionDate;
double epoch; Base time of Geomagnetic model epoch (yrs)
char ModelName[20];
double *Main_Field_Coeff_G; C - Gauss coefficients of main geomagnetic model (nT)
double *Main_Field_Coeff_H; C - Gauss coefficients of main geomagnetic model (nT)
double *Secular_Var_Coeff_G; CD - Gauss coefficients of secular geomagnetic model (nT/yr)
double *Secular_Var_Coeff_H; CD - Gauss coefficients of secular geomagnetic model (nT/yr)
int nMax; Maximum degree of spherical harmonic model
int nMaxSecVar; Maxumum degree of spherical harmonic secular model
int SecularVariationUsed; Whether or not the magnetic secular variation vector will be needed by program
OUTPUT none
CALLS : none
*/
{
if(MagneticModel->Main_Field_Coeff_G)
{
free(MagneticModel->Main_Field_Coeff_G);
MagneticModel->Main_Field_Coeff_G = NULL;
}
if(MagneticModel->Main_Field_Coeff_H)
{
free(MagneticModel->Main_Field_Coeff_H);
MagneticModel->Main_Field_Coeff_H = NULL;
}
if(MagneticModel->Secular_Var_Coeff_G)
{
free(MagneticModel->Secular_Var_Coeff_G);
MagneticModel->Secular_Var_Coeff_G = NULL;
}
if(MagneticModel->Secular_Var_Coeff_H)
{
free(MagneticModel->Secular_Var_Coeff_H);
MagneticModel->Secular_Var_Coeff_H = NULL;
}
if(MagneticModel)
{
free(MagneticModel);
MagneticModel = NULL;
}
return TRUE;
} /*MAG_FreeMagneticModelMemory */
int MAG_FreeLegendreMemory(MAGtype_LegendreFunction *LegendreFunction)
/* Free the Legendre Coefficients memory used by the WMM functions.
INPUT : LegendreFunction Pointer to data structure with the following elements
double *Pcup; ( pointer to store Legendre Function )
double *dPcup; ( pointer to store Derivative of Lagendre function )
OUTPUT: none
CALLS : none
*/
{
if(LegendreFunction->Pcup)
{
free(LegendreFunction->Pcup);
LegendreFunction->Pcup = NULL;
}
if(LegendreFunction->dPcup)
{
free(LegendreFunction->dPcup);
LegendreFunction->dPcup = NULL;
}
if(LegendreFunction)
{
free(LegendreFunction);
LegendreFunction = NULL;
}
return TRUE;
} /*MAG_FreeLegendreMemory */
int MAG_FreeSphVarMemory(MAGtype_SphericalHarmonicVariables *SphVar)
/* Free the Spherical Harmonic Variable memory used by the WMM functions.
INPUT : LegendreFunction Pointer to data structure with the following elements
double *RelativeRadiusPower
double *cos_mlambda
double *sin_mlambda
OUTPUT: none