xref: /openbmc/openbmc-tools/altitude/altitude (revision 2302fa41)
1#!/usr/bin/python3
2
3# SPDX-License-Identifier: Apache-2.0
4# Copyright 2020 IBM Corp.
5
6# https://en.wikipedia.org/wiki/Barometric_formula
7
8import argparse
9from math import exp, log
10
11Pb = 101325.00
12Tb = 288.15
13Lb = -0.0065
14hb = 0
15Rstar = 8.3144598
16g0 = 9.80665
17M = 0.0289644
18C0 = 273.15
19
20
21def P(h):
22    return Pb * exp((-g0 * M * (h - hb)) / (Rstar * Tb))
23
24
25def T(h):
26    return (h - hb) * Lb + Tb
27
28
29def Hp(p):
30    return (log(p / Pb) * (Rstar * Tb)) / (-g0 * M) + hb
31
32
33def Ht(t):
34    return ((t - Tb) / Lb) + hb
35
36
37def K(c):
38    return C0 + c
39
40
41def C(k):
42    return k - C0
43
44
45def main():
46    parser = argparse.ArgumentParser()
47    parser.add_argument(
48        "--height",
49        type=float,
50        default=None,
51        help="Height above sea level in metres",
52    )
53    parser.add_argument(
54        "--temperature",
55        type=float,
56        default=None,
57        help="Temperature in Celcius",
58    )
59    parser.add_argument(
60        "--pressure",
61        type=float,
62        default=None,
63        help="Atmospheric pressure in Pascals",
64    )
65    args = parser.parse_args()
66    out = []
67    if args.height is not None:
68        local = []
69        local.append("Height")
70        p = P(args.height)
71        local.append("Pressure at {:.2f}m: {:.2f}Pa".format(args.height, p))
72        c = C(T(args.height))
73        local.append("Temperature at {:.2f}m: {:.2f}C".format(args.height, c))
74        out.append("\n\t".join(local))
75    if args.temperature is not None:
76        local = []
77        local.append("Temperature")
78        ht = Ht(K(args.temperature))
79        local.append("Height at {:.2f}C: {:.2f}m".format(args.temperature, ht))
80        p = P(ht)
81        local.append("Pressure at {:.2f}m: {:.2f}Pa".format(ht, p))
82        out.append("\n\t".join(local))
83    if args.pressure is not None:
84        local = []
85        local.append("Pressure")
86        hp = Hp(args.pressure)
87        local.append("Height at {:.2f}Pa: {:.2f}m".format(args.pressure, hp))
88        t = C(T(hp))
89        local.append("Temperature at {:.2f}m: {:.2f}C".format(hp, t))
90        out.append("\n\t".join(local))
91    print("\n\n".join(out))
92
93
94if __name__ == "__main__":
95    main()
96