;+
; Project     : SDAC
;                   
; Name        : FLDISPLAY
;               
; Purpose     : This procedure finds and displays the archived BATSE flare plots.
;		Option 3 on the batse_menu, displays archived flare plots.
;               
; Category    : BATSE
;               
; Explanation : The user is given a menu of options and commands.  The command
;		is selected and an action is taken.
;               
; Use         : FLDISPLAY
;    
; Inputs      : 
;               
; Opt. Inputs : None
;               
; Outputs     : None
;
; Opt. Outputs: None
;               
; Keywords    : FLARE - plot this flare number.
;		COMBINED - sum both traces together in plot.
;		PMULTI - set !p.multi to this.
;		FIDUCIAL_MARK - if set display mark at trigger time.
;		TRIGGERX- if set indicate BATSE/MSFC burst trigger number.
;		CREATE_PLOTFILE- if set do this.
;		NOEXIT	- if set remain in program.
;
; Calls       : CHECKVAR, READ_FLARE, HXRBS_FORMAT, ALPHA_PAGE, GRAPHICS_PAGE
;		CHKLOG, READ_SEQFILE, MAILFILE, CONCAT_DIR, SELECT_DEV
;		TEK_PRINT, MAILPLOT, DEL_PLOTFILE, PARSE_COMLINE, TIME_ARC_NAME, FLSTRING_TYPE
;		FS_ARCHIVE_RW, FS_ARCHIVE_DRAW, YOHKOH_FORMAT, FSELECT
;
; Common      : Flare_catalog, Fscom, Remotecom
;               
; Restrictions: 
;               
; Side effects: None.
;               
; Prev. Hist  :
;
; Modified    : 
;   Modified 10/13/94 by AES  If a long window exists when fldisplay starts,
;    replaces it with a standard size window.  This compensates for fsplot,
;    which creates a long window for its multiple plot option. 
;   Modified 6/28/95 by AES check OS before printing filename
;   Modified 8/29/95 by AES goto start after mailing file
;	Version 4, RAS, 17-March-1997, documentation header added
; VERSION 5, richard.schwartz@gsfc.nasa.gov, 26-aug-1997,
; used loc_file with path_dir('batse') to locate help files.
; Version 6, richard.schwartz@gsfc.nasa.gov, 18-mar-1998, specify window number from current. 
; Version 7, richard.schwartz@gsfc.nasa.gov, 7-may-1998, use batse_time_input() to allow for
;	various time input formats to be translated in HXRBS format. 
;-            
;==============================================================================
;
pro fldisplay, flare=flare, combined=combined, pmulti=pmulti, $
   fiducial_mark=fid, triggerx=trigx, create_plotfile=create, $
   noexit=noexit

@flare_catalog

!quiet = 1
;
result=execute("if (!d.window ne -1) and have_windows() then " + $
               "window,!d.window, retain=2")
@fscom
@remotecom

auto = keyword_set(flare)
checkvar, noexit, (auto ne 1)

hxrbs_format, old_format=old_time_format

able = ['Disable','Enable'] ;CHANGE************!!!!!!!!!!!this line only
dable = ['Enable','Disable']
sep = ['Combined', 'Separate']
; 
; 
set_graphics
;
; Get number of flares in BATSE flare catalog
batse_read_cat
;
;
inline = ' '
checkvar, iflare, flare, 1
ch_separate = (keyword_set(combined) ne 1); 0/1 means combined/separate channels
xmulti = 1
ymulti = 1
if keyword_set(pmulti) then !p.multi = pmulti else !p.multi = [0,1,1]
noplotfile = (keyword_set(create) ne 1)
save_bangp = replicate(!p, 2)
save_bangx = replicate(!x, 2)
save_bangy = replicate(!y, 2)
data_plotted = 0
reltotrig = keyword_set(trigx)
fiducmark = keyword_set(fid)
;
start:
if auto then goto, graph
alpha_page
print, ' '
print, $
'---------------------------------------------------------------------'
print, $
 'Archive Flare Plot Display Program - draws the archived flare plots '
