pro obs_summary, sttim0, entim0, outfil=outfil, append=append, hc=hc
;
;+
;NAME:
;	obs_summary
;PURPOSE:
;	Given a short time span, read the observing log and print (a) the
;	number of datasets for each instrument, (b) the peak flux in HXT-LOW
;	and SXS2, (c) the GOES flare event level, and (d) the SXT PFI image
;	summary (#images for each filter/resolution, DP mode/rate, heliocentric 
;	location, NOAA active region #)
;SAMPLE CALLING SEQUENCE:
;	obs_summary, sttim, entim
;	obs_summary, '1-jun-93', '1-jun-93 2:00', outfil='limb_search.txt'
;	obs_summary, evn(i), anytim2ints(evn(i), off=evn(i).duration), /hc
;INPUT:
;	sttim	- The starting time to search
;	entim	- The ending time to search
;OPTIONAL KEYWORD INPUT:
;	outfil	- If set, save the output to the file name (default is to 
;		  display to the screen).  If you call with /OUTFIL then the
;		  output file name is "obs_summary.txt"
;	append	- If set, append the results to the output file if it exists
;	hc	- If set, send the results to the printer
;HISTORY:
;	Written 28-Jan-93 by M.Morrison
;	19-Aug-93 (MDM) - Modified to use /EXTRA_OUT switch on SEQ_SUMMARY
;	 1-Oct-93 (MDM) - Added /APPEND option
;			- Modified the summary line to hold the PFI location
;			  in heliocentric coordinates and the GOES event level
;	 4-Oct-93 (MDM) - Modified so that the default is to come to the screen
;			- Added /HC option
;			- Added documentation header
;-
;
sttim = anytim2ints(sttim0)
entim = anytim2ints(entim0)
ss = where(entim.day le 0)
if (ss(0) ne -1) then entim(ss).day = sttim(ss).day
;
if (keyword_set(hc)) then outfil = concat_dir(getenv('HOME'), 'obs_summary.tmp')
;
lun = -1
qprint_tit = 1
if ( keyword_set(outfil) ) then begin
    if (data_type(outfil) ne 7) then outfil = 'obs_summary.txt'

    if ( (not keyword_set(append)) or (not file_exist(outfil)) ) then begin
	openw, lun, outfil, /get_lun
    end else begin
	openu, lun, outfil, /get_lun, /append
	qprint_tit = 0
    end
end
if (qprint_tit) then begin
    printf, lun, 'OBS_SUMMARY  Ver 3.0
    printf, lun, 'Program Run: ', !stime
    printf, lun, '   '
    printf, lun, '      Date          Times          #Img  Mode    Table#    Filter A/B  Res  Exp  C DPE  msec    ImgSize  Loc   PntChange  NOAA
    printf, lun, ' '
end
;
last_day = 0
for i=0,n_elements(sttim)-1 do begin
    bcs = 0b
    sxtf = 0b
    sxtp = 0b
    w_h = 0b
    rd_obs, sttim(i), entim(i), bcs, sxtf, sxtp, w_h
    ;
    nbcs = 0	& if (get_nbytes(bcs) gt 10) then nbcs = n_elements(bcs)
    nsxtp = 0	& if (get_nbytes(sxtp) gt 10) then nsxtp = n_elements(sxtp)
    nsxtf = 0	& if (get_nbytes(sxtf) gt 10) then nsxtf = n_elements(sxtf)
    nw_h = 0	& if (get_nbytes(w_h) gt 10) then nw_h = n_elements(w_h)
    ;
    hxtl = 0	& if (nw_h gt 0) then hxtl = max(gt_sum_l(w_h))
    sxs = 0	& if (nw_h gt 0) then sxs  = max(gt_sxs2(w_h))
    ;
    helio = ' '
    if (nsxtp gt 0) then begin
	fov = gt_center(sxtp, /cmd, /angle)
	vals = (fov(0,*)+20) + (fov(1,*)+20)*40
	ii = get_most_comm(vals, uvals, h)
	helio = gt_center(sxtp(ii), /cmd, /helio, /str)
    end
    ;
    goes = ' '
    rd_gev, anytim2ints(sttim(i), off=-6*60.*60), anytim2ints(entim(i), off=6*60*60.), gev
    if (get_nbytes(gev) gt 10) then begin
	goes_peak = anytim2ints(gev, offset=gev.peak)

	;;ss_goes = sel_timrange(goes_peak, anytim2ints(sttim(i), off=-300), anytim2ints(entim(i), off=300), /bet)
	;--- find if goes peak is within the Yohkoh event (check five minutes before/after)

	ss_goes = where(tim2match(sttim, gev, entim_ref=entim, /peak) ge 0)
	if (ss_goes(0) ne -1) then begin
	    goes = string(gev(ss_goes(0)).st$class) + '*'
	end else begin
	    ss_goes = where(tim2match(sttim, gev, entim_ref=entim, /entim_dur) ge 0)
	    if (ss_goes(0) ne -1) then goes = string(gev(ss_goes(0)).st$class) + ' '
	end
    end
    ;
    day = gt_day(anytim2ints(sttim(i)))
    if (day ne last_day) then printf, lun, '----------------------------------------------------------------------------------------------------------------------------------
    fmt = "(1x, a, ' - ', a, '    BCS:', i4, '    SXTP:', i4, '    SXTF:', i4, '    W_H:', i4, " + $
		" 4x, ' HXT-L:', i5, ' SXS2:', i5, 2x, a6, 2x, a5)"
    printf, lun, fmt_tim(sttim(i)), gt_time(entim(i), /str), nbcs, nsxtp, nsxtf, nw_h, hxtl, sxs, helio, goes, format=fmt
    ;
    if (nsxtp gt 0) then begin
	seqs = seq_summary(sxtp, out2=out2, out3=out3, /noheader, /extra_out)
	ss = where(strpos(seqs,'---') eq -1)
	out = '    ' + out2(ss) + strmid(seqs(ss), 10, 100) + ' ' + out3(ss)
	ss = sort(out)	;time sort it
	out = out(ss)
	for j=0,n_elements(out)-1 do printf, lun, out(j)
    end
    flush, lun

    last_day = day
end
;
if (lun ne -1) then free_lun, lun
;
if (keyword_set(hc)) then begin
    dprint, outfil, /land
    file_delete, outfil
end
;
end
