function [D,U]  = sedimentatlas_korrel_read(filename)
%SEDIMENTATLAS_KORREL_READ   Reads ASCII grain size file from SedimentAtlas Waddenzee.
%
%  [D,U]  = sedimentatlas_korrel_read(filename)
%
% Reads grain size sieve distributions from sediment atlas WaddenZee.
% * Rijkswaterstaat, 1998. Sedimentatlas Waddenzee. CD-Rom Rijkswaterstaat
%   Waterdienst. Available from: http://www.waddenzee.nl (in Dutch).
%

% Calculates mud distribution with algorithms in EVA II project.
% * Rijkswaterstaat, 2004. Bodemgesteldheid en mechanische kokkelvisserij in de
%   Waddenzee. Rijkswaterstaat Waterdienst RIZA report 2004.028. Available
%   from: http://www.rijkswaterstaat.nl (in Dutch).
% 
%See also: HTTP://WWW.WADDENZEE.NL/

% © G.J. de Boer, Sept. 20th. 2004 (g.j.deboer@tudelft.nl)

% TO DO: calculate D50 and D90

DAT              = dlmread(filename,',',1,0);

D.AREA           = DAT(:, 1);
D.PERIMETER      = DAT(:, 2);
D.KORREL_        = DAT(:, 3);
D.KORREL_ID      = DAT(:, 4);
D.X_COORD        = DAT(:, 5);
D.Y_COORD        = DAT(:, 6);
D.RELWAARDE      = DAT(:, 7);
D.PHI( 1,:)      = DAT(:, 8);
D.PHI( 2,:)      = DAT(:, 9);
D.PHI( 3,:)      = DAT(:,10);
D.PHI( 4,:)      = DAT(:,11);
D.PHI( 5,:)      = DAT(:,12);
D.PHI( 6,:)      = DAT(:,13);
D.PHI( 7,:)      = DAT(:,14);
D.PHI( 8,:)      = DAT(:,15);
D.PHI( 9,:)      = DAT(:,16);
D.PHI(10,:)      = DAT(:,17);
D.PHI(11,:)      = DAT(:,18);
D.PHI(12,:)      = DAT(:,19);
D.PHI(13,:)      = DAT(:,20);
D.PHI(14,:)      = DAT(:,21);
D.PHI(15,:)      = DAT(:,22);
D.PHI(16,:)      = DAT(:,23);
D.PHI(17,:)      = DAT(:,24);
D.PHI(18,:)      = DAT(:,25);
D.PHI(19,:)      = DAT(:,26);
D.PHI(20,:)      = DAT(:,27);
D.PHI(21,:)      = DAT(:,28);
D.PHI(22,:)      = DAT(:,29);
D.PHI(23,:)      = DAT(:,30);
D.PHI(24,:)      = DAT(:,31);
D.PHI(25,:)      = DAT(:,32);
D.PHI(26,:)      = DAT(:,33);

U.PHI            = 'percentage_by_weight';

D.cumPHI         = 100-cumsum(D.PHI);

D.SUMPHI         = sum(D.PHI,1)';
D.MODPHI         = max(D.PHI)';

D.pmud16_from_16 = 0.2988.*sum(D.PHI(17:end,:),1)'; % mud if D < 16 um corrected according to Zwarts 2004
D.pmud16_from_63 = 0.1879.*sum(D.PHI(13:end,:),1)'; % mud if D < 63 um corrected according to Zwarts 2004. Volgens mij moet dit D.PHI(14:end,:) zijn....

D.pmud_16 = sum(D.PHI(17:end,:),1)'; % mud if D < 16 um not corrected
D.pmud_63 = sum(D.PHI(13:end,:),1)'; % mud if D < 63 um not corrected

U.pmud16_from_16 = 'percentage_by_weight';
U.pmud16_from_63 = 'percentage_by_weight';

