function writeMDVWAC(WVCfilename,WVCdata)
%function writeMDVWAC(WVCfilename,WVCdata)
%
% INPUT:
%   WVCfilename    string with name of MDVWAC file (generated as output)
%   WVCdata        structure with SCO data or filename of SCO file
%                  .h0                  array with water levels [m w.r.t Reference level]
%                  .hs                  array with significant wave heights [m]
%                  .tp                  array with peak wave periods [s]
%                  .xdir                array with wave directions [°N]
%                  .dirspr              array with cosine power for directional spreading [-]
%                  .windspeed           array with windspeed (optional) [m/s]
%                  .winddir             array with wind directions (optional) [°N]
%                  ADD FOR SPACE VARYING BOUNDARY CONDITIONS
%                  .nrconditions        value for number of conditions [-]
%                  .distance            array with wind directions (optional) [°N]
%                  ADD FOR WIND CONDITIONS
%                  .windfiles           cell string with wind directions (optional) [°N]


%% default values for fields
default=struct;
default.windspeed         = 0;
default.winddir           = 0;
default.dirspr            = 4;

%% checking if default fields exist
fprintf('Checking input of MDVWAC data structure\n');
fieldnames = fields(default);
for jj=1:length(fields(default))
    if ~isfield(WVCdata,fieldnames{jj})
        fprintf(' - Warning : Field %s not specified (using %1.0f)\n',fieldnames{jj},default.(fieldnames{jj}));
        WVCdata.(fieldnames{jj}) = repmat(default.(fieldnames{jj}),size(WVCdata.hs));
    elseif length(WVCdata.(fieldnames{jj}))==1
        WVCdata.(fieldnames{jj}) = repmat(WVCdata.(fieldnames{jj}),size(WVCdata.hs));
    end
end   
if isfield(WVCdata,'windfiles')
    % create cell string with wind files
    S = size(WVCdata.hs);
    nrLOCS = round(S(1)/length(WVCdata.windfiles));
    for jj=1:S(1)
        windfiles{jj} = '';
        if mod(jj,nrLOCS)==1
            tt = ceil(jj/nrLOCS);
            windfiles{jj} = WVCdata.windfiles{tt};
        end
    end
end
    
%% write file
fprintf('Writing MD-VWAC file\n');
if ~isfield(WVCdata,'distance')
    nrconditions = length(WVCdata.hs);
else
    nrconditions = WVCdata.nrconditions;
end

fid = fopen(WVCfilename,'wt');
if ~isfield(WVCdata,'distance')
    fprintf(fid,'* Wave tabel generated for boundary conditions\n');
    fprintf(fid,'UNIBEST *(MORSYS/UNIBEST)\n');
    fprintf(fid,' %3.0f   *total number of wave conditions\n',nrconditions);
    %% SPATIALLY UNIFORM CONDITIONS
    if ~isfield(WVCdata,'windfiles')
        fprintf(fid,'*      Hm0       Tp       theta       ms       H0        U10     theta_w\n');
        fprintf(fid,'*      (m)       (s)      (N^o)       (-)      (m)      (m/s)     (N^o)\n');
        fprintf(fid,' %9.3f %9.3f %9.2f %9.1f %9.3f %9.2f %9.2f \n',....
            [WVCdata.hs,WVCdata.tp,WVCdata.xdir,WVCdata.dirspr,WVCdata.h0,WVCdata.windspeed,WVCdata.winddir]');
    
    %% SPATIALLY UNIFORM CONDITIONS + WIND FILES
    else
        WVCdata.windspeed = zeros(S);
        WVCdata.winddir = zeros(S);
        for jj=1:S(1)
            fprintf(fid,'*      Hm0       Tp       theta       ms       H0        U10     theta_w\n');
            fprintf(fid,'*      (m)       (s)      (N^o)       (-)      (m)      (m/s)     (N^o)\n');
            fprintf(fid,' %9.3f %9.3f %9.3f %9.2f %9.1f %9.3f %9.2f %9.2f  %s \n',....
                WVCdata.hs(jj),WVCdata.tp(jj),WVCdata.xdir(jj),WVCdata.dirspr(jj),WVCdata.h0(jj),WVCdata.windspeed(jj),WVCdata.winddir(jj),windfiles{jj});
        end
    end
else
    fprintf(fid,'* Wave tabel generated for boundary conditions\n');
    fprintf(fid,'VARBOUND *(MORSYS/UNIBEST)\n');
    fprintf(fid,' %3.0f   *total number of wave conditions\n',nrconditions);
    %% SPACE VARYING CONDITIONS
    if ~isfield(WVCdata,'windfiles')
        fprintf(fid,'*  Distance      Hm0       Tp       theta       ms       H0        U10     theta_w\n');
        fprintf(fid,'*     (m)        (m)       (s)      (N^o)       (-)      (m)      (m/s)     (N^o)\n');
        fprintf(fid,' %10.2f %9.3f %9.3f %9.2f %9.1f %9.3f %9.2f %9.2f \n',....
            [WVCdata.distance,WVCdata.hs,WVCdata.tp,WVCdata.xdir,WVCdata.dirspr,WVCdata.h0,WVCdata.windspeed,WVCdata.winddir]');
    
    %% SPACE VARYING CONDITIONS + WIND FILES
    else
        WVCdata.windspeed = zeros(S);
        WVCdata.winddir = zeros(S);
        for jj=1:S(1)
            fprintf(fid,'*  Distance      Hm0       Tp       theta       ms       H0        U10     theta_w\n');
            fprintf(fid,'*     (m)        (m)       (s)      (N^o)       (-)      (m)      (m/s)     (N^o)\n');
            fprintf(fid,' %10.2f %9.3f %9.3f %9.2f %9.1f %9.3f %9.2f %9.2f  %s \n',....
                WVCdata.distance(jj),WVCdata.hs(jj),WVCdata.tp(jj),WVCdata.xdir(jj),WVCdata.dirspr(jj),WVCdata.h0(jj),WVCdata.windspeed(jj),WVCdata.winddir(jj),windfiles{jj});
        end
    end
end
fclose all;
