Skip to content

Commit 07dff34

Browse files
committed
Fix interpolated coefficient shape
The interpolation was always going to the maximum degree instead of stopping at the desired maximum.
1 parent e155008 commit 07dff34

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

harmonica/_spherical_harmonics/igrf.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def load_igrf(path):
7676
return years, coeffs["g"], coeffs["h"], coeffs["g_sv"], coeffs["h_sv"]
7777

7878

79-
def interpolate_coefficients(date, years, g, h, g_sv, h_sv):
79+
def interpolate_coefficients(date, max_degree, years, g, h, g_sv, h_sv):
8080
"""
8181
Interpolate the Gauss coefficients to the given date.
8282
@@ -87,6 +87,8 @@ def interpolate_coefficients(date, years, g, h, g_sv, h_sv):
8787
----------
8888
date : :class:`datetime.datetime`
8989
The date and time at which to interpolate the coefficients.
90+
max_degree : int
91+
The maximum degree that will be interpolated.
9092
years : 1d-array
9193
The years for the knot points (epochs) of the time interpolation.
9294
g, h : 3d-arrays
@@ -106,7 +108,8 @@ def interpolate_coefficients(date, years, g, h, g_sv, h_sv):
106108
g_date, h_date : 2d-arrays
107109
The interpolated Gauss coefficients. The first dimension is the degree
108110
n and the second is the order m. At m > n, the coefficients are
109-
assigned zero. Units are nT.
111+
assigned zero. The shape of the arrays will be ``(max_degree + 1,
112+
max_degree + 1)``. Units are nT.
110113
"""
111114
if date.year < years[0] or date.year >= years[-1] + 5:
112115
message = (
@@ -124,9 +127,9 @@ def interpolate_coefficients(date, years, g, h, g_sv, h_sv):
124127
- datetime.datetime(year=years[index], month=1, day=1)
125128
).total_seconds()
126129
year_in_seconds = 365.25 * 24 * 60 * 60
127-
g_date = np.zeros(g.shape[1:])
128-
h_date = np.zeros(h.shape[1:])
129-
for n in range(1, g.shape[1]):
130+
g_date = np.zeros((max_degree + 1, max_degree + 1))
131+
h_date = np.zeros((max_degree + 1, max_degree + 1))
132+
for n in range(1, max_degree + 1):
130133
for m in range(n + 1):
131134
if index >= years.size - 1:
132135
variation_g = g_sv[n, m] / year_in_seconds
@@ -294,7 +297,7 @@ def __init__(
294297
def coefficients(self):
295298
if self._coefficients is None:
296299
self._coefficients = interpolate_coefficients(
297-
self.date, *load_igrf(self._fetch_coefficient_file())
300+
self.date, self.max_degree, *load_igrf(self._fetch_coefficient_file())
298301
)
299302
return self._coefficients
300303

harmonica/tests/test_igrf.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def test_interpolate_coefficients():
163163
for i, year in enumerate(years):
164164
g_date, h_date = interpolate_coefficients(
165165
datetime.datetime(year, month=1, day=1, hour=0, minute=1, second=0),
166+
g.shape[1] - 1,
166167
years,
167168
g,
168169
h,
@@ -175,6 +176,23 @@ def test_interpolate_coefficients():
175176
npt.assert_allclose(h_date[n, m], h[i, n, m], atol=0.001)
176177

177178

179+
def test_interpolate_coefficients_max_degree():
180+
"Check that the returned coefficients stop at the max degree"
181+
max_degree = 13
182+
years, g, h, g_sv, h_sv = load_igrf(IGRF14("2020-02-10")._fetch_coefficient_file())
183+
g_date, h_date = interpolate_coefficients(
184+
datetime.datetime(year=2023, month=1, day=20, hour=0, minute=1, second=0),
185+
max_degree,
186+
years,
187+
g,
188+
h,
189+
g_sv,
190+
h_sv,
191+
)
192+
assert g_date.shape == h_date.shape
193+
assert g_date.shape == (max_degree + 1, max_degree + 1)
194+
195+
178196
@pytest.mark.parametrize(
179197
"date",
180198
[datetime.datetime(1800, month=1, day=1), datetime.datetime(2030, month=1, day=1)],
@@ -184,6 +202,7 @@ def test_interpolate_coefficients_invalid_date(date):
184202
with pytest.raises(ValueError, match="Invalid date"):
185203
g_date, h_date = interpolate_coefficients(
186204
date,
205+
None,
187206
list(range(1900, 2030, 5)),
188207
None,
189208
None,

0 commit comments

Comments
 (0)