forked from astropy/astropy
/
test_geodetic_representations.py
235 lines (182 loc) · 7.92 KB
/
test_geodetic_representations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Test geodetic representations"""
import pytest
from astropy import units as u
from astropy.coordinates.representation import (
CartesianRepresentation,
REPRESENTATION_CLASSES,
BaseGeodeticRepresentation,
BaseBodycentricRepresentation,
GRS80GeodeticRepresentation,
WGS72GeodeticRepresentation,
WGS84GeodeticRepresentation,
)
from astropy.coordinates.representation.geodetic import ELLIPSOIDS
from astropy.tests.helper import assert_quantity_allclose
from astropy.units.tests.test_quantity_erfa_ufuncs import vvd
# Preserve the original REPRESENTATION_CLASSES dict so that importing
# the test file doesn't add a persistent test subclass
from astropy.coordinates.tests.test_representation import ( # noqa: F401
setup_function,
teardown_function,
)
class CustomGeodetic(BaseGeodeticRepresentation):
_flattening = 0.01832
_equatorial_radius = 4000000.0 * u.m
class CustomSphericGeodetic(BaseGeodeticRepresentation):
_flattening = 0.0
_equatorial_radius = 4000000.0 * u.m
class CustomSphericBodycentric(BaseBodycentricRepresentation):
_flattening = 0.0
_equatorial_radius = 4000000.0 * u.m
class IAUMARS2000GeodeticRepresentation(BaseGeodeticRepresentation):
_equatorial_radius = 3396190.0 * u.m
_flattening = 0.5886007555512007 * u.percent
class IAUMARS2000BodycentricRepresentation(BaseBodycentricRepresentation):
_equatorial_radius = 3396190.0 * u.m
_flattening = 0.5886007555512007 * u.percent
def test_geodetic_bodycentric_equivalence_spherical_bodies():
initial_cartesian = CartesianRepresentation(
x=[1, 3000.0] * u.km, y=[7000.0, 4.0] * u.km, z=[5.0, 6000.0] * u.km
)
gd_transformed = CustomSphericGeodetic.from_representation(initial_cartesian)
bc_transformed = CustomSphericBodycentric.from_representation(initial_cartesian)
assert_quantity_allclose(gd_transformed.lon, bc_transformed.lon)
assert_quantity_allclose(gd_transformed.lat, bc_transformed.lat)
assert_quantity_allclose(gd_transformed.height, bc_transformed.height)
@pytest.mark.parametrize(
"geodeticrepresentation",
[
CustomGeodetic,
WGS84GeodeticRepresentation,
IAUMARS2000GeodeticRepresentation,
IAUMARS2000BodycentricRepresentation,
],
)
def test_cartesian_geodetic_roundtrip(geodeticrepresentation):
# Test array-valued input in the process.
initial_cartesian = CartesianRepresentation(
x=[1, 3000.0] * u.km, y=[7000.0, 4.0] * u.km, z=[5.0, 6000.0] * u.km
)
transformed = geodeticrepresentation.from_representation(initial_cartesian)
roundtripped = CartesianRepresentation.from_representation(transformed)
assert_quantity_allclose(initial_cartesian.x, roundtripped.x)
assert_quantity_allclose(initial_cartesian.y, roundtripped.y)
assert_quantity_allclose(initial_cartesian.z, roundtripped.z)
@pytest.mark.parametrize(
"geodeticrepresentation",
[
CustomGeodetic,
WGS84GeodeticRepresentation,
IAUMARS2000GeodeticRepresentation,
IAUMARS2000BodycentricRepresentation,
],
)
def test_geodetic_cartesian_roundtrip(geodeticrepresentation):
initial_geodetic = geodeticrepresentation(
lon=[0.8, 1.3] * u.radian,
lat=[0.3, 0.98] * u.radian,
height=[100.0, 367.0] * u.m,
)
transformed = CartesianRepresentation.from_representation(initial_geodetic)
roundtripped = geodeticrepresentation.from_representation(transformed)
assert_quantity_allclose(initial_geodetic.lon, roundtripped.lon)
assert_quantity_allclose(initial_geodetic.lat, roundtripped.lat)
assert_quantity_allclose(initial_geodetic.height, roundtripped.height)
def test_geocentric_to_geodetic():
"""Test that we reproduce erfa/src/t_erfa_c.c t_gc2gd"""
# Here, test the chain. Direct conversion from Cartesian to
# various Geodetic representations is done indirectly in test_earth.
x, y, z = (2e6, 3e6, 5.244e6)
status = 0 # help for copy & paste of vvd
gc = CartesianRepresentation(x, y, z, u.m)
gd = WGS84GeodeticRepresentation.from_cartesian(gc)
e, p, h = gd.lon.to(u.radian), gd.lat.to(u.radian), gd.height.to(u.m)
vvd(e, 0.9827937232473290680, 1e-14, "eraGc2gd", "e1", status)
vvd(p, 0.97160184819075459, 1e-14, "eraGc2gd", "p1", status)
vvd(h, 331.4172461426059892, 1e-8, "eraGc2gd", "h1", status)
gd = gd.represent_as(GRS80GeodeticRepresentation)
e, p, h = gd.lon.to(u.radian), gd.lat.to(u.radian), gd.height.to(u.m)
vvd(e, 0.98279372324732907, 1e-14, "eraGc2gd", "e2", status)
vvd(p, 0.97160184820607853, 1e-14, "eraGc2gd", "p2", status)
vvd(h, 331.41731754844348, 1e-8, "eraGc2gd", "h2", status)
gd = gd.represent_as(WGS72GeodeticRepresentation)
e, p, h = gd.lon.to(u.radian), gd.lat.to(u.radian), gd.height.to(u.m)
vvd(e, 0.98279372324732907, 1e-14, "eraGc2gd", "e3", status)
vvd(p, 0.97160181811015119, 1e-14, "eraGc2gd", "p3", status)
vvd(h, 333.27707261303181, 1e-8, "eraGc2gd", "h3", status)
def test_geodetic_to_geocentric():
"""Test that we reproduce erfa/src/t_erfa_c.c t_gd2gc"""
# These tests are also done implicitly in test_earth.py.
e = 3.1 * u.rad
p = -0.5 * u.rad
h = 2500.0 * u.m
status = 0 # help for copy & paste of vvd
gd = WGS84GeodeticRepresentation(e, p, h)
xyz = gd.to_cartesian().get_xyz()
vvd(xyz[0], -5599000.5577049947, 1e-7, "eraGd2gc", "0/1", status)
vvd(xyz[1], 233011.67223479203, 1e-7, "eraGd2gc", "1/1", status)
vvd(xyz[2], -3040909.4706983363, 1e-7, "eraGd2gc", "2/1", status)
gd = GRS80GeodeticRepresentation(e, p, h)
xyz = gd.to_cartesian().get_xyz()
vvd(xyz[0], -5599000.5577260984, 1e-7, "eraGd2gc", "0/2", status)
vvd(xyz[1], 233011.6722356703, 1e-7, "eraGd2gc", "1/2", status)
vvd(xyz[2], -3040909.4706095476, 1e-7, "eraGd2gc", "2/2", status)
gd = WGS72GeodeticRepresentation(e, p, h)
xyz = gd.to_cartesian().get_xyz()
vvd(xyz[0], -5598998.7626301490, 1e-7, "eraGd2gc", "0/3", status)
vvd(xyz[1], 233011.5975297822, 1e-7, "eraGd2gc", "1/3", status)
vvd(xyz[2], -3040908.6861467111, 1e-7, "eraGd2gc", "2/3", status)
@pytest.mark.parametrize(
"representation",
[
WGS84GeodeticRepresentation,
IAUMARS2000BodycentricRepresentation,
],
)
def test_default_height_is_zero(representation):
gd = representation(10 * u.deg, 20 * u.deg)
assert gd.lon == 10 * u.deg
assert gd.lat == 20 * u.deg
assert gd.height == 0 * u.m
@pytest.mark.parametrize(
"representation",
[
WGS84GeodeticRepresentation,
IAUMARS2000BodycentricRepresentation,
],
)
def test_non_angle_error(representation):
with pytest.raises(u.UnitTypeError, match="require units equivalent to 'rad'"):
representation(20 * u.m, 20 * u.deg, 20 * u.m)
@pytest.mark.parametrize(
"representation",
[
WGS84GeodeticRepresentation,
IAUMARS2000BodycentricRepresentation,
],
)
def test_non_length_error(representation):
with pytest.raises(u.UnitTypeError, match="units of length"):
representation(10 * u.deg, 20 * u.deg, 30)
def test_subclass_bad_ellipsoid():
# Test incomplete initialization.
msg = "module 'erfa' has no attribute 'foo'"
with pytest.raises(AttributeError, match=msg):
class InvalidCustomEllipsoid(BaseGeodeticRepresentation):
_ellipsoid = "foo"
assert "foo" not in ELLIPSOIDS
assert "invalidcustomellipsoid" not in REPRESENTATION_CLASSES
@pytest.mark.parametrize(
"baserepresentation",
[
BaseGeodeticRepresentation,
BaseBodycentricRepresentation,
],
)
def test_geodetic_subclass_missing_equatorial_radius(baserepresentation):
msg = "'_equatorial_radius' and '_flattening'."
with pytest.raises(AttributeError, match=msg):
class MissingCustomAttribute(baserepresentation):
_flattening = 0.075 * u.dimensionless_unscaled
assert "missingcustomattribute" not in REPRESENTATION_CLASSES