pro tr_wrt_fits_i1, outfil, dp_header, img0, img, iimg, nimg, $
		percentd, apid, pkt_time, mprognam, mprogver, $
		offset_time=offset_time, norotation=norotation, $
		start_extend=start_extend
;+
;NAME:
;	tr_wrt_fits_i1
;PURPOSE:
;	Write a single image to a FITS file per hour for TRACE
;SAMPLE CALLING SEQUENCE:
;	tr_wrt_fits_i1, outfil, dp_header, img0, img, iimg, nimg, $
;		percentd, apid, pkt_time, $
;		offset_time=offset_time
;INPUT:
;	outfil
;	dp_header
;	img0	- raw (compressed) image.  It is overwritten by
;		  img if it is compressed
;	img	- Decompressed image (0b if img0 is already 
;		  decompressed
;	iimg	- The image number in the file, starts at 0
;	nimg	- The number of images to go into that file
;	apid	- The APID where the image came from
;	pkt_time- The packet time (seconds past 24-May-68)
;	percentd- The amount of data present
;OPTIONAL KEYWORD INPUT:
;	offset_time-The number of seconds to subtract from pkt_time to
;		  get the "real" GMT Time (used during ground
;		  tests)
;	norotation- If set, don't set the "rot_out" value based
;		   on the rotation.
;OPTIONAL KEYWORD OUTPUT:
;	start_extend-The byte offset to the start of the extended header
;HISTORY OF IMG_VER:
;1.0	- "mk_trace_i0"
;2.0	- "mk_trace_i1" initial value
;2.01	- Added APID and PKT_TIME to the binary fits extension
;	- Changed "n_words" to "n_bytes"
;	- Changed "comp_style" to "comp_code"
;2.10	- Changed to have "start_byte" be from the beginning of
;	  the file (not the beginning of data section)
;3.0	- First version of TR_WRT_FITS_I1
;3.1	- Added rotation
;
;HISTORY:
;V1.00	Written 1-Dec-97 by M.Morrison (guts of MK_TRACE_I1)
;V1.01	 3-Dec-97 (MDM) - Added MPROGNAM and MPROGVER
;V1.02	 3-Dec-97 (MDM)	- STDEV protection
;V1.03	 4-Dec-97 (MDM) - Adjusted QCOMP logic (handle memory dumps)
;V1.04	 4-Dec-97 (MDM) - Corrected OFFSET_TIME usage
;V1.05	28-Jan-98 (MDM) - Modified to make the .dp_header tag to NOT be
;			  hardwired to 505 elements long.  It is defined
;			  by the input.  This handles the non-image
;			  data product types.
;			- progver was still set to 1.03 (note)
;V1.06	10-Feb-98 (MDM) - Modified to look at the data type when writing
;			  it out (because jitter data is byte type)
;V1.10	11-Mar-98 (MDM) - Added "rot_out" for rotation to put the image
;			  up properly for solar north up
;			- Reversed nx_out/ny_out because of the rotation
;			- Added /NOROTATION
;V1.11	17-Mar-98 (MDM) - Corrected V1.06 mod (didn't work for JPEG properly)
;			- Made /NOROTATION not flip the NX/NY
;V1.12	13-Jul-98 (MDM) - Added start_extend output
;-
;
common tr_wrt_fits_i1_blk1a, lun1, header, nbyte_head, start_byte, index
;
if (n_elements(offset_time) eq 0) then offset_time = 0
;
imgver = 3.10
progver = 1.12
;
if (iimg eq 0) then begin	;first image
    junk = bytarr(10)
    fxhmake, header, junk, /extend, /initialize
    fxaddpar, header, 'EXT_NROW', nimg
    fxaddpar, header, 'IMG_VER', imgver
    fxaddpar, header, 'MPROGNAM', mprognam
    fxaddpar, header, 'MPROGVER', mprogver
    fxaddpar, header, 'PROG_NAM', 'TR_WRT_FITS_I1', 'Make a FITS file per hour'
    fxaddpar, header, 'PROG_VER', progver
    wrt_fits_bin_exten, wrt_fits_ver, /get_ver
    fxaddpar, header, 'BEXTEN_V', wrt_fits_ver
    fxaddpar, header, 'PROG_RUN', !stime
    fxwrite, outfil, header, junk
    ;
    nbyte_head = get_nbytes(byte(header))
    ;
    fxbhmake, bheader, nimg, 'TRACE_EXT', 'Binary extension with per image header info'
    ;
    ndp_header = n_elements(dp_header)
    ;
    index0 = {n_bytes: 0L, nx_out: 0L, ny_out: 0L, rot_out: 0, $ ;--- Required Standard tags for new convention
	data_type: 0L, start_byte: 0L, comp_code: 0L, $
	img_min: 0., img_max: 0., img_avg: 0., $		;--- Optional Standard tags for new convention
	img_dev: 0., img_percentd: 0., $
	apid: 0L, pkt_time: 0L, typ: 0, $			;--- TRACE raw image specific
	dp_header: intarr(ndp_header), $
	time: 0L, day: 0}
    index = replicate(index0, nimg)
    ;
    openw, lun1, outfil, /get_lun
    ss = where(header eq '', nss)
    if (nss ne 0) then header(ss) = string(' ', format='(a80)')
    writeu, lun1, byte(header)              ;write out the header - it'll have to be re-written
    ;
    start_byte = nbyte_head			;0L
end
;
;--- Per Image write-out
;
    writeu, lun1, img0

    index(iimg).n_bytes		= get_nbytes(img0)
    index(iimg).start_byte	= start_byte
    start_byte = start_byte + get_nbytes(img0)

    ;---- Data portion of index
    ;
    if (dp_header(2) ge 0) then begin		;image data
        amp = gtt_mnem(dp_header, 'ikcmamp', /nostring)	;0=A, 1=B
        rot_arr = [6, 4]			;rotate(img,6) for amp A, and 4 for amp B
        if (keyword_set(norotation)) then rot_arr = [0,0]
	rot_out = rot_arr(amp)
    end else begin
	rot_out = 0	;no rotation
    end
    ;
    qcomp = (n_elements(img0(0,*)) eq 1) and (keyword_set(img))
    if (qcomp) then img0 = img		;copy decompressed image over raw image
    nx = n_elements(img0(*,0))
    ny = n_elements(img0(0,*))
    if (rot_out ne 0) then begin		;reverse nx/ny for image rotation
	nx = n_elements(img0(0,*))
	ny = n_elements(img0(*,0))
    end
    index(iimg).data_type	= data_type(img0)
    index(iimg).nx_out 		= nx
    index(iimg).ny_out 		= ny
    index(iimg).rot_out		= rot_out
    index(iimg).comp_code 	= long(qcomp)

    imin = min(img0, max=imax)
    if (n_elements(img0) eq 1) then begin
	idev = 0.
	iavg = img0(0)
    end else begin
	idev = stdev(img0, iavg)
    end
    index(iimg).img_min		= float(imin)
    index(iimg).img_max		= float(imax)
    index(iimg).img_avg		= iavg
    index(iimg).img_dev		= idev
    index(iimg).img_percentd	= float(percentd)
    index(iimg).apid		= apid
    index(iimg).pkt_time	= pkt_time

    index(iimg).dp_header	= dp_header
    index(iimg).typ		= dp_header(2)

    daytim = anytim2ints('24-May-68', off=double(pkt_time - offset_time))
    index(iimg).time		= daytim.time
    index(iimg).day		= daytim.day
;
if (iimg eq nimg-1) then begin		;last image written out
    ;---- Pad out the data section to 2880 boundary
    npad = 2880 - (start_byte mod 2880)
    if (npad ne 2880) then writeu, lun1, bytarr(npad)
    point_lun, -1*lun1, start_extend
    ;
    ;---- Update the header
    ;
    fxaddpar, header, 'NAXIS1', start_byte - nbyte_head
    point_lun, lun1, 0
    writeu, lun1, byte(header)	;overwrite the header
    ;
    free_lun, lun1
    ;
    ;---- Write out the index as a binary extension header
    ;
    wrt_fits_bin_exten, index, outfil, /append
    print, '**************** Finished making: ' + outfil + ' ***************************'
end

end
