;+
; PROJECT:
;	SDAC
; NAME: 
;	FLISTOC
;
; PURPOSE:
; 	Flistoc is operator communications for flist routine which lets users
; 		display BATSE flares with user-specified parameters.
;
; CATEGORY:
;	BATSE
;
; CALLING SEQUENCE:
;	Flistoc, cmnd, listoc
;
; CALLS:
;       Remotecom, Flistcom, Flare_catalog, Checkvar, Parse_comline,
;	Setut, Getut	 
;
; INPUTS:
;       Listoc: 0/1 means don't/do print entire menu.
;
; OUTPUTS:
;       Listoc: 0/1 means don't/do print entire menu.
;	Cmnd: 3-character command entered by operator.
;
; KEYWORDS:
;  	INCOMMANDS: RAS method of allowing noninteractive access to program
;  		The syntax is: incommands=['command','param,var']
;  		If incommands(0) is '', then flist will behave interactively!
;
; PROCEDURE:
;       none
;
; MODIFICATION HISTORY:
; 	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. by EAC 4/96 to use RAS's incommands keyword for noninteractive 
;		operation
;	Mod. by RCJ 03/97. Rearrange documentation.
;       Version 5, richard.schwartz@gsfc.nasa.gov, now accepts all ANYTIM compatible time strings for
;       time parameters  START, and END.
;-
;
pro flistoc, cmnd, listoc, incommands=incommands
;
@remotecom
;
@flistcom
@flare_catalog
;
; EAC 4.96 Add in RAS's incommands keyword to allow noninteractive operation
checkvar, incommands, strarr(1)
wcommands = where(incommands ne '', ncommands)
if ncommands ge 1 then incommands=incommands(wcommands)

; first 3 characters of all commands
commands = 'LISSELEXIPRIHELRETQUITYPDEF'  ; made SOR and USE special
                                          ; to support WWW 4/96 EAC
; if SOR or USE, flistoc will write cmnd as sor,val or use,#,#-#,#..#;
; then flist will take over if incommands is set otherwise it will return
; to flist with SOR or USE for further input from user
inline = ' '
aoutprint = ['Terminal','Printer', 'File']
;
if incommands(0) ne '' then goto,read  ; you don't want a menu if your noninteractive
if listoc eq 0 then goto,prompt
list:
listoc = 0
print, ' '
print, $
 '---------------------------------------------------------------------------'
print, 'BATSE Flare List Program - Displays cataloged parameters for flares'
print, 'with user-specified parameters.'
print, $
 '---------------------------------------------------------------------------'
print, ' '
print, 'The BATSE Flare Catalog currently contains ',num_flares,' flares.'
print, ' '
print, 'Change options by entering first 3 letters of item, comma, new value.'
print, 'After setting options, enter one of the available commands.'
print, ' '
print, 'Enter SelectFlares command to retrieve flares. Default is to select all'
print, 'flares.  Select subsets of the catalog by specifying the min and max'
print, 'values for the parameters (e.g. fla,100,150 to select flares 100-150)'
print, 'and then entering the SelectFlares command.'
print, 'For items requiring a time, the time format is YY/MM/DD,HHMM:SS.XXX.'
print, 'To select all possible values for a parameter (which is the default'
print, 'setting), type first 3 letters of item, comma, ALL (e.g. pea,all).'
print, ' '
print, 'To direct output to a file (e.g., for mailing to yourself), type'
print, 'OUT,FILE before entering SelectFlares or PrintFlares command.'
print, 'PrintFlares command sends the most recent selection of flares to the'
print, 'selected output device.'
print, ' '
print, $
 '---------------------------------------------------------------------------'
print, ' '
print, format='("Source flare file      ALL/(filename)",T48,A)', $
   inpfile
print, format='("Destination flare file NONE/(filename)",T48,A)', outfile
print, format='("Start date and time    ALL/(other)",T48,A)', atime(sec_st,/hxr)
print, format='("End date and time      ALL/(other)",T48,A)', atime(sec_en,/hxr)
print, format='("Time of day            ALL/[Start,End]",T48,A,",",T65,A)', $
   strmid(atime(sod,/hxr),10,11)
