function mat2dat ( Initialize, Time, Filename, Variables, varargin )
%
% Convert Matlab data variables to a Tecplot *.dat file.
%
% Input:
%   -Initialize: =0 --> (default) open file for writing; discard existing contents 
%                =1 --> open file for reading and writing; append data to end of file
%   -Time      : (number) the time in Zone. 0 is default.
%   -Filename  : (string) the name of the file to be writing. 'Tecplot.dat' is default.
%   -Variables : (string) the name of variables. The default is the name of varargin,
%                if the name can be identified, otherwise the code automatically replace 
%                it with the order, 'Vi', for example, mat2dat([],[],[],[],x, y', Q) creates 
%                the Variables as 'x, V2, Q'. 
%                Use ',' to separate the variables, for example, 
%                   mat2dat([],[],[],'x, y, Q', x, y', Q)
%   -varargin  : (vectors or matrices) the input data variables. For example, x, y, Q, ...
%
% Examples:
%   mat2dat ( Initialize, Time, Filename, Variables,  x, y, Q, ... )
%   mat2dat ( []        , Time, Filename, Variables,  x, y, Q, ... )
%   mat2dat ( Initialize, Time, []      , Variables,  x, y, Q, ... )
%   mat2dat ( Initialize, []  , Filename, Variables,  x, y, Q, ... )
%   mat2dat ( Initialize, Time, Filename, []       ,  x, y, Q, ... )
%   mat2dat ( []        , []      , []  , []       ,  x, y, Q, ... )

%
% Copyright Bao-Shan Wang, School of Mathematical Sciences, Ocean University of China
% Link : http://www.amsc-ouc.ac.cn/Research_skills.html
% $ MATLAB R2019a $ $ Date: 2019/09/05 14:35:25 $
%

Varnum = nargin - 4;
if Varnum < 1, error( 'Not enough input arguments.' ) ; end

if isempty( varargin  ), error( 'Data is empty.' )    ; end

[ I, J ] = issamesize( Varnum, varargin{:} );

                          Varstr = inputname(5  )     ; 
    if isempty( Varstr ), Varstr = 'V1'               ; end
for i = 2:Varnum
                             str = inputname(4+i)     ; 
    if isempty( str   ),     str = ['V', num2str(i)]  ; end
                          Varstr = [Varstr, ', ', str]; 
end

if isempty( Initialize), Initialize = 0               ; end
if isempty( Time      ), Time      = 0                ; end
if isempty( Filename  ), Filename  = 'Tecplot.dat'    ; end
if isempty( Variables ), Variables = Varstr           ; end

varn = sum( Variables == ',' ) + 1;
if varn~= Varnum, error('Variables cannot match input data.'); end

                    fmt = 'w' ;
if Initialize == 1; fmt = 'a+'; end

fid = fopen( Filename, fmt );

Title     = 'Title="Matlab2tecplot 2D"';
Variables = ['Variables = ', Variables];
Zone      = ['Zone T="WENO ', num2str(Time), ' " I=',num2str(I), ' J=',num2str(J), ' F=block'];

if Initialize == 0
    fprintf( fid, '%s\n', Title     );
    fprintf( fid, '%s\n', Variables );
end
    fprintf( fid, '%s\n', Zone      );

for i = 1:Varnum
    fprintf( fid, '%16.15f \n', varargin{i} );
end

fclose ( fid );

end

function [ s, t ] = issamesize( Varnum, varargin )

[ s, t ] = size( varargin{1} );

for i = 2:Varnum
    [ s0, t0 ] = size( varargin{i} );
    if s ~= s0 || t ~= t0, error('Data dimensions must agree.'); end
end
end