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 8from math import exp, log 9import sys 10import argparse 11 12Pb = 101325.00 13Tb = 288.15 14Lb = -0.0065 15hb = 0 16Rstar = 8.3144598 17g0 = 9.80665 18M = 0.0289644 19C0 = 273.15 20 21def P(h): 22 return Pb * exp((-g0 * M * (h - hb)) / (Rstar * Tb)) 23 24def T(h): 25 return (h - hb) * Lb + Tb 26 27def Hp(p): 28 return (log(p / Pb) * (Rstar * Tb)) / (-g0 * M) + hb 29 30def Ht(t): 31 return ((t - Tb) / Lb) + hb 32 33def K(c): 34 return C0 + c 35 36def C(k): 37 return k - C0 38 39def 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 72if __name__ == "__main__": 73 main() 74