;+
; NAME:
;      UPDATE_TERM
; PURPOSE:
;     Update the terminator database structures for use in TERM_SCORE2
; CATEGORY:
;      Yohkoh
; CALLING SEQUENCE:
;      update_term[,st_date,en_date]
; INPUTS:
;      Does not require any 
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     st_date - starting date if different from date specified in current
;               'upto' file names.
;     en_date - end date.  If not specified uses current time (!stime).
; OUTPUTS:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;      DA, written 26-Jun-1995
;-

pro update_term,st_date,en_date

;
;   Get sft and sxtf 'upto' files from correct directory
;
infil1=findfile('/0p/sxt_co/terminator/analysis/sxtf_upto*')
infil2=findfile('/0p/sxt_co/terminator/analysis/sft_upto*')

sxtf_fil=''
sxtf_fil=infil1(0)
sft_fil=''
sft_fil=infil2(0)

;
;   If st_date not specified get st_dates from file names and convert
;   to input format dd/mm/yy
;
if keyword_set(st_date) then begin
   st_date1 = st_date
   st_date2 = st_date
endif else begin
   st_date1=strmid(sxtf_fil,45,2) +'/'+strmid(sxtf_fil,43,2)+'/'+strmid(sxtf_fil,41,2)
   st_date2=strmid(sft_fil,44,2)+'/'+strmid(sft_fil,42,2)+'/'+strmid(sft_fil,40,2)
endelse

;
;   If en_date not specified use current time
;
if not keyword_set(en_date) then en_date=!stime

;
;  create sft and sxtf structures from database for
;  dates specified
;
new_sxtf_struct=get_points(st_date1,en_date,/sxtf)
new_sft_struct=get_points(st_date2,en_date,/sft0)

;
;   get old sft and sxtf structures
;
sxtf_file=strmid(sxtf_fil,0,47)
sft_file=strmid(sft_fil,0,46)
restgen,file=sxtf_file,old_sxtf_struct
restgen,file=sft_file,old_sft_struct

;
;   update structures
;
update_sxtf_struct=str_concat(old_sxtf_struct,new_sxtf_struct)
update_sft_struct=str_concat(old_sft_struct,new_sft_struct)


;
; convert en_date to file_end date, e.g. 950626
;
b1=anytim2ex(en_date)
a=''
if b1(5) lt 10 then a='0'
b2=strarr(4) 
b2(0)=b1(6) & b2(1)=a & b2(2)=b1(5) & b2(3)=b1(4)
b3=strcompress(b2(0)+b2(1)+b2(2)+b2(3),/remove_all)

;
;  replace old 'upto' files with updated versions
;
savegen,file=strmid(sxtf_fil,0,41)+b3+'.genx',update_sxtf_struct
savegen,file=strmid(sft_fil,0,40)+b3+'.genx',update_sft_struct

;
;  remove old 'upto' files
;
$rm -f sft_fil sxtf_fil
end