function A = siltprofiler_read_asc(fname)
%SILTPROFILER_READ_ASC   read ASCII file from Deltares SiltProfiler
%
%   D = siltprofiler_read_asc(ascname)
%
%See also: SILTPROFILER_PLOT

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2010 Delft University of Technology
%       Gerben J. de Boer
%
%       g.j.deboer@tudelft.nl	
%
%       Fluid Mechanics Section
%       Faculty of Civil Engineering and Geosciences
%       PO Box 5048
%       2600 GA Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

%% read header

   A = dir(fname);

   fid                = fopen(fname);
   rec                = fgetl(fid);
   A.header           = [];
   A.n_channel        = 0;
   A.n_sampler        = 0;
   A.varname          = ''; % lower case, letter 1 to 4
   A.units            = '';
   A.calibration      = [];
   A.sampler_depth    = [];
   A.sampler_altitude = [];

   A.pres             = [];
   A.r                = [];
   A.t                = [];
   A.obs              = [];
   A.chlo             = [];
   A.alti             = [];
   A.grou             = [];
   A.supp             = [];
   A.depth            = [];
   A.salinity         = [];
   A.samp1            = [];
   A.samp2            = [];
   A.samp3            = [];
   A.fit1             = [];
   A.fit2             = [];
   A.fit3             = [];
   A.fit4             = [];
 
   while strcmpi(strtok(rec),'*')
   
   A.header{end+1} = rec;
   
   if any(strfind(lower(rec),'start date'))
   ind = strfind(rec,',');
   yyyy_mm_dd = rec(ind(1)+1:end);
   end

   if any(strfind(lower(rec),'start time'))
   ind = strfind(rec,',');
   A.datenum = datenum([yyyy_mm_dd,rec(ind(1)+1:end)],'yyyy/mm/ddHH:MM:SS');
   A.datestr = datestr(A.datenum,31);
   end

   if any(strfind(lower(rec),'column'))
      A.n_channel   = A.n_channel + 1;
      
      ind = strfind(rec,',');
      A.varname    {end+1} = rec(ind(1)+1:ind(2)-1);
      A.varname    {end}   = lower(A.varname{end}(1:min(4,length(A.varname{end}))));
      A.units      {end+1} = rec(ind(2)+1:ind(3)-1);
      A.calibration{end+1} = str2num(strrep(rec(ind(3)+1:end),',',' '));
   end 
   
    try
   if any(strfind(lower(rec),'sampler'))
      A.n_sampler   = A.n_sampler + 1;
      
      ind1 = strfind(rec,',');
      ind2 = strfind(rec,':');
      A.sampler_depth   (end+1) = str2num(strtok(rec(ind2(1)+1:ind1(2)-1)))./100;
      A.sampler_altitude(end+1) = str2num(strtok(rec(ind2(2)+1:end)))./100;
   end
   catch
      disp('no correct format of samplers found')
   end
    
   
   rec             = fgetl(fid);
   end
   A.header = char(A.header);
   
%% rewind   
   
   fclose(fid);
   fid           = fopen(fname);
   for irow=1:size(A.header,1)
   dummy = fgetl(fid);
   end
   
%% read data

   fmt      = [repmat('%g,',[1 A.n_channel-1]),'%g'];
   raw      = fscanf(fid,fmt,Inf);
   n        = length(raw)./A.n_channel;
   raw      = reshape(raw,[A.n_channel n]);
   
   for icol=1:A.n_channel
   
      varname     = mkvar((A.varname{icol}));
      A.(varname) = raw(icol,:);
    
   end

   A.depth           = [];
   A.varname{10}     = 'Depth';
   A.units{10}       = 'm';
   A.calibration{10} = 1;
   
   A.salinity        = [];
   A.varname{9}      = 'salinity';
   A.units{9}        = 'psu';
   A.calibration{9}  = 1;
   
   clear raw
   
   fclose(fid);

%% EOF