pro readata
common pictures,a
;
;  This subroutine reads images from the 160k decom file(s).
;  It calls 'rdftr'
;

again:
dir=getenv('MDI_SCI160K_LOG_DIR')
if strmid(dir,strlen(dir),1) ne '/' then dir=dir+'/'

ans=' '
print,'Using '+dir+' as directory; change it (Y/N) ?
read,"$(1A)",ans
if (strupcase(ans) eq 'Y') then begin
  dir=' '
  print,format='($,"Enter directory to use")'
  read,"$(A)",dir
  if strmid(dir,strlen(dir),1) ne '/' then dir=dir+'/'
endif

seq=' '
print,format='($,"Enter filename (no extension)")'
read,"$(A)",seq

fnam=dir+seq
list=findfile(fnam+'*.tfr',count=count)
if count eq 0 then begin
  print,fnam+'*.tfr not found'
  goto,again
endif

imbeg=' '
imno='0'
print,'Specify image range (type 0 to read all)'
print,format='($,"   First image")'
read,imbeg
if imbeg gt 0 then begin
  imno=' '
  print,format='($,"   Number of images")'
  read,imno
endif
first=fix(imbeg)
nimg=fix(imno)

rdtfr,fnam,a,first=first,nimg=nimg

end

pro show,lo=lo,hi=hi
common pictures,a
;
;  This subroutine allows images from array a 
;  to be displayed for a "quicklook"
;

sa=size(a)
if sa(0) eq 3 then nimg=sa(3) else nimg=1
last=strtrim(string(nimg),2)
tvwindow,xsize=sa(1),ysize=sa(2),title='telemetry data display'
loadct,3

done=0
repeat begin
  repeat begin
    print,'You have '+last+' images to Your disposition'
    imno=' '
    print,format='($,"   Display which one? (1-'+last+', 0 for all, Q to quit)")'
    read,"$(1A)",imno 
    if (strupcase(imno) eq 'Q') then begin
      done=1
      goto,finish
    endif 
    imno=fix(imno) & endrep $
  until (imno le nimg)
  if imno gt 0 then begin
    mx=max(a(*,*,imno-1),min=mn) & sig=stdev(a(*,*,imno-1),mid)
    if keyword_set(lo) then low=lo else low=mn
    if keyword_set(hi) then up=hi else up=mx
    tv,bytscl(a(*,*,imno-1),low,up)
    print,'Image '+strtrim(string(imno),2)+': min='+strtrim(string(mn),2)+$
	', max='+strtrim(string(mx),2)+', mean='+strtrim(string(mid),2)+$
	', stdev='+strtrim(string(sig),2)+'; Scaled: '+strtrim(string(low),2)+$
 	' - '+strtrim(string(up),2)& endif $
  else begin
    ans=' '
    for i=0,nimg-1 do begin
      mx=max(a(*,*,i),min=mn) & sig=stdev(a(*,*,i),mid)
      if keyword_set(lo) then low=lo else low=mn
      if keyword_set(hi) then up=hi else up=mx
      tv,bytscl(a(*,*,i),low,up)
      print,'Image '+strtrim(string(i),2)+': min='+strtrim(string(mn),2)+$
	', max='+strtrim(string(mx),2)+', mean='+strtrim(string(mid),2)+$
	', stdev='+strtrim(string(sig),2)+'; Scaled: '+strtrim(string(low),2)+$
 	' - '+strtrim(string(up),2)
      print,format='($,"--> Hit <return> to continue (Q to quit)")'
      read,"$(1A)",ans
      if (strupcase(ans) eq 'Q') then begin
	done=1
	goto,finish
      endif
    endfor
  endelse
  finish: 
  endrep $
until (done gt 0)

end
