The Sunrise Wallpaper Project

Script Viewer

The following scripts are used to calculate sunrise time, capture the images, transfer the images to the web server, resize the images for thumbnails and many other tasks. Some of the variables in the scripts below have been replaced by placeholders in square brackets. This is to maintain a certain level of protection from abuse - I certainly don't need spammers harvesting email addresses from here, or people downloading the images directly from the limited bandwidth imaging server.

Don't forget to check out the technical write-up too!

Capture.bat
This is the main script that captures the images, called by RunCapture.bat. It waits until sunrise, then starts the PSRemote camera application to capture the images. Once the required number of images has been taken, the script retrieves the current weather data and finally sends an email to my mail server. When the mail server recieves the message it runs GetImages.bat which downloads the images to the web server, where they become available for users to download.
Copy of GetImages.bat
This script retrieves the images from the imaging computer. It is called from the mail server, when an email message is recieved informing it that new images are available for download. Originally, I was going to do this with FTP, but it was just too problematic trying to get it to work through two firewalls.
GetImages.bat
This script retrieves the images from the imaging computer. It is called from the mail server, when an email message is recieved informing it that new images are available for download. Originally, I was going to do this with FTP, but it was just too problematic trying to get it to work through two firewalls.
ParseXML.cs
This very small C# program is used to parse the XML weather data in order to discover sunrise time for the day. Once sunrise has been determined, the program writes out RunCapture.bat - a single-line batch file that calls the main Capture file to actually do the work.
Run.bat
This script is launched every day by the Windows scheduler at 3am (well before sunrise), to download the weather data from weather.com. Once the weather data is retrieved, a small C# application (ParseXML) is executed to parse the XML data and extract the sunrise time. ParseXML writes a one-line batch file (RunCapture.bat) containing the sunrise time, the number of images to capture and the time interval between each image. RunCapture is then executed and this script waits to be launched again tomorrow to update the sunrise time.
RunCapture.bat
This file is recreated every day by run.bat, with an updated sunrise time. It simply calls the main capture script, capture.bat.

Capture.bat

@echo off
:: %1 = Sunrise Time
:: %2 = Photo count
:: %3 = Delay (in seconds) between photos
 
cls
set psrDir="C:\Program Files\BreezeSys\PSRemote"
set psrApp="C:\Program Files\BreezeSys\PSRemote\PSRemote.exe"
set psrBat="C:\Program Files\BreezeSys\PSRemote\PSRemoteTest\PSRemoteTest.exe"
set psrOut=c:\psremote
set photoCount=0
set sleepfrequency=10
set targetFolder=%DATE:~10,4%\%DATE:~4,2%\%DATE:~7,2%
set weatherFile=%psrOut%\%targetFolder%\weather.xml
 
echo Starting Image Capture...
echo Sunrise Time: %1
echo Photo Count: %2
echo Image Spacing: %3 seconds
echo Capture Folder: %psrOut%\%targetFolder%
echo.
echo Waiting for Sunrise...
 
set prevTime=empty
 
:sleepLoop
sleep %sleepFrequency%
if not "%prevTime%"=="%TIME:~0,5%" echo %TIME:~0,5%
set prevTime=%TIME:~0,5%
if not "%TIME:~0,5%" == %1 goto sleepLoop
 
:: Start the camera
echo Starting PSRemote...
start "" %psrApp%
 
:: Wait for the camera to be ready
sleep %3
 
:: Take a shot
:shotLoop
set /A photoCount=%photoCount%+1
echo Capturing Image %photoCount% of %2...
start "Capturing Image %photoCount%" %psrBat%
sleep %3
if not "%photoCount%"=="%2" goto shotLoop
 
:: Get the current weather
mkdir %psrOut%\%targetFolder%
htget -v "http://xoap.weather.com/weather/local/USCA1229?cc=*&prod=xoap&par=1016000997&key=59865ab53b8f7846" "%weatherFile%"
 
:: Terminate the program
echo Image Capture Complete!
echo Terminating PSRemote...
%psrBat% -X
 
:: Send a notification email to the server to get it to download the latest images
:: We only have to this because FTP though two firewalls is just too painful for words.
:email
bmail -s [MailServer] -t [EmailAddress] -r [EmailAddress] -h -a "%date% - Images are Available"
goto :eof

Copy of GetImages.bat

@echo off
 
set basePath=d:\mount oso
set targetFolder=2007\11\17
set imageCount=0
 
mkdir "%basepath%\%targetFolder%\thumbs"
mkdir "%basepath%\%targetFolder%\preview"
 
