function [s]=ww3baz2az(s,varargin)
%WW3BAZ2AZ    Converts wave direction from local backazimuth to azimuth
%
%    Usage:    s=ww3baz2az(s)
%
%    Description:
%     S=WW3BAZ2AZ(S) converts the wave direction data in WW3 struct S from
%     local backazimuth (where the waves are coming from) to local azimuth
%     (which way the waves are going).
%
%    Notes:
%
%    Examples:
%     % Read a local wave direction file, convert to local
%     % azimuth so the mapped data makes intuitive sense:
%     s=ww3struct('multi_1.glo_30m.dp.201311.grb2');
%     ww3map(ww3baz2az(s),[0 360],hsv(64));
%
%    See also: WW3STRUCT, WW3REC, WW3CAT, WW3MAP, WW3MAPMOV, WW3MOV,
%              PLOTWW3, PLOTWW3TS, WW3UV2SA

%     Version History:
%        Feb.  5, 2014 - initial version
%
%     Written by Garrett Euler (ggeuler at wustl dot edu)
%     Last Updated Feb.  5, 2014 at 00:40 GMT

% todo:

% check ww3 input
if(nargin==0) % gui selection of grib file
    s=ww3struct();
elseif(isstruct(s))
    valid={'path' 'name' 'description' 'units' 'data' ...
        'lat' 'lon' 'time' 'latstep' 'lonstep' 'timestep'};
    if(any(~ismember(valid,fieldnames(s))))
        error('seizmo:ww3baz2az:badWW3',...
            'S must be a struct as generated by WW3STRUCT!');
    end
elseif(ischar(s)) % filename given
    s=ww3struct(s,varargin{:});
else
    error('seizmo:ww3baz2az:badWW3',...
        'FILE must be a string!');
end

% loop over struct elements and convert wind data when found
for i=1:numel(s)
    % find wind data from description field values
    if(isequal(s(i).description,{'Primary wave direction'}))
        s(i).description={'Primary wave azimuth'};
        s(i).data{1}=s(i).data{1}-180;
        s(i).data{1}(s(i).data{1}<0)=s(i).data{1}(s(i).data{1}<0)+360;
    end
end

end
