Model/model_data/app.py
Khalim Conn-Kowlessar 537caa410c record of todo
2023-06-21 17:42:30 +01:00

169 lines
6.7 KiB
Python

from tqdm import tqdm
import os
from model_data.BoreholeClient import BoreholeClient
from model_data.LandRegistryClient import LandRegistryClient
from model_data.ConservationAreaClient import ConservationAreaClient
from model_data.temp_inputs import input_data
from model_data.Property import Property
from model_data.config import EPC_AUTH_TOKEN
from epc_api.client import EpcClient
from model_data.downloader import pagenated_epc_download
from model_data.EpcClean import EpcClean
from model_data.OpenUprnClient import OpenUprnClient
LAND_REGISTRY_PATHS = [
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-monthly-update-new-version.csv",
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-2022 (1).csv",
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-2021.csv",
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-2020.csv",
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-2019.csv",
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-2018.csv",
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-2017-part1.csv",
os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/pp-2017-part2.csv",
]
def handler():
# To begin with, the input data is a list of dictionaries, however we would read this file in
epc_client = EpcClient(auth_token=EPC_AUTH_TOKEN)
input_properties = [
Property(postcode=config['postcode'], address1=config['address1'], epc_client=epc_client)
for config in input_data
]
for p in input_properties:
p.search_address_epc()
uprns = [p.data['uprn'] for p in input_properties]
open_uprn_client = OpenUprnClient(
path=os.path.abspath(
os.path.dirname(__file__)
) + "/model_data/local_data/osopenuprn_202306_csv/osopenuprn_202305.csv",
uprns=uprns
)
open_uprn_client.read()
# We're using Ordinance Survey Open Uprn data
# to find the coordinates of each address, which we will then be able to use at a later stage
for p in input_properties:
p.get_coordinates(open_uprn_client)
conservation_area_client = ConservationAreaClient(
historic_england_path=os.path.abspath(
os.path.dirname(__file__)
) + "/model_data/local_data/Historic_Eng_Conservation_Areas/Conservation_Areas.shp",
gov_path=os.path.abspath(
os.path.dirname(__file__)
) + "/model_data/local_data/gov-conservation-area.geojson"
)
conservation_area_client.read()
# Check if the property is in a conversation area
for p in input_properties:
p.set_is_in_conservation_area(conservation_area_client)
local_authorities = {p.data['local-authority'] for p in input_properties}
data = []
for la in tqdm(local_authorities):
data.extend(
pagenated_epc_download(
client=epc_client,
params={"local-authority": la},
page_size=5000,
n_pages=10,
)
)
# Incorporate input data into cleaning
cleaner = EpcClean(data + [p.data for p in input_properties])
cleaner.clean()
address_meta = [
{
"postcode": x["postcode"].upper(),
"address1": x["address1"].upper(),
"address2": x["address2"].upper(),
"address3": x["address3"].upper(),
"address": x["address"],
"uprn": x["uprn"]
} for x in data
]
# Land registry
land_registry_client = LandRegistryClient(
paths=LAND_REGISTRY_PATHS,
addresses=address_meta
)
land_registry_client.read()
# Borehole
borehole_client = BoreholeClient(
path=os.path.abspath(os.path.dirname(__file__)) + "/model_data/local_data/borehole/borehole.dbf"
)
borehole_client.read()
# Now, for our input properties, we need to identify the components of the building, based
# on the cleaning we've done
for p in input_properties:
p.get_components(cleaner)
# Now, given the components, we want to idenfity upgrade options
import pandas as pd
walls_df = pd.DataFrame(
[{"address1": p.address1, **p.walls} for p in input_properties]
)
input_properties[6].data["address1"]
input_properties[6].data["postcode"]
walls_df["address1"].values[6]
walls_df["original_description"].values[6]
# Walls
# Property 0
# '28 Distillery Wharf', 'Average thermal transmittance 0.16 W/m-¦K'
# Because the insulation is already within the max threshold for new builds
# Also, I know that the property was built after 1990 so was built with insulation (let's get land registry data)
# It's likely not worth doing an insulation upgrade, however if anything, we would do internal wall insulation
# logic:
# if building built after 1990 + we're able to identify U-value + U-value less than 0.18
# and if in or close to a conversation area, recommend internal wall insulation
# Property 1
# 'Flat 14 Godley V C House', Solid brick, as built, no insulation (assumed)
# Since the wall is solid brick (therefore no cavity), we can recommend the following:
# External wall insulation
# Internal wall insulation
# Property 2
# '49, Elderfield Road', Solid brick, as built, no insulation (assumed)
# Same as property 1
# Property 3
# 26, Stanhope Road', 'Average thermal transmittance 0.14 W/m-¦K'
# Same as property 0
# Property 4
# 'Flat 3 Frederick Building' 'Solid brick, as built, no insulation (assumed)'
# Same as property 1
# 'Flat 4 Frederick Building' 'Solid brick, as built, no insulation (assumed)'
# Same as property 1
# 'Flat 28, 22 Adelina Grove' 'Solid brick, as built, insulated (assumed)'
from model_data.recommendations.WallRecommendations import WallRecommendations
self = WallRecommendations(property_instance=input_properties[6])
# We need to deduce a U-value for "Good" energy effieciency
df = pd.DataFrame(data)
df = df[df["walls-description"].str.contains("Average thermal transmittance")]
mainheating = pd.DataFrame(
[{"address1": p.address1, "postcode": p.postcode, **p.main_heating} for p in input_properties])
hotwater = pd.DataFrame([{"address1": p.address1, **p.hotwater} for p in input_properties])
mainheating[["address1", "postcode"]]
# TODO: I want to knwo what "Good" efficiency means for the description
# 'Flat 28, 22 Adelina Grove' 'Solid brick, as built, insulated (assumed)'
# so to do this, filter on the local authority code and property type, where we have U
# values for the wall and take a median!