;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; file UVSP.PRO - displays UVSP data
;
;	UVSP$DATA (ULTRIX: UVSP_DATA) can be define as data disk location
;	UVSP$CATALOG (ULTRIX: UVSP_CATLOG) can be defined a location and
;					   name of UVSP catalog
;
; A "-" means programs call consecutively
; A "->" means first program calls the second
;
; Upon entering the program control is given to the event handler
;    UVSP -> EVAL_EVENT (will process events as user clicks) -> 
;
; When an experiment is selected (list or manual):
;    UVSPREAD (->uvspdevck) - UVSPRECLAIM - UVSPSET - UVSPDEFAULTS -> UVSPPROCESS ->  ***
;     
; When "RESET" is clicked:
;    UVSPREAD (->uvspdevck) - UVSPDEFAULTS -> UVSPPROCESS -> ***
;
; When "DISPLAY" is clicked:
;    UVSPPROCESS -> ***
;    
; Then follows the UVSPPLOT path or the UVSPIMAGE path.     
;                        
;                        -> UVPSPLOT
;                       /
;                      /
;  *** -> UVSPPROCESS->                  -> UVSPSEPER
;                      \                /
;                       \              /
;                        -> UVSPIMAGE->
;                                      \
;                                       \
;                                        -> UVSPAVER (-> uvsprollit, uvspcondur)
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

pro eval_event, event

common order1, diskexp, control, generic, comment
common grouplead, base
common rastcomm, rastit, ras_lin,  ras_sqrt, ras_log, ras_rol,   ras_smuth, $
		         ras_con,  ras_ave,  ras_sep, ras_quiet, ras_loud,  $
                         ras_info

                                             
common locals, widgt,  expran, yaxbase, detcount, binbase, lastmexe
common clips, clipbase, clipit, clipmeth

common uvspsav, savexp, exptyp, saviwin, banner, savwinfile, $
		 widstatus, savsilent
common curser, topper, bottom, left, right, horz, hsiz, vsiz, m_m
common ctable, color_def
common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
common uvspcolors, norm_r, norm_g, norm_b, line_r, line_g, line_b, $
		   dopp_r, dopp_g, dopp_b, middopp

