Skip to content

Commit a63701c

Browse files
committed
FIX: correct default Earth radius in geopotential height conversions
The default radius of geopotential_height_to_geometric_height, geopotential_to_height_asl and geopotential_to_height_agl was 63781370 m - 10x the WGS-84 semi-major axis - contradicting their own docstrings ("defaulting to 6378137.0"). The docstring examples were anchored to the 10x-radius values, masking the bug. The main Environment processing paths already pass earth_radius explicitly and are unaffected; the default only bites when the helpers are reused directly (e.g. EnvironmentAnalysis surface elevation from NOAA reanalysis 'z'), biasing heights low by ~14 m at 10 km and ~57 m at 20 km. Defaults now use 6378137.0 m; docstring examples and a regression test pin the correct values.
1 parent 7e785a6 commit a63701c

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

‎rocketpy/tools.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ def time_num_to_date_string(time_num, units, timezone, calendar="gregorian"):
969969
return date_string, hour_string, date_time
970970

971971

972-
def geopotential_height_to_geometric_height(geopotential_height, radius=63781370.0):
972+
def geopotential_height_to_geometric_height(geopotential_height, radius=6378137.0):
973973
"""Converts geopotential height to geometric height.
974974
975975
Parameters
@@ -992,14 +992,14 @@ def geopotential_height_to_geometric_height(geopotential_height, radius=63781370
992992
>>> geopotential_height_to_geometric_height(0)
993993
0.0
994994
>>> geopotential_height_to_geometric_height(10000)
995-
10001.568101798659
995+
10015.70317975257
996996
>>> geopotential_height_to_geometric_height(20000)
997-
20006.2733909262
997+
20062.91151008542
998998
"""
999999
return radius * geopotential_height / (radius - geopotential_height)
10001000

10011001

1002-
def geopotential_to_height_asl(geopotential, radius=63781370, g=9.80665):
1002+
def geopotential_to_height_asl(geopotential, radius=6378137.0, g=9.80665):
10031003
"""Compute height above sea level from geopotential.
10041004
10051005
Source: https://en.wikipedia.org/wiki/Geopotential
@@ -1010,7 +1010,7 @@ def geopotential_to_height_asl(geopotential, radius=63781370, g=9.80665):
10101010
Geopotential in m^2/s^2. It is the geopotential value at a given
10111011
pressure level, to be converted to height above sea level.
10121012
radius : float, optional
1013-
Earth radius in m. Default is 63781370 m.
1013+
Earth radius in m. Default is 6378137.0 m (WGS-84 semi-major axis).
10141014
g : float, optional
10151015
Gravity acceleration in m/s^2. Default is 9.80665 m/s^2.
10161016
@@ -1025,15 +1025,15 @@ def geopotential_to_height_asl(geopotential, radius=63781370, g=9.80665):
10251025
>>> geopotential_to_height_asl(0)
10261026
0.0
10271027
>>> geopotential_to_height_asl(100000)
1028-
10198.792680243916
1028+
10213.491133844715
10291029
>>> geopotential_to_height_asl(200000)
1030-
20400.84750449947
1030+
20459.74503595946
10311031
"""
10321032
geopotential_height = geopotential / g
10331033
return geopotential_height_to_geometric_height(geopotential_height, radius)
10341034

10351035

1036-
def geopotential_to_height_agl(geopotential, elevation, radius=63781370, g=9.80665):
1036+
def geopotential_to_height_agl(geopotential, elevation, radius=6378137.0, g=9.80665):
10371037
"""Compute height above ground level from geopotential and elevation.
10381038
10391039
Parameters
@@ -1044,7 +1044,7 @@ def geopotential_to_height_agl(geopotential, elevation, radius=63781370, g=9.806
10441044
elevation : float
10451045
Surface elevation in m
10461046
radius : float, optional
1047-
Earth radius in m. Default is 63781370 m.
1047+
Earth radius in m. Default is 6378137.0 m (WGS-84 semi-major axis).
10481048
g : float, optional
10491049
Gravity acceleration in m/s^2. Default is 9.80665 m/s^2.
10501050
@@ -1059,9 +1059,9 @@ def geopotential_to_height_agl(geopotential, elevation, radius=63781370, g=9.806
10591059
>>> geopotential_to_height_agl(0, 0)
10601060
0.0
10611061
>>> geopotential_to_height_agl(100000, 0)
1062-
10198.792680243916
1062+
10213.491133844715
10631063
>>> geopotential_to_height_agl(100000, 1000)
1064-
9198.792680243916
1064+
9213.491133844715
10651065
"""
10661066
return geopotential_to_height_asl(geopotential, radius, g) - elevation
10671067

‎tests/unit/test_tools.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
euler313_to_quaternions,
1313
find_roots_cubic_function,
1414
generate_monte_carlo_ellipses,
15+
geopotential_height_to_geometric_height,
1516
haversine,
1617
inverted_haversine,
1718
mercator_to_wgs84,
@@ -22,7 +23,6 @@
2223
tuple_handler,
2324
)
2425

25-
2626
WEB_MERCATOR_EARTH_RADIUS = 6378137.0
2727

2828

@@ -347,3 +347,21 @@ def test_mercator_extent_to_local_preserves_offset_sign(
347347
assert local_extent[0] < local_extent[1]
348348
assert local_extent[2] < local_extent[3]
349349
assert all(expected_sign * value > 0 for value in local_extent)
350+
351+
352+
def test_geopotential_height_to_geometric_height_default_radius():
353+
"""The default radius must be the WGS-84 semi-major axis (6378137.0 m).
354+
A previous default of 63781370.0 (10x the Earth radius) biased the
355+
conversion low by ~14 m at 10 km and ~57 m at 20 km.
356+
"""
357+
assert geopotential_height_to_geometric_height(0) == 0.0
358+
assert geopotential_height_to_geometric_height(10000) == pytest.approx(
359+
10015.70317975257, rel=1e-10
360+
)
361+
assert geopotential_height_to_geometric_height(20000) == pytest.approx(
362+
20062.91151008542, rel=1e-10
363+
)
364+
# Explicit radius still overrides the default
365+
assert geopotential_height_to_geometric_height(
366+
10000, radius=6378137.0
367+
) == pytest.approx(10015.70317975257, rel=1e-10)

0 commit comments

Comments
 (0)