;+
; PROJECT:
;	SDAC
; NAME: 
;       QLARCHIVE
;
; PURPOSE:
;	This procedure creates the daily archive file, IDL save file.
; 	Generate archive quicklook plot files from BATSE data base (BDB) files.
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;	qlarchive, file
;
; CALLS:
;	CHECKVAR, HXRBS_FORMAT, BREAKFILE, FS_OPEN, QL_READBDB, ATIME, SC_SECONDS_ED, QLRADEC_FILL
;	QL_READBDB, DN_TRANS, ORBIT, FORM_FILENAME, DET_POINT, BATSE_OUT_ARR, 
;
; INPUTS:
;       bdbfile -  file name of batse bdb file, discla+housekeeping merge file. Only file+ext,
;	not the full path.
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	PATH - Path for save file other than $BATSE_QLOOK (def).
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;
; Kim Tolbert  6/9/92
; Mod KT 3/93 to handle omni data.  Calls qlradec_fill
; Mod KT 3/94 to use new compression scheme.  We were scaling everything
; to i*2's (between 1 and 20000).  Now scale the arrays (except the times)
; to bytes (between 1 and 255).  Also now scales logarithmically instead
; of linearly.  Times (seconds array) are still scaled to i*2's between
; 1 and 20000.
; Mod AES 7/94 to refer to old channel 1 as channel 0 - only affects
; comments, not code
; Mod AES 9/7/94 to save qlook files in XDR format
;
;Version 5, richard.schwartz@gsfc.nasa.gov 2-sep-1997
;	changed output file name extraction, now use break_file on bdbfile.
;	changed out_arr call to batse_out_arr	
; 	time and data arrays were stored scaled down as i*2.  
; 	Old style compressed with digitization error.  The new restore (2-sep-1997) maps the
; 	times onto packet times (multiples of 2.048 sec from xrange(0)) with the
; 	only error being daily clock drift.
;-
pro qlarchive, bdbfile, path=path

!quiet = 1
;
common quick_com, plot_st, plot_en, first_sec, fctr
checkvar, path, ''
hxrbs_format, old_format = old_time_format

plot_st = 0.d0
plot_en = 0.d0
dmark = 0.d0

; Open the BDB file for reading.


break_file, bdbfile, bdisk, bdir, bfnam, ext
savfile = form_filename(bfnam, '.sav',dir=(['BATSE_QLOOK',path])(keyword_set(path)))

dd_type = ext eq '.fdb'     ; input file type is bdb
fs_open, file=bdbfile, dd_open=dd_open, dd_type=dd_type
;
print, 'End time of bdb file = ',atime(dd_open.endsec,/hxrbs)
;
;
seconds = dblarr(3000)
bdb = replicate(dd_open.data, 3000)
;
startt = dd_open.startsec
endt = startt + 6000.d0
got_orbit = 0
getorbit:
ql_readbdb, dd_open, bdb, startt, endt, maxind
if maxind le 0 then begin
   print,'No data between ', atime(startt,/hxrbs), ' and ',atime(endt,/hxrbs)
   goto,getmore
endif
;
seconds(0) = sc_seconds_ed(bdb(0:maxind).sctime)
; orbit will return next_n = the next night crossing, and next_d = the
;  next day crossing after that night crossing
if not(got_orbit) then orbit, seconds(0:maxind), bdb(0:maxind).x_pos, $
       bdb(0:maxind).y_pos, bdb(0:maxind).z_pos, next_day=next_d, $
       next_night=next_n, period=period, bad_data=bad_data
if not(bad_data) then got_orbit = 1
qlradec_fill, seconds, bdb, maxind, radec_save=radec_save, error=rderror

if bad_data or rderror then begin
getmore:
;print,'getting more, startt=',atime(startt)
   startt = endt 
   if startt gt dd_open.endsec then begin
      print,'Couldn''t find any data with good pointing and position data.'
      goto, done
   endif
   endt = endt + 6000.d0
   goto,getorbit
endif

;
;
nsave = 0
norbit = -1
orbit_start = dblarr(20)
day_start = dblarr(20)
save_period = dblarr(20)
cos_det = fltarr(8,20)
savearr = fltarr(22000,7)
savesec = dblarr(22000)
;
;
;---  Beginning of plot loop.  Loops through orbits. ---

nextplot:
; next_n is the time of the next night in seconds (relative to 79/1/1)
; next_d is the time of the next day after next_n in seconds
; period is the orbital period in seconds
; plot_st, plot_en are start,end times of current plot in seconds
plot_en = (next_d(0) + next_n(0)) / 2.d0
plot_st = plot_en - period
if plot_st gt dd_open.endsec then goto,done
print,'Working on plot from ',atime(plot_st,/hxr), ' to ', atime(plot_en,/hxr)
;
norbit = norbit + 1
orbit_start(norbit) = plot_st
day_start(norbit) = next_d(0) - period
save_period(norbit) = period
;
dmark = next_d - period
nmark = next_n
;
ql_readbdb, dd_open, bdb, plot_st, plot_en, maxind
if maxind le 0 then begin
   print,'No data between ', atime(plot_st,/hxr), ' and ',atime(plot_en,/hxr)
   next_d(0) = next_d(0) + period
   next_n(0) = next_n(0) + period
   goto,nextplot
