function D = rws_suppletiedatabase(FileName)
%SUPPLETIEDATABASE_LOAD_AND_CLEAN_UP_MESS   load/clean-up/process rijkswaterstaat xls data file of suppleties
%
% 'n_code' is not numeric, and is belonging to the old db. Not used anymore
% 
% the following fields in the .xls db must be numeric
% 
%    OPT.numeric = {'p_beginmnduitv',...
%        'p_beginuitvoering',...
%        'p_einduitvoering',...
%        'Eind_uitvoering_mnd',...
%        'begin_uitvoering_mnd',...
%        'b_gemeten'};
%
%
%
% See also: XRSP2XYRD, RWS_KUSTVAK
%%
   
%% load raw data
    
    D = xls2struct(FileName) % database until August 2012
  % D = xls2struct('Suppletiedatabase-31-12-2010.xls') % database until 2010
  % D = xls2struct('Suppletiedatabase-volledig-export-13augustus2009-aangevuld met actuele gegevens_v2.public.xls') % database until 2009
   
%% get area codes
   
% just be careful to the names 
% (e.g.: Zeeuws Vlaanderen -> Zeeuws-Vlaanderen, Maasvlakte -> Maasvlakte / slufter)
   D.areacode = rws_kustvak(D.p_kustvak);
   
   if iscell(D.areacode)
       D.areacode = [D.areacode{:}];
   end
   
% handling fields. Better to change NaN them to [] in preprocessing.
for jj = 1:length(D.n_code)
     if isnan(D.n_code{jj}); D.n_code{jj} = []; end
     if isnan(D.p_opdrachtgever{jj}); D.p_opdrachtgever{jj} = ''; end
     if isnan(D.p_doelstelling{jj}); D.p_doelstelling{jj} = ''; end
     if isnan(D.l_kustverdediging{jj}); D.l_kustverdediging{jj} = ''; end
     if isnan(D.l_zandgolf{jj}); D.l_zandgolf{jj} = ''; end
     if isnan(D.l_geteigeul{jj}); D.l_geteigeul{jj} = ''; end
     if isnan(D.l_brekerbanken{jj}); D.l_brekerbanken{jj} = ''; end
     if isnan(D.b_methode{jj}); D.b_methode{jj} = ''; end 
     if isnan(D.w_geonaam{jj}); D.w_geonaam{jj} = ''; end 
     if isnan(D.w_afstandsuppletie{jj}); D.w_afstandsuppletie{jj} = ''; end  
     if isnan(D.u_werkwijze{jj}); D.u_werkwijze{jj} = ''; end  
     if isnan(D.u_materieel{jj}); D.u_materieel{jj} = ''; end
     if isnan(D.u_varend{jj}); D.u_varend{jj} = ''; end 
     if isnan(D.u_hydraulisch{jj}); D.u_hydraulisch{jj} = []; end
     if isnan(D.d_peildatum{jj}); D.d_peildatum{jj} = ''; end 
     if isnan(D.m_inpeiling{jj}); D.m_inpeiling{jj} = ''; end 
     if isnan(D.m_uitpeiling{jj}); D.m_uitpeiling{jj} = ''; end
     if isnan(D.d_ontwerpmethode{jj}); D.d_ontwerpmethode{jj} = ''; end
     if isnan(D.d_modellen{jj}); D.d_modellen{jj} = ''; end
     if isnan(D.o_opmerkingen{jj}); D.o_opmerkingen{jj} = ''; end
     if isnan(D.x_update{jj}); D.x_update{jj} = ''; end     
end

%% get raai coordinates (nearest)
   
   [D.x_beginraai,D.y_beginraai] = jarkus_rsp2xy(0,D.areacode,D.l_beginraai*100);
   [D.x_eindraai ,D.y_eindraai ] = jarkus_rsp2xy(0,D.areacode,D.l_eindraai *100);
   
%% get volume per m beach
   
   l_lengteraai_min = min(D.l_lengteraai);
   
   % if D.volume{} is char (e.g. 'samen'), just divide the volume for the empty row 
   % by the number of its transects
   
   for ii = 1:length(D.l_lengteraai)
       if strcmp(D.volume{ii},'samen') % apply weighted average
           dummy          = D.volume{ii-1};
           D.volume{ii-1} = dummy*D.l_lengteraai(ii-1)/(D.l_lengteraai(ii-1)+D.l_lengteraai(ii));
           D.volume{ii}   = dummy*D.l_lengteraai(ii)/(D.l_lengteraai(ii-1)+D.l_lengteraai(ii));
       end
   end
   D.volume = cell2mat(D.volume);
   
   D.volume_per_m = D.volume./D.l_lengteraai; % m3/m
   
   disp(['set minumum suppletie length to ',num2str(l_lengteraai_min)])
   
%% get dates
   
   D.datenum0 = datenum(D.p_beginuitvoering,D.p_beginmnduitv,1);
   D.datenum1 = datenum(D.p_einduitvoering, D.p_eindmnduitv ,1);
