Make all the frames and combine into a video¶
To make the video we need to run the plot script thousands of times, making an image file for each point in time, and then to merge the thousands of resulting images into a single video.
To make a smooth video we generate one frame every hour (interpolated from once-daily data).
This script calls the plot script for each hour over a period:
#!/usr/bin/env python
# Make all the individual frames for a movie
import os
import cftime
import datetime
# Function to check if the job is already done for this timepoint
def is_done(year, month, day, hour, minute):
op_file_name = (
"%s/AnimH/visualizations/3_hr_precip/" + "%04d%02d%02d%02d%02d.png"
) % (
os.getenv("SCRATCH"),
year,
month,
day,
hour,
minute,
)
if os.path.isfile(op_file_name):
return True
return False
f = open("run.txt", "w+")
start_day = cftime.datetime(2021, 1, 1, 0, calendar="360_day")
end_day = cftime.datetime(2021, 12, 30, 18, calendar="360_day")
current_day = start_day
while current_day <= end_day:
if is_done(
current_day.year,
current_day.month,
current_day.day,
current_day.hour,
current_day.minute,
):
current_day = current_day + datetime.timedelta(minutes=15)
continue
cmd = (
"./make_frame.py --year=%d --month=%d "
+ "--day=%d --hour=%d --minute=%d"
+ "\n"
) % (
current_day.year,
current_day.month,
current_day.day,
current_day.hour,
current_day.minute,
)
f.write(cmd)
current_day = current_day + datetime.timedelta(minutes=15)
f.close()
We then run those jobs in parallel, either with GNU parallel or by submitting them to a batch system (I used the MO SPICE cluster).
When all the frame images are rendered we make a video using ffmpeg. We will render at 1080p resolution - 5Mb/s bandwidth. (Overkill for such a simple video, but now everyone has lots of internet bandwidth, so it’s not worth optimizing).
#!/bin/bash
cd $SCRATCH/AnimH/visualizations/
ffmpeg -r 36 -pattern_type glob -i 3_hr_precip/\*.png \
-c:v libx264 -threads 16 -preset veryslow -tune film \
-profile:v high -level 4.2 -pix_fmt yuv420p \
-b:v 5M -maxrate 5M -bufsize 20M \
-c:a copy 3_hr_precip.mp4