Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active April 19, 2018 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/d77a4d42ae10d15e4c9f to your computer and use it in GitHub Desktop.
Save komasaru/d77a4d42ae10d15e4c9f to your computer and use it in GitHub Desktop.
Ruby script to calculate values about pressure.
#! /usr/local/bin/ruby
# coding: utf-8
#*********************************************
# Ruby script to calculate values about pressure.
#*********************************************
#
class CalcPressure
# Constants
C_1 = 0.0065
C_2 = 273.15
C_3 = 5.257
# Calculate a sealevel pressure.
def calc_sealevel(p, t, h)
v = 1 - (C_1 * h) / (t + C_1 * h + C_2)
v **= C_3
return p / v
end
# Calculate a pressure from a height above sea level.
def calc_h2p(p, t, h)
v = 1 - (C_1 * h) / (t + C_1 * h + C_2)
v **= C_3
return p * v
end
# Calculate a height above sea level from a pressure.
def calc_p2h(p_0, p, t)
v = (p_0 / p.to_f) ** (1 / C_3) - 1
v *= t + C_2
return v / C_1
end
# Calculate a temperature at the destination.
def calc_temp_dest(t_a, h_a, h)
return t_a - C_1 * (h - h_a)
end
end
if __FILE__ == $0
obj = CalcPressure.new
# Calculate a sealevel pressure.
p, t, h = 850, 15, 1729
p_0 = obj.calc_sealevel(p, t, h)
puts "P = #{p}, T = #{t}, h = #{h}"
puts "P_0 = #{p_0}"
puts "---"
# Calculate a pressure from a height above sea level.
p_0, t, h = 1015.25, 5, 1729
p = obj.calc_h2p(p_0, t, h)
puts "P_0 = #{p_0}, T = #{t}, h = #{h}"
puts "P = #{p}"
puts "---"
# Calculate a height above sea level from a pressure.
p_0, p, t = 1005.75, 900, 15
h = obj.calc_p2h(p_0, p, t)
puts "P_0 = #{p_0}, p = #{p}, T = #{t}"
puts "h = #{h}"
puts "---"
# Calculate a temperature at the destination.
t_a, h_a, h = 15, 1729, 0
t = obj.calc_temp_dest(t_a, h_a, h)
puts "T_a = #{t_a}, h_a = #{h_a}, h = #{h}"
puts "T = #{t}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment