;+
;
; FILE HXISSETLEVEL.PRO -- includes CALCONLEV.PRO, SETLEVEL_EV, HXISSETLEVEL
;
;===========================================================================
;
; NAME:
;	CALCONLEV
;
; PURPOSE:
;	Calculate contour levels
;
; CATEGORY:
;	HXIS display
;
; CALLING SEQUENCE:
;	CALCONLEV
;
; CALLED BY:
;	SETLEVEL_EV
;
; CALLS TO:
;	none
;
; INPUTS:
;       none explicit, only through commons
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUT:
;       none explicit, only through commons
;
; OPTIONAL OUTPUT:
;	none
;
; COMMON BLOCKS:
;	HXISSETLEV	to pass contour level variables through XAMANAGER
;	HXISWIDGET	for the ID of the comment widget
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	Depending on the parameter set by the user (in SETLEVEL_EV)
;	the conour levels are chosen.
;
; MODIFICATION HISTORY:
;	DEC-92, Elaine Einfalt (HSTX) 
;-

pro calconlev
;
; Recalculate and write after each parameter change
;

@hxissetlev.common
@hxiswidget.common

if SPAC eq 0 then begin  				; log scale of levels
    ALEV = user_MAX / user_DIV ^ indgen(user_NLEV)
endif else if SPAC eq 1 then begin                      ; lin scale of levels
    ALEV = user_MAX - user_DIV * indgen(user_NLEV)
endif else begin                                        ; user input levels
    if n_elements(clev) ne 0 then alev = clev $
			     else alev = 0
endelse

if ABSV then begin  					; using data units
	LEVELS = ALEV  &  PLEVELS = LEVELS/oMAX   
endif else begin                                        ; using percentages
	PLEVELS = ALEV/100.  &  LEVELS = oMAX*PLEVELS  
endelse

A = where(oMIN le LEVELS and LEVELS le oMAX,NLEV)
if NLEV eq 0 then begin 
  widget_control, comment, set_value='No contour levels in data range',/append
  widget_control, conlev, set_value='none'
  conerror=1
  return
endif

LEVELS = LEVELS(A)  
LEVELS = LEVELS(sort(LEVELS))
PLEVELS = PLEVELS(A) 
PLEVELS = PLEVELS(sort(PLEVELS))

if ABSV then ALEV = LEVELS $
	else ALEV = 100*PLEVELS

A = ''  &  for I=0,NLEV-1 do A = A+'  '+strcompress(ALEV(I))
B = ['(Percentages) ','(Data units) ']
widget_control, conlev, set_value= B(ABSV) + A

the_levels = levels

return
end

;===========================================================================
;+
;
; NAME:
;	SETLEVEL_EV
;
; PURPOSE:
;	Event handler for HXISSETLEVEL
;
; CATEGORY:
;	HXIS display
;
; CALLING SEQUENCE:
;	SETLEVEL_EV, EVENT
;
; CALLED BY:
;	HXISSETLEVEL (through the XMANAGER)
;
; CALLS TO:
;	CALCONLEV
;
; INPUTS:
;       none explicit, only through commons
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUT:
;       none explicit, only through commons
;
; OPTIONAL OUTPUT:
;	none
;
; COMMON BLOCKS:
;	HXISSETLEV	to pass contour level variables through XAMANAGER
;	HXISWIDGET	for the ID of the comment widget
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	Recieve events from the XMANAGER and process.
;
; MODIFICATION HISTORY:
;	DEC-92, Elaine Einfalt (HSTX) 
;-

pro setlevel_ev, event

