function GAPx=adi_svp_interp(GAP)
%
% This subroutine interprets the geoacoustic profile from an ORCA SVP file
% and converts it to standard units;
%
nlayx=0;
geox=zeros(2,5,3*GAP.nlay); hx=zeros(3*GAP.nlay,1); fexp=ones(2,3*GAP.nlay);
for jlay=1:GAP.nlay,
	nlayx=nlayx + 1;
	ii=GAP.bot_layers(jlay,1);
	h=GAP.bot_layers(jlay,2);
	cp1=GAP.bot_layers(jlay,3);
	if jlay==1 & cp1<0,
		cs_cw_rat=-cp1;		% Negative sound speed means a csed/cwater ratio;
		cp1=cs_cw_rat*1500;	% Leave geox(1,1,1) negative since csed value depends on cw;
        GAP.bot_layers(jlay,3)=cp1;
	end
	if jlay==1 & h<0,
		tau_2way=-h; 	% Negative thickness means a two-way travel time in s;
		gbar=0.75;		% Nominal average gradient (since we don't know it for sure):
		h=cp1*(exp(gbar*tau_2way/2) - 1)/gbar; GAP.bot_layers(jlay,2)=h;
		disp(['Negative thickness interpreted as two-way travel time: h = ',num2str(h)]);
	end
%
	hx(nlayx)=h;
	geox(1,1:5,nlayx)=GAP.bot_layers(jlay,[3:2:11]);
	geox(2,1:5,nlayx)=GAP.bot_layers(jlay,[4:2:12]);
	switch abs(ii)
	case 1,
		cp2=GAP.bot_layers(jlay,4);
	case 2,
		g=GAP.bot_layers(jlay,4);
		beta=GAP.bot_layers(jlay,13);
		cp2=blug_eval(h,cp1,-999,g,beta,0);
	case 3,
		cp2=GAP.bot_layers(jlay,4);
		beta=GAP.bot_layers(jlay,13);
		dcp=cp2 - cp1;
		g=blug_eval(h,cp1,cp2,-999,beta,0);
	case 4,
		cp2=GAP.bot_layers(jlay,4);
		g=GAP.bot_layers(jlay,13);
		dcp=cp2 - cp1;
		beta=blug_eval(h,cp1,cp2,g,-999,0);
	otherwise,
		error(['ILLEGAL BOTTOM TYPE FOR LAYER ',int2str(jlay),': ',num2str(ii)]);
	end
% Interpret negative attenuations as dB/lambda and convert to dB/m/kHz:
	geox(1,2,nlayx)=attn_check(geox(1,2,nlayx),cp1);
	geox(2,2,nlayx)=attn_check(geox(2,2,nlayx),cp2);
	geox(1,5,nlayx)=attn_check(geox(1,5,nlayx),geox(1,4,nlayx));
	geox(2,5,nlayx)=attn_check(geox(2,5,nlayx),geox(2,4,nlayx));
%%	if ii<0, fexp(:,nlayx)=GAP.bot_layers(jlay,15:16)'; end
	fexp(:,nlayx)=GAP.bot_layers(jlay,15:16)';
% Examine BLUG layers and subdivide if necessary:
	if any(abs(ii)==[2 3 4]),
		geox(2,1,nlayx)=cp2;
		ctol=GAP.bot_layers(jlay,14);
		if ctol==0, ctol=5; end
		if ctol<0,
			error(['ILLEGAL CTOL FOR LAYER ',int2str(jlay),': ',num2str(ctol)]);
		end
		done=0; hb=h;
		while done==0,
			zb=linspace(0,hb,21);
			cb=blug_eval(hb,cp1,cp2,g,beta,zb);
			clin=linspace(cp1,cp2,21);
%			clin=1./sqrt(linspace(1/cp1^2,1/cp2^2,21));
			iib=find(abs(cb - clin) > ctol);
			if ~isempty(iib),
				h1=zb(iib(1)); c1=cb(iib(1));
				nlayx=nlayx + 1;
				geox(:,:,nlayx)=geox(:,:,nlayx-1);	% Copy parms
				hx(nlayx-1)=h1; hx(nlayx)=h-h1;		% Set thicknesses
				fexp(1:2,nlayx)=fexp(1:2,nlayx-1);	% Set frequency exponents
				geox(2,1,nlayx-1)=c1;				% Set cp at h1
				fac=(c1 - cp1)/(cp2 - cp1);			% Interpolate other four parms:
				geox(2,2:5,nlayx-1)=geox(1,2:5,nlayx-1) + fac* ...
					(geox(2,2:5,nlayx-1)-geox(1,2:5,nlayx-1));
				geox(1,1:5,nlayx)=geox(2,1:5,nlayx-1);	% Make continuous
% Update hb,cp1,cp2,g,beta to check on next part of layer:
				g=g*cp1*(1+beta)/(c1 + beta*cp1);	% Compute gradient at h1
				hb=hx(nlayx); cp1=c1; beta=blug_eval(hb,cp1,cp2,g,-999,0); 
			else
				done=1;
			end
		end
	end
end
%%keyboard
% Enter halfspace as last layer:
nlayx=nlayx + 1; hx(nlayx)=0;
geox(1,1:5,nlayx)=GAP.hsp_bot(1:5); 
geox(2,1:5,nlayx)=GAP.hsp_bot(1:5); 
if length(GAP.hsp_bot)<7,
    fexp(:,nlayx)=[1; 1];
    disp('Warning: fexpp,fexps of halfspace not read');
else
    fexp(:,nlayx)=GAP.hsp_bot(6:7)';
end
%
GAPx.svp_file=GAP.svp_file;
GAPx.nlay=nlayx; GAPx.h=hx(1:nlayx);
GAPx.geo=geox(:,:,1:nlayx); GAPx.fexp=fexp(:,1:nlayx);
%
%%%
%
function ap=attn_check(ap,cp);
% Check for negative attenuation and, if so, converts from dB/lambda to dB/m/kHz:
if ap<0, ap=-ap/(cp/1000); else, ap=ap; end
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
