summer_course_2024/comparing.py
Andrea Gabaldon Moreno 0ce76bef13 20240722 changes
2024-07-22 18:14:54 -04:00

215 lines
14 KiB
Python

import json
from pathlib import Path
import matplotlib.pyplot as plt
# Specify the GeoJSON file path
input_files_path = (Path(__file__).parent / 'input_files')
# Load the updated GeoJSON file cotaining the results
baseline_scenario_path =input_files_path / 'updated_buildings_with_all_data_baseline.geojson'
energy_efficient_scenario_path = input_files_path / 'updated_buildings_with_all_data_2024.geojson'
with open(baseline_scenario_path , 'r') as f:
baseline_scenario = json.load(f)
with open(energy_efficient_scenario_path, 'r') as f:
energy_efficient_scenario=json.load(f)
percentage_data = {
1646: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 2672.550473, "total_floor_area": 26725.50473},
1647: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 2653.626087, "total_floor_area": 26536.26087},
1648: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1056.787496, "total_floor_area": 10567.87496},
1649: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1906.620746, "total_floor_area": 19066.20746},
1650: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 659.1119416, "total_floor_area": 5272.895533},
1651: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1167.208109, "total_floor_area": 9337.664871},
1652: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1193.251653, "total_floor_area": 9546.013222},
1653: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1491.722543, "total_floor_area": 11933.78035},
1654: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1168.005028, "total_floor_area": 9344.040224},
1655: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1264.906961, "total_floor_area": 10119.25569},
1656: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1281.768818, "total_floor_area": 10254.15054},
1657: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 290.3886018, "total_floor_area": 2323.108814},
1658: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 847.5095193, "total_floor_area": 6780.076155},
1659: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1115.319153, "total_floor_area": 8922.553224},
1660: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 469.2918062, "total_floor_area": 3754.33445},
1661: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1292.298346, "total_floor_area": 10338.38677},
1662: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 625.7828863, "total_floor_area": 5006.263091},
1663: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1876.02897, "total_floor_area": 15008.23176},
1664: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1118.224781, "total_floor_area": 22364.49562},
1665: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1502.787808, "total_floor_area": 30055.75617},
1666: {"type1_%": 0.891045711, "type2_%": 0.108954289, "type3_%": 0, "roof_area": 3038.486076, "total_floor_area": 30384.86076},
1667: {"type1_%": 0.8, "type2_%": 0.2, "type3_%": 0, "roof_area": 1343.832818, "total_floor_area": 13438.32818},
1668: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 961.0996956, "total_floor_area": 4805.498478},
1669: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 489.1282111, "total_floor_area": 1956.512845},
1673: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 1693.141465, "total_floor_area": 5079.424396},
1674: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 3248.827576, "total_floor_area": 9746.482729},
1675: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 4086.842191, "total_floor_area": 12260.52657},
1676: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 2786.114146, "total_floor_area": 11144.45658},
1677: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 5142.784184, "total_floor_area": 15428.35255},
1678: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 6068.664574, "total_floor_area": 18205.99372},
1679: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 5646.751407, "total_floor_area": 16940.25422},
1680: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 1601.765953, "total_floor_area": 4805.297859},
1681: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 9728.221797, "total_floor_area": 29184.66539},
1687: {"type1_%": 0.606611029, "type2_%": 0.28211422, "type3_%": 0.11127475, "roof_area": 4268.608743, "total_floor_area": 59760.52241},
1688: {"type1_%": 0.92, "type2_%": 0.08, "type3_%": 0, "roof_area": 2146.654828, "total_floor_area": 38639.7869},
1689: {"type1_%": 0.96, "type2_%": 0.04, "type3_%": 0, "roof_area": 2860.270711, "total_floor_area": 57205.41421},
1690: {"type1_%": 0.94, "type2_%": 0.06, "type3_%": 0, "roof_area": 2189.732519, "total_floor_area": 28466.52275},
1691: {"type1_%": 0.75, "type2_%": 0.25, "type3_%": 0, "roof_area": 3159.077523, "total_floor_area": 31590.77523},
}
# Define the demand types to be plotted
demand_types = [
'heating_demand_kWh',
'cooling_demand_kWh',
'domestic_hot_water_heat_demand_kWh',
'appliances_electrical_demand_kWh',
'lighting_electrical_demand_kWh'
]
# # Function to extract demand data from the GeoJSON
# def extract_demand_data(geojson_data, demand_type, period_start, period_end):
# demand_data = {}
# for feature in geojson_data['features']:
# building_id = int(feature['properties']['id'])
# if demand_type in feature['properties']:
# demand_series = feature['properties'][demand_type][period_start:period_end]
# demand_data[building_id] = demand_series
# return demand_data
#
# # Define time periods
# period_start_january = 0
# period_end_january = 168 # First week of January (0-168h)
# period_start_summer = 3360
# period_end_summer = 3528 # One week in summer
#
# # Function to plot data for all buildings
# def plot_all_buildings(data, period, title):
# plt.figure(figsize=(15, 8))
# for building_id, values in data.items():
# plt.plot(range(len(values)), values, label=f'Building {building_id}')
# plt.xlabel('Hour')
# plt.ylabel('Energy Consumption (kWh)')
# plt.title(title)
# plt.legend(loc='upper right', bbox_to_anchor=(1.15, 1))
# plt.show()
#
# # Function to compare baseline and efficient scenarios for selected buildings
# def plot_comparison(selected_buildings, baseline_data, efficient_data, period, title):
# plt.figure(figsize=(15, 8))
# for building_id in selected_buildings:
# baseline = baseline_data.get(building_id, [])
# efficient = efficient_data.get(building_id, [])
# plt.plot(range(len(baseline)), baseline, label=f'Baseline Building {building_id}')
# plt.plot(range(len(efficient)), efficient, linestyle='--', label=f'Efficient Building {building_id}')
# plt.xlabel('Hour')
# plt.ylabel('Energy Consumption (kWh)')
# plt.title(title)
# plt.legend(loc='upper right', bbox_to_anchor=(1.15, 1))
# plt.show()
#
# # Loop through each demand type and extract data, then plot
# for demand_type in demand_types:
# # Extract demand data for the specified periods
# baseline_january = extract_demand_data(baseline_scenario, demand_type, period_start_january, period_end_january)
# baseline_summer = extract_demand_data(baseline_scenario, demand_type, period_start_summer, period_end_summer)
# efficient_january = extract_demand_data(energy_efficient_scenario, demand_type, period_start_january, period_end_january)
# efficient_summer = extract_demand_data(energy_efficient_scenario, demand_type, period_start_summer, period_end_summer)
#
# # Plot all buildings for the first week of January and one week in summer
# plot_all_buildings(baseline_january, range(period_start_january, period_end_january), f'First Week of January - Baseline Scenario - {demand_type}')
# plot_all_buildings(baseline_summer, range(period_start_summer, period_end_summer), f'One Week in Summer - Baseline Scenario - {demand_type}')
#
# # Selected buildings for comparison
# selected_buildings = [1646, 1691, 1687]
#
# # Plot comparison for the first week of January and one week in summer
# plot_comparison(selected_buildings, baseline_january, efficient_january, range(period_start_january, period_end_january), f'First Week of January - Comparison - {demand_type}')
# plot_comparison(selected_buildings, baseline_summer, efficient_summer, range(period_start_summer, period_end_summer), f'One Week in Summer - Comparison - {demand_type}')
#
# Function to extract and sum demand data from the GeoJSON
def extract_and_sum_demand_data(geojson_data, demand_type, period_start, period_end):
district_demand = [0] * (period_end - period_start)
for feature in geojson_data['features']:
if demand_type in feature['properties']:
demand_series = feature['properties'][demand_type][period_start:period_end]
district_demand = [sum(x) for x in zip(district_demand, demand_series)]
# Convert kWh to MWh
district_demand_mwh = [value / 1000 for value in district_demand]
return district_demand_mwh
# Define time periods
period_start_january = 0
period_end_january = 168 # First week of January (0-168h)
period_start_summer = 3360
period_end_summer = 3528 # One week in summer
# Function to plot district demand
def plot_district_demand(district_demand, title):
plt.figure(figsize=(15, 8))
plt.plot(range(len(district_demand)), district_demand, label='District Demand')
plt.xlabel('Hour')
plt.ylabel('Energy Consumption (MWh)')
plt.title(title)
plt.legend(loc='upper right')
plt.show()
# Function to compare district demands between baseline and efficient scenarios
def plot_district_comparison(district_demand_baseline, district_demand_efficient, title):
plt.figure(figsize=(15, 8))
plt.plot(range(len(district_demand_baseline)), district_demand_baseline, label='Baseline District Demand')
plt.plot(range(len(district_demand_efficient)), district_demand_efficient, linestyle='--', label='Efficient District Demand')
plt.xlabel('Hour')
plt.ylabel('Energy Consumption (MWh)')
plt.title(title)
plt.legend(loc='upper right')
plt.show()
# # Loop through each demand type, extract and sum data, then plot
# for demand_type in demand_types:
# # Extract and sum demand data for the specified periods
# district_demand_january_baseline = extract_and_sum_demand_data(baseline_scenario, demand_type, period_start_january, period_end_january)
# district_demand_summer_baseline = extract_and_sum_demand_data(baseline_scenario, demand_type, period_start_summer, period_end_summer)
# district_demand_january_efficient = extract_and_sum_demand_data(energy_efficient_scenario, demand_type, period_start_january, period_end_january)
# district_demand_summer_efficient = extract_and_sum_demand_data(energy_efficient_scenario, demand_type, period_start_summer, period_end_summer)
#
# # Plot district demand for the first week of January and one week in summer for baseline scenario
# plot_district_demand(district_demand_january_baseline, f'District Demand - First Week of January - Baseline Scenario - {demand_type}')
# plot_district_demand(district_demand_summer_baseline, f'District Demand - One Week in Summer - Baseline Scenario - {demand_type}')
#
# # Plot district demand for the first week of January and one week in summer for energy-efficient scenario
# plot_district_demand(district_demand_january_efficient, f'District Demand - First Week of January - Energy Efficient Scenario - {demand_type}')
# plot_district_demand(district_demand_summer_efficient, f'District Demand - One Week in Summer - Energy Efficient Scenario - {demand_type}')
#
# # Plot comparison of district demand between baseline and energy-efficient scenarios
# plot_district_comparison(district_demand_january_baseline, district_demand_january_efficient, f'District Demand Comparison - First Week of January - {demand_type}')
# plot_district_comparison(district_demand_summer_baseline, district_demand_summer_efficient, f'District Demand Comparison - One Week in Summer - {demand_type}')
# Function to extract and sum demand data from the GeoJSON
# Define time period for a year
period_start = 0
period_end = 8760 # Full year (0-8760h)
# Dictionary to store the district demand data for export
district_demand_data = {'hour': list(range(period_start, period_end))}
import pandas as pd
# Loop through each demand type, extract and sum data, then store in dictionary
for demand_type in demand_types:
# Extract and sum demand data for the full year
district_demand_mwh_baseline = extract_and_sum_demand_data(baseline_scenario, demand_type, period_start, period_end)
district_demand_mwh_efficient = extract_and_sum_demand_data(energy_efficient_scenario, demand_type, period_start,
period_end)
# Store data in the dictionary
district_demand_data[f'{demand_type}_baseline'] = district_demand_mwh_baseline
district_demand_data[f'{demand_type}_efficient'] = district_demand_mwh_efficient
# Calculate total district demand values for the year
total_district_baseline = sum(district_demand_mwh_baseline)
total_district_efficient = sum(district_demand_mwh_efficient)
# Print total district demand values
print(f'Total District Demand for {demand_type} (Baseline): {total_district_baseline:.2f} MWh')
print(f'Total District Demand for {demand_type} (Efficient): {total_district_efficient:.2f} MWh')
# Convert dictionary to DataFrame and export to CSV
district_demand_df = pd.DataFrame(district_demand_data)
output_csv_path = input_files_path / 'district_demand.csv'
district_demand_df.to_csv(output_csv_path, index=False)
print(f'District demand data exported to {output_csv_path}')