@uvspkeys.common
@uvspplotcom.common
@uvspstruct.common

 ;=============================================================================
 ; process widget choices
 ;=============================================================================

 widget_control, event.id, get_uvalue=input
 if n_elements(input) eq 0 then input=''
 type = strmid(tag_names(event, /structure_name),7,1000)

 case type of
    'BUTTON'   : case strmid(input,0,7) of
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;
	; General buttons 
	;
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        'GENKILL' : widget_control, event.top, /destroy ; kill top widget

        'NOTHING' : 		; do nothing, just a button used as an escape

	'DELWIN'  : winup	; get wiget that will allow window deletion

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;
	; Select a button that will get an experiment number
        ;
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	'TIME_IT' : begin
	   if (xregistered('EXPRAN')) then return	; already have it

	   repeat begin
		fail = 0
	   	twidget, rdate, year=[1980,1989], /month, /day,    $
				group_leader=base, error=error
		if error then return			; user did a cancel

		if (rdate.year lt 1980) or (rdate.year eq 1981) or $
		   (rdate.year eq 1982) or (rdate.year eq 1983) or $
		   (rdate.year gt 1989) then begin
			fail=1
			widget_control, comment, set_value='UVSP was not ' +$
				'active in ' + strtrim(rdate.year,2), /append
	        endif
	   endrep until not(fail)

           ;
           ; Have full date now so list experiments from that day
           ;

	   widget_control, comment, set_value='Looking for experiment(s).', $
							/append
           uvsplistexp, struc=struc, yr=rdate.year-1980, mth=rdate.month, $
					day=rdate.day, group_leader=base

	   end


        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;
        ; Options used by both raster and plot routines
        ;
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        'DISPLAY' : begin  	; processes the data with predefined flags
           widget_control, control, sensitive=0		; hand off until ready
	   UVSPPROCESS, struc=struc, data=data, hd=hd, ave_sep=ave_sep, $
			scal=scal, p_scal=p_scal, contur=contur, $
			silent=silent, rollo=rollo, cycle=cycle, $
			smuth=smuth, templev=templev, maxcyc=maxcyc, $
			exptyp=exptyp, timfil=timfil, $
			abscissa=abscissa, plotover=plotover, one_det=one_det, $
			c_det=c_det, mytick=mytick, $
			ylohi=ylohi, xlohi=xlohi, nsome=nsome, mypsym=mypsym

           widget_control, control, sensitive=1		; hand on again
	   end

        'DEFAULT' : begin   ; "RESET" button
	   widget_control, control, sensitive=0  ; no user until ready again

	   ; read in data again, data array may have been modified
	   UVSPREAD, struc=struc, data=data, hd=hd, status=status
           if status eq 0 then return		; bad reading on file
	   UVSPDEFAULTS, exptyp=exptyp, /reset

	   widget_control, control, sensitive=1  	; let user pick again
	   end        
	
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;
	; Make a print of one of the available IDL windows
	;
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	'BWCURRT' : begin 
	   colo = 0  &  curany=0  &  uvspprint, curany=curany
	   end
 	'CLCURRT' : begin 
	   colo = 1  &  curany=0  &  uvspprint, curany=curany
           end
	'BWANYWN' : begin 
	   colo = 0  &  curany=1  &  uvspprint, curany=curany
           end
	'CLANYWN' : begin 
	   colo = 1  &  curany=1  &  uvspprint, curany=curany
           end


	'HELLLLP'  : uvsphelp, group_leader=base

	'SAVFILE'  : uvspsave, file='UVSP'+struc.st6.expnu +'.DAT'


	;=====================================================================
	;
	; The following buttons deal with the pull down
	;	 menu "Window Functions"
	;
	;=====================================================================

	'PROTECT' : savexp = 1   ; save windows of current experiment
                                 ;   so program doesn't reuse them

	'REUSEWN' : savexp = 0   ; current exp will loss it's window when
				 ;   the next exp. is requested

	'SAVEWIN' : uvspsavres, 0

	'RESTWIN' : uvspsavres, 1

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 	;
	; Set variables/options and wait for next processing pass, 
	;	which will be at next 'Display experiment' 
        ; Some buttons may execute immediately (COLORTLB)
	;
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        'LINEARS' : if event.select ne 0 then scal = 0	; user selects linear
 	'SQUARRT' : if event.select ne 0 then scal = 1	; user selects sqr.root 
	'LOGRITH' : if event.select ne 0 then scal = 2	; user selects log 

        'RASTROL' : if event.select ne 0 then rollo=1 else rollo=0 

	'RASTSMO' : if event.select ne 0 then smuth=1 else smuth=0

	'RASTCON' : if event.select ne 0 then contur=1 else contur=0

	'RASTAVE' : if event.select ne 0 then ave_sep = 0 

	'RASTSEP' : if event.select ne 0 then ave_sep = 1

        'SSILENT' : if event.select ne 0 then silent = 1

	'LOUDMTH' : if event.select ne 0 then silent = 0

        'HISTOGM' : if event.select eq 0 then mypsym=0 else mypsym=10

	'PLTGRID' : if event.select eq 0 then mytick=0.02 else mytick=1

	'INFOPAG' : begin   	; produce a header text page 
	   widget_control, control, sensitive=0    ; give program a chance

	   ; get a window for header
           uvspwin, struc=struc, typwin=struc.st5.headwin, xwin=425, ywin=750
 	   struc.st5.headwin = !d.window

           if exptyp eq 0 then maxcyc=0	   	   ; plots only give 1 info page

	   UVSPFORM, struc=struc, data=data,   maxcyc=maxcyc, $
		      cycle=cycle, point=point, ih=hd,  lastmexe=lastmexe

	   if maxcyc ne 1 then begin		; there are multiple pages
	      if cycle-1 lt maxcyc and exptyp ne 0 then begin	
		 ; not flag page nor plot
	         widget_control, ras_info, set_value='More info' 
	      endif else begin
		 widget_control, ras_info, set_value='Info page'
              endelse
	   endif


	   widget_control, control, sensitive=1    ; allow access again
           end

        'CLIPBUT' : begin   	; get user's cutoff values for each detector
           clipit = strarr(hd(60))	; holds the clip value for each det.
           clipmeth = 0 		; default is data clipped to zero
	   clipbase = widget_base(title='Select detector then enter cutoff '+$
				'value.', /column, space=20, xpad=20, ypad=20)

	    junk = widget_label(clipbase, value='The procedure overwrites'+$
						   ' the data array.')
	    detlist = strarr(hd(60))  &  detorder = detlist
	    from = struc.st1.tempfrst - 1   & 	to = from+struc.st1.tempnfr-1 
            for d = 0, hd(60)-1 do begin
		detlist(d) = 'Detector ' + strtrim(hd(137+d),2) + '  (max: '+ $
		     strtrim(max(data(*,*,*,*,from:to,d)),2) +')'
                detorder(d) = strtrim(d,2)
            endfor
	    detbase = lonarr(hd(60))
	    xmenu, detlist, clipbase, /exclusive, /frame, $
			    button=detbase, uvalue='CLIPPIC' + detorder

            enter = widget_base(clipbase, /frame, /column)
             clipdet = widget_label(enter, value='Enter Clipping value ' + $
	          			'for choosen detector and RETURN: ')
             clipval = widget_text(enter, /editable, uvalue='CLIPVAL')
             
             meth = widget_base(clipbase, /column)
	      junk = widget_label(meth, value='Select clipping option')
              lent = widget_base(meth, /exclusive, /column, /frame)
	       zero = widget_button(lent, value='Set clipped data to zero.' , $
				     	uvalue='CLIPZER')
      	       input = widget_button(lent, value='Set clipped data to input '+$
				                 'value.', uvalue='CLIPINP')
             widget_control, zero, /set_button

           clipcan = widget_button(clipbase, value='Cancel',uvalue='GENKILL')
           done = widget_button(clipbase, value='Ready', uvalue='CLIPRUN')

           widget_control,  clipbase, /realize
	   widget_control,  detbase(0), set_button=1
    	   detcount = 0                              
	   xmanager,'eval', clipbase, event_handler='eval_event', /modal
                             
           end

        'CLIPPIC' : if event.select ne 0 then detcount = fix(strmid(input,7,1))
	'CLIPZER' : if event.select ne 0 then clipmeth=0
	'CLIPINP' : if event.select ne 0 then clipmeth=1
        'CLIPRUN' :  begin		; go alter the data
           widget_control, clipbase, /destroy 		; kill clip widget 
           uvspclip, struc=struc, data=data, hd=hd, exptyp=exptyp, glip=clipit
           end


	'COLORDF' : if event.select ne 0 then color_def = 1
	'COLORTB' : if event.select ne 0 then begin
	   		xloadct
	   		color_def = 0 
                    endif
	'COLORPL' : if event.select ne 0 then begin
	   		xpalette
	   		color_def = 0 
                    endif
	'LOADRED' : if event.select ne 0 then begin
			r_curr=norm_r &  g_curr=norm_g  &  b_curr=norm_b
			tvlct,r_curr,g_curr,b_curr
		    end
	'LOADDOP' : if event.select ne 0 then begin
			r_curr=dopp_r &  g_curr=dopp_g  &  b_curr=dopp_b
			tvlct,r_curr,g_curr,b_curr
		    end
	'LOADLIN' : if event.select ne 0 then begin
			r_curr=line_r &  g_curr=line_g  &  b_curr=line_b
			tvlct,r_curr,g_curr,b_curr
		    end

	'R_MAGNI' : begin      
	   widget_control, control, sensitive=0

           UVSPMAGNI, struc=struc, data=data, ave_sep=ave_sep,   $
			rollo=rollo, exptyp=exptyp, xlohi=xlohi, $
			abscissa=abscissa, timfil=timfil, magcase=0

	   widget_control, control, sensitive=1
           end

	'P_MAGNI' : begin      
	   widget_control, control, sensitive=0

           UVSPMAGNI, struc=struc, data=data, ave_sep=ave_sep, $
		      rollo=rollo, exptyp=exptyp, xlohi=xlohi, $
		      abscissa=abscissa, timfil=timfil, magcase=1

	   widget_control, control, sensitive=1
           end

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;
	; plotting options for raster images
	;
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	'PIXAREA' : begin
	   widget_control, control, sensitive=0
           uvsparea, struc=struc, data=data, hd=hd, ave_sep=ave_sep,  $
		       rollo=rollo, hard=hard, abscissa=abscissa, $
		       topper=topper, bottom=bottom, left=left, right=right, $
		       hsiz=hsiz, vsiz=vsiz, timfil=timfil, mypsym=mypsym, $
			mytick=mytick, p_scal=p_scal, ylohi=ylohi, xlohi=xlohi

	   widget_control, control, sensitive=1
	   end

	'SLICEIT' : begin
	   widget_control, control, sensitive=0
           uvspslice, struc=struc, data=data, hd=hd, ave_sep=ave_sep ,  $
		       rollo=rollo, hard=hard, horz=horz, $
		       topper=topper, bottom=bottom, left=left, right=right
	   widget_control, control, sensitive=1
	   end

	'RASTMAX' : begin
	   widget_control, control, sensitive=0
	   m_m = 0 					; flag as do max 
	   UVSPM_M, struc=struc, data=data, abscissa=abscissa, hd=hd, m_m=m_m
	   widget_control, control, sensitive=1
	   end

	'RASMEAN' : begin
	   m_m = 1 					; flag as do means
	   widget_control, control, sensitive=0
	   UVSPM_M, struc=struc, data=data, abscissa=abscissa, hd=hd, m_m=m_m
	   widget_control, control, sensitive=1
	   end

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;
        ; Movie options
	;
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	'SAVMOVI' : uvspmovie, struc=struc, movie=1   	; save single frame
	'SHOWMOV' : uvspmovie, struc=struc, movie=2   	; show the movie
	'RASTMOV' : uvspmovie, struc=struc, movie=3   	; show the movie


	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;
	; now the buttons for the PLOTIT widget that have not already been
	;    done above
	;
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        'PLINEAR' : if event.select ne 0 then p_scal = 0  ; user selects linear
	'PLOGRIT' : if event.select ne 0 then p_scal = 1  ; user selects log 

	; linear and log already done above

        'OVERDET' : if event.select ne 0 then begin	; overplot all det
         		  plotover = 1   &   one_det = 0  
	 	    endif

	'SEPDETR' : if event.select ne 0 then begin	; det in separate plots
           		plotover = 0   &   one_det = 0 
	   	    endif

	'NEXTDET' : if event.select ne 0 then begin	; plot next det
           		plotover = 0   &  one_det = 1   ; plot only one det.
	   	    endif


	'LITAUTO' : if event.select ne 0 then ylohi = [0L, 0L] ; auto y_axis

	'LITEMAN' : if event.select ne 0 then begin	; get user's ranges
	   yaxbase = widget_base(title='Select Y-axis range', /column, $
			         space=20, xpad=20, ypad=20)

	     group = widget_base(yaxbase, /frame, /column)
             yaxmin = widget_label(group, value='Enter Y-axis minimum and ' + $
						'RETURN')
             minval = widget_text(group, /editable, uvalue='AXISMIN')
             yaxmax = widget_label(group, value='Enter Y-axis maximum and ' + $
						'RETURN')
             maxval = widget_text(group, /editable, uvalue='AXISMAX')

           done = widget_button(yaxbase,    value='Ready', uvalue='GENKILL')
           clipcan = widget_button(yaxbase, value='Cancel',uvalue='GENKILL')

           widget_control, yaxbase, /realize
	   xmanager,'eval', yaxbase, event_handler='eval_event', /modal
           end


        ;======================================================================
        ; Options used by LIGHT routine 
        ;======================================================================

	'BINNING' : begin
	   binbase = widget_base(title='Bin data', /column, space=20, $
					xpad=20, ypad=20)
           enter=widget_base(binbase, /frame, /column)
             clipdet = widget_label(enter, value='Enter binning value and' + $
						 'RETURN ')
             clipval = widget_text(enter, /editable, uvalue='BINVALU')
           done = widget_button(binbase, value='Continue', uvalue='GENKILL')

           widget_control, binbase, /realize
	   xmanager,'eval', binbase, /modal
	   end


     	else  : widget_control, comment, set_value='ERROR-EVAL_EVENT, ' + $
			'Invalid button widget selection, ' + input, /append
     endcase

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

    'TEXT'   : begin
   	widget_control, event.id, get_value=value, set_value=''
        case input of 
           'MANOVER' :  begin
                ;==============================================================
                ; parse out disk location and exp # from manually input text 
                ;==============================================================

                value = strupcase(value(0))        ; input from text widget
                if value(0) ne '' then begin	   ; ignore returns
                   len = strlen(value(0))          ; length of user's input
		   pos = strpos(value(0),'.')      ; is there an extension

		   ; extract from 1st digit in exp. number to end of input
		   if pos eq -1 then begin start=len-5 & til=5  ; no ext.
		      endif else begin start=pos-5 & til=len-(pos-5) & endelse
 		   expnumb = strmid(value(0), start, til)
		   struc.st6.expnu = string(long(expnumb), '(i5.5)')
  
                   ; disk location (if any) will be 1st input character to 
		   ;   the 1st digit of number minus the the 'V' position.
	 	   ;   If a location must have a 'V'
		   struc.st6.disk = 'any location:'		; reset
		   endloc = start-1		; last chara. position of disk
		   if endloc gt 0 then $        ; is there a disk location
		      struc.st6.disk  = strmid(value(0), 0, endloc)

		   ;
                   ; read in the data file
		   ;

		   UVSPREAD, struc=struc, data=data, hd=hd, status=status
                   if status eq 0 then return		; bad reading on file

           	   widget_control, generic, sensitive=0  ; keep user away until
	   	   widget_control, rastit,  sensitive=0  ;  have new experiment
	   	   widget_control, plotit,  sensitive=0 	

	  	   UVSPRECLAIM, struc=struc, savexp=savexp
                   UVSPSET, struc=struc, data=data, hd=hd, exptyp=exptyp,  $
			    savexp=savexp, abscissa=abscissa, timfil=timfil, $
			    error=error
		   if error then return

		   ;
		   ; This will go call UVSPPROCESS
		   ;

 		   UVSPDEFAULTS, exptyp=exptyp

		   widget_control, generic, sensitive=1       	
		   widget_control, plotit, sensitive=1   
		
		   ; raster stuff
		   if exptyp eq 1 then widget_control, rastit, sensitive=1
                endif 


           	end

	   'AXISMIN' : ylohi(0) = value(0)
	   'AXISMAX' : ylohi(1) = value(0)
	   'CLIPVAL' : clipit(detcount) = value(0)  ; user is clipping the array

	   'BINVALU' : begin
	      nsome = value(0)
              widget_control, plt_bin, set_value='Bin [' + strtrim(nsome,2) + $
								']'
	      end

     	   else  : widget_control, comment, set_value='ERROR-EVAL_EVENT, '+ $
			'Invalid text widget selection, ' + input, /append
	endcase
    end       

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

    'LIST'   : begin
       value= input(event.index)
       myvalue = strmid(value,0,7) 			; case identifier
       value = strmid(value,7,strlen(value)) 		; item selected
       case myvalue of
             'EXPLIST' : begin   	
		;
		; User picked exp from exp. list
		;

                struc.st6.expnu = strmid(value,0,5)
	        struc.st6.disk  = 'any location:'	; reset

		;
                ; read in the data file
		;

		UVSPREAD, struc=struc, data=data, hd=hd, status=status
                if status eq 0 then return

           	widget_control, generic, sensitive=0 	; keep user away until
	   	widget_control, rastit,  sensitive=0 	;  have new experiment
	   	widget_control, plotit,  sensitive=0 
	
	   	UVSPRECLAIM, struc=struc, savexp=savexp
                UVSPSET, struc=struc, data=data, hd=hd, exptyp=exptyp, $
		     	 savexp=savexp, abscissa=abscissa, timfil=timfil, $	
			 error=error
		if error then return

		;
		; This will go call UVSPPROCESS
		;

 		UVSPDEFAULTS, exptyp=exptyp

		widget_control, generic, sensitive=1  
		widget_control, plotit, sensitive=1
		
		if exptyp eq 1 then widget_control, rastit, sensitive=1  
	     end
	
     	     else  : widget_control, comment, set_value='ERROR-EVAL_EVENT, '+$
		     			'Invalid list widget selection, ' + $
					input(event.index), /append
          endcase                                
        end

