pro sxl_analysis,sttim,entim,index,flux,filter=filter,res=res,lvls=lvls, $
	loud=loud

;+
;       NAME: sxl_analysis
;
;
;       PURPOSE: Collect data from the SXL data base for study of the
;		radiative output of the corona within different brightness
;		regimes versus time.  Basic input data are the SXL 
;		histograms.  Output data are total decompressed DN/HRpix/sec.
;		The normalization to /sec comes from the fact that the 
;		SFDs are normalized this way. 
;	COMMENTS - (3/10/96):
;                Changed the conversion to throw away sxl(dn)=0
;		to compensate error introduced by mk_sfd ignoring
;		negative values after dark/leak subtraction.  Kept integer
;		truncation of sfd_decomp as this gave a more accurate
;		result for the 1 case (25-May-95) tested.
;		 However, for the on-disk fluxes between levels I have
;		kept the sxl(dn)=0 in the sum because the on-disk intensities
;		are more likely to be above background.
;
;       CALLING SEQUENCE:
;           sxl_analysis, sttime, entime, index, flux $
;              [, filter=filter, res=res, lvls=lvls]
;           sxl_analysis,'1-dec-92','15-feb-93',ind0,flux0
;	    sxl_analysis,'1-dec-92','15-feb-93',ind0,flux0, $
;		filter=2,res=[1,2]
;
;       INPUT:
;               sttime = starting time
;               entime = ending time
;
;	OPTIONAL KEYWORD INPUT
;		filter, (2=Al.1, 3=AlMg), default=3
;		  can do either filter or both, i.e., filter=[2,3]
;		res = resolution, (1=half, 2=qrtr), default=1
;		  Note: QR data have been multipled by 4 to match HR data.
;		lvls = Brightness levels in sfd_compressed DN for basis
;                 of sorting on-disk data.
;		  Default: lvls = [0, 50, 100, 150, 255]; for AlMg
;                 Use:     lvls = [0, 62, 112, 161, 255]; for Al.1
;		/loud, prints out diagnostic info
;
;       RETURNED/OUTPUT:
;         ***Note: QR data HAVE BEEN multipled by 4 to match HR data.***
;               index  = index(nn) of sfd images analyzed
;		flux(6,nn) = array of fluxes in decompressed DN/sec
;	  flux(0,nn) = signal in the entire image
;	  flux(1,nn) = signal above radius = 1.05 solar radius
;         flux(2:5,nn) = Total DN per second of on-disk (R<0.86Rsun) image
;                             within the intervals set
;                             by the array lvls within the program.
;                             Presently lvls = [0,50,100,150,255].
;
;	RESTRICTIONS:
;	     1. Program returns at least a full week of data, even if
;		sttime, entime span less than a week.
;
;       HISTORY:
;               Written by LWA 25-Feb-94
;		Expanded and changed to read one week at a time 2-Mar-94
;		Changed to output roadmap rather than index 2-mar-94.  lwa
;		Changed to allow keywords and print info while running,
;		  also use STR_CONCAT.  3-Mar-94, lwa.
;		Changed header to explicitely state that output is 
;		  decompressed DN/sec.
;		Added clarifications on QR data and sttime, entime span
;		  LWA 11/18/95, 12/8/95.
;		Empirical mod to compensate for serious "flux enhancement"
;		  by mk_sfd for low signal cases where all zero or negative
;		  values of the corrected image are set = 1.  LWA 3/10/96
;		Incorporate smaller index structure.  LWA 3/10/96
;		Changed name from sxl_analysis2 to sxl_analysis. LWA 12/5/97
;		Changed variable filtr back to filter for historical reasons.
;		and modified so both Al.1 and AlMg can be done at once.  LWA 6/24/99
;		4-Jul-99 LWA, Added keyword loud.
;		4-Nov-99 LWA, Replaced delind with kill_index.
;		21-Jul-00 LWA, Clarified header definition of flux(2:5,nn)
;		  and changed extension to a.03.
;		 1-Aug-00 LWA, Changed extension back to a.02 (dumb mistake).
;		10-May-01 LWA, Fixed Y2K problem in file generator.
;-

;   HERE WE SET UP THE FILES TO READ THE DESIRED DATA
exten = 'a.02'
if (keyword_set(old)) then exten = 'a.01'
;

    weeks = week_loop(sttim, entim)
    files = 'sxl' + string(strmid(weeks.year,6), format='(i2.2)') + $
	'_' + string(weeks.week, format='(i2.2)') + exten
    files = concat_dir('$DIR_SXT_SXL', files)
    ss = where(file_exist(files), count)
    if (count eq 0) then begin
        print, 'RD_SXL: Cannot find the necessary files:'
        prstr, '   ' + files
        return
    end
    files = files(ss)