D.GEMETEN        = DAT(:,34);
D.NR             = DAT(:,35);
D.info1          = 'PHIs 1st index zeef percentages [-20:5:105]';
D.info2          = 'PHIs 2nd index station number';
D.pmud16_from_16_info = 'EVA II algorithm to calculate fine sediment fraction from sieve diamaters distribution <http://www.rijkswaterstaat.nl/rws/riza/home/publicaties/rapporten/2004/2004_028.htm>, Bodemgesteldheid en mechanische kokkelvisserij in de Waddenzee / RIZA Rapport 2004.028/2004/';
D.pmud16_from_63_info = 'EVA II algorithm to calculate fine sediment fraction from sieve diamaters distribution <http://www.rijkswaterstaat.nl/rws/riza/home/publicaties/rapporten/2004/2004_028.htm>, Bodemgesteldheid en mechanische kokkelvisserij in de Waddenzee / RIZA Rapport 2004.028/2004/';

D.PHIvalue( 1,:) = -2.0;
D.PHIvalue( 2,:) = -1.5;
D.PHIvalue( 3,:) = -1.0;
D.PHIvalue( 4,:) = -0.5 ;
D.PHIvalue( 5,:) = 0  ;
D.PHIvalue( 6,:) = 0.5  ;
D.PHIvalue( 7,:) = 1.0 ;
D.PHIvalue( 8,:) = 1.5 ;
D.PHIvalue( 9,:) = 2.0 ;
D.PHIvalue(10,:) = 2.5 ;
D.PHIvalue(11,:) = 3.0 ;
D.PHIvalue(12,:) = 3.5 ;
D.PHIvalue(13,:) = 4.0 ;
D.PHIvalue(14,:) = 4.5 ;
D.PHIvalue(15,:) = 5.0 ;
D.PHIvalue(16,:) = 5.5 ;
D.PHIvalue(17,:) = 6.0 ;
D.PHIvalue(18,:) = 6.5 ;
D.PHIvalue(19,:) = 7.0 ;
D.PHIvalue(20,:) = 7.5 ;
D.PHIvalue(21,:) = 8.0 ;
D.PHIvalue(22,:) = 8.5 ;
D.PHIvalue(23,:) = 9.0 ;
D.PHIvalue(24,:) = 9.5 ;
D.PHIvalue(25,:) = 10.0;
D.PHIvalue(26,:) = 10.5;

U.PHIvalue = 'diameter in mm = 2^(-phi)';

D.D( 1,:)        = 4000; % 2828 - 4000
D.D( 2,:)        = 2828; % 2000 - 2828
D.D( 3,:)        = 2000; % 1414 - 2000
D.D( 4,:)        = 1414; % 1000 - 1414
D.D( 5,:)        = 1000; % 707 - 1000
D.D( 6,:)        =  707; % 500 - 707
D.D( 7,:)        =  500; % 354 - 500
D.D( 8,:)        =  354; % 250 - 354
D.D( 9,:)        =  250; % 177 - 250
D.D(10,:)        =  177; % 125 - 177
D.D(11,:)        =  125; % 88 - 125
D.D(12,:)        =   88; % 63 - 88
D.D(13,:)        =   63; % 44 - 63
D.D(14,:)        =   44; % 32 - 44
D.D(15,:)        =   32; % 22 - 32
D.D(16,:)        =   22; % 16 - 22
D.D(17,:)        =   16; % 11 - 16
D.D(18,:)        =   11; % 8 - 11
D.D(19,:)        =    8.0; % 5.5 - 8
D.D(20,:)        =    5.5; % 4 - 5.5
D.D(21,:)        =    4.0; % 2.7 - 4
D.D(22,:)        =    2.7; % 2 - 2.7
D.D(23,:)        =    2.0; % 1.4 - 2
D.D(24,:)        =    1.4; % 1-1.4
D.D(25,:)        =    1.0; % 0.7 - 1
D.D(26,:)        =    0.7; % <0.7

U.D              = 'micrometer';
