|
1 | | -import { freezingLevelAltitude } from '../../src/formulas/altitude'; |
| 1 | +import { |
| 2 | + freezingLevelAltitude, |
| 3 | + altitudeFromPressureDifference |
| 4 | +} from '../../src/formulas/altitude'; |
2 | 5 | import { cloudBaseHeight } from '../../src/phenomena/cloud'; |
3 | 6 |
|
4 | 7 | describe('freezingLevelHeight', () => { |
@@ -88,3 +91,56 @@ describe('cloudBaseHeight', () => { |
88 | 91 | expect(result).toBeCloseTo(1249.4, 2); |
89 | 92 | }); |
90 | 93 | }); |
| 94 | + |
| 95 | +describe('altitudeFromPressureDifference', () => { |
| 96 | + it('should return 0 when pressures are equal', () => { |
| 97 | + const result = altitudeFromPressureDifference(101325, 101325); |
| 98 | + expect(result).toBe(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should calculate positive altitude difference when current pressure is lower', () => { |
| 102 | + // Sea level pressure to ~1000m altitude pressure |
| 103 | + // Using standard temperature 288.15K (15°C) |
| 104 | + const result = altitudeFromPressureDifference(101325, 89874.46, 288.15); |
| 105 | + // Expected: ~1000m (approximately, based on barometric formula) |
| 106 | + // The hypsometric formula may give slightly different results than barometric |
| 107 | + expect(result).toBeCloseTo(1011, 0); // Within 1 meter |
| 108 | + }); |
| 109 | + |
| 110 | + it('should calculate negative altitude difference when current pressure is higher', () => { |
| 111 | + // From higher altitude to sea level (going down) |
| 112 | + const result = altitudeFromPressureDifference(89874.46, 101325, 288.15); |
| 113 | + // Expected: ~-1000m (going down) |
| 114 | + expect(result).toBeCloseTo(-1011, 0); // Within 1 meter |
| 115 | + }); |
| 116 | + |
| 117 | + it('should calculate altitude difference at standard sea level to 500m', () => { |
| 118 | + // Sea level standard: 101325 Pa |
| 119 | + // At 500m altitude: ~95461 Pa (approximately) |
| 120 | + // Using hypsometric formula: Δh = (R * T / g) * ln(P1/P2) |
| 121 | + // With R=287.05, T=288.15K, g=9.80665 |
| 122 | + // Scale height = 287.05 * 288.15 / 9.80665 = 8434.5 m |
| 123 | + // For ~500m: P2 = P1 * exp(-500/8434.5) = 101325 * 0.9424 = 95461 Pa |
| 124 | + const result = altitudeFromPressureDifference(101325, 95461, 288.15); |
| 125 | + expect(result).toBeCloseTo(500, -1); // Within 10 meters |
| 126 | + }); |
| 127 | + |
| 128 | + it('should account for temperature in calculations', () => { |
| 129 | + // Same pressure difference at different temperatures should yield different altitudes |
| 130 | + const coldTemp = 273.15; // 0°C |
| 131 | + const warmTemp = 303.15; // 30°C |
| 132 | + |
| 133 | + const resultCold = altitudeFromPressureDifference(101325, 95000, coldTemp); |
| 134 | + const resultWarm = altitudeFromPressureDifference(101325, 95000, warmTemp); |
| 135 | + |
| 136 | + // Warmer air is less dense, so same pressure drop represents larger altitude change |
| 137 | + expect(resultWarm).toBeGreaterThan(resultCold); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should use default temperature when not provided', () => { |
| 141 | + const resultWithDefault = altitudeFromPressureDifference(101325, 95000); |
| 142 | + const resultWithExplicit = altitudeFromPressureDifference(101325, 95000, 288.15); |
| 143 | + |
| 144 | + expect(resultWithDefault).toBe(resultWithExplicit); |
| 145 | + }); |
| 146 | +}); |
0 commit comments