clear  
close 
% Data file: EB, ky, kx in hdf5 format 
data = h5read('CVS_FMap_200K.h5','/FMap_76eV_CN');

blackblue = customcolormap([0 0.5 1], {'#000000','#0000ff','#ffffff'});
colormap(blackblue); 

vid = VideoWriter('arpes3d.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 30;
open(vid);
%figure('units','pixels','position',[0 0 1920 1080]);
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7); 

% Define borders to be trimmed which contain NaNs 
trimkyLo = 11;
trimkyHi = 270; 
kyRange = trimkyHi - trimkyLo + 1; 
trimkxLo = 10; 
trimkxHi = 337; 
kxRange = trimkxHi - trimkxLo + 1; 
% kykx layer corresponding to Fermi surface 
trimEBhi = 872; 

% -------------------------------------------------------------------------
% Scan E_B 

zmax = 6*floor(size(data,1)/6);
EB1 = zmax:-3:1;
EB2 = 2:6:trimEBhi; % Only back up to Fermi surface 
EB = [EB1 EB2]; 

for ii = EB % Move kykx-plane down through binding energies 
    newplot
    hold off
    hold on
    % kykx plane contains Fermi surface data 
    % Trim borders which contain NaNs 
    kykx = mat2gray(data(ii,trimkyLo:trimkyHi,trimkxLo:trimkxHi)); 
    % Eky plane 
    % Trim borders which contain NaNs     
    Eky = mat2gray(data(1:ii,trimkyLo:trimkyHi,trimkxLo)); 
    % Ekx plane 
    % Trim borders which contain NaNs     
    Ekx = mat2gray(data(1:ii,trimkyLo,trimkxLo:trimkxHi)); 
    
    kykx = uint8(255*kykx); % Convert to uint8 data 
    imgkykx = reshape(kykx,[kyRange,kxRange]); % Reshape from 3D to 2D matrix 
    rgbkykxImg = ind2rgb(imgkykx,blackblue); % Render it in a nice colormap 
    kykxXX = [0 kxRange; 0 kxRange];
    kykxYY = [0 0; kyRange kyRange];
    kykxZZ = [ii ii; ii ii]/2; % Division by 2 compresses EB axis 
    surf(kykxXX,kykxYY,kykxZZ,rgbkykxImg,'FaceColor','texturemap','EdgeColor','none');

    Eky = uint8(255*Eky); % Convert to uint8 data 
    imgEky = (reshape(Eky, [ii,kyRange]))'; % Reshape from 3D to 2D matrix 
    rgbEkyImg = ind2rgb(imgEky,blackblue); % Render it in a nice colormap     
    EkyXX = [0 0; 0 0];
    EkyYY = [0 0; kyRange kyRange];
    EkyZZ = [0 ii; 0 ii]/2;
    surf(EkyXX,EkyYY,EkyZZ,rgbEkyImg,'FaceColor','texturemap','EdgeColor','none');

    Ekx = uint8(255*Ekx); % Convert to uint8 data 
    imgEkx = (reshape(Ekx, [ii,kxRange]))'; % Reshape from 3D to 2D matrix 
    rgbEkxImg = ind2rgb(imgEkx,blackblue); % Render it in a nice colormap     
    EkxXX = [0 0; kxRange kxRange];
    EkxYY = [0 0; 0 0];
    EkxZZ = [0 ii; 0 ii]/2;
    surf(EkxXX,EkxYY,EkxZZ,rgbEkxImg,'FaceColor','texturemap','EdgeColor','none');

    line([156 156], [-25,kyRange+25], [-0.1,-0.1], 'LineWidth', 1, 'Color', 'k');
    line([-25 kxRange+25], [216 216], [-0.1,-0.1], 'LineWidth', 1, 'Color', 'k');
    line([-1 -1], [kyRange kyRange]+1, [-0.11,960/2], 'LineWidth', 1, 'Color', 'w');

    
    xlim([-25 kxRange+25]);
    ylim([-25 kyRange+25]);
    zlim([-0.11 963/2])
    axis off
    axis equal 
    xmax = size(imgkykx,2);
    ymax = size(imgkykx,1);

    set(gca, 'Projection','perspective');
    view(-40,20) 

    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off
end 

% -------------------------------------------------------------------------
% Scan k_y

zmax = 6*floor(size(data,1)/6);
ky1 = 0:2:kyRange-1;
ky2 = kyRange-2:-2:0; 
ky = [ky1 ky2]; 

