function [x y z info] = arc_asc_read(fname, varargin)
%ARC_ASC_READ  Reads ASC file generated by ArcGIS
%
%   Reads ASC file generated by ArcGIS. Returns a bathymetry in three
%   matrices x, y and z and an info structure containing the file headers.
%
%   Syntax:
%   [x y z info] = arc_asc_read(fname, varargin)
%
%   Input:
%   fname       = Filepath to ASC file
%   varargin    = header:   number of header lines in ASC file
%                 format:   number format used in ASC file
%                 zscale:   division factor for elevation values
%
%   Output:
%   x           = Matrix with x-coordinates (pixel centres)
%   y           = Matrix with y-coordinates (pixel centres)
%   z           = Matrix with z-values
%
%   Example
%   [x y z info] = arc_asc_read(fname)
%
%See also: ARCGISREAD (reads same file)

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2010 Deltares
%       Bas Hoonhout
%
%       bas.hoonhout@deltares.nl	
%
%       Rotterdamseweg 185
%       2629HD Delft
%
%   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/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://OpenEarth.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 14 Dec 2010
% Created with Matlab version: 7.9.0.529 (R2009b)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%% read options

OPT.header = 6;
OPT.format = '%f';
OPT.zscale = 1;

OPT = setproperty(OPT, varargin{:});

%% read asc file

% output vars
x = [];
y = [];
z = [];
info = struct();

if exist(fname, 'file')
    
    [pathstr, name, ext, versn] = fileparts(fname);

    fid = fopen(fname);

    % walk through file
    i = 1; j = 1; ni = 0;
    while ~feof(fid)
        fline = fgetl(fid);
        fline = strrep(fline, ',', '.');

        if i <= OPT.header
            % first lines are header lines, store in info struct
            [n,rest] = strtok(fline);
            v = str2num(rest);
            info.(upper(n)) = v;

            i = i + 1;
        else
            % scan current line for numbers and mask empty values
            zi = sscanf(fline, OPT.format);
            zi(zi == info.NODATA_VALUE) = nan;

            % create matrix
            if j == 1
                z = nan([info.NROWS*info.NCOLS/length(zi) length(zi)]);
            end

            % fill matrix
            z(size(z,1)-j+1,:) = zi;

            j = j + 1;
        end
    end

    fclose(fid);

    % create output
   %x = info.XLLCORNER+[0  :1:info.NCOLS    ]*info.CELLSIZE; % these are the corners that span the pixels
   %y = info.YLLCORNER+[0  :1:info.NROWS    ]*info.CELLSIZE; % these are the corners that span the pixels
    x = info.XLLCORNER+[0.5:1:info.NCOLS-0.5]*info.CELLSIZE; % these are the CENTRES where the data reside
    y = info.YLLCORNER+[0.5:1:info.NROWS-0.5]*info.CELLSIZE; % these are the CENTRES where the data reside
    z = reshape(z, info.NROWS, info.NCOLS)./OPT.zscale;
    
else
    disp(['File does not exist [' fname ']']);
end
