function TL=PE_driver0(iiparm,freq,GAP,xy_src,brg_lims,rng_lims,zs,zr,svg_root,BATHY,out_root,iidB);
%
% Call TL=PE_driver0(iiparm,freq,GAP,xy_src,brg_lims,rng_lims,zs,zr,svg_root,BATHY,out_root,iidB);
%
% Creates RAM PE input file and runs the RAM PE model.
%
% INPUTS:
% iiparm 	= [safety_fac,iires,npt_bathy,nmid,iifast], where,
%	safety_fac	= a factor for PE sampling dr and dz (>1 for more safety)
%	iires		= 1 to make a high-resolution output, 0 for same as in normal mode;
%	npt_bathy	= number of points to interpolate BATHY.hw (0=use normal mode hw_vs_rng);
%	nmid		= number of points in each hw_vs_rng section to output PE profiles (>=1);
%	iifast		= 1 to run optimized, double precision version;
% freq          = Frequency or frequency list;
% GAP			= Geoacoustic profile read from an ORCA SVP file;
% xy_src	    = source position;
% brg_lims	    = [brg1,brg2,nbrg], [brg1,brg2,-dbrg], [brgmin,brgmax], [brg1;brg2;...brgn]
% rng_lims      = [r1,r2,nr], etc;
% zr            = [zs1,zs2,nzs], [zs1,zs2,-dzs], [zs1;zs2;...zsn]; 
% zr	        = [zr1,zr2,nzr], [zr1,zr2,-dzr], [zr1;zr2;...zrn]; 
% out_root	    = output file root for RAM PE run;
% iidB          = 1 for TL in dB, 0 for complex field;
%
% OUTPUTS:
% TL		= structure similar to output of adi_tl_radial containing RAM PE results;
%
%
% 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.
format compact
global SVG PROV
%
if nargin<1 | isempty(iiparm), iiparm=[1 1 0 4 1]; end
if nargin<12, iidB=1; end
if length(iiparm)<5, iifast=1; else, iifast=iiparm(5); end
%
svg_open(svg_root);
if exist('PROV')~=1 | isempty(PROV),
	error('USER ERROR: PROV structure not available.  Choose SVG file in RDA.');
end
if isempty(xy_src),
	error('USER ERROR: You must choose xy position of source');
end
%
ranges=adi_lim_eval(rng_lims); bearings=adi_lim_eval(brg_lims);
zs=adi_lim_eval(zs); 
nbrg=length(bearings); nzs=length(zs);
% Set up output structure containing data and supporting information:
TL.runtime=datestr(now);
TL.svg_root=SVG.out_root;
TL.out_root=[out_root,'_ram'];
[nlat,nlong]=size(BATHY.hw);
lat_box=BATHY.lat([1 nlat nlat 1 1])'; long_box=BATHY.long([1 1 nlong nlong 1]);
lat_box=lat_box(:)'; long_box=long_box(:)';
[x_box,y_box]=bathy_ll2xy(lat_box,long_box);
% TL.lat_box=lat_box; TL.long_box=long_box;
% TL.x_box=x_box; TL.y_box=y_box;
% TL.x_box0=x_box; TL.y_box0=y_box;
TL.x_src=xy_src(1,1); TL.y_src=xy_src(1,2);
[TL.lat_src,TL.long_src]=bathy_xy2ll(TL.x_src,TL.y_src);
TL.freq=freq; 
TL.zs=zs; 
TL.zr=[];
zrmax=max(zr);
if ~iiparm(2),
	if size(zr,3)<3, error('For no high res, must place set zr=[zrmin,zrmax,dzr]'); end
	delz0=zr(1,3);
else
	delz0=0;
end
TL.nrange=length(ranges); TL.ranges=ranges;
% TL.delr=TL.ranges(min(2,TL.nrange))-TL.ranges(1); % TL.dr_miss=0; 
% TL.rmin=TL.ranges(1); TL.rmax=TL.ranges(TL.nrange); 
TL.nbearing=nbrg; TL.bearings=bearings;
% if TL.bearings(nbrg)-TL.bearings(1)>359, 
% 	TL.iicirc=1; 
% else;
% 	TL.iicirc=0; 
% end
% TL.indx_r1=ones(1,nbrg); TL.indx_r2=zeros(1,nbrg);
TL.h_src=interp2(BATHY.xax,BATHY.yax,BATHY.hw,TL.x_src,TL.y_src); 
TL.c_src=[]; TL.svp_src=[];
%
TL.hw_vs_rng=[]; TL.jadi_vs_rng=[]; 
% TL.provs_vs_rng=[];
if iidB==0, iidB=-1; end
TL.iidB=iidB;
% TL.iiTs=0; TL.iiLL=0;

% Read in all SVPs from _svg file used:
nprof=fscanf(SVG.fid,'%i',1);
svp_svg=zeros(100,2,nprof); nz_svg=zeros(1,nprof);
for jprof=1:nprof,
	nz_svg(jprof)=fscanf(SVG.fid,'%i',1);
	zc=fscanf(SVG.fid,'%f',2*nz_svg(jprof));
	svp_svg(1:nz_svg(jprof),1:2,jprof)=reshape(zc,2,nz_svg(jprof))';
end
%
suf=[]; TL.tl_coh=[]; nrmax=0; nzrmax=0; nrun=0; nrng_out=0;
% Loop over bearings:
for jbrg=1:nbrg,
	brgx=bearings(jbrg)*pi/180;