:getLoop
set /A imageCount=%imageCount%+1
echo Retrieving image %imageCount% from [ImageCaptureComputer]
start /wait "Retrieving Image %imageCount%" htget -v "http://[ImageCaptureComputer]:[port]/%targetfolder%/000%imageCount%.jpg" "%basepath%\%targetFolder%\000%imageCount%.jpg"
if not "%imageCount%" == "4" goto getLoop
 
start /wait "Retrieving weather data" htget -v "http://[ImageCaptureComputer]:[port]/%targetfolder%/weather.xml" "%basepath%\%targetFolder%\weather.xml"
 
d:
cd "%basepath%\%targetFolder%"
 
:: Delete any zero-length files
for %%I in (*.jpg) do if "%%~zI"=="0" del "%%I"
 
:: Convert the images into thumbnails
for %%v in (*.jpg) do "c:\program files\imgman\imgman.exe" %%v -scale h180 thumbs\thumb_%%v
for %%v in (*.jpg) do "c:\program files\imgman\imgman.exe" %%v -scale h480 preview\preview_%%v
 
if not exist *.jpg rmdir thumbs
if not exist *.jpg rmdir preview
 
cd "\mount oso"
 
:end

GetImages.bat

@echo off
 
set basePath=d:\mount oso
set targetFolder=%DATE:~10,4%\%DATE:~4,2%\%DATE:~7,2%
set imageCount=0
 
mkdir "%basepath%\%targetFolder%\thumbs"
mkdir "%basepath%\%targetFolder%\preview"
 
:getLoop
set /A imageCount=%imageCount%+1
echo Retrieving image %imageCount% from [ImageCaptureComputer]
start /wait "Retrieving Image %imageCount%" htget -v "http://[ImageCaptureComputer]:[port]/%targetfolder%/000%imageCount%.jpg" "%basepath%\%targetFolder%\000%imageCount%.jpg"
if not "%imageCount%" == "4" goto getLoop
 
start /wait "Retrieving weather data" htget -v "http://[ImageCaptureComputer]:[port]/%targetfolder%/weather.xml" "%basepath%\%targetFolder%\weather.xml"
 
d:
cd "%basepath%\%targetFolder%"
 
:: Delete any zero-length files
for %%I in (*.jpg) do if "%%~zI"=="0" del "%%I"
 
:: Convert the images into thumbnails
for %%v in (*.jpg) do "c:\program files\imgman\imgman.exe" %%v -scale h180 thumbs\thumb_%%v
for %%v in (*.jpg) do "c:\program files\imgman\imgman.exe" %%v -scale h480 preview\preview_%%v
 
if not exist *.jpg rmdir thumbs
if not exist *.jpg rmdir preview
 
cd "\mount oso"
 
:end

ParseXML.cs

using System;
using System.Data;
using System.IO;
 
namespace ParseXML
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
            ds.ReadXml( args[ 0 ] );
 
            DateTime dt = Convert.ToDateTime( ds.Tables[ "loc" ].Rows[ 0 ][ "sunr" ] );
            dt = dt.AddSeconds( Convert.ToInt32( args[ 3 ] ) * -2 );
            string strTime = ( dt.Hour < 10 ? " " : "" ) + dt.Hour.ToString() + ":" + dt.Minute.ToString( "00" );
            StreamWriter sw = new StreamWriter( args[ 1 ], false );
            sw.Write( "@call capture.bat " +
                "\"" + strTime + "\" " +
                args[ 2 ] + " " +
                args[ 3 ] + "\r\n" );
            sw.Close();
        }
    }
}

Run.bat

@echo off
 
set xml=c:\psremote\weather.xml
 
:: Get the XML file containing sunrise time for today
start "" /wait "htget" -v "http://xoap.weather.com/weather/local/USCA1229?cc=*&prod=xoap&par=1016000997&key=59865ab53b8f7846" "%xml%"
 
:: Run the C# application to parse the XML and create RunCapture.bat
start "" /wait "C:\User\Mount Oso\ParseXML\bin\Release\ParseXML.exe" "%xml%" "c:\user\mount oso\temp.bat" 4 300
 
:: Copy the comments back into the top of the batch file. This is for the benefit of the Scriptviewer on the web site.
c:
cd "\user\mount oso\"
copy RunCapture.txt RunCapture.bat
type temp.bat >> RunCapture.bat
del temp.bat
 
:: Start the capture process
RunCapture

RunCapture.bat

@call capture.bat " 6:30" 4 300