A Timelapse of UV Weather Data
The Project
Most days I check the UV (ultra violet) weather for my region. This is especially important during the summer months because the sunlight in New Zealand is harsh.
To check the UV weather forecast, I go to the NIWA UV forecasting site . I noticed that the forecast images are static URLs and that the image is updated each day. So, I just bookmark the URL and visit it in the morning to check the forcast for the day.
I thought it would be neat to have a series of these forecast images presented as an animation.
Getting the Images
I wrote a simple Python script (thanks, ChatGPT) that would download the image from the URL. It downloads images and saves them with the date as the name. The date format follows ISO-8601 format , as all good files should. This script will also check if the date has already been downloaded as a safety check.
I cannot verify that this is the safest code, YMMV .
import requests
import os
from datetime import datetime
# Define the URL of the image you want to download
image_url = "https://niwa.co.nz/static/uv-data/uvi_forecast/uvi_forecast_hamilton.png"
# Define the folder where you want to save the images
save_folder = os.path.abspath("")
# Put the absolute path folder in the quotation marks
# Create the folder if it doesn't exist
if not os.path.exists(save_folder):
os.makedirs(save_folder)
# Get the current date in the format YYYY-MM-DD
current_date = datetime.now().strftime("%Y-%m-%d")
# Define the filename with the current date
file_name = f"{current_date}.png"
# Check if an image with today's date already exists in the folder
if os.path.exists(os.path.join(save_folder, file_name)):
print(f"An image with today's date already exists: {file_name}")
else:
# Send a GET request to the image URL
response = requests.get(image_url)
# Check if the request was successful
if response.status_code == 200:
# Save the image with the current date as the filename
with open(os.path.join(save_folder, file_name), "wb") as file:
file.write(response.content)
print(f"Image downloaded and saved as: {file_name}")
else:
print(f"Failed to download the image. Status code: {response.status_code}")
I set up a cron job that would run each morning at 9 am so the forecast for the day is downloaded.
0 9 * * * /usr/bin/python3 [absolute reference for python script] > [absolute location for logfile]
This was all stored on a 24/7 server that would curate the images over time.
Creating the Animation
The animation itself is made using the convert tool from ImageMagick. The convert tool takes images and puts them together to make an animation. There are probably lots of different parameters that you can use with convert but I’ve stuck to this line of code.
convert -delay 7.5 -loop 0 ./images/*.png 7.5delay.gif
The delay parameter sets the time between images. I played around with the delay setting to get something that looked right to me. The delay setting is measured in centiseconds. A delay setting of 10 is 100 ms. I settled on a setting of 7.5 cs (75 ms). A delay of 10 felt too slow, where a delay of 5 was too fast.
The loop parameter sets how many time the animation loops. The 0 (zero) setting should mean it loops forever. I’m not sure if changing it will actually affect a gif, maybe something to tinker with another time.
Result
I’ve been collecting images from late September until now and the result is the animation at the top of this page.
I’m quite pleased with how it turned out. You can see when daylight savings changes in my region due to the shift of the UV curve, and the “Hours” label at the bottom of the charge changes. I’m going to continue gathering images for a year and then make a year long animation.