%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2009-2012, Centre for Advanced Internet Architectures
% Swinburne University of Technology, Melbourne, Australia
% (CRICOS number 00111D).
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% makemovie.m
% A script for Matlab to draw a world map using the Mapping Toolbox, 
% draw a "heatmap" of game client probes extracted from a .shp file, 
% calculate and draw a CDF over the longitude, plot a day-night terminator on top 
% and save the result in a .png file.
% This procedure is repeated for every .shp file found in the current folder.
%
% This script is the final step to create the single frames for animations as found 
% at http://caia.swin.edu.au/sting/gametraffic/anim100309A/
%
% It will create one image for each .shp file (converted from a .kml file) found in the 
% current folder. The date and time encoded in the filename is converted to UTC
% (as generated by the per_15min_kml.py script) and used to generated the correct 
% day-night terminator. It is also used to generate the title of the graph, 
% which functions as a clock in the final animation.
% The heatmap is generated using the "makesymbolspec" function, and the color of the dots 
% representing the location of game clients is chosen by the alitude of the location, 
% which represents the number of addresses per location.
% For saving the image "print" rather than "saveas" has been chosen for speed.
%
% Running this script may take quite a while. 
% The animations at http://caia.swin.edu.au/sting/gsanimations/ have been created 
% using 7200 single images.
%
% This file is part of the animation_scripts.tar.gz tarball that can be obtained at:
% http://caia.swin.edu.au/sting/tools/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This software was developed by Mattia Rossi <mrossi@swin.edu.au>
%
% This software has been made possible in part by a grant from
% APNIC Pty. Ltd., Canberra, Australia. http://www.apnic.net/
%
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions
% are met:
% 1. Redistributions of source code must retain the above copyright
%    notice, this list of conditions and the following disclaimer.
% 2. Redistributions in binary form must reproduce the above copyright
%    notice, this list of conditions and the following disclaimer in the
%    documentation and/or other materials provided with the distribution.
% 3. The names of the authors, the "Centre for Advanced Internet Architecture"
%    and "Swinburne University of Technology" may not be used to endorse
%    or promote products derived from this software without specific
%    prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
% FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
% OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
% SUCH DAMAGE.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function M=makemovie(lat,long)
cmap=hot(30);
dotspec=makesymbolspec('Point',{'Default','MarkerEdgeColor',cmap(30,:),'Marker','.'},{'altitude',[0 30],'MarkerEdgeColor',cmap(:,:),'Marker','.'});
d=dir('*.shp');
count=numel(d);
for i= 1:count
    date=[sscanf(d(i).name,'sting-%4d%2d%2d-%2d%2d.shp');0];
    utcnum=datenum(date')-(10/24);
    utc=datevec(utcnum);
    % we don't want a window to pop up all the time
    figure('visible','off');
    plotdaynightterminator(utc(3),utc(2),utc(1),utc(4),utc(5),utc(6));
    h3=gca;
    tit=sprintf('%2d/%2d/%4d %02d:%02d UTC',utc(3),utc(2),utc(1),utc(4),utc(5));
    set(h3,'PlotBoxAspectRatio',[200 100 1], 'YLim',[-90 90], 'XLim', [-180 180]);
    s=shaperead(d(i).name);
    mapshow(long,lat,'Color','black');
    mapshow(s,'SymbolSpec',dotspec');
    sts=[[s.X]',[s.altitude]'];
    t=sum(sts(:,2));
    sts=[sts,cumsum(sts(:,2))/t];
    sts=sortrows(sts,1);
    sts(:,3)=cumsum(sts(:,2))/t;
    h1=gca;
    h2 = axes('Position',get(h1,'Position'));
    plot(sts(:,1),sts(:,3),'Color','green','LineWidth',1,'DisplayName',text);
    set(h2,'YAxisLocation','right','Color','none','XTickLabel',[]);
    set(h2,'XLim', [-180 180],'Layer','top');
    set(h2,'PlotBoxAspectRatio',get(h1,'PlotBoxAspectRatio'));
    ylabel(h2,'CDF');
    ylabel(h3,'Latitude');
    set(h3,'Position',[0.055,0.01,0.88,0.99],'FontName','Helvetica','Box','off','XTick',[]);
    set(h1,'Position',[0.055,0.01,0.88,0.99],'FontName','Helvetica','XTick',[],'YTick',-90:30:90,'XTick',[],'XLim', [-180 180]);
    set(h2,'Position',[0.055,0.01,0.88,0.99],'FontName','Helvetica','Box','off','TickDir','out','XTick',-180:30:180,'XTickLabel',-180:30:180);
    xlabel(h2,'Longitude');
%     tmpdate = sscanf(d(i).name,'%*6s%4s%2s%2s-%2s%2s%*4s');
%     tit=sprintf('%2s/%2s/%4s - %2s:%2s',tmpdate(7:8),tmpdate(5:6),tmpdate(1:4),tmpdate(9:10),tmpdate(11:12));
    th=title(h1,tit);
    set(th,'FontName','Helvetica','Fontsize',12,'FontWeight','bold');
%     M(i)=getframe();
    filename=sprintf('client%04d.png',i);
    set(gcf,'OuterPosition',[0,0,720,405]);
    set(gcf,'Position',[0,0,720,405]);
    set(gcf,'PaperUnits','points');
    set(gcf,'PaperSize',[720 405]);
    set(gcf,'PaperPosition',[0,0,720,405]);
    print(filename,'-dpng','-r300');
%     saveas(gcf,filename,'png');
    close(gcf);
end
return