% Find intersection with edges of box and thus rmax:
	[r2sides,betw]=line_intersect(TL.x_src,TL.y_src,brgx, ...
		[x_box(1:4)',x_box(2:5)'],[y_box(1:4)',y_box(2:5)']);
	r_rad=min(r2sides(find(betw)));
	iir=find(ranges <= r_rad+.001); liir=length(iir);
	rmax=ranges(iir(liir));
%%	TL.indx_r2(jbrg)=iir(liir);
	rng=ranges(iir); nrng=length(rng);
	x_end=TL.x_src + rng(nrng)*sin(brgx); 
	y_end=TL.y_src + rng(nrng)*cos(brgx);
	npt=max(3,nrng);
	iifig=0; iipe=1;		% Don't limit hw to hmax done for modes;
	[ii_a,r_a,h_a,ip_a]=adi_prov_rad([TL.x_src,TL.y_src],[x_end,y_end],1,npt,iifig,iipe);
%
	nrh=length(ii_a); nrhmax=size(TL.jadi_vs_rng,1);
	if nrhmax<nrh,
%%		TL.hw_vs_rng=[TL.hw_vs_rng; NaN*ones(nrh-nrhmax,2,nbrg)];
		TL.jadi_vs_rng=[TL.jadi_vs_rng; NaN*ones(nrh-nrhmax,nbrg)];
% 		TL.provs_vs_rng=[TL.provs_vs_rng; NaN*ones(nrh-nrhmax,nbrg)];
	end
	hw_vs_rng_adi=[r_a,h_a];
	TL.jadi_vs_rng(1:nrh,jbrg)=ii_a;
% 	TL.provs_vs_rng(1:nrh,jbrg)=ip_a;
%
	for jzs=1:nzs
		nrun=nrun + 1;
		if nbrg*nzs>1, suf=['_',sprintf('%2.2i',nrun)]; end
		ram_infile=[out_root,suf,'_ram.in'];
		out_file=[out_root,suf];  TL.out_root=out_file;
%
		[hw_vs_rng,svp_src]=PE_convert2(TL,hw_vs_rng_adi,GAP,jbrg,TL.zs(jzs),iiparm,ram_infile, ...
			svp_svg,nz_svg,rmax,zrmax,delz0,BATHY,SVG.gdem_root,SVG.out_root);
		if jzs==1 & jbrg==1, TL.svp_src=svp_src; end
		if jbrg==1, 
			[dzs,kk]=min(abs(TL.zs(jzs)-svp_src(:,1))); 
			TL.c_src(jzs)=svp_src(kk,2);
		end
		if jzs==1,
			nrh=size(hw_vs_rng,1); nrhmax=size(TL.hw_vs_rng,1);
			if nrhmax<nrh,
				TL.hw_vs_rng=[TL.hw_vs_rng; NaN*ones(nrh-nrhmax,2,nbrg)];
			end
			TL.hw_vs_rng(1:nrh,:,jbrg)=hw_vs_rng;
		end
% Run RAM PE:
		ramp(ram_infile,out_file);
		if isunix | strcmp(computer,'PCWIN'),
			if exist([out_file,'.hdf'])~=2,
				disp(['Output HDF file from RAMP not present.  RAM did not finish successfully.']);
				TL=[]; return
			end
			SD=hdfsd_open([out_file,'.hdf']);
			[DATA,status]=hdfsd_read(SD,[],1);
%%			size(data),dim_sizes,status,scales,SD.data_name
			range=DATA.scales{2}; depth=DATA.scales{1};
% Determine number of receiver depths and ranges available:
			nzr(jzs,jbrg)=length(depth);
			nrange(jzs,jbrg)=length(range); 
			rmax=range(end);
			if nrange(jzs,jbrg)>nrng_out, 
				rng_out=range(:); nrng_out=length(rng_out);
			end
			if nzr(jzs,jbrg)>length(TL.zr), TL.zr=depth; end
% 			[dum,TL.indx_r2(jbrg)]=min(abs(rng_out-rmax));
            if iidB==1,
    			TEMP(jzs,jbrg).tl_coh=DATA.data.';
            else
				[PHASE,status]=hdfsd_read(SD,[],2);
                field=10.^(DATA.data/20) .* exp((i*pi/180)*PHASE.data);
    			TEMP(jzs,jbrg).tl_coh=field.';
            end
		end
	end
end
%
if (isunix | strcmp(computer,'PCWIN')) & ~isempty(out_root),
	TL.nrange=nrng_out; TL.ranges=rng_out;
% 	TL.rmin=TL.ranges(1); TL.rmax=TL.ranges(nrng_out);
% 	TL.delr=TL.ranges(min(2,TL.nrange))-TL.ranges(1);
	nzrmax=max(nzr(:)); nrmax=max(nrange(:));
	atl_file=[out_root,'_atl'];
	TL.tl_coh=NaN*ones(nrmax,nzrmax,nzs,nbrg);
% 	TL.tl_inc=[];
	for jbrg=1:nbrg,
		for jzs=1:nzs,
			iir=1:nrange(jzs,jbrg); iizr=1:nzr(jzs,jbrg);
			TL.tl_coh(iir,iizr,jzs,jbrg)=TEMP(jzs,jbrg).tl_coh;
		end
	end
	str=['save ',atl_file,' TL;']; eval(str); 
end
if ~isunix & ~strcmp(computer,'PCWIN'),
	disp('Run ramp, then orca_hdf_plot: ')
    clim=[-100,-50];
	str=['[HDF]=orca_hdf_plot(',out_file,',''tl'',1,clim,''Range - km'',''Depth - m'',[],[],tsize)'];
	disp(str)
end
