pro pr_evn, st_date, en_date, qstop=qstop, outfile=outfile, hc=hc, indir=indir, $
		flare=flare, mindur=mindur, counts=counts, allgoes=allgoes, gro=gro
;
;+
;NAME:
;	pr_evn
;PURPOSE:
;	To list the times when there is Yohkoh Data.
;CALLING SEQUENCE:
;	pr_evn			;contacts for today
;	pr_evn, '1-jan', /flare
;	pr_evn, '1-jan', /flare, mindur=2 
;	pr_evn, '30-oct-91', '2-nov-91', /hc
;INPUT:
;	st_date	- The JST date to search for.  If no date is entered,
;		  the current date is used.  The year is not
;		  required, but if entered should be of the
;		  form 1-Oct-91 style.  The date should be
;		  entered in string style with date first.
;OPTIONAL INPUT:
;	en_date	- The ending date to list data for.  If no date is passed,
;		  it only searches for "st_date"
;OPTIONAL KEYWORD INPUT:
;	outfile	- If a filename is passed, the results will also
;		  be written to that file
;	hc	- If set, write the results to a temporary file, print
;		  it and delete it.
;	flare	- If set, only display the flare modes
;	mindur	- Only list events which have a duration longer than
;		  this value (in minutes)
;	counts	- If set, print the count rates instead of # of datasets
;	allgoes	- If set, list all goes events, even if there was not a
;		  Yohkoh event
;	gro	- If set, list the BATSE GRO events
;OUTPUT:
;	The times of the S/C start of day and start of night.
;	Also lists the station contacts during the orbit (not the times)
;	and whether there is an SAA passage.  List the FILEID also.
;HISTORY:
;	Written 1-May-92 by M.Morrison
;	 6-Jul-92 (MDM) - Added "allgoes" option
;	28-Aug-92 (MDM) - Added INFIL option
;	29-Oct-92 (MDM) - Adjusted the header to be aligned
;	 5-Jan-93 (MDM) - Corrected error of when using /ALLGOES option to
;			  print the proper DP mode when not in flare mode
;	21-Jan-93 (MDM) - Added the /GRO option
;	 6-Apr-93 (MDM/SLF) - Modified call to RD_EVN to not use the "flag"
;			  keyword - use "status" instead
;	14-May-93 (MDM) - Added INDIR option (removed INFIL option)
;	27-May-93 (MDM) - Modified to accept any time format as input and
;			  to use the hours of the input as requested
;	18-Jun-93 (MDM) - Modified to use RD_GBE instead of RD_BATSE
;-
;
qhc = 0
qout = 0
if (keyword_set(outfile)) then begin
    openw, lunout, outfile, /get_lun
    qout = 1
end
if (keyword_set(hc)) then begin
    openw, lunhc, 'pr_evn.temporary', /get_lun
    qhc = 1
end
;
if (n_elements(st_date) eq 0) then begin
    st_date = strmid(!stime, 0, 6)
end else begin
    ; If the date was provided, make sure to add a yr if none provided:
    ;;day_arr = timstr2ex(st_date)
    ;;if day_arr(6) eq 0 then day_arr(6) = fix(strmid(!stime,9,2))
    ;;dummy = fmt_tim(day_arr, day_str, time_str)
    st_date = fmt_tim(st_date)
end
;
day_arr1 = anytim2ex(st_date)
;
if (n_elements(en_date) eq 0) then begin
    ex2int, day_arr1, time, day
    time = time + 2*60.*60.*1000.		;add two hours so first orbit of next day is printed
    check_time, time, day
    int2ex, time, day+1, day_arr2		;just end 24 hours later
end else begin
    day_arr2 = anytim2ex(en_date)
end
;
qflare = keyword_set(flare)
if (n_elements(mindur) eq 0) then mindur = 0.
rd_evn, fmt_tim(day_arr1), fmt_tim(day_arr2), evn_data, status=status, indir=indir		;flag=flag, infil=infil
if (status gt 0) then begin
    message, 'SORRY - could not find input file(s)', /info
    return
