Class: Inlist
- Inherits:
-
Object
- Object
- Inlist
- Defined in:
- lib/mesa_script.rb
Class Attribute Summary collapse
-
.d_files ⇒ Object
Returns the value of attribute d_files.
-
.d_paths ⇒ Object
Returns the value of attribute d_paths.
-
.have_data ⇒ Object
Returns the value of attribute have_data.
-
.inlist_data ⇒ Object
Returns the value of attribute inlist_data.
-
.namelists ⇒ Object
Returns the value of attribute namelists.
-
.nt_files ⇒ Object
Returns the value of attribute nt_files.
-
.nt_paths ⇒ Object
Returns the value of attribute nt_paths.
Instance Attribute Summary collapse
-
#data_hash ⇒ Object
Making an instance of Inlist first checks to see if the class methods are set up for the namelists in Inlist.namelists.
-
#names ⇒ Object
readonly
Returns the value of attribute names.
Class Method Summary collapse
-
.f_end ⇒ Object
Determine proper file suffix for fortran source.
- .full_line(lines, i) ⇒ Object
-
.get_data ⇒ Object
Generate methods for the Inlist class that set various namelist parameters.
-
.get_defaults(temp_data, namelist, d_filename = nil, whine = false) ⇒ Object
Similar to Inlist.get_names_and_types, but takes the output of Inlist.get_names_and_types and assigns defaults and orders to each item.
-
.get_namelist_data(namelist, nt_filename = nil, d_filename = nil) ⇒ Object
Reads names and types for a specified namelist from given file (intended to be of the form of something like star/private/star_controls.inc).
- .get_names_and_types(namelist, nt_filenames = nil) ⇒ Object
- .has_comment?(line) ⇒ Boolean
-
.have_data? ⇒ Boolean
Checks to see if the data/methods for the Inlist class has been initialized.
-
.inlist_to_mesascript(inlist_file, script_file, dbg = false) ⇒ Object
Converts a standard inlist to its equivalent mesascript formulation.
- .is_blank?(line) ⇒ Boolean
- .is_comment?(line) ⇒ Boolean
-
.make_inlist(name = 'inlist', &block) ⇒ Object
Create an Inlist object, execute block of commands that presumably populate the inlist, then write the inlist to a file with the given name.
-
.make_parentheses_method(datum) ⇒ Object
Three ways to access array categories.
-
.make_regular_method(datum) ⇒ Object
Two ways to access/change scalars.
-
.parse_input(name, value, type) ⇒ Object
Ensure provided value’s data type matches expected data type.
-
.version ⇒ Object
Get access to current MESA version.
Instance Method Summary collapse
- #flag_command(name) ⇒ Object
-
#flagged ⇒ Object
Marks a data category so that it can be staged into an inlist.
-
#initialize ⇒ Inlist
constructor
A new instance of Inlist.
-
#make_fresh_writelist ⇒ Object
Zeroes out all staged data and blank lines.
- #namelists ⇒ Object
-
#stage_flagged ⇒ Object
Collects all data categories into a hash of arrays (each array is a namelist) that is read whenever the inlist is converted to a string (i.e. when it is printed to a file or the screen).
- #stage_namelist_command(name) ⇒ Object
-
#to_s ⇒ Object
Takes the staged data categories and formats them into a string series of namelists that are MESA-readable.
- #unflag_command(name) ⇒ Object
Constructor Details
#initialize ⇒ Inlist
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/mesa_script.rb', line 541 def initialize Inlist.get_data unless Inlist.have_data? @data = Inlist.inlist_data @data_hash = {} @data.each_value do |namelist_data| namelist_data.each do |datum| @data_hash[datum.name] = datum.dup end end @names = @data_hash.keys @data = {} Inlist.namelists.each do |namelist| @data[namelist] = Array.new(Inlist.inlist_data[namelist].size, '') end end |
Class Attribute Details
.d_files ⇒ Object
Returns the value of attribute d_files.
60 61 62 |
# File 'lib/mesa_script.rb', line 60 def d_files @d_files end |
.d_paths ⇒ Object
Returns the value of attribute d_paths.
60 61 62 |
# File 'lib/mesa_script.rb', line 60 def d_paths @d_paths end |
.have_data ⇒ Object
Returns the value of attribute have_data.
59 60 61 |
# File 'lib/mesa_script.rb', line 59 def have_data @have_data end |
.inlist_data ⇒ Object
Returns the value of attribute inlist_data.
60 61 62 |
# File 'lib/mesa_script.rb', line 60 def inlist_data @inlist_data end |
.namelists ⇒ Object
Returns the value of attribute namelists.
60 61 62 |
# File 'lib/mesa_script.rb', line 60 def namelists @namelists end |
.nt_files ⇒ Object
Returns the value of attribute nt_files.
60 61 62 |
# File 'lib/mesa_script.rb', line 60 def nt_files @nt_files end |
.nt_paths ⇒ Object
Returns the value of attribute nt_paths.
60 61 62 |
# File 'lib/mesa_script.rb', line 60 def nt_paths @nt_paths end |
Instance Attribute Details
#data_hash ⇒ Object
Making an instance of Inlist first checks to see if the class methods are set up for the namelists in Inlist.namelists. If they aren’t ready, it creates them. Then creates a hash with an array associated to each namelist that is the exact size of the number of entries available in that namelist.
539 540 541 |
# File 'lib/mesa_script.rb', line 539 def data_hash @data_hash end |
#names ⇒ Object (readonly)
Returns the value of attribute names.
540 541 542 |
# File 'lib/mesa_script.rb', line 540 def names @names end |
Class Method Details
.f_end ⇒ Object
Determine proper file suffix for fortran source
13 14 15 16 17 18 19 |
# File 'lib/mesa_script.rb', line 13 def self.f_end if Inlist.version >= 7380 "f90" else "f" end end |
.full_line(lines, i) ⇒ Object
517 518 519 520 |
# File 'lib/mesa_script.rb', line 517 def self.full_line(lines, i) return lines[i] unless lines[i][-1] == '&' [lines[i].sub('&', ''), full_line(lines, i+1)].join(' ') end |
.get_data ⇒ Object
Generate methods for the Inlist class that set various namelist parameters.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mesa_script.rb', line 65 def self.get_data Inlist.namelists.each do |namelist| @inlist_data[namelist] = Inlist.get_namelist_data(namelist, Inlist.nt_files[namelist], Inlist.d_files[namelist]) end # create methods (interface) for each data category @inlist_data.each_value do |namelist_data| namelist_data.each do |datum| if datum.is_arr Inlist.make_parentheses_method(datum) else Inlist.make_regular_method(datum) end end end # don't do this nonsense again unles specifically told to do so Inlist.have_data = true end |
.get_defaults(temp_data, namelist, d_filename = nil, whine = false) ⇒ Object
Similar to Inlist.get_names_and_types, but takes the output of Inlist.get_names_and_types and assigns defaults and orders to each item. Looks for this information in the specified defaults filename.
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/mesa_script.rb', line 462 def self.get_defaults(temp_data, namelist, d_filename = nil, whine = false) d_filename ||= namelist + '.defaults' d_full_path = Inlist.d_paths[namelist] + d_filename raise "Couldn't find file #{d_filename}" unless File.exists?(d_full_path) contents = File.readlines(d_full_path) contents.reject! { |line| is_comment?(line) or is_blank?(line) } contents.map! do |line| my_line = line.dup if has_comment?(line) my_line = my_line[0...my_line.index('!')] end raise "Equal sign missing in line:\n\t #{my_line}\n in file " + "#{full_path}." unless my_line =~ /=/ my_line.strip! end pairs = contents.map {|line| line.split('=').map {|datum| datum.strip}} n_d_hash = {} n_o_hash = {} pairs.each_with_index do |pair, i| name = pair[0] default = pair[1] if name =~ /\(.*\)/ selector = name[/\(.*\)/][1..-2] name.sub!(/\(.*\)/, '') if selector.include?(':') default = Hash.new(default) elsif selector.count(',') == 0 default = {selector.to_i => default} else selector = selector.split(',').map { |index| index.strip.to_i } default = default = {selector => default} end end if n_d_hash[name].is_a?(Hash) n_d_hash[name].merge!(default) else n_d_hash[name] = default end n_o_hash[name] ||= i end temp_data.each do |datum| unless n_d_hash.keys.include?(datum.name) puts "WARNING: no default found for control #{datum.name}. Using standard defaults." if whine end default = n_d_hash[datum.name] if default.is_a?(Hash) and datum.value.is_a?(Hash) datum.value = datum.value.merge(default) else datum.value = default || datum.value end datum.order = n_o_hash[datum.name] || datum.order end temp_data end |
.get_namelist_data(namelist, nt_filename = nil, d_filename = nil) ⇒ Object
Reads names and types for a specified namelist from given file (intended to be of the form of something like star/private/star_controls.inc).
Returns an array of InlistItem Struct instances that contain a parameter’s name, type (:bool, :string, :float, :int, or :type), the namelist it belongs to, and its relative ordering in that namelist. Bogus defaults are assigned according to the object’s type, and the ordering is unknown.
373 374 375 376 |
# File 'lib/mesa_script.rb', line 373 def self.get_namelist_data(namelist, nt_filename = nil, d_filename = nil) temp_data = Inlist.get_names_and_types(namelist, nt_filename) Inlist.get_defaults(temp_data, namelist, d_filename) end |
.get_names_and_types(namelist, nt_filenames = nil) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/mesa_script.rb', line 378 def self.get_names_and_types(namelist, nt_filenames = nil) nt_filenames ||= Inlist.nt_files[namelist] unless nt_filenames.respond_to?(:each) nt_filenames = [nt_filenames] end nt_full_paths = nt_filenames.map { |file| Inlist.nt_paths[namelist] + file } namelist_data = [] nt_full_paths.each do |nt_full_path| unless File.exists?(nt_full_path) raise "Couldn't find file #{nt_full_path}" end contents = File.readlines(nt_full_path) # Throw out comments and blank lines, ensure remaining lines are a proper # Fortran assignment, then remove leading and trailing white space contents.reject! { |line| is_comment?(line) or is_blank?(line) } contents.map! do |line| my_line = line.dup my_line = my_line[0...my_line.index('!')] if has_comment?(my_line) my_line.strip! end full_lines = [] contents.each_with_index do |line, i| break if line =~ /\A\s*contains/ next unless line =~ /::/ full_lines << Inlist.full_line(contents, i) end pairs = full_lines.map do |line| line.split('::').map { |datum| datum.strip} end pairs.each do |pair| type = case pair[0] when /logical/ then :bool when /character/ then :string when /real/ then :float when /integer/ then :int when /type/ then :type else raise "Couldn't determine type of entry #{pair[0]} in " + "#{nt_full_path}." end name_chars = pair[1].split('') names = [] paren_level = 0 name_chars.each do |char| if paren_level > 0 and char == ',' names << '!' next elsif char == '(' paren_level += 1 elsif char == ')' paren_level -= 1 end names << char end names = names.join.split(',').map { |name| name.strip } names.each do |name| is_arr = false num_indices = 0 if name =~ /\(.*\)/ is_arr = true num_indices = name.count('!') + 1 name.sub!(/\(.*\)/, '') elsif pair[0] =~ /dimension\((.*)\)/i is_arr = true num_indices = $1.count(',') + 1 end type_default = {:bool => false, :string => '', :float => 0.0, :int => 0} dft = is_arr ? Hash.new(type_default[type]) : type_default[type] namelist_data << InlistItem.new(name, type, dft, namelist, -1, is_arr, num_indices) end end end namelist_data end |
.has_comment?(line) ⇒ Boolean
530 531 532 |
# File 'lib/mesa_script.rb', line 530 def self.has_comment?(line) line.include?('!') end |
.have_data? ⇒ Boolean
Checks to see if the data/methods for the Inlist class has been initialized.
361 362 363 |
# File 'lib/mesa_script.rb', line 361 def self.have_data? @have_data end |
.inlist_to_mesascript(inlist_file, script_file, dbg = false) ⇒ Object
Converts a standard inlist to its equivalent mesascript formulation. Comments are preserved and namelist separators are converted to comments. Note that comments do NOT get put back into the fortran inlist through mesascript. Converting an inlist to mesascript and then back again will clean up and re-order your inlist, but all comments will be lost. All other information SHOULD remain intact.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/mesa_script.rb', line 270 def self.inlist_to_mesascript(inlist_file, script_file, dbg = false) Inlist.get_data unless Inlist.have_data # ensure we have inlist data inlist_contents = File.readlines(inlist_file) # make namelist separators comments new_contents = inlist_contents.map do |line| case line when /^\s*&/ then '# ' + line.chomp # start namelist when /^\s*\// then '# ' + line.chomp # end namelist else line.sub('!', '#').chomp # fix comments end end new_contents.map! do |line| if line =~ /^\s*#/ or line.strip.empty? # leave comments and blanks result = line else if dbg puts "parsing line:" puts line end comment_pivot = line.index('#') if comment_pivot command = line[0...comment_pivot] comment = line[comment_pivot..-1].to_s.strip else command = line comment = '' end command =~ /(^\s*)/ # save leading space leading_space = Regexp.last_match(1) command =~ /(\s*$)/ # save buffer space buffer_space = Regexp.last_match(1) command.strip! # remove white space name, value = command.split('=').map { |datum| datum.strip } if dbg puts "name: #{name}" puts "value: #{value}" end if name =~ /\((\d+)\)/ # fix 1D array assignments name.sub!('(', '[') name.sub!(')', ']') name = name + ' =' elsif name =~ /\((\s*\d+\s*,\s*)+\d\s*\)/ # fix multi-D arrays # arrays become hashes in MesaScript, so rather than having multiple # indices, the key becomes the array of indices themselves, hence # the double braces replacing single parentheses name.sub!('(', '[[') name.sub!(')', ']]') name = name + ' =' end name.downcase! if value =~ /'.*'/ or value =~ /".*"/ result = name + ' ' + value # leave strings alone elsif %w[.true. .false.].include?(value.downcase) result = name + ' ' + value.downcase.gsub('.', '') # fix booleans elsif value =~ /\d+\.?\d*([eEdD]\d+)?/ result = name + ' ' + value.downcase.sub('d', 'e') # fix floats else result = name + ' ' + value # leave everything else alone end result = leading_space + result + buffer_space + comment if dbg puts "parsed to:" puts result puts '' end end result end File.open(script_file, 'w') do |f| f.puts "require 'mesa_script'" f.puts '' f.puts "Inlist.make_inlist('#{File.basename(inlist_file)}') do" new_contents.each { |line| f.puts ' ' + line } f.puts "end" end end |
.is_blank?(line) ⇒ Boolean
526 527 528 |
# File 'lib/mesa_script.rb', line 526 def self.is_blank?(line) not (line =~/[a-z0-9]+/) end |
.is_comment?(line) ⇒ Boolean
522 523 524 |
# File 'lib/mesa_script.rb', line 522 def self.is_comment?(line) line =~ /\A\s*!/ end |
.make_inlist(name = 'inlist', &block) ⇒ Object
Create an Inlist object, execute block of commands that presumably populate the inlist, then write the inlist to a file with the given name. This is the money routine with user-supplied commands in the instance_eval block.
353 354 355 356 357 358 |
# File 'lib/mesa_script.rb', line 353 def self.make_inlist(name = 'inlist', &block) inlist = Inlist.new inlist.instance_eval(&block) inlist.stage_flagged File.open(name, 'w') { |f| f.write(inlist) } end |
.make_parentheses_method(datum) ⇒ Object
Three ways to access array categories. All methods will cause the data category to be staged into your inlist, even if you do not change it Basically, if it appears in your mesascript, it will definitely appear in your inlist. A command can be unflagged by calling ‘unflag_command(’COMMAND_NAME’)‘ where COMMAND_NAME is the case-sensitive name of the command to be unflagged.
-
Standard array way like
xa_lower_limit_species[1] = 'h1'(note square braces, NOT parentheses). Returns new value.
-
Just access (and flag), but don’t change via array access, like
xa_lower_limit_species[1](again, note square braces). Returns current value
-
No braces method, like
xa_lower_limit_species() # flags and returns hash of values xa_lower_limit_species # same, but more ruby-esque xa_lower_limit_species(1) # flags and returns value 1 xa_lower_limit_species 1 # Same xa_lower_limit_species(1, 'h1') # flags and sets value 1 xa_lower_limit_species 1, 'h1' # same
For multi-dimensional arrays, things are even more vaired. You can treat them like 1-dimensional arrays with the “index” just being an array of indices, for instance:
text_summary1_name[[1,2]] = 'star_mass' # flags ALL values and sets
text_summary1_name([1,2], 'star_mass') # text_summary1_name(1,2)
text_summary1_name [1,2], 'star_mass # to 'star_mass'
text_summary1_name [1,2] # flags ALL values and
text_summary1_name([1,2]) # returns
# text_sumarry_name(1,2)
text_summary_name() # flags ALL values and
text_summary_name # returns entire hash for
# text_summary_name
Alternatively, can use the more intuitive form where indices are separate and don’t need to be in an array, but this only works with the parentheses versions (i.e. the first option directly above has no counterpart):
text_summary1_name(1, 2, 'star_mass')
text_summary1_name 1, 2, 'star_mass' # same as above (first 3)
text_summary1_name
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/mesa_script.rb', line 132 def self.make_parentheses_method(datum) name = datum.name num_indices = datum.num_indices define_method(name + '[]=') do|arg1, arg2| if num_indices > 1 raise "First argument of #{name}[]= (part in brackets) must be an array with #{num_indices} indices since #{name} is a multi-dimensional array." unless (arg1.is_a?(Array) and arg1.length == num_indices) end self.flag_command(name) self.data_hash[name].value[arg1] = arg2 end define_method(name + '[]') do |arg| if num_indices > 1 raise "Argument of #{name}[] (part in brackets) must be an array with #{num_indices} indices since #{name} is a multi-dimensional array." unless (arg.is_a?(Array) and arg.length == num_indices) end self.flag_command(name) self.data_hash[name].value[arg] end define_method(name) do |*args| self.flag_command(name) case args.length when 0 then self.data_hash[name].value when 1 if num_indices > 1 raise "First argument of #{name} must be an array with #{num_indices} indices since #{name} is a multi-dimensional array OR must provide all indices as separate arguments." unless (args[0].is_a?(Array) and args[0].length == num_indices) end self.data_hash[name].value[args[0]] when 2 if num_indices == 1 and (not args[0].is_a?(Array)) self.data_hash[name].value[args[0]] = args[1] elsif num_indices == 2 and (not args[0].is_a?(Array)) and args[1].is_a?(Fixnum) self.data_hash[name].value[args] elsif num_indices > 1 raise "First argument of #{name} must be an array with #{num_indices} indices since #{name} is a multi-dimensional array OR must provide all indices as separate arguments." unless (args[0].is_a?(Array) and args[0].length == num_indices) self.data_hash[name].value[args[0]] = args[1] else raise "First argument of #{name} must be an array with #{num_indices} indices since #{name} is a multi-dimensional array OR must provide all indices as separate arguments. The optional final argument is what the #{name} would be set to. Omission of this argument will simply flag #{name} to appear in the inlist." end when num_indices self.data_hash[name].value[args] when num_indices + 1 raise "Bad arguments for #{name}. Either provide an array of #{num_indices} indices for the first argument or provide each index in succession, optionally specifying the desired value for the last argument." if args[0].is_a?(Array) self.data_hash[name].value[args[0..-2]] = args[-1] else raise "Wrong number of arguments for #{name}. Can provide zero arguments (just flag command), one argument (array of indices for multi-d array or one index for 1-d array), two arguments (array of indices/single index for multi-/1-d array and a new value for the value), #{num_indices} arguments where the elements themselves are the right indices (returns the specified element of the array), or #{num_indices + 1} arguments to set the specific value and return it." end end alias_method name.downcase.to_sym, name.to_sym alias_method (name.downcase + '[]').to_sym, (name + '[]').to_sym alias_method (name.downcase + '[]=').to_sym, (name + '[]=').to_sym end |
.make_regular_method(datum) ⇒ Object
Two ways to access/change scalars. All methods will cause the data category to be staged into your inlist, even if you do not change the value. Basically, if it appears in your mesascript, it will definitely appear in your inlist.
-
Change value, like
initial_mass(1.0) initial_mass 1.0This flags the category to go in your inlist and changes the value. There is no difference between these two syntaxes (it’s built into ruby). Returns new value.
-
Just access, like
initial_mass() initial_massThis flags the category, but does not change the value. Again, both syntaxes are allowed, though the one without parentheses is more traditional for ruby (why do you want empty parentheses anyway?). Returns current value.
A command can be unflagged by calling ‘unflag_command(’COMMAND_NAME’)‘ where COMMAND_NAME is the case-sensitive name of the command to be unflagged.
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/mesa_script.rb', line 207 def self.make_regular_method(datum) name = datum.name define_method(name) do |*args| self.flag_command(name) return self.data_hash[name].value if args.empty? self.data_hash[name].value = args[0] end aliases = [(name + '=').to_sym, (name.downcase + '=').to_sym, name.downcase.to_sym] aliases.each { |ali| alias_method ali, name.to_sym } end |
.parse_input(name, value, type) ⇒ Object
Ensure provided value’s data type matches expected data type. Then convert to string for printing to an inlist. If value is a string, change nothing (no protection). If value is a string and SHOULD be a string, wrap it in single quotes.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/mesa_script.rb', line 225 def self.parse_input(name, value, type) if value.class == String if type == :string value = "'#{value}'" unless value[0] == "'" and value[-1] == "'" end return value elsif type == :bool unless [TrueClass, FalseClass].include?(value.class) raise "Invalid value for namelist item #{name}: #{value}. Use " + "'.true.', '.false.', or a Ruby boolean (true/false)." end if value == true return '.true.' elsif value == false return '.false.' else raise "Error converting value #{value} of #{name} to a boolean." end elsif type == :int raise "Invalid value for namelist item #{name}: #{value}. Must provide"+ " an int or float." unless value.is_a?(Integer) or value.is_a?(Float) if value.is_a?(Float) puts "WARNING: Expected integer for #{name} but got #{value}. Value" + " will be converted to an integer." end return value.to_i.to_s elsif type == :float raise "Invalid value for namelist item #{name}: #{value}. Must provide " + "an int or float." unless value.is_a?(Integer) or value.is_a?(Float) return sprintf("%g", value).sub('e', 'd') elsif type == :type puts "WARNING: 'type' values are currently unsupported (regarding #{name}) because your humble author has no idea what they look like in an inlist. You should tell him what to do at [email protected]. Your input, #{value}, has been passed through to your inlist verbatim." return value.to_s else raise "Error parsing value for namelist item #{name}: #{value}. Expected " "type was #{type}." end end |
.version ⇒ Object
Get access to current MESA version.
7 8 9 10 |
# File 'lib/mesa_script.rb', line 7 def self.version v_num = IO.read(File.join(ENV['MESA_DIR'], 'data', 'version_number')).to_i return v_num end |
Instance Method Details
#flag_command(name) ⇒ Object
570 571 572 |
# File 'lib/mesa_script.rb', line 570 def flag_command(name) @data_hash[name].flagged = true end |
#flagged ⇒ Object
Marks a data category so that it can be staged into an inlist
603 604 605 |
# File 'lib/mesa_script.rb', line 603 def flagged @data_hash.keys.select { |key| @data_hash[key].flagged } end |
#make_fresh_writelist ⇒ Object
Zeroes out all staged data and blank lines
559 560 561 562 563 564 |
# File 'lib/mesa_script.rb', line 559 def make_fresh_writelist @to_write = {} @data.keys.each do |namelist| @to_write[namelist] = Array.new(@data[namelist].size, '') end end |
#namelists ⇒ Object
566 567 568 |
# File 'lib/mesa_script.rb', line 566 def namelists @data.keys end |
#stage_flagged ⇒ Object
Collects all data categories into a hash of arrays (each array is a namelist) that is read whenever the inlist is converted to a string (i.e. when it is printed to a file or the screen).
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/mesa_script.rb', line 610 def stage_flagged make_fresh_writelist # start from scratch flagged.each { |name| stage_namelist_command(name) } # stage each datum # blank lines between disparate data namelists.each do |namelist| @to_write[namelist].each_index do |i| next if (i == 0 or i == @to_write[namelist].size - 1) this_line = @to_write[namelist][i] prev_line = @to_write[namelist][i-1] this_line = '' if this_line.nil? prev_line = '' if prev_line.nil? if this_line.empty? and not(prev_line.empty? or prev_line == "\n") @to_write[namelist][i] = "\n" end end end end |
#stage_namelist_command(name) ⇒ Object
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/mesa_script.rb', line 578 def stage_namelist_command(name) datum = @data_hash[name] if datum.is_arr lines = @data_hash[name].value.keys.map do |key| prefix = " #{datum.name}(" suffix = ") = " + Inlist.parse_input(datum.name, datum.value[key], datum.type) + "\n" if key.respond_to?(:inject) indices = key[1..-1].inject(key[0].to_s) do |res, elt| "#{res}, #{elt}" end else indices = key.to_s end prefix + indices + suffix end lines = lines.join @to_write[datum.namelist][datum.order] = lines else @to_write[datum.namelist][datum.order] = " " + datum.name + ' = ' + Inlist.parse_input(datum.name, datum.value, datum.type) + "\n" end end |
#to_s ⇒ Object
Takes the staged data categories and formats them into a string series of namelists that are MESA-readable.
633 634 635 636 637 638 639 640 641 |
# File 'lib/mesa_script.rb', line 633 def to_s result = '' namelists.each do |namelist| result += "\n&#{namelist}\n" result += @to_write[namelist].join("") result += "\n/ ! end of #{namelist} namelist\n" end result.sub("\n\n\n", "\n\n") end |
#unflag_command(name) ⇒ Object
574 575 576 |
# File 'lib/mesa_script.rb', line 574 def unflag_command(name) @data_hash[name].flagged = false end |