-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
143 lines (104 loc) · 5.19 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Simulation tab of the Estidama daylight app."""
import streamlit as st
import time
import folium
from streamlit_folium import st_folium
from typing import Union, Tuple
from pathlib import Path
from honeybee_radiance.lightsource.sky.cie import CIE
from pollination_streamlit.api.client import ApiClient
from pollination_streamlit.interactors import NewJob, Recipe
from estidama import PointInTime, SIM_TIMES
def cie_sky(latitude: float, longitude: float, month: int,
day: int, hour: int, north_angle: int, ground_reflectance: float) -> str:
"""Get representation of the CIE sky as a string.
args:
location: A Ladybug location object.
month: Month in a year. Acceptable numbers are between 1 to 12.
day: Day in the month. Acceptable numbers are between 1 to 31.
hour: Hour in the day. Acceptable numbers are between 0 to 23.
north: The angle in degrees from positive Y.
returns:
A string representation of the CIE clear sky with sun.
"""
cie = CIE.from_lat_long(latitude, longitude, 4, month,
day, hour, 0, north_angle, ground_reflectance)
return f'cie -alt {cie.altitude} -az {cie.azimuth} -type 0 -g 0.2'
def create_job(hbjson_path: Path, api_client: ApiClient, owner: str, project: str,
name: str, description: str, latitude: float, longitude: float,
north_angle: int, ground_reflectance: float = 0.2) -> NewJob:
"""Create a Job to run a simulation on Pollination.
args:
hbjson_path: Path to the HBJSON file with grids.
api_client: ApiClient object with Pollination API key.
owner: Username as a string.
project: Project name as a string.
name: Name of the simulation as a string.
description: Description of the simulation as a string.
north: The angle in degrees from positive Y.
epw: A Ladybug EPW object.
returns:
A NewJob object to schedule a simulation on Pollination.
"""
recipe = Recipe('ladybug-tools', 'point-in-time-grid', 'latest', api_client)
new_job = NewJob(owner, project, recipe, name=name,
description=description, client=api_client)
arguments = []
for point in SIM_TIMES:
argument = {}
model_path = new_job.upload_artifact(hbjson_path, '.')
argument['model'] = model_path
argument['metric'] = 'illuminance'
argument['radiance-parameters'] = '-ab 7'
argument['sky'] = cie_sky(latitude, longitude, point.month,
point.day, point.hour, north_angle, ground_reflectance)
arguments.append(argument)
argument['month_day_hour'] = str(point)
new_job.arguments = arguments
return new_job
def show_map(latitude: float, longitude: float, zoom: float) -> None:
"""Create a Folium map to show the selected location.
args:
latitude: Latitude of the selected location.
longitude: Longitude of the selected location.
zoom: Zoom level of the map.
"""
m = folium.Map(location=[latitude, longitude], zoom_start=zoom)
folium.Marker([latitude, longitude], popup="Liberty Bell",
tooltip='Selected location').add_to(m)
st_folium(m, width=725)
def simulation(hbjson_path: Path) -> Union[Tuple[str, ApiClient],
Tuple[None, None]]:
"""UI for the simulation tab of the Estidama app."""
latitude = st.number_input('Latitude', value=24.453766,
help='Enter the latitude of the site.')
longitude = st.number_input('Longitude', value=54.377375,
help='Enter the longitude of the site.')
zoom = st.number_input('Zoom level', value=14)
st.write('Visually check location on the map.')
show_map(latitude, longitude, zoom)
api_key = st.text_input('Pollination API key', type='password')
owner = st.text_input('Project owner')
project = st.text_input('Project name')
study_name = st.text_input('Study name', value='Estidama-daylight')
study_description = st.text_input(
'Study description', value='PBRS LBi-7 Daylight & Glare compliance')
north_angle = st.number_input('North angle', value=0, min_value=0, max_value=360)
ground_reflectance = st.number_input(
'Ground reflectance', value=0.2, help='Enter a decimal number to indicate'
' the percentage of light being reflected back by the ground.')
submit = st.button('Submit')
if submit:
if not all([api_key, owner, project, latitude, longitude]):
st.error('Make sure to fill all the fields first.')
return None, None
api_client = ApiClient(api_token=api_key)
job = create_job(hbjson_path, api_client, owner, project,
study_name, study_description, latitude, longitude,
north_angle, ground_reflectance)
running_job = job.create()
time.sleep(2)
job_url = f'https://app.pollination.cloud/{running_job.owner}/projects/{running_job.project}/jobs/{running_job.id}'
st.success('Job submitted to Pollination. Move to the next tab.')
return job_url, api_client
return None, None