efb 0.7.1

Electronic Flight Bag library to plan and conduct a flight.
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Joe Pearson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Climb and descent performance modelling.

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

use crate::measurements::{Altitude, Duration, Length, Pressure, Speed, VerticalRate, Volume};
use crate::{Fuel, FuelFlow, FuelType, VerticalDistance};

/// One row of a climb or descent performance table.
///
/// Each row describes the aircraft performance within the altitude band that
/// ends at [level]. The band starts at the previous row's level, or at
/// ground for the first row.
///
/// In a climb table, the [vertical rate] is the rate of climb; in a descent table
/// it is the rate of descent. The value is always positive — direction is
/// implied by the context in which the table is used.
///
/// [level]: ClimbDescentBand::level
/// [vertical rate]: ClimbDescentBand::vertical_rate
///
/// # Example
///
/// ```
/// use efb::prelude::*;
///
/// let row = ClimbDescentBand {
///     level: VerticalDistance::Altitude(5_000),
///     tas: Speed::kt(85.0),
///     vertical_rate: VerticalRate::fpm(650.0),
///     ff: FuelFlow::PerHour(Fuel::new(Mass::kg(15.0), FuelType::AvGas)),
/// };
/// ```
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct ClimbDescentBand {
    /// Upper bound of this altitude band (e.g. `Altitude(5000)` for a band
    /// that covers everything up to 5 000 ft).
    pub level: VerticalDistance,

    /// True airspeed during this band, used to compute horizontal distance
    /// covered while climbing or descending.
    pub tas: Speed,

    /// Rate of altitude change (always positive; direction is implied by
    /// whether this is a climb or descent table).
    pub vertical_rate: VerticalRate,

    /// Fuel flow during this band.
    pub ff: FuelFlow,
}

/// One row of a cumulative "time, fuel, and distance to climb" table as found
/// in most POH / AFM documents.
///
/// Each entry represents the **cumulative** time, fuel, and distance from
/// sea level (or ground) to the given [`level`]. The first entry should be
/// the baseline (typically sea level with all values at zero).
///
/// Use [`ClimbDescentPerformance::from_cumulative`] to convert a slice of
/// these entries into a performance table.
///
/// [`level`]: CumulativeClimbDescentEntry::level
///
/// # Reading from a POH
///
/// A typical PA-28-181 Archer II POH climb table (ISA, gross weight) looks
/// like:
///
/// | Press. Alt | Time  | Fuel Used | Distance |
/// |------------|-------|-----------|----------|
/// | SL         | 0 min | 0 gal     | 0 NM     |
/// | 2 000 ft   | 4 min | 0.9 gal   | 5 NM     |
/// | 4 000 ft   | 8 min | 1.8 gal   | 11 NM    |
/// | …          | …     | …         | …        |
///
/// Each row of that table maps to one `CumulativeClimbDescentEntry`. Enter
/// the values exactly as printed — they are cumulative from sea level.
/// POH tables are indexed by pressure altitude, so use
/// [`VerticalDistance::PressureAltitude`] for the level. For the sea-level
/// baseline, either [`VerticalDistance::Gnd`] or `PressureAltitude(0)` may
/// be used.
///
/// # Example
///
/// ```
/// use efb::prelude::*;
///
/// // Sea-level baseline
/// let sl = CumulativeClimbDescentEntry {
///     level: VerticalDistance::Gnd,
///     time: Duration::m(0),
///     fuel: Volume::gal(0.0),
///     distance: Length::nm(0.0),
/// };
///
/// // At 2 000 ft pressure altitude
/// let entry = CumulativeClimbDescentEntry {
///     level: VerticalDistance::PressureAltitude(2_000),
///     time: Duration::m(4),
///     fuel: Volume::gal(0.9),
///     distance: Length::nm(5.0),
/// };
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct CumulativeClimbDescentEntry {
    /// Altitude for this row, typically
    /// [`VerticalDistance::PressureAltitude`] as printed in the POH, or
    /// [`VerticalDistance::Gnd`] for the sea-level baseline.
    pub level: VerticalDistance,

    /// Cumulative time from the baseline altitude.
    pub time: Duration,

    /// Cumulative fuel consumed from the baseline altitude.
    ///
    /// Enter the volume as printed in the POH (e.g. `Volume::gal(0.9)`).
    /// The conversion to mass is handled by [`from_cumulative`] using the
    /// supplied [`FuelType`].
    ///
    /// [`from_cumulative`]: ClimbDescentPerformance::from_cumulative
    pub fuel: Volume,

    /// Cumulative still-air distance from the baseline altitude.
    pub distance: Length,
}