print, $
 'on your terminal and/or to a plot file.' 
print, $
'---------------------------------------------------------------------'
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, 'The BATSE Flare Catalog currently contains ',num_flares,' flares.'
print,' '
;
;CHANGED ALL FORMAT TABS FROM 'T40' TO 'T50' TO HAVE MORE ROOM******!!!!!!
;print,format='("Flare number ", T50,I)', iflare
print,format='("Channels         Combined/Separate", t50,a)', sep(ch_separate)
print,format='("Multiple plots", T50,i1,"x",i1)', xmulti,ymulti
;CHANGE*****************!!!!!!!!!!!!!!!!!!!below
print,format='("Triggertime Xaxis Enable/Disable",T50,A)', $
   able(reltotrig)
print,format='("Fiducial mark at triggertime Enable/Disable",T50,A)', $
   able(fiducmark)
;CHANGE*************!!!!!!!!!!!!!!above
print,format='("Create plot file Enable/Disable", T50,A)', dable(noplotfile)
addrexist = size(mailaddr)
if addrexist(1) ne 0 then print,format='("Email address", T50, A)', mailaddr
print,'  '
print, $
 'Commands: ListMenu ArchivePlot HardPlot MailPlot SelectDevices Help Exit'
print, 'Change parameters or enter a command'
;
; Prompt for input and read response from operator.
;
on_ioerror,errorinput
;on_error,3 ;CHANGE*******
read:
read,inline
inline = strupcase(inline)
inline = strcompress (inline, /remove_all)

; Process user input
process:
;
; Compare commands.
;
cmnd = strmid(inline, 0, 3)
;
if cmnd eq 'LIS' then goto,start
;
if (cmnd eq 'EXI') or (cmnd eq 'QUI') or (cmnd eq 'RET') then goto,getout
;
if cmnd eq 'HEL' then begin
   helpname  = 'help_flareplot.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
   goto,start
endif
;
if cmnd eq 'ARC' then goto,graph
;
if cmnd eq 'SEL' then begin
  select_dev
  goto,start
endif
;
if (cmnd eq 'HAR') or (cmnd eq 'LAS')then begin
  if data_plotted and not(noplotfile)  then begin
    tek_print,filename='fsplot'
  endif else begin
    if not(data_plotted) then print,'No data to plot'
    if noplotfile then print,'You didn''t create a plot file.'
    if noplotfile then print,'Set CREATE PLOT FILE to ENABLE before ', $
                             'making plot.'
  endelse   
  goto,read
endif
;
if cmnd eq 'MAI' then begin
  if data_plotted and not(noplotfile)  then begin
    mailplot, file='fsplot', /verify
  endif else begin
    if not(data_plotted) then print,'No data plotted'
    if noplotfile then print,'You didn''t create a plot file.'
    if noplotfile then print,'Set CREATE PLOT FILE to ENABLE before ', $
                             'making plot.'
  endelse   
  goto,read
end
;
if cmnd eq 'DEL' then begin
   del_plotfile
   goto,read
endif
;
param = cmnd
strt = strpos(inline, ',') + 1
case 1 of 
 param eq 'CHA': begin
    strng = strmid(inline,strt,3)
    temp = -1
    if strng eq 'COM' then temp = 0
    if strng eq 'SEP' then temp = 1
    if temp eq -1 then goto,errorinput
    ch_separate = temp
 end
;
 param eq 'MUL': begin
     s = strmid(inline, strt, 20)
     xpos = strpos(s, 'X')
     if xpos eq -1 then begin
        ix = fix(s)
        if ix ne 1 then begin
           print, 'Error - enter either:'
           print, '1 to disable multiple plots or '
           print, 'm x n for m plots across, n plots down.'
        endif else begin
           xmulti = 1
           ymulti = 1
           !p.multi = [0,1,1]
        endelse
     endif else begin
        ix = fix (strtrim(strmid(s,0,xpos),2))
        iy = fix (strtrim(strmid(s,xpos+1,20),2))
        if (ix lt 1) or (iy lt 1) or (ix gt 9) or (iy gt 9) then goto,errorinput
        xmulti = ix
        ymulti = iy
        !p.multi = [0,xmulti,ymulti]
     endelse
 end
