;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;  UVSPIMAGE.PRO
;
;  Routine that manipulates the raster for imaging.  Raster repeats are
;		either averaged together or display as separate rasters.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


pro image_event, event
common order1, diskexp, control, generic, comment
common imager, imginp, vfr

@uvspstruct.common

 widget_control, event.id, get_uvalue=input
 if n_elements(input) eq 0 then input=''

 type = strmid(tag_names(event, /structure_name),7, 4)

 case type of
    'BUTT'   : case input of
        'IMAGECAN' : begin
	   imginp = 'def'			; reset to default detector
	   widget_control, event.top, /destroy  ; kill top widget
           end
        'IMAGE_GO'  : widget_control, event.top, /destroy     ; kill top widget

	; section for doppler display options
        'First det'	  : if event.select ne 0 then imginp = '1'
	'Second det'	  : if event.select ne 0 then imginp = '2'
	'Sum of'    	  : if event.select ne 0 then imginp = '3'
	'All Det'      	  : if event.select ne 0 then imginp = '4'
	'Sum and dif/sum' : if event.select ne 0 then imginp = '5' 

	; section for detector to display
        '1'   : if event.select ne 0 then imginp = input
	'2'   : if event.select ne 0 then imginp = input
	'3'   : if event.select ne 0 then imginp = input
	'4'   : if event.select ne 0 then imginp = input
	'all' : if event.select ne 0 then imginp = input
        else: 

    endcase
    'TEXT'   : begin
   	widget_control, event.id, get_value=value, set_value=''
	case input of
	   'REPTXT' : begin
	   	on_ioerror, TRYAGAIN
          	  if strtrim(value(0),2) eq '' then begin
                    imginp = 'def'   			   ; use default
		    return
		  endif
		  repin = fix(value(0))                    ; repeats entered 
	       	on_ioerror, null

    		if repin le struc.st7.nrpt and repin gt 0 then imginp=value(0)$
		else widget_control, comment, set_value='WARNING: '+$ 
				'Input number of repeats too large.', /append

		end

	   'FRAMTXT' : begin
	   	on_ioerror, TRYAGAIN
          	  if strtrim(value(0),2) eq '' then begin
                    imginp = 'def'   			   ; use default
		    return
		  endif
		  framit = fix(value(0))                    ; repeats entered 
	       	on_ioerror, null

  		if framit le vfr and framit gt 0 then imginp=value(0)$
		else widget_control, comment, set_value='WARNING: '+$ 
		      'Too many repeats to start here or less than 1.', /append

		end

        endcase
    end
 endcase

TRYAGAIN:
end

;=============================================================================

PRO uvspIMAGE, struc=struc, data=CEXP, hd=HD, ave_sep=AVE_SEP, scal=SCAL, $
	contur=CONTUR, silent=SILENT, solgrid=solgrid, rollo=ROLLO, $
	point=point, smuth=SMUTH, $
	templev=templev, hard=hard, xlohi=xlohi, abscissa=abscissa    

common order1, diskexp, control, generic, comment
common ctable, color_def
common imager, imginp, vfr
common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr

