Skip to content

Commit

Permalink
Fix airport weather calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Mar 7, 2024
1 parent 9612dc2 commit a31a81b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ pub async fn snippet_weather(State(state): State<Arc<AppState>>) -> Result<Html<
.split_terminator('\n')
.flat_map(|line| {
parse_metar(line).map_err(|e| {
warn!("Metar parsing failure: {e}");
let airport = line.split(' ').next().unwrap_or("Unknown");
warn!("Metar parsing failure for {airport}: {e}");
e
})
})
Expand Down
18 changes: 15 additions & 3 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub struct AirportWeather<'a> {

/// Parse a METAR to determine if conditions are VMC or IMC.
pub fn parse_metar(line: &str) -> Result<AirportWeather> {
let visibility: u8 = 0;
let parts: Vec<_> = line.split(' ').collect();
let airport = parts.first().ok_or_else(|| anyhow!("Blank metar?"))?;
let mut cloud_layer = 1_001;
Expand All @@ -34,18 +33,31 @@ pub fn parse_metar(line: &str) -> Result<AirportWeather> {
cloud_layer = part
.chars()
.skip_while(|c| c.is_alphabetic())
.take_while(|c| c.is_numeric())
.collect::<String>()
.parse::<u16>()?
* 100;
break;
}
}
let visibility: u8 = parts
.iter()
.find(|part| part.ends_with("SM"))
.map(|part| {
let vis = part.replace("SM", "");
if vis.contains('/') {
0
} else {
vis.parse().unwrap()
}
})
.unwrap_or(0);
Ok(AirportWeather {
name: airport,
weather: if visibility >= 3 && cloud_layer > 1_000 {
"IMC"
} else {
"VMC"
} else {
"IMC"
},
raw: line,
})
Expand Down

0 comments on commit a31a81b

Please sign in to comment.