pro mwlt_tape2jpeg,start,stop,device=dev,dir=dir,qstruc=qstruc,qmovie=qmovie

if n_elements(dev) eq 0 then dev='/dev/nrst4'
if n_elements(dir) eq 0 then dir='.'
if n_elements(start) gt 0 then begin
   start = anytim2ints(start)
   if n_elements(stop) gt 0 then stop = anytim2ints(stop) $
      else stop = anytim2ints(start,off=24.0*3600.0)
endif else begin
   start = anytim2ints('31-dec-78')   ; zero-point of Yohkoh int time format
   stop = anytim2ints('31-dec-49')    ; highest possible date in int format
endelse

print,' '
print,'Important: if the program stops due to an end-of-file error in rfits,'
print,'           just type'
print,'                        IDL> free_lun,unit'
print,'                        IDL> return,0'
print,'           and the program will exit gracefully.'
print,' '
print,'     Note: if the program stops due to an I/O error,'
print,'           or you suspect that the end-of-tape has not been reached,'
print,'           use'
print,'                        IDL> free_lun,unit'
print,'                        IDL> return,bytarr(2)'
print,'           The program will continue and try to skip the bad file.'
print,' '

repeat begin

   im = rfits(dev,time_obs=time,date_obs=date,head=hd)

   if n_elements(date) ne 0 then begin   ; normal case

      ; fix for the very early data, where the date and time parameters
      ; weren't fully FITS conformant (causing rfits to miss a digit)
      if strlen(date) eq 7 then if strmid(hd(5),9,1) eq ' ' $
         then date = strmid(hd(5),11,8) else date = strmid(hd(5),10,8)
      if strlen(time) eq 7 then if strmid(hd(6),9,1) eq ' ' $
         then time = strmid(hd(6),11,8) else time = strmid(hd(6),10,8)
      ; end of fix

      tt = fitim2ints(time,date)
   endif else tt = anytim2ints('31-dec-78')   ; only necessary after read error
                                              ; in rfits at beginning of tape

   if int2secarr(tt,start) ge 0.0 and int2secarr(tt,stop) lt 0.0 $
      and n_elements(im) gt 1 and max(im) ge 64 then begin
      im = rotate(im,5)                  ; flip in x -> N up, W right

      fil = '/mwj' + strmid(date,6,2) + strmid(date,3,2) + strmid(date,0,2) $
             + '.' + strmid(time,0,2) + strmid(time,3,2) + '.jpg'
      write_jpeg,dir+fil,im,quality=95

      mwlt_analyze,im,qstruc,qmovie,time_obs=time,date_obs=date

      save,qstruc,qmovie,file=dir+'/temp_log.sav'
   endif

   spawn,'mt -f '+dev+' fsf 1'

endrep until int2secarr(tt,stop) ge 0.0 or n_elements(im) le 1

fil = '/log' + strmid(date,6,2) + strmid(date,3,2) + strmid(date,0,2) $
          + '.' + strmid(time,0,2) + strmid(time,3,2) + '.sav'
save,qstruc,qmovie,file=dir+fil
spawn,'rm '+dir+'/temp_log.sav'

return
end