Class: StandardsData

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#standards_dataObject

Returns the value of attribute standards_data.



21
22
23
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 21

def standards_data
  @standards_data
end

Instance Method Details

#excel_to_json(xlsx_file = 'standards.xlsx', output_folder = File.dirname(__FILE__)) ⇒ Object



85
86
87
88
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 85

def excel_to_json(xlsx_file = 'standards.xlsx', output_folder = File.dirname(__FILE__))
  self.extract_excel_tables(xlsx_file, output_folder)
  self.extract_excel_constants_and_formulas(xlsx_file, output_folder)
end

#get_standards_constant(name) ⇒ Object



23
24
25
26
27
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 23

def get_standards_constant(name)
  object = @standards_data['constants'].detect {|constant| constant['name'] == name}
  raise("could not find #{name} in standards constants database. ") if object.nil? or object['value'].nil?
  return object['value']
end

#get_standards_formula(name) ⇒ Object



29
30
31
32
33
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 29

def get_standards_formula(name)
  object = @standards_data['formulas'].detect {|formula| formula['name'] == name}
  raise("could not find #{name} in standards formual database. ") if object.nil? or object['value'].nil?
  return object['value']
end

#get_standards_table_row(table_name, search_criteria = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 35

def get_standards_table_row(table_name, search_criteria = nil)
  return_objects = nil
  object = @standards_data['tables'].detect {|table| table['name'] == table_name}
  raise("could not find #{table_name} in standards table database. ") if object.nil? or object['table'].nil?
  if search_criteria.nil?
    return object['table']
  else
    return_objects = self.model_find_object(object['table'], search_criteria)
    return return_object
  end
end

#get_standards_table_rows(table_name, search_criteria = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 47

def get_standards_table_rows(table_name, search_criteria = nil)
  return_objects = nil
  object = @standards_data['tables'].detect {|table| table['name'] == table_name}
  raise("could not find #{table_name} in standards table database. ") if object.nil? or object['table'].nil?
  if search_criteria.nil?
    return object['table']
  else
    return_objects = self.model_find_objects(object['table'], search_criteria)
    return return_objects
  end
end

#json_to_excel(data_folder = File.dirname(__FILE__), xlsx_file = 'standards.xlsx') ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 60

def json_to_excel(data_folder = File.dirname(__FILE__), xlsx_file = 'standards.xlsx')
  standards_data = self.load_standards_data(data_folder)
  workbook = RubyXL::Workbook.new
  workbook.worksheets.delete(workbook['Sheet1'])
  #Write Constants Sheet.
  self.array_of_hashes_to_excel_sheet(workbook.add_worksheet('constants'), standards_data['constants'])
  self.array_of_hashes_to_excel_sheet(workbook.add_worksheet('formulas'), standards_data['formulas'])
  standards_data['tables'].each do |table|
    sheet = workbook.add_worksheet(table['name'])
    row = 0
    table.each do |key, value|
      unless key == 'table'
        sheet.add_cell(row, 0, key).change_font_bold(true)
        sheet.add_cell(row, 1, value)
        row += 1
      end
    end
    row += 1
    sheet.add_cell(row, 0, 'Table').change_font_bold(true)
    self.array_of_hashes_to_excel_sheet(sheet, table['table'], (row + 1))
  end
  workbook.write(xlsx_file)
  return xlsx_file
end

#load_standards_data(datafolder = File.dirname(__FILE__)) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 90

def load_standards_data(datafolder = File.dirname(__FILE__))
  @standards_data = {}
  if __dir__[0] == ':' # Running from OpenStudio CLI
    files = embedded_files_relative('./', /.*\.json/)
    files.each do |file|
      @standards_data = standards_data.deep_merge(JSON.parse(EmbeddedScripting.getFileAsString(file)))
    end
  else
    files = Dir.glob("#{datafolder}/**/*.json").select {|e| File.file? e}
    files.each do |file|
      @standards_data = standards_data.deep_merge (JSON.parse(File.read(file)))
    end
  end
  @standards_data = @standards_data.sort_by_key(true)
  return @standards_data
end