/// Aircraft climb or descent performance data.
///
/// Stores a table of [`ClimbDescentBand`]s describing aircraft performance
/// across altitude bands. The same type is used for both climb and descent;
/// typically an application creates two separate instances and passes them
/// into the [`FlightPlanningBuilder`].
///
/// Construct with [`new`](Self::new), [`from_fn`](Self::from_fn), or
/// [`from_cumulative`](Self::from_cumulative), then call
/// [`between`](Self::between) to obtain a [`ClimbDescentResult`].
/// Optionally correct the horizontal distance for wind with
/// [`ClimbDescentResult::with_wind`].
///
/// [`FlightPlanningBuilder`]: crate::fp::FlightPlanningBuilder
///
/// # Examples
///
/// Climb from sea level to FL 100 with wind correction:
///
/// ```
/// use efb::prelude::*;
///
/// let ff = FuelFlow::PerHour(Fuel::new(Mass::kg(15.0), FuelType::AvGas));
///
/// let climb = ClimbDescentPerformance::new(vec![
///     ClimbDescentBand {
///         level: VerticalDistance::Altitude(5_000),
///         tas: Speed::kt(80.0),
///         vertical_rate: VerticalRate::fpm(700.0),
///         ff,
///     },
///     ClimbDescentBand {
///         level: VerticalDistance::Altitude(10_000),
///         tas: Speed::kt(90.0),
///         vertical_rate: VerticalRate::fpm(500.0),
///         ff,
///     },
/// ]);
///
/// let result = climb
///     .between(&VerticalDistance::Gnd, &VerticalDistance::Fl(100))
///     .expect("valid altitude range");
///
/// // Apply 15 kt headwind to get ground distance
/// let corrected = result.with_wind(Speed::kt(15.0));
/// ```
///
/// Descent from FL 080 to 500 ft using [`from_fn`](Self::from_fn):
///
/// ```
/// # use efb::prelude::*;
/// let ff = FuelFlow::PerHour(Fuel::new(Mass::kg(10.0), FuelType::AvGas));
///
/// let descent = ClimbDescentPerformance::from_fn(
///     |_level| (Speed::kt(100.0), VerticalRate::fpm(500.0), ff),
///     VerticalDistance::Altitude(10_000),
/// );
///
/// // Destination at 500 ft, cruise at FL 080
/// let result = descent
///     .between(&VerticalDistance::Altitude(500), &VerticalDistance::Fl(80))
///     .unwrap();
/// ```
#[derive(Clone, PartialEq, Debug, Default)]
pub struct ClimbDescentPerformance {
    table: Vec<ClimbDescentBand>,
}

/// The result of a [`ClimbDescentPerformance::between`] call.
///
/// Contains the aggregated time, fuel, and horizontal distance for the
/// altitude change. The horizontal distance is initially based on TAS alone;
/// call [`with_wind`] to obtain a wind-corrected copy.
///
/// [`with_wind`]: ClimbDescentResult::with_wind
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct ClimbDescentResult {
    /// Total time spent climbing or descending.
    pub time: Duration,

    /// Total fuel burned during the climb or descent.
    pub fuel: Fuel,

    /// Horizontal distance covered during the climb or descent.
    ///
    /// Initially computed from TAS (still-air distance). After calling
    /// [`with_wind`], this is the wind-corrected ground distance.
    ///
    /// [`with_wind`]: ClimbDescentResult::with_wind
    pub horizontal_distance: Length,
}

