% start up matlab in ../cost_trace directory and
% cut-and-paste these lines at prompt
% note: you'll have to change the name of the file
% at the top of each trace.
 

% trace of raw runtimes of each snapshot
TraceName='reduce_plus';
InputDataDescriptor='50-element int vector';
PlotName=regexprep(TraceName,'_','\\_');
str2 = {};
str2(1)={strcat('\bf Program:\rm ',PlotName)};
str2(2)={strcat('\bf Input:\rm ',InputDataDescriptor)};
times=textread(strcat(TraceName,'.dat'));
Tmp = size(times);
XMax = Tmp([1]);
YMax = max(times);
steps=[1:XMax];
H= plot(steps,times,'b');
set(H,'EraseMode','xor');
text((XMax*0.8),(YMax*1.1),str2,'HorizontalAlignment','left');
xlabel('optimisation step');
ylabel('running time (instructions)');
axis([0 (XMax*1.2) 0 (YMax*1.2)]);
set(gcf,'Color',[1 1 1]);
saveas(gcf,strcat(TraceName,'.eps'));


%read and trace animation (cut and past this into Matlab prompt)

TraceName=strcat(TraceName,'_trace');
PlotName=regexprep(TraceName,'_','\\_');
Fid = fopen(strcat(TraceName,'.dat'));
Names={}
RawData={}
Blocks={}
Data=[,]
Block=1
while(~feof(Fid))
  Names{end+1} = textscan(Fid,'%s',1); 
  RawData{end+1}= textscan(Fid,'%d %d');
  Data(:,[1])=RawData{Block}{1};
  Data(:,[2])=RawData{Block}{2};
  Blocks{Block} = Data;
  Data = [,];
  Block=Block+1;
end
Fid = fclose(Fid);
TBlocks = Blocks; %originally had a trim op here.
BSize = size(TBlocks)
NFrames= BSize([2])
% find the values of xmax and ymax from block
% data
XMax=0
YMax=0
for F=1:NFrames,
  Tmpx= max(Blocks{F}(:,[1]));
  if Tmpx > XMax,
     XMax = Tmpx;
  end;
  Tmpy= max(Blocks{F}(:,[2]));
  if Tmpy > YMax,
     YMax = Tmpy;
  end;
end;  

%% now make the movie

%str2(1)={strcat('\bf ',PlotName)};
%str2(2)={strcat('\rm ',InputDataDescriptor)};
str2(3)={'start'};
AviObj = avifile(strcat(TraceName,'.avi'),'fps',2);
for F=1:NFrames,
  name=Names{F}{1};
  str2(3)=strcat('\bfApplied:\rm ',name);
  tmp=TBlocks{F};
  H = plot(tmp(:,1),tmp(:,2));
  set(H,'EraseMode','xor');
  %uicontrol('Style','text','Position',[80 80 200 30],'String',str1);
  text(XMax*0.8,YMax*1.1,str2,'HorizontalAlignment','left')
  xlabel('instruction count');
  ylabel('space used (words)');
  axis([0 (XMax*1.2) 0 (YMax*1.2)]);
  %axis equal
  Frame=getframe(gcf); %gca
  AviObj = addframe(AviObj,Frame);
end;
AviObj = close(AviObj);