def save_uwg_file(self, uwg_file_path=None): """Write the properties of the RunManager to a .uwg file. Args: uwg_file_path: Full file path to the .uwg file that you want to write. The default is set to go to an URBAN folder in the same directory as the existing rural EPW. Returns: uwg_file_path: The file path to the .uwg file. """ start_folder, epw_name = os.path.split(self._epw_file) epw_name = epw_name.replace('.epw', '') if uwg_file_path is None: end_folder = '{}\\URBAN\\'.format(start_folder) name = '{}_URBAN.uwg'.format(epw_name) else: end_folder, name = os.path.split(uwg_file_path) if not name.lower().endswith('.uwg'): name = name + '.uwg' write_to_file_by_name(end_folder, name, self.uwg_file_string, True) uwg_file_path = os.path.join(end_folder, name) print('.uwg file successfully written to: {}'.format(uwg_file_path)) return uwg_file_path
def _write_dynamic_shade_files(folder, sub_folder, group, minimal=False): """Write out the files that need to go into any dynamic model folder. Args: folder: The model folder location on this machine. sub_folder: The sub-folder for the three files (relative to the model folder). group: A DynamicShadeGroup object to be written into files. minimal: Boolean noting whether radiance strings should be written minimally. Returns: A list of dictionaries to be written into the states.json file. """ # destination folder for all of the radiance files dest = os.path.join(folder, sub_folder) # loop through all states and write out the .rad files for them states_list = group.states_json_list for state_i, file_names in enumerate(states_list): default_str = group.to_radiance(state_i, direct=False, minimal=minimal) direct_str = group.to_radiance(state_i, direct=True, minimal=minimal) write_to_file_by_name(dest, file_names['default'].replace('./', ''), default_str) write_to_file_by_name(dest, file_names['direct'].replace('./', ''), direct_str) return states_list
def to_files(self, folder, count, base_name=None, mkdir=False): """Split this sensor grid and write them to several files. Args: folder: Target folder. count: Number of files. base_name: Optional text for a unique base_name for sensor files. (Default: self.display_name) mkdir: A boolean to indicate if the folder should be created in case it doesn't exist already (Default: False). Returns: A list of dicts containing the grid name, path to the grid and full path to the grid. """ count = typing.int_in_range(count, 1, input_name='file count') base_name = base_name or self.display_name if count == 1 or self.count == 0: name = '%s_0000' % base_name full_path = self.to_file(folder, name, mkdir) return [ {'name': name if not name.endswith('.pts') else name.replace('.pts', ''), 'path': name + '.pts' if not name.endswith('.pts') else name, 'full_path': full_path, 'count': self.count} ] # calculate sensor count in each file sc = int(round(self.count / count)) sensors = iter(self._sensors) for fc in range(count - 1): name = '%s_%04d.pts' % (base_name, fc) content = '\n'.join((next(sensors).to_radiance() for _ in range(sc))) futil.write_to_file_by_name(folder, name, content + '\n', mkdir) # write whatever is left to the last file name = '%s_%04d.pts' % (base_name, count - 1) content = '\n'.join((sensor.to_radiance() for sensor in sensors)) futil.write_to_file_by_name(folder, name, content + '\n', mkdir) grids_info = [] for c in range(count): name = '%s_%04d' % (base_name, c) path = '%s.pts' % name full_path = os.path.join(folder, path) grids_info.append({ 'name': name, 'path': path, 'full_path': full_path, 'count': sc }) # adjust the count for the last grid grids_info[-1]['count'] = self.count - sc * (count - 1) return grids_info
def to_file(self, folder, name=None, hoys=None, mkdir=False): """Write matrix to a Radiance file. This method also writes the wea information to a .wea file. Args: folder: Target folder. name: File name. hoys: Optional list of hoys to filter the hours of the wea. If None, this object's wea will be used as-is. Note that you may not want to use this input if this object's wea is not annual since an exception will be raised if a given hoy is not found in the wea. (Default: None). mkdir: A boolean to note if the directory should be created if doesn't exist (default: False). Returns: Full path to the newly created file. """ name = typing.valid_string(name) if name \ else '%s.rad' % self.__class__.__name__.lower() # write wea file first wea_file = self.write_wea(folder, hoys=hoys) content = self.to_radiance(wea_file=os.path.split(wea_file)[-1]) return futil.write_to_file_by_name(folder, name, '!' + content, mkdir)
def to_file(self, folder, file_name=None, mkdir=False, ignore_group=False): """Write this sensor grid to a Radiance sensors file. Args: folder: Target folder. If grid is part of a sensor group identifier it will be written to a subfolder with group identifier name. file_name: Optional file name without extension. (Default: self.identifier) mkdir: A boolean to indicate if the folder should be created in case it doesn't exist already. (Default: False). ignore_group: A boolean to indicate if creating a new subfolder for sensor group should be ignored. (Default: False). Returns: Full path to newly created file. """ identifier = file_name or self.identifier + '.pts' if not identifier.endswith('.pts'): identifier += '.pts' if not ignore_group and self.group_identifier: folder = os.path.normpath( os.path.join(folder, self.group_identifier)) mkdir = True # in most cases the subfolder does not exist already return futil.write_to_file_by_name(folder, identifier, self.to_radiance() + '\n', mkdir)
def to_file(self, folder='.', name=None, mkdir=False): """Write ground to a .ground Radiance file. Returns: Full path to the newly created file. """ content = self.to_radiance() + '\n' name = typing.valid_string(name) if name else 'ground.rad' return futil.write_to_file_by_name(folder, name, content, mkdir)
def _write_mtx_files(folder, sub_folder, group, states_json_list, minimal=False): """Write out the mtx files needed for 3-phase simulation into a model folder. Args: folder: The model folder location on this machine. sub_folder: The sub-folder for the three files (relative to the model folder). group: A DynamicSubFaceGroup object to be written into files. states_json_list: A list to be written into the states.json file. minimal: Boolean noting whether radiance strings should be written minimally. Returns: A list of dictionaries to be written into the states.json file. """ dest = os.path.join(folder, sub_folder) # destination folder for radiance files # check if all of the states of all of the vmtx and dmtx geometry are default one_mtx = all(st.mtxs_default for obj in group.dynamic_objects for st in obj.properties.radiance._states) if one_mtx: # if they're all default, we can use one file mtx_file = './{}..mtx.rad'.format(group.identifier) # loop through all states and write out the .rad files for them tmxt_valid = False for state_i, st_dict in enumerate(states_json_list): tmtx_bsdf = group.tmxt_bsdf(state_i) if tmtx_bsdf is not None: # it's a valid state for 3-phase tmxt_valid = True # add the tmxt to the states_json_list bsdf_name = os.path.split(tmtx_bsdf.bsdf_file)[-1] states_json_list[state_i]['tmtx'] = bsdf_name # add the vmtx and the dmtx to the states_json_list if one_mtx: states_json_list[state_i]['vmtx'] = mtx_file states_json_list[state_i]['dmtx'] = mtx_file else: states_json_list[state_i]['vmtx'] = \ './{}..vmtx..{}.rad'.format(group.identifier, str(state_i)) states_json_list[state_i]['dmtx'] = \ './{}..dmtx..{}.rad'.format(group.identifier, str(state_i)) vmtx_str = group.vmtx_to_radiance(state_i, minimal) dmtx_str = group.dmtx_to_radiance(state_i, minimal) write_to_file_by_name( dest, states_json_list[state_i]['vmtx'].replace('./', ''), vmtx_str) write_to_file_by_name( dest, states_json_list[state_i]['dmtx'].replace('./', ''), dmtx_str) # write the single mtx file if everything is default if one_mtx and tmxt_valid: mtx_str = group.vmtx_to_radiance(0, minimal) write_to_file_by_name(dest, mtx_file, mtx_str)
def find_max_cooling_des_day(des_days, sim_par, base_strs): """Find the cooling design day with the highest coincident peak load.""" # create sizing parameters with all of the design days sim_par_dup = sim_par.duplicate() sim_par_dup.output.outputs = None for dy in des_days: sim_par_dup.sizing_parameter.add_design_day(dy) # write the IDF and run the sizing calculation idf_str_init = '\n\n'.join([sim_par_dup.to_idf()] + base_strs) idf = os.path.join(directory, 'in.idf') write_to_file_by_name(directory, 'in.idf', idf_str_init, True) sql, zsz, rdd, html, err = run_idf(idf, silent=True) # determine the design day with the highest peak using the sizing results sql_obj = SQLiteResult(sql) d_day_dict = {d_day.name.upper(): [0, d_day] for d_day in des_days} peak_cool_dict = {} for zs in sql_obj.zone_cooling_sizes: d_day_dict[zs.design_day_name][0] += zs.calculated_design_load peak_cool_dict[zs.zone_name] = zs.calculated_design_load day_loads = list(d_day_dict.values()) day_loads.sort(key=lambda y: y[0]) return [day_loads[-1][1]], peak_cool_dict
def to_file(self, folder, name=None, mkdir=False): """Write sky hemisphere to a sky_hemisphere.rad Radiance file. Args: folder: Target folder. name: File name. mkdir: A boolean to note if the directory should be created if doesn't exist (default: False). Returns: Full path to the newly created file. """ content = self.to_radiance() name = typing.valid_string(name) if name else 'skydome.rad' return futil.write_to_file_by_name(folder, name, content, mkdir)
def adjust_sky_for_metric(sky, metric, folder, name): """Adjust a sky file to ensure it is suitable for a given metric. Specifcally, this ensures that skies being created with gendaylit have a -O option that aligns with visible vs. solar energy. \b Args: sky: Path to a .sky file to be adjusted based on the metric. """ try: with open(sky) as inf: content = inf.read() if content.startswith('!gendaylit'): split_content = content.split('\n') first_line = split_content[0].replace('-O 0', '-O 1') if metric in \ ('irradiance', 'radiance') else split_content[0].replace('-O 1', '-O 0') split_content[0] = first_line content = '\n'.join(split_content) name = '{}.sky'.format(metric) if name is None else name write_to_file_by_name(folder, name, content, True) except Exception: _logger.exception('Failed to adjust sky.') sys.exit(1)
def to_file(self, folder, file_name=None, mkdir=False): """Write this sensor grid to a Radiance sensors file. Args: folder: Target folder. file_name: Optional file name without extension. (Default: self.display_name) mkdir: A boolean to indicate if the folder should be created in case it doesn't exist already (Default: False). Returns: Full path to newly created file. """ display_name = file_name or self.display_name + '.pts' if not display_name.endswith('.pts'): display_name += '.pts' return futil.write_to_file_by_name( folder, display_name, self.to_radiance() + '\n', mkdir)
def to_file(self, folder, file_name=None, mkdir=False): """Save view to a file. Args: folder: Target folder. file_name: Optional file name without extension (Default: self.identifier). mkdir: A boolean to indicate if the folder should be created in case it doesn't exist already (Default: False). Returns: Full path to newly created file. """ identifier = file_name or self.identifier + '.vf' # add rvu before the view itself content = 'rvu ' + self.to_radiance() return futil.write_to_file_by_name(folder, identifier, content, mkdir)
def to_file(self, folder, name=None, mkdir=False): """Write sky hemisphere to a sky_hemisphere.rad Radiance file. Args: folder: Target folder. name: File name. mkdir: A boolean to note if the directory should be created if doesn't exist (default: False). Returns: Full path to the newly created file. """ content = self.to_radiance() name = typing.valid_string(name) if name \ else '%.3f_%.3f_%d_%d.sky' % ( self.altitude, self.azimuth, self.direct_normal_irradiance, self.diffuse_horizontal_irradiance ) return futil.write_to_file_by_name(folder, name, content, mkdir)
'\nis not as accurate as design days from DDYs distributed with the EPW.' give_warning(ghenv.Component, msg) print msg # create the strings for simulation paramters and model ver_str = energyplus_idf_version() if energy_folders.energyplus_version \ is not None else energyplus_idf_version(compatibe_ep_version) sim_par_str = _sim_par_.to_idf() model_str = _model.to.idf(_model, schedule_directory=sch_directory, patch_missing_adjacencies=True) idf_str = '\n\n'.join([ver_str, sim_par_str, model_str]) # write the final string into an IDF idf = os.path.join(directory, 'in.idf') write_to_file_by_name(directory, 'in.idf', idf_str, True) # run the IDF through EnergyPlus silent = True if _run == 1 else False sql, zsz, rdd, html, err = run_idf(idf, _epw_file, silent=silent) if html is None and err is not None: # something went wrong; parse the errors err_obj = Err(err) print(err_obj.file_contents) for error in err_obj.fatal_errors: raise Exception(error) # parse the result sql and get the monthly data collections if os.name == 'nt': # we are on windows; use IronPython like usual sql_obj = SQLiteResult(sql) cool_init = sql_obj.data_collections_by_output_name(cool_out) heat_init = sql_obj.data_collections_by_output_name(heat_out)
def _write_static_files(folder, sub_folder, file_id, geometry, geometry_blk, modifiers, modifiers_blk, mod_combs, mod_names, punched_verts=False, minimal=False): """Write out the three files that need to go into any static radiance model folder. This includes a .rad, .mat, and .blk file for the folder. This method will also catch any cases of BSDF modifiers and copy the XML files to the bsdf folder of the model folder. Args: folder: The model folder location on this machine. sub_folder: The sub-folder for the three files (relative to the model folder). file_id: An identifier to be used for the names of each of the files. geometry: A list of geometry objects all with default blk modifiers. geometry_blk: A list of geometry objects with overridden blk modifiers. modifiers: A list of modifiers to write. modifiers_blk: A list of modifier_blk to write. mod_combs: Dictionary of modifiers from _unique_modifier_blk_combinations method. mod_names: Modifier names from the _unique_modifier_blk_combinations method. punched_verts: Boolean noting whether punched geometry should be written minimal: Boolean noting whether radiance strings should be written minimally. """ if len(geometry) != 0 or len(geometry_blk) != 0: # write the strings for the geometry face_strs = [] if punched_verts: for face in geometry: modifier = face.properties.radiance.modifier rad_poly = Polygon(face.identifier, face.punched_vertices, modifier) face_strs.append(rad_poly.to_radiance(minimal, False, False)) for face, mod_name in zip(geometry_blk, mod_names): modifier = mod_combs[mod_name][0] rad_poly = Polygon(face.identifier, face.punched_vertices, modifier) face_strs.append(rad_poly.to_radiance(minimal, False, False)) else: for face in geometry: modifier = face.properties.radiance.modifier rad_poly = Polygon(face.identifier, face.vertices, modifier) face_strs.append(rad_poly.to_radiance(minimal, False, False)) for face, mod_name in zip(geometry_blk, mod_names): modifier = mod_combs[mod_name][0] rad_poly = Polygon(face.identifier, face.vertices, modifier) face_strs.append(rad_poly.to_radiance(minimal, False, False)) # write the strings for the modifiers mod_strs = [] mod_blk_strs = [] for mod in modifiers: if isinstance(mod, BSDF): _process_bsdf_modifier(mod, mod_strs, minimal) else: mod_strs.append(mod.to_radiance(minimal)) for mod in modifiers_blk: if isinstance(mod, BSDF): _process_bsdf_modifier(mod, mod_strs, minimal) else: mod_blk_strs.append(mod.to_radiance(minimal)) # write the three files for the model sub-folder dest = os.path.join(folder, sub_folder) write_to_file_by_name(dest, '{}.rad'.format(file_id), '\n\n'.join(face_strs)) write_to_file_by_name(dest, '{}.mat'.format(file_id), '\n\n'.join(mod_strs)) write_to_file_by_name(dest, '{}.blk'.format(file_id), '\n\n'.join(mod_blk_strs))
def split_view(view, count, skip_overture, octree, rad_params, folder, log_file): """Split a radiance view file into smaller views based on count. \b Args: view: Full path to input sensor view file. count: Maximum number of sensors in new files. The number will be rounded to closest round number for each file. For example if the input file has 21 sensors and input count is set to 5 this command will generate 4 files where the first three files will have 5 sensors and the last file will have 6. """ try: # split the view into smaller views view_obj = View.from_file(view) views = view_obj.grid(y_div_count=count) views_info = [] for c, v in enumerate(views): name = '%s_%04d' % (view_obj.identifier, c) path = '%s.vf' % name full_path = os.path.join(folder, path) v.to_file(folder, path, mkdir=True) views_info.append({ 'name': name, 'path': path, 'full_path': full_path }) # create the ambient cache file if specified amb_file = os.path.basename(view).replace('.vf', '.amb') if not skip_overture: options = RpictOptions() if rad_params: options.update_from_string(rad_params.strip()) # overwrite default image size to be small for the ambient cache (64 x 64) options.x = 64 options.y = 64 options.af = amb_file # create command and run it to get the .amb file assert octree is not None, \ 'Octree must be specified for an overture calculation.' out_file = os.path.basename(view).replace('.vf', '.unf') rpict = Rpict(options=options, output=out_file, octree=octree, view=view) env = None if folders.env != {}: env = folders.env env = dict(os.environ, **env) if env else None rpict.run(env=env, cwd=folder) os.remove(os.path.join(folder, out_file)) else: # write a dummy ambient file so that queenbee does not crash write_to_file_by_name(folder, amb_file, '') # record all of the view files that were generated log_file.write(json.dumps(views_info)) except Exception: _logger.exception('Failed to split view file.') sys.exit(1) else: sys.exit(0)
raise ImportError('\nFailed to import lbt_recipes:\n\t{}'.format(e)) check_radiance_date() if all_required_inputs(ghenv.Component): # set defaults and process the sky input _size_ = 500 if _size_ is None else _size_ if isinstance(_sky, str): # convert the sky string into a sky object _sky = string_to_sky(_sky) sky_content = _sky.to_radiance(1) if isinstance( _sky, ClimateBased) else _sky.to_radiance() # set up the paths for the various files used in translation sky_dir = os.path.join(folders.default_simulation_folder, 'sky_visualiztion') sky_file, sky_oct = 'weather.sky', 'sky_visual.oct' write_to_file_by_name(sky_dir, sky_file, sky_content, mkdir=True) ghi_res, full_ghi_res = 'ghi.res', os.path.join(sky_dir, 'ghi.res') init_hdr, final_hdr = 'sky_init.HDR', 'sky.HDR' hdr = os.path.join(sky_dir, final_hdr) if os.path.isfile(hdr): os.remove(hdr) # build up the commands to render the image of the sky oconv = Oconv(inputs=[sky_file], output=sky_oct) oconv.options.f = True rpict = Rpict(octree=sky_oct, output=init_hdr) rpict.options.i = True rpict.options.t = 10 rpict.options.ab = 1 rpict.options.ad = 1000