;==============================================================================
                                                                   
     'SLIDER': begin
                  if input eq 'DATE' then new_date = event.value
	       end

     else: widget_control, comment, set_value='ERROR-EVAL_EVENT, ' + $
			'Invalid type of widget, '+ type, /append
 endcase

end

;============================================================================
;============================================================================
;============================================================================
;
; Main level UVSP display program
;
;
;============================================================================
;============================================================================
;============================================================================

pro UVSP, group_leader=group 

common order1, diskexp, control, generic, comment
common grouplead, base               
common rastcomm, rastit, ras_lin,  ras_sqrt, ras_log, ras_rol,   ras_smuth, $
		         ras_con,  ras_ave,  ras_sep, ras_quiet, ras_loud,  $
                         ras_info

@uvspplotcom.common

common locals, widgt,  expran, yaxbase, detcount, binbase, lastmexe
common ctable, color_def
common uvspcolors, norm_r, norm_g, norm_b, line_r, line_g, line_b, $
		   dopp_r, dopp_g, dopp_b, middopp
common winkeep, lastmark, getdev_xsize, getdev_ysize 

@uvspkeys.common
@uvspstruct.common

 nsome  = 1
 if n_elements(mypsym) eq 0 then mypsym = 0	; 0 is normal, 10 is histrogram
 if n_elements(mytick) eq 0 then mytick = 0.02	; 0.02 is normal, 1 is grid

 ;
 ; make UVSP color tables
 ;

 uvspcolor, norm_r=norm_r, norm_g=norm_g, norm_b=norm_b, $
	    line_r=line_r, line_g=line_g, line_b=line_b, $
	    dopp_r=dopp_r, dopp_g=dopp_g, dopp_b=dopp_b, middopp=middopp

 color_def = 1	      		; use default color table until switched

 ;
 ; Set up the structure that will be passed thoughout the code, 
 ; containing current options, etc.
 ;
 
 uvspstruc, struc=struc 	; setup structure of structures

 ;
 ; The user's window size, and window positioning stuff
 ;

 device, get_screen_siz=sc		; sc(0) is x size, sc(1) is y size
 lastmark = 0	&  getdev_xsize = sc(0)  &  getdev_ysize = sc(1)
 
 ;
 ; Find the UVSP catalog location
 ;

 if !version.os eq 'vms' then catplace = 'UVSP$CATALOG' $
   else catplace = '/home/res/smm/sdac/uvsp/data/CATALOG.CAT' 

 logtest = getenv(catplace)			; is this defined

 if logtest ne '' then struc.st6.cat = logtest $	; use defined value
   else begin						; go find a catalog
    	file = 'CATALOG.CAT'
    	opdev = uvspdevck(file=file, comment=comment)	; look everywhere
  	struc.st6.cat = opdev + file
   endelse