end
;
if (n_elements(counts) eq 0) then counts = 0
;
tit = strarr(7)
tit(0) = 'PR_EVN.PRO Run on '+!stime
tit(1) = 'Search Start Time: '+fmt_tim(day_arr1)
tit(2) = 'Search End Time:   '+fmt_tim(day_arr2)
tit(3) = 'Minimum event duration: ' + string(mindur, format='(f8.2)') + ' minutes'
tit(4) = '     Start         End       Duration   DP         Number of Datasets
tit(5) = '      (UT)        (UT)         (min)  Mode    SXT-FFI SXT-PFI HXT/WBS  BCS
;
if (counts eq 1) then begin
    tit = [tit, strarr(4)]	;need four more lines
    tit(4) = ' '
    tit(5) = 'NOTE: "*" means Yohkoh missed the beginning or end of the flare duration
    tit(6) = '      "+" means Yohkoh has a sub portion of data within the flare duration
    tit(7) = ' '
    tit(8) = '     Start         End       Duration   DP           HXT    /         WBS                 /         BCS         / GOES
    tit(9) = '      (UT)        (UT)         (min)  Mode   Low  Med2 High /   SXS2   HXS   GRS1   RBMSD / Fe26 Fe25 Ca19  S15 /
end
for i=0,n_elements(tit)-1 do begin
    print, tit(i)
    if (qhc) then printf, lunhc, tit(i)
    if (qout) then printf, lunout, tit(i)
end
;
ihxt = tag_index(evn_data(0), 'HXT_SUM_L')		;SUM_M1, SUM_M2, SUM_H
iwbs = tag_index(evn_data(0), 'WBS_SXS1')		;SXS2, HXS, GRS1, GRS2, RBMSC, RBMSD
ibcs = tag_index(evn_data(0), 'TOTAL_CPS')		;ALL_CPS, ACC_CPS
hxt_arr = lonarr(4)
wbs_arr = lonarr(7)
bcs_arr = lonarr(4,3)
;
case counts of
    1: begin & sshxt = [0,2,3] 		& sswbs = [1,2,3,6]	& ssbcs = [0,1,2,3]		& end
    else:
endcase
nspace = 0
blank = ''
landscape = 0
;
if (counts ge 1) then begin
    landscape = 1
    rd_gev, fmt_tim(day_arr1), fmt_tim(day_arr2), goes
    goes_peak = anytim2ints(goes, offset=goes.peak)
    qgoes_out = bytarr(n_elements(goes))		;boolean to see if record has been written out
    tim2orbit, goes_peak, scday=g_scday, saa=g_saa

    if (sshxt(0) ne -1) then nspace = nspace + n_elements(sshxt)*5 + 2
    if (sswbs(0) ne -1) then nspace = nspace + n_elements(sswbs)*7 + 2
    if (ssbcs(0) ne -1) then nspace = nspace + n_elements(ssbcs)*5 + 2
    if (nspace gt 0) then blank = string(' ', format='('+strtrim(nspace-1,2)+'x,a)')
end
qall_goes = keyword_set(allgoes)		;list goes events that do not show up in Yohkoh event listing
st_event = evn_data
en_event = anytim2ints(evn_data, off=evn_data.duration)
;
if (keyword_set(gro)) then begin
    rd_gbe, fmt_tim(day_arr1), fmt_tim(day_arr2), gro_data
    gro_peak = anytim2ints(gro_data, off=gro_data.peak)
