function [LGRAM,FFT]=lgram_3d(fft_root,Tlims,jrec,jfreq,parms,iifig,clim);
%
% Call [LGRAM,FFT]=lgram_3d(fft_root,Tlims,jrec,jfreq,parms,iifig,clim);
%
% fft_root	= Root of FFT file (suffix = _fft) ([] to be prompted);
% Tlims		= FFT sequences to output ([] for all);  Column vector of sequences, or
%			  [Tmin,Tmax], where T in hod for iihr=1, or minutes into FFT file 
%			  for iihr=0, or range in km for iihr=3;
% jrec		= List of receiver numbers to output;
% jfreq		= [fmin,fmax], or column vector of frequency indices, to output;
% parms		= [dfnorm,iihr,iinorm,iidB,iipr], where:  
% 	dfnorm	= 1 to norm intensities by 1/df (uPa^2/Hz) (use 0 for simulated TL);
% 	iihr	= vertical axis units: 0 = min into FFT, 1 = HOD, 2 = range in km;
%	iinorm	= 1 to normalize each time series to unity (0 dB);
%   iidB    = 1 to convert to dB, -1 to leave complex;
%   iipr    = 1 to print progress;
% iifig		= 1 to call view5(SGRAM);
% clim		= [cmin,cmax] for colorbar.
% LGRAM.scales{2}		= receiver or beam number;
% LGRAM.scales{1}		= frequency;
% LGRAM.scales{3}		= sequence or time;
% LGRAM.data	= array(1:nfreq,1:nrec,1:nseq);
%
%
% 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, fft_root=[]; end
if nargin<2, Tlims=[]; end
if nargin<3, jrec=[]; end
if nargin<4, jfreq=[]; end
if nargin<5 | isempty(parms), parms=[1 1 0 1]; end
if nargin<6, iifig=1; end
if nargin<7, clim=[]; end
%
dfnorm=parms(1); iihr=parms(2); iinorm=parms(3);
if length(parms)<4, iidB=1; else, iidB=parms(4); end
if length(parms)<5, iipr=1; else, iipr=parms(5); end
%
fft_file=[fft_root,'_fft'];
% if exist(fft_file)~=2,
% 	disp(['Note: ',fft_file,' not found locally.']);
% 	[fft_file,path_name]=uigetfile('*_fft','Please Choose _fft File');
% 	if fft_file==0, LGRAM=[]; return; end
% 	lfft=length(fft_file); fft_root=fft_file(1:lfft-4);
% 	eval(['cd ',path_name]);
% end
%
FFT=fft_read1_open(fft_root,dfnorm);
fax=(FFT.imap(:,1) - 1) * FFT.df;
[nr,nc]=size(jfreq);
if nr==1 & nc==2,
	flo=jfreq(1); fhi=jfreq(2);
	if flo<fax(1)-FFT.df/2 | fhi>fax(FFT.nfreq)+FFT.df/2,
		disp('NOTE: flo or fhi outside of frequency band in FFT file.')
		disp(['flo = ',num2str(flo),'; fhi = ',num2str(fhi),'; FFT band = ', ...
			num2str([FFT.f1,FFT.f2])]);
	end
	jfreq=find(fax>=flo-FFT.df/2 & fax<=fhi+FFT.df/2);
elseif isempty(jfreq), 
	jfreq=[1:FFT.nfreq];
end
if isempty(jfreq),
	disp('No overlap between requested and FFT frequency band. Stopping');
	return
end
if isempty(jrec), jrec=[1:FFT.nrec]; end
%
[nr,nc]=size(Tlims);
if isempty(Tlims), 
	jseq=[1:FFT.nseq];
elseif nr==1 & nc==2,
% convert Tlims in min after start of FFt file to hod;
	if iihr==0, Tlims=mod(FFT.moy0+Tlims,1440)/60; end
	if iihr==2,
		jseq=find(FFT.range>=Tlims(1) & FFT.range<=Tlims(2));
	else	
		jseq=find(FFT.hodax>=Tlims(1) & FFT.hodax<=Tlims(2));
	end