;
;
; Create the widget
;
;

 mainbase = widget_base(title='UVSP workbench Version 1.2', /column, space=15)

 
 ;
 ; BASE contains three bases:  
 ;	      DISKEXP - select experiment and generic tool, etc.
 ;            CONTROL - data manipulation options
 ;            MESSAGE - program message window, to keep user informed
 ;

 base = widget_base(mainbase, /column, space=15) ; main base

 ;
 ; DISKEXP - select experiment and generic tool, etc.
 ;
 diskexp = widget_base(base, /column, space=15, /frame)

  instruct = widget_label(diskexp, $
	value='Manually input experiment number or select experiment by date')

  pickit = widget_base(diskexp, /row)

   manual = widget_base(pickit, /frame, /column)
      instruct1 = widget_label(manual, value= $ 
 			'[node::][disk:][directory][V]experiment and RETURN')
      man = widget_text(manual, /editable, uvalue='MANOVER', xsize=25, ysize=1)

   date_it = widget_button(pickit, value='   Select experiment by date   ', $
					uvalue='TIME_IT')

  holdpen = widget_base(diskexp, /row, space=30, xpad=20)
     quit = [ $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [224B ,007B ,030B ,222B ,255B ,251B ,127B ], $
                [248B ,031B ,030B ,222B ,255B ,251B ,127B ], $
                [248B ,031B ,030B ,222B ,255B ,251B ,127B ], $
                [252B ,063B ,030B ,222B ,255B ,251B ,127B ], $
                [060B ,060B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,120B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,123B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,127B ,030B ,030B ,060B ,128B ,007B ], $
                [030B ,126B ,030B ,030B ,060B ,128B ,007B ], $
                [060B ,062B ,030B ,030B ,060B ,128B ,007B ], $
                [252B ,127B ,062B ,223B ,255B ,131B ,007B ], $
                [248B ,255B ,252B ,207B ,255B ,131B ,007B ], $
                [248B ,239B ,252B ,207B ,255B ,131B ,007B ], $
                [224B ,199B ,248B ,199B ,255B ,131B ,007B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ], $
                [000B ,000B ,000B ,000B ,000B ,000B ,000B ]  $
                ]

     quit  = widget_button(holdpen, value=quit, uvalue='GENKILL')
   
	
     generic = widget_base(holdpen, /row, space=10) ;, ypad=15, xpad=20)
        gen1 = widget_button(generic, value = 'Display', uvalue='DISPLAY')
        gen2 = widget_button(generic, value = 'Reset ', uvalue = 'DEFAULT')

     	xpdmenu, $
	['"Window functions" {',	$
	    '"Delete windows" DELWIN', $
	    '"PROTECT experiment windows (next pass)" PROTECT',$
	    '"RECYCLE current experiment''s window(s)" REUSEWN',$
	    '"WRITE an existing IDL window to a file" SAVEWIN',  $
	    '"RESTORE a previously saved window (from above method)" RESTWIN', $
            '"EXIT" NOTHING', '}' ], generic 
	

     	xpdmenu, ['"TOOLS" {',	$
                 '"Print" {',	$
		       '"Black/White Print" {',	$
			  '"Current Data" BWCURRT',	 $
			  '"Any Window"   BWANYWN', 	$
			  '"EXIT"         NOTHING', '}',	$
		       '"Color Print" {',		$
			  '"Current Data" CLCURRT',	 $
			  '"Any Window"   CLANYWN', 	$
			  '"EXIT"         NOTHING', '}',	$
	                 '"EXIT" NOTHING', '}', $
		'"Color" {',	$
			'"Default colors (next pass)" COLORDF', $
			'"Load red color table" LOADRED', $
			'"Load doppler table" LOADDOP', $
			'"Load plot table" LOADLIN', $
			'"Use User''s Library color tables (XLOADCT)" COLORTB',  $
			'"Use IDL Palette to create table (XPALETTE)" COLORPL', $
			'"Exit" NOTHING',	'}', $
		'"Help" HELLLLP',	$
		'"Save data to file" SAVFILE',	$
	        '}' ], generic


 ;
 ; Display manipulation options use by raster and light curve
 ;
 ;    CONTROL bases:
 ;	 EACH    - options available to only one or the other data
 ;

