clear
clc

Barcodes={'S:\OperaQEHS\OperaDB\Jonathan Arias\JA_20161127_WTcontrol_AutoSensor\JA_20161127_WTcontrol_AutoSensor'};
b = 1;
Barcode = Barcodes{b};
    Objects = {};   
    clear ObjectsAll
    ObjectsCell = {};
    
    %% Document script
    % SavePathMainDirectory = ['.\', Barcode];
    BarcodeName = regexp(Barcode, '.*\\(.*)', 'tokens'); 
    BarcodeName = BarcodeName{:};
    SavePathBegining = {'Y:\16-Our Papers\In Preparation\PINK1 _ Javier\PNAS\Revision_alternative\Figure_3\Partials\A\Zoomed_images\'};
    SavePathMainDirectory = [SavePathBegining{:}, BarcodeName{:}];
    AnalysisTimeStamp = datestr(now, 'yyyymmdd_HHMMSS');
    SavePath = [SavePathMainDirectory, '\Analysis_', AnalysisTimeStamp];
    mkdir(SavePath)
	PreviewPath = [SavePath, filesep, 'Previews'];
    mkdir(PreviewPath)
	FileNameShort = 'Rosella_Auto_FP.m';
	f_LogDependencies(FileNameShort, SavePath); 

InfoTable = f_InfoTable(Barcodes{b});
	
f=7; 
        InfoTableThis = InfoTable(f,:);
        cube = readflexcube(InfoTable.files{f}, 'PlaneCount', 1); % Read 4-D image cube
        ch1 = cube.data(:,:,:,1); % it(ch1)
        ch2 = cube.data(:,:,:,2); % it(ch2)

BarCodeThis = unique(InfoTable.Barcode); Barcode = BarCodeThis{:};
column = InfoTable.Column(f);
row = InfoTable.Row(f);
field = InfoTable.field(f);
Areaname = InfoTable.AreaName{f};
imSize = [size(ch1, 1), size(ch1, 2)];
    [BarMask, ~] = f_barMask(20, 0.2152, imSize, imSize(1)-50, 550, 10);		
RGB = cat(3, imadjust(ch2), imadjust(ch1), zeros([size(ch1,1), size(ch1,2)], 'uint16'));
RGB = imoverlay2(RGB, BarMask, [1 1 1]);

PreviewGF = imadjust(ch1);
PreviewRF = imadjust(ch2);

[ZoomOverlayRGB_1,ZoomRGB_1,ZoomBoxinImageRGB_1] = f_ZoomBoxRGB_JJ(RGB,360,440,70,70,3,1);
[ZoomOverlayRGB_2,ZoomRGB_2,ZoomBoxinImageRGB_2] = f_ZoomBoxRGB_JJ(RGB,8,460,70,70,3,1);
[ZoomOverlayRGB_3,ZoomRGB_3,ZoomBoxinImageRGB_3] = f_ZoomBoxRGB_JJ(RGB,300,180,70,70,3,1);

filename_ZoomOverlayRGB_1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayRGB_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomRGB_1= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomRGB_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageRGB_1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageRGB_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayRGB_1, filename_ZoomOverlayRGB_1);
imwrite(ZoomRGB_1, filename_ZoomRGB_1);
imwrite(ZoomBoxinImageRGB_1, filename_ZoomBoxinImageRGB_1);

filename_ZoomOverlayRGB_2 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayRGB_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomRGB_2= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomRGB_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageRGB_2 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageRGB_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayRGB_2, filename_ZoomOverlayRGB_2);
imwrite(ZoomRGB_2, filename_ZoomRGB_2);
imwrite(ZoomBoxinImageRGB_2, filename_ZoomBoxinImageRGB_2);

filename_ZoomOverlayRGB_3 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayRGB_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomRGB_3= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomRGB_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageRGB_3 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageRGB_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayRGB_3, filename_ZoomOverlayRGB_3);
imwrite(ZoomRGB_3, filename_ZoomRGB_3);
imwrite(ZoomBoxinImageRGB_3, filename_ZoomBoxinImageRGB_3);


[ZoomOverlayGF_1,ZoomGF_1,ZoomBoxinImageGF_1] = f_ZoomBoxRGB_JJ(PreviewGF,360,440,70,70,3,1);
[ZoomOverlayGF_2,ZoomGF_2,ZoomBoxinImageGF_2] = f_ZoomBoxRGB_JJ(PreviewGF,8,460,70,70,3,1);
[ZoomOverlayGF_3,ZoomGF_3,ZoomBoxinImageGF_3] = f_ZoomBoxRGB_JJ(PreviewGF,300,180,70,70,3,1);

filename_ZoomOverlayGF_1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayGF_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomGF_1= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomGF_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageGF_1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageGF_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayGF_1, filename_ZoomOverlayGF_1);
imwrite(ZoomGF_1, filename_ZoomGF_1);
imwrite(ZoomBoxinImageGF_1, filename_ZoomBoxinImageGF_1);

filename_ZoomOverlayGF_2 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayGF_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomGF_2= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomGF_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageGF_2 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageGF_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayGF_2, filename_ZoomOverlayGF_2);
imwrite(ZoomGF_2, filename_ZoomGF_2);
imwrite(ZoomBoxinImageGF_2, filename_ZoomBoxinImageGF_2);

filename_ZoomOverlayGF_3 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayGF_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomGF_3= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomGF_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageGF_3 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageGF_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayGF_3, filename_ZoomOverlayGF_3);
imwrite(ZoomGF_3, filename_ZoomGF_3);
imwrite(ZoomBoxinImageGF_3, filename_ZoomBoxinImageGF_3);

[ZoomOverlayRF_1,ZoomRF_1,ZoomBoxinImageRF_1] = f_ZoomBoxRGB_JJ(PreviewRF,360,440,70,70,3,1);
[ZoomOverlayRF_2,ZoomRF_2,ZoomBoxinImageRF_2] = f_ZoomBoxRGB_JJ(PreviewRF,8,460,70,70,3,1);
[ZoomOverlayRF_3,ZoomRF_3,ZoomBoxinImageRF_3] = f_ZoomBoxRGB_JJ(PreviewRF,300,180,70,70,3,1);

filename_ZoomOverlayRF_1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayRF_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomRF_1= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomRF_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageRF_1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageRF_1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayRF_1, filename_ZoomOverlayRF_1);
imwrite(ZoomRF_1, filename_ZoomRF_1);
imwrite(ZoomBoxinImageRF_1, filename_ZoomBoxinImageRF_1);

filename_ZoomOverlayRF_2 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayRF_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomRF_2= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomRF_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageRF_2 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageRF_2.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayRF_2, filename_ZoomOverlayRF_2);
imwrite(ZoomRF_2, filename_ZoomRF_2);
imwrite(ZoomBoxinImageRF_2, filename_ZoomBoxinImageRF_2);

filename_ZoomOverlayRF_3 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayRF_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomRF_3= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomRF_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
filename_ZoomBoxinImageRF_3 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageRF_3.tiff', PreviewPath, Barcode, Areaname, row, column, field);
imwrite(ZoomOverlayRF_3, filename_ZoomOverlayRF_3);
imwrite(ZoomRF_3, filename_ZoomRF_3);
imwrite(ZoomBoxinImageRF_3, filename_ZoomBoxinImageRF_3);