Skip to content

Commit fdde0b9

Browse files
authored
Merge pull request #74 from jbrodovsky/jbrodovsky/issue44
Jbrodovsky/issue44
2 parents 8304e65 + 6dbc061 commit fdde0b9

4 files changed

Lines changed: 327 additions & 78 deletions

File tree

core/src/earth.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,48 +72,46 @@ pub const MAGNETIC_FIELD_STRENGTH: f64 = 3.12e-5; // T, reference mean magnetic
7272
pub const METERS_TO_DEGREES: f64 = 1.0 / (60.0 * 1852.0);
7373
/// Rough conversion factor from degrees to meters for latitude/longitude via nautical miles (1 degree ~ 60 nautical miles; 1 nautical mile ~ 1852 meters)
7474
pub const DEGREES_TO_METERS: f64 = 60.0 * 1852.0;
75-
/// Sea-level atmospheric pressure in pascals
76-
pub const SEA_LEVEL_PRESSURE: f64 = 101325.0; // Pa, standard sea-level pressure
77-
/// Temperature lapse rate in Kelvin per meter
78-
pub const TEMPERATURE_LAPSE_RATE: f64 = 0.00976; // K/m, standard temperature lapse rate in the troposphere
79-
/// Constant-pressure specific heat
80-
pub const CONSTANT_PRESSURE_SPECIFIC_HEAT: f64 = 1004.68506; // J/(kg·K), specific heat at constant pressure for dry air
81-
/// Sea level standard temperature in Kelvin
82-
pub const SEA_LEVEL_STANDARD_TEMPERATURE: f64 = 288.15; // K, standard sea-level temperature
83-
/// Molar mass of dry air in kg/mol
84-
pub const MOLAR_MASS_DRY_AIR: f64 = 0.02896968; // kg/mol, molar mass of dry air
85-
/// Universal gas constant in J/(mol·K)
86-
pub const UNIVERSAL_GAS_CONSTANT: f64 = 8.314462618; // J/(mol·K), universal gas constant
87-
/// Calculate the barometric altitude from pressure and temperature
75+
/// Atmospheric pressure at sea level in Pascals ($P_0$)
76+
pub const SEA_LEVEL_PRESSURE: f64 = 101325.0;
77+
/// Standard temperature at sea level in Kelvin ($T_0$)
78+
pub const SEA_LEVEL_TEMPERATURE: f64 = 288.15;
79+
/// Molar mass of dry air in kg/mol ($M$)
80+
pub const MOLAR_MASS_DRY_AIR: f64 = 0.0289644;
81+
/// Universal gas constant in J/(mol·K) ($R_0$)
82+
pub const UNIVERSAL_GAS_CONSTANT: f64 = 8.314462618;
83+
/// Standard lapse rate in K/m ($L$)
84+
pub const STANDARD_LAPSE_RATE: f64 = 0.0065;
85+
/// Calculate a barometric altitude from a measured pressure
8886
///
89-
/// This function calculates the barometric altitude using the barometric formula, which relates
90-
/// the pressure at a given altitude to the pressure at sea level. The formula is based on the
91-
/// assumption of an isothermal atmosphere and the ideal gas law.
92-
///
93-
/// # Arguments
94-
/// - `pressure` - The atmospheric pressure at the given altitude in pascals
87+
/// This function calculates the altitude above sea level based on the measured pressure
88+
/// using the barometric formula. The formula assumes a standard atmosphere and uses the
89+
/// universal gas constant, standard lapse rate, and molar mass of dry air.
9590
///
91+
/// # Parameters
92+
/// - `pressure` - The measured pressure in Pascals
9693
/// # Returns
97-
/// The barometric altitude in meters
98-
pub fn barometric_altitude(pressure: f64) -> f64 {
99-
let exponent: f64 = -((UNIVERSAL_GAS_CONSTANT * TEMPERATURE_LAPSE_RATE) / (G0 * MOLAR_MASS_DRY_AIR));
100-
(SEA_LEVEL_STANDARD_TEMPERATURE / TEMPERATURE_LAPSE_RATE) * (pressure / SEA_LEVEL_PRESSURE) * exponent.exp()
94+
/// The calculated altitude in meters above sea level
95+
pub fn barometric_altitude(pressure: &f64) -> f64 {
96+
let exponent: f64 = - (UNIVERSAL_GAS_CONSTANT * STANDARD_LAPSE_RATE) / (G * MOLAR_MASS_DRY_AIR);
97+
(SEA_LEVEL_PRESSURE / STANDARD_LAPSE_RATE) * (pressure / SEA_LEVEL_PRESSURE - 1.0) * exponent.exp()
10198
}
102-
/// Calculates the relative barometric altitude from pressure and temperature
99+
/// Calculate the relative barometric altitude from a measured pressure
103100
///
104-
/// This function calculates the relative barometric altitude using the barometric formula,
105-
/// which relates the pressure at a given altitude to the pressure at a reference altitude.
106-
/// This is similar to how aircraft pressure altimeters work, where the preassure at a known
107-
/// altitude is used to calibrate the altimeter.
101+
/// This function calculates the relative altitude from a reference pressure using the
102+
/// barometric formula. The formula assumes a standard atmosphere and uses the universal
103+
/// gas constant, standard lapse rate, and molar mass of dry air.
108104
///
109-
/// # Arguments
110-
/// - `pressure` - The measured atmospheric pressure
111-
/// - `initial_pressure` - The atmospheric pressure at the reference altitude in pascals
105+
/// # Parameters
106+
/// - `pressure` - The measured pressure in Pascals
107+
/// - `initial_pressure` - The reference pressure in Pascals
108+
/// - `average_temperature` - Optional average temperature in Kelvin (defaults to standard sea level temperature)
112109
///
113110
/// # Returns
114-
/// The relative barometric altitude in meters, i.e. the altitude relative to the reference.
115-
pub fn relative_barometric_altitude(pressure: f64, initial_pressure: f64) -> f64 {
116-
((UNIVERSAL_GAS_CONSTANT * SEA_LEVEL_PRESSURE) / (G0 * MOLAR_MASS_DRY_AIR)) * (initial_pressure / pressure).ln()
111+
/// The calculated relative altitude in meters above the reference pressure
112+
pub fn relative_barometric_altitude(pressure: f64, initial_pressure: f64, average_temperature: Option<f64>) -> f64 {
113+
let average_temperature = average_temperature.unwrap_or(SEA_LEVEL_TEMPERATURE);
114+
((UNIVERSAL_GAS_CONSTANT * average_temperature) / (G * MOLAR_MASS_DRY_AIR)) * (initial_pressure / pressure).ln()
117115
}
118116
/// Convert a three-element vector to a skew-symmetric matrix
119117
///

0 commit comments

Comments
 (0)