;6/93 control = widget_base(base, /column, /frame)
 control = widget_base(base, /column)
                  
  ; 
  ; EACH contains 2 bases:
  ;	 RASTIT - control options for raster data
  ;	 PLOTIT - control options for plot data
  ;


  each = widget_base(control, /row, space=20)
                               
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   ;
   ; RASTIT (1st of 2 in EACH) contains to bases:
   ;   RAS_L - options that are set then data processed later
   ;   RAS_R - options that process the data immediately
   ;
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;6/93   rastit = widget_base(each, /frame, /row, xpad=10, ypad=10)
   rastit = widget_base(each, /frame, /column, xpad=10, ypad=10)

   lab = widget_label(rastit, value='Raster options:')
	   
   rasters = widget_base(rastit, /row)
   
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;
    ; RAS_L - left of 2 columns of raster options.  These set variables,
    ;	      but don't initiate display processing
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;6/93    ras_l = widget_base(rastit, /column)
    ras_l = widget_base(rasters, /column)

     ras_scl = widget_base(ras_l, /column, /exclusive, /frame)
       ras_lin  = widget_button(ras_scl, value='Linear scale', $
						uvalue='LINEARS')
       ras_sqrt = widget_button(ras_scl, value='Square root scale', $
						uvalue='SQUARRT')
       ras_log  = widget_button(ras_scl, value='Logrithmic scale', $ 
						uvalue='LOGRITH')

     ras_opt = widget_base(ras_l, /nonexclusive, /column, /frame)
       ras_rol = widget_button(ras_opt, value='Roll correction', $
				       uvalue='RASTROL')
       ras_smuth  = widget_button(ras_opt, value='Smooth image', $
				       uvalue='RASTSMO')
       ras_con  = widget_button(ras_opt, value='Contour image', $
				       uvalue='RASTCON')

     ras_avesep = widget_base(ras_l, /exclusive, /column, /frame)
      ras_ave = widget_button(ras_avesep, value='Average images',  uvalue='RASTAVE')
      ras_sep = widget_button(ras_avesep, value= 'Separate images', $
						uvalue='RASTSEP')

     ras_talk = widget_base(ras_l, /exclusive, /column, /frame)
      ras_quiet =widget_button(ras_talk, value='Silent running', $
					uvalue='SSILENT')
      ras_loud = widget_button(ras_talk, value='Inquisitive',   $
				        uvalue='LOUDMTH')


    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;
    ; RAS_R - right of 2 columns of raster options.  These buttons will 
    ;	      immediately initiate display processing of given function.
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;6/93    ras_r = widget_base(rastit, /column, space=5)
    ras_r = widget_base(rasters, /column, space=5)

     ras_info= widget_button(ras_r, value='Info page', uvalue='INFOPAG') 
     ras_clip = widget_button(ras_r, value = 'Clip data', uvalue = 'CLIPBUT')

     ras_zoom = widget_button(ras_r, value='Zoom', uvalue='R_MAGNI')
     xpdmenu, ['"Plotting" {',			$
	 	'"Plot pixel area" PIXAREA',		$
		'"Plot slice through image" SLICEIT',	$
                '"Plot raster maximums" RASTMAX',	$
                '"Plot raster means" RASMEAN', 	$
		'"Exit" NOTHING',	'}' ], ras_r 

