        PRO go_teem_t, index, data, tec, emc, dtec, demc, fl1c, fl2c, $
		time=time, comments=comments, subx=subx, suby=suby, $
		interp=interp, emzero=emzero, $
		unc_data=unc_data, satpix=satpix, density=density, $
		noplot=noplot, lc_plot=lc_plot, backsub=backsub, $
                area=area_all
;	---------------------------------------------------------------
;+							12-Mar-94
;	NAME: go_teem_t
;
;	PURPOSE: Compute the time-profile for the average Te, EM, and 
;		normalized Intensity for an aligned data-cube from 
;		"sxt_prep*".  The only modification from go_teem is
;		to be able to keep the information on the areas selected.
;	
;	CALLING SEQUENCE:
;		go_teem_t, index, data, te, em, dte, dem, fl1, fl2, $
;			[time=time, comments=comments, interp=interp, $
;			unc_data=unc_data, satpix=satpix, $
;			noplot=noplot, lc_plot=lc_plot, area=area]
;
;	INPUT:
;		index	index-data records prepared by "sxt_prep*"
;		data	data cube: decompressed, bkg subtracted,
;			leak corrected, and aligned via "sxt_prep*".
;
;	OUTPUT: 
;		te	Log of the electron temperature
;		em	Log of the emission measure
;		dte	Log of the Te errors
;		dem	Log of the em errors
;		fl1	Normalized Intensity profile for thicker fil. images
;		fl2	Normalized Intensity profile from thinner fil. images
;		area	Selected areas - bombs if an area has more than 400
;			pixels
;
;	OPTIONAL INPUT:
;		interp  switch to use spline vs linear interpolation
;		average	always computes average Te over area selected.  
;			FUTURE option-- 
;			will make this a switch but is not available yet
;		unc_data uncertainty data from "sxt_prep*"
;		sat_pix	saturated pixel maps from "sxt_prep*"
;		noplot	switch to turn-off interactive plots
;		lc_plot	switch to request light-curve plots
;		backsub	switch to request interactive background emission
;			subtraction.
;
;	OPTIONAL OUTPUT:
;		time	list of interpolated times
;		comments User entered notes for each run, also used
;			as plot titles.
;
;	EXAMPLE USAGE:
;
;
;	NOTES and WARNINGS:
;		
;	HISTORY:
;		written by GAL summer 93
;		modified for release Sept. 93
;		22-Oct-93, added code to handle uncertainty values for
;			background subtraction and interpolation.
;	19-Nov-93, J. R. Lemen, Modified to call pprint
;	22-Mar-94, N. Nitta, added a keyword area to remember which
;		areas are selected.
;
;-
; 	---------------------------------------------------------------



        area_all=lonarr(400,100)-1

        save_count=-1

;	Setup controll varibles
	backsub_ok = 1			;ok to subtract background only once

;	>>>>>>> Pick Analysis Filters <<<<<<<<<
;	Split index and data into selected filter data:
;		index1 & data1  for thick filtered data
;	        index2 & data2  for thin filtered data


	wfilpck, index, thici, thini, interp_thin  ;pick filters

        index1 = index(thici)			;thick filtered images
	data1  = data(*,*,thici)
	index2 = index(thini)              	;thin filtered images
	data2  = data(*,*,thini)

	if n_elements(unc_data) ne 0 then begin
	  unc_data1 = unc_data(*,*,thici)
	  unc_data2 = unc_data(*,*,thini)
	endif
	if n_elements(satpix) ne 0 then begin
	  satpix1 = satpix(*,*,thici)
	  satpix2 = satpix(*,*,thini)
	endif

;	>>>>>>> Interpolate Images to same time <<<<<<
;	interp_thin = 1		;interp thin images & use index1 for times
;	if n_elements(thini) gt n_elements(thici) then begin
;	  mess = "Interpolate Thin Filtered images to times of thick?"
;	endif else begin
;	  if n_elements(thini) lt n_elements(thici) then begin
;	    mess = "Interpolate Thick Filtered images to times of thin?"
;	    interp_thin = 0	;interp thic images & use index2 for times
;	  endif else $
;	    mess = "Same number of Thin and Thick images. Interpolate?"
;	endelse
;	ans = 'y'
;	input, mess, ans, ans
	
	IF interp_thin ne -1 THEN begin		;do some interpolating
	  if interp_thin then begin
            SXT_interp, index2, data2, index1, unc_data2, satpix2
	    time = anytim2ints(index1)		;thick filter times
;	    unc_data2 = byte(SQRT(fix(unc_data2)^2 + fix(unc_data2)^2))	;est. of new errors
	  endif else begin
	    SXT_interp, index1, data1, index2, unc_data1, satpix1
	    time = anytim2ints(index2)          ;thin filter times
