function output_filename = maketiff(varargin);
%this function writes a 3-D image stack as a multi-frame .tif file
%use : maketiff(myvariable, output_filename)
%   this returns the output_filename
%   if output_filname is excluded, a uiputfile gui opens to select a
%   filename
%note, an extra qualifier variable can be added to the end
%   maketiff(myvariable,qualifier) or maketiff(myvariable, output_filename, qualifier)
%the qualifier can be 'transpose', or 'nowb' to either transpose the matrix
%   before writing the tif, or to eliminate the waitbar.
%
%Written by Philbert S. Tsai on 08/03/05
%Last Updated : 4/14/15


nin = nargin;
filename = ' ';
transpose_yn = 0;
waitbar_yn = 0;
append_file = 0;
input_matrix = varargin{1};
if nin>1,
    qualifier1 = varargin{2};
    switch qualifier1,
        case 'transpose', transpose_yn = 1;
        case 'notranspose', transpose_yn = 0;
        case {'nowb','no_wb'}, waitbar_yn = 0;
        case {'wb','waitbar'}, waitbar_yn = 1;
        case {'append','append_file'}, append_file = 1;    
        otherwise filename = varargin{2};
    end
end


if filename == ' ';
    [filename,pathname,filterindex] = uiputfile('*.tif', 'Save as multiframe tif');
    filename = [pathname,filename];
end

if nin>2,
    qualifier1 = varargin{3};
    switch qualifier1,
        case 'transpose', transpose_yn = 1;
        case 'notranspose', transpose_yn = 0;
        case {'nowb','no_wb'}, waitbar_yn = 0;
        case {'wb','waitbar'}, waitbar_yn = 1;
        case {'append','append_file'}, append_file = 1;    
    end
end

if nin>3,
    qualifier1 = varargin{4};
    switch qualifier1,
        case 'transpose', transpose_yn = 1;
        case 'notranspose', transpose_yn = 0;
        case {'nowb','no_wb'}, waitbar_yn = 0;
        case {'wb','waitbar'}, waitbar_yn = 1;
        case {'append','append_file'}, append_file = 1;    
    end
end

mysize = size(input_matrix);
x_size = mysize(1);
y_size = mysize(2);
temp = size(mysize);
if temp(2) == 3,
    z_size = mysize(3);
else
    z_size = 1;
end


if or(isa(input_matrix,'uint8'),isa(input_matrix,'logical')); %WRITE AS 8-BIT
    if isa(input_matrix,'logical'), mymultiplier = 255; else mymultiplier = 1; end;
    current_frame = uint8(input_matrix(:,:,1)).*mymultiplier;
    if transpose_yn == 1; current_frame = transpose(current_frame);end
    if append_file == 1,
        successfulWriteFlag = 0;
        failedAttempts = 0;
        while successfulWriteFlag == 0
            try
                imwrite(uint8(current_frame),filename,'tiff','WriteMode','append','Compression','none','Resolution',1);
                successfulWriteFlag = 1;
            catch
                disp('failed write attempt. retrying...');
                failedAttempts = failedAttempts + 1;
                pause(1);
                if failedAttempts>10,
                    break %while successfulWriteFlag...
                end%if failedAttempts>10
            end%try-catch
        end%while successfulWriteFlag...
    else%if append_file == 1,
        imwrite(uint8(current_frame),filename,'tiff','Compression','none','Resolution',1);
    end%if append_file == 1,
    if z_size >1,
        if waitbar_yn == 1, wb = waitbar(0,'Writing multiframe tiff...');end 
        for k = 2:z_size,
            pause(0.1);
            current_frame = uint8(input_matrix(:,:,k)).*mymultiplier;
            if transpose_yn == 1; current_frame = transpose(current_frame);end
            successfulWriteFlag = 0;
            failedAttempts = 0;
            while successfulWriteFlag == 0
                try
                    imwrite(uint8(current_frame),filename,'tiff','WriteMode','append','Compression','none','Resolution',1);
                    successfulWriteFlag = 1;
                catch
                    disp('failed write attempt. retrying...');
                    failedAttempts = failedAttempts + 1;
                    pause(1);
                    if failedAttempts>10,
                        break %while successfulWriteFlag...
                    end%if failedAttempts>10
                end%try-catch
            end%while successfulWriteFlag...
            if waitbar_yn == 1, waitbar(k/z_size,wb), end
        end%for k = 2:z_size,
        if waitbar_yn == 1, close(wb); end
    end%if z_size >1,

    output_filename = filename;

else %WRITE AS 16-BIT
    
    current_frame = input_matrix(:,:,1);
    if transpose_yn == 1; current_frame = transpose(current_frame);end
    if append_file == 1,
        successfulWriteFlag = 0;
        failedAttempts = 0;
        while successfulWriteFlag == 0
            try
                imwrite(uint16(current_frame),filename,'tiff','WriteMode','append','Compression','none','Resolution',1);
                successfulWriteFlag = 1;
            catch
                disp('failed write attempt. retrying...');
                failedAttempts = failedAttempts + 1;
                pause(1);
                if failedAttempts>10,
                    break %while successfulWriteFlag...
                end%if failedAttempts>10
            end%try-catch
        end%while successfulWriteFlag...
    else%if append_file == 1,
        imwrite(uint16(current_frame),filename,'tiff','Compression','none','Resolution',1);
    end%if append_file == 1,
    if z_size >1,
        if waitbar_yn == 1, wb = waitbar(0,'Writing multiframe tiff...');end 
        for k = 2:z_size,
            current_frame = input_matrix(:,:,k);
            if transpose_yn == 1; current_frame = transpose(current_frame);end
                successfulWriteFlag = 0;
                failedAttempts = 0;
                while successfulWriteFlag == 0
                    try
                        imwrite(uint16(current_frame),filename,'tiff','WriteMode','append','Compression','none','Resolution',1);
                        successfulWriteFlag = 1;
                    catch
                        disp('failed write attempt. retrying...');
                        failedAttempts = failedAttempts + 1;
                        pause(1);
                        if failedAttempts>10,
                            break %while successfulWriteFlag...
                        end%if failedAttempts>10
                    end%try-catch
                end%while successfulWriteFlag...
            if waitbar_yn == 1, waitbar(k/z_size,wb), end
        end%for k = 2:z_size,
        if waitbar_yn == 1, close(wb); end
    end%if z_size >1,

    output_filename = filename;

end %if isa
    
    
return





