%this is a template  to generate the SWAN forcing files
% This may need to be modified to get the correct X,Y,H for any specific
% grid.


%diry='Z:\users-tmp\abever\TMP\'
diry='/home/beach/guest/bevera/SWF/FORCING/FRC2008/'

% Make thses SWAN forcing files (1 for yes, 0 for no).
grid=1;
bathy=1;
wind=1;

if (grid==1 | bathy==1)
    rgrid=netcdf([diry,'swf_grid4.nc']);
%   rgrid=netcdf('test_head_curv_grd.nc')
% SWAN deals in X and Y, not lon/lat
    x_rho=rgrid{'x_rho'}(:);
    y_rho=rgrid{'y_rho'}(:);
    h=rgrid{'h'}(:);
    mask=rgrid{'mask_rho'}(:);
    
% Set land values to something we then tell the driver input file about    
      h(~mask)=9e8;
      x_rho(~mask)=9e8;
      y_rho(~mask)=9e8;

% Initialize variables and reshape the ROMS matricies to vectors fro SWAN, 
% The reshaping is what may need to be modified...
    X=[];
    Y=[];
    H=[];
    [m,n]=size(h);
    for i=1:m
        X=[X,x_rho(i,:)];
        Y=[Y,y_rho(i,:)];
        H=[H,h(i,:)];
    end

%     for i=1:n
%          X=[X;x_rho(:,i)];
%          Y=[Y;y_rho(:,i)];
%      end
H = H';
  grd=[X';Y'];

% x_rho=reshape(x_rho,[1 25000]);
% y_rho=reshape(y_rho,[1 25000]);
% 
% grd=[x_rho;y_rho];

if (grid==1)
   save swan_swf.grd grd -ASCII
   disp('Saved grid')
end
if bathy==1
    save swan_swf.bot H -ASCII
    disp('Saved Bathy')
end
end

if(wind==1)
    % Get U and V wind speeds from forcing file.
    frc=netcdf([diry,'swf_wind2008.nc']);
    
    u=frc{'Uwind'}(:);
    u=mean(mean(u,3),2);
    v=frc{'Vwind'}(:);
    v=mean(mean(v,3),2);
% Set u and v winds for the 4 corners of the grid
% This sets a constant wind over the whole domain, which we specify in the
% driver input file. Hence, the number 8 below, 4 corners and 2 variables
% makes a total of 8 values for each timestep. We set the timestep and
% timestamp in the driver input file.
    L=length(u);
    for i=1:L
        tmp(8*(i-1)+1:8*(i-1)+4)=u(i);
        tmp(8*i-3:8*i)=v(i);
    end
    tmp=tmp';
    save swf_wind.wnd tmp -ASCII
end
    