function PULSE=orca_pulse_env(SD,f_lims,zs_lims,zr_lims,rng_lims,tau_lims,p_lims,BW,iinorm,tsize,iifig,iinewfig);
% Call PULSE=orca_pulse_env(SD,f_lims,zs_lims,zr_lims,rng_lims,tau_lims,p_lims,BW,iinorm,tsize,iifig,iinewfig);
%
%
% 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<9, iinorm=1; end
if nargin<10, tsize=[]; end
if nargin<11, iifig=1; end
if nargin<12, iinewfig=1; end
%
% Read in needed
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), error('Re(phi) not found'); end
%
[PHIR_re,stat]=hdfsd_read(SD,{zr_lims,[],f_lims,p_lims},jset_phi_re);
if stat~=0, error('Unsuccesful hdfsd_read of PHIR_re'); end
if ~isempty(jset_phi_im)
    [PHIR_im,stat]=hdfsd_read(SD,{zr_lims,[],f_lims,p_lims},jset_phi_im);
    if stat~=0, error('Unsuccesful hdfsd_read of PHIR_im'); end
    phir_mag=sqrt(PHIR_re.data.^2 + PHIR_im.data.^2);
else
    phir_mag=abs(PHIR_re.data);    
end
zr=PHIR_re.scales{1}; nzr=length(zr);

%[PHIS,stat]=hdfsd_read(SD,{zs_lims,[],f_lims,p_lims},jset_phi); 
%if stat~=0, error('Unsuccesful hdfsd_read of PHIS'); end
[PHIS_re,stat]=hdfsd_read(SD,{zs_lims,[],f_lims,p_lims},jset_phi_re);
if stat~=0, error('Unsuccesful hdfsd_read of PHIS_re'); end
if ~isempty(jset_phi_im)
    [PHIS_im,stat]=hdfsd_read(SD,{zs_lims,[],f_lims,p_lims},jset_phi_im);
    if stat~=0, error('Unsuccesful hdfsd_read of PHIS_im'); end
    phis_mag=sqrt(PHIS_re.data.^2 + PHIS_im.data.^2);
else
    phis_mag=abs(PHIS_re.data);    
end
zs=PHIS_re.scales{1}; nzs=length(zs);

freq=PHIR_re.scales{3}; nfreq=length(freq);
if length(PHIR_re.scales)>3,
    parms=PHIR_re.scales{4}; nparm=length(parms);
else
    parms=0; nparm=1;
end
%
jset_knr=find(strcmp(SD.data_name,'Re(kn)'));
if isempty(jset_knr), error('Re(kn) not found'); end
[KNR,stat]=hdfsd_read(SD,{f_lims,[],p_lims},jset_knr);
if stat~=0, error('Unsuccesful hdfsd_read of KNR'); end
jset_kni=find(strcmp(SD.data_name,'Im(kn)'));
if isempty(jset_kni), error('Im(kn) not found'); end
[KNI,stat]=hdfsd_read(SD,{f_lims,[],p_lims},jset_kni);
if stat~=0, error('Unsuccesful hdfsd_read of KNI'); end
jset_vg=find(strcmp(SD.data_name,'Group Speed - m/s'));
if isempty(jset_vg), error('Group Speed not found'); end
[VG,stat]=hdfsd_read(SD,{f_lims,[],p_lims},jset_vg);
if stat~=0, error('Unsuccesful hdfsd_read of VG'); end
%
rng_km=adi_lim_eval(rng_lims); nrng=length(rng_km); rng=1000*rng_km;
%
Ts_ax=spread_env_init2(tau_lims,1/BW,3);
tau=Ts_ax.xax;
ntau=Ts_ax.nx;
fac=sqrt(2*pi);
PULSE.file_name=SD.hdf_file;
PULSE.dlab='TL - dB';
%nparm,
%disp([nzr,nrng,ntau,nzs,nfreq])
if nparm==1,
    PULSE.data=zeros(nzr,nrng,ntau,nzs,nfreq);
    PULSE.ndim=5;
    PULSE.dims=[nzr,nrng,ntau,nzs,nfreq];
    PULSE.labels={'Depth - m','Range - km','Time Delay - s','Source Depth - m','Frequency - Hz'};
    PULSE.scales={zr,rng_km,tau,zs,freq};
else
    PULSE.data=zeros(nzr,nrng,ntau,nzs,nfreq,nparm);
    PULSE.ndim=6;
    PULSE.dims=[nzr,nrng,ntau,nzs,nfreq,nparm];
    PULSE.labels={'Depth - m','Range - km','Time Delay - s','Source Depth - m','Frequency - Hz','Parameter'};
    PULSE.scales={zr,rng_km,tau,zs,freq,parms};
end
PULSE.iidB=1;
if iinorm,
    PULSE.dB_max=zeros(nrng,nfreq,nparm);
end
PULSE.vg_ref=zeros(nfreq,nparm);
%
for jparm=1:nparm,
	for jf=1:nfreq,
        iim=find(~isnan(VG.data(jf,:,jparm))); nmode=length(iim);
        [im_min,jm_ref]=min(KNI.data(jf,iim,jparm)); vg_ref=VG.data(jf,iim(jm_ref),jparm),
%        [im_min,jm_ref]=max(VG.data(jf,iim,jparm)); vg_ref=VG.data(jf,iim(jm_ref),jparm),
        tau_norm=rng/vg_ref; PULSE.vg_ref(:,jparm)=vg_ref;
		for jzs=1:nzs,
			for jzr=1:nzr,
                phi_fac=fac*phis_mag(jzs,iim,jf,jparm).*phir_mag(jzr,iim,jf,jparm);
				for jrng=1:nrng,
					taux=rng(jrng)./VG.data(jf,iim,jparm) - tau_norm(jrng);
					p_mode=phi_fac.*exp(-KNI.data(jf,iim,jparm)*rng(jrng))./sqrt(KNR.data(jf,iim,jparm)*rng(jrng));
					psq_mode=max(1e-30,p_mode.^2);
					PULSE.data(jzr,jrng,:,jzs,jf,jparm)=10*log10(spread_env3(Ts_ax,taux,psq_mode)');
				end
			end
		end
        if iinorm,
            for jrng=1:nrng,
                dB_max=max(max(max(PULSE.data(:,jrng,:,jzs,jf,jparm),[],3),[],1),[],4);
                PULSE.data(:,jrng,:,:,jf,jparm)=PULSE.data(:,jrng,:,:,jf,jparm) - repmat(dB_max,[nzr,1,ntau,nzs]);
                PULSE.dB_max(jrng,jf,jparm)=dB_max;
            end
        end
	end
end
dBmax=ceil(max(PULSE.data(:))/5)*5;
PULSE.clim=[dBmax-40,dBmax];
%
if iifig,
    [dim_sort,ii]=sort([nzr,nrng,0,nzs,nfreq,nparm]); vdims=[ii(6),3,ii(5)];
    view6(PULSE,vdims,[],iinewfig,tsize);
end