impl ClimbDescentResult {
    /// Returns the result with wind corrected [horizontal distance].
    ///
    /// The ground distance is computed as `(TAS − headwind) × time`. If the
    /// headwind exceeds TAS (extreme case), the distance is clamped to zero.
    ///
    /// Headwind is the headwind component on the leg:
    /// - **positive** = headwind (reduces ground speed),
    /// - **negative** = tailwind (increases ground speed).
    ///
    /// [horizontal distance]: ClimbDescentResult::horizontal_distance
    pub fn with_wind(self, headwind: Speed) -> Self {
        // ground_distance = (TAS - headwind) * time
        //                 = TAS * time - headwind * time
        //                 = horizontal_distance - headwind * time
        let wind_correction = headwind * self.time;
        let ground_dist = self.horizontal_distance - wind_correction;
        // Clamp to zero for extreme headwind situations (headwind >= TAS)
        let ground_dist = if ground_dist < Length::nm(0.0) {
            Length::nm(0.0)
        } else {
            ground_dist
        };
        Self {
            horizontal_distance: ground_dist,
            ..self
        }
    }
}

/// Resolves a [`VerticalDistance`] to an [`Altitude`] at standard pressure and
/// sea-level elevation.
///
/// Returns `None` for [`VerticalDistance::Unlimited`].
fn to_altitude(vd: &VerticalDistance) -> Option<Altitude> {
    vd.to_msl(Pressure::STD, Length::ft(0.0))
}

impl ClimbDescentPerformance {
    /// Creates a performance table from a pre-built vector of rows.
    ///
    /// The rows **must** be sorted in ascending order by
    /// [`level`](ClimbDescentBand::level). No validation is
    /// performed; an unsorted table will produce incorrect results from
    /// [`between`](Self::between).
    pub fn new(table: Vec<ClimbDescentBand>) -> Self {
        Self { table }
    }

    /// Builds a performance table by sampling `f` in 1000 ft steps from
    /// ground up to `ceiling` (inclusive).
    ///
    /// The closure receives the [`VerticalDistance`] of each band boundary and
    /// returns `(tas, vertical_rate, fuel_flow)` for that band. The first
    /// invocation receives [GND]; subsequent ones receive
    /// 1000 ft, 2000 ft, etc.
    ///
    /// This is a convenience constructor for tables with uniform altitude
    /// spacing. For irregularly spaced bands or data taken directly from a
    /// POH, use [`new`](Self::new) instead.
    ///
    /// [GND]: `VerticalDistance::Gnd`
    pub fn from_fn<F>(f: F, ceiling: VerticalDistance) -> Self
    where
        F: Fn(&VerticalDistance) -> (Speed, VerticalRate, FuelFlow),
    {
        let mut table: Vec<ClimbDescentBand> = Vec::new();
        let mut vd = VerticalDistance::Gnd;
        let mut alt = 0u16;

        while vd <= ceiling {
            let (tas, vertical_rate, ff) = f(&vd);
            table.push(ClimbDescentBand {
                level: vd,
                tas,
                vertical_rate,
                ff,
            });

            alt += 1000;
            vd = VerticalDistance::Altitude(alt);
        }

        Self { table }
    }

