siderust-py 0.2.2

Python bindings for siderust astrometry and astrodynamics
Documentation
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
"""Tests for core siderust API surface."""

import pytest

import siderust
from siderust import (
    Body,
    CrossingDirection,
    CrossingEvent,
    CulminationEvent,
    CulminationKind,
    Direction,
    MoonPhaseGeometry,
    MoonPhaseLabel,
    Observer,
    PhaseEvent,
    PhaseKind,
    Star,
    above_threshold,
    altitude_at,
    azimuth_at,
    below_threshold,
    crossings,
    culminations,
    find_moon_phases,
    moon_phase,
)


class TestModuleExports:
    """Verify all expected names are importable."""

    def test_version_exists(self):
        assert hasattr(siderust, "__version__")
        assert isinstance(siderust.__version__, str)

    def test_all_exports(self):
        expected = {
            "Observer",
            "Body",
            "Star",
            "Direction",
            "CrossingEvent",
            "CulminationEvent",
            "CrossingDirection",
            "CulminationKind",
            "MoonPhaseGeometry",
            "MoonPhaseLabel",
            "PhaseEvent",
            "PhaseKind",
            "altitude_at",
            "above_threshold",
            "below_threshold",
            "crossings",
            "culminations",
            "azimuth_at",
            "moon_phase",
            "find_moon_phases",
            "__version__",
        }
        for name in expected:
            assert hasattr(siderust, name), f"Missing export: {name}"


class TestObserver:
    """Observer creation and properties."""

    def test_custom_observer(self):
        obs = Observer(-17.8925, 28.7543, 2396.0)
        assert abs(obs.lon_deg - (-17.8925)) < 1e-4
        assert abs(obs.lat_deg - 28.7543) < 1e-4
        assert abs(obs.height_m - 2396.0) < 0.1

    def test_default_height(self):
        obs = Observer(0.0, 0.0)
        assert abs(obs.height_m) < 1e-6

    @pytest.mark.parametrize("field", ["lon_deg", "lat_deg", "height_m"])
    @pytest.mark.parametrize("value", [float("nan"), float("inf"), float("-inf")])
    def test_non_finite_values_are_rejected(self, field, value):
        parts = {"lon_deg": 0.0, "lat_deg": 0.0, "height_m": 0.0}
        parts[field] = value
        with pytest.raises(ValueError, match=rf"{field} must be finite"):
            Observer(**parts)

    def test_roque(self):
        obs = Observer.roque_de_los_muchachos()
        assert abs(obs.lat_deg - 28.7543) < 0.01

    def test_paranal(self):
        obs = Observer.el_paranal()
        assert obs.lat_deg < 0  # Southern hemisphere

    def test_mauna_kea(self):
        obs = Observer.mauna_kea()
        assert obs.height_m > 4000

    def test_la_silla(self):
        obs = Observer.la_silla()
        assert obs.lon_deg < 0  # Western hemisphere

    def test_repr(self):
        obs = Observer(0.0, 45.0, 100.0)
        r = repr(obs)
        assert "Observer" in r
        assert "45" in r

    def test_equality(self):
        a = Observer.roque_de_los_muchachos()
        b = Observer.roque_de_los_muchachos()
        assert a == b

    def test_hash(self):
        a = Observer.roque_de_los_muchachos()
        b = Observer.roque_de_los_muchachos()
        assert hash(a) == hash(b)


class TestBody:
    """Body enum and methods."""

    def test_all_bodies_exist(self):
        bodies = [
            Body.Sun,
            Body.Moon,
            Body.Mercury,
            Body.Venus,
            Body.Mars,
            Body.Jupiter,
            Body.Saturn,
            Body.Uranus,
            Body.Neptune,
        ]
        assert len(bodies) == 9

    def test_repr(self):
        assert "Sun" in repr(Body.Sun)

    def test_str(self):
        assert str(Body.Mars) == "Mars"

    def test_equality(self):
        assert Body.Sun == Body.Sun
        assert Body.Sun != Body.Moon

    def test_hash(self):
        assert hash(Body.Sun) == hash(Body.Sun)
        s = {Body.Sun, Body.Moon, Body.Sun}
        assert len(s) == 2

    def test_altitude_at(self):
        obs = Observer.roque_de_los_muchachos()
        alt = Body.Sun.altitude_at(obs, 60000.0)
        assert isinstance(alt, float)
        assert -90.0 <= alt <= 90.0

    def test_azimuth_at(self):
        obs = Observer.roque_de_los_muchachos()
        az = Body.Sun.azimuth_at(obs, 60000.0)
        assert isinstance(az, float)


