function FFT=fft_write1_open(fft_root,SP,REC,ii_app);
%
% FFT=fft_write1_open(fft_root,SP,REC,ii_app);
%
% where
% fft_root		= Root of output FFT file;
% SP.imap(1:nfreq,3)/f1,f2/flims	= frequency indices or limits; 
% SP.nfreq,nfft,fs		= nfreq,nfft,fs;
% SP.pri,nT_int,T_step,iihann,nfreq
% REC.nrec/nph		= nrec
% REC.zr/z,dzr		= receiver depths;
%
%
% 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<4, ii_app=0; end
%
FFT.fft_root=fft_root;
FFT.fft_name=[fft_root,'_fft'];
if isfield(REC,'nph'), 
	nrec=REC.nph;
elseif isfield(REC,'nrec'), 
	nrec=REC.nrec; 
end
ii_exist=exist(FFT.fft_name,'file');
if ii_app,
	if ~ii_exist,
		disp('Asked to apped, but creating new file since does not exist');
		ii_app=0;
	end
elseif ii_exist,
	disp('Warning: Overwriting existing FFT file.');	
end
%
df=SP.fs/SP.nfft;
if isfield(SP,'imap');
	imap=SP.imap; nfreq=SP.nfreq;
elseif isfield(SP,'f1') & isfield(SP,'f2'),
	nf1=round(SP.f1/df) + 1; nf2=round(SP.f2/df) + 1; nfreq=nf2-nf1+1
	imap=zeros(nfreq,3);
	imap(:,1)=[nf1:nf2]';
	imap(:,2)=20 + [1:2:2*nfreq]';
	imap(:,3)=imap(:,2) + 1;
elseif isfield(SP,'flims'),
	nf1=round(SP.f_lims(1)/df) + 1; nf2=round(SP.f_lims(2)/df) + 1;
	nfreq=nf2-nf1+1; 
	imap=zeros(nfreq,3);
	imap(:,1)=[nf1:nf2]';
	imap(:,2)=20 + [1:2:2*nfreq]';
	imap(:,3)=imap(:,2) + 1;
else
	error('Input structure SP must have either imap or f1,f2 fields');
end
%
jf1=imap(1,1)-1; jf2=imap(nfreq,1)-1;
%
if ii_app,
	FFT=fft_read1_open(fft_root);
	if FFT.nfft~=SP.nfft | FFT.fs~=SP.fs | FFT.nrec~=nrec | ...
		FFT.xh(15)~=jf1 | FFT.xh(16)~=jf2,
		disp('Cannot append since FFT parms not same: ');
		FFT,SP,
		stop
	end
	return
end
%
[FFT.fft_fid,err_msg]=fopen(FFT.fft_name,'w','ieee-be');	
%
if FFT.fft_fid<0,
    disp(err_msg);
	error(['CANNOT OPEN OUTPUT FFT FILE ',FFT.fft_name, ...
		'. Name too long or disk full?']);
end
%
FFT.nfft=SP.nfft;
FFT.fs=SP.fs;
FFT.dt=1/SP.fs;
FFT.df=SP.fs/SP.nfft;
FFT.Tfft=1/FFT.df;
if isfield(SP,'pri'), FFT.pri=SP.pri; else, FFT.pri=0; end
FFT.nT_int=SP.nT_int;
FFT.nbands=1;
FFT.nrec=nrec;
if isfield(REC,'z'), 
	FFT.zr=REC.z + REC.dzr;
elseif isfield(REC,'zr'), 
	FFT.zr=REC.zr; 
end
FFT.nfreq=nfreq;
FFT.imap=imap;
FFT.nwords=20 + 2*nfreq;
FFT.lenrec=4*FFT.nwords;
FFT.nseq=0;
if isfield(SP,'iihann'), 
	FFT.iihann=SP.iihann; 
else, 
	disp('Put iihann into SP of fft_write1_open call'); FFT.iihann=0; 
end
if isfield(SP,'T_step'), 
	FFT.T_step=SP.T_step; 
else, 
	disp('Put T_step into SP of fft_write1_open call'); FFT.T_step=FFT.Tfft; 
end
%
FFT.xh=[0 FFT.nT_int 0 1 FFT.nfft ...
		FFT.fs  FFT.nbands FFT.pri FFT.iihann -FFT.T_step ...
		FFT.nrec  0  0  0  jf1  ...
		jf2  0  0  0  0];
