Skip to content

Commit eb236ff

Browse files
committed
Trying to make test data
Using the BGS API to make some grids of the IGRF to test against their results.
1 parent ca69c33 commit eb236ff

File tree

7 files changed

+103
-7
lines changed

7 files changed

+103
-7
lines changed

bx.npy

22.1 KB
Binary file not shown.

by.npy

22.1 KB
Binary file not shown.

bz.npy

22.1 KB
Binary file not shown.

harmonica/_spherical_harmonics/igrf.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
# Copyright (c) 2018 The Harmonica Developers.
2+
# Distributed under the terms of the BSD 3-Clause License.
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
#
5+
# This code is part of the Fatiando a Terra project (https://www.fatiando.org)
6+
#
17
"""
28
Calculation of the IGRF magnetic field.
39
"""
10+
411
import pathlib
512

613
import boule
@@ -14,8 +21,7 @@
1421

1522

1623
def fetch_igrf13():
17-
"""
18-
"""
24+
""" """
1925
path = pooch.retrieve(
2026
url="doi:10.5281/zenodo.11269410/igrf13coeffs.txt",
2127
path=pooch.os_cache("harmonica"),
@@ -40,9 +46,9 @@ def interpolate_coefficients(date, g, h, years):
4046
return g_date, h_date
4147

4248

43-
class IGRF13():
49+
class IGRF14:
4450
"""
45-
13th generation of the International Geomagnetic Reference Field
51+
14th generation of the International Geomagnetic Reference Field
4652
"""
4753

4854
def __init__(self, date, ellipsoid=boule.WGS84):
@@ -82,12 +88,16 @@ def predict(self, coordinates, field="b"):
8288
b_north = np.zeros(n_data)
8389
b_up = np.zeros(n_data)
8490
g, h = self.coefficients
85-
_evaluate_igrf(longitude, colatitude, radius, g, h, self.max_degree, b_east, b_north, b_up)
91+
_evaluate_igrf(
92+
longitude, colatitude, radius, g, h, self.max_degree, b_east, b_north, b_up
93+
)
8694
return b_east, b_north, b_up
8795

8896

8997
@numba.jit(parallel=True, nopython=True)
90-
def _evaluate_igrf(longitude, colatitude, radius, g, h, max_degree, b_east, b_north, b_up):
98+
def _evaluate_igrf(
99+
longitude, colatitude, radius, g, h, max_degree, b_east, b_north, b_up
100+
):
91101
n_data = longitude.size
92102
for i in numba.prange(n_data):
93-
plm =
103+
plm = 1
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) 2018 The Harmonica Developers.
2+
# Distributed under the terms of the BSD 3-Clause License.
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
#
5+
# This code is part of the Fatiando a Terra project (https://www.fatiando.org)
6+
#
7+
"""
8+
Script to use the BGS API to get values of the IGRF for testing purposes.
9+
10+
"""
11+
12+
import datetime
13+
import json
14+
15+
import numpy as np
16+
import requests
17+
import verde as vd
18+
import xarray as xr
19+
20+
coordinates = vd.grid_coordinates((0, 359, -90, 90), spacing=10, extra_coords=0)
21+
22+
dates = ["1930-04-20", "1986-07-29", "2024-01-10", "2029-10-30"]
23+
24+
shape = (len(dates), *coordinates[0].shape)
25+
bx = np.empty(shape)
26+
by = np.empty(shape)
27+
bz = np.empty(shape)
28+
29+
url = (
30+
"https://geomag.bgs.ac.uk/web_service/GMModels/igrf/14/?"
31+
"latitude={latitude}&longitude={longitude}&altitude={altitude}&date={date}&"
32+
"format=json"
33+
)
34+
35+
for k in range(shape[0]):
36+
for i in range(shape[1]):
37+
for j in range(shape[2]):
38+
response = requests.get(
39+
url.format(
40+
longitude=coordinates[0][i, j],
41+
latitude=coordinates[1][i, j],
42+
altitude=coordinates[2][i, j],
43+
date=dates[k],
44+
)
45+
)
46+
response.raise_for_status()
47+
results = json.loads(response.text)
48+
field = results["geomagnetic-field-model-result"]["field-value"]
49+
bx[k, i, j] = field["east-intensity"]["value"]
50+
by[k, i, j] = field["north-intensity"]["value"]
51+
# The BGS gives vertical downward
52+
bz[k, i, j] = -field["vertical-intensity"]["value"]
53+
54+
np.save("bx.npy", bx)
55+
np.save("by.npy", by)
56+
np.save("bz.npy", bz)
57+
58+
bx = np.load("bx.npy")
59+
by = np.load("by.npy")
60+
bz = np.load("bz.npy")
61+
62+
time = [datetime.datetime.fromisoformat(d) for d in dates]
63+
dims = ("time", "latitude", "longitude")
64+
grid = xr.Dataset(
65+
{"bx": (dims, bx), "by": (dims, by), "bz": (dims, bz)},
66+
coords={
67+
"time": time,
68+
"longitude": coordinates[0][0, :],
69+
"latitude": coordinates[1][:, 0],
70+
},
71+
)
72+
grid.to_netcdf("igrf14-bgs.nc")

harmonica/tests/test_igrf.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2018 The Harmonica Developers.
2+
# Distributed under the terms of the BSD 3-Clause License.
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
#
5+
# This code is part of the Fatiando a Terra project (https://www.fatiando.org)
6+
#
7+
"""
8+
Test the IGRF results against the ones calculated by the BGS.
9+
"""
10+
import pytest
11+
12+
13+
from .._spherical_harmonics.igrf import load_igrf, fetch_igrf,
14+
interpolate_coefficients, IGRF14

igrf14-bgs.nc

76.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)