pro tvmov,img,log=log,sqrt=sqrt,dispmin=dispmin,dispmax=dispmax,title=title,bw=bw

IF (n_params(0) LT 1) THEN BEGIN
   print,'   Usage: tvmov,im_cube,[/log,/sqrt],dispmin=dispmin,dispmax=dispmax,title=title,/bw'
   print,'         '
   print,'          where im_cube is a cube of images with the last argument'
   print,'          the image index. Displays sequence of images.'
   print,'          /log  will do log scaling'
   print,'          /sqrt will do sqrt scaling'
   print,'          /bw will do loadct,0, otherwise use color table 3'
   print,'          dispmin,dispmax set TV bytscaling for linear display only.'
   print,'          title is an alternative string array used to label images.'
   goto,last
endif

set_plot,'X'
if keyword_set(bw) then loadct,0
sz=size(img)
maxdim=max([sz(1),sz(2)])
factor=512./maxdim
xs=fix(factor*sz(1))
ys=fix(factor*sz(2))
window,0,xs=xs,ys=ys
if (sz(0) eq 3) then nims=sz(3) else nims=1
if keyword_set(title) then begin           ; make sure the title array is a
   tsz=size(title)                         ; string array of the correct size
   if (tsz[2] ne 7 or tsz[1] ne nims) then begin
      print,'Title array not correct: using replacement.'
      title=strarr(nims)
      for j=0,nims-1 do begin
         m1=max([max(img[*,*,j]),-min(img[*,*,j])])
         if (m1 ne max(img(*,*,j))) then m1=-m1
         title[j]='Seq '+strtrim(string(j),2)+': Max = '+string(m1)
      end
   endif
endif else begin 
   title=strarr(nims)
   if (nims eq 1) then begin
      m1=max([max(img),-min(img)])
      if (m1 ne max(img)) then m1=-m1
      title[0]='Max = '+string(m1)
   endif else begin
      for j=0,nims-1 do begin
         m1=max([max(img[*,*,j]),-min(img[*,*,j])])
         if (m1 ne max(img(*,*,j))) then m1=-m1
         title[j]='Seq '+strtrim(string(j),2)+': Max = '+string(m1)
      end
   endelse 
endelse

if (sz(0) eq 2) then begin                   ; single image
   j=0
   if (keyword_set(log)) then begin         ; case log display
                                            ; limit dynamic range to 200
     a=alog(img>(max(img/200.)))
     b=congrid(a,xs,ys,cubic=-0.5)
     tvscl,b
     xyouts,xs/2,ys-20,title[j],alignment=0.5,color=255,device=1
   endif else if (keyword_set(sqrt)) then begin
     a=sqrt(img>0.)
     b=congrid(a,xs,ys,cubic=-0.5)
     tvscl,b
     xyouts,xs/2,ys-20,title[j],alignment=0.5,color=255,device=1
   endif else begin
     b=congrid(img,xs,ys,cubic=-0.5)
     if not keyword_set(dispmin) then dmin=min(b) else dmin=dispmin
     if not keyword_set(dispmax) then dmax=max(b) else dmax=dispmax
     tv,!d.table_size*bytscl(b,min=dmin,max=dmax)/256.
     xyouts,xs/2,ys-20,title[j],alignment=0.5,color=255,device=1
  endelse
  goto,last
endif 

for j=0,nims-1 do begin                     ; do each image in sequence
   if (keyword_set(log)) then begin         ; case log display
                                            ; limit dynamic range to 200
     a=alog(img(*,*,j)>(max(img(*,*,j)/200.)))
     b=congrid(a,xs,ys,cubic=-0.5)
     tvscl,b
     xyouts,xs/2,ys-20,title[j],alignment=0.5,color=255,device=1,charsize=2
   endif else if (keyword_set(sqrt)) then begin
     a=sqrt(img(*,*,j)>0.)
     b=congrid(a,xs,ys,cubic=-0.5)
     tvscl,b
     xyouts,xs/2,ys-20,title[j],alignment=0.5,color=255,device=1,charsize=2
   endif else begin
     b=congrid(img(*,*,j),xs,ys,cubic=-0.5)
     if not keyword_set(dispmin) then dmin=min(b) else dmin=dispmin
     if not keyword_set(dispmax) then dmax=max(b) else dmax=dispmax
     tv,!d.table_size*bytscl(b,min=dmin,max=dmax)/256.
     xyouts,xs/2,ys-20,title[j],alignment=0.5,color=255,device=1,charsize=2
  endelse
endfor

last:
return
end
