Andrew Jeffery | 1b23a5d | 2020-09-14 13:40:11 +0930 | [diff] [blame] | 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 | |
| 8 | from math import exp, log |
| 9 | import sys |
| 10 | import argparse |
| 11 | |
| 12 | Pb = 101325.00 |
| 13 | Tb = 288.15 |
| 14 | Lb = -0.0065 |
| 15 | hb = 0 |
| 16 | Rstar = 8.3144598 |
| 17 | g0 = 9.80665 |
| 18 | M = 0.0289644 |
| 19 | C0 = 273.15 |
| 20 | |
| 21 | def P(h): |
| 22 | return Pb * exp((-g0 * M * (h - hb)) / (Rstar * Tb)) |
| 23 | |
| 24 | def T(h): |
| 25 | return (h - hb) * Lb + Tb |
| 26 | |
| 27 | def Hp(p): |
| 28 | return (log(p / Pb) * (Rstar * Tb)) / (-g0 * M) + hb |
| 29 | |
| 30 | def Ht(t): |
| 31 | return ((t - Tb) / Lb) + hb |
| 32 | |
| 33 | def K(c): |
| 34 | return C0 + c |
| 35 | |
| 36 | def C(k): |
| 37 | return k - C0 |
| 38 | |
| 39 | def main(): |
| 40 | parser = argparse.ArgumentParser() |
| 41 | parser.add_argument("--height", type=float, default=None, help="Height above sea level in metres") |
| 42 | parser.add_argument("--temperature", type=float, default=None, help="Temperature in Celcius") |
| 43 | parser.add_argument("--pressure", type=float, default=None, help="Atmospheric pressure in Pascals") |
| 44 | args = parser.parse_args() |
| 45 | out = [] |
| 46 | if args.height is not None: |
| 47 | local = [] |
| 48 | local.append("Height") |
| 49 | p = P(args.height) |
| 50 | local.append("Pressure at {:.2f}m: {:.2f}Pa".format(args.height, p)) |
| 51 | c = C(T(args.height)) |
| 52 | local.append("Temperature at {:.2f}m: {:.2f}C".format(args.height, c)) |
| 53 | out.append("\n\t".join(local)) |
| 54 | if args.temperature is not None: |
| 55 | local = [] |
| 56 | local.append("Temperature") |
| 57 | ht = Ht(K(args.temperature)) |
| 58 | local.append("Height at {:.2f}C: {:.2f}m".format(args.temperature, ht)) |
| 59 | p = P(ht) |
| 60 | local.append("Pressure at {:.2f}m: {:.2f}Pa".format(ht, p)) |
| 61 | out.append("\n\t".join(local)) |
| 62 | if args.pressure is not None: |
| 63 | local = [] |
| 64 | local.append("Pressure") |
| 65 | hp = Hp(args.pressure) |
| 66 | local.append("Height at {:.2f}Pa: {:.2f}m".format(args.pressure, hp)) |
| 67 | t = C(T(hp)) |
| 68 | local.append("Temperature at {:.2f}m: {:.2f}C".format(hp, t)) |
| 69 | out.append("\n\t".join(local)) |
| 70 | print("\n\n".join(out)) |
| 71 | |
| 72 | if __name__ == "__main__": |
| 73 | main() |