;+

pro copy_visible, dday=dday, visdat=visdat, wid=wid, verbose=verbose, $
                  outfil=outfil

; NAME:  copy_visible
; PURPOSE: edit Seko-san's KSC schedule file to the visible format,
;         exempting sxt_co from the boring and error-prone mk_visible.
; OPTIONAL INPUT: dday (default=5), used for getting the week ID
;                 wid (string): something like 01_35, i.e., yy_ww (year and week)
;                     to specify the week.
;                 outfil (string and scaler): to specify where the visible file 
;                          should be written (for debugging purposes).
; SIDE EFFECTS: visible file like visible_01_35.dat is written to
;               $DIR_GEN_STATUS.  If outfil is specified, the visible file
;               is written to outfil.
; HISTORY:  24-May-2001, written by NVN
;           29-Oct-2001, corrected a bug by NVN
;           16-Nov-2001, Appended '.1' to week ID to be consistent with
;              the new convention in the file names on flare1, following AT
;           17-Nov-2001, Resolved misunderstanding of AT's message.  Now 
;             take the latest file for the week of interest (NVN).               
;           29-Nov-2001, Finding a position showing 'HALCA','34m','AKEBONO','X' by JS 
; WARNING: The part that lists all the files in the order of creation time
;         depends on ssh1 to flare1 (with -l sxt).  If flare workstation 
;         managers disable ssh1 on flare1, this routine stops working.
;         Then we should as them to export the flare1:/home directory to
;         isass0/1.
;-


print,'The part that lists all the files in the order of creation time'
print,'depends on ssh1 to flare1 (with -l sxt).  If flare workstation'
print,'managers disable ssh1 on flare1, this routine stops working.'
print,'Then we should as them to export the flare1:/home directory to'
print,'isass0/1.'


if n_elements(dday) ne 1 then dday=5   ; next week = dday days from now

pwid=tim2weekid(addtime(!stime,delta=-60.*24.*8))   ; previous week
if n_elements(wid) ne 1 then wid=tim2weekid(addtime(!stime,delta=60.*24.*dday))
fwid=strmid(wid,3,2)+'_'+strmid(wid,0,2)   ; on flare1
fwid=fwid+'*'                ; allow for version number, etc. on flare1 (17-Nov-2001)
lisfil='/home/flare1/yohkoh/ksc_sched/w'+fwid
rcmd='ls -t '+lisfil  
cmd0='ssh1 flare1 -l sxt "'+rcmd+'"
spawn,cmd0,a
if n_elements(a) lt 1 then begin
   print,'No file for ',wid
   return
endif
if keyword_set(verbose) then $ 
 print,string(n_elements(a),format='(i2)')+' files found.  Take the latest one.'

infil=a(0)  ; take the latest
print,'file to be copied: '+infil
cmd='scp -p -1 sxt@flare1:"'+infil+'" dum_sched'
if keyword_set(verbose) then $ 
 print,'copy Seko-san''s file to the present working directory.'
spawn,cmd
tempfile='dum_sched'
alldat=rd_tfile(tempfile)
header=alldat(2:4)
header(1) =header(1) + ' Pass#'
alldat=alldat(5:*) + '    '
nt=n_elements(alldat)
; visdat=string(strarr(nt),format='(a72)')

; outfil='$DIR_GEN_STATUS/visible_'+wid+'.dat'
if n_elements(outfil) ne 1 then $
   outfil='/nfs_isass/ssw_isass5/yohkoh/gen/status/visible_'+wid+'.dat'

; blank lines
jb=where(strlen(alldat) lt 72, nb)
if jb(nb-1) ge nt-2 then begin   ; when the last line is blank, disregard it.
   jb=jb(0:nb-2)
   nb=nb-1
endif

; Make the first line blank if it is not
padded=0
if jb(0) ge 5 then begin
   nb=nb+1
   alldat=[arr2str(replicate(' ',80),''),alldat]
   nt=nt+1
   jb=[0,jb+1]
   padded=1
endif
print,nb,' days found.'
js=jb(0:nb-1)+1
je=jb(1:nb-1)-1 & je=[je,nt-1]


visdat=string(strarr(nt),format='(a80)')

; add by JS 29-Nov-01'
p_f0=strpos(alldat,'34m')
p_f1=strpos(alldat,'HALCA')
p_f2=strpos(alldat,'AKEBONO')
p_f3=strpos(alldat,'X')
maxp=max([p_f0,p_f1,p_f2,p_f3])
if maxp le -1 then maxp=69
; add by JS

