function [plate] = getlowres(prm,obj)
% Getting lowres  images from the microscope and stiching them together in
% a big super image to be used for pattern recognition

%
% Listen until the terminating job
%
imcount = 0;
mes = '';
while 1
    
    % listen for image arriving
    mesold = mes;
    while isequal(mesold,mes)    
        mes = fscanf(obj);    
    end;
    msg = ['Receiving ' mes];
    disp(msg);

    % get image info
    % prm.lowreprm.mes = '/cli:terminal /app:matrix /cmd:startscan/relpath:4927\experiment--2014_04_16_13_22_01\slide--S00\chamber--U00--V00\field--X00--Y00\image--L0000--S00--U00--V00--J32--E01--O00--X00--Y00--T0000--Z00--C00.ome.tif /jobname:LoResPrescan'; 
    lowres = getimageinfo(mes);    
            
    if isequal(prm.lowres.jobnamewait,lowres.jobnameread)
        msg = ['Finding terminating job number, returning'];
        disp(msg);
        break;
    end;

    if ~isequal(prm.lowres.jobname,lowres.jobnameread)
        disp('Received image not a lowres image, waiting');
        continue;
    end;

    msg = ['Receiving lowres job'];
    disp(msg);
    
    % we have a lowres image
    imcount = imcount + 1;    
    
    % store the read information
    plate.info{imcount}.lowres = lowres;
    
    
end;

msg = ['Received ' int2str(imcount) ' images'];
disp(msg);

save test

%
% Find max and min coordinate
%
X = [];Y = [];
for i = 1 : numel(plate.info)
    % find min and max coordinates
    X = [X; plate.info{i}.lowres.attributen.Y];
    Y = [Y; plate.info{i}.lowres.attributen.X];
end;
minx = min(X)+1;
maxx = max(X)+1;
miny = min(Y)+1;
maxy = max(Y)+1;
dim = [maxx-minx+1,maxy-miny+1];
offsetx = minx-1;
offsety = miny-1;



%
% Collect images and stich them together
%

plate.lowresim = cell(prm.lowres.nch,1);
for i = 1 : prm.lowres.nch
    plate.lowresim{i} = zeros(dim(1)*prm.lowres.dim(1),dim(2)*prm.lowres.dim(2),'uint8');
end;
% plate.labelim = zeros(dim(1)*prm.lowres.dim(1),dim(2)*prm.lowres.dim(2),'uint16');
plate.labelim = zeros(dim(1)*prm.lowres.dim(1),dim(2)*prm.lowres.dim(2),'single');

msg = ['Collecting images and stiching them together'];
disp(msg);

% Read the images
nimages = numel(plate.info);
for i = 1 : nimages
    
    % this field information
    lowres = plate.info{i}.lowres;
    
    % Load the image
    ind = strfind(lowres.relpath,'--C');
    
    % channels to use        
    im = cell(prm.lowres.nch,1);
    for j = 1 : prm.lowres.nch
        % the channel to use
        lowres.relpath(ind+4) = prm.lowres.ch{j};    
    
        % read the image
        pathload = [prm.lowres.exportpath  lowres.relpath];   
        msg = ['Reading ' pathload];
        disp(msg);
        im{j} = imread(pathload);
        im{j} = imrotate(im{j},-90);
    
    end;    
    lowres.dim = size(im{1});

    % put the image into the mesh of the plate    
    x = lowres.attributen.Y-offsetx;
    y = lowres.attributen.X-offsety;

    % put the image into the lowres image
    x = x*lowres.dim(1) + 1;
    y = y*lowres.dim(2) + 1;

    for j = 1 : prm.lowres.nch
        plate.lowresim{j}(x:x+lowres.dim(1)-1,y:y+lowres.dim(2)-1) = uint8(im{j});
    end;
    % the label image to know the field for later use in the highres
%     plate.labelim(x:x+lowres.dim(1)-1,y:y+lowres.dim(2)-1) = i*ones(lowres.dim,'uint16');
    plate.labelim(x:x+lowres.dim(1)-1,y:y+lowres.dim(2)-1) = i*ones(lowres.dim,'single');
        
end;





% extract the image to show
% [im1,box] = boundreg(plate.lowresim{1},1,0);
% [im2,box] = boundreg(plate.lowresim{2},1,0);
% show(im1,1)
% show(im2,2)
% pause


% startscan: Starting the server, moving to the upper corner, ready to
% scan, default screening job
% startcamscan: Starting the highres scanning

% Start server and open a rout
% NB always port 8895 on the client
% echotcpip('on',40000)
% obj = tcpip('129.177.232.61',8895);
% fopen(obj);

% echotcpip('on',40000)
% obj = tcpip('localhost',40000,'LocalPort', 5500,'InputBufferSize',3000);
% obj = tcpip('129.177.233.155',40000);
% fopen(obj);

% Close server and close port
% echotcpip('off');
% fclose(obj);





