- "code": "\nimport os\nimport subprocess\n\ntry:\n from ladybug.futil import write_to_file\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug:\\n\\t{}'.format(e))\n\ntry:\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry:\n from honeybee_radiance.postprocess.annualdaylight import _process_input_folder\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_radiance:\\n\\t{}'.format(e))\n\ntry:\n from honeybee_radiance_postprocess.dynamic import DynamicSchedule\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_radiance:\\n\\t{}'.format(e))\n\ntry:\n from pollination_handlers.outputs.helper import read_sensor_grid_result\nexcept ImportError as e:\n raise ImportError('\\nFailed to import pollination_handlers:\\n\\t{}'.format(e))\n\ntry:\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, list_to_data_tree, \\\n give_warning\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\ndef parse_sun_up_hours(sun_up_hours, hoys, timestep):\n \"\"\"Parse the sun-up hours from the result file .txt file.\n\n Args:\n sun_up_hours: A list of integers for the sun-up hours.\n hoys: A list of 8760 * timestep values for the hoys to select. If an empty\n list is passed, None will be returned.\n timestep: Integer for the timestep of the analysis.\n \"\"\"\n if len(hoys) != 0:\n schedule = [False] * (8760 * timestep)\n for hr in hoys:\n schedule[int(hr * timestep)] = True\n su_pattern = [schedule[int(h * timestep)] for h in sun_up_hours]\n return su_pattern\n\n\ndef cumulative_values(ill_file, su_pattern, timestep):\n \"\"\"Compute average values for a given result file.\"\"\"\n cumul_vals = []\n with open(ill_file) as results:\n if su_pattern is None: # no HOY filter on results\n for pt_res in results:\n values = [float(r) for r in pt_res.split()]\n cumul_vals.append(sum(values) / timestep)\n else: \n for pt_res in results:\n values = [float(r) for r, is_hoy in zip(pt_res.split(), su_pattern) if is_hoy]\n cumul_vals.append(sum(values) / timestep)\n return cumul_vals\n\n\nif all_required_inputs(ghenv.Component):\n # set up the default values\n grid_filter_ = '*' if grid_filter_ is None else grid_filter_\n res_folder = os.path.dirname(_results[0]) if os.path.isfile(_results[0]) \\\n else _results[0]\n\n # check to see if results use the newer numpy arrays\n if os.path.isdir(os.path.join(res_folder, '__static_apertures__')) or \\\n os.path.isfile(os.path.join(res_folder, 'grid_states.json')):\n cmds = [folders.python_exe_path, '-m', 'honeybee_radiance_postprocess',\n 'post-process', 'cumulative-values', res_folder, '-sf', 'metrics']\n if len(_hoys_) != 0:\n hoys_str = '\\n'.join(str(h) for h in _hoys_)\n hoys_file = os.path.join(res_folder, 'hoys.txt')\n write_to_file(hoys_file, hoys_str)\n cmds.extend(['--hoys-file', hoys_file])\n if grid_filter_ != '*':\n cmds.extend(['--grids-filter', grid_filter_])\n if len(dyn_sch_) != 0:\n if os.path.isfile(os.path.join(res_folder, 'grid_states.json')):\n dyn_sch = dyn_sch_[0] if isinstance(dyn_sch_[0], DynamicSchedule) else \\\n DynamicSchedule.from_group_schedules(dyn_sch_)\n dyn_sch_file = dyn_sch.to_json(folder=res_folder)\n cmds.extend(['--states', dyn_sch_file])\n else:\n msg = 'No dynamic aperture groups were found in the Model.\\n' \\\n 'The input dynamic schedules will be ignored.'\n print(msg)\n give_warning(ghenv.Component, msg)\n use_shell = True if os.name == 'nt' else False\n custom_env = os.environ.copy()\n custom_env['PYTHONHOME'] = ''\n process = subprocess.Popen(\n cmds, cwd=res_folder, shell=use_shell, env=custom_env,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n stdout, stderr = process.communicate() # wait for the process to finish\n if process.returncode != 0:\n print(stderr)\n raise ValueError('Failed to compute cumulative values.')\n avg_dir = os.path.join(res_folder, 'metrics', 'cumulative_values')\n if os.path.isdir(avg_dir):\n values = read_sensor_grid_result(avg_dir, 'cumulative','full_id', False)\n values = list_to_data_tree(values)\n\n else:\n if len(dyn_sch_) != 0:\n msg = 'Dynamic Schedules are currently only supported for Annual Daylight ' \\\n 'simulations.\\nThe input schedules will be ignored.'\n print(msg)\n give_warning(ghenv.Component, msg)\n\n # extract the timestep if it exists\n timestep = 1\n tstep_file = os.path.join(res_folder, 'timestep.txt')\n if os.path.isfile(tstep_file):\n with open(tstep_file) as tf:\n timestep = int(tf.readline())\n\n # parse the sun-up-hours\n grids, sun_up_hours = _process_input_folder(res_folder, grid_filter_)\n su_pattern = parse_sun_up_hours(sun_up_hours, _hoys_, timestep)\n\n # compute the average values\n values = []\n for grid_info in grids:\n ill_file = os.path.join(res_folder, '%s.ill' % grid_info['full_id'])\n dgp_file = os.path.join(res_folder, '%s.dgp' % grid_info['full_id'])\n if os.path.isfile(dgp_file):\n cumul = cumulative_values(dgp_file, su_pattern, timestep)\n else:\n cumul = cumulative_values(ill_file, su_pattern, timestep)\n values.append(cumul)\n values = list_to_data_tree(values)\n",
0 commit comments