coniston/q_calculation.py

76 lines
2.6 KiB
Python
Raw Normal View History

2024-07-07 18:53:08 -04:00
import pandas as pd
import plotly.graph_objects as go
2024-07-10 09:40:57 -04:00
# Load the data
2024-07-07 18:53:08 -04:00
data = pd.read_csv('06-25_07-07.csv')
2024-07-10 09:40:57 -04:00
2024-07-07 18:53:08 -04:00
# Convert 'Date/Time (--4:0:0)' column to datetime
data['Date/Time (--4:0:0)'] = pd.to_datetime(data['Date/Time (--4:0:0)'])
2024-07-10 09:40:57 -04:00
# Calculate the time difference between consecutive points in minutes
data['Time Diff (min)'] = data['Date/Time (--4:0:0)'].diff().dt.total_seconds() / 60
# Set flow rate and specific heat capacity
flow_rate_gpm = 10
density_water = 62.4 # lbm/ft^3
flow_rate_lbm_min = flow_rate_gpm * 0.133681 * density_water # Convert GPM to lbm/min
# Calculate Q (BTU)
cp_water = 1 # BTU/lbm°F
data['Delta T (F)'] = data['Sewer Water Out'] - data['Sewer Water In']
data['Q (BTU)'] = flow_rate_lbm_min * cp_water * data['Delta T (F)'] * data['Time Diff (min)']
2024-07-07 18:53:08 -04:00
# Set the datetime column as the index
data.set_index('Date/Time (--4:0:0)', inplace=True)
# Apply a smoothing function (moving average)
window_size = 10
2024-07-10 09:40:57 -04:00
data['Q Smooth'] = data['Q (BTU)'].rolling(window=window_size, center=True).mean()
2024-07-07 18:53:08 -04:00
# Ensure index is a datetime index for ease of filtering
data.index = pd.to_datetime(data.index)
# Create the plot
fig = go.Figure()
# Add traces for each line with spline smoothing and increased visibility
fig.add_trace(go.Scatter(x=data.index, y=data['Q Smooth'], mode='lines', name='Transferred Heat (BTU/lb)',
2024-07-10 09:40:57 -04:00
line=dict(shape='spline', color='blue', width=2, dash='solid')))
2024-07-07 18:53:08 -04:00
# Add shading to separate days
for i in range((data.index[-1] - data.index[0]).days + 1):
start = data.index[0] + pd.Timedelta(days=i)
end = start + pd.Timedelta(days=1)
fig.add_vrect(
x0=start, x1=end,
fillcolor='grey' if i % 2 == 0 else 'white',
opacity=0.1,
line_width=0
)
# Update layout for better styling and to set figure size
fig.update_layout(
title=dict(
text='Transferred Heat',
font=dict(size=20, family='Arial', color='black', weight='bold')
),
xaxis_title=dict(
text='Date/Time',
font=dict(size=18, family='Arial', color='black', weight='bold')
),
yaxis_title=dict(
2024-07-10 09:40:57 -04:00
text='Energy (BTU)',
2024-07-07 18:53:08 -04:00
font=dict(size=18, family='Arial', color='black', weight='bold')
),
legend=dict(
title=dict(font=dict(size=15, family='Arial', color='black', weight='bold')),
font=dict(size=14, family='Arial', color='black', weight='bold')
),
font=dict(size=14),
width=1400, # Set the width of the figure
height=800 # Set the height of the figure
)
# Save the plot (requires kaleido package)
fig.write_image('heat.png')
# Display the plot
fig.show()