@hxissetlev.common
@hxiswidget.common

 widget_control, event.id, get_uvalue=input        ; user's name for widget

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

 case type of 						    ; (button, list...)

    'BUTT' :  begin
      if event.select eq 0 then return	; skip button turn offs
      case input of   			; which button was selected
        'CANCEL'  : begin
		      widget_control, event.top, /destroy   ; kill top widget
		      error = 1         ; will use default levels
		      return
		    end

	'READY'   : begin
		     widget_control, event.top, /destroy   ; kill top widget
		     error = 0
		     return
                    end
	'PERCENT' : begin
			absv = 0
			user_max = 100.
			widget_control, maxwidg, set_value='100 %'
		    end
	'DATA'    : begin
			absv = 1
			user_max = omax
			widget_control, maxwidg, set_value=string(omax,'(f10.4)')
		    end

	'LOG'     : begin
			spac = 0
			widget_control, xlev, sensitive=1 
			widget_control, mlev, sensitive=1 
			widget_control, dlev, sensitive=1 
			widget_control, elev, sensitive=0 
		    end
	'LIN'     : begin
			spac = 1
			widget_control, xlev, sensitive=1 
			widget_control, mlev, sensitive=1 
			widget_control, dlev, sensitive=1 
			widget_control, elev, sensitive=0 
		    end
	'ARB'     : begin
			spac = 2
			widget_control, xlev, sensitive=0 
			widget_control, mlev, sensitive=0 
			widget_control, dlev, sensitive=0 
			widget_control, elev, sensitive=1 
		    end

        else:
      endcase
    end

    'TEXT' :  begin
        error = 0
	widget_control, event.id, get_value=value, set_value=''
	case input of
	   'NLEVELS' : begin

		        on_ioerror, BADLEV
			user_nlev = fix(value(0))
			on_ioerror, null
			user_nlev = 1 > user_nlev < 16
			widget_control, numwidg, set_value=strtrim(user_nlev,2)
		       end

	   'MAXCON' :  begin

		        on_ioerror, BADMAX
			user_max = float(value(0))
			on_ioerror, null
			if absv then user_max = omin > user_max < omax  $
				else user_max = 0. > user_max < 100.
			widget_control, maxwidg, $
					set_value=string(user_max, '(f10.4)')
		       end

	   'DSPACE' :  begin

		        on_ioerror, BADDIV
			user_div = float(value(0))
			on_ioerror, null
			user_div = 1 > user_div < omax
			widget_control, divwidg, $
					set_value=string(user_div, '(f10.4)')
		       end
	    'MYLEVELS' : begin

    			ALEV = fltarr(16)
			clev = value(0) + ','
			clen = strlen(clev)

			if clen eq 0 then return
			spos = 0   &   count = 0

			repeat begin
		   		epos = strpos(clev, ',', spos)
		   		alev(count) = strmid(clev, spos, epos-spos)
				spos = epos + 1
		   		count = count + 1
	       		endrep until (spos ge clen)

			cLEV = ALEV
			error = 0
            	       end
	    else :
	endcase
    end

    else:

 endcase

 ;
 ; Recalculate and write after each parameter change
 ;
 calconlev

return

BADLEV:
			user_nlev = nlev	; reset to original
			widget_control, numwidg, set_value=strtrim(user_nlev,2)
			error = 1
			return
BADMAX:
    			if ABSV then user_max = oMAX $
	    			else user_max = 100.
			widget_control, maxwidg, $
				set_value=string(user_max, '(f10.4)')
			error = 1
			return
BADDIV:
			user_div = div
			widget_control, divwidg, $
					set_value=string(user_div, '(i6)')
			error = 1

return
end

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

;+
;
; NAME:
;	HXISSETLEVEL
;
; PURPOSE:
;	Selected contour levels
;
; CATEGORY:
;	HXIS display
;
; CALLING SEQUENCE:
;	HXISSETLEVEL, LEVELS=levels, min=LMIN, max=LMAX, log=log, 
;		      im_num=ibs, error=error
;
; INPUTS: 
;	LMIN   :  minimum data value
;	LMAX   :  maximum data value
;	LOG    :  the current program scaling request (INFO.SCAL)
;	IM_NUM :  the loop counter from calling routine
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;	LEVELS :  the selected contour levels
;	ERROR  :  return 0 if sucessfully completed, else 1 if failed
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	hxisdata_info.common
;	hxissetlev.common
;	hxiswidget.common
;
; SIDE EFFECTS:
;	Create a widget.
;
; RESTRICTIONS:
;	none
;	
; PROCEDURE:
;	Create a widget that allows the user to select contour levels for
;	image display.
;
; MODIFICATION HISTORY:
;	DEC-1992, Elaine Einfalt (HSTX) - converted to widgets
;-

pro HXISSETLEVEL, min=LMIN, max=LMAX, levels=LEVELS, $
		  log=LOG,  im_num=ibs, error=conerror

@hxisdata_info.common
@hxissetlev.common
@hxiswidget.common

;
; Defaults
;

absv = 0   &   nlev = 6   &   omax = float(lmax)   &   omin = float(lmin)

spac = 1 - info.scal 			; 0=log; 1=lin (opposite of scal)
VMAX = 100.                     	; iniitialize as percentages
div  = 5 

