;+
; PROJECT:
;	SDAC
; NAME: 
;	FLIST
;
; PURPOSE:
;	Flist allows users to display BATSE flares with user-specified 
;	parameters.	
;
; CATEGORY:
;	BATSE
;
; CALLING SEQUENCE:
;	Flist
;
; CALLS:
;	Flistcom, Flare_catalog, Hxrbs_format, batse_read_cat, Flare_latest,
;	Flare_list, Flistoc, Flare_select, Findfile, Concat_dir, 
;	Mailfile, Read_seqfile, Fselect, Yohkoh_format       
;
; INPUTS:
;       none, only through keywords.
;
; OUTPUTS:
;       Writebatse: Name of output file. It will be batse_flares.out if 
;		htmlout=0 and batse_flares.html if htmlout=1
;
; KEYWORDS:
;       INCOMMANDS: Same as RAS in fplot/fsoc program, allows user to enter 
;		commands w/out interaction.
;       	Syntax is:
;         	incommands=['commands','param,value']
;	   	specify 'exit' if you don't want to get dumped back to the 
;		interface. DO NOT set the first element of incommands to ''. 
;
;       HTMLOUT: If set, will write output to BATSE_FLARES.html, with 
;		hypertext links to appropriate gif and ps archive plots
;        	Values of HTMLOUT:     0: no link, 1: GIF and PS links
;
;	LATEST: If set, will list 10 latest flares.
;
; SIDE EFFECTS:
;	Output will be sent to BATSE_OUT, if not defined it will be
; 		sys$login for vms and $PWD for Unix (see flare_list.pro)
;		In flistoc.pro , incommands is always set, but if it is '', 
;		then it will run in interactive mode.
;
; PROCEDURE:
;       none
;
; MODIFICATION HISTORY:
;	Written by Kim Tolbert 3/91
; 	Mod. by Erika Lin 9/92 to select flares from catalog based on user-
;   		specified criteria in addition to start and end times.
; 	Mod. 1/10/94 by AKT to call hxrbs_format (for ASCII time format) and
;   		then restore original time format on exit. 
;  	Mod. by Eric Carzon 5/6/96 setup to take commands directly from a 
;		Web interface 
;   	Mod. EAC - Copy RAS's Incommands=incommands methodology to allow for
;         	a noninteractive operation, new keyword automail
;   		See also modifications to flistoc, flare_list, and mailit 
;   	Mod. EAC - 5/14/96 Add keyword htmlout for producing hypertext output
;   	Mod. EAC -5/15/96 New variable writebatse to toggle between .html 
;		and .out file extensions for the output, add vals to 
;		htmlout,see mod. to flare_list
;   		[Note that writebatse is redundantly set in both progs. 
;		to allow the seperate operation of each program]
;	Mod. by RCJ 03/97 We decided against automail for html.
;		Rearranged documentation.
;	VERSION 9, richard.schwartz@gsfc.nasa.gov, 26-aug-1997,
;	used loc_file with path_dir('batse') to locate help files.
;-
;
pro flist, writebatse, latest=latest, incommands=incommands, htmlout=htmlout
!quiet=1
;
@flistcom
;
@flare_catalog
;  
hxrbs_format, old_format=old_time_format
;
; batse_read_cat will read the catalog into common flare_cat
batse_read_cat
;
;Default values of parameters...
if not keyword_set(incommands) then incommands='' ; null string if no keyword set for incommands
if not keyword_set(htmlout) then begin
   htmlout = 0 
   writebatse='BATSE_FLARES.out'
endif else begin
   writebatse = 'BATSE_FLARES.html'
