PRO go_nvs4, increment=increment, display=display, initFrm=initFrm, $
        nvsDirfn = nvsDirfn
;   --------------------------------------------------------------
;   --------------------------------------------------------------

bell = string(7b)
print,bell+'* go_nvs4 is obselete.  Please use go_nvs5 instead'+bell
input,'* Do you want to continue anyway?'+bell, ans, 'N'
ans = strmid(strupcase(ans),0,1)
if ans ne 'Y' then return

movie_dir = '/ys/gen/soft/rel/movie'        ; HARD-CODED
print,'*** Changing current directory to: ',movie_dir
cd,movie_dir,current=save_dir

IF (KEYWORD_SET(increment) eq 0) THEN increment = 10

IF (KEYWORD_SET(display) ne 0) THEN BEGIN
;  tvdisplay = 1    ;display xray image in record fmt (mk_nvs_movie)
   tvdisplay = 0
ENDIF ELSE BEGIN
  if n_elements(tvdisplay) eq 0 then tvdisplay = 0  ; Don't display on tty
  display = 0       ;Don't display  each image
ENDELSE

IF (KEYWORD_SET(nvsDirfn) eq 0) THEN BEGIN
  fileout = '~/nvs0'            ; go to user default directory
  input,'* Enter the output root filename', fileout, fileout
  nvsDirfn = fileout            ;default for now
ENDIF ELSE BEGIN
  fileout = nvsDirfn            ;pass to old var. name
endelse


if n_elements(setup) eq 0 then setup = 'Y' else begin
  input,'* Set up the filenames and comment fields?', setup, setup
  setup = strupcase(strmid(setup,0,1))
endelse

if setup eq 'Y' then begin

input,'* Use default sfd files for input data?',ans, 'Y'
ans = strmid(strupcase(ans),0,1)
IF (ans ne 'Y') THEN BEGIN
  fil_path = ''
  input,'* Enter path and file prefix:', filein, ''
ENDIF ELSE BEGIN
  fil_path  = '/yd1/sfd'
  filein = 'sfd*'
  filein2= 'sfw*'
ENDELSE
fil_path2 = fil_path
filein  = file_list(fil_path,filein,file=file)
if filein(0) eq '' then goto,dir_save           ; No files found
if n_elements(filein) gt 1 then begin
  abcd = wmenu_sel(file)
  if abcd(0) eq -1 then begin
    print,'No files selected ... '
    goto,dir_save           ; No files selected
  endif
  filein = filein(abcd(0))
endif 

rd_roadmap, filein, roadmap, ndset, filidx=filidx
info1 = get_info2(roadmap,/noninter)

input,'* Display a 2nd image set?', ans0, 'Y'
ans0 = strupcase(strmid(ans0,0,1))
if ans0 eq 'Y' then begin
  if ans ne 'Y' then input,'* Enter path and file prefix:', filein2, ''

  filein2  = file_list(fil_path2,filein2,file=file2)
  if filein2(0) eq '' then goto,dir_save        ; No files found
  if n_elements(filein2) gt 1 then begin
    abcd = wmenu_sel(file2)
    if abcd(0) eq -1 then begin
    print,'No files selected ... '
    goto,dir_save                   ; No files selected
    endif
    filein2 = filein2(abcd(0))

    rd_roadmap, filein2, roadmap2, ndset2, filidx=filidx2
    info2 = get_info2(roadmap2,/noninter)
  endif                     ; n_elements(filein2) gt 1

;  Get the closest times:
  v0 = int2secarr( roadmap, roadmap(0) )
  v1 = int2secarr( roadmap2, roadmap(0) )   ; Use roadmap 0 as reference
  close = mk_close_vec(v0,v1)           ; Get the closest times
  q2_flag = 1
endif else q2_flag = 0          ; Only one image set

if q2_flag eq 1 then begin
  input,'* Decompress the 2nd image?', ans1, 'Y'
  ans1 = strupcase(strmid(ans1,0,1))
  if ans1 eq 'Y' then q_decomp = 1 else q_decomp = 0
  
  if n_elements( q_expand) eq 0 then q_expand = 'N'
  input,'* Clip and expand white light (2nd) image', ans2, 'N'
  ans2 = strupcase(strmid(ans2,0,1))
  if ans2 eq 'Y' then q_expand = 1 else q_expand = 0