; starting values
user_nlev = nlev   &   user_max = vmax   &   user_div=div

levbase = widget_base(title='HXIS MANUAL CONTOUR LEVELS', /column, $
				xpad=20, ypad=20, space=25)

 instr = widget_base(levbase, /column)
  lab = widget_label(instr, value='Contours for image ' + strtrim(ibs+1,2) )
  lab = widget_label(instr, value='Select the your choice of parameters')
  lab = widget_label(instr, value='Data range is ' + $
				string(oMIN, '(f10.4)') + $
				' to ' + string(oMAX, '(f10.4)') )
  bel = widget_base(instr, /column, /frame)
    lab = widget_label(bel, value='Current contour levels are :')
    conlev = widget_text(bel, /scroll, value='none', ysize=1)

AGAIN:

  row = widget_base(levbase, /row, space=20)
  
  r1 = widget_base(row, /column)
    lab = widget_label(r1, value='Contour level units')
    c2r1 = widget_base(r1, /column)
      choice_1 = widget_base(c2r1, /exclusive, /frame)
         per = widget_button(choice_1, value='Percentages', uvalue='PERCENT')
         dat = widget_button(choice_1, value='Data units', uvalue='DATA')

  r2 = widget_base(row, /column)
    lab = widget_label(r2, value='Contour spacing')
    c2r2 = widget_base(r2, /column)
      scl = lonarr(3)
      choice_2 = widget_base(c2r2, /exclusive, /frame)
         scl(0) = widget_button(choice_2, value='Logarithmic', uvalue='LOG')
         scl(1) = widget_button(choice_2, value='Linear', uvalue='LIN')
         scl(2) = widget_button(choice_2, value='Individually selected', $
					uvalue='ARB')
    xlev = widget_base(levbase, /column)
     f1 = widget_base(xlev, /row)
      	lab = widget_label(f1, value='Number of levels is')
      	numwidg = widget_label(f1,  value=strtrim(user_nlev,2) )
     f2 = widget_base(xlev, /row)
      	lab = widget_label(f2, value='Change number of levels :')
      	numtext = widget_text(f2, /editable, ysize=1, uvalue='NLEVELS')
   
    mlev = widget_base(levbase, /column)
     m1 = widget_base(mlev, /row)
      	lab = widget_label(m1, value='Maximum contour level is')
      	maxwidg = widget_label(m1,  value=strtrim(user_max,2) )
     m2 = widget_base(mlev, /row)
      	lab = widget_label(m2, value='Change maximumn level :')
      	maxtext = widget_text(m2, /editable, ysize=1, uvalue='MAXCON')

    dlev = widget_base(levbase, /column)
     d1 = widget_base(dlev, /row)
      	lab = widget_label(d1, value='Spacing between divisions is')
      	divwidg = widget_label(d1,  value=strtrim(user_div, 2) )

     d2 = widget_base(dlev, /row)
      	lab = widget_label(d2, value='Change spacing of levels :')
      	divtext = widget_text(d2, /editable, ysize=1, uvalue='DSPACE')

    elev = widget_base(levbase, /column)
     lab = widget_label(elev, value='Enter levels separated by ' +$
				     'commas and press RETURN')
     lab = widget_label(elev, value='Up to 16 contours levels available')

     e2 = widget_base(elev, /row)
      	lab = widget_label(e2, value='Enter levels here : ')
      	enttext = widget_text(e2, /editable, /scroll,ysize=1, uvalue='MYLEVELS')

    canit = widget_button(levbase, value='CANCEL', uvalue='CANCEL')
    doit  = widget_button(levbase, value='READY',  uvalue='READY')

  wait,1					; let Xmanager catch up
  widget_control, levbase, /realize

  widget_control, per, /set_button       	; start with percentages
  widget_control, scl(spac), /set_button	; start with image scaling
  widget_control, elev, sensitive=0		; not entering yet

  ;
  ; Calculate and write initial parameters
  ;
  calconlev

  xmanager, 'setlevel', levbase, event_handler='setlevel_ev', /modal

conerror = error				; pass back error status

if error then return				; was cancelled by user
if spac eq 0 then log = 1 $   			; spacing is log
	     else if spac eq 1 then log = 0 $	; spacing is lin
			       else log = -1	; spacing is up to the user

levels = the_levels		; pass back the contour levels

return  &  end