clear all;
close all;
clc;

%[CrossSection]
%    id                    = 3
%    branchid              = 1
%    chainage              = 0.000
%    shift                 = 1.000
%    definition            = rect1

% We need to create:
% 1)CrossSectionDefinitions.ini
%   Defines each cross-section
% [General]
%     majorVersion          = 1                   
%     minorVersion          = 0                   
%     fileType              = crossDef            
% 
% [Definition]
%     id                    = CrossSection1       
%     type                  = tabulated           
%     thalweg               = 0.000               
%     numLevels             = 2                   
%     levels                = 0.00000 10.00000    
%     flowWidths            = 200.00000 200.00000 
%     totalWidths           = 200.00000 200.00000 
%     sd_crest              = 0.000               
%     sd_flowArea           = 0.000               
%     sd_totalArea          = 0.000               
%     sd_baseLevel          = 0.000               
%     main                  = 200.000             
%     floodPlain1           = 0.000               
%     floodPlain2           = 0.000               
%     groundlayerUsed       = 0                   
%     groundlayer           = 0.000               

% 2)CrossSectionLocations.ini
%   Defines location of cross-section
% [General]
%     majorVersion          = 1                   
%     minorVersion          = 0                   
%     fileType              = crossLoc            
% 
% [CrossSection]
%     id                    = CrossSection1       
%     branchid              = Channel1            
%     chainage              = 11336.853           
%     shift                 = 0.000               
%     definition            = CrossSection1       

dflow1d_dir = '..\sobek3_dimr\dflow1d\';
xlsfile = [dflow1d_dir 'NetworkDefinition.xlsx'];

fnm1 = [dflow1d_dir 'CrossSectionDefinitions.ini'];
fid1 = fopen(fnm1,'w');

fnm2 = [dflow1d_dir 'CrossSectionLocations.ini'];
fid2 = fopen(fnm2,'w');

sobek_network_data = xlsread(xlsfile,'Sheet1','B2:E59');

channel  = 'Channel1';
Chainage = sobek_network_data(:,3);
Bedmain  = sobek_network_data(:,4);
N  = size(sobek_network_data,1);

% print general part of files
fprintf(fid1,'%s\n','[General]');
fprintf(fid1,'%s\n','    majorVersion          = 1');
fprintf(fid1,'%s\n','    minorVersion          = 0');
fprintf(fid1,'%s\n','    fileType              = crossDef');
fprintf(fid1,'%s\n',' ');

fprintf(fid2,'%s\n','[General]');
fprintf(fid2,'%s\n','    majorVersion          = 1');
fprintf(fid2,'%s\n','    minorVersion          = 0');
fprintf(fid2,'%s\n','    fileType              = crossLoc');
fprintf(fid2,'%s\n',' ');

for i=1:N
    
    ch = Chainage(i);
    zb = Bedmain(i);
    shift = 0; % want zit in zb
    
    crosssec_name_id = ['CrossSection_chainage_', num2str(ch,'%.3f')];
    
    fprintf(fid1,'%s\n','[Definition]                                     ');
    fprintf(fid1,'%s\n',['    id                    = ' crosssec_name_id]);        
    fprintf(fid1,'%s\n',['    type                  = tabulated           ']);
    fprintf(fid1,'%s\n',['    thalweg               = 0.000               ']);
    fprintf(fid1,'%s\n',['    numLevels             = 2                   ']);
    fprintf(fid1,'%s\n',['    levels                = ', num2str(zb,'%.5f'), ' ',num2str(zb+10,'%.5f')]);
    fprintf(fid1,'%s\n',['    flowWidths            = 200.00000 200.00000 ']);
    fprintf(fid1,'%s\n',['    totalWidths           = 200.00000 200.00000 ']);
    fprintf(fid1,'%s\n',['    sd_crest              = 0.000               ']);
    fprintf(fid1,'%s\n',['    sd_flowArea           = 0.000               ']);
    fprintf(fid1,'%s\n',['    sd_totalArea          = 0.000               ']);
    fprintf(fid1,'%s\n',['    sd_baseLevel          = 0.000               ']);
    fprintf(fid1,'%s\n',['    main                  = 200.000             ']);
    fprintf(fid1,'%s\n',['    floodPlain1           = 0.000               ']);
    fprintf(fid1,'%s\n',['    floodPlain2           = 0.000               ']);
    fprintf(fid1,'%s\n',['    groundlayerUsed       = 0                   ']);
    fprintf(fid1,'%s\n',['    groundlayer           = 0.000               ']);
    fprintf(fid1,'%s\n',' ');
    
    fprintf(fid2,'%s\n','[CrossSection]');
    fprintf(fid2,'%s\n',['    id                    = ' crosssec_name_id ]);
    fprintf(fid2,'%s\n',['    branchid              = ' channel ]);
    fprintf(fid2,'%s\n',['    chainage              = ' , num2str(ch     ,'%0.3f')]);
    fprintf(fid2,'%s\n',['    shift                 = ' , num2str(shift  ,'%0.3f')]);
    fprintf(fid2,'%s\n',['    definition            = ' crosssec_name_id ]);
    fprintf(fid2,'%s\n',' ');

end

fclose(fid1);
fclose(fid2);
