Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved methodology to compute legs #24

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions methodology.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ Source code is available at [src/icao_to_trace.rs](./src/icao_to_trace.rs).
This is performed automatically by the computer program. A leg is defined in this methodology
has a continuous sequence of ADS-B positions in time where the aircraft is flying.

The aircraft at a given segment between two ADS-B positions is considered grounded (not flying) when either:
* both positions are on the ground
* the time between these positions is > 5m and the aircraft is below 10.000 feet
The aircraft at a given segment between two ADS-B positions is considered grounded (not flying) when any of:
1. both positions are on the ground
2. the time between these positions is > 5m and any of the positions is below 10.000 feet
3. the time between these positions is > 10h

The latter condition is used to mitigate the risk that ADS-B receivers sometimes
Condition 1. is the normal case where ADS-B signal was received when the aircraft landed.
Condition 2. is used to mitigate the risk that ADS-B receivers sometimes
do not receive an aircraft's signal when the aircraft is at low altitude.
When this happens for more than 5m, we consider that the aircraft approached and landed.
Condition 3. is used to mitigate situations where the aircraft enters regions
of low ADS-B coverage (e.g. central Africa) while flying and then returns flying
(sometimes days later), which should be intepreted as the aircraft being flying for the whole
time.

Source code is available at [src/legs.rs](./src/legs.rs).

Expand Down
37 changes: 24 additions & 13 deletions src/legs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,36 @@ impl Leg {
}
}

fn grounded_heuristic(prev_position: &Position, position: &Position) -> bool {
let is_flying = matches!(
(&prev_position, &position),
(Position::Flying { .. }, Position::Flying { .. })
| (Position::Flying { .. }, Position::Grounded { .. })
| (Position::Grounded { .. }, Position::Flying { .. })
);
let lost_close_to_ground = position.datetime() - prev_position.datetime()
> time::Duration::minutes(5)
&& (position.altitude() < 10000.0 || prev_position.altitude() < 10000.0);

// lost signal for more than 10h => assume it landed somewhere
let lost_somewhere = position.datetime() - prev_position.datetime() > time::Duration::hours(10);

is_flying && (lost_close_to_ground || lost_somewhere)
}

/// Implementation of the definition of landed in [M-4](../methodology.md).
fn landed(prev_position: &Position, position: &Position) -> bool {
matches!(
(&prev_position, &position),
(Position::Flying { .. }, Position::Grounded { .. })
) || (matches!(
(&prev_position, &position),
(Position::Flying { .. }, Position::Flying { .. })
) && position.datetime() - prev_position.datetime() > time::Duration::minutes(5)
&& position.altitude() < 10000.0)
) || grounded_heuristic(prev_position, position)
}

fn is_grounded(prev_position: &Position, position: &Position) -> bool {
matches!(
(&prev_position, &position),
(Position::Grounded { .. }, Position::Grounded { .. })
) || (matches!(
(&prev_position, &position),
(Position::Flying { .. }, Position::Flying { .. })
) && position.datetime() - prev_position.datetime() > time::Duration::minutes(5)
&& position.altitude() < 10000.0)
) || grounded_heuristic(prev_position, position)
}

/// Returns a set of [`Leg`]s from a sequence of [`Position`]s.
Expand All @@ -78,9 +87,11 @@ pub fn all_legs(mut positions: impl Iterator<Item = Position>) -> Vec<Leg> {
sequence.push(position.clone());
}
if landed(&prev_position, &position) {
legs.push(Leg {
positions: std::mem::take(&mut sequence),
});
if !sequence.is_empty() {
legs.push(Leg {
positions: std::mem::take(&mut sequence),
});
}
}
prev_position = position;
});
Expand Down
44 changes: 36 additions & 8 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,14 @@ async fn legs(
.collect::<Vec<_>>();
positions.sort_unstable_by_key(|p| p.datetime());

log::info!("Computing legs {}", icao_number);
let legs = flights::legs(positions.into_iter());

// filter by location
Ok(legs)
Ok(flights::legs(positions.into_iter()))
}

/// Verifies that condition 2. of `M-4` is correctly applied.
/// https://globe.adsbexchange.com/?icao=458d90&lat=53.265&lon=8.038&zoom=6.5&showTrace=2023-07-21
#[tokio::test]
async fn multi_day_legs() -> Result<(), Box<dyn Error>> {
async fn ads_b_lost_on_ground() -> Result<(), Box<dyn Error>> {
let legs = legs(date!(2023 - 07 - 21), date!(2023 - 07 - 23), "458d90", None).await?;

// same as ads-b computes: https://globe.adsbexchange.com/?icao=458d90&lat=53.265&lon=8.038&zoom=6.5&showTrace=2023-07-21
assert_eq!(legs.len(), 6);
Ok(())
}
Expand All @@ -91,6 +87,38 @@ async fn fs_azure() -> Result<(), Box<dyn Error>> {
let client = flights::fs_azure::initialize_anonymous("privatejets", "data");

let _ = flights::positions("459cd3", date!(2020 - 01 - 01), Some(&client)).await?;
Ok(())
}

#[tokio::test]
async fn case_459257_2023_12_17() -> Result<(), Box<dyn Error>> {
let legs = legs(date!(2023 - 12 - 17), date!(2023 - 12 - 20), "459257", None).await?;
assert_eq!(legs.len(), 4);
Ok(())
}

/// Verifies that condition 3. of `M-4` is correctly applied.
/// Case of losing signal for 2 days mid flight while traveling to central Africa.
/// https://globe.adsbexchange.com/?icao=45dd84&lat=9.613&lon=22.035&zoom=3.8&showTrace=2023-12-08
#[tokio::test]
async fn case_45dd84_2023_12_06() -> Result<(), Box<dyn Error>> {
let legs = legs(date!(2023 - 12 - 06), date!(2023 - 12 - 09), "45dd84", None).await?;
assert_eq!(legs.len(), 3);
let day = 24.0 * 60.0 * 60.0;
assert!(legs[0].duration().as_seconds_f32() < day);
assert!(legs[1].duration().as_seconds_f32() < day);
assert!(legs[2].duration().as_seconds_f32() < day);
Ok(())
}

#[tokio::test]
async fn case_45c824_2023_12_12() -> Result<(), Box<dyn Error>> {
let legs = legs(date!(2023 - 12 - 12), date!(2023 - 12 - 16), "45c824", None).await?;

assert_eq!(legs.len(), 3);
let day = 24.0 * 60.0 * 60.0;
assert!(legs[0].duration().as_seconds_f32() < day);
assert!(legs[1].duration().as_seconds_f32() < day);
assert!(legs[2].duration().as_seconds_f32() < day);
Ok(())
}
Loading