class TestStar:
    """Star type tests."""

    def test_catalog_vega(self):
        vega = Star.catalog("Vega")
        assert vega.name == "Vega"
        assert vega.distance_ly > 0

    def test_catalog_case_insensitive(self):
        s1 = Star.catalog("sirius")
        s2 = Star.catalog("SIRIUS")
        assert s1.name == s2.name

    def test_catalog_unknown(self):
        with pytest.raises(ValueError, match="Unknown star"):
            Star.catalog("Nonexistent")

    def test_from_ra_dec(self):
        s = Star.from_ra_dec("TestStar", 100.0, 30.0)
        assert s.name == "TestStar"
        assert abs(s.ra_deg - 100.0) < 0.01
        assert abs(s.dec_deg - 30.0) < 0.01

    def test_properties(self):
        vega = Star.catalog("Vega")
        assert vega.mass_solar > 0
        assert vega.luminosity_solar > 0

    def test_altitude_at(self):
        obs = Observer.roque_de_los_muchachos()
        vega = Star.catalog("Vega")
        alt = vega.altitude_at(obs, 60000.0)
        assert isinstance(alt, float)
        assert -90.0 <= alt <= 90.0

    def test_repr(self):
        vega = Star.catalog("Vega")
        assert "Vega" in repr(vega)

    def test_equality(self):
        a = Star.catalog("Vega")
        b = Star.catalog("Vega")
        assert a == b


class TestDirection:
    """Direction (ICRS) type tests."""

    def test_creation(self):
        d = Direction(ra_deg=180.0, dec_deg=45.0)
        assert abs(d.ra_deg - 180.0) < 0.01
        assert abs(d.dec_deg - 45.0) < 0.01

    @pytest.mark.parametrize("field", ["ra_deg", "dec_deg"])
    @pytest.mark.parametrize("value", [float("nan"), float("inf"), float("-inf")])
    def test_non_finite_values_are_rejected(self, field, value):
        parts = {"ra_deg": 0.0, "dec_deg": 0.0}
        parts[field] = value
        with pytest.raises(ValueError, match=rf"{field} must be finite"):
            Direction(**parts)

    def test_altitude_at(self):
        obs = Observer.roque_de_los_muchachos()
        d = Direction(ra_deg=0.0, dec_deg=90.0)  # North celestial pole
        alt = d.altitude_at(obs, 60000.0)
        # NCP altitude ≈ observer latitude
        assert abs(alt - obs.lat_deg) < 1.0

    def test_repr(self):
        d = Direction(ra_deg=10.0, dec_deg=20.0)
        r = repr(d)
        assert "Direction" in r
        assert "10" in r

    def test_equality(self):
        a = Direction(ra_deg=10.0, dec_deg=20.0)
        b = Direction(ra_deg=10.0, dec_deg=20.0)
        assert a == b

    def test_hash(self):
        a = Direction(ra_deg=10.0, dec_deg=20.0)
        b = Direction(ra_deg=10.0, dec_deg=20.0)
        assert hash(a) == hash(b)

    def test_pickle(self):
        import pickle

        d = Direction(ra_deg=100.0, dec_deg=-30.0)
        d2 = pickle.loads(pickle.dumps(d))
        assert abs(d.ra_deg - d2.ra_deg) < 1e-10
        assert abs(d.dec_deg - d2.dec_deg) < 1e-10


class TestAltitudeQueries:
    """Free-function altitude/azimuth queries."""

    def test_altitude_at_body(self):
        obs = Observer.roque_de_los_muchachos()
        alt = altitude_at(Body.Sun, obs, 60000.0)
        assert isinstance(alt, float)
        assert -90.0 <= alt <= 90.0

    def test_altitude_at_star(self):
        obs = Observer.roque_de_los_muchachos()
        vega = Star.catalog("Vega")
        alt = altitude_at(vega, obs, 60000.0)
        assert isinstance(alt, float)

    def test_altitude_at_direction(self):
        obs = Observer.roque_de_los_muchachos()
        d = Direction(ra_deg=0.0, dec_deg=45.0)
        alt = altitude_at(d, obs, 60000.0)
        assert isinstance(alt, float)

    def test_azimuth_at(self):
        obs = Observer.roque_de_los_muchachos()
        az = azimuth_at(Body.Moon, obs, 60000.0)
        assert isinstance(az, float)

    def test_invalid_target_type(self):
        obs = Observer.roque_de_los_muchachos()
        with pytest.raises(TypeError, match="target must be"):
            altitude_at("not a target", obs, 60000.0)


class TestThresholdQueries:
    """above_threshold, below_threshold over a 1-day window."""

    def test_above_threshold_sun(self):
        obs = Observer.roque_de_los_muchachos()
        periods = above_threshold(Body.Sun, obs, 60000.0, 60001.0, 0.0)
        assert isinstance(periods, list)
        for start, end in periods:
            assert start < end

    def test_below_threshold(self):
        obs = Observer.roque_de_los_muchachos()
        periods = below_threshold(Body.Sun, obs, 60000.0, 60001.0, 0.0)
        assert isinstance(periods, list)

    def test_invalid_window(self):
        obs = Observer.roque_de_los_muchachos()
        with pytest.raises(ValueError, match="Invalid time window"):
            above_threshold(Body.Sun, obs, 60001.0, 60000.0, 0.0)


