#!/usr/bin/env ruby

require "narray"
include Math

def temp(h)

  if h < 0.0 then
    temp = 725.0
  elsif h >= 0.0 && h < 10.0 then
    temp = 725.0 + (650.0-725.0)/10.0*h
  elsif h >= 10.0 && h < 20.0 then
    temp = 650.0 + (570-650)/10.0*(h-10)
  elsif h >= 20.0 && h < 30.0 then
    temp = 570.0 + (490-570)/10.0*(h-20)
  elsif h >= 30.0 && h < 40.0 then
    temp = 490.0 + (410-490)/10.0*(h-30)
  elsif h >= 40.0 && h < 50.0 then
    temp = 410.0 + (340-410)/10.0*(h-40)
  elsif h >= 50.0 && h < 60.0 then
    temp = 340.0 + (275-340)/10.0*(h-50)
  elsif h >= 60.0 && h < 70.0 then
    temp = 275.0 + (220-275)/10.0*(h-60)
  elsif h >= 70.0 && h < 80.0 then
    temp = 220.0 + (190-220)/10.0*(h-70)
  elsif h >= 80.0 && h < 90.0 then
    temp = 190.0 + (180-190)/10.0*(h-80)
  else 
    temp = 180.0
  end
end


RGas = 191.4
Grav = 8.87

NZ = 50
NZ1 = 50
z   = NArray.float(NZ)
sig = NArray.float(NZ)

# ----- 高度の設定 ------

for i in 0..10
  z[i]= 1.0*i
end

for i in 11..NZ-1
  z[i]= 10.0 + 2.0*(i-10)
end

# ----- σの計算 ------

phi = 0.0
sig[0] = 1.0

for i in 0..NZ-2
  for j in 1..NZ1
    dz = (z[i+1]-z[i])/(NZ1-1)
    z1 = z[i]+dz*(j-0.5)
    phi = phi + Grav/(RGas*temp(z1))*dz*1e3
  end
  sig[i+1] = exp(-phi)
end


for i in 0..NZ-1
  printf("%d %f %f %e\n",i, z[i], temp(z[i]), sig[i])
end

