;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; UVSPPROCESS.PRO -- send program to imaging code if experiment is an image,
;		      or to the plotting code if it is a light curve;
; 		      where the data is processed and displayed
;
; struc 	: for both  : a structure of structure (see UVSPSTRUC.PRO)
; data		: for both  : array holding the experiment data
; hd		: for both  : array holding the experiment header
; ave_sep	: for images: 0=average repeats for one image
;			      1=display each repeat separately
; scal		: for image : 0=linear scale, 1=sqrt, 2=log
; p_scal	: for plot  : 0=linear scale, 1=log
; contur    	: for images: 0=no contours over image, 1=contouring in effect
; silent	: for image  : 0=use defaults without asking user for inputs
;		  	      1=ask for input w/ additional options
; solgrid	: for images: 0=no heliograghic grid, 1=yes grid
; rollo         : for images: 0=don't roll the image so north is up
;			      1=roll image
; smuth		: for images: 0=not smooth image, 1=do smooth image
; templev	: for images: the contour values 
; exptyp	: for here  : 0=plot data, 1=raster data
; abscissa     	: for both  : the x-axis array for plots
; plotover	: for plots : 0=overplot each detector on same plot
;		 	      1=plot each detector in separate
; one_det	:
; c_det		:
; ylohi		: for plots : array with min and max values of y-axis 
; xlohi		: for plots : array with min and max values of X-axis 
; nsome		: for plots : value by which to bin data points
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

pro uvspprocess, struc=struc, data=data, hd=hd, ave_sep=ave_sep, $
			scal=scal, p_scal=p_scal, contur=contur, $
			silent=silent, solgrid=solgrid, point=point, $
			rollo=rollo, $
			smuth=smuth, templev=templev, cycle=cycle, $
			exptyp=exptyp, timfil=timfil, hard=hard, $
			abscissa=abscissa, plotover=plotover, one_det=one_det, $
			c_det=c_det, mytick=mytick, maxcyc=maxcyc, $
			ylohi=ylohi, xlohi=xlohi, nsome=nsome, mypsym=mypsym
                            
common order1, diskexp, control, generic, comment

@uvspplotcom.common
@uvsprast.common

 if n_elements(hard) eq 0 then hard=0		; not making a hardcopy

 ;
 ; Have all the option so set the buttons to match the conditions
 ;

 widget_control, plt_lin, set_button = 1 - p_scal    	     ; linear plot ?
 widget_control, plt_log, set_button = p_scal 	     	     ; log plot ?

 widget_control, plt_over, set_button = plotover gt one_det  ; oplot det ?
 widget_control, plt_sep,  set_button = plotover eq one_det  ; plot sep. det. ?
 widget_control, plt_nex,  set_button = plotover lt one_det  ; plot next det. ?

 widget_control, plt_auto,  set_button = total(ylohi) eq 0
 widget_control, plt_axman, set_button = total(ylohi) ne 0

 widget_control, plt_histo, set_button = mypsym eq 10
 widget_control, plt_grid,  set_button = mytick eq 1.0

 but_name = [ 'Info page', 'More info' ]
 widget_control, plt_info, $
		 set_value=but_name(cycle-1 lt maxcyc and exptyp ne 0)

 widget_control, plt_bin, set_value='Bin [' + strtrim(nsome,2) + ']'

 widget_control, ras_lin,  set_button = scal eq 0
 widget_control, ras_sqrt, set_button = scal eq 1
 widget_control, ras_log,  set_button = scal eq 2
 
 widget_control, ras_grid,   set_button = solgrid
 widget_control, ras_rol,   set_button = rollo
 widget_control, ras_smuth, set_button = smuth
 widget_control, ras_con,   set_button = contur

 widget_control, ras_ave, set_button = ave_sep eq 0
 widget_control, ras_sep, set_button = ave_sep eq 1

 widget_control, ras_quiet, set_button = silent eq 1
 widget_control, ras_loud,  set_button = silent eq 0

 widget_control, ras_info, $
		 set_value=but_name(cycle-1 lt maxcyc and exptyp ne 0) 

case exptyp of

 0: begin 		; plotting experiment


;6/93    if plotover eq 1 then begin 		; plot all detector on one plot
;        ;
;	; Detector w/ the max passed to UVSPPLOT as C_DET.
;        ; Redetermine det w/ max value, it can change.
;        ;
;
;        lastmax = -1  &  lastmin = 99999
;        siz = size(data)  		     		; need # pol & WL
;        polwl= siz(3) * siz(4) 	     		; polarimeter * WL
;        r1 = xlohi(0)/polwl  &  r2 = xlohi(1)/polwl 	; range w/in a det
;
;        for i = 0, struc.st7.ndet-1 do begin
;	   maxtest= size(data) & where(maxtest(1:
;	   tmax = max(data(*,*,*,*,r1:r2,i), min=tmin)
;           if tmax gt lastmax then c_det = i  		; det w/max
;	   lastmax = tmax > lastmax  & lastmin = tmin < lastmin  
;        endfor                 
;
;        !c = 0 		; undo box min/max
;
;        ; Be specific if log scale, if user isn't already forcing the Y-axis
;        if (scal eq 2) and (ylohi(0)+ylohi(1) eq 0) then begin
;		sav_ymin = ylohi(0)  &  sav_ymax = ylohi(1)
;		ylohi(0) = lastmin   &  ylohi(1) = lastmax
;        endif
;
;    endif


   ; 
   ; PLOT UVSP DATA
   ;


   uvspplot, struc=struc, data=data, abscissa=abscissa, plotover=plotover, $
			    hd=hd, $
			    p_scal=p_scal, one_det=one_det, c_det=c_det, $
			    hard=hard, mytick=mytick, mypsym=mypsym, $
			    ylohi=ylohi, xlohi=xlohi, nsome=nsome, timfil=timfil

;6/93    ;
;    ; if overplotting and program forced Y-axis then reset Y-axis
;    ;
;
;    if plotover eq 1 and ((scal eq 2) and (ylohi(0)+ylohi(1) eq 0)) then begin 
;	ylohi(0) = sav_ymin  
;	ylohi(1) = sav_ymax 
;    endif


   end

 1: begin      		; raster type

;6/93    if not(hard) then begin  			; will need a window
;       uvspwin, struc=struc, typwin=struc.st5.imagewin, xwin=512, ywin=512 
;       struc.st5.imagewin = !d.window
;    endif


    ;
    ; UVSP IMAGE DATA
    ;

    uvspimage, struc=struc, data=data, hd=hd, ave_sep=ave_sep, scal=scal, $
		contur=contur, silent=silent, solgrid=solgrid, rollo=rollo, $#
		smuth=smuth, point=point, $
		templev=templev, hard=hard, xlohi=xlohi, abscissa=abscissa   
    end

 else: widget_conrol, comment, set_value='UVSPPROCESS: ' + $
				'Invalid class of experiment.', /append
endcase                              

end
