function [] = reorderlif(varargin)
% REORDERLIF  Reordering of raw microscope data read by BFOPEN. 
% 
% REORDERLIF(DATA,NCH) opens the struct DATA from BFOPEN, reads NCH channels
% and saves the data to disk as .mat format. Supported formats are 
% matlab ('mat'), tiff ('tif') and nifti ('nii'). The files are written to
% disk with the naming stack1.mat, stack2.mat ....
% 
%
%
%   See also BFOPEN
% 	=======================================================================================
% 	CELLSEGM: Cell segmentation, Version 2012
% 	Copyright (c): Erlend Hodneland
%   Jonas Lies Vei 91,
%   5009 Bergen
%   Email: erlend.hodneland@biomed.uib.no
% 	=======================================================================================
%
%
data = varargin{1};
nch = varargin{2};
format = 'mat';
if nargin == 3
    format = varargin{3};
end;

% extract from lif file
nseries = size(data,1);
% nseries = 2;
for i = 1 : nseries
    B = data{i,1};
    
    
    % find foldername
    A = B{1,2};
    ind = findstr(A,';');
    folder = A(1:ind(1)-1);
    [a b c] = fileparts(folder);
    if isempty(a)
        folder = b;
    else
        folder = [a '/' b];
    end;
    basepath = folder;

    
    % find voxelsize
    A = data{i,2};

    h(1) = str2double(A.get('15 HardwareSetting|ScannerSettingRecord|dblVoxelX 0'));
    h(2) = str2double(A.get('15 HardwareSetting|ScannerSettingRecord|dblVoxelY 0'));
    h(3) = str2double(A.get('15 HardwareSetting|ScannerSettingRecord|dblVoxelZ 0'));
    
    msg = ['Voxelsize ' num2str(h)];
    disp(msg);
    
    n = size(B,1);

    ch = 0;
    z = 1;
    nz = n/nch;
    % channels and z planes are merged z1:ch1,ch2,ch3, z2:ch1,ch2,ch3
    for j = 1 : n
        if j == 1 
            dim = size(B{1,1});
            dim = [dim nz nch];
            im = zeros(dim);
        end;
            
        ch = ch + 1;
        im(:,:,z,ch) = B{j,1};
        if ch == nch
            ch = 0;
            z = z + 1;
        end;        
        
    end;
    
    
    im = double(im);
    
    folderpath = [basepath '-matlab'];
    [a b c] = mkdir(folderpath);
    if isequal(format,'mat')
        filesave = [folderpath '/' 'stack' int2str(i) '.mat'];
        msg = ['Saving ' filesave];
        disp(msg);
        save(filesave,'im','h');
    elseif isequal(format,'nii')
        filesave = [folderpath '/' 'stack' int2str(i) '.nii'];
        msg = ['Saving ' filesave];
        disp(msg);
        hdr = cbiCreateNiftiHeader(im);
        hdr.pixdim(2:4) = h';
        cbiWriteNifti(filesave,im,[]);        
    elseif isequal(format,'tif')
        dim = size(im);
        imhere = zeros(dim(1),dim(2),dim(3)*dim(4));
        c = 0;
        for j = 1 : dim(4)
            for k = 1 : dim(3)
                c = c + 1;
                imhere(:,:,c) = im(:,:,k,j);
            end;
        end;

        filesave = [folderpath '/' 'stack' int2str(i) '.tif'];
        msg = ['Saving ' filesave];
        disp(msg);        
        imwritemulttif(filesave,imhere,h);        
        clear imhere;
%         t = Tiff(filesave,'w');
%         tagstruct.ModelPixelScaleTag = h;
%         t.setTag(tagstruct);
        
    else
        error('Not supported format');
    end;

    
end;