; until this is corrected for widget version
     xpdmenu, ['"Movie" {',					$
		'"Save movie frame by frame" SAVMOVI',		$
		'"Run frame by frame movie" SHOWMOV',		$
;		'"Create and run a movie of repeated rasters" RASTMOV', $
		'"Exit" NOTHING', '}' ], ras_r

   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   ;
   ; PLOTIT (1st of 2 in EACH) contains to bases:
   ;   PLT_L - options that are set then data processed later
   ;   PLT_R - options that process the data immediately
   ;
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;6/93   plotit = widget_base(each, /frame, /row, xpad=10, ypad=10)
   plotit = widget_base(each, /frame, /column, xpad=10, ypad=10)
   
   lab = widget_label(plotit, value='Plot options:')	   

   plotters = widget_base(plotit, /row)

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;
    ; PLT_L - left of 2 columns of plot options.  These set variables,
    ;	      but don't initiate display processing
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;6/93    plt_l = widget_base(plotit, /column)
    plt_l = widget_base(plotters, /column)

     plt_scl = widget_base(plt_l, /column, /exclusive, /frame)
       plt_lin  = widget_button(plt_scl, value='Linear scale', uvalue='PLINEAR')
       plt_log  = widget_button(plt_scl, value='Logrithmic scale', $ 
						uvalue='PLOGRIT')

     plt_det = widget_base(plt_l, /exclusive, /column, /frame)
       plt_over = widget_button(plt_det, value='Overplot detectors', $
					uvalue='OVERDET')
       plt_sep  = widget_button(plt_det, value='Separate detectors', $
					uvalue='SEPDETR')
       plt_nex  = widget_button(plt_det, value='Plot next detector', $
					uvalue='NEXTDET')

     plt_axis = widget_base(plt_l, /column, /exclusive, /frame)
       plt_auto  = widget_button(plt_axis, value='Auto Y-axis scale', $
				          uvalue='LITAUTO')
       plt_axman = widget_button(plt_axis, value='Manual Y-axis scale', $
					  uvalue='LITEMAN')

