;+
;NAME:
;	view_image
;PURPOSE:
;	Rough method to display an image
;SAMPLE CALLING SEQUENCE:
;	.run view_image
;METHOD:
;	The program reads images from the 160k decom file or from fits
;	files or from F0 files.
;HISTORY:
;	Written 11-Mar-94 by M.Morrison (using SHOWDATA as starting point)
;	 9-Jun-94 (MDM) - Modified to handle stanford FITS files.
;	10-Jun-94 (MDM) - Set STEPPER_BREAK
;	28-Jun-94 (MDM) - Modified to work at Stanford for raw telem files
;-
;
host = strlowcase(get_host())
qstanford = strpos(host, 'stanford') ne -1

yesnox, 'Do you wish to read some images from a file? ', ans, 'Yes'
;
if (ans) then begin
    yesnox, 'Do you wish to read a raw telemetry file (other option is FITS)', qtelem, 'No'
    qdone = 0
    while not qdone do begin
	if (n_elements(last_infil) eq 0) then last_infil = ''
	print, 'For telemetry files, do not include the extension.   Some examples are:
	print, '      940311_232118'
	print, '      /md66/log/sci160k/940311_232118'
	print, '      855
	print, 'Enter MENU to display a menu of the available files/datasets:'
	input, 'Enter the filename (QUIT to abort)', infil, last_infil
	if (strupcase(infil) eq 'QUIT') then stop
	if (strupcase(infil) eq 'MENU') then begin
	    if (qtelem) then begin
		if (qstanford) then begin
		    dirs = '/home/soi/data/'
		    ff = file_list(dirs, 'tlm*', /cd, file=files)
		end else begin
		    dirs = rd_tfile('$MDI_CAL_INFO/dirs_160k.txt')
		    ff = file_list(dirs, '*_01.tfr', file=files)
		end
	    end else begin
		if (n_elements(dsds_dir) eq 0) then begin
		    spawn, 'ls -d /DSDS*/D*/*', ff
		    ss = where(strpos(ff, '.fits') eq -1)
		    ff = ff(ss)
		    break_file, ff, dsk_log, dsds_dir, filnam
		    files = strmid(filnam, 3, 99)
		end
	    end
	    ff = ff(sort(files))
	    imenu = wmenu_sel(ff, /one)
	    if (imenu eq -1) then stop
	    infil = ff(imenu)
	end
	last_infil = infil
	;
	qdone = 1
	if (qtelem) then begin
	    break_file, infil, dsklog, dir, filnam
	    if (dir eq '') and (not file_exist(infil)) then begin
		dir=getenv('MDI_SCI160K_LOG_DIR')
		print, 'Using directory: ', dir
	    end

	    fnam=concat_dir(dir, filnam)
	    ;;list=findfile(fnam+'*.tfr',count=count)
	    read_mdi, fnam, dummy, count, /nimages
	    if (count eq 0) then begin
		tbeep
		print, 'No files found for: ', fnam
		qdone = 0
	    end
	end else begin
	    break_file, infil, dsk_log, dir00, filnam
	    fnam = filnam
	    if (strlen(fnam) ge 6) then fnam = strmid(filnam, 3, 99)
	end
    end 

    read_mdi, fnam, dummy, nimg, /nimages
    print, 'Number of images available: ', nimg
    input, 'Enter the starting image to extract (starting at 0)', stimg, 0
    input, 'Enter the ending image to extract                  ', enimg, nimg-1

    n = (enimg-stimg+1)>1
    images = indgen(n)+stimg
    read_mdi, fnam, images, img, head

    image_info = strarr(n)
    for i=0,n-1 do begin
	imax=max(img(*,*,i), min=imin)
	idev=stdev(img(*,*,i), iavg)
	str = 'Image ' + string(images(i), format='(i4)') + $
		'  Min = ' + string(imin) + $
		'  Max = ' + string(imax) + $
		'  Avg = ' + string(iavg) + $
		'  Dev = ' + string(idev) 
	print, str
	image_info(i) = str
    end
end else begin
    if (n_elements(image_info) ne 0) then prstr, image_info
end

nx = n_elements(img(*,0,0))
ny = n_elements(img(0,*,0))
nimg = n_elements(img(0,0,*))
;
last=strtrim(string(nimg),2)
if (n_elements(qfirst_view_image) eq 0) then loadct,3		;only call loadct the first time
qfirst_view_image = 1

print, 'The image cube is stored in the variable "img"
print, 'The header information is stored in the variable "head"
print, 'You can run stepper any time using the command "stepper, img, xsiz=512"
print, ' '

setenv, 'STEPPER_BREAK=44'
stepper, img, image_info, xsiz=512

end