    /// Builds a performance table from a cumulative "time, fuel, and distance
    /// to climb" table as found in most POH / AFM documents.
    ///
    /// The `entries` slice must contain at least two rows sorted in ascending
    /// [`level`](CumulativeClimbDescentEntry::level) order. The first entry
    /// is the baseline (typically sea level with all cumulative values at
    /// zero). Each subsequent entry is differenced against the previous one
    /// to derive the per-band rate of climb, TAS, and fuel flow that
    /// [`between`](Self::between) needs.
    ///
    /// `fuel_type` is needed to convert the POH's volumetric fuel figures
    /// into mass. Returns `None` if `entries` has fewer than two rows, any
    /// per-band Δtime is zero, or any level cannot be expressed in feet.
    ///
    /// # Derivation per band
    ///
    /// For two consecutive entries at altitudes *h₁* and *h₂*:
    ///
    /// - *Δalt* = *h₂* − *h₁* (ft)
    /// - *Δtime* = time(*h₂*) − time(*h₁*)
    /// - *Δfuel* = fuel(*h₂*) − fuel(*h₁*) (volume)
    /// - *Δdist* = dist(*h₂*) − dist(*h₁*)
    ///
    /// From these:
    ///
    /// - `vertical_rate` = Δalt / Δtime (ft/min)
    /// - `tas` = Δdist / Δtime
    /// - `ff` = Δfuel / Δtime (volume/h, converted to mass via `fuel_type`)
    ///
    /// # Examples
    ///
    /// The following shows how a PA-28-181 Archer II climb table might look like:
    ///
    /// ```
    /// use efb::prelude::*;
    ///
    /// let entries = [
    ///     CumulativeClimbDescentEntry {
    ///         level: VerticalDistance::Gnd,
    ///         time: Duration::m(0),
    ///         fuel: Volume::gal(0.0),
    ///         distance: Length::nm(0.0),
    ///     },
    ///     CumulativeClimbDescentEntry {
    ///         level: VerticalDistance::PressureAltitude(2_000),
    ///         time: Duration::m(4),
    ///         fuel: Volume::gal(0.9),
    ///         distance: Length::nm(5.0),
    ///     },
    ///     CumulativeClimbDescentEntry {
    ///         level: VerticalDistance::PressureAltitude(4_000),
    ///         time: Duration::m(8),
    ///         fuel: Volume::gal(1.8),
    ///         distance: Length::nm(11.0),
    ///     },
    ///     CumulativeClimbDescentEntry {
    ///         level: VerticalDistance::PressureAltitude(6_000),
    ///         time: Duration::m(13),
    ///         fuel: Volume::gal(2.9),
    ///         distance: Length::nm(18.0),
    ///     },
    ///     CumulativeClimbDescentEntry {
    ///         level: VerticalDistance::PressureAltitude(8_000),
    ///         time: Duration::m(18),
    ///         fuel: Volume::gal(4.1),
    ///         distance: Length::nm(27.0),
    ///     },
    /// ];
    ///
    /// let climb = ClimbDescentPerformance::from_cumulative(
    ///     &entries,
    ///     FuelType::AvGas,
    /// ).expect("valid table");
    ///
    /// // Now use it to compute a climb from field elevation to cruise
    /// let result = climb.between(
    ///     &VerticalDistance::Gnd,
    ///     &VerticalDistance::PressureAltitude(6_000),
    /// );
    /// assert!(result.is_some());
    /// ```
    pub fn from_cumulative(
        entries: &[CumulativeClimbDescentEntry],
        fuel_type: FuelType,
    ) -> Option<Self> {
        if entries.len() < 2 {
            return None;
        }

        let mut table: Vec<ClimbDescentBand> = Vec::with_capacity(entries.len() - 1);

        for pair in entries.windows(2) {
            let prev = &pair[0];
            let cur = &pair[1];

            let delta_alt = to_altitude(&cur.level)? - to_altitude(&prev.level)?;
            let delta_time = cur.time - prev.time;

            if *delta_time.value() == 0 {
                return None;
            }

            // TAS = Δdist / Δtime (Length / Duration = Speed)
            let tas = (cur.distance - prev.distance) / delta_time;

            // vertical_rate = Δalt / Δtime (Altitude / Duration = VerticalRate)
            let vertical_rate = delta_alt / delta_time;

            // fuel_flow = Δfuel / Δtime, scaled to per-hour
            let delta_fuel = cur.fuel - prev.fuel;
            let ff = Fuel::from_volume(delta_fuel, fuel_type) / delta_time;

            table.push(ClimbDescentBand {
                level: cur.level,
                tas,
                vertical_rate,
                ff,
            });
        }

        Some(Self { table })
    }

    /// Returns the performance row applicable at `level`.
    ///
    /// Uses a reverse-find to return the row with the highest level that is
    /// less than or equal to the target. Does not interpolate.
    ///
    /// # Panics
    ///
    /// Panics if the table is empty.
    fn at_level(&self, level: &VerticalDistance) -> &ClimbDescentBand {
        self.table
            .iter()
            .rfind(|row| &row.level <= level)
            .expect("climb/descent performance table must not be empty")
    }

