Skip to content

Commit 8b831cb

Browse files
Bazifrasoolpre-commit-ci[bot]cclauss
authored
Added Altitude Pressure equation (TheAlgorithms#8909)
* Added Altitude Pressure equation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removed trailing whitespaces * Removed pylint * Fix lru_cache_pythonic.py * Fixed spellings * Fix again lru_cache_pythonic.py * Update .vscode/settings.json Co-authored-by: Christian Clauss <[email protected]> * Third fix lru_cache_pythonic.py * Update .vscode/settings.json Co-authored-by: Christian Clauss <[email protected]> * 4th fix lru_cache_pythonic.py * Update physics/altitude_pressure.py Co-authored-by: Christian Clauss <[email protected]> * lru_cache_pythonic.py: def get(self, key: Any, /) -> Any | None: * Delete lru_cache_pythonic.py * Added positive and negative pressure test cases * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent d31750a commit 8b831cb

File tree

2 files changed

+52
-113
lines changed

2 files changed

+52
-113
lines changed

other/lru_cache_pythonic.py

-113
This file was deleted.

physics/altitude_pressure.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Title : Calculate altitude using Pressure
3+
4+
Description :
5+
The below algorithm approximates the altitude using Barometric formula
6+
7+
8+
"""
9+
10+
11+
def get_altitude_at_pressure(pressure: float) -> float:
12+
"""
13+
This method calculates the altitude from Pressure wrt to
14+
Sea level pressure as reference .Pressure is in Pascals
15+
https://en.wikipedia.org/wiki/Pressure_altitude
16+
https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702
17+
18+
H = 44330 * [1 - (P/p0)^(1/5.255) ]
19+
20+
Where :
21+
H = altitude (m)
22+
P = measured pressure
23+
p0 = reference pressure at sea level 101325 Pa
24+
25+
Examples:
26+
>>> get_altitude_at_pressure(pressure=100_000)
27+
105.47836610778828
28+
>>> get_altitude_at_pressure(pressure=101_325)
29+
0.0
30+
>>> get_altitude_at_pressure(pressure=80_000)
31+
1855.873388064995
32+
>>> get_altitude_at_pressure(pressure=201_325)
33+
Traceback (most recent call last):
34+
...
35+
ValueError: Value Higher than Pressure at Sea Level !
36+
>>> get_altitude_at_pressure(pressure=-80_000)
37+
Traceback (most recent call last):
38+
...
39+
ValueError: Atmospheric Pressure can not be negative !
40+
"""
41+
42+
if pressure > 101325:
43+
raise ValueError("Value Higher than Pressure at Sea Level !")
44+
if pressure < 0:
45+
raise ValueError("Atmospheric Pressure can not be negative !")
46+
return 44_330 * (1 - (pressure / 101_325) ** (1 / 5.5255))
47+
48+
49+
if __name__ == "__main__":
50+
import doctest
51+
52+
doctest.testmod()

0 commit comments

Comments
 (0)