Class: BTAP::Database

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/openstudio-standards/btap/costing/database.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Database

Returns a new instance of Database.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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/openstudio-standards/btap/costing/database.rb', line 9

def initialize
  @db = Hash.new # Stores all database information

  # Load the database from the individual CSV/JSON files
  @db["costs"]                = []
  @db["localization_factors"] = [] # Local costing factors
  @db["raw"]                  = {}
  @db["constructions"]        = {}
  @db["db_errors"]            = []

  # Load costing data
  data_costs = CSV.read(Paths.costs_path)

  1.upto data_costs.length - 1 do |i|
    row = data_costs[i]
    index = row.each
    item = Hash.new
    item["baseCosts"] = Hash.new
    costs = item["baseCosts"]

    item["id"]               = index.next
    item["sheet"]            = index.next
    item["source"]           = index.next
    item["description"]      = index.next
    item["city"]             = index.next
    item["province_state"]   = index.next
    costs["materialOpCost"]  = index.next.to_f
    costs["laborOpCost"]     = index.next.to_f
    costs["equipmentOpCost"] = index.next.to_f

    @db["costs"] << item
  end

  # Load the localization factors
  data_factors = CSV.read(Paths.costs_local_factors_path)

  1.upto data_factors.length - 1 do |i|
    row = data_factors[i]
    index = row.each
    item = Hash.new

    item["province_state"] = index.next
    item["city"]           = index.next
    item["division"]       = index.next
    item["code_prefix"]    = index.next
    item["material"]       = index.next.to_f
    item["installation"]   = index.next.to_f
    item["total"]          = index.next.to_f

    @db["localization_factors"] << item
  end

  # Load the construction data
  @db["constructions"] = File.open(Paths::CONSTRUCTIONS_PATH) { |file| JSON.load(file) }

  # Load the raw data
  costing_file_names = Paths::COSTING_DATA_PATHS.map { |path| path.basename(".*").to_s }
  0.upto(costing_file_names.length - 1) do |i|
    data_path = BTAP::Paths::COSTING_DATA_PATHS[i]
    unless File.exist?(data_path)
      raise("Error: Could not find #{data_path}")
    end
    @db['raw'][costing_file_names[i]] = CSV.read(data_path, headers: true).map { |row| row.to_hash}
  end
end

Instance Method Details

#[](element) ⇒ Object

Overload the element of operator for database accesses



95
96
97
# File 'lib/openstudio-standards/btap/costing/database.rb', line 95

def [](element)
  @db[element]
end

#[]=(element, value) ⇒ Object

Overload the element assignment operator for inputting additional data



100
101
102
# File 'lib/openstudio-standards/btap/costing/database.rb', line 100

def []=(element, value)
  @db[element] = value
end

#validate_ahu_items_and_quantities ⇒ Object

This method verifies that, for a given row the number of items listed in the 'id_layers' column is the same as the number of quantities listed in the 'Id_layers_quantity_multipliers' column in the 'hvac_vent_ahu' sheet in the costing spreadsheet. If there is a difference in the number of items and number of quantities in a row then that row needs to be investigated and fixed.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/openstudio-standards/btap/costing/database.rb', line 81

def validate_ahu_items_and_quantities()
  # Find out if there are a different number of items and number oof quantities in any row of the 'hvac_vent_ahu'
  # sheet.
  diff_id_quantities = @db['raw']['hvac_vent_ahu'].select{|data| data['id_layers'].to_s.split(',').size != data['Id_layers_quantity_multipliers'].to_s.split(',').size}
  # If there is a difference (that is the diff_id_quantities has something in it) then raise an error.
  unless diff_id_quantities.empty?
    puts "Errors in the hvac_vent_ahu Costing Table.  The number of id_layers does not match the number of"
    puts "Id_layers_quantity_multipliers for the following item(s):"
    puts JSON.pretty_generate(diff_id_quantities)
    raise("costing spreadsheet validation failed")
  end
end