;+
;
; NAME: 
;       FS_AUTO_LOG
;
; PURPOSE:
;
; This procedure prompts the 
; operator to mark the start and end of the event and at least 1 background
; intervals; event start and end times, background interval start and end times,
; and total counts of the event above background will be calculated and stored 
; in the auto_log buffer. Prompt the operator for an event number and whether 
; to write the buffer to the BATSE flare catalog.
;
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;	fs_auto_log, flare = iflare
;
; CALLS:
;	none
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	FLARE - selected flare number
;	EXTEND - Added to expand the time interval software searches for pointing data
;		 while looking at omni events if 3x15min intervals is not long enough.
;		 Expanded to 4x60 min intervals.
; COMMON BLOCKS:
;	fscom, fs_saveaccum, fs_saveplot
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	Implemented only on sdac
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
; Shelby Kennard                                          26Feb1991
; Version 2, Separate Archive plot draw from writing fdb file!, RAS 8 aug 1996
; Version 3, richard.schwartz@gsfc.nasa.gov, 2-feb-1998, use xdevice and sps --
;           (for better fonts).
;  Version 4, eva@kano.nascom.nasa.gov, 13-May-1999.
;	Added flag EXTEND -- when set, will expand the time interval
;	that software searches through to find pointing info when event is omni 
;	flare. Search changes from original 3x15 min intervals to 4x60min intervals.
;-
pro fs_auto_log, flare = iflare, EXTEND=EXTEND
;
;
; Structures for header and data records for the BATSE flare catalog.
;
@fscom
@log_struct
@fs_saveaccum
@fs_saveplot
hxps=get_hxp()
;
auto = 0
if keyword_set(iflare) then auto=1  ; don't talk to operator
;
; Initialize variables
f_start = 0.d0
p_time = 0.d0
f_end = 0.d0
p_rate = 0.d0
b_start = [0.d0,0.d0]
b_end = [0.d0,0.d0]
b_num = 0
;
if auto then goto,flare_stuff
;
alpha_page
getinput:
on_ioerror, getinput
iflare = ' '
read, 'Enter number of flare to modify or press return for new flare: ', iflare
;
flare_stuff:
if iflare eq '' then begin
   iflare = 0
   newflare = 1
   batlog.start_secs = 0.d0
   batlog.duration = 0.
   batlog.peak_secs = 0.
   batlog.peak_rate = 0.
   batlog.total_counts = 0.
   batlog.bkg1_start_secs = 0.d0
   batlog.bkg1_duration = 0.
   batlog.bkg2_start_secs = 0.d0
   batlog.bkg2_duration = 0.
endif else begin
   newflare = 0
   read_flare, iflare, batlog, recordn = recordn, error = error
   b_num = 2
   if batlog.bkg2_start_secs eq 0.d0 then b_num=1
   if error then goto,getinput
endelse
;
if auto then goto,set_batlog
;
; Prompt operator with mini-menu with instructions for logging this event.
list = 1
menu:
log_menu,char,list
;
; Execute action specified by operator.
;
case 1 of
;
  char eq 'I' :begin
    flare_mark,f_start,f_end,abort
    if abort then goto,menu
    batlog.start_secs = f_start + getutbase(0)
    batlog.duration = f_end - f_start
    goto,menu
  end
;
  char eq 'B' :begin
    backg_mark,b_start,b_end,b_num,abort
    if abort then goto,menu
    if b_num eq 1 then begin
       batlog.bkg1_start_secs = b_start(0) + getutbase(0)
       batlog.bkg1_duration = b_end(0) - b_start(0)
    endif else begin
       batlog.bkg1_start_secs = b_start(0) + getutbase(0)
       batlog.bkg1_duration = b_end(0) - b_start(0)
       batlog.bkg2_start_secs = b_start(1) + getutbase(0)
       batlog.bkg2_duration = b_end(1) - b_start(1)
    endelse
    goto,menu
  end
;
  char eq 'Q' :begin
    goto,getout
  end
;
  char eq 'F' :begin
  end
endcase
;
; Check that flare, peak, and background have been marked. If not, print a 
; help on the log structure, notify the operator of the problem, and return
; to log_menu.
;
if (batlog.start_secs eq 0.d0) or (batlog.duration eq 0.)  or $
   (batlog.bkg1_start_secs eq 0.d0) or (batlog.bkg1_duration eq 0.) $
   then begin
   alpha_page
   print,'*************** ATTENTION ****************'
   print,'Flare or background times have not been set.'
   print,format='(////)'
   wait,3
   graphics_page
   goto,menu
