% SBEACH toolbox - fsbeach_genwaves
% Written by Joshua Simmons 11/2017

%   Syntax:
%   wavesout = fsbeach_genwaves(varargin)
%
%   Input:
%   varargin  = time:       array of measurement times for w (minutes)
%
%               Hsig:       significant wave height (m)
%               Tp1:        peak wave period (s)
%               ang:        wave direction (degrees from True North)
%               pfang:      Profile orientation - from the shore to the 
%                           most seaward point (degrees from True North)
%
%   Output:
%   wavesout        = structure array of wave inputs

function wavesout = fsbeach_genwaves(outpath,name,varargin)
    OPT = struct( ...
        'time', [0 100 200 300 400 500]', ...
        'Hsig', [0 100 200 300 400 500]', ...
        'Tp1', [-10 -5 0 1 3 10]', ...
        'ang', [-10 -5 0 1 3 10]', ...
        'pfang', NaN ...
    );

    OPT = setproperty(OPT, varargin{:});
    
    %transpose them if needed
    OPT = fsbeach_makecolumn(OPT,{'time','Hsig','Tp1','ang'});
    
    %check they can be zipped
    if length(OPT.Hsig) ~= length(OPT.Tp1)
        error('Hsig is of length %d, while Tp1 is of length %d...',length(OPT.Hsig),length(OPT.Tp1))
    end
    
    %zip wave data
    wavfile = [OPT.Hsig OPT.Tp1];
    
    % correct for SBEACH wave angle convention 
    % See SBEACH Report 3 - User's manual (pg 19)
    if isnan(OPT.pfang)
        error('Please provide the orientation of your profile as pfang.')
    end
    
    angfile = OPT.pfang - OPT.ang;
    
    %check
    if any(abs(angfile)>90)
        warning('Check the orientation of your profile relative to the waves.')
    end

    %and write
    writetofilewav(wavfile, outpath, name)
    writetofileang(angfile, outpath, name)
    
    wavesout.time = OPT.time;
    wavesout.Hsig = OPT.Hsig;
    wavesout.Tp1 = OPT.Tp1;
    wavesout.ang = angfile;
end