;	    unc_data1 = byte(SQRT(fix(unc_data1)^2 + fix(unc_data1)^2))	;est. of new errors
	  endelse
	ENDIF else BEGIN
	  time = anytim2ints(index2)	;use thin filter times
	  interp_thin = 0		;set interp flag for thin filt times
	ENDELSE

; stop, 'Interpol DONE--SAVE data1 and data2 to sda file:'
;	Get general Information on filtered data:
	dat1sz =  size(data1)
	dat2sz =  size(data2)
	nx = dat2sz(1) & ny = dat2sz(2)

	continue = 1	
	first_time = 1
        imgn = 0


	While (continue) DO BEGIN
	  
	  if not first_time then begin
	    ans = 'y'
	    input, "Reselect a sub-region to analyze?", ans, ans
	  endif else ans ='Y'
          
          dummy_nn=bytarr(nx,ny)
	  IF strupcase(ans) eq 'Y' THEN BEGIN
	   If dat1sz(0) eq 3 Then Begin
	    wdef, 3
	    zm = 512/nx
            regs = wdefroi(data2,xvert,yvert,time=time,/lc,zoom=zm)
            area=regs
            dummy_nn(regs)=1
           ; xvert = xvert/zm & yvert = yvert/zm
           ; print,xvert,yvert
           ; area = polyfillv(xvert,yvert,nx,ny)
           ;  print,area
	   Endif Else Begin
	    input, "Input index number to display for region selection:",imgn,imgn

            wdef,3			;clear
	    zm = 512/nx
	    tv, rebin(bytscl(alog10(data2(*,*,imgn))), zm*nx, zm*ny,/sample)
            regs = wdefroi(data2,xvert,yvert,zoom=zm)
            dummy_nn(regs)=1
            area=regs
            ocontour,congrid(dummy_nn,zm*nx, zm*ny)
           ; xvert = xvert/zm & yvert = yvert/zm
           ; print,xvert,yvert
           ; area = polyfillv(xvert,yvert,nx,ny)
            print,area
	   Endelse
	  ENDIF

	  comment1 = ''
	  input, "Enter an analysis note for this selection:",comment1,comment1

; stop, 'PRE--ck bkg_sub:'
	  IF n_elements(backsub) ne 0 THEN BEGIN
	    if not first_time then begin
;	      Background subtract the pre-event emission?
	      if ( backsub_ok ) then begin	;ok to do only ONCE
		backsub_ok = 0			;will not allow again
	        ans = 'N'
	        input, "Subtract pre-event emission?", ans, ans
	        If strupcase(ans) eq 'Y' THEN BEGIN
;		  Need to add another new estimate of the unc_data ....
;		  guess = unc_d(of frames used to cal ave.) + all other frames
	          bkg_sub, index1, data1, unc=unc_data1			;do thick and 
	          bkg_sub, index2, data2, em=emcub, ave_em=ave_em, $
				unc=unc_data2	;thin
	          emzero = ave_em
	        ENDIF	    
	      endif			;backsub_ok ck
	    endif
	  ENDIF

; stop, 'post--ck use stepper....bkg_sub:'
; stop, 'ck szreg and regs'
		
	  szreg = size(regs)
	  if szreg(0) eq 2 then begin
	    npixs = szreg(1)	;npixels by nimgs
	  endif else begin
	    npixs = n_elements(regs)
	  endelse

	  Print, "** Number of selected pixels: ", npixs

;         xsz =  subx(1)-subx(0)+1	;for box coordinates
;	  ysz =  suby(1)-suby(0)+1
    
;	  dat1s = Intarr(n_elements(reg),dat1sz(3))
;	  dat2s = Intarr(n_elements(reg),dat2sz(3))


          FOR i= 0, dat1sz(3)-1 DO begin 	;for all images

	    if n_elements(regs) ne 0 then reg = regs(*,i)

;	    tdat1 = data1(*,*,i) 
;	    tdat2 = data2(*,*,i)

;	    tdat1 = data1(*,*,i)	& dat1s(0,i) = tdat1(reg)
;	    tdat2 = data2(*,*,i)	& dat2s(0,i) = tdat2(reg)

;		qdat1s(0,0,i) = data1(subx(0):subx(1),suby(0):suby(1),i)
;		dat2s(0,0,i) = data2(subx(0):subx(1),suby(0):suby(1),i)

