pro KAPftp, qnode=qnode
; NAME:
;   KAPftp
; PURPOSE:
;   Maintain (through ftp) current 3 days of KAP schedule files.
;   User name and password are save in KAP_passwd file (below) for
;   security of external site.  (The read permission of KAP_passwd is 
;   restricted.  Determines current date, and whether to initiate ftp, 
;   by examining the ftp log file that it writes to upon successful
;   completion.
;
;   This routine will only work from trusted hosts.
;   As of 30-Jan-96, this was true for only flare20 and isass0.
;
; CALLING SEQUENCE:
;   KAPftp
;
; OPTIONAL OUTPUT PARAMETERS:
;   qnode	- 0 if ping fails, 1 if ping is successful.
; METHOD:
;   Calls ftp_copy twice.  The first time
;   will be to get a listing of the ftp directory and the 
;   second to copy only those files that need to be updated.  
;
; HISTORY:
;  13 MAY 98  PGS  Modified from KAPftp.pro
;-
;
local_log_file 	= concat_dir('$DIR_SDAC_KAP','KAP_ftp.list')
ftp_fil		= concat_dir('$DIR_SDAC_KAP','KAP_ftp_copy.ftp')
ftp_log		= concat_dir('$DIR_SDAC_KAP','KAP_ftp.log')
outdir		= concat_dir('$DIR_SDAC_KAP','')
date_flag       = ftp_log 
KAP_passwd_file = concat_dir('$DIR_SDAC_KAP', 'KAP_passwd') 
;
;spawn, 'chmod 777 ' + concat_dir(outdir, 'ECS*.KAP')
;
GET_UTC, date_st, /EXTERNAL
mjd_no = date2mjd(date_st.year, date_st.month, date_st.day);integer result only
mjd2date, (mjd_no + 1), year_1, mo_1, day_1 ; "~today's"     date +- 1 day (Japan)
mjd2date, (mjd_no + 2), year_2, mo_2, day_2 ; "~tomorrow's"  date +- 1 day (Japan)
mjd2date, (mjd_no    ), year_3, mo_3, day_3 ; "~yesterday's" date +- 1 day (Japan)
;
;make year_? a string of length 4
year_1s = strtrim(string(year_1),2) 
year_2s = strtrim(string(year_2),2) 
year_3s = strtrim(string(year_3),2) 
;
;make mo_? a string of length 2
mo_1s =  strtrim(string(mo_1),2) & IF (mo_1 LT 10) THEN mo_1s = '0' + mo_1s
mo_2s =  strtrim(string(mo_2),2) & IF (mo_2 LT 10) THEN mo_2s = '0' + mo_2s
mo_3s =  strtrim(string(mo_3),2) & IF (mo_3 LT 10) THEN mo_3s = '0' + mo_3s
;
;qmake day_? a string of length 2
day_1s = strtrim(string(day_1),2) & IF (day_1 LT 10) THEN day_1s = '0' + day_1s
day_2s = strtrim(string(day_2),2) & IF (day_2 LT 10) THEN day_2s = '0' + day_2s
day_3s = strtrim(string(day_3),2) & IF (day_3 LT 10) THEN day_3s = '0' + day_3s
;
;Determine Info necessary to determine which files to search for.
;
KAP_dflag = file_info2(date_flag, finfo)
IF (KAP_dflag EQ 1) THEN BEGIN
   long_st_date = anytim2cal(finfo.date, form = 8)
   flag_year = strmid(long_st_date, 0, 4)
   flag_mo   = strmid(long_st_date, 4, 2)
   flag_day  = strmid(long_st_date, 6, 2)
   IF ((flag_year NE year_1s) OR (flag_mo NE mo_1s) OR (flag_day NE day_1s)) THEN BEGIN
      print, 'THE PRESENT KAP FILES ARE NO LONGER CURRENT: DELETING OLD FILES.'
      print, 'INITIATING FTP SESSION TO CAPTURE CURRENT FILES.'
      ;REMOVE PRIOR ECS*.KAP FILES: risk being without any ECS*.KAP files if ftp doesn't go through.
;      spawn, 'chmod 755 ' + file_list('$DIR_SDAC_KAP','ECS*.KAP')
      file_delete, file_list('$DIR_SDAC_KAP','ECS*.KAP')
   ENDIF ELSE BEGIN
      print, ' '
      print, 'THE KAP FILES SHOULD STILL BE CURRENT.  ABORTING FTP SESSION.'
      print, ' '
      return
   ENDELSE
ENDIF ELSE BEGIN
;print, 'KAP_date_flag missing: Continuing.  After successful ftp, new KAP_date_flag will be created.'
file_delete, file_list('$DIR_SDAC_KAP','ECS*.KAP')
ENDELSE
;
ifile1 = ('*ECS'+year_1s+mo_1s+day_1s) + '*' + '.KAP'
ifile2 = ('*ECS'+year_2s+mo_2s+day_2s) + '*' + '.KAP'
ifile3 = ('*ECS'+year_3s+mo_3s+day_3s) + '*' + '.KAP'
ifiles_t = [ifile1, ifile2, ifile3]
inode  = 'umbra.nascom.nasa.gov'
subdir   = '/service/soho-arch03/home/soho/public/data/summary/asplanned'
;
str0 = arr2str(replicate('-',80),'')
str1 = string('KAPftp started at: ',fmt_tim(!stime))
print,str1 & file_append, ftp_log, [str0,str1,str0]
;
restore, KAP_passwd_file
t0 = systime(1)				; Get the starting time
noftp = 0
ftp_copy, ifiles_t, inode, KAP_user_name, KAP_passwd, /get, /list,  $
                subdir=subdir, /ascii, status=status,		    $
		ftp_fil=ftp_fil, ftp_log=ftp_log,	            $
		dirlist=dirlist, ftp_result=ftp_result,   	    $
		dirname=dirname, outfil=outfil,		            $
		outdir=outdir, noftp=noftp,         	            $
		qnode=qnode,	                                    $
		logappend=logappend, /rev_time_dir

;------------------------------------------------------
; Now compare the lists
; -----------------------------------------------------

print,'Comparing the lists now ... '
new_files_1 = wc_where(dirlist,ifile1,/case_ignore)
new_files_2 = wc_where(dirlist,ifile2,/case_ignore)
new_files_3 = wc_where(dirlist,ifile3,/case_ignore)
t_ifiles = [dirlist(new_files_1), dirlist(new_files_2), dirlist(new_files_3)]
ifiles = strextract(t_ifiles, 'ECS', '.KAP', /include)
; -----------------------------------------------------
; Spawn the ftp command 
; -----------------------------------------------------

;
ftp_copy, ifiles, inode, KAP_user_name, KAP_passwd, /get,       $
                subdir=subdir, /ascii, status=status,           $
                ftp_fil=ftp_fil, ftp_log=ftp_log,               $
                dirlist=dirlist, ftp_result=ftp_result,         $
                dirname=dirname, outfil=outfil,                 $
                outdir=outdir, noftp=noftp,                     $
                qnode=qnode,                                    $
                /logappend
;
file_append, ftp_log, string('Status from ftp_copy_new =',status)
str_stat = string('KAP FTP copy job took ',(systime(1)-t0)/60.,' mins.',format='(a,f5.1,a)')
print,str_stat
file_append, ftp_log, str_stat
if qnode eq 0 then begin
  str_stat = 'Ping to node: '+inode+' failed'
  print,str_stat
  file_append, ftp_log, str_stat
endif
;
;-------------------------------------------
; Final housekeeping -- delete the list file
;-------------------------------------------
file_delete, dirname		; I don't know why it (dirname) is called this
file_delete, local_log_file     ; pgs
spawn, 'chmod 775 ' + concat_dir(outdir, 'ECS*.KAP')
end
