-
Notifications
You must be signed in to change notification settings - Fork 1
/
GeomagnetismLibrary.c
4067 lines (3536 loc) · 168 KB
/
GeomagnetismLibrary.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 "GeomagnetismHeader.h"
// $Id: GeomagnetismLibrary.c 842 2012-04-20 20:59:13Z awoods $
/*
* ABSTRACT
*
* The purpose of Geomagnetism Library is primarily to support the World Magnetic Model (WMM) 2010-2015.
* 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 the EMM.
*
* 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 XP with CodeGear C++ compiler
* 3. Sun Solaris with GCC Compiler
*
*
* MODIFICATIONS
*
* Date Version
* ---- -----------
* Jul 15, 2009 0.1
* Nov 17, 2009 0.2
* Nov 23, 2009 0.3
* Jan 28, 2010 1.0
* Contact Information
* Sponsoring Government Agency
* National Geospatial-Intelligence Agency
* PRG / CSAT, M.S. L-41
* 3838 Vogel Road
* Arnold, MO 63010
* Attn: Craig Rollins
* Phone: (314) 263-4186
* Email: [email protected]
* National Geophysical Data Center
* NOAA EGC/2
* 325 Broadway
* Boulder, CO 80303 USA
* Attn: Susan McLean
* Phone: (303) 497-6478
* Email: [email protected]
* Software and Model Support
* National Geophysical Data Center
* NOAA EGC/2
* 325 Broadway"
* Boulder, CO 80303 USA
* Attn: Manoj Nair or Stefan Maus
* Phone: (303) 497-6522 or -6522
* Email: [email protected] or [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
* Revision Number: $Revision: 842 $
* Last changed by: $Author: awoods $
* Last changed on: $Date: 2012-04-20 14:59:13 -0600 (Fri, 20 Apr 2012) $
*/
/******************************************************************************
************************************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 18 , 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*/
int MAG_Grid(MAGtype_CoordGeodetic minimum, MAGtype_CoordGeodetic maximum, double
cord_step_size, double altitude_step_size, double time_step, MAGtype_MagneticModel *MagneticModel, MAGtype_Geoid
*Geoid, MAGtype_Ellipsoid Ellip, MAGtype_Date StartDate, MAGtype_Date EndDate, int ElementOption, int PrintOption, char *OutputFile)
/*This function calls WMM subroutines to generate a grid as defined by the user. The function may be used
to generate a grid of magnetic field elements, time series or a profile. The selected geomagnetic element
is either printed to the file GridResults.txt or to the screen depending on user option.
INPUT: minimum :Data structure with the following elements (minimum limits of the grid)
double lambda; (longitude)
double phi; ( geodetic latitude)
double HeightAboveEllipsoid; (height above the ellipsoid (HaE) )
double HeightAboveGeoid;(height above the Geoid )
maximum : same as the above (maximum limist of the grid)
step_size : double : spatial step size, in decimal degrees
a_step_size : double : double altitude step size (km)
step_time : double : time step size (decimal years)
StartDate : data structure with the following elements used
double DecimalYear; ( decimal years )
EndDate : Same as the above;
MagneticModel : 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
Geoid : data structure with the following elements
Pointer to data structure Geoid with the following elements
int NumbGeoidCols ; ( 360 degrees of longitude at 15 minute spacing )
int NumbGeoidRows ; ( 180 degrees of latitude at 15 minute spacing )
int NumbHeaderItems ; ( min, max lat, min, max long, lat, long spacing )
int ScaleFactor; ( 4 grid cells per degree at 15 minute spacing )
float *GeoidHeightBuffer; (Pointer to the memory to store the Geoid elevation data )
int NumbGeoidElevs; (number of points in the gridded file )
int Geoid_Initialized ; ( indicates successful initialization )
Ellip data structure with the following elements
double a; semi-major axis of the ellipsoid
double b; semi-minor axis of the ellipsoid
double fla; flattening
double epssq; first eccentricity squared
double eps; first eccentricity
double re; mean radius of ellipsoid
ElementOption : int : Geomagnetic Elelment to print
PrintOption : int : 1 Print to File, Otherwise, print to screen
OUTPUT: none (prints the output to a file )
CALLS : MAG_AllocateModelMemory To allocate memory for model coefficients
MAG_TimelyModifyMagneticModel This modifies the Magnetic coefficients to the correct date.
MAG_ConvertGeoidToEllipsoidHeight (&CoordGeodetic, &Geoid); Convert height above msl to height above WGS-84 ellipsoid
MAG_GeodeticToSpherical Convert from geodeitic to Spherical Equations: 7-8, WMM Technical report
MAG_ComputeSphericalHarmonicVariables Compute Spherical Harmonic variables
MAG_AssociatedLegendreFunction Compute ALF Equations 5-6, WMM Technical report
MAG_Summation Accumulate the spherical harmonic coefficients Equations 10:12 , WMM Technical report
MAG_RotateMagneticVector Map the computed Magnetic fields to Geodeitic coordinates Equation 16 , WMM Technical report
MAG_CalculateGeoMagneticElements Calculate the geoMagnetic elements, Equation 18 , WMM Technical report
*/
{
int NumTerms;
double a, b, c, d, PrintElement;
MAGtype_MagneticModel *TimedMagneticModel;
MAGtype_CoordSpherical CoordSpherical;
MAGtype_MagneticResults MagneticResultsSph, MagneticResultsGeo, MagneticResultsSphVar, MagneticResultsGeoVar;
MAGtype_SphericalHarmonicVariables *SphVariables;
MAGtype_GeoMagneticElements GeoMagneticElements;
MAGtype_LegendreFunction *LegendreFunction;
FILE *fileout = NULL;
if(PrintOption == 1)
{
fileout = fopen(OutputFile, "w");
if(!fileout)
{
printf("Error opening %s to write", OutputFile);
return FALSE;
}
}
if(fabs(cord_step_size) < 1.0e-10) cord_step_size = 99999.0; //checks to make sure that the step_size is not too small
if(fabs(altitude_step_size) < 1.0e-10) altitude_step_size = 99999.0;
if(fabs(time_step) < 1.0e-10) time_step = 99999.0;
NumTerms = ((MagneticModel->nMax + 1) * (MagneticModel->nMax + 2) / 2);
TimedMagneticModel = MAG_AllocateModelMemory(NumTerms);
LegendreFunction = MAG_AllocateLegendreFunctionMemory(NumTerms); /* For storing the ALF functions */
SphVariables = MAG_AllocateSphVarMemory(MagneticModel->nMax);
a = minimum.HeightAboveGeoid; //sets the loop initialization values
b = minimum.phi;
c = minimum.lambda;
d = StartDate.DecimalYear;
for(minimum.HeightAboveGeoid = a; minimum.HeightAboveGeoid <= maximum.HeightAboveGeoid; minimum.HeightAboveGeoid += altitude_step_size) /* Altitude loop*/
{
for(minimum.phi = b; minimum.phi <= maximum.phi; minimum.phi += cord_step_size) /*Latitude loop*/
{
for(minimum.lambda = c; minimum.lambda <= maximum.lambda; minimum.lambda += cord_step_size) /*Longitude loop*/
{
if(Geoid->UseGeoid == 1)
MAG_ConvertGeoidToEllipsoidHeight(&minimum, Geoid); //This converts the height above mean sea level to height above the WGS-84 ellipsoid
else
minimum.HeightAboveEllipsoid = minimum.HeightAboveGeoid;
MAG_GeodeticToSpherical(Ellip, minimum, &CoordSpherical);
MAG_ComputeSphericalHarmonicVariables(Ellip, CoordSpherical, MagneticModel->nMax, SphVariables); /* Compute Spherical Harmonic variables */
MAG_AssociatedLegendreFunction(CoordSpherical, MagneticModel->nMax, LegendreFunction); /* Compute ALF Equations 5-6, WMM Technical report*/
for(StartDate.DecimalYear = d; StartDate.DecimalYear <= EndDate.DecimalYear; StartDate.DecimalYear += time_step) /*Year loop*/
{
MAG_TimelyModifyMagneticModel(StartDate, MagneticModel, TimedMagneticModel); /*This modifies the Magnetic coefficients to the correct date. */
MAG_Summation(LegendreFunction, TimedMagneticModel, *SphVariables, CoordSpherical, &MagneticResultsSph); /* Accumulate the spherical harmonic coefficients Equations 10:12 , WMM Technical report*/
MAG_SecVarSummation(LegendreFunction, TimedMagneticModel, *SphVariables, CoordSpherical, &MagneticResultsSphVar); /*Sum the Secular Variation Coefficients, Equations 13:15 , WMM Technical report */
MAG_RotateMagneticVector(CoordSpherical, minimum, MagneticResultsSph, &MagneticResultsGeo); /* Map the computed Magnetic fields to Geodeitic coordinates Equation 16 , WMM Technical report */
MAG_RotateMagneticVector(CoordSpherical, minimum, MagneticResultsSphVar, &MagneticResultsGeoVar); /* Map the secular variation field components to Geodetic coordinates, Equation 17 , WMM Technical report*/
MAG_CalculateGeoMagneticElements(&MagneticResultsGeo, &GeoMagneticElements); /* Calculate the Geomagnetic elements, Equation 18 , WMM Technical report */
MAG_CalculateSecularVariationElements(MagneticResultsGeoVar, &GeoMagneticElements); /*Calculate the secular variation of each of the Geomagnetic elements, Equation 19, WMM Technical report*/
switch(ElementOption) {
case 1:
PrintElement = GeoMagneticElements.Decl; /*1. Angle between the magnetic field vector and true north, positive east*/
break;
case 2:
PrintElement = GeoMagneticElements.Incl; /*2. Angle between the magnetic field vector and the horizontal plane, positive downward*/
break;
case 3:
PrintElement = GeoMagneticElements.F; /*3. Magnetic Field Strength*/
break;
case 4:
PrintElement = GeoMagneticElements.H; /*4. Horizontal Magnetic Field Strength*/
break;
case 5:
PrintElement = GeoMagneticElements.X; /*5. Northern component of the magnetic field vector*/
break;
case 6:
PrintElement = GeoMagneticElements.Y; /*6. Eastern component of the magnetic field vector*/
break;
case 7:
PrintElement = GeoMagneticElements.Z; /*7. Downward component of the magnetic field vector*/
break;
case 8:
PrintElement = GeoMagneticElements.GV; /*8. The Grid Variation*/
break;
case 9:
PrintElement = GeoMagneticElements.Decldot; /*9. Yearly Rate of change in declination*/
break;
case 10:
PrintElement = GeoMagneticElements.Incldot; /*10. Yearly Rate of change in inclination*/
break;
case 11:
PrintElement = GeoMagneticElements.Fdot; /*11. Yearly rate of change in Magnetic field strength*/
break;
case 12:
PrintElement = GeoMagneticElements.Hdot; /*12. Yearly rate of change in horizontal field strength*/
break;
case 13:
PrintElement = GeoMagneticElements.Xdot; /*13. Yearly rate of change in the northern component*/
break;
case 14:
PrintElement = GeoMagneticElements.Ydot; /*14. Yearly rate of change in the eastern component*/
break;
case 15:
PrintElement = GeoMagneticElements.Zdot; /*15. Yearly rate of change in the downward component*/
break;
case 16:
PrintElement = GeoMagneticElements.GVdot;
/*16. Yearly rate of chnage in grid variation*/;
break;
default:
PrintElement = GeoMagneticElements.Decl; /* 1. Angle between the magnetic field vector and true north, positive east*/
}
if(Geoid->UseGeoid == 1)
{
if(PrintOption == 1) fprintf(fileout, "%5.2lf %6.2lf %8.4lf %7.2lf %10.2lf\n", minimum.phi, minimum.lambda, minimum.HeightAboveGeoid, StartDate.DecimalYear, PrintElement);
else printf("%5.2lf %6.2lf %8.4lf %7.2lf %10.2lf\n", minimum.phi, minimum.lambda, minimum.HeightAboveGeoid, StartDate.DecimalYear, PrintElement);
} else
{
if(PrintOption == 1) fprintf(fileout, "%5.2lf %6.2lf %8.4lf %7.2lf %10.2lf\n", minimum.phi, minimum.lambda, minimum.HeightAboveEllipsoid, StartDate.DecimalYear, PrintElement);
else printf("%5.2lf %6.2lf %8.4lf %7.2lf %10.2lf\n", minimum.phi, minimum.lambda, minimum.HeightAboveEllipsoid, StartDate.DecimalYear, PrintElement);
}
} /* year loop */
} /*Longitude Loop */
} /* Latitude Loop */
} /* Altitude Loop */
if(PrintOption == 1) fclose(fileout);
MAG_FreeMagneticModelMemory(TimedMagneticModel);
MAG_FreeLegendreMemory(LegendreFunction);
MAG_FreeSphVarMemory(SphVariables);
return TRUE;
} /*MAG_Grid*/
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 */
int MAG_robustReadMagneticModel_Large(char *filename, char *filenameSV, MAGtype_MagneticModel **MagneticModel, int array_size)
{
char line[MAXLINELENGTH], ModelName[] = "Enhanced Magnetic Model";/*Model Name must be no longer than 31 characters*/
int n, nMax = 0, nMaxSV = 0, num_terms, a, epochlength=5, i;
FILE *MODELFILE;
MODELFILE = fopen(filename, "r");
fgets(line, MAXLINELENGTH, MODELFILE);
do
{
if(NULL == fgets(line, MAXLINELENGTH, MODELFILE))
break;
a = sscanf(line, "%d", &n);
if(n > nMax && (n < 99999 && a == 1 && n > 0))
nMax = n;
} while(n < 99999 && a == 1);
fclose(MODELFILE);
MODELFILE = fopen(filenameSV, "r");
n = 0;
fgets(line, MAXLINELENGTH, MODELFILE);
do
{
if(NULL == fgets(line, MAXLINELENGTH, MODELFILE))
break;
a = sscanf(line, "%d", &n);
if(n > nMaxSV && (n < 99999 && a == 1 && n > 0))
nMaxSV = n;
} while(n < 99999 && a == 1);
fclose(MODELFILE);
num_terms = CALCULATE_NUMTERMS(nMax);
*MagneticModel = MAG_AllocateModelMemory(num_terms);
(*MagneticModel)->nMax = nMax;
(*MagneticModel)->nMaxSecVar = nMaxSV;
if(nMaxSV > 0) (*MagneticModel)->SecularVariationUsed = TRUE;
for(i = 0; i < num_terms; 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;
}
MAG_readMagneticModel_Large(filename, filenameSV, *MagneticModel);
(*MagneticModel)->CoefficientFileEndDate = (*MagneticModel)->epoch + epochlength;
strcpy((*MagneticModel)->ModelName, ModelName);
(*MagneticModel)->EditionDate = (*MagneticModel)->epoch;
return 1;
} /*MAG_robustReadMagneticModel_Large*/
int MAG_robustReadMagModels(char *filename, MAGtype_MagneticModel *(*magneticmodels)[], int array_size)
{
char line[MAXLINELENGTH];
int n, nMax = 0, num_terms, a;
FILE *MODELFILE;
MODELFILE = fopen(filename, "r");
fgets(line, MAXLINELENGTH, MODELFILE);
if(line[0] == '%')
MAG_readMagneticModel_SHDF(filename, magneticmodels, array_size);
else if(array_size == 1)
{
do
{
if(NULL == fgets(line, MAXLINELENGTH, MODELFILE))
break;
a = sscanf(line, "%d", &n);
if(n > nMax && (n < 99999 && a == 1 && n > 0))
nMax = n;
} while(n < 99999 && a == 1);
num_terms = CALCULATE_NUMTERMS(nMax);
(*magneticmodels)[0] = MAG_AllocateModelMemory(num_terms);
(*magneticmodels)[0]->nMax = nMax;
(*magneticmodels)[0]->nMaxSecVar = nMax;
MAG_readMagneticModel(filename, (*magneticmodels)[0]);
(*magneticmodels)[0]->CoefficientFileEndDate = (*magneticmodels)[0]->epoch + 5;
} else return 0;
fclose(MODELFILE);
return 1;
} /*MAG_robustReadMagModels*/
/*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*/
char MAG_GeomagIntroduction_EMM(MAGtype_MagneticModel *MagneticModel, char* VersionDate)
{
char ans = 'h';
printf("\n\n Welcome to the Enhanced Magnetic Model (EMM) %d C-Program\n\n", (int) MagneticModel->epoch);
printf(" --- Model Release Date: 15 Dec 2009 ---\n");
printf(" --- Software Release Date: %s ---\n\n", VersionDate);
printf("\n This program estimates the strength and direction of ");
printf("\n Earth's main Magnetic field and crustal variation for a given point/area.");
while(ans != 'c' && ans != 'C')
{
printf("\n Enter h for help and contact information or c to continue.");
printf("\n >");
scanf("%c%*[^\n]", &ans);
getchar();
if((ans == 'h') || (ans == 'H'))
{
printf("\n Help information ");
printf("\n The Enhanced Magnetic Model (EMM) for %d", (int) MagneticModel->epoch);
printf("\n is a model of Earth's main Magnetic and crustal field. The EMM");
printf("\n is recomputed every five (5) years, in years divisible by ");
printf("\n five (i.e. 2005, 2010). See the contact information below");
printf("\n to obtain more information on the EMM and associated software.");
printf("\n ");
printf("\n Input required is the location in geodetic latitude and");
printf("\n longitude (positive for northern latitudes and eastern ");
printf("\n longitudes), geodetic altitude in meters, and the date of ");
printf("\n interest in years.");
printf("\n\n\n The program computes the estimated Magnetic Declination");
printf("\n (Decl) which is sometimes called MagneticVAR, Inclination (Incl), Total");
printf("\n Intensity (F or TI), Horizontal Intensity (H or HI), Vertical");
printf("\n Intensity (Z), and Grid Variation (GV). Declination and Grid");
printf("\n Variation are measured in units of degrees and are considered");
printf("\n positive when east or north. Inclination is measured in units");
printf("\n of degrees and is considered positive when pointing down (into");
printf("\n the Earth). The WMM is reference to the WGS-84 ellipsoid and");
printf("\n is valid for 5 years after the base epoch.");
printf("\n\n\n It is very important to note that a degree and order 720 model,");
printf("\n such as EMM, describes the long wavelength spatial Magnetic ");
printf("\n fluctuations due to Earth's core. Also included in the EMM series");
printf("\n models are intermediate and short wavelength spatial fluctuations ");
printf("\n that originate in Earth's mantle and crust. Not included in");
printf("\n the model are temporal fluctuations of Magnetospheric and ionospheric");
printf("\n origin. On the days during and immediately following Magnetic storms,");
printf("\n temporal fluctuations can cause substantial deviations of the Geomagnetic");
printf("\n field from model values. If the required declination accuracy is");
printf("\n more stringent than the EMM series of models provide, the user is");
printf("\n advised to request special (regional or local) surveys be performed");
printf("\n and models prepared. Please make requests of this nature to the");
printf("\n National Geospatial-Intelligence Agency (NGA) at the address below.");
printf("\n\n\n Contact Information");
printf("\n Software and Model Support");
printf("\n National Geophysical Data Center");
printf("\n NOAA EGC/2");
printf("\n 325 Broadway");
printf("\n Boulder, CO 80303 USA");
printf("\n Attn: Manoj Nair or Stefan Maus");
printf("\n Phone: (303) 497-4642 or -6522");
printf("\n Email: [email protected] or [email protected] \n");
}
}
return ans;
}
char MAG_GeomagIntroduction_WMM(MAGtype_MagneticModel *MagneticModel, char *VersionDate)
/*Prints the introduction to the Geomagnetic program. It needs the Magnetic model for the epoch.
* INPUT MagneticModel : MAGtype_MagneticModel With Model epoch (input)
OUTPUT ans (char) user selection
CALLS : none
*/
{
char help = 'h';
char ans;
printf("\n\n Welcome to the World Magnetic Model (WMM) %d C-Program\n\n", (int) MagneticModel->epoch);
printf(" --- Model Release Date: 15 Dec 2009 ---\n");
printf(" --- Software Release Date: %s ---\n\n", VersionDate);
printf("\n This program estimates the strength and direction of ");
printf("\n Earth's main Magnetic field for a given point/area.");
while(help != 'c' && help != 'C')
{
printf("\n Enter h for help and contact information or c to continue.");
printf("\n >");
scanf("%c%*[^\n]", &help);
getchar();
if((help == 'h') || (help == 'H'))
{
printf("\n Help information ");
printf("\n The World Magnetic Model (WMM) for %d", (int) MagneticModel->epoch);
printf("\n is a model of Earth's main Magnetic field. The WMM");
printf("\n is recomputed every five (5) years, in years divisible by ");
printf("\n five (i.e. 2005, 2010). See the contact information below");
printf("\n to obtain more information on the WMM and associated software.");
printf("\n ");
printf("\n Input required is the location in geodetic latitude and");
printf("\n longitude (positive for northern latitudes and eastern ");
printf("\n longitudes), geodetic altitude in meters, and the date of ");
printf("\n interest in years.");
printf("\n\n\n The program computes the estimated Magnetic Declination");
printf("\n (Decl) which is sometimes called MagneticVAR, Inclination (Incl), Total");
printf("\n Intensity (F or TI), Horizontal Intensity (H or HI), Vertical");
printf("\n Intensity (Z), and Grid Variation (GV). Declination and Grid");
printf("\n Variation are measured in units of degrees and are considered");
printf("\n positive when east or north. Inclination is measured in units");
printf("\n of degrees and is considered positive when pointing down (into");
printf("\n the Earth). The WMM is reference to the WGS-84 ellipsoid and");
printf("\n is valid for 5 years after the base epoch.");
printf("\n\n\n It is very important to note that a degree and order 12 model,");
printf("\n such as WMM, describes only the long wavelength spatial Magnetic ");
printf("\n fluctuations due to Earth's core. Not included in the WMM series");
printf("\n models are intermediate and short wavelength spatial fluctuations ");
printf("\n that originate in Earth's mantle and crust. Consequently, isolated");
printf("\n angular errors at various positions on the surface (primarily over");
printf("\n land, along continental margins and over oceanic sea-mounts, ridges and");
printf("\n trenches) of several degrees may be expected. Also not included in");
printf("\n the model are temporal fluctuations of Magnetospheric and ionospheric");
printf("\n origin. On the days during and immediately following Magnetic storms,");
printf("\n temporal fluctuations can cause substantial deviations of the Geomagnetic");
printf("\n field from model values. If the required declination accuracy is");
printf("\n more stringent than the WMM series of models provide, the user is");
printf("\n advised to request special (regional or local) surveys be performed");
printf("\n and models prepared. Please make requests of this nature to the");
printf("\n National Geospatial-Intelligence Agency (NGA) at the address below.");
printf("\n\n\n Contact Information");
printf("\n Software and Model Support");
printf("\n National Geophysical Data Center");
printf("\n NOAA EGC/2");
printf("\n 325 Broadway");
printf("\n Boulder, CO 80303 USA");
printf("\n Attn: Manoj Nair or Stefan Maus");
printf("\n Phone: (303) 497-4642 or -6522");
printf("\n Email: [email protected] or [email protected] \n");
}
}
ans = help;
return ans;
} /*MAG_GeomagIntroduction_WMM*/
int MAG_GetUserGrid(MAGtype_CoordGeodetic *minimum, MAGtype_CoordGeodetic *maximum, double *step_size, double *a_step_size, double *step_time, MAGtype_Date
*StartDate, MAGtype_Date *EndDate, int *ElementOption, int *PrintOption, char *OutputFile, MAGtype_Geoid *Geoid)
/* Prompts user to enter parameters to compute a grid - for use with the MAG_grid function
Note: The user entries are not validated before here. The function populates the input variables & data structures.
UPDATE : minimum Pointer to data structure with the following elements
double lambda; (longitude)
double phi; ( geodetic latitude)
double HeightAboveEllipsoid; (height above the ellipsoid (HaE) )
double HeightAboveGeoid;(height above the Geoid )
maximum -same as the above -MAG_USE_GEOID
step_size : double pointer : spatial step size, in decimal degrees
a_step_size : double pointer : double altitude step size (km)
step_time : double pointer : time step size (decimal years)
StartDate : pointer to data structure with the following elements updates
double DecimalYear; ( decimal years )
EndDate : Same as the above
CALLS : none
*/
{
FILE *fileout;
char filename[] = "GridProgramDirective.txt";
char buffer[20];
int dummy;
printf("Please Enter Minimum Latitude (in decimal degrees):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &minimum->phi);
strcpy(buffer, "");
printf("Please Enter Maximum Latitude (in decimal degrees):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &maximum->phi);
strcpy(buffer, "");
printf("Please Enter Minimum Longitude (in decimal degrees):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &minimum->lambda);
strcpy(buffer, "");
printf("Please Enter Maximum Longitude (in decimal degrees):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &maximum->lambda);
strcpy(buffer, "");
printf("Please Enter Step Size (in decimal degrees):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", step_size);
strcpy(buffer, "");
printf("Select height (default : above MSL) \n1. Above Mean Sea Level\n2. Above WGS-84 Ellipsoid \n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%d", &dummy);
if(dummy == 2) Geoid->UseGeoid = 0;
else Geoid->UseGeoid = 1;
strcpy(buffer, "");
if(Geoid->UseGeoid == 1)
{
printf("Please Enter Minimum Height above MSL (in km):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &minimum->HeightAboveGeoid);
strcpy(buffer, "");
printf("Please Enter Maximum Height above MSL (in km):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &maximum->HeightAboveGeoid);
strcpy(buffer, "");
printf("Please Enter height step size (in km):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", a_step_size);
strcpy(buffer, "");
} else
{
printf("Please Enter Minimum Height above the WGS-84 Ellipsoid (in km):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &minimum->HeightAboveGeoid);
minimum->HeightAboveEllipsoid = minimum->HeightAboveGeoid;
strcpy(buffer, "");
printf("Please Enter Maximum Height above the WGS-84 Ellipsoid (in km):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &maximum->HeightAboveGeoid);
maximum->HeightAboveEllipsoid = minimum->HeightAboveGeoid;
strcpy(buffer, "");
printf("Please Enter height step size (in km):\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", a_step_size);
strcpy(buffer, "");
}
printf("\nPlease Enter the decimal year starting time:\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &StartDate->DecimalYear);
strcpy(buffer, "");
printf("Please Enter the decimal year ending time:\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", &EndDate->DecimalYear);
strcpy(buffer, "");
printf("Please Enter the time step size:\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%lf", step_time);
strcpy(buffer, "");
printf("Enter a geomagnetic element to print. Your options are :\n");
printf(" 1. Declination 9. Ddot\n 2. Inclination 10. Idot\n 3. F 11. Fdot\n 4. H 12. Hdot\n 5. X 13. Xdot\n 6. Y 14. Ydot\n 7. Z 15. Zdot\n 8. GV 16. GVdot\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%d", ElementOption);
strcpy(buffer, "");
printf("Select output :\n");
printf(" 1. Print to a file \n 2. Print to Screen\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%d", PrintOption);
strcpy(buffer, "");
fileout = fopen(filename, "a");
if(*PrintOption == 1)
{
printf("Please enter output filename\nfor default ('GridResults.txt') press enter:\n");
fgets(buffer, 20, stdin);
if(strlen(buffer) <= 1)
{
strcpy(OutputFile, "GridResults.txt");
fprintf(fileout, "\nResults printed in: GridResults.txt\n");
strcpy(OutputFile, "GridResults.txt");
} else
{
sscanf(buffer, "%s", OutputFile);
fprintf(fileout, "\nResults printed in: %s\n", OutputFile);
}
/*strcpy(OutputFile, buffer);*/
strcpy(buffer, "");
/*sscanf(buffer, "%s", OutputFile);*/
} else
fprintf(fileout, "\nResults printed in Console\n");
fprintf(fileout, "Minimum Latitude: %lf\t\tMaximum Latitude: %lf\t\tStep Size: %lf\nMinimum Longitude: %lf\t\tMaximum Longitude: %lf\t\tStep Size: %lf\n", minimum->phi, maximum->phi, *step_size, minimum->lambda, maximum->lambda, *step_size);
if(Geoid->UseGeoid == 1)
fprintf(fileout, "Minimum Altitude above MSL: %lf\tMaximum Altitude above MSL: %lf\tStep Size: %lf\n", minimum->HeightAboveGeoid, maximum->HeightAboveGeoid, *a_step_size);
else
fprintf(fileout, "Minimum Altitude above MSL: %lf\tMaximum Altitude above WGS-84 Ellipsoid: %lf\tStep Size: %lf\n", minimum->HeightAboveEllipsoid, maximum->HeightAboveEllipsoid, *a_step_size);
fprintf(fileout, "Starting Date: %lf\t\tEnding Date: %lf\t\tStep Time: %lf\n\n\n", StartDate->DecimalYear, EndDate->DecimalYear, *step_time);
fclose(fileout);
return TRUE;
}
int MAG_GetUserInput(MAGtype_MagneticModel *MagneticModel, MAGtype_Geoid *Geoid, MAGtype_CoordGeodetic *CoordGeodetic, MAGtype_Date *MagneticDate)
/*
This prompts the user for coordinates, and accepts many entry formats.
It takes the MagneticModel and Geoid as input and outputs the Geographic coordinates and Date as objects.
Returns 0 when the user wants to exit and 1 if the user enters valid input data.
INPUT : MagneticModel : Data structure with the following elements used here
double epoch; Base time of Geomagnetic model epoch (yrs)
: Geoid Pointer to data structure MAGtype_Geoid (used for converting HeightAboveGeoid to HeightABoveEllipsoid
OUTPUT: CoordGeodetic : Pointer to data structure. Following elements are updated
double lambda; (longitude)
double phi; ( geodetic latitude)
double HeightAboveEllipsoid; (height above the ellipsoid (HaE) )
double HeightAboveGeoid;(height above the Geoid )
MagneticDate : Pointer to data structure MAGtype_Date with the following elements updated
int Year; (If user directly enters decimal year this field is not populated)
int Month;(If user directly enters decimal year this field is not populated)
int Day; (If user directly enters decimal year this field is not populated)
double DecimalYear; decimal years
CALLS: MAG_DMSstringToDegree(buffer, &CoordGeodetic->lambda); (The program uses this to convert the string into a decimal longitude.)
MAG_ValidateDMSstringlong(buffer, Error_Message)
MAG_ValidateDMSstringlat(buffer, Error_Message)
MAG_Warnings
MAG_ConvertGeoidToEllipsoidHeight
MAG_DateToYear
*/
{
char Error_Message[255];
char buffer[40];
char tmp;
int i, j, a, b, c, done = 0;
strcpy(buffer, ""); /*Clear the input */
printf("\nPlease enter latitude");
printf("\nNorth Latitude positive, For example:");
printf("\n30, 30, 30 (D,M,S) or 30.508 (Decimal Degrees) (both are north)\n");
fgets(buffer, 40, stdin);
for(i = 0, done = 0, j = 0; i <= 40 && !done; i++)
{
if(buffer[i] == '.')
{
j = sscanf(buffer, "%lf", &CoordGeodetic->phi);
if(j == 1)
done = 1;
else
done = -1;
}
if(buffer[i] == ',')
{
if(MAG_ValidateDMSstringlat(buffer, Error_Message))
{
MAG_DMSstringToDegree(buffer, &CoordGeodetic->phi);
done = 1;
} else
done = -1;
}
if(buffer[i] == ' ')/* This detects if there is a ' ' somewhere in the string,
if there is the program tries to interpret the input as Degrees Minutes Seconds.*/
{
if(MAG_ValidateDMSstringlat(buffer, Error_Message))
{
MAG_DMSstringToDegree(buffer, &CoordGeodetic->phi);
done = 1;
} else
done = -1;
}
if(buffer[i] == '\0' || done == -1)
{
if(MAG_ValidateDMSstringlat(buffer, Error_Message) && done != -1)
{
sscanf(buffer, "%lf", &CoordGeodetic->phi);
done = 1;
} else
{
printf("%s", Error_Message);
strcpy(buffer, "");
printf("\nError encountered, please re-enter as '(-)DDD,MM,SS' or in Decimal Degrees DD.ddd:\n");
fgets(buffer, 40, stdin);
i = -1;
done = 0;
}
}
}
strcpy(buffer, ""); /*Clear the input*/
printf("\nPlease enter longitude");
printf("\nEast longitude positive, West negative. For example:");
printf("\n-100.5 or -100, 30, 0 for 100.5 degrees west\n");
fgets(buffer, 40, stdin);
for(i = 0, done = 0, j = 0; i <= 40 && !done; i++)/*This for loop determines how the user is trying to enter their data, and makes sure that it is copied into the correct location*/
{
if(buffer[i] == '.') /*This detects if there is a '.' somewhere in the string, if there is the program tries to interpret the input as a double, and copies it to the longitude*/
{
j = sscanf(buffer, "%lf", &CoordGeodetic->lambda);
if(j == 1)
done = 1; /*This control ends the loop*/
else
done = -1; /*This copies an end string into the buffer so that the user is sent to the Re-enter input message*/
}
if(buffer[i] == ',')/*This detects if there is a ',' somewhere in the string, if there is the program tries to interpret the input as Degrees, Minutes, Seconds.*/
{
if(MAG_ValidateDMSstringlong(buffer, Error_Message))
{
MAG_DMSstringToDegree(buffer, &CoordGeodetic->lambda); /*The program uses this to convert the string into a decimal longitude.*/
done = 1;
} else
done = -1;
}
if(buffer[i] == ' ')/* This detects if there is a ' ' somewhere in the string, if there is the program tries to interpret the input as Degrees Minutes Seconds.*/
{
if(MAG_ValidateDMSstringlong(buffer, Error_Message))
{
MAG_DMSstringToDegree(buffer, &CoordGeodetic->lambda);
done = 1;
} else
done = -1;
}
if(buffer[i] == '\0' || done == -1) /*If the program reaches the end of the string before finding a "." or a "," or if its been sent by an error it does this*/
{
MAG_ValidateDMSstringlong(buffer, Error_Message); /*The program attempts to determine if all the characters in the string are legal, and then tries to interpret the string as a simple degree entry, like "0", or "67"*/
if(MAG_ValidateDMSstringlong(buffer, Error_Message) && done != -1)
{
sscanf(buffer, "%lf", &CoordGeodetic->lambda);
done = 1;
} else /*The string is neither DMS, a decimal degree, or a simple degree input, or has some error*/