    /// Returns the time, fuel, and horizontal distance for a climb or descent
    /// between two altitude levels.
    ///
    /// The computation walks the performance table band by band, applying the
    /// appropriate row for each altitude segment within `[from_level,
    /// to_level]`. If `to_level` exceeds the highest row in the table, the
    /// topmost row's performance is extrapolated for the remaining altitude.
    ///
    /// For **descent** planning, pass the *lower* altitude (destination
    /// elevation) as `from_level` and the *higher* altitude (cruise level) as
    /// `to_level`. The resulting fuel and time represent the descent through
    /// those bands.
    ///
    /// Returns `None` if `from_level >= to_level`, the table is empty, or
    /// either level cannot be expressed in feet (e.g. [AGL], [Unlimited]).
    ///
    /// [AGL]: `VerticalDistance::Agl`
    /// [Unlimited]: `VerticalDistance::Unlimited`
    pub fn between(
        &self,
        from_level: &VerticalDistance,
        to_level: &VerticalDistance,
    ) -> Option<ClimbDescentResult> {
        if from_level >= to_level || self.table.is_empty() {
            return None;
        }

        let from_alt = to_altitude(from_level)?;
        let to_alt = to_altitude(to_level)?;

        let mut band_floor = from_alt;
        let mut total_time = Duration::s(0);
        let mut accumulated_fuel: Option<Fuel> = None;
        let mut total_dist = Length::m(0.0);

        let mut accumulate = |row: &ClimbDescentBand, delta_alt: Altitude| {
            let time = delta_alt / row.vertical_rate;
            let fuel = row.ff * time;

            total_time = total_time + time;
            accumulated_fuel = Some(match accumulated_fuel {
                Some(f) => f + fuel,
                None => fuel,
            });
            total_dist = total_dist + row.tas * time;
        };

        // Walk the table to cover all bands within (from_alt, to_alt]
        for row in &self.table {
            let row_alt = to_altitude(&row.level)?;

            if row_alt <= band_floor {
                continue; // this band is below our starting altitude
            }

            let band_ceiling = if row_alt < to_alt { row_alt } else { to_alt };
            let delta_alt = band_ceiling - band_floor;

            accumulate(row, delta_alt);
            band_floor = band_ceiling;

            if band_floor >= to_alt {
                break;
            }
        }

        // If to_alt exceeds all table rows, apply the highest row for the tail
        if band_floor < to_alt {
            let row = self.at_level(to_level);
            let delta_alt = to_alt - band_floor;
            accumulate(row, delta_alt);
        }

        let fuel = accumulated_fuel?;

        Some(ClimbDescentResult {
            time: total_time,
            fuel,
            horizontal_distance: total_dist,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::measurements::{LengthUnit, SpeedUnit, VerticalRateUnit};
    use crate::FuelType;

    fn avgas_ff(lph: f32) -> FuelFlow {
        FuelFlow::PerHour(avgas!(Volume::l(lph)))
    }

    fn simple_table() -> ClimbDescentPerformance {
        // Two-band table: ground→2000 ft and 2000→4000 ft
        ClimbDescentPerformance::new(vec![
            ClimbDescentBand {
                level: VerticalDistance::Altitude(2000),
                tas: Speed::kt(80.0),
                vertical_rate: VerticalRate::fpm(800.0),
                ff: avgas_ff(20.0),
            },
            ClimbDescentBand {
                level: VerticalDistance::Altitude(4000),
                tas: Speed::kt(85.0),
                vertical_rate: VerticalRate::fpm(600.0),
                ff: avgas_ff(18.0),
            },
        ])
    }

    #[test]
    fn between_single_band() {
        let perf = simple_table();
        // Climb from 0 to 2000 ft: 2000 ft / 800 fpm = 2.5 min = 150 s
        let result = perf
            .between(&VerticalDistance::Gnd, &VerticalDistance::Altitude(2000))
            .expect("should produce a result");

        assert_eq!(*result.time.value(), 150, "time should be 150 s");
        // Horizontal distance: 80 kt * (2.5/60) h ≈ 3.33 NM
        let dist_nm = *result
            .horizontal_distance
            .convert_to(LengthUnit::NauticalMiles)
            .value();
        assert!(
            (dist_nm - 3.333).abs() < 0.05,
            "distance ~3.33 NM, got {dist_nm}"
        );
    }

    #[test]
    fn between_two_bands() {
        let perf = simple_table();
        // Climb from 0 to 4000 ft:
        //   Band 0→2000: 2000 / 800 = 2.5 min = 150 s
        //   Band 2000→4000: 2000 / 600 = 3.33 min = 200 s
        //   Total: 350 s
        let result = perf
            .between(&VerticalDistance::Gnd, &VerticalDistance::Altitude(4000))
            .expect("should produce a result");

        assert_eq!(*result.time.value(), 350, "time should be 350 s");
    }

    #[test]
    fn from_to_equal_returns_none() {
        let perf = simple_table();
        let result = perf.between(
            &VerticalDistance::Altitude(2000),
            &VerticalDistance::Altitude(2000),
        );
        assert!(result.is_none());
    }

    #[test]
    fn from_above_to_returns_none() {
        let perf = simple_table();
        let result = perf.between(
            &VerticalDistance::Altitude(3000),
            &VerticalDistance::Altitude(1000),
        );
        assert!(result.is_none());
    }

    #[test]
    fn with_wind_reduces_distance_for_headwind() {
        let perf = simple_table();
        let result = perf
            .between(&VerticalDistance::Gnd, &VerticalDistance::Altitude(2000))
            .unwrap();
        let no_wind_dist = *result
            .horizontal_distance
            .convert_to(LengthUnit::NauticalMiles)
            .value();
        let with_headwind = result.with_wind(Speed::kt(20.0));
        let headwind_dist = *with_headwind
            .horizontal_distance
            .convert_to(LengthUnit::NauticalMiles)
            .value();
        assert!(
            headwind_dist < no_wind_dist,
            "headwind should reduce ground distance: {headwind_dist} >= {no_wind_dist}"
        );
    }

    #[test]
    fn from_fn_builds_table() {
        let ff = avgas_ff(20.0);
        let perf = ClimbDescentPerformance::from_fn(
            |_| (Speed::kt(80.0), VerticalRate::fpm(700.0), ff),
            VerticalDistance::Altitude(4000),
        );
        // from_fn generates rows at Gnd, 1000, 2000, 3000, 4000
        assert_eq!(perf.table.len(), 5);
    }

    // --- from_cumulative tests ---

    fn pa28_cumulative_entries() -> Vec<CumulativeClimbDescentEntry> {
        // Approximate PA-28-181 Archer II climb data (ISA, gross weight)
        vec![
            CumulativeClimbDescentEntry {
                level: VerticalDistance::Gnd,
                time: Duration::m(0),
                fuel: Volume::gal(0.0),
                distance: Length::nm(0.0),
            },
            CumulativeClimbDescentEntry {
                level: VerticalDistance::PressureAltitude(2_000),
                time: Duration::m(4),
                fuel: Volume::gal(0.9),
                distance: Length::nm(5.0),
            },
            CumulativeClimbDescentEntry {
                level: VerticalDistance::PressureAltitude(4_000),
                time: Duration::m(8),
                fuel: Volume::gal(1.8),
                distance: Length::nm(11.0),
            },
            CumulativeClimbDescentEntry {
                level: VerticalDistance::PressureAltitude(6_000),
                time: Duration::m(13),
                fuel: Volume::gal(2.9),
                distance: Length::nm(18.0),
            },
            CumulativeClimbDescentEntry {
                level: VerticalDistance::PressureAltitude(8_000),
                time: Duration::m(18),
                fuel: Volume::gal(4.1),
                distance: Length::nm(27.0),
            },
        ]
    }

    #[test]
    fn from_cumulative_builds_correct_table_size() {
        let entries = pa28_cumulative_entries();
        let perf = ClimbDescentPerformance::from_cumulative(&entries, FuelType::AvGas)
            .expect("valid table");
        // 5 entries → 4 bands (windows of 2)
        assert_eq!(perf.table.len(), 4);
    }

    #[test]
    fn from_cumulative_derives_correct_vertical_rate() {
        let entries = pa28_cumulative_entries();
        let perf = ClimbDescentPerformance::from_cumulative(&entries, FuelType::AvGas).unwrap();

        // Band 0→2000: 2000 ft / 4 min = 500 fpm
        let row0 = &perf.table[0];
        let roc_fpm = *row0
            .vertical_rate
            .convert_to(VerticalRateUnit::FeetPerMinute)
            .value();
        assert!(
            (roc_fpm - 500.0).abs() < 1.0,
            "first band RoC should be ~500 fpm, got {roc_fpm}"
        );

        // Band 2000→4000: 2000 ft / 4 min = 500 fpm
        let row1 = &perf.table[1];
        let roc_fpm = *row1
            .vertical_rate
            .convert_to(VerticalRateUnit::FeetPerMinute)
            .value();
        assert!(
            (roc_fpm - 500.0).abs() < 1.0,
            "second band RoC should be ~500 fpm, got {roc_fpm}"
        );

        // Band 4000→6000: 2000 ft / 5 min = 400 fpm
        let row2 = &perf.table[2];
        let roc_fpm = *row2
            .vertical_rate
            .convert_to(VerticalRateUnit::FeetPerMinute)
            .value();
        assert!(
            (roc_fpm - 400.0).abs() < 1.0,
            "third band RoC should be ~400 fpm, got {roc_fpm}"
        );
    }

    #[test]
    fn from_cumulative_derives_correct_tas() {
        let entries = pa28_cumulative_entries();
        let perf = ClimbDescentPerformance::from_cumulative(&entries, FuelType::AvGas).unwrap();

        // Band 0→2000: 5 NM / 4 min * 60 = 75 kt
        let row0 = &perf.table[0];
        let tas_kt = *row0.tas.convert_to(SpeedUnit::Knots).value();
        assert!(
            (tas_kt - 75.0).abs() < 0.5,
            "first band TAS should be ~75 kt, got {tas_kt}"
        );

        // Band 4000→6000: 7 NM / 5 min * 60 = 84 kt
        let row2 = &perf.table[2];
        let tas_kt = *row2.tas.convert_to(SpeedUnit::Knots).value();
        assert!(
            (tas_kt - 84.0).abs() < 0.5,
            "third band TAS should be ~84 kt, got {tas_kt}"
        );
    }

    #[test]
    fn from_cumulative_matches_poh_totals() {
        let entries = pa28_cumulative_entries();
        let perf = ClimbDescentPerformance::from_cumulative(&entries, FuelType::AvGas).unwrap();

        // Compute climb from ground to 8000 ft PA
        let result = perf
            .between(
                &VerticalDistance::Gnd,
                &VerticalDistance::PressureAltitude(8_000),
            )
            .expect("should produce a result");

        // POH says 18 min total → 1080 s
        let time_s = *result.time.value();
        assert!(
            (time_s as f32 - 1080.0).abs() < 30.0,
            "total time should be ~1080 s, got {time_s}"
        );

        // POH says 27 NM total distance
        let dist_nm = *result
            .horizontal_distance
            .convert_to(LengthUnit::NauticalMiles)
            .value();
        assert!(
            (dist_nm - 27.0).abs() < 1.0,
            "total distance should be ~27 NM, got {dist_nm}"
        );
    }

    #[test]
    fn from_cumulative_partial_climb() {
        let entries = pa28_cumulative_entries();
        let perf = ClimbDescentPerformance::from_cumulative(&entries, FuelType::AvGas).unwrap();

        // Climb from ground to 4000 ft PA only (first two bands)
        let result = perf
            .between(
                &VerticalDistance::Gnd,
                &VerticalDistance::PressureAltitude(4_000),
            )
            .expect("should produce a result");

        // POH says 8 min → 480 s
        let time_s = *result.time.value();
        assert!(
            (time_s as f32 - 480.0).abs() < 10.0,
            "time to 4000 ft should be ~480 s, got {time_s}"
        );
    }

    #[test]
    fn from_cumulative_too_few_entries_returns_none() {
        let entries = [CumulativeClimbDescentEntry {
            level: VerticalDistance::Gnd,
            time: Duration::m(0),
            fuel: Volume::gal(0.0),
            distance: Length::nm(0.0),
        }];
        assert!(ClimbDescentPerformance::from_cumulative(&entries, FuelType::AvGas).is_none());
    }

    #[test]
    fn from_cumulative_empty_returns_none() {
        assert!(ClimbDescentPerformance::from_cumulative(&[], FuelType::AvGas).is_none());
    }
}