%% open connection

% TCPIP port
% prm.port = 8895;

% IP address of the host you are trying to connect to
% prm.iphost = '129.177.232.61';

% where to do the segmentation
prm.nucleusch = 1;
prm.segmch = 2;

% number of lines
prm.lowres.line = [2,3];
prm.lowres.nline = numel(prm.lowres.line);
% a = [1,18,44,69];
% for i = 1 : numel(prm.lowres.line)
%     prm.lowres.lineseparator(i,1) = a(prm.lowres.line(i));
%     prm.lowres.lineseparator(i,2) = a(prm.lowres.line(i)+1);
% end;
% the channels to read (NB: one number down from the segmentation and 
% nucleus channel, counting starts at 0)
prm.lowres.ch = {'0','1'};

% number of channels
prm.lowres.nch = numel(prm.lowres.ch);

% voxel size in the lowres job
prm.lowres.h = [1.52,1.52];

% voxel volume in the lowres job
prm.lowres.voxelvol = prod(prm.lowres.h);

% Dimension of lowres images
prm.lowres.dim = [512,512];

% the lowres job name
prm.lowres.jobname = 'lowres';

% the job number indicating that we have reached the end of well
prm.lowres.jobnamewait = 'wait';

% highres job name
prm.highres.jobname = 'jobhighres';

% % scan field dimension% 87
% prm.scanfield.dim2 = [87,142];

% path where the images are exported to (mapped drive on the client)
prm.lowres.exportpath = 'C:\Users\SP5\Documents\MatrixScreenerImages';


%
% Settings for finding the candidates of interest in the lowres job
% 

% level for thresholding to find DiD cells in lowres job
prm.detect.leveldid = 100;

% level for thresholding to find nuclei in lowres job. NB dont use this,
% finds it automatically now by graythresh
prm.detect.levelnuclei = 100;

% level for thresholding to find rim in lowres job (quite stable with this threshold)
prm.detect.levelrim = 130;

% smallest distance between did cells (all distances related to voxel
% size
prm.detect.smallestdistcells = 200;

% smallest distance to the rim
prm.detect.smallestdistrim = 200;

% smallest and largest nuclei area
prm.detect.smallestnucleiarea = 80;
prm.detect.largestnucleiarea = 700;

% smallest and largest cell area (used for DiD cell definition)
prm.detect.smallestcellarea = 300;
prm.detect.largestcellarea = 8000;

% average cell diameter (used for density measurements)
prm.detect.averagecelldiameter = 110;

% Minimum number of neighbors
prm.detect.minnumneigh = 15;

% number of control images
prm.detect.ncontrol = 15;

% start lowres scan
send = startscan;
sendcmd(obj,send);

% clear old data
clear labelim im bw plate lowresim

imagelabel = cell(prm.lowres.nline,1);
for i = 1 : prm.lowres.nline
    
    msg = ['Well number ' int2str(i)];
    disp(msg);
    
    % get lowres
    plate = getlowres(prm,obj);
        
    % find objects to image
    clear cm;
    [imsegm,imnucleus,labelim,did,cm,nuclei,rim,controlim] = getimageobj(plate,prm);
    
    % all coordinates    
    cm.all.rel = [cm.did.rel;cm.control.rel];
    cm.all.label = [cm.did.label;cm.control.label];
    % label as control and non control images to know later, do we really
    % need to know?
    n1 = size(cm.did.rel,1);
    n2 = size(cm.control.rel,1);    
    
    n = size(cm.all.rel,1);
    v = randperm(n);
    cm.all.rel = cm.all.rel(v,:);
    cm.all.label = cm.all.label(v,:);    
    
    msg = ['Printing images'];
    disp(msg);
    
    A = imsegm;
    A = uint8(A);
    pathsave = ['../Evaluation/line' int2str(i) '-DiDimage.tiff'];    
    imwrite(A,pathsave,'tiff');

    % print for evaluation
    A = did.bw;
    A = uint8(A)*256;
    pathsave = ['../Evaluation/line' int2str(i) '-donorcells.tiff'];    
    imwrite(A,pathsave,'tiff');

    A = imnucleus;
    A = uint8(A);
    pathsave = ['../Evaluation/line' int2str(i) '-nucleiimage.tiff'];    
    imwrite(A,pathsave,'tiff');

    A = nuclei.bw;
    A = uint8(A)*256;
    pathsave = ['../Evaluation/line' int2str(i) '-nuclei.tiff'];    
    imwrite(A,pathsave,'tiff');

    A = controlim;
    A = uint8(A)*128;
    pathsave = ['../Evaluation/line' int2str(i) '-controlim.tiff'];    
    imwrite(A,pathsave,'tiff');
    
    A = rim.bw;
    A = uint8(A)*256;
    pathsave = ['../Evaluation/line' int2str(i) '-rim.tiff'];    
    imwrite(A,pathsave,'tiff');

    clear A;    
    
    % start highres
    cmdsave{1,1} = starthighres(obj,cm.all.rel,cm.all.label,plate,plate.labelim,prm.highres.jobname);
    
end

% get the experimental data and save them
save('Experiment.mat','prm','cmdsave');


% % SEssion with Volker, this worked!
% fprintf(obj,'/cli:asd /app:matrix /cmd:deletelist')
% fprintf(obj,'/cli:asd /app:matrix /cmd:add /tar:camlist /exp:jobhighres /slide:1 /wellx:1 /welly:1 /fieldx:11 /fieldy:11 /dxpos:1 /dypos:10')
% fprintf(obj,'/cli:asd /app:matrix /cmd:add /tar:camlist /exp:jobhighres /slide:1 /wellx:1 /welly:1 /fieldx:11 /fieldy:11 /dxpos:1 /dypos:110')
% fprintf(obj,'/cli:sdfdsf /app:matrix /cmd:startcamscan')
% fprintf(obj,'/cli:asdsd /app:matrix /cmd:stopwaitingforcam')
%  obj.Terminator='LF/CR'
% 
%    TCPIP Object : TCPIP-129.177.232.61
% 
%    Communication Settings 
%       RemotePort:         8895
%       RemoteHost:         129.177.232.61
%       Terminator:         'LF/CR'
%       NetworkRole:        client
% 
%    Communication State 
%       Status:             open
%       RecordStatus:       off
% 
%    Read/Write State  
%       TransferStatus:     idle
%       BytesAvailable:     512
%       ValuesReceived:     54
%       ValuesSent:         2962
%  