class TestCrossingsAndCulminations:
    """Crossing and culmination event queries."""

    def test_crossings(self):
        obs = Observer.roque_de_los_muchachos()
        events = crossings(Body.Sun, obs, 60000.0, 60001.0, 0.0)
        assert isinstance(events, list)
        for e in events:
            assert isinstance(e, CrossingEvent)
            assert isinstance(e.mjd, float)
            assert isinstance(e.direction, CrossingDirection)

    def test_crossing_directions(self):
        assert CrossingDirection.Rising != CrossingDirection.Setting
        assert "Rising" in repr(CrossingDirection.Rising)

    def test_culminations(self):
        obs = Observer.roque_de_los_muchachos()
        events = culminations(Body.Sun, obs, 60000.0, 60001.0)
        assert isinstance(events, list)
        for e in events:
            assert isinstance(e, CulminationEvent)
            assert isinstance(e.mjd, float)
            assert isinstance(e.altitude_deg, float)
            assert isinstance(e.kind, CulminationKind)

    def test_culmination_kinds(self):
        assert CulminationKind.Max != CulminationKind.Min
        assert "Max" in repr(CulminationKind.Max)


class TestMoonPhase:
    """Moon phase queries."""

    def test_moon_phase_geocentric(self):
        # J2000.0
        geom = moon_phase(2451545.0)
        assert isinstance(geom, MoonPhaseGeometry)
        assert 0.0 <= geom.illuminated_fraction <= 1.0
        assert isinstance(geom.label, MoonPhaseLabel)

    def test_moon_phase_topocentric(self):
        obs = Observer.roque_de_los_muchachos()
        geom = moon_phase(2451545.0, observer=obs)
        assert isinstance(geom, MoonPhaseGeometry)

    def test_moon_phase_repr(self):
        geom = moon_phase(2451545.0)
        r = repr(geom)
        assert "MoonPhaseGeometry" in r

    def test_find_moon_phases(self):
        events = find_moon_phases(60000.0, 60030.0)
        assert isinstance(events, list)
        assert len(events) > 0  # ~4 events per month
        for e in events:
            assert isinstance(e, PhaseEvent)
            assert isinstance(e.kind, PhaseKind)
            assert 60000.0 <= e.mjd <= 60030.0

    def test_phase_labels(self):
        labels = [
            MoonPhaseLabel.NewMoon,
            MoonPhaseLabel.WaxingCrescent,
            MoonPhaseLabel.FirstQuarter,
            MoonPhaseLabel.WaxingGibbous,
            MoonPhaseLabel.FullMoon,
            MoonPhaseLabel.WaningGibbous,
            MoonPhaseLabel.LastQuarter,
            MoonPhaseLabel.WaningCrescent,
        ]
        assert len(labels) == 8

    def test_phase_kinds(self):
        kinds = [
            PhaseKind.NewMoon,
            PhaseKind.FirstQuarter,
            PhaseKind.FullMoon,
            PhaseKind.LastQuarter,
        ]
        assert len(kinds) == 4


class TestEndToEnd:
    """End-to-end scientific workflows."""

    def test_observation_planning(self):
        """Plan a night of observation at Roque de los Muchachos."""
        obs = Observer.roque_de_los_muchachos()
        mjd_start = 60000.0
        mjd_end = 60001.0

        # Find when Sun is below horizon
        night = below_threshold(Body.Sun, obs, mjd_start, mjd_end, 0.0)
        assert len(night) > 0
        night_start, night_end = night[0]

        # Check if Vega is visible during the night
        vega = Star.catalog("Vega")
        mid_night = (night_start + night_end) / 2.0
        vega_alt = vega.altitude_at(obs, mid_night)
        assert isinstance(vega_alt, float)

    def test_sunrise_sunset(self):
        """Find sunrise and sunset times."""
        obs = Observer.roque_de_los_muchachos()
        events = crossings(Body.Sun, obs, 60000.0, 60001.0, 0.0)
        rising = [e for e in events if e.direction == CrossingDirection.Rising]
        setting = [e for e in events if e.direction == CrossingDirection.Setting]
        # Should have at least one of each in a day
        assert len(rising) + len(setting) >= 1

    def test_multi_body_comparison(self):
        """Compare altitudes of multiple bodies at the same time."""
        obs = Observer.el_paranal()
        mjd = 60000.5  # Mid-day

        altitudes = {}
        for body in [Body.Sun, Body.Moon, Body.Mars, Body.Jupiter]:
            altitudes[str(body)] = altitude_at(body, obs, mjd)

        assert all(isinstance(v, float) for v in altitudes.values())

    def test_moon_phase_during_observation(self):
        """Check moon phase during an observation window."""
        geom = moon_phase(2460000.5)  # JD for a specific night
        assert 0.0 <= geom.illuminated_fraction <= 1.0
        # Verify we get a sensible description
        s = str(geom)
        assert "illuminated" in s