endif
;
seconds(0) = sc_seconds_ed (bdb(0:maxind).sctime)
;
; dn_trans will return an array of 31 day and night crossings in DAYS 
;   and NIGHTS surrounding the times we passed to it.
dn_trans, seconds(0:maxind), bdb(0:maxind).x_pos, bdb(0:maxind).y_pos, $
       bdb(0:maxind).z_pos, d_period=d_period, days=days, nights=nights, $
       bad_data=bad_data
if not bad_data then begin
;  curr_day will always be before curr_night since we're looking for
;  the first day and night after plot_st which is midnight.

   if norbit eq 0 then begin
      curr_day = days((where(days gt plot_st))(0))
      curr_night = nights((where(nights gt plot_st))(0))
   endif else begin
      curr_day = days((where(days gt orbit_start(norbit-1)+period))(0))
      curr_night = nights((where(nights gt curr_day))(0))
   endelse
   day_start(norbit) = curr_day
   orbit_start(norbit) = (curr_night - d_period + curr_day) / 2.d0
   save_period(norbit) = d_period
endif
;
qlradec_fill, seconds, bdb, maxind, radec_save=radec_save
;Call det_point to generate array of cosines for each detector.
det_point, seconds(0:maxind), bdb(0:maxind).x_ra, bdb(0:maxind).x_dec, $
           pnt_ndx, cosines, bdb(0:maxind).z_ra, bdb(0:maxind).z_dec
cos_det(*,norbit) = cosines(*,0)
;
batse_out_arr, seconds(0:maxind), bdb(0:maxind), cos_det(*,norbit), arr, top4
;
iout = -1
sam_av = 4
nnew = (2*(maxind+1)/sam_av)<(22000-nsave) 

for i=0,7  do begin
   if i eq 4 then goto,nextarr ; skip sum of ch 0, sunward detectors
   iout = iout + 1
   savearr(nsave:nsave+nnew-1,iout) = rebin (arr(0:nnew*sam_av-1,i), nnew)
   nextarr:
endfor
savesec(nsave:nsave+nnew-1) = seconds(indgen(nnew)*sam_av/2) + sam_av * .512
nsave = nsave + nnew


next_d = next_d + period
next_n = next_n + period
goto,nextplot
;
;---  End of plotting orbits loop -------------
;
;
done:
free_lun, dd_open.lun
print,'Saving into file ',savfile
nwrite = (nsave / 8) * 8   ; write only a multiple of 8 points
print,'# orbits=',norbit+1, ' # time point=',nwrite
norbits = norbit+1
period = save_period(0:norbit)
midnights = orbit_start(0:norbit)
days = day_start(0:norbit)
cosines = cos_det(*,0:norbit)
seconds = savesec(0:nwrite-1)
arrays = savearr(0:nwrite-1,*)


; Scale time array to integer*2's between 0 and 2e4
xrange = [min(seconds), max(seconds)]
seconds = fix((seconds-xrange(0))/2.048 + 0.2)
; restore seconds with this
; 
; seconds = (dindgen( 42200l) * 2.048 + xrange(0))(seconds and 65535L)
; 
; Old compression
;seconds = fix(2e4*(seconds - xrange(0)) / (xrange(1)-xrange(0))) 
newrestore = "seconds = (dindgen( 42200l) * 2.048 + xrange(0))(seconds and 65535L)"

; Scale other arrays logarithmically to bytes between 1 and 255
yrange = fltarr(2,7)

npoints = n_elements(seconds)
bytarrays = bytarr(npoints,7)
for i = 0,6 do begin

   qpos = where (arrays(0:npoints-1,i) gt 0., kpos)
   if kpos gt 0 then begin
      arrlog = alog10(arrays(qpos,i))
      yrange (*,i) = [min(arrlog), max(arrlog)]
      bytarrays(qpos,i) = byte( 1 + 255 * (arrlog - yrange(0,i)) / $
         (yrange(1,i) - yrange(0,i)) )
      endif
 
endfor

; Save enough data in BATSE_QLOOK .sav file to be able to reconstruct
; orbital and full-day plots in qldisplay program.

save, /xdr, file=savfile, norbits, period, midnights, days, cosines, $
      xrange, yrange, seconds, bytarrays, newrestore
;

getout:
if old_time_format eq 'YOHKOH' then yohkoh_format

end