endif
;
set_batlog:
f_start = batlog.start_secs
f_end = f_start + batlog.duration
b_start(0) = batlog.bkg1_start_secs
b_end(0) = b_start(0) + batlog.bkg1_duration
b_start(1) = batlog.bkg2_start_secs
b_end(1) = b_start(1) + batlog.bkg2_duration
;
; Check if operator has created a background interval that overlaps the flare
; interval.
;
if (b_start(0) lt f_start and b_end(0) gt f_start) or $
   (b_start(0) lt f_end and b_end(0) gt f_end) or $
   (b_start(1) lt f_start and b_end(1) gt f_start) or $
   (b_start(1) lt f_end and b_end(1) gt f_end) or $
   (b_start(0) gt f_start and b_end(0) lt f_end) or $
   (b_start(1) gt f_start and b_end(1) lt f_end) then begin
;
   alpha_page
   print,'*************** ATTENTION ****************'
   print,'At least one background interval overlaps the flare interval.'
   print,'Please change the background or flare intervals.'
   print,format='(////)'
   wait,5
   graphics_page
   goto,menu
endif
; 
;
; Operator has finished marking flare. 
;
; Calculate peak rates and total counts for four sunward detectors
peakr = batlog.peak_rate
totc = batlog.total_counts
p_time = batlog.peak_secs
get_prat_tcoun, f_start, f_end, b_start, b_end, b_num, $
                totc_all, peakr_all, totc, peakr, p_time
; 
batlog.peak_rate = peakr
batlog.peak_secs = p_time
batlog.total_counts = totc
;
; Call batse_write_log to display flare parameters defined, and add this flare 
; to catalog if operator chooses to.
; 
alpha_page
batse_write_log, auto, peakr_all, totc_all, batlog, iflare, recordn, saveflare
;
if saveflare ne 'Y' then goto,getout
print,format='("Flare number ",i4," added to Batse flare catalog.")',iflare
;
if auto then goto,getout
writefdb = 'N'
read, 'Create an FDB file for this file now?  Y/N? ',writefdb
writefdb = strupcase(writefdb) 
if writefdb eq 'Y' then begin
   ;
   ; Generate a FDB (flare data base) file  for this event.
   ;
   fst = f_start 
   fen = f_end
   bst = b_start 
   ben = b_end 
   ;
   write_fdb,iflare,fst,fen,bst,ben, EXTEND=EXTEND
   ;
endif else print, 'Remember to run do_fdbfiles later.'

   ; Generate and save a plot file showing the flare and background
   ;
   drawplot = 'N'
   read, 'Create archive flare plot file now?  Y/N? ',drawplot
   drawplot = strupcase(drawplot) 
if drawplot eq 'Y' then begin

      ; Before drawing plot, save users selections, then restore after 
      ; drawing plot.
      sav_ch_arr = ch_arr & sav_det_arr = det_arr & sav_sam_av = sam_av
      plot_flare, flare=iflare, /archive

      ; Now create ps file. (Didn't do this in previous step because archive
      ; file has channels separate, and we want ps files to have combined
      ; channels to save disk space.)
      noplotfile = 0  ; means do create plot file
      set_graphics, screen=oldscreen, printer=oldprinter
      set_graphics, screen='null', printer='PS'
      olddevice=!d.name & oldfont=!p.font& sps,/portrait & set_plot,xdevice(olddevice) &!p.font=oldfont
      plot_flare, flare=iflare
      olddevice=!d.name & oldfont=!p.font & sps  & device,/close & set_plot, olddevice &!p.font=oldfont
      set_graphics, screen=oldscreen, printer=oldprinter

      ; copy the ps file (fsplot.ps) to BATSE_DISK0:[batse.psplots] into a file
      ; called batse_xxx.ps, then ftp to umbra, then delete fsplot.ps
      psfile = 'batse_' + strtrim(iflare,2) + '.ps'
      spawn, 'copy/log fsplot.ps BATSE_DISK0:[batse.psplots]' + psfile
      spawn, 'delete/log fsplot.ps;*'
      ftp_batseplots,hxps

      ch_arr = sav_ch_arr & det_arr = sav_det_arr & sam_av = sav_sam_av
endif else print,'Remember to run do_archiveplots later.'
;
getout:
return&end