@uvspcolors.common

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;
 ; Called by routine RASTER
 ; Calls routine AVER, SEPER
 ;
 ; CEXP 	:  data array 
 ; HD 		:  the experiment header info.
 ; AVE_SEP 	:  type of display 0 = average all rasters into one image
 ;			           1 = display each raster separately
 ; SCAL 	:  scaling 0 = linear, 1 = square root, 2 = log.
 ; CONTUR	:  0 = don't contour over the image, 1 = contour over image
 ; SILENT	:  whether user is prompted for display info.
 ;				0 = ask user, 1 = don't ask user anything
 ; SOLGRID	:  0= no grid, 1=yes grid
 ; ROLLO	:  0 = just the -90 for uvsp is rotated, 1 = rotates image
 ;				so 0 degrees is up.
 ; SMUTH	:  0 = no array smoothing, 1 = smooth the array
 ; HARD		:  0 = no hard copy, 1 = hard copy
 ;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 if n_elements(hard) eq 0 then hard = 0


 ; Initialization used by both cases of AVE_SEP

 top_scale= !d.n_colors-2 		; max color minus 1 for white line

 siz = size(cexp) 	   
 struc.st7.nx = siz(1)                                ; the width dimension
 struc.st7.ny = siz(2)                                 ; the height dimension

 struc.st7.wava = 1  &  struc.st7.polo = 1
 struc.st7.nrpt = 1  &  struc.st7.ndet = 1

 if siz(0) ge 3 then begin
    struc.st7.wava = siz(3)      	 	      ; # wavelength dimensions
    if siz(0) ge 4 then begin
       struc.st7.polo = siz(4)			      ; # polarimeter dimensions
       if siz(0) ge 5 then begin
          struc.st7.nrpt = siz(5)	                ; # experiment repeats
          if siz(0) ge 6 then struc.st7.ndet = hd(60)   ; number of detectors
       endif
    endif
 endif

 roll = hd(24)/100.                     ; roll angle in degrees
 exp_type = hd(59)                      ; type of UVSP experiment

 ; doppergram variables
 struc.st4.sum = 0  &  struc.st4.all = 0  
 struc.st4.vel = 0  &  struc.st4.jdet = 0      


 ;
 ; 	Check for Dopplergram display
 ;

 IF (((hd(60) eq 2) or (hd(60) eq 4)) and  $
    ((exp_type eq 2) or (exp_type eq 4))) THEN struc.st4.twodet = 1 $
					  ELSE struc.st4.twodet = 0

 if not(silent) and not(hard) then $
	canit = uvspinquiz(struc=struc, hd=hd, type=0)

 if struc.st4.twodet then begin
    case struc.st1.tempipc of                   
         1: struc.st4.jdet = 1        	; 1st det. only
         2: struc.st4.jdet = 2		; 2nd det. only
         3: struc.st4.sum  = 1		; sum 1st and 2nd det/ 3rd + 4th
         4: struc.st4.all = 1		; show all det.
         5: begin
               struc.st4.sum = 1 & struc.st4.vel = 1	; show dopplergram
            end
    endcase 

    if struc.st4.jdet ne 0 then begin 
         ; only the one det. choosen
   	 struc.st7.ndet = 1  &  struc.st3.d1 = struc.st4.jdet-1  
         struc.st3.d2 = struc.st3.d1    
    endif else if struc.st4.sum and not(struc.st4.vel) then begin
         ; SUM of det. without velocities
         struc.st3.d1 = 0  &  struc.st3.d2 = struc.st7.ndet-1  
         struc.st7.ndet = 1      
    endif else begin
         ; use all det.
         struc.st3.d1 = 0  &  struc.st3.d2 = struc.st7.ndet-1 
    endelse
    
    struc.st3.tempnd = struc.st7.ndet             ; now let everyone else know
 endif 

;--

 ;
 ; Set plotting xrange (XLOHI)
 ;

 xlohi = [ abscissa(struc.st1.tempfrst-1), $
	   abscissa(struc.st1.tempfrst-1 + struc.st1.tempnfr-1) ]


 if struc.st1.tempscal ne scal then struc.st1.tempscal = scal ; changed scaling

 
 if color_def then begin	; if using default color tables, load now
   if not(struc.st4.vel) then begin
	r_curr=norm_r &  g_curr=norm_g  &  b_curr=norm_b   ; normal UVSP color 
   endif else begin
	r_curr=dopp_r &  g_curr=dopp_g  &  b_curr=dopp_b   ; doppler colors
   endelse

   r_orig=r_curr  &  g_orig=g_curr  &  b_orig=b_orig
   tvlct, r_curr, g_curr, b_curr
 endif
   

 case ave_sep of                

   ; 
   ; Averaged images display one averaged image for each detector.
   ; Separated images display each repeat, wavelength, and detector.
   ; 

   0 :  uvspaver,struc=struc, data=cexp, hd=hd, solgrid=solgrid, rollo=rollo, $
	     point=point, rolls=roll, $
	     smuth=smuth, contur=contur, silent=silent, $
	     top_scale=top_scale, templev=templev, hard=hard
   1 :  uvspseper, struc=struc, data=cexp, hd=hd, exp=exp_type, $
	     solgrid=solgrid, point=point, rollo=rollo,$
	     smuth=smuth, contur=contur, silent=silent, top_scale=top_scale, $
	     templev=templev, hard=hard
   else: 
 endcase	


return		; from image to raster
end		; end of image