;
;CHANGE***************!!!!!!!!!!!!!!!!!!below
 param eq 'TRI': begin
    strng = strmid(inline,strt,3)
      case 1 of 
	  strng eq 'ENA': begin
	     reltotrig = 1
	  end
	  strng eq 'DIS': begin
	     reltotrig = 0
	  end
	  else: begin
	     print,'Enter either ''enable'' or ''disable'', try again'
	  end
      endcase
 end
;
 param eq 'FID': begin
    strng = strmid(inline,strt,3)
      case 1 of
          strng eq 'ENA': begin
             fiducmark = 1
          end
          strng eq 'DIS': begin
             fiducmark = 0
          end
          else: begin
             print,'Enter either ''enable'' or ''disable'', try again'
          end
      endcase
 end
;CHANGE*************************!!!!!!!!!!!!!!!!above
;
 param eq 'CRE': begin
    strng = strmid(inline,strt,3)
    temp = -1
    if strng eq 'ENA' then temp = 0
    if strng eq 'DIS' then temp = 1
    if temp eq -1 then goto,errorinput
    noplotfile = temp
    if not noplotfile then begin
       cd,current=dir
       print, 'Plot file will be created called '
      case !version.os of
       'vms' :print, dir,'fsplot.tek or .ps for Tektronix or PostScript'
       'OSF' :print, dir,'/fsplot.tek or .ps for Tektronix or PostScript'
      endcase
    endif 
end
;
 param eq 'EMA': begin
    mailaddr = strmid (inline, strt, 100)
 end
;
 else: begin
    errorinput:
    print,'Error in input -- please reenter.'
 end
endcase
goto,read
;
graph:
if noplotfile then begin
   print, ' '
   print,'NOTE: To mail a plot to yourself or to make a hardcopy ', $
         'of this plot (via '
   print,'      HardPlot command), CREATE PLOT FILE must be ENABLED ', $
         'before graphing.'
   print, ' '
endif

if not(auto) then $
   fselect, flares, fcount, what = 'flares as ', oper_string=oper_string, $
	oc_options = 1 $
   else begin 
      flares = iflare
      fcount = n_elements(flares)
   endelse

;FOR CASE WHERE FSELECT CANNOT INTERPRET OPERATOR'S INPUT STRING...
if fcount eq 0 then begin	
   oper_string_arr = parse_comline(batse_time_input( oper_string,['INP']),nvalues)
   if nvalues eq 1 then begin
      print,'Error in input.'
      goto,read
   endif
   instring_arr = oper_string_arr(1:*)
   flstring_type,instring_arr=instring_arr,out_info,out_type=out_type,error=error
   if error then begin
      print,'Error in input.'
      goto,read
   endif
   if (out_type eq 'T') or (out_type eq 'J') then begin
	time_arc_name, out_info, filesearch, num_file
	if num_file eq 0 then goto,read
   endif
   if out_type eq 'F' then flares = out_info
   if out_type eq 'FN' then filesearch = out_info

   fcount = 1
endif 

for i=0,fcount-1  do begin
   checkvar, filesearch, ''   ;CHANGE_2****!!!!
   if filesearch eq '' then filesearch='flare_'+strtrim(flares(i),2)+'.sav'
   files = findfile (concat_dir((chklog('BATSE_FLARES'))(0),filesearch), count=count)
   filesearch = ''    ;CHANGE_2****!!!!!
;
   if count eq 0 then begin
      print,'No plot file for flare ',flares(i)
      goto,next_flare
   endif
;
   if not(noplotfile) then data_plotted = 1
   fs_archive_rw, file=files(0), /read, error=error
   if not (error) then fs_archive_draw, flare=flares(i), channels = ch_separate
next_flare:
endfor
if auto then begin
   auto = 0
   if noexit then goto, start else goto, getout
endif
goto, read
;
getout:

if old_time_format eq 'YOHKOH' then yohkoh_format

return&end