for ii = ky % Move Ekx plane along ky wavevector  
    newplot
    hold off
    hold on
    % kykx plane contains Fermi surface data 
    % Trim borders which contain NaNs and remove gradually kxky plane as
    % the Ekx plane moves along the ky wavevector 
    % Choose kykx-plane representing FS = layer trimEBhi 
    kykx = mat2gray(data(trimEBhi,trimkyLo+ii:trimkyHi,trimkxLo:trimkxHi)); 
    % Eky plane 
    % Trim borders which contain NaNs     
    Eky = mat2gray(data(1:trimEBhi,trimkyLo+ii:trimkyHi,trimkxLo)); 
    % Ekx plane 
    % Trim borders which contain NaNs     
    Ekx = mat2gray(data(1:trimEBhi,trimkyLo+ii,trimkxLo:trimkxHi)); 
    
    kykx = uint8(255*kykx); % Convert to uint8 data 
    imgkykx = reshape(kykx, [kyRange-ii,kxRange]); % Reshape from 3D to 2D matrix 
    rgbkykxImg = ind2rgb(imgkykx,blackblue); % Render it in a nice colormap 
    kykxXX = [0 kxRange; 0 kxRange];
    kykxYY = [ii ii; kyRange kyRange];
    kykxZZ = [1 1; 1 1]*trimEBhi/2;
    surf(kykxXX,kykxYY,kykxZZ,rgbkykxImg,'FaceColor','texturemap','EdgeColor','none');

    Eky = uint8(255*Eky); % Convert to uint8 data 
    imgEky = (reshape(Eky, [trimEBhi,kyRange - ii]))'; % Reshape from 3D to 2D matrix 
    rgbEkyImg = ind2rgb(imgEky,blackblue); % Render it in a nice colormap     
    EkyXX = [0 0; 0 0];
    EkyYY = [ii ii; kyRange kyRange];
    EkyZZ = [0 trimEBhi; 0 trimEBhi]/2;
    surf(EkyXX,EkyYY,EkyZZ,rgbEkyImg,'FaceColor','texturemap','EdgeColor','none');

    Ekx = uint8(255*Ekx); % Convert to uint8 data 
    imgEkx = (reshape(Ekx, [trimEBhi,kxRange]))'; % Reshape from 3D to 2D matrix 
    rgbEkxImg = ind2rgb(imgEkx,blackblue); % Render it in a nice colormap     
    EkxXX = [0 0; kxRange kxRange];
    EkxYY = [ii ii; ii ii];
    EkxZZ = [0 trimEBhi; 0 trimEBhi]/2;
    surf(EkxXX,EkxYY,EkxZZ,rgbEkxImg,'FaceColor','texturemap','EdgeColor','none');

    line([156 156], [-25,kyRange+25], [-0.1,-0.1], 'LineWidth', 1, 'Color', 'k');
    line([-25 kxRange+25], [216 216], [-0.1,-0.1], 'LineWidth', 1, 'Color', 'k');

    xlim([-25 kxRange+25]);
    ylim([-25 kyRange+25]);
    zlim([-0.11 963/2])
    axis off;
    axis equal
    xmax = size(imgkykx,2);
    ymax = size(imgkykx,1);

    set(gca, 'Projection','perspective');
    view(-40,20) 
    
    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off
end 

% -------------------------------------------------------------------------
% Scan k_x 

zmax = 6*floor(size(data,1)/6);
kx1 = 0:2:kxRange-1;
kx2 = kxRange-2:-2:0; 
kx = [kx1 kx2]; 

for ii = kx % Move Eky plane along kx wavevector  
    newplot
    hold off
    hold on
    % kykx plane contains Fermi surface data 
    % Trim borders which contain NaNs and remove gradually kxky plane as
    % the Eky plane moves along the kx wavevector 
    % Choose kykx-plane representing FS = layer trimEBhi 
    kykx = mat2gray(data(trimEBhi,trimkyLo:trimkyHi,trimkxLo+ii:trimkxHi)); 
    % Eky plane 
    % Trim borders which contain NaNs     
    Eky = mat2gray(data(1:trimEBhi,trimkyLo:trimkyHi,trimkxLo+ii)); 
    % Ekx plane 
    % Trim borders which contain NaNs     
    Ekx = mat2gray(data(1:trimEBhi,trimkyLo,trimkxLo+ii:trimkxHi)); 
    
    kykx = uint8(255*kykx); % Convert to uint8 data 
    imgkykx = reshape(kykx, [kyRange,kxRange-ii]); % Reshape from 3D to 2D matrix 
    rgbkykxImg = ind2rgb(imgkykx,blackblue); % Render it in a nice colormap 
    kykxXX = [ii kxRange; ii kxRange];
    kykxYY = [0 0; kyRange kyRange];
    kykxZZ = [1 1; 1 1]*trimEBhi/2;
    surf(kykxXX,kykxYY,kykxZZ,rgbkykxImg,'FaceColor','texturemap','EdgeColor','none');

    Eky = uint8(255*Eky); % Convert to uint8 data 
    imgEky = (reshape(Eky, [trimEBhi,kyRange]))'; % Reshape from 3D to 2D matrix 
    rgbEkyImg = ind2rgb(imgEky,blackblue); % Render it in a nice colormap     
    EkyXX = [ii ii; ii ii];
    EkyYY = [0 0; kyRange kyRange];
    EkyZZ = [0 trimEBhi; 0 trimEBhi]/2;
    surf(EkyXX,EkyYY,EkyZZ,rgbEkyImg,'FaceColor','texturemap','EdgeColor','none');

    Ekx = uint8(255*Ekx); % Convert to uint8 data 
    Ekx = squeeze(Ekx);
    imgEkx = (reshape(Ekx, [trimEBhi,kxRange-ii]))'; % Reshape from 3D to 2D matrix 
    rgbEkxImg = ind2rgb(imgEkx,blackblue); % Render it in a nice colormap     
    EkxXX = [ii ii; kxRange kxRange];
    EkxYY = [0 0; 0 0];
    EkxZZ = [0 trimEBhi; 0 trimEBhi]/2;
    surf(EkxXX,EkxYY,EkxZZ,rgbEkxImg,'FaceColor','texturemap','EdgeColor','none');

    
    line([156 156], [-25,kyRange+25], [-0.1,-0.1], 'LineWidth', 1, 'Color', 'k');
    line([-25 kxRange+25], [216 216], [-0.1,-0.1], 'LineWidth', 1, 'Color', 'k');

    xlim([-25 kxRange+25]);
    ylim([-25 kyRange+25]);
    zlim([-0.11 963/2])
    axis off
    axis equal
    xmax = size(imgkykx,2);
    ymax = size(imgkykx,1);
    set(gca, 'Projection','perspective');
    view(-40,20) 
    
    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off
end 

% Output the movie as an mpg file
close(vid)