function BETA=orca_beta_calc(hdf_root,iifig)
%
% Call BETA=orca_beta_calc(hdf_root,iifig);
%
%
% 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<2, iifig=0; end
%
[SD,status]=hdfsd_open([hdf_root,'.hdf']); if status<0, error('Reading SD'); end
jset_kn=find(strcmp(SD.data_name,'Re(kn)'));
iibad=0;
if isempty(jset_kn), 
	disp('USER ERROR in orca_beta_calc: HDF file does not contain Re(kn), Im(kn).'); iibad=1;
end
jset_vg=find(strncmp(SD.data_name,'Group Speed',11));
if isempty(jset_vg), 
	disp('USER ERROR in orca_beta_calc: HDF file does not contain Group Speed.'); iibad=1;
end
if iibad, error('Exiting orca_beta_calc...'); 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
[VG,status]=hdfsd_read(SD,[],jset_vg); if status<0, error('Reading VG'); end
%
[nfreq,nmode]=size(KNR.data);
f_ax=KNR.scales{1};
fa=repmat(f_ax(:),[1,nmode]);
iim=[1:nmode-1];
%
cph=2*pi*(fa./KNR.data);
vg=VG.data;
%
cphx=(.5*(cph(:,iim) + cph(:,iim+1)));
vgx=.5*(vg(:,iim) +vg(:,iim+1));
beta=-(vgx.^2./cphx.^2).*diff(cph,1,2)./diff(vg,1,2);
iibad=find(beta<-4 | beta>2); beta(iibad)=NaN;
BETA.title=KNR.title;
BETA.dlab='Beta';
BETA.ndim=2;
BETA.dims=[length(f_ax),nmode-1];
BETA.labels={'Frequency - Hz' 'Mode Number'};
BETA.scales={f_ax, [.5:nmode]};
BETA.data=beta;
BETA.iidB=0;
BETA.clim=[min(BETA.data(:)),max(BETA.data(:))];
if iifig, view5(BETA); end
