Skip to content

Commit

Permalink
feat: table export functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
newbthenewbd committed Mar 23, 2024
1 parent 6e9f740 commit 92bb4f6
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/atmospheric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "atmospheric/version"
require_relative "atmospheric/isa"
require_relative "atmospheric/export"

module Atmospheric
class Error < StandardError; end
Expand Down
185 changes: 185 additions & 0 deletions lib/atmospheric/export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
require "yaml"

module Atmospheric
module Export
class << self
private

def dict_to_yaml(dict, filename)
File.write(filename, dict.to_yaml(indentation: 4)
.gsub("\n- ", "\n\n - ").gsub(/^(.*):\n/, "\n\\1:")) # Make fancy
end

def round_to_sig_figs(num, num_sig_figs)
num.round(num_sig_figs - Math.log10(num).ceil).to_f
end

public

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/BlockLength
# rubocop:disable Metrics/MethodLength
def iso_2533_1975_table5_to_yaml(filename)
d = { "rows-h" => [], "rows-H" => [] }

(-2000.0..80000.0).step(50.0) do |h1|
# Step 100 above 32k, 200 above 51k
next if (h1 > 32000 && h1 % 100 != 0) \
|| (h1 > 51000 && h1 % 200 != 0)

h2 = Isa.geopotential_altitude_from_geometric(h1)
d["rows-h"] << {
"geometrical-altitude" => h1.round,
"geopotential-altitude" => h2.round,
"temperature-K" =>
(Isa.temperature_at_layer_from_geopotential(h2) * 1000).round,
"temperature-C" =>
(Isa.temperature_at_layer_celcius(h2) * 1000).round,
"p-mbar" =>
round_to_sig_figs(Isa.pressure_from_geopotential_mbar(h2), 6),
"p-mmHg" =>
round_to_sig_figs(Isa.pressure_from_geopotential_mmhg(h2), 6),
"density" =>
round_to_sig_figs(Isa.density_from_geopotential(h2), 6),
"acceleration" => Isa.gravity_at_geometric(h1).round(4),
}

h2 = Isa.geometric_altitude_from_geopotential(h1)
d["rows-H"] << {
"geopotential-altitude" => h1.round,
"geometrical-altitude" => h2.round,
"temperature-K" =>
(Isa.temperature_at_layer_from_geopotential(h1) * 1000).round,
"temperature-C" =>
(Isa.temperature_at_layer_celcius(h1) * 1000).round,
"p-mbar" =>
round_to_sig_figs(Isa.pressure_from_geopotential_mbar(h1), 6),
"p-mmHg" =>
round_to_sig_figs(Isa.pressure_from_geopotential_mmhg(h1), 6),
"density" =>
round_to_sig_figs(Isa.density_from_geopotential(h1), 6),
"acceleration" => Isa.gravity_at_geopotential(h1).round(4),
}
end
dict_to_yaml(d, filename)
end

def iso_2533_1975_table6_to_yaml(filename)
d = { "rows-h" => [], "rows-H" => [] }

(-2000.0..80000.0).step(50.0) do |h1|
# Step 100 above 32k, 200 above 51k
next if (h1 > 32000 && h1 % 100 != 0) \
|| (h1 > 51000 && h1 % 200 != 0)

h2 = Isa.geopotential_altitude_from_geometric(h1)
d["rows-h"] << {
"geometrical-altitude" => h1.round,
"ppn" => round_to_sig_figs(Isa.p_p_n_from_geopotential(h2), 6),
"rhorhon" =>
round_to_sig_figs(Isa.rho_rho_n_from_geopotential(h2), 6),
"sqrt-rhorhon" =>
round_to_sig_figs(Isa.root_rho_rho_n_from_geopotential(h2), 6),
"speed-of-sound" =>
(Isa.speed_of_sound_from_geopotential(h2) * 1000).round,
"dynamic-viscosity" =>
round_to_sig_figs(
Isa.dynamic_viscosity_from_geopotential(h2), 5
),
"kinematic-viscosity" =>
round_to_sig_figs(
Isa.kinematic_viscosity_from_geopotential(h2), 5
),
"thermal-conductivity" =>
round_to_sig_figs(
Isa.thermal_conductivity_from_geopotential(h2), 5
),
}

d["rows-H"] << {
"geopotential-altitude" => h1.round,
"ppn" => round_to_sig_figs(Isa.p_p_n_from_geopotential(h1), 6),
"rhorhon" =>
round_to_sig_figs(Isa.rho_rho_n_from_geopotential(h1), 6),
"sqrt-rhorhon" =>
round_to_sig_figs(Isa.root_rho_rho_n_from_geopotential(h1), 6),
"speed-of-sound" =>
(Isa.speed_of_sound_from_geopotential(h1) * 1000).round,
"dynamic-viscosity" =>
round_to_sig_figs(
Isa.dynamic_viscosity_from_geopotential(h1), 5
),
"kinematic-viscosity" =>
round_to_sig_figs(
Isa.kinematic_viscosity_from_geopotential(h1), 5
),
"thermal-conductivity" =>
round_to_sig_figs(
Isa.thermal_conductivity_from_geopotential(h1), 5
),
}
end
dict_to_yaml(d, filename)
end

def iso_2533_1975_table7_to_yaml(filename)
d = { "rows-h" => [], "rows-H" => [] }

(-2000.0..80000.0).step(50.0) do |h1|
# Step 100 above 32k, 200 above 51k
next if (h1 > 32000 && h1 % 100 != 0) \
|| (h1 > 51000 && h1 % 200 != 0)

h2 = Isa.geopotential_altitude_from_geometric(h1)
d["rows-h"] << {
"geometrical-altitude" => h1.round,
"pressure-scale-height" =>
Isa.pressure_scale_height_from_geopotential(h2).round(1),
"specific-weight" =>
round_to_sig_figs(Isa.specific_weight_from_geopotential(h2), 5),
"air-number-density" =>
round_to_sig_figs(
Isa.air_number_density_from_geopotential(h2), 5
),
"mean-speed" =>
Isa.mean_air_particle_speed_from_geopotential(h2).round(2),
"frequency" =>
round_to_sig_figs(
Isa.air_particle_collision_frequency_from_geopotential(h2), 5
),
"mean-free-path" =>
round_to_sig_figs(
Isa.mean_free_path_of_air_particles_from_geopotential(h2), 5
),
}

d["rows-H"] << {
"geopotential-altitude" => h1.round,
"pressure-scale-height" =>
Isa.pressure_scale_height_from_geopotential(h1).round(1),
"specific-weight" =>
round_to_sig_figs(Isa.specific_weight_from_geopotential(h1), 5),
"air-number-density" =>
round_to_sig_figs(
Isa.air_number_density_from_geopotential(h1), 5
),
"mean-speed" =>
Isa.mean_air_particle_speed_from_geopotential(h1).round(2),
"frequency" =>
round_to_sig_figs(
Isa.air_particle_collision_frequency_from_geopotential(h1), 5
),
"mean-free-path" =>
round_to_sig_figs(
Isa.mean_free_path_of_air_particles_from_geopotential(h1), 5
),
}
end
dict_to_yaml(d, filename)
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/BlockLength
# rubocop:enable Metrics/MethodLength
end
end
end

0 comments on commit 92bb4f6

Please sign in to comment.