endelse
inpfile = 'ALL'
outfile = 'NONE'
sec_st = anytim('1991/4/19,0',/sec)
sec_en = sys2ut()
sod = [0.d0,86399.999d]
fn = [min(fldata.flare_num), max(fldata.flare_num)]
bn = [min(fldata.burst_num), max(fldata.burst_num)]
dur = [min(fldata.duration), max(fldata.duration)]
prate = [min(fldata.peak_rate), max(fldata.peak_rate)]
totc = [min(fldata.total_counts), max(fldata.total_counts)]
outprint = 0 ; default is output to terminal
yes_sort = 0 ; sort by times is default
set_use = 0 ; new var. for incommands, if 1 then USE is activated
;
if keyword_set(latest) then begin
   flare_latest, good
   ;print,'wflare GETENV of BATSE_OUT before',getenv('BATSE_OUT')
   flare_list, good, writebatse, /latest, htmlout=htmlout  
						;Add html output EAC 5/96
   goto,getout
endif
;
; Add Incommands keyword for WWW operation.

listoc = 1
op_com:
flistoc, cmnd, listoc, incommands=incommands

gotcommand:
case 1 of 
cmnd eq 'SEL': begin
select:
   if (n_elements(sort_item) eq 0) or (yes_sort eq 0) then sort_item='TIM' 
   if cmnd eq 'USE' or set_use eq 1 then begin
      if use_inp_good(0) lt 0 then good = -1 else $
         flare_select, good, inp_good=use_inp_good, sort_item=sort_item, $
            /use_criteria
   endif else begin
      if inpfile ne 'ALL' then begin
         openr,lu_inp,/get,inpfile
         inp_good = lonarr(num_flares)
         on_ioerror, done_read_in
         readf,lu_inp, inp_good
         done_read_in:
         close,lu_inp
         free_lun,lu_inp
         if (where(inp_good ne 0))(0) ne -1 $
      	   then inp_good = (inp_good(where(inp_good ne 0)))-1  ;index=flare_num-1
         flare_select, good, inp_good=inp_good, sort_item=sort_item,  $
            /use_criteria
      endif else begin
         flare_select, good, sort_item=sort_item,  /use_criteria
      endelse
   endelse
   yes_sort = 0
   if good(0) eq -1 then begin
      print, ' '
      print, 'No flares selected.'
      print, ' '
   endif else begin
; print,'GETENV of BATSE_OUT before call to flare_list',getenv('BATSE_OUT')
      flare_list, good, writebatse, htmlout=htmlout ;add html output EC 5/96
      if outfile ne 'NONE' then begin
         openw,lu_out,/get,outfile
         printf,lu_out,(fldata.flare_num)(good)
         close,lu_out
         free_lun,lu_out
         print, ' '
         print, 'Flare numbers of selected flares saved in the file ',outfile 
      endif
      if outprint eq 0 then goto,done_printing
      if outprint ge 1 then goto,printit
   endelse
end
;
cmnd eq 'PRI': begin
; Send output to specified device terminal or laser print.
;
   print,' '
   print, $
 'NOTE: If you haven''t issued the SelectFlares command yet, PrintFlares prints'
   print, $
 'the list of flares resulting from the last selection made on this account.'
   print,' '
printit:
   case outprint of
    0: spawn,'type/page BATSE_FLARES.out'
    1: spawn,'print/setup=port BATSE_FLARES.out'
    else: 
   endcase
done_printing:
   print,''
   if not keyword_set(incommands) then begin
     print, 'This information is saved in file ', $
		findfile(concat_dir(getenv('BATSE_OUT'),'BATSE_FLARES.out'))
       mailfile,concat_dir(getenv('BATSE_OUT'),writebatse)
   endif

end
;
cmnd eq 'TYP': begin
   type_flare
end
;
cmnd eq 'DEF' : begin
   ;Default values of parameters...
   inpfile = 'ALL'
   outfile = 'NONE'
   sec_st = anytim('1991/4/19,0',/sec)
   sec_en = sys2ut()
   sod = [0.d0,86399.999d]
   fn = [min(fldata.flare_num), max(fldata.flare_num)]
   bn = [min(fldata.burst_num), max(fldata.burst_num)]
   dur = [min(fldata.duration), max(fldata.duration)]
   prate = [min(fldata.peak_rate), max(fldata.peak_rate)]
   totc = [min(fldata.total_counts), max(fldata.total_counts)]
   outprint = 0 ; default is output to terminal
   if incommands(0) ne '' then outprint = 1; With Incommands default
					       ; must be file