else,
	jseq=Tlims(:)';
end
if isempty(jseq),
	disp('USER ERROR: No FFT sequences to be processed.');
	iihr,Tlims,%FFT.hodax,
	return;
end
%
nfreq=length(jfreq); nrec=length(jrec); nseq=length(jseq);
LGRAM.ndim=3; LGRAM.dims=[nfreq,nrec,nseq];
LGRAM.title=fft_root;
LGRAM.labels={'Frequency - Hz', 'Receiver', ['Time - hour of day ',int2str(FFT.jday0)]};
LGRAM.scales={(FFT.imap(jfreq,1)-1)*FFT.df, jrec, FFT.hodax(jseq)};
LGRAM.data=zeros(nfreq,nrec,nseq);
LGRAM.dfnorm=dfnorm;
%
if dfnorm,
	LGRAM.dlab='Spectral Level - dB re mPa2/Hz'; dffac=FFT.df;
else
	LGRAM.dlab='Level - dB re mPa2'; dffac=1;
end
%
lroot=length(fft_root); lr2=max(1,lroot-2);
iibf=strcmp(fft_root(lr2:lroot),'_bf');
if iihr==2,
	LGRAM.scales{3}=FFT.range(jseq);
	LGRAM.labels{3}='Range - km'
elseif iihr~=1,
	LGRAM.scales{3}=FFT.jday0*1440 + FFT.hodax(jseq)*60 - FFT.moy0;
	LGRAM.labels{3}=['Time - min'];
end
%
cspec=zeros(nfreq,nrec) + i*ones(nfreq,nrec);
for js=1:nseq,
	jseq_out=jseq(js);
	[XH,cspec(1:nfreq,1:nrec)]=fft_read1(FFT,jseq_out,jrec,jfreq);
	if js==1,
		if iibf,
			LGRAM.scales{2}=XH.recs;
			if all(LGRAM.scales{2}>=-3 & LGRAM.scales{2}<=4),
				LGRAM.labels{2}='Bearing - sin(\phi)';
			else		
				LGRAM.labels{2}='Bearing - deg';
			end
		end
	else
		iibf=0;
	end
%	LGRAM.data(:,:,js)=dBdf(cspec,dffac);
    LGRAM.data(:,:,js)=cspec;
	if iihr==1,
		LGRAM.scales{3}(js)=XH.hod;
	else iihr==0,
		LGRAM.scales{3}(js)=XH.moy+t0/60 - FFT.moy0;
	end
	if iinorm,
		Lmax=max(LGRAM.data(:,:,js),[],1);
		LGRAM.data(:,:,js)=LGRAM.data(:,:,js) - ones(nfreq,1)*Lmax;
	end
	if iipr & mod(js,10)==1, 
        disp(['Finished sequence ',int2str(js),' of ',int2str(nseq)]); pause(.001); 
    end
end
if iidB==1, 
    LGRAM.data=dBdf(LGRAM.data,dffac); 
    cmax=ceil(max(LGRAM.data(:))/5)*5;
    cmin=floor(min(LGRAM.data(:))/5)*5;
    LGRAM.iidB=1;
else,
    LGRAM.data=LGRAM.data/sqrt(dffac); 
    cmax=ceil(max(LGRAM.data(:))/5)*5;
    cmin=floor(min(LGRAM.data(:))/5)*5;
    LGRAM.iidB=-1;
end
%
if isempty(clim),
	clim=[max(cmin,cmax-60),cmax];
elseif iinorm,
	clim=[clim(1)-clim(2),0];
else
	if clim(1)>cmax | clim(2)<cmin,		% clim clearly wrong, so reset;
		clim=[max(cmin,cmax-60),cmax];
	end
end
LGRAM.clim=clim;
%
fclose(FFT.fft_fid);
%
if iifig & ~isempty(LGRAM.data), view5(LGRAM); end
