pro tr_wrt_fits, outfil, shead, img, ehead
;+
;NAME:
;	tr_wrt_fits
;PURPOSE:
;	To write a TRACE FITS file with a binary extension
;SAMPLE CALLING SEQUENCE:
;	tr_wrt_fits, outfil, shead, img, ehead
;INPUT:
;	outfil	- The name of the output file
;	shead	- The simple FITS header (string array)
;	img	- The data to be saved
;	ehead	- The binary extended header in structure format
;HISTORY:
;	Written 18-Dec-96 by M.Morrison
;-
;
prognam = 'TR_WRT_FITS
progver = 1.0
;
if (n_elements(shead) eq 0) then fxhmake, shead, img, /extend, /initialize
;
fxaddpar, shead, 'PROG_NAM', prognam
fxaddpar, shead, 'PROG_VER', progver
fxwrite, outfil, shead, img
;
if (data_type(ehead) eq 8) then begin
    nimg = 1
    fxbhmake, bheader, nimg, 'TRACE_EXT', 'Binary extension with per image header info'
    tags = tag_names(ehead)
    ;
    n = n_elements(tags)
    colarr = intarr(n)
    for i=0,n-1 do begin
	fxbaddcol, temp, bheader, ehead.(i), tags(i)
	;fxbaddcol, col_dp, bheader, dp_header, 'DP_Header'
	colarr(i) = temp
    end
    fxbcreate, lun, outfil, bheader

    icol=1
    for i=0,n-1 do begin
	fxbwrite, lun, ehead.(i), colarr(i), icol
	;fxbwrite, lun, dp_header, col_dp, icol
    end

    fxbfinish, lun
end

end
