celestial-time 0.1.1-alpha.2

Pure Rust astronomical time scales
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use super::angle::SiderealAngle;
use super::gast::GAST;
use super::gmst::GMST;
use super::lmst::LMST;
use crate::scales::{TT, UT1};
use crate::transforms::nutation::NutationCalculator;
use crate::TimeResult;
use celestial_core::angle::wrap_0_2pi;
use celestial_core::Location;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LAST {
    angle: SiderealAngle,
    location: Location,
}

impl LAST {
    pub fn from_ut1_tt_and_location(ut1: &UT1, tt: &TT, location: &Location) -> TimeResult<Self> {
        let gast = GAST::from_ut1_and_tt(ut1, tt)?;

        let last_rad = gast.radians() + location.longitude;

        let last_normalized = wrap_0_2pi(last_rad);

        let angle = SiderealAngle::from_radians_exact(last_normalized);

        Ok(Self {
            angle,
            location: *location,
        })
    }

    pub fn from_lmst_and_equation_of_equinoxes(
        ut1: &UT1,
        tt: &TT,
        location: &Location,
    ) -> TimeResult<Self> {
        Self::from_ut1_tt_and_location(ut1, tt, location)
    }

    pub fn from_hours(hours: f64, location: &Location) -> Self {
        Self {
            angle: SiderealAngle::from_hours(hours),
            location: *location,
        }
    }

    pub fn from_degrees(degrees: f64, location: &Location) -> Self {
        Self {
            angle: SiderealAngle::from_degrees(degrees),
            location: *location,
        }
    }

    pub fn from_radians(radians: f64, location: &Location) -> Self {
        Self {
            angle: SiderealAngle::from_radians(radians),
            location: *location,
        }
    }

    pub fn j2000(location: &Location) -> TimeResult<Self> {
        let ut1 = UT1::j2000();
        let tt = TT::j2000();
        Self::from_ut1_tt_and_location(&ut1, &tt, location)
    }

    pub fn angle(&self) -> SiderealAngle {
        self.angle
    }

    pub fn location(&self) -> Location {
        self.location
    }

    pub fn hours(&self) -> f64 {
        self.angle.hours()
    }

    pub fn degrees(&self) -> f64 {
        self.angle.degrees()
    }

    pub fn radians(&self) -> f64 {
        self.angle.radians()
    }

    pub fn hour_angle_to_target(&self, target_ra_hours: f64) -> f64 {
        self.angle.hour_angle_to_target(target_ra_hours)
    }

    pub fn to_gast(&self) -> GAST {
        let longitude_hours = self.location.longitude * 12.0 / celestial_core::constants::PI;

        let gast_hours = self.hours() - longitude_hours;

        GAST::from_hours(gast_hours)
    }

    pub fn to_lmst(&self, tt: &TT) -> TimeResult<LMST> {
        let nutation = tt.nutation_iau2006a()?;

        let jd = tt.to_julian_date();
        let mean_obliquity = celestial_core::obliquity::iau_2006_mean_obliquity(jd.jd1(), jd.jd2());

        let ee_rad = nutation.nutation_longitude() * libm::cos(mean_obliquity);
        let ee_hours = ee_rad * 12.0 / celestial_core::constants::PI;

        let lmst_hours = self.hours() - ee_hours;

        Ok(LMST::from_hours(lmst_hours, &self.location))
    }

    pub fn to_gmst(&self, tt: &TT) -> TimeResult<GMST> {
        let lmst = self.to_lmst(tt)?;

        Ok(lmst.to_gmst())
    }
}

