pro ascii_raw
;
common dumpcom, usefdb, dd_open, dd_type, sec_st, sec_to, sec_en, det_arr, $
  prcounts, prposition, prhouse, outprint

if not(usefdb) and dd_type eq 1 then dd_type = 0
flare = file2flare(dd_open.filename)
if flare eq 0 then begin
   fs_open, time=batse_file2time(dd_open.filename), dd_type=dd_type, $
   dd_open=dd_open, /verbose, error=error
endif else begin
   fs_open, flare=file2flare(dd_open.filename), dd_type=dd_type, $
   dd_open=dd_open, /verbose, error=error
endelse
if error then goto, getout

openw, lu, 'batse_raw.out', /get_lun

if prhouse and dd_type gt 1 then begin
   prhouse = 0
   print, 'Housekeeping information not available for CONT or DISCSP files.'
endif

count = 0 ; initialize counter for number of lines printed to 0
page_print, outprint, count, lu, ' '
page_print, outprint, count, lu, $
   format='("BATSE Raw Data Dump", t42, "Current time:", a)', $
   strmid (atime(sys2ut(0),/hxr), 0, 14)
page_print, outprint, count, lu, $
   format='("-------------------")'
;
page_print, outprint, count, lu, ' '
type = ['BDB','FDB','CONT','DISCSP']
page_print, outprint, count, lu, $
   format='("Header data:      Type of file = ",a,t42,'+ $
   '"Number of packets in file = ",i5)', $
   type(dd_open.type),dd_open.numrec
page_print, outprint, count, lu, $
   format='(t42,"Start time = ",a)',atime(dd_open.startsec,/hxr)
page_print, outprint, count, lu, $
   format='(t42,"End time   = ",a)',atime(dd_open.endsec,/hxr)

find_packet, sec_st, dd_open, first_rec, err = err
if err then begin
   print,'Error reading input file.'
   goto, getout
endif

record = first_rec - 1

fstat = fstat(dd_open.lun)
pad_byte = getenv('pad_byte_'+dd_open.filename)
if pad_byte eq '' then pad_byte = 0 else pad_byte=fix(pad_byte)
point_lun, dd_open.lun, first_rec * (size_struct(dd_open.data)+pad_byte)

rec = dd_open.data
loop:
   record = record + 1
   if record gt dd_open.numrec then goto,done
   readu, dd_open.lun, rec
   rec = conv_vax_unix( use_vax_float(/old2new,rec))
   rec_time = sc_seconds_ed(rec.sctime)
   rec_time = [rec_time, rec_time + 1.024]
   if dd_type gt 1 then nsampack = 1 else nsampack = 2
   for isamp = 0,nsampack-1 do begin
      page_print, outprint, count, lu, abort=abort,' '
      if abort then goto,done
      page_print, outprint, count, lu, abort = abort, $
      format='("********    ",a,"  packet # ",'+ $
             'i9,"    ********")',atime(rec_time(isamp),/hxr),rec.counter
      if abort then goto,done
      page_print, outprint, count, lu,abort=abort, ' '
      if abort then goto,done
;
;--- print counts data if requested
      if prcounts eq 1 then begin
         case 1 of 

            dd_type le 1: begin
               page_print, outprint, count, lu, abort=abort, format= $
                  '("DISCLA:",t17,"ch 0   ch 1   ch 2   ch 3    NaI    CPD")'
;CHANGE above:  channels listed as 0-3 rather than 1-4
               if abort then goto,done
               for i=0,n_elements(det_arr)-1 do begin
                  id = det_arr(i)
                  page_print, outprint, count, lu, abort=abort, $
                     format='("Detector",i2,": ",t14,6i7)',$
                     id,rec.disc(0:5,id  ,isamp)
                  if abort then goto,done
               endfor
            end

            dd_type eq 2: begin
               page_print, outprint, count, lu, abort=abort, format= $
                '("CONT:",t16, ' + $
                '"ch  0  ch  1  ch  2  ch  3  ch  4  ch  5  ch  6  ch  7")'
