function [D]  = read_sedimentatlas_1990(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)

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

D.sampleno           = DAT(:, 4);
D.x                  = DAT(:, 5);
D.y                  = DAT(:, 6);
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);

D.phi                = fliplr(D.phi); %small to large

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

D.particlesize       = fliplr(D.particlesize); %small to large

D.cumphi             = cumsum(D.phi,2);

msk1 = D.particlesize>=63; %sand
msk2 = D.particlesize<63 & D.particlesize>=2; %silt
msk3 = D.particlesize<2; %clay

D.sand = sum(D.phi(:,msk1),2);
D.silt = sum(D.phi(:,msk2),2);
D.clay = sum(D.phi(:,msk3),2);

for ii = 1:size(D.phi,1)
    perc=10;
    D.d10(ii,1) = max([D.particlesize(1,1) D.particlesize(find(D.cumphi(ii,:)<perc,1,'last')) + ((D.particlesize(find(D.cumphi(ii,:)>perc,1,'first'))-D.particlesize(find(D.cumphi(ii,:)<perc,1,'last')))/(D.cumphi(ii,find(D.cumphi(ii,:)>perc,1,'first'))-D.cumphi(ii,find(D.cumphi(ii,:)<perc,1,'last'))))*(perc-D.cumphi(ii,find(D.cumphi(ii,:)<perc,1,'last')))]);  
    perc=50;
    D.d50(ii,1) = max([D.particlesize(1,1) D.particlesize(find(D.cumphi(ii,:)<perc,1,'last')) + ((D.particlesize(find(D.cumphi(ii,:)>perc,1,'first'))-D.particlesize(find(D.cumphi(ii,:)<perc,1,'last')))/(D.cumphi(ii,find(D.cumphi(ii,:)>perc,1,'first'))-D.cumphi(ii,find(D.cumphi(ii,:)<perc,1,'last'))))*(perc-D.cumphi(ii,find(D.cumphi(ii,:)<perc,1,'last')))]);  
    perc=90;
    D.d90(ii,1) = max([D.particlesize(1,1) D.particlesize(find(D.cumphi(ii,:)<perc,1,'last')) + ((D.particlesize(find(D.cumphi(ii,:)>perc,1,'first'))-D.particlesize(find(D.cumphi(ii,:)<perc,1,'last')))/(D.cumphi(ii,find(D.cumphi(ii,:)>perc,1,'first'))-D.cumphi(ii,find(D.cumphi(ii,:)<perc,1,'last'))))*(perc-D.cumphi(ii,find(D.cumphi(ii,:)<perc,1,'last')))]);  
end

D.dimDinfo       = 'micrometer';
D.phiinfo        = 'percentage_by_weight';
D.instrument     = 'van Veen grab';