def test_equality(): """Test the equality of files imported from the same source.""" wea_file = './tests/fixtures/wea/chicago.wea' wea_1 = Wea.from_file(wea_file) wea_2 = Wea.from_file(wea_file) assert wea_1 == wea_2 wea_2.direct_normal_irradiance[12] = 200 assert wea_1 != wea_2
def test_from_file(self): """Test import from wea file.""" wea_file = './tests/wea/san_francisco_10min.wea' with pytest.raises(Exception): Wea.from_file(wea_file) # wrong timestep wea = Wea.from_file(wea_file, 6) assert wea.direct_normal_radiation[45] == 88 assert wea.diffuse_horizontal_radiation[45] == 1 assert wea.direct_normal_radiation[46] == 313 assert wea.diffuse_horizontal_radiation[46] == 3
def sunpath_from_wea(wea, north, folder, name, log_file, leap_year, reverse_vectors): """Generate a climate-based sunpath from a Wea file. This command also generates a mod file which includes all the modifiers in sunpath. mod file is usually used with rcontrib command to indicate the list of modifiers. Since rcontrib command has a hard limit of 10,000 modifiers in a single run the files will be broken down into multiple files if number of modifiers is more than 10000 modifiers. wea: Path to a wea file. """ try: wea = Wea.from_file(wea) sp = Sunpath(wea.location, north) hoys = wea.hoys sp_files = sp.to_file(folder, name, wea=wea, hoys=hoys, leap_year=leap_year, reverse_vectors=reverse_vectors) files = [{ 'path': os.path.relpath(path, folder), 'full_path': path } for path in sp_files['suns']] log_file.write(json.dumps(files)) except Exception: _logger.exception('Failed to generate sunpath.') sys.exit(1) else: sys.exit(0)
def sky_matrix(directory): wea = './scripts/wea/denver.wea' wea = Wea.from_file(wea) sky_mtx = SkyMatrix(wea) sky_mtx.north = 10 dest_file = os.path.join(directory, 'sky_matrix.json') with open(dest_file, 'w') as fp: json.dump(sky_mtx.to_dict(), fp, indent=4)
def test_dict_to_object_sky_mtx(): """Test the dict_to_object method with SkyMatrix objects.""" wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sky_mtx_obj = SkyMatrix(wea) sky_mtx_dict = sky_mtx_obj.to_dict() new_sky_mtx = dict_to_object(sky_mtx_dict) assert isinstance(new_sky_mtx, SkyMatrix)
def tets_to_radiance_options(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sky_mtx = SkyMatrix(wea, density=4) assert '-m 4' in sky_mtx.to_radiance() assert '-O1' in sky_mtx.to_radiance(output_type=1) assert '-A' in sky_mtx.to_radiance(cumulative=True) assert '-d' in sky_mtx.to_radiance(components=1) assert '-s' in sky_mtx.to_radiance(components=2)
def test_to_file(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sun_mtx = SunMatrix(wea) sun_mtx_file = sun_mtx.to_file('./tests/assets/temp', mkdir=True) assert os.path.isfile(sun_mtx_file) with open(sun_mtx_file, 'r') as skyf: content = skyf.read() assert sun_mtx.to_radiance() in str(content)
def test_from_monthly_average(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sky1 = ClimateBased.from_wea_monthly_average(wea, 1, 8) epw = './tests/assets/epw/denver.epw' epw = EPW(epw) sky2 = ClimateBased.from_epw_monthly_average(epw, 1, 8) assert sky1.direct_normal_irradiance == sky2.direct_normal_irradiance assert sky1.diffuse_horizontal_irradiance == sky2.diffuse_horizontal_irradiance
def test_equality(): """Test the equality of files imported from the same source.""" wea_file = './tests/assets/wea/chicago.wea' wea_1 = Wea.from_file(wea_file) wea_2 = wea_1.duplicate() assert wea_1 == wea_2 wea_2.direct_normal_irradiance[12] = 200 assert wea_1 != wea_2 assert Wea.count_timesteps(wea_file) == 8760
def test_dict_methods(): """Test JSON serialization methods""" # test first with a continuous annual Wea epw_path = './tests/assets/epw/chicago.epw' wea = Wea.from_epw_file(epw_path) assert wea.to_dict() == Wea.from_dict(wea.to_dict()).to_dict() # then test with a filtered Wea including datetimes in the dict wea_file = './tests/assets/wea/chicago_filtered.wea' wea = Wea.from_file(wea_file) assert wea.to_dict() == Wea.from_dict(wea.to_dict()).to_dict()
def test_to_and_from_dict(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sun_mtx = SunMatrix(wea) sun_mtx_from_dict = sun_mtx.from_dict(sun_mtx.to_dict()) # update this once Wea class has equality method implemeneted assert sun_mtx.wea.location == sun_mtx_from_dict.wea.location assert sun_mtx.wea.direct_normal_irradiance.values == \ sun_mtx_from_dict.wea.direct_normal_irradiance.values assert sun_mtx.wea.direct_normal_irradiance.datetimes == \ sun_mtx_from_dict.wea.direct_normal_irradiance.datetimes
def test_check_defaults(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sun_mtx = SunMatrix(wea) assert sun_mtx.wea == wea assert sun_mtx.north == 0 assert sun_mtx.location == wea.location sun_mtx_radiance = sun_mtx.to_radiance() assert 'gendaymtx -n -D suns.mtx' in sun_mtx_radiance assert '-M suns.mod' in sun_mtx_radiance assert '-O1 in.wea' in sun_mtx_radiance assert sun_mtx.is_climate_based is True assert sun_mtx.is_point_in_time is False
def test_from_file(): """Test import from wea file.""" wea_file = './tests/assets/wea/san_francisco_10min.wea' with pytest.raises(Exception): Wea.from_file(wea_file) # wrong timestep wea = Wea.from_file(wea_file, 6) assert isinstance(wea.direct_normal_irradiance, HourlyContinuousCollection) assert isinstance(wea.diffuse_horizontal_irradiance, HourlyContinuousCollection) assert wea.is_annual assert wea.is_continuous assert wea.direct_normal_irradiance[45] == 69 assert wea.diffuse_horizontal_irradiance[45] == 1 assert wea.direct_normal_irradiance[46] == 137 assert wea.diffuse_horizontal_irradiance[46] == 7 direct, diff = wea.get_irradiance_value(1, 1, 8) assert direct == 273 assert diff == 20 direct, diff = wea.get_irradiance_value_for_hoy(8) assert direct == 273 assert diff == 20
def test_check_defaults(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sky_mtx = SkyMatrix(wea) assert sky_mtx.wea == wea assert sky_mtx.north == 0 assert sky_mtx.density == 1 assert sky_mtx.location == wea.location sky_mtx_radiance = sky_mtx.to_radiance() # gendaymtx -O0 in.wea > sky.mtx assert '> sky.mtx' in sky_mtx_radiance assert '-O0 in.wea' in sky_mtx_radiance assert sky_mtx.is_climate_based is True assert sky_mtx.is_point_in_time is False
def solar_tracking(folder, sun_up_hours, wea, north, tracking_increment, sub_folder): """Postprocess a list of result folders to account for dynamic solar tracking. \b This function essentially takes .ill files for each state of a dynamic tracking system and produces a single .ill file that models the tracking behavior. \b Args: folder: Results folder containing sub-folders that each represent a state of the dynamic solar tracking system. Each sub-folder should contain .ill files for that state and the names of these .ill files should be the same across all sub-folders. sun_up_hours: The .txt file containing the sun-up hours that were simulated. wea: The .wea file that was used in the simulation. This will be used to determine the solar positions. """ try: # load all of the result sub-folders in the folder and sort them models = [f for f in os.listdir(folder) if os.path.isdir(os.path.join(folder, f)) and os.path.isfile(os.path.join(folder, f, 'grids_info.json'))] model_num = [int(''.join([i for i in f if i.isdigit()])) for f in models] sorted_models = [x for _, x in sorted(zip(model_num, models))] models = [os.path.join(folder, f) for f in sorted_models] dest_folder = os.path.join(folder, sub_folder) if len(models) == 1: # not a dynamic system; just copy the files if not os.path.isdir(dest_folder): os.mkdir(dest_folder) for f in os.listdir(models[0]): shutil.copyfile( os.path.join(models[0], f), os.path.join(dest_folder, f)) else: wea_obj = Wea.from_file(wea) post_process_solar_tracking( models, sun_up_hours, wea_obj.location, north, tracking_increment, dest_folder) except Exception: _logger.exception('Failed to compute irradiance metrics.') sys.exit(1) else: sys.exit(0)
def test_from_file_discontinuous(): """Test import from wea file with discontinuous data.""" wea_file = './tests/assets/wea/chicago_filtered.wea' wea = Wea.from_file(wea_file) assert isinstance(wea.direct_normal_irradiance, HourlyDiscontinuousCollection) assert isinstance(wea.diffuse_horizontal_irradiance, HourlyDiscontinuousCollection) assert not wea.is_annual assert not wea.is_continuous assert len(wea) == 4427 assert wea.datetimes[0].hour == 7 with pytest.raises(ValueError): direct, diff = wea.get_irradiance_value(1, 1, 5) # non-existent date direct, diff = wea.get_irradiance_value(1, 1, 8) assert direct == 397 assert diff == 47 direct, diff = wea.get_irradiance_value_for_hoy(8) assert direct == 397 assert diff == 47
def test_from_wea(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sky = ClimateBased.from_wea(wea, 1, 1, 8) assert sky.direct_normal_irradiance == 68 assert sky.diffuse_horizontal_irradiance == 87
def test_equality(): wea_file = './tests/fixtures/wea/chicago.wea' wea_1 = Wea.from_file(wea_file) wea_2 = Wea.from_file(wea_file) assert wea_1 == wea_2
def test_north(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sun_mtx = SunMatrix(wea, 120) assert sun_mtx.north == 120 assert '-r 120' in sun_mtx.to_radiance()
def tets_to_radiance_options(): wea = './tests/assets/wea/denver.wea' wea = Wea.from_file(wea) sun_mtx = SunMatrix(wea) assert '-O0' in sun_mtx.to_radiance(output_type=0)
def leed_illuminance(wea, north, folder, name, log_file): """Generate two climate-based lear skies for LEED v4.1 Daylight Option 2. This involves evaluating the input TMY Wea, finding the clearest day within 15 days of September and March, and using that to generate skies at 9AM and 3PM. \b Args: wea: Path to a Typical Meteorological Year (TMY) .wea file. The file must be annual with a timestep of 1 for a non-leap year. """ try: # get HOYs for the time around the equinoxes mar_9, sep_9 = DateTime(3, 21, 9).hoy, DateTime(9, 21, 9).hoy mar_3, sep_3 = DateTime(3, 21, 15).hoy, DateTime(9, 21, 15).hoy hoys_mar9 = list( range(int(mar_9 - (15 * 24)), int(mar_9 + (15 * 24)), 24)) hoys_sep9 = list( range(int(sep_9 - (15 * 24)), int(sep_9 + (15 * 24)), 24)) hoys_mar3 = list( range(int(mar_3 - (15 * 24)), int(mar_3 + (15 * 24)), 24)) hoys_sep3 = list( range(int(sep_3 - (15 * 24)), int(sep_3 + (15 * 24)), 24)) # analyze the Wea file to get the sunniest days around the equinoxes wea_obj = Wea.from_file(wea) dni = wea_obj.direct_normal_irradiance dhi = wea_obj.diffuse_horizontal_irradiance irr_mar9 = [ x for _, x in sorted(zip([dni[h] for h in hoys_mar9], hoys_mar9)) ] irr_sep9 = [ x for _, x in sorted(zip([dni[h] for h in hoys_sep9], hoys_sep9)) ] irr_mar3 = [ x for _, x in sorted(zip([dni[h] for h in hoys_mar3], hoys_mar3)) ] irr_sep3 = [ x for _, x in sorted(zip([dni[h] for h in hoys_sep3], hoys_sep3)) ] # create the clear sky objects from the averaged irradiance dni_9 = (dni[irr_mar9[-1]] + dni[irr_sep9[-1]]) / 2 dhi_9 = (dhi[irr_mar9[-1]] + dhi[irr_sep9[-1]]) / 2 dni_3 = (dni[irr_mar3[-1]] + dni[irr_sep3[-1]]) / 2 dhi_3 = (dhi[irr_mar3[-1]] + dhi[irr_sep3[-1]]) / 2 sky_obj_9 = hbsky.ClimateBased.from_location(wea_obj.location, 3, 21, 9, dni_9, dhi_9, north) sky_obj_3 = hbsky.ClimateBased.from_location(wea_obj.location, 3, 21, 15, dni_3, dhi_3, north) # write out the sky files and the log file output_9 = sky_obj_9.to_file(folder, '{}9AM.sky'.format(name), True) output_3 = sky_obj_3.to_file(folder, '{}3PM.sky'.format(name), True) files = [{ 'id': '{}9AM'.format(name), 'path': os.path.relpath(output_9, folder), 'full_path': output_9 }, { 'id': '{}3PM'.format(name), 'path': os.path.relpath(output_3, folder), 'full_path': output_3 }] log_file.write(json.dumps(files)) except Exception: _logger.exception('Failed to create LEED skies.') sys.exit(1)