impl std::fmt::Display for LAST {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let lat_deg = self.location.latitude * celestial_core::constants::RAD_TO_DEG;
        let lon_deg = self.location.longitude * celestial_core::constants::RAD_TO_DEG;
        write!(
            f,
            "LAST {} at ({:.4}°, {:.4}°)",
            self.angle, lat_deg, lon_deg
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn mauna_kea() -> Location {
        Location::from_degrees(19.8283, -155.4783, 4145.0).unwrap()
    }

    fn greenwich() -> Location {
        Location::greenwich()
    }

    #[test]
    fn test_last_at_greenwich_equals_gast() {
        // At Greenwich (0° longitude), LAST should equal GAST
        let ut1 = UT1::j2000();
        let tt = TT::j2000();
        let location = greenwich();

        let gast = GAST::from_ut1_and_tt(&ut1, &tt).unwrap();
        let last = LAST::from_ut1_tt_and_location(&ut1, &tt, &location).unwrap();

        // Should be identical (within numerical precision)
        assert!(
            (last.hours() - gast.hours()).abs() < 1e-14,
            "LAST at Greenwich should equal GAST: LAST={}, GAST={}",
            last.hours(),
            gast.hours()
        );
    }

    #[test]
    fn test_last_method_consistency() {
        // Both calculation methods should give identical results
        let ut1 = UT1::j2000();
        let tt = TT::j2000();
        let location = mauna_kea();

        let last_method1 = LAST::from_ut1_tt_and_location(&ut1, &tt, &location).unwrap();
        let last_method2 = LAST::from_lmst_and_equation_of_equinoxes(&ut1, &tt, &location).unwrap();

        // Both methods should produce identical results within numerical precision
        assert!(
            (last_method1.hours() - last_method2.hours()).abs() < 1e-14,
            "LAST calculation methods should match: method1={}, method2={}",
            last_method1.hours(),
            last_method2.hours()
        );
    }

    #[test]
    fn test_last_longitude_correction() {
        // Test longitude correction: 1 degree = 4 minutes = 1/15 hour
        let ut1 = UT1::j2000();
        let tt = TT::j2000();

        let greenwich_loc = greenwich();
        let east_15deg = Location::from_degrees(0.0, 15.0, 0.0).unwrap(); // 15°E = +1 hour
        let west_15deg = Location::from_degrees(0.0, -15.0, 0.0).unwrap(); // 15°W = -1 hour

        let last_greenwich = LAST::from_ut1_tt_and_location(&ut1, &tt, &greenwich_loc).unwrap();
        let last_east = LAST::from_ut1_tt_and_location(&ut1, &tt, &east_15deg).unwrap();
        let last_west = LAST::from_ut1_tt_and_location(&ut1, &tt, &west_15deg).unwrap();

        // 15°E should be +1 hour ahead of Greenwich
        let diff_east = last_east.hours() - last_greenwich.hours();
        assert!(
            (diff_east - 1.0).abs() < 1e-12,
            "15°E should be +1 hour: {}",
            diff_east
        );

        // 15°W should be -1 hour behind Greenwich
        let diff_west = last_west.hours() - last_greenwich.hours();
        assert!(
            (diff_west + 1.0).abs() < 1e-12,
            "15°W should be -1 hour: {}",
            diff_west
        );
    }

    #[test]
    fn test_last_vs_gast_longitude() {
        // LAST = GAST + longitude correction
        let ut1 = UT1::j2000();
        let tt = TT::j2000();
        let location = mauna_kea();

        let last = LAST::from_ut1_tt_and_location(&ut1, &tt, &location).unwrap();
        let gast = GAST::from_ut1_and_tt(&ut1, &tt).unwrap();

        // Calculate longitude correction manually
        let longitude_hours = location.longitude * 12.0 / celestial_core::constants::PI;

        // LAST should equal GAST + longitude correction
        let expected_last = gast.hours() + longitude_hours;
        let expected_last_normalized = ((expected_last % 24.0) + 24.0) % 24.0;

        assert!(
            (last.hours() - expected_last_normalized).abs() < 1e-14,
            "LAST = GAST + longitude: LAST={}, GAST={}, longitude={}, expected={}",
            last.hours(),
            gast.hours(),
            longitude_hours,
            expected_last_normalized
        );
    }

    #[test]
    fn test_last_mauna_kea() {
        // Mauna Kea is at -155.4783° = -10.365 hours west of Greenwich
        let ut1 = UT1::j2000();
        let tt = TT::j2000();
        let location = mauna_kea();

        let gast = GAST::from_ut1_and_tt(&ut1, &tt).unwrap();
        let last = LAST::from_ut1_tt_and_location(&ut1, &tt, &location).unwrap();

        // Expected longitude correction in hours
        let expected_offset = -155.4783 / 15.0; // degrees to hours
        let actual_offset = last.hours() - gast.hours();

        assert!(
            (actual_offset - expected_offset).abs() < 1e-10,
            "Mauna Kea LAST offset incorrect: expected={}, actual={}",
            expected_offset,
            actual_offset
        );
    }

    #[test]
    fn test_last_j2000() {
        let location = mauna_kea();
        let last = LAST::j2000(&location).unwrap();

        // LAST should be in valid range
        let hours = last.hours();
        assert!(
            (0.0..24.0).contains(&hours),
            "LAST should be in [0, 24) hours: {}",
            hours
        );
    }

    #[test]
    fn test_last_hour_angle_calculation() {
        let location = mauna_kea();
        let last = LAST::from_hours(12.0, &location);
        let target_ra = 6.0;
        let hour_angle = last.hour_angle_to_target(target_ra);
        assert_eq!(hour_angle, 6.0);
    }

    #[test]
    fn test_last_to_gast_roundtrip() {
        let location = mauna_kea();
        let original_gast = GAST::from_hours(15.5);

        // Convert GAST -> LAST -> GAST
        let longitude_hours = location.longitude * 12.0 / celestial_core::constants::PI;
        let last_hours = original_gast.hours() + longitude_hours;
        let last = LAST::from_hours(last_hours, &location);
        let recovered_gast = last.to_gast();

        assert!(
            (recovered_gast.hours() - original_gast.hours()).abs() < 1e-14,
            "GAST->LAST->GAST roundtrip failed: original={}, recovered={}",
            original_gast.hours(),
            recovered_gast.hours()
        );
    }

    #[test]
    fn test_last_to_lmst_conversion() {
        let ut1 = UT1::j2000();
        let tt = TT::j2000();
        let location = mauna_kea();

        let original_lmst = LMST::from_ut1_tt_and_location(&ut1, &tt, &location).unwrap();

        // Convert LMST -> LAST -> LMST
        let last = LAST::from_lmst_and_equation_of_equinoxes(&ut1, &tt, &location).unwrap();
        let recovered_lmst = last.to_lmst(&tt).unwrap();

        // Note: Small precision difference expected due to CIO-based LAST vs classical LMST
        // Roundtrip precision limited by algorithm difference
        assert!(
            (recovered_lmst.hours() - original_lmst.hours()).abs() < 1e-7,
            "LMST->LAST->LMST roundtrip failed: original={}, recovered={}",
            original_lmst.hours(),
            recovered_lmst.hours()
        );
    }

    #[test]
    fn test_last_from_constructors() {
        let location = mauna_kea();

        // Test all constructor methods produce equivalent results
        let hours = 14.5;
        let degrees = hours * 15.0;
        let radians = hours * celestial_core::constants::PI / 12.0;

        let last_hours = LAST::from_hours(hours, &location);
        let last_degrees = LAST::from_degrees(degrees, &location);
        let last_radians = LAST::from_radians(radians, &location);

        assert!((last_hours.hours() - last_degrees.hours()).abs() < 1e-14);
        assert!((last_hours.hours() - last_radians.hours()).abs() < 1e-14);
        assert_eq!(last_hours.location(), location);
        assert_eq!(last_degrees.location(), location);
        assert_eq!(last_radians.location(), location);
    }

    #[test]
    fn test_last_display() {
        let location = mauna_kea();
        let last = LAST::from_hours(12.5, &location);
        let display = format!("{}", last);

        // Should include LAST time and location coordinates
        assert!(display.contains("LAST"));
        assert!(display.contains("19.8283")); // Latitude
        assert!(display.contains("-155.4783")); // Longitude
    }

    #[test]
    fn test_last_location_enforcement() {
        // Test that LAST enforces location through type system
        let location = mauna_kea();
        let last = LAST::from_hours(12.0, &location);

        // Location is always available and cannot be None/invalid
        let stored_location = last.location();
        assert_eq!(stored_location.latitude, location.latitude);
        assert_eq!(stored_location.longitude, location.longitude);
        assert_eq!(stored_location.height, location.height);
    }

    #[test]
    fn test_extreme_longitudes() {
        let ut1 = UT1::j2000();
        let tt = TT::j2000();

        // Test extreme valid longitudes
        let east_extreme = Location::from_degrees(0.0, 180.0, 0.0).unwrap(); // 180°E = +12 hours
        let west_extreme = Location::from_degrees(0.0, -180.0, 0.0).unwrap(); // 180°W = -12 hours

        let gast = GAST::from_ut1_and_tt(&ut1, &tt).unwrap();
        let last_east = LAST::from_ut1_tt_and_location(&ut1, &tt, &east_extreme).unwrap();
        let last_west = LAST::from_ut1_tt_and_location(&ut1, &tt, &west_extreme).unwrap();

        // 180°E should be +12 hours ahead
        let diff_east = last_east.hours() - gast.hours();
        let expected_east = if diff_east < 0.0 {
            diff_east + 24.0
        } else {
            diff_east
        };
        assert!(
            (expected_east - 12.0).abs() < 1e-12,
            "180°E should be +12 hours"
        );

        // 180°W should be -12 hours behind (equivalent to +12 hours due to 24h wrap)
        let diff_west = last_west.hours() - gast.hours();
        let expected_west = if diff_west > 12.0 {
            diff_west - 24.0
        } else {
            diff_west
        };
        assert!(
            (expected_west + 12.0).abs() < 1e-12,
            "180°W should be -12 hours"
        );
    }

    #[test]
    fn test_last_equation_of_equinoxes_range() {
        // Test that equation of equinoxes is reasonable (should be small)
        let ut1 = UT1::j2000();
        let tt = TT::j2000();
        let location = mauna_kea();

        let last = LAST::from_ut1_tt_and_location(&ut1, &tt, &location).unwrap();
        let lmst = LMST::from_ut1_tt_and_location(&ut1, &tt, &location).unwrap();

        // Equation of equinoxes should be small (typically < 1 second = 1/3600 hours)
        let ee_hours = last.hours() - lmst.hours();
        let ee_hours_normalized = if ee_hours > 12.0 {
            ee_hours - 24.0
        } else if ee_hours < -12.0 {
            ee_hours + 24.0
        } else {
            ee_hours
        };

        assert!(
            ee_hours_normalized.abs() < 0.001, // Less than 3.6 seconds
            "Equation of equinoxes too large: {} hours = {} seconds",
            ee_hours_normalized,
            ee_hours_normalized * 3600.0
        );
    }

    #[test]
    fn test_last_constructors_and_accessors() {
        let location = greenwich();

        // Test from_degrees constructor
        let last_deg = LAST::from_degrees(45.0, &location);
        assert_eq!(last_deg.degrees(), 45.0);
        assert_eq!(last_deg.hours(), 3.0);

        // Test from_radians constructor
        let last_rad = LAST::from_radians(celestial_core::constants::PI / 4.0, &location);
        assert!((last_rad.radians() - celestial_core::constants::PI / 4.0).abs() < 1e-15);
        assert_eq!(last_rad.hours(), 3.0);

        // Test angle() accessor
        let angle = last_deg.angle();
        assert_eq!(angle.degrees(), 45.0);

        // Test location() accessor
        let stored_location = last_deg.location();
        assert_eq!(stored_location.latitude, location.latitude);
        assert_eq!(stored_location.longitude, location.longitude);
        assert_eq!(stored_location.height, location.height);

        // Test degrees() method
        let degrees = last_deg.degrees();
        assert_eq!(degrees, 45.0);
    }
}