function [trajmat, outfile] = gdftrans(timemat, outfile)
%
% function [trajmat, outfile] = gdftrans(timemat, outfile)
%
%    GDFTRANS - Convert GPT Matlab data file into trajectory format
%
%                         gdftrans(timemat, outfile)
%             [trajmat] = gdftrans(timemat)
%    [trajmat, outfile] = gdftrans(timemat, outfile)
%
%    GDFTRANS is used to convert GPT data, already imported into Matlab,
%    into trajectory data.  The input file can either be a structure array
%    of time slice data (TIMEMAT), imported with the GDF2MAT function, or a
%    .MAT-file containing such a data array.  This function is the
%    equivalent of the GPT "GDFTRANS" command.
%
%    gdftrans(timemat, outfile) - reads in Matlab data array TIMEMAT,
%    converts the data into trajectory format, and saves it in the file
%    OUTFILE.  If TIMEMAT is a .MAT-file, GDFTRANS loads this file and
%    extracts the TIMEMAT array from it.  TIMEMAT must be a [ 1 x N ]
%    structure array with at least the following fields: x, y, z, Bx, By,
%    Bz, rxy, time, xp, yp, ID.
%
%    [trajmat] = gdftrans(timemat) - reads in Matlab data array TIMEMAT (or
%    the .MAT-file TIMEMAT containing the data array), converts the data
%    into trajectory format and outputs the data as the structure array
%    TRAJMAT.
%
%    [trajmat, outfile] = gdftrans(timemat, outfile) - as above, but saves
%    the data to file OUTFILE and echoes this filename as a variable.

    if nargin < 1
        error('Too few input variables: syntax is [trajmat, outfile] = gdftrans(timemat, outfile)') ;
    elseif nargin > 2
        error('Too many input variables: syntax is [trajmat, outfile] = gdftrans(timemat, outfile)') ;
    elseif nargout > 2
        error('Too many output variables: syntax is [trajmat, outfile] = gdftrans(timemat, outfile)') ;
%    elseif ( nargin < 2 ) || ( nargout < 1 )
%        error('Incorrect syntax: must specify either output variable or outfile (see help)') ;
    elseif ( nargout >= 2 ) && ( nargin < 2 )
        error('Must specify output file to echo it back: [trajmat, outfile] = gdftrans(timemat, outfile)') ;
    elseif ( nargin >= 2 ) && ~ischar(outfile)
        error('Output filename must be a string') ;
    end

    if ischar(timemat) && ( exist(timemat,'file') == 2 )
        load(timemat,'timemat') ;
    elseif ~isa(timemat, 'struct')
        error('Input file must either be valid .MAT-filename or Matlab structure variable') ;
    end

    trajmat.x = [] ;
    trajmat.xp = [] ;
    trajmat.y = [] ;
    trajmat.yp = [] ;
    trajmat.z = [] ;
    trajmat.Bx = [] ;
    trajmat.By = [] ;
    trajmat.Bz = [] ;
    trajmat.rxy = [] ;
    trajmat.time = [] ;

    wbh = waitbar(0,'Loading timeslices... ','Name','GDF Trans Progress') ;

    timeloops = length(timemat) ;

%    counttick = ceil(timeloops./10) ;

    for i = 1:timeloops

        percdone = i./timeloops ;
        wbh = waitbar(percdone,wbh,['Loading timeslice ' num2str(i) ' (' num2str(percdone.*100,'%0.0f') '%)']) ;

        partids = timemat(i).ID ;

%        if ( rem((i-1),counttick) == 0 )
%            perc = ((i-1)./counttick).*10 ;
%            disp([num2str(perc) '% read ... ']) ;
%        end

        for j = 1:length(partids)

            partid = partids(j) ;

            if length(trajmat) < partid || isempty(trajmat(partid).x)
                trajmat(partid).x = timemat(i).x(j) ;
                trajmat(partid).xp = timemat(i).xp(j) ;
                trajmat(partid).y = timemat(i).y(j) ;
                trajmat(partid).yp = timemat(i).yp(j) ;
                trajmat(partid).z = timemat(i).z(j) ;
                trajmat(partid).Bx = timemat(i).Bx(j) ;
                trajmat(partid).By = timemat(i).By(j) ;
                trajmat(partid).Bz = timemat(i).Bz(j) ;
                trajmat(partid).rxy = timemat(i).rxy(j) ;
                trajmat(partid).time = timemat(i).time ;
            else
                trajmat(partid).x(end+1) = timemat(i).x(j) ;
                trajmat(partid).xp(end+1) = timemat(i).xp(j) ;
                trajmat(partid).y(end+1) = timemat(i).y(j) ;
                trajmat(partid).yp(end+1) = timemat(i).yp(j) ;
                trajmat(partid).z(end+1) = timemat(i).z(j) ;
                trajmat(partid).Bx(end+1) = timemat(i).Bx(j) ;
                trajmat(partid).By(end+1) = timemat(i).By(j) ;
                trajmat(partid).Bz(end+1) = timemat(i).Bz(j) ;
                trajmat(partid).rxy(end+1) = timemat(i).rxy(j) ;
                trajmat(partid).time(end+1) = timemat(i).time ;
            end

        end

    end

%    Close waitbar

    close(wbh) ;

    if nargin > 1

        save(outfile,'trajmat') ;

    end

    return