{ "cells": [ { "cell_type": "markdown", "id": "069ba213", "metadata": {}, "source": [ "# demo_editing_a_epJSON_file\n", "\n", "This notebook demonstrates how to use Python to edit an epJSON EnergyPlus input file, as described in this blog post: https://www.stevenfirth.com/5-ways-to-edit-energyplus-input-files/\n" ] }, { "cell_type": "markdown", "id": "c30906a0", "metadata": {}, "source": [ "## The Sample Code in the blog post\n", "\n", "Here is the original code in the blog post:" ] }, { "cell_type": "code", "execution_count": 1, "id": "c27ef8a1", "metadata": {}, "outputs": [], "source": [ "import json\n", "with open('1ZoneUncontrolled.epJSON') as f:\n", " input_data=json.load(f)\n", "input_data['Building']['Simple One Zone (Wireframe DXF)']['north_axis']=90\n", "with open('new_edit.epJSON','w') as f:\n", " json.dump(input_data,f, indent=4)" ] }, { "cell_type": "markdown", "id": "e67b2f04", "metadata": {}, "source": [ "Running this code will open an epJSON file, make an edit and save the edited file with the name 'new_edit.epJSON'." ] }, { "cell_type": "markdown", "id": "e764969a", "metadata": {}, "source": [ "Let's break this code down into sections to see exactly what it is doing" ] }, { "cell_type": "markdown", "id": "0587baf0", "metadata": {}, "source": [ "### 1. Importing the `json` package\n", "\n", "This imports the [`json` package](https://docs.python.org/3/library/json.html) from the Python standard library." ] }, { "cell_type": "code", "execution_count": 2, "id": "08a24e74", "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "markdown", "id": "e34e8bb5", "metadata": {}, "source": [ "We can see that the `json` package has been imported by printing it." ] }, { "cell_type": "code", "execution_count": 3, "id": "ce910afb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json" ] }, { "cell_type": "markdown", "id": "2755cc2c", "metadata": {}, "source": [ "### 2. Parsing the original epJSON file\n", "\n", "This opens the *1ZoneUncontrolled.epJSON* file using the Python [`open`](https://docs.python.org/3/library/functions.html#open) built-in method and then uses the [`json.load`](https://docs.python.org/3/library/json.html#json.load) method to parse the file contents into the `input_data` variable.\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "35bc5d9a", "metadata": {}, "outputs": [], "source": [ "with open('1ZoneUncontrolled.epJSON') as f:\n", " input_data=json.load(f)" ] }, { "cell_type": "markdown", "id": "4d2f9cec", "metadata": {}, "source": [ "The `input_data` variable is now a dicitonary containing the contents of the *1ZoneUncontrolled.epJSON* file. We can see this by printing it. " ] }, { "cell_type": "code", "execution_count": 5, "id": "e3dd7bcc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Building': {'Simple One Zone (Wireframe DXF)': {'loads_convergence_tolerance_value': 0.04,\n", " 'maximum_number_of_warmup_days': 30,\n", " 'minimum_number_of_warmup_days': 6,\n", " 'north_axis': 0,\n", " 'solar_distribution': 'MinimalShadowing',\n", " 'temperature_convergence_tolerance_value': 0.004,\n", " 'terrain': 'Suburbs'}},\n", " 'BuildingSurface:Detailed': {'Zn001:Flr001': {'construction_name': 'FLOOR',\n", " 'number_of_vertices': 4,\n", " 'outside_boundary_condition': 'Adiabatic',\n", " 'sun_exposure': 'NoSun',\n", " 'surface_type': 'Floor',\n", " 'vertices': [{'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 0.0,\n", " 'vertex_z_coordinate': 0.0},\n", " {'vertex_x_coordinate': 0.0,\n", " 'vertex_y_coordinate': 0.0,\n", " 'vertex_z_coordinate': 0.0},\n", " {'vertex_x_coordinate': 0.0,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 0.0},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 0.0}],\n", " 'view_factor_to_ground': 1.0,\n", " 'wind_exposure': 'NoWind',\n", " 'zone_name': 'ZONE ONE'},\n", " 'Zn001:Roof001': {'construction_name': 'ROOF31',\n", " 'number_of_vertices': 4,\n", " 'outside_boundary_condition': 'Outdoors',\n", " 'sun_exposure': 'SunExposed',\n", " 'surface_type': 'Roof',\n", " 'vertices': [{'vertex_x_coordinate': 0.0,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 4.572},\n", " {'vertex_x_coordinate': 0.0,\n", " 'vertex_y_coordinate': 0.0,\n", " 'vertex_z_coordinate': 4.572},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 0.0,\n", " 'vertex_z_coordinate': 4.572},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 4.572}],\n", " 'view_factor_to_ground': 0,\n", " 'wind_exposure': 'WindExposed',\n", " 'zone_name': 'ZONE ONE'},\n", " 'Zn001:Wall001': {'construction_name': 'R13WALL',\n", " 'number_of_vertices': 4,\n", " 'outside_boundary_condition': 'Outdoors',\n", " 'sun_exposure': 'SunExposed',\n", " 'surface_type': 'Wall',\n", " 'vertices': [{'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 4.572},\n", " {'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 4.572}],\n", " 'view_factor_to_ground': 0.5,\n", " 'wind_exposure': 'WindExposed',\n", " 'zone_name': 'ZONE ONE'},\n", " 'Zn001:Wall002': {'construction_name': 'R13WALL',\n", " 'number_of_vertices': 4,\n", " 'outside_boundary_condition': 'Outdoors',\n", " 'sun_exposure': 'SunExposed',\n", " 'surface_type': 'Wall',\n", " 'vertices': [{'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 4.572},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 4.572}],\n", " 'view_factor_to_ground': 0.5,\n", " 'wind_exposure': 'WindExposed',\n", " 'zone_name': 'ZONE ONE'},\n", " 'Zn001:Wall003': {'construction_name': 'R13WALL',\n", " 'number_of_vertices': 4,\n", " 'outside_boundary_condition': 'Outdoors',\n", " 'sun_exposure': 'SunExposed',\n", " 'surface_type': 'Wall',\n", " 'vertices': [{'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 4.572},\n", " {'vertex_x_coordinate': 15.24,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 4.572}],\n", " 'view_factor_to_ground': 0.5,\n", " 'wind_exposure': 'WindExposed',\n", " 'zone_name': 'ZONE ONE'},\n", " 'Zn001:Wall004': {'construction_name': 'R13WALL',\n", " 'number_of_vertices': 4,\n", " 'outside_boundary_condition': 'Outdoors',\n", " 'sun_exposure': 'SunExposed',\n", " 'surface_type': 'Wall',\n", " 'vertices': [{'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 4.572},\n", " {'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 15.24,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 0},\n", " {'vertex_x_coordinate': 0,\n", " 'vertex_y_coordinate': 0,\n", " 'vertex_z_coordinate': 4.572}],\n", " 'view_factor_to_ground': 0.5,\n", " 'wind_exposure': 'WindExposed',\n", " 'zone_name': 'ZONE ONE'}},\n", " 'Construction': {'FLOOR': {'outside_layer': 'C5 - 4 IN HW CONCRETE'},\n", " 'R13WALL': {'outside_layer': 'R13LAYER'},\n", " 'ROOF31': {'outside_layer': 'R31LAYER'}},\n", " 'Exterior:Lights': {'ExtLights': {'control_option': 'AstronomicalClock',\n", " 'design_level': 5250,\n", " 'end_use_subcategory': 'Grounds Lights',\n", " 'schedule_name': 'AlwaysOn'}},\n", " 'GlobalGeometryRules': {'GlobalGeometryRules 1': {'coordinate_system': 'World',\n", " 'starting_vertex_position': 'UpperLeftCorner',\n", " 'vertex_entry_direction': 'Counterclockwise'}},\n", " 'HeatBalanceAlgorithm': {'HeatBalanceAlgorithm 1': {'algorithm': 'ConductionTransferFunction'}},\n", " 'Material': {'C5 - 4 IN HW CONCRETE': {'conductivity': 1.729577,\n", " 'density': 2242.585,\n", " 'roughness': 'MediumRough',\n", " 'solar_absorptance': 0.65,\n", " 'specific_heat': 836.8,\n", " 'thermal_absorptance': 0.9,\n", " 'thickness': 0.1014984,\n", " 'visible_absorptance': 0.65}},\n", " 'Material:NoMass': {'R13LAYER': {'roughness': 'Rough',\n", " 'solar_absorptance': 0.75,\n", " 'thermal_absorptance': 0.9,\n", " 'thermal_resistance': 2.290965,\n", " 'visible_absorptance': 0.75},\n", " 'R31LAYER': {'roughness': 'Rough',\n", " 'solar_absorptance': 0.75,\n", " 'thermal_absorptance': 0.9,\n", " 'thermal_resistance': 5.456,\n", " 'visible_absorptance': 0.75}},\n", " 'OtherEquipment': {'Test 352 minus': {'design_level': -352,\n", " 'design_level_calculation_method': 'EquipmentLevel',\n", " 'fraction_latent': 0,\n", " 'fraction_lost': 0,\n", " 'fraction_radiant': 0,\n", " 'fuel_type': 'None',\n", " 'schedule_name': 'AlwaysOn',\n", " 'zone_or_zonelist_name': 'ZONE ONE'},\n", " 'Test 352a': {'design_level': 352,\n", " 'design_level_calculation_method': 'EquipmentLevel',\n", " 'fraction_latent': 0,\n", " 'fraction_lost': 0,\n", " 'fraction_radiant': 0,\n", " 'fuel_type': 'None',\n", " 'schedule_name': 'AlwaysOn',\n", " 'zone_or_zonelist_name': 'ZONE ONE'}},\n", " 'Output:Constructions': {'Output:Constructions 1': {'details_type_1': 'Constructions'}},\n", " 'Output:Meter:MeterFileOnly': {'Output:Meter:MeterFileOnly 1': {'key_name': 'ExteriorLights:Electricity',\n", " 'reporting_frequency': 'Hourly'},\n", " 'Output:Meter:MeterFileOnly 2': {'key_name': 'EnergyTransfer:Building',\n", " 'reporting_frequency': 'Hourly'},\n", " 'Output:Meter:MeterFileOnly 3': {'key_name': 'EnergyTransfer:Facility',\n", " 'reporting_frequency': 'Hourly'}},\n", " 'Output:Surfaces:Drawing': {'Output:Surfaces:Drawing 1': {'report_type': 'DXF:WireFrame'}},\n", " 'Output:Table:SummaryReports': {'Output:Table:SummaryReports 1': {'reports': [{'report_name': 'AllSummary'}]}},\n", " 'Output:Variable': {'Output:Variable 1': {'key_value': '*',\n", " 'reporting_frequency': 'Hourly',\n", " 'variable_name': 'Site Outdoor Air Drybulb Temperature'},\n", " 'Output:Variable 2': {'key_value': '*',\n", " 'reporting_frequency': 'Daily',\n", " 'variable_name': 'Site Daylight Saving Time Status'},\n", " 'Output:Variable 3': {'key_value': '*',\n", " 'reporting_frequency': 'Daily',\n", " 'variable_name': 'Site Day Type Index'},\n", " 'Output:Variable 4': {'key_value': '*',\n", " 'reporting_frequency': 'Hourly',\n", " 'variable_name': 'Zone Mean Air Temperature'},\n", " 'Output:Variable 5': {'key_value': '*',\n", " 'reporting_frequency': 'Hourly',\n", " 'variable_name': 'Zone Total Internal Latent Gain Energy'},\n", " 'Output:Variable 6': {'key_value': '*',\n", " 'reporting_frequency': 'Hourly',\n", " 'variable_name': 'Zone Mean Radiant Temperature'},\n", " 'Output:Variable 7': {'key_value': '*',\n", " 'reporting_frequency': 'Hourly',\n", " 'variable_name': 'Zone Air Heat Balance Surface Convection Rate'},\n", " 'Output:Variable 8': {'key_value': '*',\n", " 'reporting_frequency': 'Hourly',\n", " 'variable_name': 'Zone Air Heat Balance Air Energy Storage Rate'},\n", " 'Output:Variable 9': {'key_value': '*',\n", " 'reporting_frequency': 'Daily',\n", " 'variable_name': 'Surface Inside Face Temperature'},\n", " 'Output:Variable 10': {'key_value': '*',\n", " 'reporting_frequency': 'Daily',\n", " 'variable_name': 'Surface Outside Face Temperature'},\n", " 'Output:Variable 11': {'key_value': '*',\n", " 'reporting_frequency': 'Daily',\n", " 'variable_name': 'Surface Inside Face Convection Heat Transfer Coefficient'},\n", " 'Output:Variable 12': {'key_value': '*',\n", " 'reporting_frequency': 'Daily',\n", " 'variable_name': 'Surface Outside Face Convection Heat Transfer Coefficient'},\n", " 'Output:Variable 13': {'key_value': '*',\n", " 'reporting_frequency': 'Monthly',\n", " 'variable_name': 'Other Equipment Total Heating Energy'},\n", " 'Output:Variable 14': {'key_value': '*',\n", " 'reporting_frequency': 'Monthly',\n", " 'variable_name': 'Zone Other Equipment Total Heating Energy'}},\n", " 'Output:VariableDictionary': {'Output:VariableDictionary 1': {'key_field': 'IDF'}},\n", " 'OutputControl:Table:Style': {'OutputControl:Table:Style 1': {'column_separator': 'All'}},\n", " 'RunPeriod': {'Run Period 1': {'apply_weekend_holiday_rule': 'No',\n", " 'begin_day_of_month': 1,\n", " 'begin_month': 1,\n", " 'day_of_week_for_start_day': 'Tuesday',\n", " 'end_day_of_month': 31,\n", " 'end_month': 12,\n", " 'use_weather_file_daylight_saving_period': 'Yes',\n", " 'use_weather_file_holidays_and_special_days': 'Yes',\n", " 'use_weather_file_rain_indicators': 'Yes',\n", " 'use_weather_file_snow_indicators': 'Yes'}},\n", " 'Schedule:Constant': {'AlwaysOn': {'hourly_value': 1.0,\n", " 'schedule_type_limits_name': 'On/Off'}},\n", " 'ScheduleTypeLimits': {'Fraction': {'lower_limit_value': 0.0,\n", " 'numeric_type': 'Continuous',\n", " 'upper_limit_value': 1.0},\n", " 'On/Off': {'lower_limit_value': 0,\n", " 'numeric_type': 'Discrete',\n", " 'upper_limit_value': 1}},\n", " 'SimulationControl': {'SimulationControl 1': {'do_hvac_sizing_simulation_for_sizing_periods': 'No',\n", " 'do_plant_sizing_calculation': 'No',\n", " 'do_system_sizing_calculation': 'No',\n", " 'do_zone_sizing_calculation': 'No',\n", " 'maximum_number_of_hvac_sizing_simulation_passes': 1,\n", " 'run_simulation_for_sizing_periods': 'Yes',\n", " 'run_simulation_for_weather_file_run_periods': 'Yes'}},\n", " 'Site:Location': {'Denver Centennial Golden N_CO_USA Design_Conditions': {'elevation': 1829.0,\n", " 'latitude': 39.74,\n", " 'longitude': -105.18,\n", " 'time_zone': -7.0}},\n", " 'SizingPeriod:DesignDay': {'Denver Centennial Golden N Ann Clg 1% Condns DB=>MWB': {'barometric_pressure': 81198.0,\n", " 'daily_dry_bulb_temperature_range': 15.2,\n", " 'day_of_month': 21,\n", " 'day_type': 'SummerDesignDay',\n", " 'daylight_saving_time_indicator': 'No',\n", " 'humidity_condition_type': 'WetBulb',\n", " 'maximum_dry_bulb_temperature': 32,\n", " 'month': 7,\n", " 'rain_indicator': 'No',\n", " 'sky_clearness': 1.0,\n", " 'snow_indicator': 'No',\n", " 'solar_model_indicator': 'ASHRAEClearSky',\n", " 'wetbulb_or_dewpoint_at_maximum_dry_bulb': 15.5,\n", " 'wind_direction': 0,\n", " 'wind_speed': 4.9},\n", " 'Denver Centennial Golden N Ann Htg 99% Condns DB': {'barometric_pressure': 81198.0,\n", " 'daily_dry_bulb_temperature_range': 0.0,\n", " 'day_of_month': 21,\n", " 'day_type': 'WinterDesignDay',\n", " 'daylight_saving_time_indicator': 'No',\n", " 'humidity_condition_type': 'WetBulb',\n", " 'maximum_dry_bulb_temperature': -15.5,\n", " 'month': 12,\n", " 'rain_indicator': 'No',\n", " 'sky_clearness': 0.0,\n", " 'snow_indicator': 'No',\n", " 'solar_model_indicator': 'ASHRAEClearSky',\n", " 'wetbulb_or_dewpoint_at_maximum_dry_bulb': -15.5,\n", " 'wind_direction': 340,\n", " 'wind_speed': 3}},\n", " 'SurfaceConvectionAlgorithm:Inside': {'SurfaceConvectionAlgorithm:Inside 1': {'algorithm': 'TARP'}},\n", " 'SurfaceConvectionAlgorithm:Outside': {'SurfaceConvectionAlgorithm:Outside 1': {'algorithm': 'DOE-2'}},\n", " 'Timestep': {'Timestep 1': {'number_of_timesteps_per_hour': 4}},\n", " 'Version': {'Version 1': {'version_identifier': '9.4'}},\n", " 'Zone': {'ZONE ONE': {'ceiling_height': 'Autocalculate',\n", " 'direction_of_relative_north': 0,\n", " 'multiplier': 1,\n", " 'type': 1,\n", " 'volume': 'Autocalculate',\n", " 'x_origin': 0,\n", " 'y_origin': 0,\n", " 'z_origin': 0}}}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_data" ] }, { "cell_type": "markdown", "id": "86be6235", "metadata": {}, "source": [ "### 3. Editing the input data\n", "\n", "This changes the *north_axis* parameter of the 'Simple One Zone (Wireframe DXF)' *Building* object from 0 (North) to 90 (East)." ] }, { "cell_type": "code", "execution_count": 6, "id": "22928b79", "metadata": {}, "outputs": [], "source": [ "input_data['Building']['Simple One Zone (Wireframe DXF)']['north_axis']=90" ] }, { "cell_type": "markdown", "id": "2d12271f", "metadata": {}, "source": [ "We can check this has worked by printing out the *Building* object." ] }, { "cell_type": "code", "execution_count": 7, "id": "62ce396a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Simple One Zone (Wireframe DXF)': {'loads_convergence_tolerance_value': 0.04,\n", " 'maximum_number_of_warmup_days': 30,\n", " 'minimum_number_of_warmup_days': 6,\n", " 'north_axis': 90,\n", " 'solar_distribution': 'MinimalShadowing',\n", " 'temperature_convergence_tolerance_value': 0.004,\n", " 'terrain': 'Suburbs'}}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_data['Building']" ] }, { "cell_type": "markdown", "id": "617c7542", "metadata": {}, "source": [ "### 4. Saving the new file\n", "\n", "This saves a new epJSON file with the name *new_edit.epJSON*. The file contents are the modified data created in Step 3 above." ] }, { "cell_type": "code", "execution_count": 8, "id": "bd2ace08", "metadata": {}, "outputs": [], "source": [ "with open('new_edit.epJSON','w') as f:\n", " json.dump(input_data,f, indent=4)" ] }, { "cell_type": "markdown", "id": "5f76e3e9", "metadata": {}, "source": [ "We can check this has worked by opening the *new_edit.epJSON* file in an editor (e.g. Notepad) and seeing that the contents are as expected." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }