Class: MesaReader::MesaData

Inherits:
Object
  • Object
show all
Defined in:
lib/mesa_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, scrub = true, dbg = false) ⇒ MesaData

Returns a new instance of MesaData.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

Add magic methods



156
157
158
159
160
161
162
163
# 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

Instance Attribute Details

#bulk_dataObject (readonly)

Returns the value of attribute bulk_data.



39
40
41
# File 'lib/mesa_reader.rb', line 39

def bulk_data
  @bulk_data
end

#bulk_namesObject (readonly)

Returns the value of attribute bulk_names.



39
40
41
# File 'lib/mesa_reader.rb', line 39

def bulk_names
  @bulk_names
end

#data_hashObject (readonly)

Returns the value of attribute data_hash.



39
40
41
# File 'lib/mesa_reader.rb', line 39

def data_hash
  @data_hash
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



39
40
41
# File 'lib/mesa_reader.rb', line 39

def file_name
  @file_name
end

#header_dataObject (readonly)

Returns the value of attribute header_data.



39
40
41
# File 'lib/mesa_reader.rb', line 39

def header_data
  @header_data
end

#header_hashObject (readonly)

Returns the value of attribute header_hash.



39
40
41
# File 'lib/mesa_reader.rb', line 39

def header_hash
  @header_hash
end

#header_namesObject (readonly)

Returns the value of attribute header_names.



39
40
41
# File 'lib/mesa_reader.rb', line 39

def header_names
  @header_names
end

Instance Method Details

#data(key) ⇒ Object



83
84
85
86
87
88
89
# 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) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/mesa_reader.rb', line 91

def data?(key)
  bulk_names.include?(key)
end

#data_at_model_number(key, n) ⇒ Object



99
100
101
102
103
104
105
# 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) ⇒ Object



75
76
77
78
79
80
81
# 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) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/mesa_reader.rb', line 95

def header?(key)
  header_names.include?(key)
end

#where(*keys) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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