function writeWAVECON(WVCfilename,WVCdata)
%function writeWAVECON(WVCfilename,WVCdata)
%
% INPUT:
%   WVCfilename    string with name of wavecon 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]
%                  .startuptime         startuptime before 1st condition [min]
%                  .timetidecycle       effective time of each condition
%                  .timebetweencond     stop time of bed update between
%                                       wave conditions [min]. Use a single
%                                       value or an array for each timestep
%                        OR
%                  .time                array [Nx1] with begin time OR 
%                                       array [Nx2] with begin/end time for 
%                                       each condition [min]. (use either 
%                                       'time' OR a combination of 'beginttime',
%                                       'timetidalcycle' and 'timebetweencond')

%% default values for fields
default=struct;
default.windspeed         = 0;
default.winddir           = 0;
default.dirspr            = 4;
if ~isfield(WVCdata,'time')
    default.timebetweencond   = 120;
    default.timetidecycle     = 750;
    default.startuptime       = 630;
end

%% checking if default fields exist
fprintf('Checking input of WAVECON 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
%% Compute time steps (if necessary)
if isfield(WVCdata,'time')
    TIME(:,1)= WVCdata.time(:,1);
    if size(WVCdata.time,2)>1
        TIME(:,2)= WVCdata.time(:,2);
    end
else
    DT = WVCdata.timebetweencond + WVCdata.timetidecycle;
    ii = [1:length(WVCdata.hs)];
    TIME(:,1) = WVCdata.startuptime+(ii-1)*DT;
    TIME(:,2) = WVCdata.startuptime+(ii)*DT-1;
end
% make sure every condition has the same value at beginning as at the end (for
% seperate conditions). ONLY IF AN END TIME IS SPECIFIED!
if size(TIME,2)>1
    TIME = reshape(TIME',[size(TIME,1)*2,1]);
    fieldnms = {'hs','tp','xdir','dirspr','h0','windspeed','winddir'};
    for jj=1:length(fieldnms)
        VAL = [WVCdata.(fieldnms{jj}),WVCdata.(fieldnms{jj})]';
        S   = [2*length(WVCdata.(fieldnms{jj})),1];
        WVCdata.(fieldnms{jj})=reshape(VAL,S);
    end
end

%% write file
fprintf('Writing MD-VWAC file\n');
nrrows    = length(WVCdata.hs);
nrcolumns = 8;
fid = fopen(WVCfilename,'wt');
fprintf(fid,'* Itdate    Hs    Tp    Dir    ms    wl    windspeed    winddir\n');
fprintf(fid,'BL01\n');
fprintf(fid,'%4.0f %4.0f   *number of rows / number of columns\n',nrrows,nrcolumns);
%for ii=1:nrrows
    fprintf(fid,'%9.3f %9.3f %9.3f %9.2f %9.1f %9.3f %9.2f %9.2f \n',....
        [TIME,WVCdata.hs,WVCdata.tp,WVCdata.xdir,WVCdata.dirspr,WVCdata.h0,WVCdata.windspeed,WVCdata.winddir]');
%end
fclose all;