function GAP=adi_svp_read(svp_root);
% Call GAP=adi_svp_read(svp_root);
%
% INPUTS:
% svp_root	= Root of _svp file to be read;
%
% OUTPUTS:
% GAP	= geoacoustic profile with fields
%	nsvp	= number of ssp points in water column;
%	ssp		= two-column matrix of ssp with z in m in first column, c in m/s in second;
%	nlay	= number of bottom layers;
% 	bot_layers(1:nlay,1:16)	= geoacoustic paramters of nlay layers with
%		  columns: 	[ii,h,cp1,cp2,ap1,ap2,rho1,rho2,cs1,cs2,as1,as2,beta,ctol,fexpp,fexps];
% 	hsp_top(1,1:7)			= geoacousitc parmaters of upper halfspace with
% 		  columns:	[cp,ap,rho,cs,as,fexpp,fexps];
% 	hsp_bot(1,1:7)			= geoacousitc parmaters of lower halfspace with
% 		  columns:	[cp,ap,rho,cs,as,fexpp,fexps];
%
%
% 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.
svp_file=[svp_root,'.svp'];
fid_svp=fopen(svp_file,'r');
if fid_svp < 0,
    svp_file2=[svp_root,'_svp'];
    fid_svp=fopen(svp_file2,'r');
    if fid_svp < 0,
	    disp(['ERROR: NEITHER SVP FILE ',svp_file,' NOR ',svp_file2,' COULD BE OPENED FOR READING.']);
	    disp(['       CHECK THAT SVP FILE EXISTS']);
	    return
    else
        svp_file=svp_file2;
    end
end
%
GAP.svp_file=svp_file;
star_seek(fid_svp);
[ver_no,count]=fscanf(fid_svp,'%g',1);
star_seek(fid_svp);
if ver_no>2,
    [GAP.hsp_top,count]=fscanf(fid_svp,'%g',[1,7]); GAP.hsp_top=GAP.hsp_top([1 4 3 2 5 6 7]);
else
    [GAP.hsp_top,count]=fscanf(fid_svp,'%g',[1,5]); GAP.hsp_top=[GAP.hsp_top([1 4 3 2 5]),1,1];
end
star_seek(fid_svp);
[inp,count]=fscanf(fid_svp,'%g',2); GAP.nsvp=inp(1); GAP.ctol=inp(2);
star_seek(fid_svp);
[ssp1,count]=fscanf(fid_svp,'%g',[4,1]);
if count~=4, error('Error reading first line of SSP in water column '); end
[ssp2,count]=fscanf(fid_svp,'%g',[2,GAP.nsvp-1]);
if count~=(GAP.nsvp-1)*2, error('Error reading SSP in water column '); end
GAP.ssp=[ssp1(1:2)'; ssp2'];
if length(ssp1>=4), 
	GAP.rho_ssp=ssp1(3); GAP.attn_ssp=ssp1(4); 
else
	GAP.rho_ssp=1; GAP.attn_ssp=0; 
end
%
star_seek(fid_svp);
[GAP.nlay,count]=fscanf(fid_svp,'%g',1);
star_seek(fid_svp);
GAP.labels={'ii','h','cp1','cp2/g','ap1','ap2','rho1','rho2','cs1','cs2','as1','as2','beta/g','ctol', ...
	'fexpp','fexps'};
GAP.bot_layers=zeros(GAP.nlay,16);
for jlay=1:GAP.nlay,
	pline=fgetl(fid_svp);
	idx1=0; idx2=length(pline); parms=[];
	while idx1+1<idx2,
		[pp,count,err,nexti]=sscanf(pline(idx1+1:idx2),'%g');
		parms=[parms; pp]; idx1=idx1 + nexti;
	end
	nparm=length(parms);
    if ver_no>=2 & nparm~=16,
		disp(['WARNING for bottom layer ',int2str(jlay),': Should have 16 entries for ver # 3.0+.']);
    else
	    if parms(1)>1 & nparm<14,
		    disp(['WARNING for bottom layer ',int2str(jlay),': Should have 14 entries for ii>1.']);
    	end 
	    if parms(1)<-1 & nparm<16,
    		disp(['WARNING for bottom layer ',int2str(jlay),': Should have 16 entries for ii<1.']);
    	end 
    	if parms(1)==1 & nparm<12,
		    disp(['WARNING for bottom layer ',int2str(jlay),': Should have 12 entries for ii=1.']);
	    end
	    if parms(1)==-1 & nparm<14,
		    disp(['WARNING for bottom layer ',int2str(jlay),': Should have 14 entries for ii=-1.']);
	    end   
    	if parms(1)==-1,
% For ii=-1, put [fexpp;fexps] in columns 15&16 instead of 13&14 if necessary:
	    	if nparm==14, parms=[parms(1:12); 0; 0; parms(13:14)]; end
	    	nparm=16;
	    end
    	if parms(1)<0,
		    fexp=parms(15:16);
		    if any(fexp<.5) | any(fexp)>3,
			    disp(['WARNING for bottom layer ',int2str(jlay), ...
			    ': Make sure entered frequency exponents are valid. [fexpp,fexps] = ',num2str(fexp.')])
		    end
	    end
    end
    if ver_no>=2,
	    fexp=parms(15:16);
	    if any(fexp<.5) | any(fexp)>3,
		    disp(['WARNING for bottom layer ',int2str(jlay), ...
                    ': Make sure entered frequency exponents are valid. [fexpp,fexps] = ',num2str(fexp.')]);
            disp('Setting to unity ...');
            parms(15:16)=1;
	    end
    end
	GAP.bot_layers(jlay,1:nparm)=parms.';
end
GAP.bot_layers=GAP.bot_layers(:,[1 2 3 4 9 10 7 8 5 6 11:16]);
%
star_seek(fid_svp);
if ver_no>2,
    [GAP.hsp_bot,count]=fscanf(fid_svp,'%g',[1,7]); GAP.hsp_bot=GAP.hsp_bot([1 4 3 2 5 6 7]);
else
    [GAP.hsp_bot,count]=fscanf(fid_svp,'%g',[1,5]); GAP.hsp_bot=[GAP.hsp_bot([1 4 3 2 5]),1,1];
end

%fprintf(fid_svp,'1/-1  h cp1 cp2 cs1 cs2 rho1 rho2 ap1 ap2 as1 as2 [fexpp fexps]\n');
%fprintf(fid_svp,'2/-2  h cp1   g cs1 cs2 rho1 rho2 ap1 ap2 as1 as2 beta ctol [fexpp fexps]\n');
%fprintf(fid_svp,'3/-3  h cp1 cp2 cs1 cs2 rho1 rho2 ap1 ap2 as1 as2 beta ctol [fexpp fexps]\n');
%fprintf(fid_svp,'4/-4  h cp1 cp2 cs1 cs2 rho1 rho2 ap1 ap2 as1 as2    g ctol [fexpp fexps]\n');
%fprintf(fid_svp,'*(6) ii  h  cp1 cp2/g  cs1  cs2  rho1 rho2  ap1 ap2  as1 as2  beta/g ctol\n');
%
fclose(fid_svp);
disp(['Finished reading SVP file ',svp_file,'.']);