;6/93     plt_talk = widget_base(plt_l, /exclusive, /column, /frame)
;      plt_quiet =widget_button(plt_talk, value='Silent running', $
;					uvalue='SSILENT')
;      plt_loud = widget_button(plt_talk, value= 'Inquisitive',   $
;				        uvalue='LOUDMTH')

     plt_talk = widget_base(plt_l, /nonexclusive, /column, /frame)
      plt_histo = widget_button(plt_talk, value='Bar Graph', $
					uvalue='HISTOGM')
      plt_grid = widget_button(plt_talk, value= 'Grid',   $
				        uvalue='PLTGRID')



    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;
    ; PLT_R - right of 2 columns of plot options.  These buttons will 
    ;	      immediately initiate display processing of given function.
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;6/93    plt_r = widget_base(plotit, /column, space=10)
    plt_r = widget_base(plotters, /column, space=10)

     plt_info = widget_button(plt_r, value='Info page', uvalue='INFOPAG') 
     plt_clip = widget_button(plt_r, value='Clip data', uvalue = 'CLIPBUT')
     plt_bin  = widget_button(plt_r, value='Bin [1]  ', uvalue='BINNING')
     plt_zoom = widget_button(plt_r, value='Zoom', uvalue='P_MAGNI')



 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;
 ; Make a text widget that will displays program output
 ;
 ;    MESSAGE (3rd of 3 in the main base)
 ;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 message = widget_base(mainbase, /column)
  junk=widget_label(message, value='Message window')
  comment = widget_text(message, /scroll, ysize=3) 


 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;=============================================================================
 ;
 ; Activate the widgets
 ;
 ;=============================================================================
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 widget_control, mainbase, /realize   	; Realize the widget

 widget_control, generic, sensitive=0   ; until user selects an experiment,
 widget_control, rastit,  sensitive=0   ;   they are not allowed to click
 widget_control, plotit,  sensitive=0 	;   on any of these data options. 


 openr, unit, struc.st6.cat, error=messup, /get   	; test open catalog
 if messup ne 0 then widget_control, comment, set_value = $
		'ERROR opening catalog: ' + struc.st6.cat, /append $
 else free_lun,unit                    	; close catalog

 xmanager,'eval', mainbase, group_leader=group 	; process widget selections

end