end
;
qfirst = 1
for i=0,n_elements(evn_data)-1 do begin
    evn = evn_data(i)
    for j=0,3 do hxt_arr(j) = long(evn.(j+ihxt)) ^2
    for j=0,6 do wbs_arr(j) = long(evn.(j+iwbs)) ^2
    bcs_arr(*,0) = evn.(ibcs)				;not compressed
    for j=1,2 do bcs_arr(*,j) = long(evn.(j+ibcs)) ^2

    qwrite = 1
    if (qflare and (gt_dp_mode(evn) ne 9)) then qwrite = 0
    if (evn.duration/60. lt mindur) then qwrite = 0

    if (qwrite) then begin
	end_ut = anytim2ints(evn, offset=evn.duration)

	if (counts ge 1) then begin
	    ss_goes = where( (int2secarr(goes_peak, evn) ge 0) and (int2secarr(goes_peak, end_ut) le 0))	;find if goes peak occurs
													;in middle of Yohkoh event
	    if (ss_goes(0) ne -1) then qgoes_out(ss_goes(0)) = 1	;will be writen out below - don't want it written 
									;twice if running "qall_goes
	end
	if (qall_goes and (counts ge 1)) then begin
	    temp = int2secarr(goes, evn)
	    ss = where((temp lt 0) and (not qgoes_out))
	    if (ss(0) ne -1) then begin
		for j=0,n_elements(ss)-1 do begin
		    ii = ss(j)
		    goes0 = goes(ii)
		    goes_end = anytim2ints(goes0, offset=goes0.duration)
		    str1 = fmt_tim(goes0) + '-' + gt_time(goes_end, /str) + string(goes0.duration/60., format='(f7.2)')
		    str2 = '         '	;9 characters
		    if (not g_scday(ii)) then str2 = '  NIGHT  '
		    if (g_saa(ii)) then       str2 = '  SAA    '
		    if (strlen(strtrim(str2)) eq 0) then begin
			;;sss = where( (int2secarr(evn_data, goes) ge 0) and (int2secarr(evn_data, goes_end) le 0))
			;;sss = where( (int2secarr(evn_data, goes0) ge 0) and (int2secarr(evn_data, goes_end) le 0))
			sss = where( (int2secarr(st_event, goes0) le 0) and (int2secarr(en_event, goes_end) ge 0))
			if (sss(0) ne -1) then begin	;whole flare duration is within the time span
			    str2 = gt_dp_mode(evn_data(sss(0)), /str, space=2)
			end else begin			;check if first or last half is in the time span
			    sss = where( (int2secarr(st_event, goes0) le 0) and (int2secarr(st_event, goes_end) ge 0))
			    if (sss(0) eq -1) then sss = where( (int2secarr(en_event, goes0) le 0) and (int2secarr(en_event, goes_end) ge 0))
			    if (sss(0) ne -1) then begin
				str2 = ' *' + gt_dp_mode(evn_data(sss(0)), /str)
			    end else begin		;check case where Yohkoh event is contained WITHIN flare time span
				sss = where( (int2secarr(st_event, goes0) ge 0) and (int2secarr(st_event, goes_end) le 0))
			        if (sss(0) ne -1) then str2 = ' +' + gt_dp_mode(evn_data(sss(0)), /str)
			    end
			end
		    end
		    str2 = str2 + blank + ' ' + string(goes0.st$class)
		    str = str1 + str2
		    print, str 
		    if (qhc) then printf, lunhc, str
		    if (qout) then printf, lunout, str
		end
		qgoes_out(ss) = 1	;these have been output now
	    end
	end

	str1 = fmt_tim(evn) + '-' + gt_time(end_ut, /str) + $
		string(evn.duration/60., format='(f7.2)') + gt_dp_mode(evn, /str, space=2)
	case counts of
	    0: str2 = string(evn.nsxt_ffi, evn.nsxt_pfi, evn.nmf, evn.nbcs, format='(4i7)')
	    else: begin
		str2 = ''
		if (sshxt(0) ne -1) then str2 = str2 + string(hxt_arr(sshxt), format='(100i5)') + ' /'
		if (sswbs(0) ne -1) then str2 = str2 + string(wbs_arr(sswbs), format='(100i7)') + ' /'
		if (ssbcs(0) ne -1) then str2 = str2 + string(bcs_arr(ssbcs), format='(100i5)') + ' /'

		if (ss_goes(0) ne -1) then begin
		    str2 = str2 + ' ' + string(goes(ss_goes(0)).st$class)
		    qgoes_out(ss_goes(0)) = 1	;this has been output now
		end else begin
		    str2 = str2 + ' ' + '    '
		end
	    end
	endcase

	str3 = ''
	if (keyword_set(gro)) then begin
	    ss_gro = where( (int2secarr(gro_peak, evn) ge 0) and (int2secarr(gro_peak, end_ut) le 0))	;find if goes peak occurs
													;in middle of Yohkoh event
	    if (ss_gro(0) ne -1) then begin
		gro0 = gro_data(ss_gro(0))
		str3 = string(gro0.event, gro0.cnts_peak, format='(i5, i6)')
	    end
	end

	str = str1 + str2 + str3
	print, str 
	if (qhc) then printf, lunhc, str
	if (qout) then printf, lunout, str
    end
end
;
if (qhc) then begin
    free_lun, lunhc
    dprint, 'pr_evn.temporary', /delete, landscape=landscape
end
if (qout) then free_lun, lunout
;
if (keyword_set(qstop)) then stop
end
