pro disp_sci160k, nxout, nyout, nout, outfil=outfil, infil=infil, qcheck=qcheck
;+
;NAME:
;	disp_sci160k
;PURPOSE:
;	To make a gif file of the last few images in the sci160k file
;INPUT PARAMETERS:
;	NXOUT - Number of columns
;	NYOUT - Number of rows
;	NOUT  - x and y output image size in pixels
;	OUTFIL - the file to write the final gif image to
;
;OPTIONAL KEYWORD INPUTS:
;
;HISTORY:
;	Written 23-Aug-96 by M.Morrison
;	16-Oct-96 (MDM) - Write out offset values
;	 8-Jan-97 (MDM) - Made into subroutine and also save last 4 images
;	30-Sep-97 (MDM) - Added X0SHIFT to display tracking
;	16-Aug-99 (CED) - Added a modicum of documentation to Mons's spaghetti.
;			Added pageback capability if infil is not specified:
;			if there are no data at all in the current .tfrx file,
;			then we pageback to the previous one, and so on up to
;			8 files back.  If there are data in the current .tfrx
;			file, but not enough, we *don't* pageback -- but in that
;			case we can at least generate something.
;-
;
;
if (n_elements(outfil) eq 0) then dev = 'x' else dev = 'z'
if (n_elements(nout) eq 0) then nout = 256

offset_file = '$MDI_CAL_INFO/offset_values.txt'

pageback = 0
if (n_elements(infil) eq 0) then begin
    infil = get_last_tfr()
    pageback = 8 ; Page back up to 8 files looking for images
end

; Scroll back through several .tfrx files if necessary...

repeat begin
    list_tfi, infil, info, /quiet
    if not isvalid(n_ago) then n_ago = 0
    if(pageback ne 0) and (data_type(info) ne 8) then begin
        n_ago = n_ago + 1
        print,'Trying ',n_ago,' .tfrx files ago...'
        infil = get_last_tfr(n_ago)
    end
end until (n_ago ge pageback) or (data_type(info) eq 8)

if (keyword_set(qcheck) and (n_elements(outfil) ne 0) ) then begin
    instat = file_info2(infil + '.tfrx', in_info)
    outstat = file_info2(outfil, out_info)
    dt = int2secarr(in_info, out_info)
    if (dt(0) lt 0) then begin
        print, '** No changes to the TFR file since the last generation'
        print, 'input file time:  ', in_info.date
        print, 'input file:  ', in_info.name
        print, 'output file time: ', out_info.date
        print, 'output file: ', out_info.name
;        spawn, 'ls -l ' + infil+'.tfrx'
;        spawn, 'ls -l ' + outfil
	return
    end
end

nout2 = nout + 30 ; margin between images
xsize = (nxout-1)*nout2 + nout
ysize = nyout*nout2

set_plot, dev
tv2, xsize, ysize, /init, /already
erase
loadct, 0

extra_str = ' '

qwritten_offset = 0
iout = nxout*nyout-1
ii = 1
n = n_elements(info)
qdone = data_type(info) ne 8

while (not qdone) do begin
    read_mdi, infil, n-ii, img, h, tfr=tfr
    dpc = sxpar(h, 'dpc')
    get_dpc_info, dpc, nx, ny, npix, sum, err, mnem
    if (dpc eq 'cfffff98'x) then extra_str = ' ** Interrupted ** '
    if (n_elements(img(0,*)) eq 1) then img = mk_dpc_image(nout, img, h=h, missing=min(img))

    if (n_elements(img(0,*)) gt 1) then begin		;found an image
	if (n_elements(img(0,*)) eq 1) then img = mk_dpc_image(nout, img, h=h)
	nx = n_elements(img(*,0))
	ny = n_elements(img(0,*))
	scale = (float(nout)/nx) < (float(nout)/ny)
	if (nx ne nout) then img = congrid(img, nx*scale, ny*scale)
	sminmax=get_mdi_scale(dpc)
	if (data_type(sminmax) eq 7) then sminmax = float(str2arr(sminmax))
	if (sminmax(0) eq sminmax(1)) then delvarx, sminmax
	if (n_elements(sminmax) ge 1) then smin = sminmax(0) else smin = min(img)
	if (n_elements(sminmax) ge 2) then smax = sminmax(1) else smax = max(img)
	;
	x0shift = 0
	y0shift = 0
	fmt_typ = mask(dpc, 12, 4)
	if ((fmt_typ eq 2) or (fmt_typ eq 3)) and (mask(dpc, 0, 12) ne 0) then begin
	    y0shift = ccd_hdr_info(tfr, 'offset')/1024 * (nout/1024.)
	    x0shift = (ccd_hdr_info(tfr, 'offset') mod 1024) * (nout/1024.)
        end
;	y0shift = ccd_hdr_info(tfr, 'offset')/1024 * (nout/1024.)
;	x0shift = (ccd_hdr_info(tfr, 'offset') mod 1024) * (nout/1024.)
	if (n_elements(img(0,*)) eq nout) then y0shift = 0
	if (n_elements(img(*,0)) eq nout) then x0shift = 0
	x0 = ((iout mod nxout)) * nout2
	y0 = (nyout-1 - (iout/nxout)) * nout2
	bimg = bytscl(img, smin, smax, top=!d.n_colors)
	tv2, bimg, x0+x0shift, y0+y0shift
	datobs = strmid(anytim(sxpar(h, 'REFTIME'),/stime),0,17)
;	xyouts2, x0+nout/2, y0+nout+15, align=0.5, sxpar(h, 'reftime'), size=1.0
	xyouts2, x0+nout/2, y0+nout+15, align=0.5, datobs, size=1.0
;	xyouts2, x0+nout/2, y0+nout+3, align=0.5, mnem + extra_str, size=0.6
	xyouts2, x0+nout/2, y0+nout+3, align=0.5, mnem + extra_str, size=0.8
	;
	;----- Log the offset location to a file
	dpc_str = ccd_hdr_info(tfr, 'dpc_str')
	dpcfmt = strmid(dpc_str, 4, 1)
	if (dpcfmt eq '2') or (dpcfmt eq '3') and (strmid(dpc_str, 5, 3) ne '000') and (not qwritten_offset) then begin
	    id = ccd_hdr_info(tfr, 'seqid')
	    get_cam_obs_info, id, cam_str
	    str = string(ccd_hdr_info(tfr, 'REFTIME_STR'), cam_str, dpc_str, ccd_hdr_info(tfr, 'offset_str'), $
			format = '(a21, 2x, a20, 2x, a, 2x, a)')
	    print, str
;	    file_append, offset_file, str, /uniq
	    qwritten_offset = 1
	end
	;
	extra_str = ' '
	iout = iout - 1
    end
    ii = ii + 1
    qdone = (ii eq -1) or (iout eq -1)
end

if (iout eq nxout*nyout) then begin
    print, 'No sci160k image files found!'
    xyouts2, !d.x_size/2, !d.y_size-50, align=0.5, size=1.5, 'Requested Time: !c' + fmt_tim(tarr)
    xyouts2, !d.x_size/2, !d.y_size-100, align=0.5, size=2.0, 'No sci160k image files found'
end

if (dev eq 'z') then begin
    file_delete, outfil
    zbuff2file, outfil, /gif
end

end
