pro write_log, auto, peakrates, totcounts, batlog, flarenum, recordn, saveflare

@log_hdr_struct
@fs_saveplot
@flare_catalog
@fs_saveaccum

common catalog_file, cunit  ; needed for auto mode since unit was opened already

if auto then begin
   saveflare = 'Y'
   goto,write_flare
endif

print, ' '
print, format='("start time",t30, a)', atime(batlog.start_secs,/hxr)
print, format='("peak time", t30, a)', atime(batlog.peak_secs,/hxr)
print, format='("duration (s)",  t30, a)', batlog.duration
print, format='("prate (c/s)", t30, a)', batlog.peak_rate
print, format='("total counts", t30, a)', batlog.total_counts
print, format='("1start time (bkgd)", t30, a)',$ 
   atime(batlog.bkg1_start_secs,/hxr)
print, format='("1duration (bkgd) (s)", t30, a)', batlog.bkg1_duration
print, format='("2start time (bkgd)", t30, a)', $
   atime(batlog.bkg2_start_secs,/hxr)
print, format='("2duration (bkgd) (s)", t30, a)', batlog.bkg2_duration
print, ' '
;
print, format='("Detector", T15, "Eff. Area", T28, "Peak Rate",'+$
              'T40, "Total Counts")'
for i=0,7 do begin
   print, format='(i5, t14, f9.2, t23, i, t40, i)', det_sorted(i), $
          2000.*cos_sorted(i), long(peakrates(i)), long(totcounts(i))
endfor
print, ' '
;
saveflare = 'N'
read,'Write this information to catalog, Y/N? ',saveflare
saveflare = strupcase(saveflare) 
if saveflare ne 'Y' then goto,getout
;
; If the catalog doesn't exist (count=0) open a new file.
;
fileexist = findfile ( (chklog('BATSE_FLARE_CATALOG'))(0), count=count)
if count eq 0 then begin 
   openw, /stream, cunit, (chklog('BATSE_FLARE_CATALOG'))(0), /get_lun
   number_flares = 0
endif else begin
   openu, /stream, cunit, (chklog('BATSE_FLARE_CATALOG'))(0), /get_lun
   readu, cunit, loghead
   loghead = conv_vax_unix(loghead)
   number_flares = loghead.last_flare
endelse
;
;
newflare = 0
; If flarenum is 0, means we're adding a new flare.  Figure out record number
; and flare number and set flag (newflare=1)
if flarenum eq 0 then begin
   newflare = 1
   stat = fstat(cunit)
;  Get current number of records and that will be new flare number.
;   flarenum = (stat.size/stat.rec_len > 1) 
   flarenum = (stat.size/size_struct( loghead) > 1) 
   recordn = flarenum 
   if flarenum le number_flares then begin
      print, 'Error in new flare number when adding new flare to catalog.'
      print, 'New flare number = ', flarenum, '  Total # flare in catalog = ', $
             number_flares
      print, ' '
      print, 'NOT ADDING THIS FLARE TO CATALOG !!!!!!!!!!!!!!!'
      print, ' '
      saveflare = 'N'
      goto, close
   endif
endif 
;   
; If we're adding a new flare, update number of flares in header record of 
; catalog.
if newflare eq 1 then begin
   loghead.last_flare = flarenum
   point_lun, cunit, 0
   writeu, cunit, loghead
endif
                                                       
write_flare:
; Write flare record after saving flare number in batlog structure.

batlog.flare_num=flarenum

; Insert pointing info at peak for this flare into the batlog structure
closest = min (abs(pseconds - batlog.peak_secs), ii)
print,'Time used for pointing = ',atime (pseconds(ii))
batse_pointing, xyz_pos = [xpos(ii),ypos(ii),zpos(ii)],$
                zx_radec=[zra(ii),zdec(ii),xra(ii),xdec(ii)], $
                ut=pseconds(ii), sun_in_gro=sun, earth_in_gro=earth
batlog.sun_position = sun
batlog.earth_position = earth

fstat = fstat(cunit)
;point_lun, cunit, recordn * fstat.rec_len
point_lun, cunit, recordn * size_struct( batlog)
writeu, cunit, use_vax_float(/new2old, batlog)

if auto then begin
   fldata(flarenum-1).peak_rate = batlog.peak_rate
   fldata(flarenum-1).total_counts = batlog.total_counts
   goto,getout
endif


; Close the file.
 
close:
free_lun,cunit

; Read current catalog into flare_catalog common since we just changed it.
batse_read_cat, /force_read

getout:

end