class MesaReader::MesaData

Attributes

bulk_data[R]
bulk_names[R]
data_hash[R]
file_name[R]
header_data[R]
header_hash[R]
header_names[R]

Public Class Methods

new(filename, scrub = true, dbg = false) click to toggle source
# File lib/mesa_reader.rb, line 41
def initialize(filename, scrub = true, dbg = false)
  # In this context, rows start at 1, not 0. These can and should be
  # changed if MESA conventions change.

  header_names_row = 2
  header_data_row = header_names_row + 1
  bulk_names_row = 6
  bulk_data_start_row = bulk_names_row + 1

  @file_name = filename
  @header_names = read_one_line(file_name, header_names_row).chomp.split
  @header_data = read_one_line(file_name, header_data_row).chomp.split
  @header_hash = {}
  header_names.each_index do |i|
    if header_data[i].include?(".")
      new_entry = header_data[i].to_f
    else
      new_entry = header_data[i].to_i
    end
    @header_hash[header_names[i]] = new_entry
  end
  @bulk_names = read_one_line(file_name, bulk_names_row).chomp.split
  @bulk_data = Array.new(bulk_names.size)
  0.upto(bulk_names.length-1) do |i|
    @bulk_data[i] = Dobjects::Dvector.new
  end
  Dobjects::Dvector.read(file_name, @bulk_data, bulk_data_start_row)
  @data_hash = {}
  bulk_names.each do |name|
    @data_hash[name] = bulk_data[bulk_names.index(name)]
  end
  remove_backups(dbg) if (data?('model_number') and scrub)
end

Public Instance Methods

data(key) click to toggle source
# File lib/mesa_reader.rb, line 83
def data(key)
  if data?(key)
    data_hash[key]
  else
    puts "WARNING: Couldn't find column #{key} in #{file_name}."
  end
end
data?(key) click to toggle source
# File lib/mesa_reader.rb, line 91
def data?(key)
  bulk_names.include?(key)
end
data_at_model_number(key, n) click to toggle source
# File lib/mesa_reader.rb, line 99
def data_at_model_number(key, n)
  if data?(key)
    data_hash[key][index_of_model_number(n)]
  else
    puts "WARNING: Couldn't find column #{key} in #{file_name}."
  end
end
header(key) click to toggle source
# File lib/mesa_reader.rb, line 75
def header(key)
  if header?(key)
    header_hash[key]
  else
    puts "WARNING: Couldn't find header #{key} in #{file_name}."
  end
end
header?(key) click to toggle source
# File lib/mesa_reader.rb, line 95
def header?(key)
  header_names.include?(key)
end
where(*keys) { |*params| ... } click to toggle source
# File lib/mesa_reader.rb, line 107
def where(*keys)
  keys.each do |key|
    raise "#{key} not a recognized data category." unless data?(key)
  end
  unless block_given?
    raise "Must provide a block for WHERE to test values of provided keys."
  end
  selected_indices = Array.new
  data(keys[0]).each_index do |i|
    params = keys.map { |key| data(key)[i] }
    selected_indices << i if yield(*params)
  end
  puts "WARNING: No model numbers/grid points met the selection critera " +
    "given. Returning an empty array." if selected_indices.empty?
  return selected_indices    
end

Private Instance Methods

index_of_model_number(n) click to toggle source
# File lib/mesa_reader.rb, line 147
def index_of_model_number(n)
  raise "No 'model_number' data heading found in #{file_name}.
     Cannot match to model number #{n}." unless data?('model_number')
  raise "No such model number: #{n.to_f} in column 'model_number' of file
    #{file_name}." unless data('model_number').include?(n.to_f)
  data('model_number').index(n.to_f)
end
method_missing(name, *args) click to toggle source

Add magic methods

Calls superclass method
# File lib/mesa_reader.rb, line 156
def method_missing(name, *args)
  if bulk_names.include?(name.to_s) 
    return data(name.to_s)
  elsif header_names.include?(name.to_s)
    return header(name.to_s)
  end
  return super
end
read_one_line(file_name, line_number) click to toggle source
# File lib/mesa_reader.rb, line 125
def read_one_line(file_name, line_number)
  File.open(file_name) do |file|
    current_line = 1
    file.each_line do |line|
      return line if line_number == current_line
      current_line += 1
    end
  end
end
remove_backups(dbg) click to toggle source
# File lib/mesa_reader.rb, line 134
def remove_backups(dbg)
  # make a list of the ones to be removed
  lst = []
  n = data('model_number').length
  (n-1).times do |k|
    lst << k if data('model_number')[k] >= data('model_number')[k+1..-1].min
  end
  return if lst.length == 0
  puts "remove #{lst.length} models because of backups" if dbg
  lst = lst.sort
  @bulk_data.each { |vec| vec.prune!(lst) }
  nil
end