%% coefficients and settings

nam = [{'zs'};{'ue'};{'u'};{'hh'};{'zb'};];

% dimensions
fid = fopen('dims.dat','r'); 
temp = fread(fid,[3,1],'double'); 
nt = temp(1);
nx = temp(2)+1;
ny = temp(3)+1;
fclose(fid);

% read grid coordinates 
fid = fopen('xy.dat','r');
xw = fread(fid,[nx,ny],'double'); 
yw = fread(fid,[nx,ny],'double');
% x = fread(fid,[nx,ny],'double'); 
% y = fread(fid,[nx,ny],'double');
fclose(fid);

% read XBeach output
ts = 0:1:nt-1;
for j = 1:length(nam)
    temp = zeros(nx,ny,nt);
    fid = fopen([nam{j},'.dat'],'r');
    for i = 1:nt
        temp(:,:,i) = fread(fid,[nx,ny],'double');  % all data
    end
    fclose(fid);
    s.(nam{j}) = zeros(nx,nt);
    s.(nam{j}) = squeeze(temp(:,2,:));
end

fid = fopen('..\data\datanam.txt');
nvar = str2num(fgetl(fid));
for i = 1:nvar
    line = fgetl(fid);
    [nam,remain] = strtok(line,' ');
    [dummy,remain] = strtok(remain,'=');
    par.(nam) = str2num(remain(2:end));
end
fclose(fid);

t = [0:1:nt]*par.tint;
tplot = 20:20:160;
figure(1); 
for i =1:length(tplot)
    ind = find(t==tplot(i));
    subplot(2,1,1); set(gca,'FontSize',16);
    plot(xw,s.zs(:,ind),'k','LineWidth',1.5); hold on;
    xlabel('x [m]'); ylabel('\eta [m]'); axis([0 1000 -0.04 0.04]); 
    subplot(2,1,2); set(gca,'FontSize',16);
    plot(xw,s.u(:,ind),'k','LineWidth',1.5); hold on;
    xlabel('x [m]'); ylabel('u [m/s]'); axis([0 1000 -0.04 0.04]);
end
pname = ['..\..\report\',testid '_' runid '_snapshot_short' '.jpg'];
eval(['print -djpeg ' pname]);




tplot = 20:20:1180;
figure(2); 
for i =1:length(tplot)
    ind = find(t==tplot(i));
    subplot(2,1,1); set(gca,'FontSize',16);
    plot(xw,s.zs(:,ind),'k','LineWidth',1.5); hold on;
    xlabel('x [m]'); ylabel('\eta [m]'); axis([0 1000 -0.04 0.04]); 
    subplot(2,1,2); set(gca,'FontSize',16);
    plot(xw,s.u(:,ind),'k','LineWidth',1.5); hold on;
    xlabel('x [m]'); ylabel('u [m/s]'); axis([0 1000 -0.04 0.04]);
end
pname = ['..\..\report\',testid '_' runid '_snapshot_long' '.jpg'];
eval(['print -djpeg ' pname]);

close all;
