Class: FesomPossibleVar

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variable_id, unit, description, time_method) ⇒ FesomPossibleVar

Returns a new instance of FesomPossibleVar.



7
8
9
10
11
12
# File 'lib/fesom_possible_var.rb', line 7

def initialize(variable_id, unit, description, time_method)
  @variable_id = variable_id
  @unit = unit
  @description = description
  @time_method = time_method
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/fesom_possible_var.rb', line 5

def description
  @description
end

#time_methodObject (readonly)

Returns the value of attribute time_method.



5
6
7
# File 'lib/fesom_possible_var.rb', line 5

def time_method
  @time_method
end

#unitObject (readonly)

Returns the value of attribute unit.



5
6
7
# File 'lib/fesom_possible_var.rb', line 5

def unit
  @unit
end

#variable_idObject (readonly)

Returns the value of attribute variable_id.



5
6
7
# File 'lib/fesom_possible_var.rb', line 5

def variable_id
  @variable_id
end

Class Method Details

.create_from_fortran_code(code, sort: true) ⇒ Array<FesomPossibleVar>

Generates a collection of FesomPossibleVar objects based upon a FORTRAN code snippet

Maps each line of the argument [String] code via a regex based upon the initialization of this class, using attributes <variable_id>, <description>, and <unit>.

Special handling of the variable “tso”, which uses TimeMethods::POINT instead of the default TimeMethods::Mean

TODO(PG – Reverse Engineering): I still need to find out of the regex needs the parameters or if instead it is used to **figure out what values they should have for initialization**.

NOTE(PG – Reverse Engineering): This looks like the Ruby equivalent of a Python `classmethod` and seems to implement an alternative way of constructing objects of this class.

Parameters:

  • code (String)

    `FORTRAN` code to pare

  • sort (Boolean) (defaults to: true)

    Whether or not to return the variables sorted (alphabetically?) by `variable_id`.

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fesom_possible_var.rb', line 32

def self.create_from_fortran_code(code, sort: true)
  vars = code.split("\n").map do |init_line|
    /.+?, ['"](?<variable_id>[^,]+?)['"], ['"](?<description>.+?)['"], ['"](?<unit>[^,]+?)['"]\) *.*/ =~ init_line
    raise "could not parse all values: variable_id:#{variable_id.inspect}, unit:#{unit.inspect}, description:#{description.inspect}, code:'#{init_line}'" unless [variable_id, unit, description].all?
    if(variable_id == "tso")
      FesomPossibleVar.new variable_id, unit, description, TimeMethods::POINT
    else
      FesomPossibleVar.new variable_id, unit, description, TimeMethods::MEAN
    end
  end
  
  if(sort)
    vars.sort_by {|v| v.variable_id}
  else
    vars
  end
end

Instance Method Details

#to_sString

Helper method to nicely print out this FesomPossibleVar.

Returns:

  • (String)


52
53
54
# File 'lib/fesom_possible_var.rb', line 52

def to_s
  "#{@variable_id}: '#{@unit}' #{@time_method} '#{@description}'"
end