function [BDOM,BETA]=orca_beta_dom_run(iifig,iisave)
%
% Call [BDOM,BETA]=orca_beta_dom_run(iifig,iisave);
%
% iifig		= 1 to view BDOM (default=1);
% iisave    = 1 to save as HDF file, 2 to save as .mat file, 3 to save as both;
%
% Runs ORCA for SVP and S/R geometry set up in ORCA GUI under "ORCA CW" Program.
% Produces outputs needed for Beta calculations, computes beta for the modal
% propagation, and finds the dominant and secondary betas for each S/R combination
% specified in the ORCA GUI under the "ORCA CW" program.
%
% Set up SVP file and S/R geometry in "ORCA CW" program and then run orca_beta_dom_run.
% The beta values versus mode number are plotted first.  Examine them using the view5
% commands (view5 is automatically called) and then press return to exit.  Then you 
% will view5 the output BDOM if iifig is set to 1.  Look at BDOM.labels to see how the
% 5-D data file BDOM is organized.
%
% See also orca_nba_ri_run to view transfer-function lofars.
%
%
% 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.
if nargin<1, iifig=1; end
if nargin<2, iisave=1; end
%
orca_global
%
cback_fig=Obox.figno;
%
type_opts=[1,0,0,0,0];
gen_opts=ORC.gen_opts;
if gen_opts(4)==0, gen_opts(4)=ORC.rng_lims(1); end
iitl=0; iimf=1; iisig=0; iimt=0; iidc=1; iikn=1; iieig=0; iimf_dphi=0; iimf_im=1;
cw_opts=[iitl,iimf,iisig,iimt,iidc,iikn,iieig,0,0,0,0,0,0,iimf_dphi,iimf_im,0];
cf=adi_lim_eval(ORC.cw_freqs); ncf=length(cf);
zs=adi_lim_eval(ORC.zs_lims); nzs=length(zs);
zr=adi_lim_eval(ORC.zr_lims); nzr=length(zr);
rng_km=adi_lim_eval(ORC.rng_lims); rng=1000*rng_km; nrng=length(rng);
zmf_lims=unique([zs(:); zr(:)]);
iizs=zeros(1,nzs); iizr=zeros(1,nzr);
for jzs=1:nzs;
	[dzmin,iizs(jzs)]=min(abs(zs(jzs)-zmf_lims));
end
for jzr=1:nzr;
	[dzmin,iizr(jzr)]=min(abs(zr(jzr)-zmf_lims));
end
%
iicomm=0;
% Output OPT file:
if strcmp(ORC.out_root,'auto'), out_root=[ORC.svp_root,'_BET']; else, out_root=ORC.out_root; end
cw_freqs=sort(cf); fmin=cw_freqs(1); fmax=cw_freqs(end);
orca_opt_write(out_root,iicomm,type_opts,gen_opts,cw_freqs,cw_opts(1:16), ...
		ORC.zs_lims,ORC.zr_lims,ORC.rng_lims,zmf_lims,ORC.bb_parms,ORC.bb_opts, ...
		ORC.var_parm1,ORC.var_parm2,ORC.kpl_parms,ORC.rc_parms);
% Run orca90:
svp_file=[ORC.adi_dir,ORC.svp_root,'.svp'];
opt_file=[out_root,'_opt'];
if 1,
	stat=orca90(svp_file,opt_file,out_root);
	if stat==0, disp('ORCA did not run succesfully'); return; else, disp('ORCA finished.'); end
end
%
[SD,status]=hdfsd_open([out_root,'.hdf']); if status<0, error('Reading SD'); end
jset_kn=find(strcmp(SD.data_name,'Re(kn)'));
if isempty(jset_kn), 
	error('Unexpected in orca_nba_ri_run: HDF file does not contain Re(kn), Im(kn).'); 
end
[KNR,status]=hdfsd_read(SD,[],jset_kn); if status<0, error('Reading KNR'); end
[KNI,status]=hdfsd_read(SD,[],jset_kn+1); if status<0, error('Reading KNI'); end
jset_vg=find(strncmp(SD.data_name,'Group Speed',11));
if isempty(jset_vg), 
	error('Unexpected in orca_nba_ri_run: HDF file does not contain Group Speed.'); 
end
[VG,status]=hdfsd_read(SD,[],jset_vg); if status<0, error('Reading VG'); end
%
jset_phi_re=find(strcmp(SD.data_name,'Re(phi)'));
jset_phi_im=find(strcmp(SD.data_name,'Im(phi)'));
if isempty(jset_phi_re) | isempty(jset_phi_im), error('Re/Im(phi) not found'); end
%
[PHI_re,status]=hdfsd_read(SD,[],jset_phi_re); if status<0, error('Reading PHI_re'); end
[PHI_im,status]=hdfsd_read(SD,[],jset_phi_im); if status<0, error('Reading PHI_im'); end
%
% Compute beta:
BETA=orca_beta_calc(out_root,0);
%
out_root=[out_root,'_BDOM'];
BDOM.title=out_root;
BDOM.dlab='Beta';
%
nparm=size(KNR.data,3);
if nparm==1,
    BDOM.data=zeros(nzr,nrng,nzs,2,ncf);
    BDOM.ndim=5;
    BDOM.dims=[nzr,nrng,nzs,2,ncf];
    BDOM.labels={'Depth - m','Range - km','Source Depth - m','Prim/Sec','Frequency - Hz'};
    BDOM.scales={zr,rng_km,zs,[1:2],cw_freqs};
else
    BDOM.data=zeros(nzr,nrng,nzs,2,ncf,nparm);
    BDOM.ndim=6;
    BDOM.dims=[nzr,nrng,nzs,2,ncf,nparm];
    BDOM.labels={'Depth - m','Range - km','Source Depth - m','Prim/Sec','Frequency - Hz','Parameter'};
    BDOM.scales={zr,rng_km,zs,[1:2],cw_freqs,parms};
end
BDOM.iidB=0;
%
for jparm=1:nparm,
	for jcf=1:ncf,
        iim=find(~isnan(VG.data(jcf,:,jparm))); nmode=length(iim);
%
		phis=PHI_re.data(iizs,1:nmode,jcf,jparm);
		phir=PHI_re.data(iizr,1:nmode,jcf,jparm);
		vg=VG.data(jcf,1:nmode,jparm);
		kn=KNR.data(jcf,1:nmode,jparm) + i*KNI.data(jcf,1:nmode,jparm);
%
		disp(['Starting cf = ',int2str(jcf),' of ',int2str(ncf)]);
		BDOM.data(:,:,:,:,jcf,jparm)=orca_beta_dom(nmode,nrng,nzs,nzr,rng,phis,phir,kn, ...
			BETA.data(jcf,:));
	end
end
%
% Save to HDF file:
if iisave==1 | iisave==3,
	SDw.SD_id=0;
	[SDw,stat]=hdfsd_write_open(out_root,SDw,1,BDOM.dlab,BDOM.dims, ...
     	BDOM.scales,BDOM.labels),
	if stat<0, disp('Unsuccessful HDF open call '); return; end
	[stat]=hdfsd_write(SDw,1,[],BDOM.data);
	if stat<0, disp('Unsuccessful HDF write call '); return; end
	status=hdfsd_write_close(SDw);
	if stat<0, disp('Unsuccessful HDF close call '); return; end
end
% Save to .mat file:
if iisave==2 | iisave==3,
    str=['save ',out_root,' BDOM']; disp(str); eval(str);
end
%
if iifig, view5(BDOM); end
