Darren Wiens
commited on
app first pass
Browse files- .gitignore +2 -0
- app.py +94 -0
- data/prov_stat.geojson +0 -0
- requirements.txt +7 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.envrc
|
| 2 |
+
.direnv
|
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import io
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
import geopandas as gpd
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import numpy as np
|
| 9 |
+
import streamlit as st
|
| 10 |
+
import xarray as xr
|
| 11 |
+
|
| 12 |
+
from geogif import gif
|
| 13 |
+
from rasterio import features
|
| 14 |
+
|
| 15 |
+
provinces = gpd.read_file("./data/prov_stat.geojson")
|
| 16 |
+
geoms = provinces.geometry.boundary
|
| 17 |
+
|
| 18 |
+
image = features.rasterize(
|
| 19 |
+
geoms,
|
| 20 |
+
out_shape=[381, 1081],
|
| 21 |
+
transform=(0.10000000149011612, 0.0, -160.0, 0.0, 0.10000000149011612, 32.0),
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
cmap = st.selectbox("Colormap", plt.colormaps(), index=1)
|
| 25 |
+
|
| 26 |
+
fps = st.slider("Frames per second", 0.01, 50.0, 16.0)
|
| 27 |
+
|
| 28 |
+
show_dt = st.checkbox("Show timestamp", True)
|
| 29 |
+
|
| 30 |
+
if st.button("Plot Data"):
|
| 31 |
+
smoke_url = "https://firesmoke.ca/forecasts/current/dispersion.nc"
|
| 32 |
+
smoke_nc = "./data/dispersion.nc"
|
| 33 |
+
if not os.path.isfile(smoke_nc):
|
| 34 |
+
response = requests.head(smoke_url)
|
| 35 |
+
|
| 36 |
+
total_size = int(response.headers.get("Content-Length"))
|
| 37 |
+
|
| 38 |
+
progress_text = "Downloading data. Please wait."
|
| 39 |
+
progress_bar = st.progress(0, text=progress_text)
|
| 40 |
+
|
| 41 |
+
size_downloaded = 0
|
| 42 |
+
chunk_size = 8192
|
| 43 |
+
|
| 44 |
+
with requests.get(smoke_url, stream=True) as r:
|
| 45 |
+
r.raise_for_status()
|
| 46 |
+
with open(smoke_nc, "wb") as f:
|
| 47 |
+
for chunk in r.iter_content(chunk_size=chunk_size):
|
| 48 |
+
f.write(chunk)
|
| 49 |
+
|
| 50 |
+
size_downloaded += chunk_size
|
| 51 |
+
percent_complete = min(size_downloaded / total_size, 1)
|
| 52 |
+
progress_bar.progress(percent_complete, text=progress_text)
|
| 53 |
+
|
| 54 |
+
progress_bar.empty()
|
| 55 |
+
|
| 56 |
+
ds_disk = xr.open_dataset(smoke_nc)
|
| 57 |
+
|
| 58 |
+
tstep = ds_disk.TFLAG
|
| 59 |
+
|
| 60 |
+
dts = []
|
| 61 |
+
for i in tstep:
|
| 62 |
+
data = i.data[0]
|
| 63 |
+
dt = datetime.datetime.strptime(
|
| 64 |
+
str(data[0]) + str(data[1]).zfill(6), "%Y%j%H%M%S"
|
| 65 |
+
)
|
| 66 |
+
dts.append(dt)
|
| 67 |
+
|
| 68 |
+
pm25 = ds_disk.PM25
|
| 69 |
+
|
| 70 |
+
pm25["TSTEP"] = dts
|
| 71 |
+
|
| 72 |
+
for idx, i in enumerate(pm25.data):
|
| 73 |
+
pm25[idx].data[0] = np.maximum(i[0], image * 6)
|
| 74 |
+
|
| 75 |
+
buffer = io.BytesIO()
|
| 76 |
+
|
| 77 |
+
if show_dt:
|
| 78 |
+
date_format = "%Y-%m-%d %H:%M:%S"
|
| 79 |
+
else:
|
| 80 |
+
date_format = None
|
| 81 |
+
|
| 82 |
+
gif(
|
| 83 |
+
pm25[:, :, ::-1, :],
|
| 84 |
+
date_format=date_format,
|
| 85 |
+
fps=fps,
|
| 86 |
+
cmap=cmap,
|
| 87 |
+
date_color=(255, 255, 255),
|
| 88 |
+
to=buffer,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
first_dt = dts[0]
|
| 92 |
+
last_dt = dts[-1]
|
| 93 |
+
|
| 94 |
+
st.image(buffer, caption=f"{first_dt} to {last_dt}")
|
data/prov_stat.geojson
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
rasterio==1.3.7
|
| 2 |
+
geopandas==0.13.2
|
| 3 |
+
matplotlib==3.7.1
|
| 4 |
+
netCDF4==1.6.4
|
| 5 |
+
numpy==1.24.3
|
| 6 |
+
xarray==2023.5.0
|
| 7 |
+
geogif==0.1.4
|