;		te, em, dte, dem, interp=interp, average=average, $

	    sxt_teem, index1(i), data1(reg), index2(i), data2(reg), $
		te, em, dte, dem, interp=interp, /average, $
		sat1=satpix1,sat2=satpix2,unc1=unc_data1,unc2=unc_data2

            if i eq 0 then begin
	      
	      tecub = te
              emcub = em
              dtecub= dte
              demcub= dem
	      fl1cub= total(data1(reg))/(gt_expdur(index1(i))/1000.)	;per sec
	      fl2cub= total(data2(reg))/(gt_expdur(index2(i))/1000.)	;per sec
	    endif else begin
              tecub = [ tecub, te ]
              emcub = [ emcub, em ]
              dtecub= [ dtecub, dte]
              demcub= [ demcub, dem]
              fl1cub= [ fl1cub, total(data1(reg))/(gt_expdur(index1(i))/1000.)]
              fl2cub= [ fl2cub, total(data2(reg))/(gt_expdur(index2(i))/1000.)]
	    endelse
	  ENDFOR
          
          areacub=reform(area(*,0)) & help,areacub 


	  IF n_elements(noplot) eq 0 THEN BEGIN	;no "noplot" switch
;	    Plot results:

	    if interp_thin then begin
	      go_teem_plots, comment1, index2(0), index1, tecub, dtecub, $
		emcub, demcub, emzero, npixs, fl1cub, fl2cub, $
		density=density, lc_plot=lc_plot
            endif else begin
	      go_teem_plots, comment1, index1(0), index2, tecub, dtecub, $
		emcub, demcub, emzero, npixs, fl1cub, fl2cub, $
		density=density, lc_plot=lc_plot
	    endelse

  	    ans = 'n'
	    Input, "Hardcopy of Plots?", ans, ans
	    if strupcase(ans) ne 'N' then begin
	      set_plot, 'ps'
	      if interp_thin then begin
	        go_teem_plots, comment1, index2(0), index1, tecub, dtecub, $
		emcub, demcub, $
		emzero, npixs, fl1cub, fl2cub, $
		density=density, /lc_plot, /hardcopy
              endif else begin
	        go_teem_plots, comment1, index1(0), index2, tecub, dtecub, $
		emcub, demcub, $
		emzero, npixs, fl1cub, fl2cub, $
		density=density, /lc_plot, /hardcopy
	      endelse
	      pprint,/reset
	    endif
	  ENDIF

;	  Continue this Analysis?
          ans = 'y'

	  input, "Continue?", ans, ans
	  IF strupcase(ans) ne 'Y' THEN BEGIN		;stopping
	    continue = 0	;all stop
	    if not first_time then begin
	      ans = 'y'
	      input, "Save/append these results (Te, EM, fl*, etc.)?",ans,ans
	      if strupcase(ans) ne 'Y' then begin
;		do nothing
	      endif else begin 		;append results

                save_count=save_count+1
                area_all(0,save_count)=areacub
                area_all=area_all(*,0:save_count) & help,area_all
                n_pix=lonarr(save_count+1)
                for iii=0,save_count do n_pix(iii)=n_elements(where(area_all(*,iii) ge 0) )
                max_npix=max(n_pix)
                area_all=area_all(0:max_npix-1,*) & help,area_all & print,n_pix & help,max_npix

                tec = [ [tec], [tecub] ]
		emc = [ [emc], [emcub] ]
		dtec= [ [dtec], [dtecub] ]
		demc= [ [demc], [demcub] ]
		fl1c= [ [fl1c], [fl1cub] ]
		fl2c= [ [fl2c], [fl2cub] ]
		comments = [comments, comment1]
	      endelse
	    endif else begin		;1st time thur & quiting

              area_all(0,0)=areacub
              npix=where(area_all(*,0) ge 0,ic) & print,npix 
              if ic ge 1 then area_all=reform(area_all(0:ic-1,0)) else area_all=-1  & help,area_all
 
	      tec = tecub 		;setup final return vars.
              emc = emcub 
              dtec= dtecub
              demc= demcub
              fl1c= fl1cub
              fl2c= fl2cub
              comments = comment1
	    endelse
	  
           ENDIF ELSE BEGIN				;continue....
	   
            if not first_time then begin
	      ans = 'y'
	      input, "Save/append these results (Te, EM, fl*, etc.)?",ans,ans
	      if strupcase(ans) ne 'Y' then begin
;		do nothing
	      endif else begin		;append results

                save_count=save_count+1
                area_all(0,save_count)=areacub


                tec = [ [tec], [tecub] ]
		emc = [ [emc], [emcub] ]
		dtec= [ [dtec], [dtecub] ]
		demc= [ [demc], [demcub] ]
		fl1c= [ [fl1c], [fl1cub] ]
		fl2c= [ [fl2c], [fl2cub] ]
		comments = [comments, comment1]
	      endelse
	    endif else begin		;1st time thur
	      tec = tecub 		;setup final return vars.
              emc = emcub 
              dtec= dtecub
              demc= demcub
              fl1c= fl1cub
              fl2c= fl2cub
	      comments = comment1

              save_count=save_count+1
              area_all(0,save_count)=areacub 

	    endelse
	  ENDELSE
	  first_time = 0		;not 1st anymore
	ENDWHILE


	END
