A C# library for geomagnetic field calculations using spherical harmonic models. GeoMag # is a port of the C++ application GeoMag 7.0 distributed by NOAA.
dotnet add package GeoMagSharp
Or via the NuGet Package Manager:
Install-Package GeoMagSharp
using GeoMagSharp;
// Load a coefficient file
var geoMag = new GeoMag();
geoMag.LoadModel("WMM2025.COF");
// Configure calculation
var options = new CalculationOptions
{
Latitude = 45.0,
Longitude = -93.0,
StartDate = new DateTime(2025, 7, 1),
SecularVariation = true,
CalculationMethod = Algorithm.BGS
};
options.SetElevation(0, Distance.Unit.meter, true);
// Run calculation
geoMag.MagneticCalculations(options);
// Access results
var result = geoMag.ResultsOfCalculation[0];
Console.WriteLine($"Declination: {result.Declination.Value:F2} degrees");
Console.WriteLine($"Inclination: {result.Inclination.Value:F2} degrees");
Console.WriteLine($"Total Field: {result.TotalField.Value:F1} nT");// Async with progress reporting and cancellation
var cts = new CancellationTokenSource();
var progress = new Progress<CalculationProgressInfo>(p =>
Console.WriteLine($"{p.PercentComplete:F0}% - {p.StatusMessage}"));
var model = await ModelReader.ReadAsync("WMM2025.COF", progress, cts.Token);
await geoMag.MagneticCalculationsAsync(options, progress, cts.Token);
await geoMag.SaveResultsAsync("output.txt", false, cts.Token);| Model | Type | Source | Included |
|---|---|---|---|
| WMM | World Magnetic Model | NOAA | WMM2025.COF, WMM2015.COF |
| WMMHR | WMM High Resolution | NOAA | WMMHR.COF |
| IGRF | International Geomagnetic Reference Field | IAGA | IGRF12.COF |
| EMM | Enhanced Magnetic Model | NOAA | No (survey required) |
| BGGM | BGS Global Geomagnetic Model | BGS | No (commercial license) |
Bundled coefficient files are in the coefficient/ directory. See coefficient/NOTICE.md for attribution and download links for non-bundled models.
- .NET Framework 4.8
- .NET Standard 2.0 (compatible with .NET Core 2.0+, .NET 5+)
GeoMag- Main entry point for loading models and running calculationsModelReader- Parses COF/DAT coefficient files into model objectsCalculator- Low-level spherical harmonic calculation engineCalculationOptions- Configuration for latitude, longitude, date, elevation, etc.
MagneticModelSet- A set of magnetic models (main field + secular variation)MagneticModelCollection- Manages multiple model sets with JSON serializationMagneticCalculations- Calculation results (declination, inclination, field components)
Each MagneticCalculations result contains MagneticValue objects with:
Value- The calculated valueChangePerYear- Secular variation (when enabled)
Available components: Declination, Inclination, TotalField, HorizontalIntensity, NorthComponent, EastComponent, VerticalComponent, GridVariation.
MIT License - see LICENSE for details.
Bundled coefficient files are U.S. Government works in the public domain. See coefficient/NOTICE.md for full attribution.