Skip to content

Commit

Permalink
concatenating precipitation and pressure
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimmudassar committed Sep 2, 2023
1 parent 701792b commit c4c9b39
Showing 1 changed file with 84 additions and 48 deletions.
132 changes: 84 additions & 48 deletions precipitation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from datetime import datetime

import pandas as pd
import plotly.graph_objects as go
import requests
from discord_webhook import DiscordEmbed, DiscordWebhook # Connect to discord
from environs import Env # For environment variables
import pandas as pd
import plotly.graph_objects as go


env = Env()
env.read_env() # read .env file, if it exists
Expand All @@ -16,9 +17,24 @@ def embed_to_discord():
# Elevation
embed.add_embed_field(name='⛰️ Elevation', value=f"{data['elevation']} m")

# Precip Total
embed.add_embed_field(
name='🌧️ Total', value=f"{data['daily']['precipitation_sum'][0]} {data['daily_units']['precipitation_sum']}")
# Precip Total but only if precipitation_sum > 0
if any(elem != 0 for elem in df['precipitation']):
embed.add_embed_field(
name='🌧️ Total', value=f"{data['daily']['precipitation_sum'][0]} {data['daily_units']['precipitation_sum']}")

# Pressure High
surface_pressure_high = df.loc[df["surface_pressure"].idxmax()]
surface_pressure_high_time = datetime.fromisoformat(
surface_pressure_high["time"]).strftime("%H:%M")
embed.add_embed_field(name='☁️ Pressure ⬆️ High',
value=f"{surface_pressure_high['surface_pressure']} hPa at {surface_pressure_high_time}", inline=True)

# Pressure Low
surface_pressure_low = df.loc[df["surface_pressure"].idxmin()]
surface_pressure_low_time = datetime.fromisoformat(
surface_pressure_low["time"]).strftime("%H:%M")
embed.add_embed_field(name='☁️ Pressure ⬇️ Low',
value=f"{surface_pressure_low['surface_pressure']} hPa at {surface_pressure_low_time}", inline=True)

# set image
embed.set_image(url='attachment://fig1.png')
Expand All @@ -41,56 +57,76 @@ def embed_to_discord():
lat = env('LATITUDE')
long = env('LONGITUDE')

# baseline to measure the difference in on the graph in hPa
STANDARD_PRESSURE = 1013.25

data = requests.get(
f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={long}&daily=precipitation_sum&hourly=rain,showers,snowfall&models=best_match&forecast_days=1&timezone=EST").json()
f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={long}&daily=precipitation_sum&hourly=surface_pressure,precipitation&models=best_match&forecast_days=1&timezone=EST").json()

all_categories = {}
for categories in data["hourly"]:
all_categories[categories] = data["hourly"][categories]
df = pd.DataFrame(all_categories)

if data["daily"]["precipitation_sum"][0] > 0:
fig = go.Figure()
fig = go.Figure()

fig.add_trace(go.Scatter(
x=df["time"],
y=df["rain"],
fill='tozeroy',
mode='lines',
name="rain"
))
fig.add_trace(go.Scatter(
x=df["time"],
y=df["showers"],
fill='tozeroy',
mode='lines',
name="showers"
))
fig.add_trace(go.Scatter(
x=df["time"],
y=df["snowfall"],
fill='tozeroy',
mode='lines',
name="snowfall"
))

fig.update_yaxes(ticklabelstep=2)
fig.add_trace(go.Scatter(
x=df['time'],
y=df['surface_pressure'] - STANDARD_PRESSURE,
name="Relative Pressure (STP)",
fill='tozeroy',
))

# Labels
fig.update_layout(title={'text': 'Precipitation vs. Time', 'x': 0.5, 'xanchor': 'center'},
yaxis_zeroline=True,
xaxis_zeroline=True,
xaxis_title="Time",
yaxis_title="Millimeters of Precipitation")

# Attribution
fig.add_annotation(text="By: Ibrahim Mudassar",
xref="paper", yref="paper",
x=1, y=-0.14,
showarrow=False,
align="center",
font=dict(size=9))

fig.write_image("fig1.png", width=1080, height=720)
if any(elem != 0 for elem in df['precipitation']):
fig.add_trace(go.Scatter(
x=df['time'],
y=df['precipitation'],
name="Precipitation",
yaxis="y2"
))

embed_to_discord()
# Create axis objects
fig.update_layout(
yaxis=dict(
title="yaxis title",
titlefont=dict(
color="#1f77b4"
),
tickfont=dict(
color="#1f77b4"
)
),
yaxis2=dict(
title="yaxis2 title",
titlefont=dict(
color="#ff7f0e"
),
tickfont=dict(
color="#ff7f0e"
),
anchor="x",
overlaying="y",
side="right",
),
)

# Labels
fig.update_layout(title={'text': 'Pressure vs. Time', 'x': 0.5, 'xanchor': 'center'},
yaxis2_zeroline=True,
xaxis_zeroline=True,
xaxis_title="Time",
yaxis_title="Difference from STP (In hPa)")


# Attribution
fig.add_annotation(text="By: Ibrahim Mudassar",
xref="paper", yref="paper",
x=1, y=-0.14,
showarrow=False,
align="center",
font=dict(size=9))

fig.write_image("fig1.png", width=1080, height=720)

embed_to_discord()

0 comments on commit c4c9b39

Please sign in to comment.