/libgeomag

NGDC geomag code re-written as a library: computes IGRF field values given lat/long/altitude/date. Also works on embedded devices without file I/O.

Primary LanguageC

libgeomag

This repository contains NGDC geomag source code that has been modified to allow it to be used as a library for inclusion in larger projects.

Building

Build with

$ make

By default, this will build the static library (libgeomag.a) and a test utility called test_geomag(.exe). (Building test_geomag requires that NetCDF be installed.)

Testing

To run the test, you will need to download the geomag program from the NGDC's website and extract IGRF12.COF. Place said file in the same directory as test_geomag. Run the test with

$ ./test_geomag

or

$ ./test_geomag.exe

on Windows. This generates the file test.nc containing a uniform sampling of the Earth's magnetic field at a fixed height covering the globe. You can use the NetCDF operators (nco) to verify that the output generated by test_geomag is the same as what geomag70.exe (the program provided by NGDC) would generate by running

$ python geomag2nc.py

which generates a file out.nc. (You will need to place geomag70.exe in the same directory as geomag2nc.py.) Then, take the difference of the two files with

$ ncbo --op_typ=- out.nc test.nc diff.nc

The output of ncbo may then be inspected visually with ncdump:

$ ncdump diff.nc

You should find that the contents of the file are very close to zero. (The reason for the error is actually because of truncation of digits in geomag70.exe output, not because of a difference in this code from the original sources.)

Use in embedded applications

libgeomag now supports easier incorporation into embedded applications.

Build the embedded version of libgeomag with:

$ make test_geomag_embedded

This will build a library libgeomag-embedded.a and an executable test_geomag_embedded. The test_geomag_embedded executable does the same as test_geomag, except it is linked against libgeomag-embedded.a. This version of the library does not provide read_model(); instead, it contains a globally defined BFieldModel instance (model, accessible via extern) containing IGRF field coefficients. This means that libgeomag-embedded.a (unlike libgeomag.a) has no dependencies on C standard I/O functions (e.g., fopen and fread).

Note that these IGRF field coefficients find their way into the source code at compile-time; you must have IGRF12.COF present in your source directory for compilation of libgeomag-embedded.a to work. At compile- time, another executable, print_model, will be generated, which is run to generate a source file igrf.c. This file contains the field coefficients and is built as part of libgeomag-embedded.a.