;    HERE WE SET UP THE LVLS AND DN FILES

if NOT (keyword_set(filter)) then filter=3 else filter=filter
if NOT (keyword_set(res)) then res=1 else res=res
if (keyword_set(lvls)) then lvls=lvls else lvls=[0, 50, 100, 150, 255]

; Prep to sum decompressed DN.
;dn = lonarr(256)
;for i = 0,255 do dn(i) = sfd_decomp(byte(i))
dn = long(sfd_decomp(byte(indgen(256))))    ; 3/9/96
ok=indgen(255)+1

if n_elements(res) eq 1 then begin
print,'You have chosen ',gt_filtb(filter,/st),'at ',gt_res(res,/str), ' resolution.'
endif else begin
PRINT
print,'You have chosen ',gt_filtb(filter,/st),'at HR and QR.'
print,'QR flux is normalized for pixel size by *4 multiplication.'
endelse

;   HERE WE READ THE DATA AND BUILD THE INDEX AND FLUX FILES
print,'Start working through the SXL data'
print
n_weeks = n_elements(files)
start = 0
for ww = 0,n_weeks-1 do begin
;prstr,files(ww)
print,files(ww)

   rd_xda, files(ww), -1, ind, dat
;  Modified to handle more than one filter at a time.  LWA  6/24/99
   ii=-2
   for i=0,n_elements(filter)-1 do begin
      for j=0,n_elements(res)-1 do begin
         jj = where((gt_filtb(ind) eq filter(i)) and $
            (gt_filta(ind) eq 1) and (gt_res(ind) eq res(j)),nn)
         if nn gt 0 then begin
            if min(ii) eq -2 then ii=jj else ii=[ii,jj]
         endif
      endfor
   endfor
   if min(ii) eq -2 then ii=-1 else ii=ii(sort(ii))
   if keyword_set(loud) then help,ii

   if ii(0) ne -1 then begin

      ind = ind(ii)
      flx = lonarr(6,n_elements(ii))

;  DEFINE THE REDUCED INDEX STRUCTURE
   indx = {time:1L,day:1,PERIPH:1B,TEMP_CCD:1B,$
            IMGPARAM:1B,EXPLEVMODE:1B,onctr:1B}
   indx = replicate(indx,n_elements(ii))
; GET THE POINTINGS
; Acceptable region for pointing:  x=437:587, y=437:630 FRpix
   pnt = sxt_cen(ind(ii),/cmd)
   ctr = where(pnt(0,*) ge 437 and pnt(0,*) le 587 and $
      pnt(1,*) ge 437 and pnt(1,*) le 630)
   if (n_elements(ctr) lt n_elements(ii)) then begin
      off = kill_index(indgen(n_elements(ii)),ctr)
      indx(off).onctr=0
   endif
   if keyword_set(loud) then help,ctr,off

; BUILD THE REDUCED INDEX STRUCTURE
   indx.time=ind.gen.time
   indx.day=ind.gen.day
   indx.periph=ind.sxt.periph
   indx.temp_ccd=ind.sxt.temp_ccd
   indx.imgparam=ind.sxt.imgparam   
   indx.explevmode=ind.sxt.explevmode

      for k = 0,n_elements(ii)-1 do begin  ; Total flux in the image.
;         for j = 0,255 do begin
;            flx(0,k) = dat(ii(k)).hist(j,0)*dn(j)+flx(0,k)
;         endfor
	  flx(0,k) = total(dat(ii(k)).hist(ok,0)*dn(ok))  ; lwa 3/10/96
	  flx(1,k) = total(dat(ii(k)).hist(ok,2)*dn(ok))  ; lwa 3/10/96
      endfor

;      for k = 0,n_elements(ii)-1 do begin   ; Flux above the limb.
;         for j = 0,255 do begin
;            flx(1,k) = dat(ii(k)).hist(j,2)*dn(j)+flx(1,k)
;         endfor
;      endfor

      for i = 2,5 do begin   ;Flux in brightness ranges on the disk.
         for k = 0,n_elements(ii)-1 do begin
            for j = lvls(i-2)+1,lvls(i+1-2) do begin
               flx(i,k) = dat(ii(k)).hist(j,1)*dn(j)+flx(i,k)
            endfor
          endfor
      endfor

      for k = 0,n_elements(ii)-1 do begin  ; Pix size normalize QR images.
         if (gt_res(ind(k)) eq 2) then flx(*,k) = flx(*,k)*4.
      endfor

      if start eq 1 then begin
         index = str_concat(index, indx)
         flux = [[flux],[flx]]
      endif

      if start eq 0 then begin
         index = indx
         flux = flx
         start = 1
      endif

   endif
endfor

end
