%% 2022-07-14
% For Danylo so he can double-check conversion of speed units (original =
% pixels/frame, desired = mm/second)

% He specifically asked for ROI 116 from SUM4, 4/21/2019
inFile = "D:\Harvard\Summit behavior\Behavior data\SUM4\2019\04-21-2019-20-51-44__Circadian_CsWF-BoardC3_MF_Emuscae_1-128_Day3\04-21-2019-20-51-44__Circadian_CsWF-BoardC3_MF_Emuscae_1-128_Day3_margoConvert_autodenoise.mat";
[startList, dateList, survivalList, boxList] = pullFileInfo(inFile);
outDir = "D:\Harvard\Summit behavior\";

plotVar = "speed"; %either "ypos" or "speed"

survival = readtable(survivalList);
load(inFile);
localT = convertToLocalTime(expmt, startList);
desiredLocalStart = 19;
xInc = 2;

[diff,idx] = min(abs(localT-desiredLocalStart));

desiredZTStart = desiredLocalStart-1;
zeitT = localT-1;

roi = 116;

width = 91.66;
height = 31.7;

figure('Units','Pixels','Position',[0 0 (width*5 +200) (height*5 + 200)]);
axesH = axes;
set(axesH,'Units','pixels','Position',[50,50,width*5,height*5]);

hold on;

if survival{roi,'Status'} == 0 && survival{roi,'Outcome'} == 1  
    plotColor = [1 0 0];
    timeOfDeath = survival{roi,'Last_mvmt'};
    
elseif survival{roi,'Status'} == 1 && survival{roi,'Outcome'} == 0 
    plotColor = [0 0 1];
end

switch plotVar
    case "ypos"
        scaledHt = scaleHt(expmt.Centroid.data(:,2,roi));
        plot(scaledHt,'Color',plotColor);
        ylabel('Y position');
        ylim([0 1]);
        try
            scatter(timeOfDeath,1,'k*');
        catch
        end  
    case "speed"
        speedConv = speedToMmPerS; % from pixels to mm
        plot(expmt.Speed.data(:,roi) * speedConv * expmt.parameters.target_rate);
        ylabel('Speed (mm/s)');
end

set(gca,'xtick',idx:3*3600*xInc:length(expmt.Centroid.data)-idx); % Set ticks every hour starting from first frame
%set(gca,'xticklabel',round(localT(idx),1):1:round(localT(length(expmt.Centroid.data)),1)); % Label ticks with "local time" hours
set(gca,'xticklabel',[desiredZTStart:xInc:23,0:xInc:round(zeitT(length(expmt.Centroid.data)))])
axesH.TickLength = [0 0];
xlim([idx length(expmt.Centroid.data)]);
xlabel('Zeitgeber time');
print(strcat(outDir,dateList,'_',boxList,'_ROI # example', num2str(roi),'.pdf'),'-dpdf');
print(strcat(outDir,dateList,'_',boxList,'_ROI # example', num2str(roi),'.png'),'-dpng');
close();