From 004fb1c57e7637bcb10b52ff8cb61f11d6c2b52c Mon Sep 17 00:00:00 2001 From: Bryce Allen Date: Sat, 13 May 2023 14:53:52 -0400 Subject: [PATCH] julia: refactor script for animations --- create_latest_map.jl | 45 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/create_latest_map.jl b/create_latest_map.jl index 575982d..5439dda 100644 --- a/create_latest_map.jl +++ b/create_latest_map.jl @@ -103,13 +103,23 @@ function get_realtime_dataframe(station_name; timezone=tz"America/New_York") return df end -function plot_station_data(station_name, station_map, station_x, station_y, df, i) +function plot_station_data(station_name, station_map, station_x, station_y, + df, i) + fig = Figure(resolution=size(station_map)) + ax = CairoMakie.Axis(fig[1, 1]) + hidespines!(ax) + hidedecorations!(ax) + plot_station_data_to_axis(ax, station_name, station_map, + station_x, station_y, df, i) + return fig +end + +function plot_station_data_to_axis(ax, station_name, station_map, station_x, station_y, + df, i) obs_time = df.TIME[i] - fig, ax, p = image(rotr90(station_map), - axis=(aspect=DataAspect(), - title="$station_name $obs_time")) - hidespines!(ax) - hidedecorations!(ax) + obs_time_s = Dates.format(obs_time, "e, u d H:MM") + image!(ax, rotr90(station_map)) + ax.title = "$station_name $obs_time_s" wind_rad = deg2rad(90 - df.WDIR[1]) wind_mph = df.WSPD[i] txt = @sprintf "Air: %.1f F, Water: %.1f F, Wind: %.1f mph" df.ATMP[i] df.WTMP[i] wind_mph @@ -117,7 +127,26 @@ function plot_station_data(station_name, station_map, station_x, station_y, df, tooltip!(ax, station_x, station_y, txt; offset=10) arrows!(ax, [Point2f(station_x, station_y)], [wind_vec], fxaa=true, linewidth=2, align=:center, arrowhead='⟰') - return fig + return ax +end + +function save_animation(station_name, out_path, days, real_hour_per_animation_second=1) + lat, lon = get_station_lat_lon(station_name) + station_map, station_x, station_y = get_map(lat, lon, 14) + df = get_realtime_dataframe(station_name; timezone=tz"America/New_York") + diff_hours = (df.TIME[0] - df.TIME[1]).value / 1000 / 3600 + fps = real_hour_per_animation_second / diff_hours + nframes = fps * days * 24 * 3600 + + fig = Figure(resolution=size(station_map)) + ax = Axis(fig[1, 1]) + hidespines!(ax) + hidedecorations!(ax) + + record(fig, out_path, range(1, nframes); framerate=fps) do i + plot_station_data_to_axis(ax, station_name, station_map, + station_x, station_y, df, nframes - i + 1) + end end function main() @@ -133,7 +162,7 @@ function main() end lat, lon = get_station_lat_lon(station_name) - station_map, station_x, station_y = get_map(lat, lon, 14) + station_map, station_x, station_y = get_map(lat, lon, 14) df = get_realtime_dataframe(station_name; timezone=tz"America/New_York") fig = plot_station_data(station_name, station_map, station_x, station_y, df, 1)