% Movie with interpolation, rods and jacket position.
% For an "easy use", only change the line giving "titre", just below.
% Do not use the computer or even move the mouse during the movie generation (while windows flash).
% The file colormap_03 should be in the same directory.
% This script works with Matlab 6.1 and 7.6
%==================================================================================================
clear all; close all; format compact;

titre = 'fig11_JOSAA_2004'; % Put here the string used in the first line of the data input file
rodsfile = [titre,'.xy.dat'];
rods = dlmread(rodsfile,'',1,0);     % use ' ' for Matlab 6.1, and '' for Matlab 7.6 
fieldfile = ['cartechamp.',titre,'.dat'];
jacketfile = ['prof_norm.',titre];
field = dlmread(fieldfile,'',1,0);   % use ' ' for Matlab 6.1, and '' for Matlab 7.6 
jacket = dlmread(jacketfile,'',1,0); % use ' ' for Matlab 6.1, and '' for Matlab 7.6 

% Rods position :
xc = rods(:,2);
yc = rods(:,3);
radius = rods(:,9); dia = 2*radius;
nrods=length(xc);

% Jacket profile :
xd = jacket(:,1); yd = jacket(:,2);

% Field
x = unique(field(:,1)); xmin = min(x); xmax = max(x); nx=length(x); dx = (xmax-xmin)/(nx-1);
y = unique(field(:,2)); ymin = min(y); ymax = max(y); ny=length(y); dy = (ymax-ymin)/(ny-1);

% Time, for the movie:
tmin = 0; tmax = 1; nbret = 30;
dt=(tmax-tmin)/nbret; 
t = linspace(tmin,tmax-dt,nbret);

for k=1:nbret
    close all
   % Final video size should preferably be a multiple of 16 pixels (for compatibility with some video codecs, if you want to compress the video later) 
    figure('Position',[400, 400, 627, 627])
    %figure('Position',[400, 400, 337, 337]) % gives a video size of 224 pixels with Matlab 6.1 (multiple of 16)
    %figure('Position',[400, 400, 627, 627]) % gives a video size of 416 pixels with Matlab 6.1 (multiple of 16)
    %figure('Position',[400,   0, 988, 988]) % gives a video size of 656 pixels with Matlab 6.1 (multiple of 16)

    load -ascii colormap_03; colormap(colormap_03)% The file colormap_03 shoud be in the same directory

    cc1=exp(-i*2*pi*t(k));
    u = field(:,3) .* exp(i*field(:,4)*pi/180);
    reU = real( u * cc1);
    z=reshape(reU,ny,[]);
    [xi,yi] = meshgrid(xmin:dx/2:xmax,ymin:dy/2:ymax);
    zi = interp2(x,y,z,xi,yi);
    pcolor(xi,yi,zi)
    shading interp
    axis square
    title(titre,'Interpreter','none');
    caxis([-1 1]); colorbar; % You can change the limits of the colormap here
    % Rods
    for it=1:nrods
     rectangle('Position',[xc(it)-radius(it),yc(it)-radius(it),dia(it),dia(it)],'Curvature',[1,1],'EdgeColor','k','LineWidth',0.5)
    end
   % Jacket
    hold on;
    plot(xd,yd,'k','LineWidth',0.5);
    M(k) = getframe;
end

movie(M,2,10) % Optional, plays the movie inside Matlab : movie(name, number of repetitions, frames/s)
movie2avi(M,titre,'compression','None') % generates the movie file

% You can further compress the video using many softwares. 

% I generally use SUPER (http://www.erightsoft.com/SUPER.html) to convert it into MPEG, using theses parameters
% Size       Video      bitrate_video  
% pixels     format     kbps             
% 224x224    MPEG-I      768            
% 416x416    MPEG-I     1152            
% 656x656    MPEG-I     3504           

% You can also use Virtualdub, and compress to DivX or Xvid

% In order to get animated gifs, I have got good results only with 123 AVI to GIF  Converter, www.bitsoft.net, but it is not freeware.