endif

Comment0 = 'SXT Movie'
break_file, filein, disk_log, dir, filnam, ext
Comment1 = filnam+ext
input,'* Enter first comment line (10 char max)',comment0, comment0
input,'* Enter second comment line (15 char max)',comment1, comment1

setup = 'N'                 ; Already setup
endif                       ; setup eq 'Y'

ndset = fix(total(ndset))
print,'There are',ndset,' images in the file'
if increment eq 0 then begin
  print,' *** Error in go_nvs4:  increment must be > 0'
  goto,dir_save 
endif
print, 'These images will processed ',increment, ' images at a time.'

if n_elements(i1) eq 0 then i1 = 0
if n_elements(i2) eq 0 then i2 = ndset-1

input,'* Enter the first image to process', i1, i1
input,'* Enter the last  image to process', i2, i2


; -----------  Start the Main Loop  ----------------------
mloops = (i2-i1+1)/increment
rem = (i2-i1+1) mod increment
IF rem ne 0 then mloops = mloops + 1
ni1 = i1
ni2 = i1 + increment -1
if ni2 gt i2 then ni2 = i2

;----- INIT NVS DEVICE and POSITION LASER DISK to 1st FRAME ------
IF (keyword_set(initfrm) eq 0) THEN BEGIN
  not_finished = 1
  while not_finished do begin
    spawn, 'nvsstat', nvs_status        ; Get the NVS status
    cur_position = ''
    for i=0,n_elements(nvs_status)-1 do                 $
    if strpos(nvs_status(i),'Current Tape Position') gt -1 then     $
    cur_position = nvs_status(i)
    if strlen(cur_position) gt 0 then begin
      cur_position = strtrim(strmid(cur_position,0,strpos(cur_position,':')),2)
      ij = strlen(cur_position)
      frame_num = fix(strmid(cur_position,ij-2,2))
      num_sec   = fix(strmid(cur_position,ij-4,2))
      num_min   = fix(strmid(cur_position,ij-6,2))
      frame_num = frame_num + 1
      if frame_num gt 29 then begin
    num_sec = num_sec + 1
    frame_num = frame_num - 30
      endif
      if num_sec gt 59 then begin
    num_min = num_min + 1
    num_sec = num_sec - 60
      endif
      initfrm = long(string(num_min,num_sec,frame_num,format='(3i2.2)'))
      print,'First Frame to Record = ',num_min,':',num_sec,':',frame_num, $
        '('+strtrim(initfrm,2)+')',format='(3(a,i2.2),5x,a)'
      input,'* Enter C to continue or anything to quit', ans3, 'C'
      ans3 = strmid(strupcase(ans3),0,1)
      if ans3 ne 'C' then not_finished = 2 else not_finished = 0
    endif else begin
      print,'**** Error in go_nvs4 ****'
      print,'     Could not get the NVS status using nvsstat'
      print,'     Check the hardware'
      input,'* Enter C to continue or anything to quit', ans3, ''
      ans3 = strmid(strupcase(ans3),0,1)
      if ans3 ne 'C' then not_finished = 2
    endelse

    if not_finished eq 2 then begin
        print,'*** Changing current directory to: ',save_dir
        cd,save_dir
    return
    endif
  endwhile
endif 

initcmd = 'nvsinit '+string(initfrm, format='(i8.8)')
print,'initcmd =',initcmd       ;***
SPAWN, initcmd

rd_sda, filein, ni1, index_temp ;get str template