end
;
; Make the cmnd SOR special if incommands is set
; flistoc will return sor,sort_item for this section to parse
strmid(cmnd,0,3) eq 'SOR' : begin
   sort_item = ''
   sort_choices = 'TIMFLABURDURPEATOTQUI'
; put in code to read sort_item non-interactively
; strupcase and strcompress were done on cmnd in flistoc
   if strpos(cmnd,',') ne -1 then begin
    sort_item=strmid(cmnd,strpos(cmnd,',')+1,3)
    goto, parse_sor_input
   endif
   read_sort_item:
   print, 'Choices are TIMes, FLAre_numbers, BURst_trigger_numbers, DURations,'
   print, '            PEAk_rates, and TOTal_counts. (Q to QUIT.)'
   read, 'Enter first three letters of item (parameter) to sort by: ',sort_item
   sort_item = strupcase(strmid(strtrim(sort_item,2),0,3))
   parse_sor_input:
   if strpos(sort_choices,sort_item) eq -1 then begin
	if incommands(0) ne '' then goto,gotcommand ;just skip if incommand
        print,sort_item+' is not a valid parameter to sort by.'
	goto,read_sort_item
   endif
   if strmid(sort_item,0,1) eq 'Q' then goto, done_sort
   yes_sort = 1
   if incommands(0) eq '' then begin
        cmnd = 'SEL'     ; only allow execution for
        goto, gotcommand ;for interactive run
   endif
   done_sort:
end
;
cmnd eq 'USE':begin
   fselect, flare_arr, count, what='flares numbers as ', $
	oper_string=oper_string
   if count eq 0 then begin
 	use_inp_good=-1 
   endif else begin
	;Check if user-inputted flare numbers are in the catalog if count ne 0.
        notincat = where (flare_arr gt n_elements(fldata), count )
        if count gt 0 then begin
           flarenums = strtrim(flare_arr(notincat),2)
           print,'Flare(s): ', flarenums (0:*), $
		 ' is/are not in the catalog.'
           incat = where (flare_arr le n_elements(fldata), count)
           if count gt 0 then flare_arr = flare_arr(incat) else flare_arr = -1
        endif
	use_inp_good=flare_arr-1  ;index=flare_num-1
        if use_inp_good(0) lt 0 then use_inp_good = -1
;	notincat = lonarr(n_elements(flare_arr))-1
;	for use_i = 0,n_elements(use_inp_good)-1 do $
;	   if n_elements(fldata)-1 lt use_inp_good(use_i) $
;	   then notincat(use_i) = use_i
;	notincatcompress = where(notincat ne -1)
;	if (notincatcompress)(0) ne -1 then begin
;	   notincat=notincat(notincatcompress)
;	   print,'Flare(s): '+strtrim(flare_arr(notincat),2)+ $
;		 ' is/are not in the catalog.'
;	   if (where(use_inp_good ne use_inp_good(notincat)))(0) eq -1 $
;		then use_inp_good=-1 else begin
;		   use_inp_good(notincat) = -1
;		   use_inp_good = use_inp_good(where(use_inp_good ne -1))
;	   endelse
;	endif
   endelse
;puts you right in the SEL case statement if interactive
     set_use = 1
    if incommands(0) eq '' then goto,select    
				; execute only if incommands is *NOT* set  
end
;
cmnd eq 'HEL': begin
   helpname  = 'help_catalog.txt'
   helpfile=loc_file(path=[curdir(), path_dir('batse'),'BATSE_DATA'],helpname) 
   if helpfile ne '' then read_seqfile, morebf, helpfile else print,'Unable to find '+helpname
   more, morebf
   mailfile, helpfile
end
;
(cmnd eq 'EXI') or (cmnd eq 'RET') or (cmnd eq 'QUI'): begin
   goto,getout
end
else: begin
   listoc = 1
   goto,op_com
end
;
endcase
;
goto,op_com
;
getout:
;
if old_time_format eq 'YOHKOH' then yohkoh_format
return&end
