function dist = jarkus_distancetoZ(Z,z,x)
% jarkus_distancetoZ calculates the distance to a certain depth Z
% if profile crosses depth multiple times all crossings are given
% this function is used by jarkus_getdepthline
%
% Syntax:
% distance=jarkus_distancetoZ(Z,altitude,distance)
%
% Input:
% Z = specified depth
% z = vector with depths at locations x
% x = vector with distances (for example distance from RSP)
%
% Output:
% dist = distance(s) to Z or NaN if profile is not crossed
%
% See also: JARKUS, snctools
%% Copyright notice
% --------------------------------------------------------------------
% Copyright (C) 2010 Deltares
% Tommer Vermaas
%
% tommer.vermaas@gmail.com
%
% 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 .
% --------------------------------------------------------------------
% This tool is part of OpenEarthTools.
% 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
% Created: 31 Oct 2016
% Created with Matlab version: 8.6.0.267246 (R2015b)
% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $
%%
x = x(~isnan(z)); %cross-shore distance
if ~iscolumn(x); x=x'; end
z = z(~isnan(z)); %altitude at x
if ~iscolumn(z); z=z'; end
up=z>=Z; %indices of z>=Z
low=z