% Program to generate a cartoon of a 1D CCD exposure and readout
clear; close all;

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

myBlue = [0.3 0.28 0.55];
numPks = 20; % Number of detected Gaussian peaks in 1D array
intArray = rand(1,numPks); % Assign random intensities
posnArray = rand(1,numPks); % Assign random positions
steps = 500; % number of steps from 0 to 1
x = 0:1/steps:1;
y = x*0.0;
sigma = 0.005; % SD for Gaussian peaks
binSize = 2; % Binning of individual pixels

pos1 = [0.1 0.64 0.8 0.255];
subplot('Position',pos1)

% Plot 1D spectrum of numPks Gaussian peaks
for j = 1:numPks
    y = y + intArray(1,j)*exp(-(x-posnArray(1,j)).^2/(2*sigma^2));
end
plot(x,y,'color','r', 'LineWidth', 2)
axis off

pos2 = [0.1 0.1 0.8 0.52];
subplot('Position',pos2)

% Dimensions and position of shutter
pos = [0 1.05*max(y)*binSize steps 0.05];
cur = [0.1 0.5];

for i = 1:binSize:steps % Loop to plot out rectangles moving to right in steps of binSize
    newplot
    if (i == 1)
        rectangle('Position',pos,'Curvature',cur,'FaceColor',[0.2 0.2 0.2],'LineStyle','none');
        xlim([0 steps]);
        ylim([0 1.1*max(y)*binSize]);
        set(gca,'linewidth',2,'fontsize',18);
        ax2 = gca;
        ax2.YAxis.Visible = 'off';   % remove y-axis
        
        pauseMov(vid,10); % Pause movie for 10 frames
    end
    xwidth = binSize;
    ystart = 0.0;
    if (i == 1)
        newplot
        for k = 0:0.05:1 % Grow signal while exposing
            newplot
            for j = 1:binSize:steps % Loop to get rectangle size and position
                xstart = steps*x(j)+(i-1);
                ytemp = sum(y(j:j+binSize-1));
                ywidth = ytemp*k;
                if (xstart < steps)
                    rectangle('Position',[xstart ystart xwidth ywidth],'FaceColor',myBlue);
                    yout = sum(y(j:j+binSize-1));
                end
                xlim([0 steps]);
                ylim([0 1.1*max(y)*binSize]);
                set(gca,'linewidth',2,'fontsize',18);
                ax2 = gca;
                ax2.YAxis.Visible = 'off';   % remove y-axis
            end % Loop of plot out of each rectangle
            xlabel('Pixel #');
            
            % Store the frame
            frame = getframe(gcf);
            writeVideo(vid,frame);
        end
    elseif (i>1)
        newplot
        for j = 1:binSize:steps % Loop to get rectangle size and position
            xstart = steps*x(j)+(i-1);
            ytemp = sum(y(j:j+binSize-1));
            ywidth = ytemp;
            if (xstart < steps)
                rectangle('Position',[xstart ystart xwidth ywidth],'FaceColor',myBlue);
                yout = sum(y(j:j+binSize-1));
            end
            xlim([0 steps]);
            ylim([0 1.1*max(y)*binSize]);
            set(gca,'linewidth',2,'fontsize',18);
            ax2 = gca;
            ax2.YAxis.Visible = 'off';   % remove y-axis
        end % Loop of plotout of each rectangle
        
        rectangle('Position',pos,'Curvature',cur,'FaceColor',[0.2 0.2 0.2],'LineStyle','none');
        xlim([0 steps]);
        ylim([0 1.1*max(y)*binSize]);
        set(gca,'linewidth',2,'fontsize',18);
        ax2 = gca;
        ax2.YAxis.Visible = 'off';   % remove y-axis
        
        str1 = num2str(yout*1000,'% 5.0f');
        if (round(yout*1000) == 1)
            str2 = ' count';
        else
            str2 = ' counts';
        end
        strTot = [str1,str2];
        hText = text(510, 0, strTot, 'FontSize',18);
        xlabel('Pixel #');
        
        % Store the frame
        frame = getframe(gcf);
        writeVideo(vid,frame);
    end
end % Loop of moving output to the right

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