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 calculation of legs #21

Merged
merged 3 commits into from
Jan 28, 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
18 changes: 10 additions & 8 deletions methodology.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ This is performed automatically by the computer program and consists in looking
the historical route of the ICAO number in https://globe.adsbexchange.com.
This contains the sequence of `(latitude, longitude)` and other information.

Each position is assigned the state `Grounded` whether
the transponder returns "grounded" or the (barometric) altitude is lower than 1000 feet,
else it is assigned the state `Flying`.
Each position is assigned the state `Grounded` when
the transponder returns "grounded", else it is assigned the state `Flying`.

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

### M-4: Identify legs of a route

This is performed automatically by the computer program and consists in identifying
legs: contiguous sequence of positions that start and end on the state grounded.
This is performed automatically by the computer program. A leg is defined in this methodology has:

Furthermore, only legs fullfilling the below conditions are considered:
> a continuous sequence of ADS-B positions split by landings whose distance is higher than 3km and duration longer than 5 minutes

* Its distance is higher than 3km
* Its duration is longer than 5m
where a landing is identified by either:
* The previous ADS-B position was flying, and the current is grounded
* Both the previous and current position is flying, and are separated by more than 5 minutes apart.

The latter condition is used to mitigate the risk that ADS-B radars do not always pick up signals
too close from the ground, resulting the leg to not be identified.

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

Expand Down
24 changes: 14 additions & 10 deletions src/fs_azure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use azure_storage::prelude::*;
pub use azure_storage_blobs::prelude::ContainerClient as _ContainerClient;
use azure_storage_blobs::{container::operations::BlobItem, prelude::ClientBuilder};
use futures::stream::StreamExt;
use futures::stream::TryStreamExt;

use crate::fs::BlobStorageProvider;

Expand All @@ -12,21 +12,25 @@ pub struct ContainerClient {

/// Lists all blobs in container
pub async fn list(client: ContainerClient) -> Result<Vec<String>, azure_storage::Error> {
let mut result = vec![];
let mut blobs = client.client.list_blobs().into_stream();
while let Some(response) = blobs.next().await {
result.extend(
response?
Ok(client
.client
.list_blobs()
.into_stream()
.try_collect::<Vec<_>>()
.await?
.into_iter()
.map(|response| {
response
.blobs
.items
.into_iter()
.filter_map(|blob| match blob {
BlobItem::Blob(blob) => Some(blob.name),
BlobItem::BlobPrefix(_) => None,
}),
);
}
Ok(result)
})
})
.flatten()
.collect())
}

/// Returns whether the blob exists in container
Expand Down
32 changes: 7 additions & 25 deletions src/icao_to_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub async fn positions(
trace_cached(icao_number, &date, client)
.await
.map(move |trace| {
trace.into_iter().map(move |entry| {
trace.into_iter().filter_map(move |entry| {
let icao = icao.clone();
let time_seconds = entry[0].as_f64().unwrap();
let time = time::Time::MIDNIGHT + time_seconds.seconds();
Expand All @@ -192,34 +192,16 @@ pub async fn positions(
longitude,
})
})
.unwrap_or_else(|| {
entry[3]
.as_f64()
.and_then(|altitude| {
// < 1000 feet => grounded, see M-3
Some(if altitude < 1000.0 {
Position::Grounded {
icao: icao.clone(),
datetime,
latitude,
longitude,
}
} else {
Position::Flying {
icao: icao.clone(),
datetime,
latitude,
longitude,
altitude,
}
})
})
.unwrap_or(Position::Grounded {
icao,
.or_else(|| {
entry[3].as_f64().and_then(|altitude| {
Some(Position::Flying {
icao: icao.clone(),
datetime,
latitude,
longitude,
altitude,
})
})
})
})
})
Expand Down
29 changes: 24 additions & 5 deletions src/legs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ impl Leg {
}
}

/// 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))
}

/// Returns a set of [`Leg`]s from a sequence of [`Position`]s.
pub fn all_legs(mut positions: impl Iterator<Item = Position>) -> Vec<Leg> {
let Some(mut prev_position) = positions.next() else {
Expand All @@ -51,18 +62,26 @@ pub fn all_legs(mut positions: impl Iterator<Item = Position>) -> Vec<Leg> {
let mut sequence: Vec<Position> = vec![];
let mut legs: Vec<Leg> = vec![];
positions.for_each(|position| {
if let (Position::Grounded { .. }, Position::Grounded { .. }) = (&prev_position, &position)
{
// legs are by definition the minimum length on ground
prev_position = position;
return;
};
sequence.push(position.clone());
if let (Position::Flying { .. }, Position::Grounded { .. }) = (&prev_position, &position) {
if landed(&prev_position, &position) {
legs.push(Leg {
positions: std::mem::take(&mut sequence),
});
};
}
prev_position = position;
});

// if it is still flying, remove the incomplete leg
if matches!(prev_position, Position::Flying { .. }) && !legs.is_empty() {
legs.pop();
// if it is still flying, make it a new leg
if !sequence.is_empty() {
legs.push(Leg {
positions: std::mem::take(&mut sequence),
})
}

legs
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use model::*;
pub use owners::*;

/// A position of an aircraft
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Position {
/// Aircraft transponder declares the aircraft is grounded
Grounded {
Expand Down
2 changes: 1 addition & 1 deletion src/trace_month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{fs, fs_azure};

fn cache_file_path(icao: &str, date: &time::Date) -> String {
format!(
"{DIRECTORY}/{DATABASE}/{}-{}/trace_full_{icao}.json",
"{DIRECTORY}/{DATABASE}/{}-{:02}/trace_full_{icao}.json",
date.year(),
date.month() as u8
)
Expand Down
33 changes: 32 additions & 1 deletion tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::error::Error;

use time::macros::date;
use flights::Leg;
use time::{macros::date, Date};

/// Verifies that we compute the correct number of legs.
/// The expected 2 was confirmed by manual inspection of
Expand Down Expand Up @@ -54,3 +55,33 @@ async fn legs_() -> Result<(), Box<dyn Error>> {
assert_eq!(legs.len(), 5);
Ok(())
}

async fn legs(
from: Date,
to: Date,
icao_number: &str,
client: Option<&flights::fs_azure::ContainerClient>,
) -> Result<Vec<Leg>, Box<dyn Error>> {
let positions = flights::aircraft_positions(from, to, icao_number, client).await?;
let mut positions = positions
.into_iter()
.map(|(_, p)| p)
.flatten()
.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)
}

#[tokio::test]
async fn multi_day_legs() -> 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(())
}
Loading