;Change above:  channels listed as 0-15 rather than 1-16
               page_print, outprint, count, lu, abort=abort, format= $
                '(t17,"   8      9     10     11     12     13     14     15")'
               if abort then goto,done
               page_print, outprint, count, lu,abort=abort, ' '
               if abort then goto,done
               for i=0,n_elements(det_arr)-1 do begin
                  id = det_arr(i)
                  page_print, outprint, count, lu, abort=abort, $
                     format='("Detector",i2,": ",t14,8i7)',$
                     id, rec.cont(0:7,id  )
                  if abort then goto,done
                  page_print, outprint, count, lu, abort=abort, $
                     format='(t14,8i7)', rec.cont(8:15,id  )
                  if abort then goto,done
               endfor
            end

            dd_type eq 3: begin
               page_print, outprint, count, lu, abort=abort, format= $
                '("DISCSP:",t16, ' + $
                '"ch  0  ch  1  ch  2  ch  3")'
;Change above:  channels listed as 0-3 rather than 1-4
               for i=0,n_elements(det_arr)-1 do begin
                  id = det_arr(i)
                  page_print, outprint, count, lu, abort=abort, $
                     format='("Detector",i2,": ",t14,4i7)',$
                     id, rec.discsp(0:3,id  )
                  if abort then goto,done
               endfor
            end

         endcase
         page_print, outprint, count, lu, abort=abort, 'ENG_TELEM = ',rec.eng_telem
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort, ' '
         if abort then goto,done
      endif
;
;--- print position data if requested
      if prposition eq 1 then begin
         page_print, outprint, count, lu, abort=abort, $
                   format ='("GRO position (x,y,z) = ",T34, 3i7,'+ $
                   '"  (1/4 km units)")', $
                   rec.x_pos,rec.y_pos,rec.z_pos
         if abort then goto,done
         if dd_type le 1 then page_print, outprint, count, lu, abort=abort,$
                format ='("Geocenter position (Az, elev) = ",T34,2i7,'+ $
                '"  (.0001 rad units)")', rec.geo_az, rec.geo_el
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort,$
                format ='("GRO pointing (z ra, z dec) = ",T34,2i7,'+ $
                '"  (.0001 rad units)")', rec.z_ra, rec.z_dec
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort,$
                format ='("             (x ra, x dec) = ",T34,2i7,'+ $
                '"  (.0001 rad units)")',  rec.x_ra, rec.x_dec
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort,' '
         if abort then goto,done
      endif
;
;--- print housekeeping data if requested
      if prhouse eq 1 then begin
         page_print, outprint, count, lu, abort=abort,$
                format= '("HKGSEQ  =", i6, " CMECHO =", i6, '+ $
                '" COINC = ",i6)', rec.hkgseq, rec.cmecho, rec.coinc
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort,$
                format= '("HKGMSC1 =",i6," HKGMSC2 =",i6," HKGMTB =",i6,'+ $
                '" DEADTM =", i6, " NLDI =",i6)', rec.hkgmsc1, rec.hkgmsc2, $
                rec.hkgmtb, rec.deadtm, rec.nldi
         if abort then goto,done                
         page_print, outprint, count, lu, abort=abort,$
                format= '("STAT    =",3i6," ERRF = ",i6," ERRDAT =",2i6)', $
                rec.stat, rec.errf, rec.errdat
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort,$
                format= '("NSPEC   =", i6, " OUTADD =", i6, " COMXQT =",'+ $
                'i4, " COMREC =", i4, " LRCVD =", i6, " LXQT =", i6)', $
                rec.nspec, rec.outadd, rec.comxqt, rec.comrec, rec.lrcvd, $
                rec.lxqt
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort, $
                 format= '("HKGMOD =", 8i7)', rec.hkgmod
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort, $
                 format= '("EXFLAG =", 8i7)', rec.exflag
         if abort then goto,done
         page_print, outprint, count, lu, abort=abort, $
                 format= '("CHECK  =", 8i7)', rec.check
         if abort then goto,done
      endif
    endfor
if rec_time(1)+1.024 lt sec_en then goto,loop
;
done:
free_lun,lu
print, 'This information is saved in file ', findfile('batse_raw.out')
;
mailfile,'batse_raw.out'
;
getout:
end