function UCIT_Delft3D2Duinafslag %(tijd, raai)
%
% ------------------------------------------------------------------------------------
%
% Function:     UCIT_Delft3D2Duinafslag.m                                                                 
% Version:      Version 1.0, January 2005                                         
% By:           M. van Koningsveld (email: mark.vankoningsveld@wldelft.nl)                                                            
% Summary:      This routine feeds a cross shore Delft3D grid ray into the
%               dune erosion routines.
%
% Syntax: 
%               close all; UCIT_Delft3D2Duinafslag          % Callantsoog
%
% Copyright (c) 2005, WL | Delft Hydraulics, FOR INTERNAL USE ONLY 
%
% ------------------------------------------------------------------------------------
%
% Comment:
% The idea of the routine is as follows. Say you want to do the calculation
% on 10 Delft3D rays: D3D-1:D3D-10. For each D3D-# ray you want to perform
% a dune erosion calculation for a given time step. This is done by following the following steps:
%   1. for D3D-# get the information from the com file for time T
%   2. transform this data into the format used by UCIT ... a structure d containing all the necessary information
%   3. with the proper boundary conditions run getDuinafslag ... the results will be stored in the log file e:\batchresult.txt
%   4. Visualise the calculation results 
%

addpath d:\Matlab\wl_quickplot\ % We need Bert Jagers' tools to read the Delft3D depth files. This addpath command is a temporary solution for local use only!

workdir=cd

cd d:\UCIT_Models\001\

% *************************************************************
% Step 1. Get D3D-# information from the com file for time T
% *************************************************************
%d3d=GetD3D('trim-03','dps')
%d3d=GetD3D('trim-001','dps',1)
% d3dtime=[1 15 20 27];  
% d3dtime=[1 27];  
% d3dray=[65:5:95];  
% d3dtime=[1 6 11 16 21 27];  
d3dtime=[1];  
d3dray=[1:161];  
for i=1:max(size(d3dtime))
    d3d=GetD3D('trim-03','dps',d3dtime(1,i))
%     figure;surf(d3d.xz,d3d.yz,d3d.dps);shading flat
%     cmap=flipud(qnhls('UCIT_bathyColormap.hls'));
%     colormap(cmap);
%     caxis([-35 15]);
%     cont=[20:-5:5 4 :-1:-5 -10:-5:-40];
%     hold on
%     [c,h]=contour3(d3d.xz,d3d.yz,d3d.dps,cont,'k');clabel(c,h);

    for j=1:max(size(d3dray))
        x=[];
        x(1,1)=0;
        for k=2:size(d3d.xz,2); %2 is in this case the cross shore direction
            x(1,k)=(((d3d.xz(d3dray(j),1)-d3d.xz(d3dray(j),k))^2) + ((d3d.yz(d3dray(j),1)-d3d.yz(d3dray(j),k))^2))^0.5
        end
        
        % *************************************************************
        % Step 2. Transform this data into the format used by UCIT
        % *************************************************************
        
%         a=[-1000:20:-20 x(1,:)]';                   % the dune erosion routines require the data to be in columns
%         b=[16.18:-0.02:15.2 d3d.dps(d3dray(j),:)]'; % the dune erosion routines require the data to be in columns
        a=[x(1,:)]';                   % the dune erosion routines require the data to be in columns
        b=[d3d.dps(d3dray(j),:)]'; % the dune erosion routines require the data to be in columns
        
        d=UCIT_getdFromD3D(a,b,d3dray(j),d3d);
        
        % *************************************************************
        % Step 3. With the proper boundary conditions run getDuinafslag 
        % *************************************************************
        
        D=0.00024455;
        Rekenpeil=4.8868;
        H0s=9.6;
        Tp=16.9;
        %     calcMeth='Beheerdersoordeel - nu';
        calcMeth='Leidraad Duinafslag - nu';
        outputDir=['e:\'];
        BOfactor=1.5;
        norm='1/10.000';
        Scen='(-)';

        result=getDuinAfslag(d,D,-1,Rekenpeil,H0s,Tp,1,calcMeth,outputDir,BOfactor,norm,Scen,0);
        
        % *************************************************************
        % Step 4. Visualise the calculation results 
        % *************************************************************
        
%         UCIT_plotDuinAfslag(result)
%         UCIT_preparePlot;
%         UCIT_print('e:\')
%         delete(findobj('tag','plotWindow'));

    end
end
d3dray=[65:5:95];  
results=load('e:\batchresults.txt')
figure(99)
for i=1:max(size(d3dray))
    SUBPLOT(1,max(size(d3dray)),i);
    id=find(results(:,3)==d3dray(i));
    plot(results(id,1),results(id,8),'-ob');
    hold on
    plot(results(id,1),results(id,11),'-dg');
    title(d3dray(i));
    axis([6000 10000 200 240]);
end

cd(workdir)