FOR jj = 0, mloops-1  DO BEGIN  ;main loop
 bufcnt = 0             ;init buff count
 dataM = BYTARR(512,512, ni2-ni1+1) ;create load buffers
 data2M= BYTARR(512,512, ni2-ni1+1)
 indexM= REPLICATE(index_temp, ni2-ni1+1)
 for i=ni1, ni2 do begin
    rd_sda, filein, i, index, data, roadmap
    siz_d = size(data)          ;get the dimensions of data
    IF ( (siz_d(1) ne 512) or (siz_d(2) ne 512) ) THEN BEGIN    ;ck dimen
      data = mk512(data(*,*))       ;change dim. to 512x512
    ENDIF
    data=bytscl(data,max=255*5./6.,min=0)


;   If a 2nd image is to be displayed, get the closest time:
    if q2_flag then begin
      j = close(i)
      rd_sda, filein2, j, index2, data2, roadmap2
      if q_decomp then data2 = bytscl(sxt_decomp( data2 ))
      data2size = size(data2)
      if (data2size(1) ne 512) then data2 = rebin(data2,512,512,/sample)
      if q_expand then begin
    ds2 = roadmap2(j).shape_cmd
    Total_size = 34.        ; Size of final image in arc-min
    nn = Total_size / 42. * ds2(0)
    ix = fix((ds2(0)-nn)/2)
        iy = fix(ix + nn -1 )
        data2 = data2(ix:iy,ix:iy)
    data2 = congrid(data2,130,130)
      endif
      print,' -- Working on the following pair --'
      print,' ',info1(i)
      print,' ',info2(j)
    endif

    dataM(0,0,bufcnt) = data(*,*)       ; collect data
    IF (N_Elements(data2) ne 0) then data2M(0,0,bufcnt)= data2(*,*)
    indexM(bufcnt) = index
    bufcnt = bufcnt + 1             ;inc bufcnt
  endfor            ;end data collection i loop

  IF (display) THEN BEGIN   ;display and review
    info_array = get_info2(indexM)  ;convert to string info
    window, 3, xs=512, ys=512, retain=2
    stepper, dataM, info=info_array

    ans = 'cr to keep all'
    defans = ans
    input, '* List frames to be REMOVED (ex. 4,7,9) ', ans, ans
    IF (ans eq defans) THEN BEGIN       ;default ans (keep all)
      wdelete,3
      count = -1
      totimg = n_elements(indexM)
    ENDIF ELSE BEGIN
      blist = STR2arr(ans, delimit=',')
      blist = fix(blist)
      idx = indgen(n_elements(indexM))  ;list of indices
      idx(blist) = -1
      glist = WHERE(idx ne -1, count)
      IF count gt 0 then begin
        dataM = dataM(*,*,glist)        ;reduce 
        IF (N_Elements(data2) ne 0) then data2M = data2M(*,*,glist)
        indexM = indexM(glist)
        totimg = n_elements(indexM)
      ENDIF 
      wdelete,3
    ENDELSE
  ENDIF ELSE BEGIN          ;no display
    count = -1
    totimg = n_elements(indexM)
  ENDELSE

  IF (count ne 0) THEN BEGIN
    icnt = ni1
    mk_nvs_movie,icnt,dataM,indexM,Comment0,Comment1=comment1,fileout=fileout, $
            color_tab1=color_tab1,tvdisplay=tvdisplay,  $
            data2=data2M,display=q2_flag,color_tab2=color_tab2
    startcop = icnt
    icnt = icnt + totimg 
    endcop = icnt-1
;
;   Execute "nvs" script for files "startcop" thur "endcop"

    print, 'execute nvsftp for files: ', startcop, ' thur ', endcop
    ftpcmd = 'nvsftp '+nvsDirfn+' '+string(startcop,format='(i3.3)')+' '+ $
    string(endcop, format='(i3.3)')
    SPAWN, ftpcmd

;   REMOVE ALL FTP-COPIED FILES
    print, 'Deleteing all nvs0.*.raw files-- '
    spawn,'rm -f '+ nvsDirfn +'*.raw'   ;del all copied files

  ENDIF
  ni1 = ni2 + 1         ; Set up for next time through the loop
  ni2 = ni1 + increment -1
  if ni2 gt i2 then ni2 = i2

ENDFOR              ;jj loop

dir_save:
print,'*** Changing current directory to: ',save_dir
cd,save_dir

end