clear all
close all

addpath(genpath('E:\My software'));

pp0=cd;

% nam = {'zb';'H';'zs';'zs0';'u'};
% 
% %% dimensions
% fid = fopen([dir,'dims.dat'],'r'); 
% temp = fread(fid,[3,1],'double'); 
% par.nt = temp(1);
% par.nx = temp(2)+1;
% par.ny = temp(3)+1;
% fclose(fid);
% 
% %% read grid coordinates 
% fid = fopen([dir,'xy.dat'],'r');
% d.xw = fread(fid,[par.nx,par.ny],'double'); 
% d.yw = fread(fid,[par.nx,par.ny],'double');
% d.x = fread(fid,[par.nx,par.ny],'double'); 
% d.y = fread(fid,[par.nx,par.ny],'double');
% 
% fclose(fid);
% 
% for j = 1:length(nam)
%     temp = zeros(par.nx,par.ny,par.nt);
%     fid = fopen([dir,nam{j},'.dat'],'r');
%     for i = 1:par.nt
%         temp(:,:,i) = fread(fid,[par.nx,par.ny],'double');  % all data
%     end
%     fclose(fid);
%     d.(nam{j}) = zeros(par.nx,par.nt);
%     d.(nam{j}) = squeeze(temp(:,2,:));
% end

pps={'Run1M_00';'Run1M_00_facua02';'Run1M_00_wetslope';'Run1M_both';'Run1M_longshore';'Run1M_longwaves'};

for i=1:6
    
    cd(fullfile(pp0,pps{i}))
    
        fid = fopen('dims.dat','r');
        nt = fread(fid,[1],'double');
        nx = fread(fid,[1],'double');
        ny = fread(fid,[1],'double');
        fclose(fid);
        fixy = fopen('xy.dat','r');
        x = fread(fid,[nx+1,ny+1],'double');
        y = fread(fid,[nx+1,ny+1],'double');
        fclose(fixy);
        
        xx=x(:,1);
        yy=y(:,1);

        % read final model profile
        fdp=WLDEP('read','newdep.dep',[nx+1,ny+1])
        
        fdp2=fdp(:,1);
        
        runres(i).nt=nt;
        runres(i).nx=nx;
        runres(i).ny=ny;
        runres(i).x=xx(:);
        runres(i).y=yy(:);
        runres(i).final=fdp2(:);

end
        
cd(ppO)

load profs_2

ic=make_icol_scheme;

dx=2;

    load(['start.dep'])

    nst = start(1,:);

    load(['final.dep'])

    nfin = final(1,:);

    
    lf=length(nst);
    lf2=length(nfin);
    
%         subplot(5,1,i)
        figure(1)
        
        
        xx=([1:lf]-1)*dx;

        xx2=([1:lf2]-1)*dx;
        
pp=plot(xx,nst,'color',ic(:,1));

            set(pp,'Linewidth',2)

            hold on

            pp=plot(xx2,nfin,'color',ic(:,2));

            set(pp,'Linewidth',2)
            
%         end

        pp=plot(prof(5).x2,flipud(prof(5).z2),'color',ic(:,2+1));

        set(pp,'Linewidth',2)
        
        
%         add formating
         xlabel('Cross-shore distance (m)');ylabel('Elevation (m)');%title(['wave condition ' num2str(ii) ' iter ' num2str(i) '/' num2str(nt)]);

        grid on 
        box on



        % leg={'Initial','Final'};
        leg={'Initial','XBeach','Final'};

        legend(leg,2)

%             % set limits
%         yl1=-6 ;
%         yl2=8;
% 
%         xx=repres(i).x;
%         nst=repres(i).z;
%         
%         
%         inds=find(nst>yl1 & nst < yl2);
% 
%         xl1=min(xx(inds));
%         xl2=max(xx(inds));
%         axis([xl1 xl2 yl1 yl2])
% 
%         plot([xl1 xl2],[2 2],'k')
