Skip to content

Commit 9bac28e

Browse files
authored
Merge pull request #39 from oyve/copilot/fix-altitude-function-tests
Add altitudeFromPressureDifference function returning final altitude
2 parents 819bea2 + 7e079e8 commit 9bac28e

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ console.log(`Dew Point: ${dewPoint} K`);
6666
| Formula | Description |
6767
|-----------------------------|---------------------------------------------------------------------------|
6868
| **Freezing Level Altitude** | Estimate the altitude where temperature drops below freezing. [🔗](https://en.wikipedia.org/wiki/Freezing_level) |
69+
| **Altitude From Pressure Difference** | Calculate the final altitude from a pressure difference using the hypsometric formula. [🔗](https://en.wikipedia.org/wiki/Hypsometric_equation) |
6970

7071
### Humidity
7172
| Formula | Description |

src/formulas/altitude.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as c from '../constants';
2+
13
/**
24
* Estimate the altitude (in meters) where the temperature drops below freezing (0°C).
35
* Assumes a linear lapse rate (default: 0.0065 K/m).
@@ -16,3 +18,39 @@ export function freezingLevelAltitude(
1618
const altitudeDiff = (surfaceTemp - 273.15) / lapseRate;
1719
return surfaceAltitude + altitudeDiff;
1820
}
21+
22+
/**
23+
* Calculate the final altitude from a pressure difference using the hypsometric formula.
24+
* Given a reference pressure at a known altitude and an observed pressure, this function
25+
* returns the altitude at which the observed pressure occurs.
26+
*
27+
* @param {number} referencePressure - Reference pressure in Pascals (Pa) at the reference altitude.
28+
* @param {number} observedPressure - Observed pressure in Pascals (Pa) at the unknown altitude.
29+
* @param {number} referenceAltitude - Altitude in meters (m) where the reference pressure was measured. Defaults to 0 (sea level).
30+
* @param {number} temperature - Average temperature in Kelvin (K) between the two altitudes. Defaults to 288.15 K (15°C).
31+
* @returns {number} The final altitude in meters (m) where the observed pressure occurs.
32+
*
33+
* @example
34+
* // Calculate altitude when pressure drops from 101325 Pa (sea level) to 89874 Pa
35+
* const altitude = altitudeFromPressureDifference(101325, 89874, 0, 288.15);
36+
* console.log(altitude); // ~1000 m
37+
*
38+
* @see https://en.wikipedia.org/wiki/Hypsometric_equation
39+
*/
40+
export function altitudeFromPressureDifference(
41+
referencePressure: number,
42+
observedPressure: number,
43+
referenceAltitude: number = 0,
44+
temperature: number = c.STANDARD_MEAN_TEMPERATURE_KELVIN
45+
): number {
46+
const g = c.DRY_AIR_CONSTANTS.gravity; // Gravitational acceleration (m/s²)
47+
const R = c.DRY_AIR_CONSTANTS.gasConstant; // Specific gas constant for dry air (J/(kg·K))
48+
49+
// Using the hypsometric formula: h = (R * T / g) * ln(P1 / P2)
50+
// Where h is the altitude difference, R is gas constant, T is temperature,
51+
// g is gravity, P1 is reference pressure, P2 is observed pressure
52+
const altitudeDifference = (R * temperature / g) * Math.log(referencePressure / observedPressure);
53+
54+
// Return the final altitude (reference altitude + altitude difference)
55+
return referenceAltitude + altitudeDifference;
56+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as temperature from "./formulas/temperature";
33
import * as pressure from "./formulas/pressure";
44
import * as wind from "./formulas/wind";
55
import * as airDensity from "./formulas/airDensity";
6+
import * as altitude from "./formulas/altitude";
67

78
import * as beaufort from "./scales/beaufort";
89
import * as saffirSimpson from "./scales/saffirSimpson";
@@ -18,5 +19,6 @@ export {
1819
pressure,
1920
wind,
2021
airDensity,
22+
altitude,
2123
scales
2224
};

tests/formulas/altitude.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { freezingLevelAltitude } from '../../src/formulas/altitude';
1+
import { freezingLevelAltitude, altitudeFromPressureDifference } from '../../src/formulas/altitude';
22
import { cloudBaseHeight } from '../../src/phenomena/cloud';
33

44
describe('freezingLevelHeight', () => {
@@ -29,6 +29,59 @@ describe('freezingLevelHeight', () => {
2929
});
3030
});
3131

32+
describe('altitudeFromPressureDifference', () => {
33+
it('should calculate altitude from sea level pressure', () => {
34+
// Sea level pressure: 101325 Pa, pressure at ~1000m: ~89874 Pa
35+
// Using hypsometric formula: h = (R * T / g) * ln(P1 / P2)
36+
// With R = 287.05 J/(kg·K), T = 288.15 K, g = 9.80665 m/s²
37+
const result = altitudeFromPressureDifference(101325, 89874, 0, 288.15);
38+
// Calculated: (287.05 * 288.15 / 9.80665) * ln(101325 / 89874) ≈ 1011.49 m
39+
expect(result).toBeCloseTo(1011.49, 0);
40+
});
41+
42+
it('should return the final altitude, not just the difference', () => {
43+
// If reference altitude is 500 m, the result should be 500 + altitude difference
44+
const result = altitudeFromPressureDifference(101325, 89874, 500, 288.15);
45+
// Should be approximately 500 + 1011.49 = 1511.49 m
46+
expect(result).toBeCloseTo(1511.49, 0);
47+
});
48+
49+
it('should return reference altitude when pressures are equal', () => {
50+
// If pressures are equal, altitude difference is 0
51+
const result = altitudeFromPressureDifference(101325, 101325, 100, 288.15);
52+
expect(result).toBeCloseTo(100, 2);
53+
});
54+
55+
it('should calculate lower altitude when observed pressure is higher', () => {
56+
// Higher pressure means lower altitude
57+
const result = altitudeFromPressureDifference(89874, 101325, 1000, 288.15);
58+
// Should be approximately 1000 - 1011.49 ≈ -11.49 m
59+
expect(result).toBeCloseTo(-11.49, 0);
60+
});
61+
62+
it('should calculate altitude at high elevation from sea level', () => {
63+
// Sea level pressure: 101325 Pa, pressure at ~5000m: ~54020 Pa
64+
const result = altitudeFromPressureDifference(101325, 54020, 0, 288.15);
65+
// Calculated value using hypsometric formula
66+
expect(result).toBeCloseTo(5305.07, 0);
67+
});
68+
69+
it('should work with default parameters', () => {
70+
// Uses default referenceAltitude = 0 and temperature = 288.15 K
71+
const result = altitudeFromPressureDifference(101325, 89874);
72+
expect(result).toBeCloseTo(1011.49, 0);
73+
});
74+
75+
it('should account for different temperatures', () => {
76+
// Colder temperature should result in slightly different altitude calculation
77+
const coldResult = altitudeFromPressureDifference(101325, 89874, 0, 273.15); // 0°C
78+
const warmResult = altitudeFromPressureDifference(101325, 89874, 0, 303.15); // 30°C
79+
80+
// Warmer air is less dense, so same pressure difference = larger altitude change
81+
expect(warmResult).toBeGreaterThan(coldResult);
82+
});
83+
});
84+
3285
describe('cloudBaseHeight', () => {
3386
it('should calculate cloud base height at sea level', () => {
3487
// Temperature: 293.15 K (20°C), Dew Point: 283.15 K (10°C)

0 commit comments

Comments
 (0)