function svp_write(svp_root,svp,geo_layers,geo_hspace);
% Call svp_write(svp_root,svp,geo_layers,geo_hspace);
%
% svp	= two-column matrix with z in m in first column, c in m/s in second.
%		  Trailing NaN rows are ignored;
% geo_layers(1:nlay,1:16)	= geoacoustic profile parameters in ORCA input format.
% geo_hspace(1,1:7)			= cp,cs,rho,ap,as,fexpp,fexps of lower halfspace;
%
%
% 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.
[nr,nc]=size(geo_layers); [nrh,nch]=size(geo_hspace);
if nr>0 & nc~=16,
	disp(num2str(geo_layers));
	error('geo_layers must be nlay by 16.');
end
if nrh~=1 | nch~=7,
	disp(num2str(geo_hspace));
	error('geo_hspace must be 1 by 7: cp,cs,rho,ap,as,fexpp,fexps.');
end
%
svp_file=[svp_root,'.svp'];
fid_out=fopen(svp_file,'w');
if fid_out < 0,
	disp(['ERROR: SVP FILE ',svp_file,' COULD NOT BE OPENED FOR WRITING.']);
	disp(['       CHECK THAT IT IS NOT BEING EDITED']);
	return
end
%
nsvp=length(find(~isnan(svp(:,1))));
nlay=size(geo_layers,1);
%
fprintf(fid_out,'	      ### SVP FILE FOR ORCA NORMAL MODE AND GAMA RAY MODELS ###\n');
fprintf(fid_out,'           ### ENTER DATA AFTER LINES WITH * IN FIRST COLUMN ###\n');
fprintf(fid_out,'                  ### USE OTHER LINES FOR COMMENTS ###\n');
fprintf(fid_out,'Note: ap,as = p-wave, s-wave attenuations (pos~db/m/kHz, neg~dB/wavelength,\n');
fprintf(fid_out,'              999 ~ Thorpe attenuation in seawater)\n');
fprintf(fid_out,'Note: Do not change version number in first line\n');
fprintf(fid_out,'*(1) ver_no  ''SVP TITLE'':\n');
fprintf(fid_out,'        3.0  ''%s'' \n',svp_file);
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
fprintf(fid_out,'*(2) cp      cs     rho      ap      as   fexpp   fexps   For Upper Halfspace:\n');
fprintf(fid_out,'  343.0     0.0  .00121     0.0     0.0     1.0     1.0\n');
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
%
fprintf(fid_out,'nsvp = number of SVP points in ocean to be read on Line (4)\n');
fprintf(fid_out,'ctol = tolerance used in fitting SVP to eliminate layers (0=keep all layers)\n');
fprintf(fid_out,'*(3) nsvp  ctol\n');
fprintf(fid_out,'%9i     0\n',nsvp); 
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
fprintf(fid_out,'Ocean SVP Profile (add constant rho, ap in ocean on 1st line):\n');
fprintf(fid_out,'*(4)  z       cp    rho  ap [rho,ap first line only]\n');
fprintf(fid_out,'%7.2f  %7.2f   1.05   0\n',svp(1,1),svp(1,2));
for jsvp=2:nsvp
	fprintf(fid_out,'%7.2f  %7.2f\n',svp(jsvp,1),svp(jsvp,2));
end
%
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
fprintf(fid_out,'*(5) nbot (# bottom layers to be read in Line 6):\n');
fprintf(fid_out,'%9i\n',nlay);
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
fprintf(fid_out,'Layer Profiles: type: 1=linear, 2=blug, 3=blug, 4=blug;   examples: \n');
fprintf(fid_out,'[Note: type<0 ==> add attn freq exponents (fexpp,fexps) at end of line]\n');
fprintf(fid_out,'1  h  cp1 cp2  cs1 cs2  rho1 rho2  ap1 ap2  as1 as2 dummy dummy fexpp fexps\n');
fprintf(fid_out,'2  h  cp1   g  cs1 cs2  rho1 rho2  ap1 ap2  as1 as2  beta  ctol fexpp fexps\n');
fprintf(fid_out,'3  h  cp1 cp2  cs1 cs2  rho1 rho2  ap1 ap2  as1 as2  beta  ctol fexpp fexps\n');
fprintf(fid_out,'4  h  cp1 cp2  cs1 cs2  rho1 rho2  ap1 ap2  as1 as2     g  ctol fexpp fexps\n');
fprintf(fid_out,'(Layer 1 only: h<0 ==> 2-way travel time, cp1<0 ==> cp1/cw ratio.)\n');
%
fprintf(fid_out,'*(6) ii  h  cp1 cp2/g  cs1 cs2  rho1 rho2  ap1 ap2  as1 as2  b/g ctol fxp fxs\n');
%
fmt=['%1i %1.2f %1.1f,%1.1f %1.1f,%1.1f %1.2f,%1.2f %0.3f,%0.3f ', ...
	'%1.2f,%1.2f %1.2f,%1.1f %2.1f,%2.1f']; 
fmt2=['%1i %1.2f %1.3f,%1.1f %1.1f,%1.1f %1.2f,%1.2f %0.3f,%0.3f ', ...
	'%1.2f,%1.2f %1.2f,%1.1f %2.1f,%2.1f']; 
for jlay=1:nlay,
    if jlay==1 & geo_layers(jlay,3)<0,
    	fprintf(fid_out,fmt2,geo_layers(jlay,:));
    else
    	fprintf(fid_out,fmt,geo_layers(jlay,:));
    end
%	if jlay<nlay, fprintf(fid_out,'\n',[]); end
	fprintf(fid_out,'\n',[]);
end
%
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
fprintf(fid_out,'*(7) cp         cs     rho      ap      as   fexpp   fexps   For Lower Halfspace:\n');
fmt=' %1.1f     %1.1f    %1.2f   %0.3f    %0.2f     %0.1f     %0.1f\n';
%%disp('Check format of halfspace.  Should come out here as cp cs rho ap as: ');
%%disp(geo_hspace(3:2:11));
fprintf(fid_out,fmt,geo_hspace(1:7));
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
fprintf(fid_out,'*(8) ntop (# Top Layers to Be Read on Line 9):\n');
fprintf(fid_out,'        0\n');
fprintf(fid_out,'-------------------------------------------------------------------------------\n');
fprintf(fid_out,'*(9) ii h cp1,cp2/g cs1,cs2 rho1,rho2 ap1,ap2 as1,as2 beta/g,ctol fexpp,fexps\n');
%
fclose(fid_out);
disp(['Created SVP file ',svp_file,'.']);