for i=0,nb-1 do begin
   dum=alldat(js(i):je(i))
   len=strlen(dum)<79
   c3=strmid(dum,maxp,3)
   dum2=string(strarr(je(i)-js(i)+1),format='(a80)')
   for j=0,je(i)-js(i) do dum2(j)=strmid(dum(j),0,68)+  $
          arr2str(replicate(' ',12),'')
; does this day contain 34 m?
   j34=where(c3 eq '34m', n34)
   if n34 eq 0 then begin
      j1=where(c3 eq '1  ',n1)
      j2=where(c3 eq '2  ',n2)
      j3=where(c3 eq '3  ',n3)
      j4=where(c3 eq '4  ',n4)
      j5=where(c3 eq '5  ',n5)
; start=first pass
      start=-1
      if n1 eq 1 then start=j1(0)
      if n1 eq 0 and n2 eq 1 then start=j2(0)-1
      if n1 eq 0 and n2 eq 0 and n3 eq 1 then start=j3(0)-2
      if n1 eq 0 and n2 eq 0 and n3 eq 0 and n4 eq 1 then start=j4(0)-3
      if n1 eq 0 and n2 eq 0 and n3 eq 0 and n4 eq 0 and n5 eq 1 then $
                start=j5(0)-4
      if start eq -1 then goto, fini_day
      for j=0,4 do begin
        dum3=dum2(start(0)+j)
        strput,dum3,'['+string(j+1,format='(i1)')+']',69
        strput,dum3,'        ',72
        d3=c3(start(0)+j)
        if d3 eq '1  ' or d3 eq '2  ' or d3 eq '3  ' or d3 eq '4  ' or $
         d3 eq '5  ' then strput,dum3,'Visible',73
        dum2(start(0)+j)=dum3
      endfor
   endif else begin
      start=0
      for j=0,4 do begin
         dum3=dum2(start(0)+j)
         strput,dum3,'['+string(j+1,format='(i1)')+']',69
         strput,dum3,'        ',72
         d3=c3(start(0)+j)
         if strmid(d3,0,2) eq '34' then strput,dum3,'Visible',73
         dum2(start(0)+j)=dum3
      endfor
   endelse
   visdat(js(i):je(i))=dum2

fini_day: if keyword_set(verbose) then $
               print,'Day ',i+1, '  finished.' 

endfor
if padded eq 1 then visdat=visdat(1:nt-1)

hhh=strarr(16)

arch=!version.arch
lenarch=strlen(arch)
lenarch=lenarch(0)
if lenarch gt 22 then begin
   arch=strmid(arch,0,22)
   lenarch=22
endif

os=!version.os
lenos=strlen(os)
lenos=lenos(0)
if lenos gt 22 then begin
   os=strmid(os,0,22)
   lenos=22
endif

vers=!version.release
lenv=strlen(vers)
lenv=lenv(0)
if lenv gt 22 then begin
   vers=strmid(vers,0,22)
   lenv=22
endif

spawn,'hostname',a
lena=strlen(a)
lena=lena(0)
if lena gt 23 then begin
   a=strmid(a,0,23)
   lena=23
endif
spawn,'pwd',b
lenb=strlen(b)
lenb=lenb(0)
if lenb gt 23 then begin
   b=strmid(b,0,23)
   lenb=23
endif
pretime=fmt_tim(anytim2ints(!stime))
uttime=fmt_tim(addtime(anytim2ints(!stime),delta=-540.))

hhh(0)='; Visible Contacts File'
hhh(1)=';                                       '
hhh(2)=';+                                      '
hhh(3)=';  ------------------------------------ '
hhh(4)='; | User Name: sxt_co                  |'
hhh(5)='; | Host Name: '+a+arr2str(replicate(' ',40-16-lena),'')+'|'
hhh(6)='; | Directory: '+b+arr2str(replicate(' ',40-16-lenb),'')+'|'
hhh(7)='; |                                    |'
hhh(8)='; | Local Time: '+pretime+'    |' 
hhh(9)='; | UT Time   : '+uttime+'    |' 
hhh(10)='; |                                    |'
hhh(11)='; | IDL Version: '+vers+arr2str(replicate(' ',40-18-lenv),'')+'|'
hhh(12)='; | Host OS    : '+os+arr2str(replicate(' ',40-18-lenos),'')+'|'
hhh(13)='; | Host ARCH  : '+arch+arr2str(replicate(' ',40-18-lenarch),'')+'|'
hhh(14)=';  ----------------------------------- '
hhh(15)=';-                                     '

openw,1,outfil
printf,1,hhh,format='(a)'
printf,1,header,format='(a)'
printf,1,visdat,format='(a)'
close,1

cmd='/bin/rm dum_sched'
if keyword_set(verbose) then $ 
   print,'delete the temporary file copied to the present working directory.'
spawn,cmd

return

end







