Aurora Monitoring with PowerShell

Wed, Aug 4, 2010 2-minute read

Over the past few days, Astronomy resources have been making a big splash about a couple of coronal mass ejections (CME) primed to cause widespread visibility of the Aurora Borealis. Last night was when the first one hit, and its effects extended deeply into southern Canada and parts of the northern US.

There may be another round tonight, so maybe this post can help you catch your first Aurora :)

NOAA has a couple of good resources, and one I was keeping special track of shows where the effects are the greatest:

aurora_2010-08-04-14-49

Areas that are red are seeing the greatest magnetic activity.

After refreshing this page a bunch last night and watching as the red started getting closer and closer to Washington state. It was getting late and I still couldn’t see it, so I decided instead to write a script to alert me when it peaked.

This script measures the amount of red over (and above) Washington State, and keeps monitoring until it senses a decrease. Once it senses a 5% decline, it alerts me. Crude image recognition at its finest!

## Launch the interesting sites
Start-Process "http://www.swpc.noaa.gov/pmap/pmapN.html"
Start-Process "http://www.swpc.noaa.gov/rt_plots/pro_3d.html"
Add-Type -Assembly System.Drawing
$wc = New-Object System.Net.WebClient

## The monitor region. Get these coordinates from the X,Y positions
## your mouse shows when hovering around any saved file (pmapN.gif) in mspaint.
$startX = 130
$startY = 300
$endX = 144
$endY = 339

## Get the "Aurora Index" for the given region
function Get-AuroraIndex
{
    ## Download the file
    $url = "http://www.swpc.noaa.gov/pmap/gif/pmapN.gif"
    $null = New-Item -Type Directory c:\temp\aurora -Force
    $destination = "c:\temp\aurora\aurora_" + ((Get-Date).ToString("yyyy-MM-dd-HH-mm")) + ".gif"

    try
    {
        $wc.DownloadFile($url, $destination)
    }
    catch { return }

   
    $image = [System.Drawing.Bitmap]::FromFile($destination)

    $totalRed = 0
    for($x = $startX; $x -lt $endX; $x++)
    {
        for($y = $startY; $y -lt $endY; $y++)
        {
            $totalRed += $image.GetPixel($x, $y).R
        }
    }

    $totalRed
}

$lastValue = $null

while($true)
{
    $currentValue = Get-AuroraIndex
    if($currentValue)
    {
        ## Catch the rising aurora
        if($currentValue -gt ($lastValue * 1.05))
        {
            $lastValue = $currentValue
            "Aurora is rising @ " + (Get-Date) + ". Current value: $currentValue"
        }

        ## Catch a falling aurora
        elseif($currentValue -lt ($lastValue * 0.95))
        {
            "Aurora is falling @ " + (Get-Date) + ". Current value: $currentValue"
            while($true)
            {
                C:\windows\Media\notify.wav
                Start-Sleep 5
            }
        }
       
        ## Aurora is stable
        else
        {
            "Aurora is stable @ " + (Get-Date) + ". Current value: $currentValue"
        }
    }

    Start-Sleep 30
}