Chasing Mountain Sunrises and Capturing Northern Lights: A DIY Sunrise Ranking and Aurora Detection Project

Introduction:

For many of us, the day-to-day life often keeps us away from the natural beauty of the world. I, too, found myself in this situation, unable to escape to the mountains as frequently as I'd like. But, what if there was a way to bridge that gap? In this blog post, we'll explore a Python project that arose from a desire to feel closer to the mountains, allowing me to catch a glimpse of the mountains and the aurora borealis even on the busiest of days. Join me as we delve into how I created an automated system to detect and rate stunning mountain sunrises and serendipitously ended up doing the same for Northern Lights.

The Main Project: Sunrise Detection and Rating:

For a significant amount of days in the year, I miss the sunrise as I am going about my day. This seemed like a problem that could be solved with some automation! The goal was to create an automated system which would retrieve an image of the sunrise, rate it, and show it to me if it was above a certain threshold. I will go into each section below.

Retrieving the images:

Using Python, I created a script that accesses the latest image from a public webcam set up in the mountains, and saves it to a local folder on my computer. There are many ways to do this but here is what I did:

I inspected the webpage and found where the images were being stored. The webcam saves a new image about every 20 minutes during the day and then about every hour during the night, but this varies a bit it seems. The images are stored online named with the date and time, so my script determines the current time using the datetime library and then iteratively checks to see if there is a file with that data and time until it finds the latest image.

For now, I just run this in a While loop that runs once every 10 minutes to get the latest image. So every 10 minutes the latest image is saved. If the image was already saved, it is written over so I don’t have any duplicates.

Now all the images are continuously saved!

Ranking a Sunset:

I can now create a script to rank these images from 0-10. The following function takes in an image and color range then outputs the % of the image with that color range.

The images can then be loaded in, and this function can be used on them. The following code shows how I use this function to detect 3 different ranges that I found to be in my “good” sunrise images. I won’t bother showing how to load the images in but num_images is just a list of images I have. After playing around with color thresholds, I found there to be two ranges of pink and one of red that I needed to use. I then add all the calculated % together and scale it out of 10.

The output of this is a plot with the ratings on it that is useful to determine if the script is correctly identifying “good” sunrises.

We can see that the two best images are rated very highly and the other are quite low. I can now add this mini “model” to my script which retrieves the images. In this way I can check every image that it saves. If an image is above a 7/10, it is saved to a separate folder on in my iCloud. This way the good images can be shared from my task computer to my other computers.

Automated Desktop Background Updates:

So that I don’t need to check in for good sunrises, I've created a folder automation on the Mac I use most often. This automation automatically updates my background on my Mac when a new image is added to the iCloud folder.

As a result, my desktop is always displaying the latest "good" sunrises, providing me with a dynamic connection to the mountains even during work hours.

This was done using the Automator application on Mac and creating an apple script that is run whenever the folder is modified. I just created a new Folder Action automation, then added this apple script to run in the automation:

Then I just set the folder to be controlled by this action by using the Folder Actions Setup. Now any new file added, will cause my desktop background to update accordingly!

Serendipitous Discovery: Northern Lights in the Night Sky:

Sometimes, the best discoveries happen by chance. While developing the script to save images from the webcams, I chose to save all the images, both day and night, with the hope of finding additional uses for them. After setting up the script, I noticed that it had captured images of the Northern Lights on three consecutive nights.

Inspired by the unexpected capture of the Northern Lights, I decided to create a simple detection model to identify them in the night images. While my original plan was to focus on detecting sunrises, this felt like a golden opportunity. I developed a color detection model that assessed the amount of green in the night images. This approach worked quite well for identifying the presence of the Northern Lights.

Using a very similar approach to the color detection for sunrise, I got this output for detecting northern lights:

I just detected green instead of red and pink, then scaled appropriately. Pretty simple but it seems to work well!

Future Possibilities:

As I gather more data over time, it could be interesting to create a neural network that rates sunsets. I am not sure that it will really do a better job but it would an interesting comparison to make!

I may also consider using my northern lights detection script with my Home Assistant system. This step will enable me to run the entire project off Home Assistant and receive push notifications to my phone when there have been some Northern Lights that night!. It means I can go about my daily routine without constant monitoring, and only get notified when there's something truly worth seeing.

Next
Next

Automated Drapes: Your Key to Sunrise on Demand