function count=fft_write1(FFT,cspec,nseq,jrec,jday,t0,jseq_parm,jrec_parm);
%
% Call count=fft_write1(FFT,cspec,nseq,jrec,jday,t0,jseq_parm,jrec_parm);
%
% FFT			= output structure from fft_write1_open;
% cspec(nf_out,nrec_out,nseq_out)	= 	complex spectra to output;
% nseq			= sequence number to write out (first of nseq_out sequences);
% jrec			= list of receiver slots in FFT file to output (nrec_out long);
%				  Note: FFT contains FFT.nrec receivers, and max(jrec)<=FFT.nrec
%				  [] means [1:nrec_out];
% jday,t0 		= Julian day, start time in seconds after jday (nseq_out long);
% jseq_parm		= sequence parameters to place in xh(3) (must be nseq_out long)
% jrec_parm		= receiver parameters to place in xh(4) (must be nrec_out long)
%
%
% 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.
[nf,nrec_out,nseq_out]=size(cspec);
if isempty(jrec), jrec=[1:nrec_out]; end
if nargin<7 | isempty(jseq_parm), jseq_parm=[1:nseq_out]; end
if nargin<8 | isempty(jrec_parm), jrec_parm=[1:nrec_out]; end		
%
data=zeros(1,FFT.nwords);
data(1:20)=FFT.xh;
%
if length(jseq_parm)~=nseq_out | length(t0)~=nseq_out,
	error(['USER ERROR: length of jseq_parm or t0 does not match size of cspec']);
end
if length(jrec_parm)~=nrec_out | length(jrec)~=nrec_out,
	nrec_out, jrec, jrec_parm,
	error(['USER ERROR: nrec_out does not match length of jrec or jrec_parm.']);
end
%
jseq_out=nseq;
for js=1:nseq_out,
	data(3)=jseq_parm(js);
% Time code into header:
%%	jminute=t0(js)/60;
%%	data(1)=jday*1440 + floor(jminute); data(14)=60*rem(jminute,1);
data(1)=jday*1440; data(14)=t0(js);
%
	for jr=1:nrec_out
		irec=((jseq_out-1)*FFT.nrec + (jrec(jr)-1));
		offset=irec*FFT.lenrec;
        %tms:  bug in fseek v 6.5, workaround w/ rewind
        frewind(FFT.fft_fid);
		seek_status=fseek(FFT.fft_fid,offset,'bof');
		data(4)=jrec_parm(jr);
		if seek_status<0,
% If attempting to write beyond end of file, fill in interim with zeros:
			nbyte=offset - ftell(FFT.fft_fid);
			count=fwrite(FFT.fft_fid,zeros(nbyte/4,1),'real*4');
            %tms:  bug in fseek v 6.5, workaround w/ rewind
            frewind(FFT.fft_fid);
			seek2=fseek(FFT.fft_fid,0,'eof');
		end
		if isfield(FFT,'zr'), data(19)=FFT.zr(jr); end
		data(FFT.imap(:,2))=real(cspec(:,jr,js)); 
		data(FFT.imap(:,3))=imag(cspec(:,jr,js));
		count=fwrite(FFT.fft_fid,data,'real*4');
		if count~=FFT.nwords,
			disp('Could not write full FFT. Disk full?'); 
			count, FFT.nwords, size(data), FFT.fft_fid
			keyboard;
			error
		end
	end
	jseq_out=jseq_out + 1;
end
