function varargout = LoadVtk1a(FileName,t,endian)


% function varargout = LoadVtk1a(FileName,t,endian)
% 
% This function read the vtk files into matlab. The files 
% are either 3D (ex. Atlas, defAtlas, ...) or combination if 3D volumes(MovieVelocity). 
% The files will be transposed in X,Y direction.
% 
% FileName: string representing the file name to be read.
% t: 1 for 3D image data and 3 for combination of 3, 3D data (vector data).
% endian: 'b' or'l'
% 
% varargout: one output or 3 outputs 
% 


if (t ~= 1) && (t ~= 3)
    
    
    error('t should be either 1 for scalar 3D data or 3 for vector 3D data.');
end


if ~strcmp(resample,'l') && ~strcmp(resample,'b') 
    
    error('Byte order should be either l for little endian or b for big endian.');
    
end






d = dir(FileName);
fid = fopen(FileName,'r+',endian);

for i=1:9;
    tline{i} = fgetl(fid);
    temp=tline{i};
    temp;
end;


A=sscanf(tline{5},'%s%d%d%d');
c=A(11);   r=A(12);   sl=A(13);

fclose(fid);




skip = abs(d.bytes - (r.*c.*sl.*t.*8));

fid = fopen(FileName,'r+',endian);
a = fread(fid,skip,'uchar');
b = fread(fid,'double');

if t == 1
    
    Temp = reshape(b,r,c,sl);
    Temp = permute(Temp,[2 1 3]);
    varargout(1) = {Temp};
else
    
    Temp = reshape(b,t,r,c,sl);
        
end

fclose(fid);

if t > 1
    
    for i =1:t
        
        Temp1 = Temp(i,:,:,:);
        Temp1 = squeeze(Temp1);
        Temp1 = permute(Temp1,[2 1 3]);
        varargout(i) = {Temp1};
        clear Temp1;
       
        
    end
    
end