print, format='("Flare number           ALL/[Min,Max]",T48,A,",",T65,A)', $
   strtrim(fn,2)
print,format='("Burst trigger number   ALL/VALID/[Min,Max]",T48,A,",",T65,A)', $
   strtrim(bn,2)
print, format='("Duration (s)           ALL/[Min,Max]",T48,A,",",T65,A)', $
   strtrim(dur,2)
print, format='("Peak rate (cnts/s)     ALL/[Min,Max]",T48,A,",",T65,A)', $
   strtrim(prate,2)
print, format='("Total counts           ALL/[Min,Max]",T48,A,",",T65,A)', $
   strtrim(totc,2)
print, format='("Outprint               Terminal/Printer/File",T48,A)', $
   aoutprint(outprint)
addrexist = size(mailaddr)
if addrexist(1) ne 0 then print, format='("Email address", T48, A)', mailaddr
print, ' '
;
prompt:
print, 'Commands: ListMenu, SelectFlares, PrintFlares, TypeFlare, '+ $
	'DefaultParams,'
print, '          SortByItem, UserSelectFlares, Help, Exit'
print, 'Change parameters or enter a command'
;
; prompt for input and read response from operator
;
on_ioerror,errorinput
read:
; enter in incommands keyword for noninteractive operation, EAC 4/96
if incommands(0) ne '' then begin
	inline = incommands(0)
        ; print,'inline at read in flistoc: ',inline
	if n_elements(incommands) gt 1 then $  ; this works by decreasing size
		incommands=incommands(1:*) else incommands=strarr(1)
endif else $
read,inline
;
; if input parameter is START or END, then recast the value as a hxrbs time!
;
inline = batse_time_input( inline, ['STA','END'])
inline = strupcase(inline)
inline = strcompress (inline, /remove_all)

; See if input is command
input = strmid(inline, 0 , 3)
cmnd = input

if input eq 'LIS' then goto,list
if strpos (commands, input) ne -1 then goto,getout ; found a command and got out
;
; parse SOR and USE (which is very special, since it can have n values after ,)
;
if input eq 'USE' then begin
  cmnd = inline ;inline is all caps and string compressed PERL script or users
  goto,getout
endif
if input eq 'SOR' then begin
   if strpos(inline,',') eq -1 then begin  ; test for just a command (interactive)
      cmnd = input	;if being run interactively
      goto,getout
   endif else begin    ; only get the first 3 letters (SOR,&&&)
      cmnd = input+','+strmid(inline,strpos(inline,',')+1,3) 
   endelse	;this string has no error checking because it is set up
  ; for use with WWW scripts which have hard wired inputs
   goto,getout
endif
; 
; Or look for parameter changes       
strng = parse_comline(inline,nvalues)
if (nvalues eq 1) or (nvalues gt 3) then goto,errorinput  ;ONLY PARAMETER NAME 
							  ;GIVEN, OR MORE THAN
							  ;TWO VALUES FOR PARA-
							  ;METERS.
strng = strng(1:*)
;
case 1 of
;
input eq 'SOU': begin
   inpfile = strng(0)
   if inpfile eq 'ALL' then goto, end_inp
   on_ioerror,done_test1
   openr,unit,/get,inpfile
   goto,done_test1_end
      done_test1:
      print,'Invalid input file name.'
      inpfile = 'ALL'
      goto, end_inp
   done_test1_end:
   close,unit
   free_lun,unit
   end_inp:
end
;
input eq 'DES':begin
   outfile = strng(0)
   if outfile eq 'NONE' then goto, end_out
   on_ioerror,done_test2
   openw,unit2,/get,outfile
   goto,done_test2_end
      done_test2:
      print,'Invalid output file name.'
      outfile = 'NONE'
      goto, end_out
   done_test2_end:
   close,unit2
   free_lun,unit2
   if strpos(outfile,'.') ne -1 then $
	spawn,'delete/noconfirm '+(findfile(outfile))(0) else $
	spawn,'delete/noconfirm '+(findfile(outfile+'.DAT'))(0)
   end_out:
end
;
input eq 'STA': begin
  if strmid(strng(0),0,3) eq 'ALL' then strng = ['91/4/19','0000']
  if nvalues eq 2 then strng = [strng,'0000']
  setut, utbase=strng(0)+','+strng(1), utstart=strng(0)+','+strng(1), $
	error = error
  if error then goto,errorinput
  getut,utstart=sec_st
end
;
input eq 'END': begin
  if strmid(strng(0),0,3) eq 'ALL' then strng = [anytim(/hxrbs,sys2ut(),/date),'0000']
  if nvalues eq 2 then strng = [strng,'0000']
  setut, utend=strng(0)+','+strng(1), error=error
  if error then goto,errorinput
  getut, utend=sec_en
end
;
input eq 'TIM' : begin
  if strmid(strng(0),0,3) eq 'ALL' then begin
     sod = [double(0.0), double(86399.)+double(.999)]
     goto, done_tim
  endif
  if nvalues eq 2 then strng = [strng,strmid(atime(sod(1),/hxr),10,11)]
  hh = double(strmid(strng,0,2))
  mm = double(strmid(strng,2,2))
  ss_xxx = double(strmid(strng,5,2))
  sod = (hh*double(3600.0))+(mm*double(60.0))+(double(ss_xxx))
  done_tim:
end
;
input eq 'FLA' : begin
   if strmid(strng(0),0,3) eq 'ALL' then begin
      fn = [min(fldata.flare_num), max(fldata.flare_num)]
      goto, done_fla
   endif
   if nvalues eq 2 then strng = [strng,string(fn(1))]
   fn = long(strng)
   done_fla:
end
;
input eq 'BUR' : begin
   if strmid(strng(0),0,3) eq 'ALL' then begin
      bn = [min(fldata.burst_num), max(fldata.burst_num)]
      goto, done_bur
   endif
   if strmid(strng(0),0,3) eq 'VAL' then begin
      bn = [1, max(fldata.burst_num)]
      goto, done_bur
   endif
   if nvalues eq 2 then strng = [strng,string(bn(1))]
   bn = long(strng)
   done_bur:
end
;
input eq 'DUR' : begin
   if strmid(strng(0),0,3) eq 'ALL' then begin
      dur = [min(fldata.duration), max(fldata.duration)]
      goto, done_dur
   endif
   if nvalues eq 2 then strng = [strng,string(dur(1))]
   dur = long(strng)
   done_dur:
end
;
input eq 'PEA' : begin
   if strmid(strng(0),0,3) eq 'ALL' then begin
      prate = [min(fldata.peak_rate), max(fldata.peak_rate)]
      goto, done_pea
   endif
   if nvalues eq 2 then strng = [strng,string(prate(1))]
   prate = float(strng)
   done_pea:
end
;
input eq 'TOT' : begin
   if strmid(strng(0),0,3) eq 'ALL' then begin
      totc = [min(fldata.total_counts), max(fldata.total_counts)]
      goto,done_tot
   endif
   if nvalues eq 2 then strng = [strng,string(totc(1))]
   totc = float(strng)
   done_tot:
end
;
input eq 'OUT': begin
  strng = strmid (strng, 0, 3)
  case 1 of
    strng(0) eq 'FIL': outprint = 2
    strng(0) eq 'PRI': outprint = 1
    strng(0) eq 'TER': outprint = 0
    else: print,'Enter ''Terminal'', ''Printer'', or ''File''.  Try again.'
  endcase
end        
;
input eq 'EMA': begin
  mailaddr = strng(0)
end
;
else: begin
  errorinput:
  print, 'Error in input -- please reenter.'
end
;
endcase
goto,read
;
getout:
;
return&end
