-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfeels_like_temperature.py
More file actions
121 lines (96 loc) · 4.48 KB
/
feels_like_temperature.py
File metadata and controls
121 lines (96 loc) · 4.48 KB
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
import streamlit as st
import plotly.express as px
import pandas as pd
import plotly.graph_objs as go
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
# Function to calculate Heat Index
def calculate_heat_index(T, RH):
# https://wonder.cdc.gov/wonder/help/Climate/ta_htindx.PDF
HI = (-42.379 + 2.04901523 * T + 10.14333127 * RH
- 0.22475541 * T * RH - 0.00683783 * T**2
- 0.05481717 * RH**2 + 0.00122874 * T**2 * RH
+ 0.00085282 * T * RH**2 - 0.00000199 * T**2 * RH**2)
return HI
# Function to calculate Wind Chill
def calculate_wind_chill(T, V):
# https://unidata.github.io/MetPy/v0.10/_static/FCM-R19-2003-WindchillReport.pdf
WC = 35.74 + 0.6215 * T - 35.75 * (V**0.16) + 0.4275 * T * (V**0.16)
return WC
# Wind speed conversion to Beaufort scale
def beaufort_to_kmh(beaufort):
scale = [0, 1, 4, 8, 13, 19, 25, 32, 39, 47, 55, 64, 73]
return scale[beaufort]
def main():
# Streamlit App
st.title("Feels-Like Temperature Calculator")
# User inputs
temp_celsius = st.sidebar.number_input("Select temperature in degrees celcius", min_value=-10, max_value=50, value=30)
T_F = celsius_to_fahrenheit(temp_celsius)
data = []
if T_F >= 80:
# Calculate Heat Index
RH_input = st.sidebar.number_input("Relative humidity", min_value=0, max_value=100, value=70)
feels_like_F_ = calculate_heat_index(T_F, RH_input)
feels_like_C_low_ = fahrenheit_to_celsius(feels_like_F_-1.3)
feels_like_C_high_ = fahrenheit_to_celsius(feels_like_F_+1.3)
feels_like_C_ = fahrenheit_to_celsius(feels_like_F_)
st.sidebar.info(f"Feels like : {round(feels_like_C_,1)} [{round(feels_like_C_low_,1)}-{round(feels_like_C_high_,1)}] ")
for RH in range (0,101):
feels_like_F = calculate_heat_index(T_F, RH)
feels_like_C = round(fahrenheit_to_celsius(feels_like_F),2)
feels_like_C_low = round(fahrenheit_to_celsius(feels_like_F-1.3),2)
feels_like_C_high = round(fahrenheit_to_celsius(feels_like_F+1.3),2)
data.append({"RH": RH, "Feels-Like Temperature (°C)": feels_like_C, "low": feels_like_C_low, "high": feels_like_C_high})
df = pd.DataFrame(data)
# Create the line for Feels-Like Temperature
line = go.Scatter(
x=df['RH'],
y=df['Feels-Like Temperature (°C)'],
mode='lines',
name='Feels-Like Temperature (°C)'
)
# Create the filled area between low and high
fill = go.Scatter(
x=df['RH'].tolist() + df['RH'][::-1].tolist(),
y=df['high'].tolist() + df['low'][::-1].tolist(),
fill='toself',
fillcolor='rgba(128, 128, 128, 0.2)',
line=dict(color='rgba(255, 255, 255, 0)'),
showlegend=False,
name='Range'
)
# Create the layout
layout = go.Layout(
title='Feels-Like Temperature vs. Relative Humidity',
xaxis=dict(title='Relative Humidity (%)'),
yaxis=dict(title='Feels-Like Temperature (°C)'),
showlegend=True,
)
# Create the figure and add traces
fig = go.Figure(data=[fill, line], layout=layout)
# Show the plot
st.plotly_chart(fig)
elif T_F <= 50:
# Calculate Wind Chill
for beaufort in range(0, 13):
wind_speed_kmh = beaufort_to_kmh(beaufort)
V_mph = wind_speed_kmh * 0.621371
if V_mph >= 3:
feels_like_F = calculate_wind_chill(T_F, V_mph)
feels_like_C = fahrenheit_to_celsius(feels_like_F)
else:
feels_like_C = temp_celsius
data.append({"Beaufort": beaufort, "Feels-Like Temperature (°C)": feels_like_C})
df = pd.DataFrame(data)
# Plotting with Plotly
fig = px.line(df, x="Beaufort", y="Feels-Like Temperature (°C)", title="Feels-Like Temperature vs. Wind Speed (Beaufort Scale)", labels={"Beaufort": "Wind Speed (Beaufort Scale)", "Feels-Like Temperature (°C)": "Feels-Like Temperature (°C)"})
st.plotly_chart(fig)
else:
st.info("Temperature is in ragnge [10-26.7°C]. Feel like temperarure is same as actual temperature ")
if __name__ == "__main__":
main()