lib/openstudio-standards/standards/standard.rb in openstudio-standards-0.2.9 vs lib/openstudio-standards/standards/standard.rb in openstudio-standards-0.2.10.rc1

- old
+ new

@@ -32,10 +32,11 @@ # standard = Standard.build('NECB2011') def self.build(name) if STANDARDS_LIST[name].nil? raise "ERROR: Did not find a class called '#{name}' to create in #{JSON.pretty_generate(STANDARDS_LIST)}" end + OpenStudio.logFree(OpenStudio::Info, 'openstudio.standards.standard', "Using OpenStudio Standards version #{OpenstudioStandards::VERSION} with template #{name}.") return STANDARDS_LIST[name].new end # set up template class variable. def intialize @@ -48,93 +49,97 @@ # @return [String] returns the lookup name as a string # @todo Unify the lookup names and eliminate this method def model_get_lookup_name(building_type) lookup_name = building_type case building_type - when 'SmallOffice' - lookup_name = 'Office' - when 'SmallOfficeDetailed' - lookup_name = 'Office' - when 'MediumOffice' - lookup_name = 'Office' - when 'MediumOfficeDetailed' - lookup_name = 'Office' - when 'LargeOffice' - lookup_name = 'Office' - when 'LargeOfficeDetailed' - lookup_name = 'Office' - when 'RetailStandalone' - lookup_name = 'Retail' - when 'RetailStripmall' - lookup_name = 'StripMall' - when 'Office' - lookup_name = 'Office' + when 'SmallOffice' + lookup_name = 'Office' + when 'SmallOfficeDetailed' + lookup_name = 'Office' + when 'MediumOffice' + lookup_name = 'Office' + when 'MediumOfficeDetailed' + lookup_name = 'Office' + when 'LargeOffice' + lookup_name = 'Office' + when 'LargeOfficeDetailed' + lookup_name = 'Office' + when 'RetailStandalone' + lookup_name = 'Retail' + when 'RetailStripmall' + lookup_name = 'StripMall' + when 'Office' + lookup_name = 'Office' end return lookup_name end - # Loads the default openstudio standards dataset. + # Loads the openstudio standards dataset for this standard. + # For standards subclassed from other standards, the lowest-level + # data will override data supplied at a higher level. + # For example, data from ASHRAE 90.1-2004 will be overriden by + # data from ComStock ASHRAE 90.1-2004. # # @return [Hash] a hash of standards data - def load_standards_database - standards_files = [] - standards_files << 'OpenStudio_Standards_boilers.json' - standards_files << 'OpenStudio_Standards_chillers.json' - standards_files << 'OpenStudio_Standards_climate_zone_sets.json' - standards_files << 'OpenStudio_Standards_climate_zones.json' - standards_files << 'OpenStudio_Standards_construction_properties.json' - standards_files << 'OpenStudio_Standards_construction_sets.json' - standards_files << 'OpenStudio_Standards_constructions.json' - standards_files << 'OpenStudio_Standards_curves.json' - standards_files << 'OpenStudio_Standards_fans.json' - standards_files << 'OpenStudio_Standards_ground_temperatures.json' - standards_files << 'OpenStudio_Standards_heat_pumps_heating.json' - standards_files << 'OpenStudio_Standards_heat_pumps.json' - standards_files << 'OpenStudio_Standards_materials.json' - standards_files << 'OpenStudio_Standards_motors.json' - standards_files << 'OpenStudio_Standards_prototype_inputs.json' - standards_files << 'OpenStudio_Standards_schedules.json' - standards_files << 'OpenStudio_Standards_space_types.json' - standards_files << 'OpenStudio_Standards_templates.json' - standards_files << 'OpenStudio_Standards_unitary_acs.json' - standards_files << 'OpenStudio_Standards_heat_rejection.json' - standards_files << 'OpenStudio_Standards_exterior_lighting.json' - standards_files << 'OpenStudio_Standards_parking.json' - standards_files << 'OpenStudio_Standards_entryways.json' - standards_files << 'OpenStudio_Standards_necb_climate_zones.json' - standards_files << 'OpenStudio_Standards_necb_fdwr.json' - standards_files << 'OpenStudio_Standards_necb_hvac_system_selection_type.json' - standards_files << 'OpenStudio_Standards_necb_surface_conductances.json' - standards_files << 'OpenStudio_Standards_water_heaters.json' - standards_files << 'OpenStudio_Standards_economizers.json' - standards_files << 'OpenStudio_Standards_refrigerated_cases.json' - standards_files << 'OpenStudio_Standards_walkin_refrigeration.json' - standards_files << 'OpenStudio_Standards_refrigeration_compressors.json' - standards_files << 'OpenStudio_Standards_hvac_inference.json' - standards_files << 'OpenStudio_Standards_size_category.json' - standards_files << 'OpenStudio_Standards_elevators.json' - # standards_files << 'OpenStudio_Standards_unitary_hps.json' - # Combine the data from the JSON files into a single hash - top_dir = File.expand_path('../../..', File.dirname(__FILE__)) - standards_data_dir = "#{top_dir}/data/standards" + def load_standards_database(data_directories = []) + OpenStudio.logFree(OpenStudio::Info, 'openstudio.standards.standard', "Loading OpenStudio Standards data for #{template}") @standards_data = {} - standards_files.sort.each do |standards_file| - temp = '' + + # Load the JSON files from each directory + data_directories.each do |data_dir| if __dir__[0] == ':' # Running from OpenStudio CLI - temp = load_resource_relative("../../../data/standards/#{standards_file}", 'r:UTF-8') + OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.standard', "Loading JSON files from OpenStudio CLI embedded directory #{data_dir}") + EmbeddedScripting.allFileNamesAsString.split(';').each do |file| + # Skip files outside of the specified directory + next unless file.start_with?("#{data_dir}/data") + + # Skip files that are not JSON + next unless File.basename(file).match(/.*\.json/) + + # Read the JSON file + data = JSON.parse(EmbeddedScripting.getFileAsString(file)) + data.each_pair do |key, objs| + # Override the template in inherited files to match the instantiated template + objs.each do |obj| + if obj.has_key?('template') + obj['template'] = template + end + end + if @standards_data[key].nil? + OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.standard', "Adding #{key} from #{File.basename(file)}") + else + OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.standard', "Overriding #{key} with #{File.basename(file)}") + end + @standards_data[key] = objs + end + end else - File.open("#{standards_data_dir}/#{standards_file}", 'r:UTF-8') do |f| - temp = f.read + OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.standard', "Loading JSON files from #{data_dir}") + files = Dir.glob("#{data_dir}/data/*.json").select {|e| File.file? e} + files.each do |file| + data = JSON.parse(File.read(file)) + data.each_pair do |key, objs| + # Override the template in inherited files to match the instantiated template + objs.each do |obj| + if obj.has_key?('template') + obj['template'] = template + end + end + if @standards_data[key].nil? + OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.standard', "Adding #{key} from #{File.basename(file)}") + else + OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.standard', "Overriding #{key} with #{File.basename(file)}") + end + @standards_data[key] = objs + end end end - file_hash = JSON.parse(temp) - @standards_data = @standards_data.merge(file_hash) end # Check that standards data was loaded if @standards_data.keys.size.zero? - OpenStudio.logFree(OpenStudio::Error, 'OpenStudio Standards JSON data was not loaded correctly.') + OpenStudio.logFree(OpenStudio::Error, 'openstudio.standards.standard', "OpenStudio Standards JSON data was not loaded correctly for #{template}.") end return @standards_data end end