get_mag_var_table.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. # This code run compatibly under Python 2 and 3.x for x >= 2.
  3. # Preserve this property!
  4. #
  5. # This file is Copyright 2019 by the GPSD project
  6. # SPDX-License-Identifier: BSD-2-Clause
  7. """Simple program to extract WMM2015 data for use in geoid.c
  8. Extract 5x5 magnetic variations in WMM2015 from MagneticField.
  9. """
  10. import sys
  11. import subprocess
  12. for lat in range(-90, 91, 5):
  13. if -90 != lat:
  14. sys.stdout.write("},")
  15. sys.stdout.write("\n /* %d */\n { " % lat)
  16. cnt = 0
  17. for lon in range(-180, 181, 5):
  18. ge = subprocess.Popen(["MagneticField"],
  19. stdin=subprocess.PIPE,
  20. stdout=subprocess.PIPE,
  21. stderr=subprocess.PIPE,
  22. bufsize=0)
  23. # magnetic variation varies, pin at 1 Jan 2020
  24. out, err = ge.communicate(b"2020-01-01 %d %d\n" % (lat, lon))
  25. # round to even cm
  26. parts = out.split()
  27. val = round(float(parts[0]) * 100)
  28. sys.stdout.write("%6d" % val)
  29. cnt += 1
  30. if 0 == (cnt % 9):
  31. sys.stdout.write(",\n ")
  32. elif 73 != cnt:
  33. sys.stdout.write(", ")
  34. sys.stdout.write("}\n};\n")