Class: EnergyPlus::EpImfFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ EpImfFile

Returns a new instance of EpImfFile.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/energyplus/EpImfFile.rb', line 25

def initialize(path)
  puts 'parsing ' + path
  @path = path.expand_path
  @text = ''
  @locations = []

  if @path.exist?
    File.open(@path) do |f|
      @text << f.read
      parse
    end
  end
end

Instance Attribute Details

#locationsObject

Returns the value of attribute locations.



23
24
25
# File 'lib/energyplus/EpImfFile.rb', line 23

def locations
  @locations
end

Instance Method Details

#location_sort(x, y) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/energyplus/EpImfFile.rb', line 77

def location_sort(x,y)
  if x[0].include?('ShowHeader')
    return -1
  elsif y[0].include?('ShowHeader')
    return 1
  else
    return x[0] <=> y[0]
  end
end

#parseObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/energyplus/EpImfFile.rb', line 50

def parse
  this_text = @text

  locations = Hash.new

  regex = /##def.*?##enddef/im
  while match_data = this_text.match(regex)
    #Grab the name of the location
    regex2 = /##def (.*?)\[\]$/im

    location_name = match_data.to_s.match(regex2)[1].lstrip.rstrip

    #remove the header/footer from imf snippet
    regex3 = /##def (.*?)\[\]\n*/im
    regex4 = /##enddef(.*)\n*/im
    imf_data = match_data.to_s.gsub(regex3, "").gsub(regex4, "")

    #clean up the IMF (which is really idf data at this point)
    imf_data.gsub!(/^(  )/,"")

    location = [location_name, imf_data]
    @locations.push(location)

    this_text = match_data.post_match
  end
end

#savefileas(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/energyplus/EpImfFile.rb', line 39

def savefileas(path)
  File.open(path, "w") do |f|
    @locations.each do |loc|
      f << "##def " + loc[0] + "[]\n"
      f << loc[1]
      f << "##enddef\n\n"
  end
  end

end

#sortObject



87
88
89
# File 'lib/energyplus/EpImfFile.rb', line 87

def sort
  @locations.sort! {|x,y| location_sort(x,y)}
end