;+
; NAME:
;	WPC
;
; PURPOSE:
;	This procedure sets up runs a simple event loop for
;       the Wavelet Packet Compression action. It is part
;		of the Wavelet Workbench functions.
;
; CATEGORY:
;	Wavelets.
;
; CALLING SEQUENCE:
;
;	WPC
;
; INPUTS:
;	none
;
; OUTPUTS:
;	This procedure runs the wavelet data input as well as the actions
;	performed on the data.
;
;
;COMMON BLOCKS:
;COMMON WWB_COMMON, $
;	TEXT_ANNOUNCE, wd, sig, len, st, noise_type, noise_lev, $
;	QMF, LD, shrinkage_type, WaveType, ParVal
;	TEXT_ANNOUNCE: The block for passing the widget that present
;	messages as well as other important wavelet parameters such
;	as the data array (sig), the data array length(len), a string 
;	describing the data (st) and the current chosen quadrature mirror 
;	filter (QMF).
;
;COMMON WPC_common, FSLID1, BUTTON1, BUTTON2, BUTTON17, $
;	wpcoef, btree, wcmsort, thr, threshslider
;	COMMON WPC_common, The block for the slider (FSLID1)
;	the two buttons (BUTTON1, BUTTON2) and 
;	array of wavelet coeffs (wpcoef), sorted array of wavelet coeffs (wcmsort), 
;	threshold value (thr), the current 
;	value of the slider (threshslider)
;
;
; MODIFICATION HISTORY:
; 	Written by:	Amara Graps, September 1995.
;-

;****************************************************************

PRO MAINWPC_Event, Event

;This routine handles the events from the Wavelet Transform
;Compression Widget.

COMMON WPC_common, FSLID1, BUTTON1, BUTTON2, BUTTON17, $
	wpcoef, btree, wcmsort, thr, threshslider
COMMON WWB_COMMON, $
	TEXT_ANNOUNCE, wd, sig, len, st, noise_type, noise_lev, $
	QMF, LD, shrinkage_type,WaveType, ParVal

type = TAG_NAMES(event, /structure_name)

;Color of text strings
IF !p.background EQ 0 THEN cl = 255 ELSE cl = 0


WIDGET_CONTROL,Event.Id,GET_UVALUE=Ev

CASE 1 OF

	( type eq 'WIDGET_BUTTON' ) and ( ev eq 'Done' ): BEGIN
		WIDGET_CONTROL, event.top, /destroy	
		END

	ELSE: BEGIN	
	
		CASE ev OF
			'FSLID1': BEGIN
				;Print, 'Event for Threshold Value'
				WIDGET_CONTROL, Event.Id, get_value=f
				threshslider = f
				thr = EXP(f)
				END
			'BUTTON1': BEGIN
				;Print, 'Event for Thresh/2'
				f = threshslider
				thr = EXP(f)/2
				f = ALOG(thr)
				END
			'BUTTON2': BEGIN
				;Print, 'Event for Thresh*2'
				f = threshslider
				thr = EXP(f)*2
				f = ALOG(thr)
				END
			'Compute': BEGIN
				;Print, 'Event for Compute'
				WIDGET_CONTROL, FSLID1, get_value=f
				threshslider = f
				wpcoef = WHARDTHR(wpcoef,thr);
	
				;Reconstruct the signal or data
				recon  = WIPTWP(btree,wpcoef,QMF)  
	
				;Set up the plot window
				;pt and d are dummy values
				pt = '' & d = FLTARR(2)
				WPLOTIT, pt, 6, d
	
				str_thresh = STRTRIM(STRING(threshslider), 2)
				signaltitle = 'WP Reconstruction (Thr='+str_thresh+') of '+ st 
				PLOT, recon, TITLE = signaltitle, color = cl
					
				end
		ENDCASE	;ev
		
	WIDGET_CONTROL, FSLID1, set_value=f
	threshslider = f
	

	END	;ELSE
		
ENDCASE		;CASE 1.. Checking buttons and menu items


END	;of procedure MAINWPC_Event

;****************************************************************

PRO WPC, GROUP=Group

;This procedure sets up the Wavelet Transform Compression widget.

COMMON WPC_common, FSLID1, BUTTON1, BUTTON2, BUTTON17, $
	wpcoef, btree, wcmsort, thr, threshslider
COMMON WWB_COMMON, $
	TEXT_ANNOUNCE, wd, sig, len, st, noise_type, noise_lev, $
	QMF, LD, shrinkage_type, WaveType, ParVal

;----------Prep work------------------

;Calculate Wavelet Packet Coefficients
Dsplit = 6	;fineness of the frequency splitting
wp = WPANALYS(sig,Dsplit,QMF)

;Set up the plot window
;pt and d are dummy values
pt = '' & d = fltarr(2)
WPLOTIT, pt, 3, d

;Do Packet Table plot
WPLPKTBL, wp,0,st

;Calculate Entropy and Find Best Basis
Entropy = 'Entropy'
ent_par = 0
stree = WSTATREE(wp,Entropy,ent_par)
btree = WBBASIS(stree,Dsplit,vtree)

;Get the coefficients
wpcoef = WUPKCOEF(btree,wp);

;Use some fraction of the coefficients
maxcoef = MAX(wpcoef)
wthresh = .05*maxcoef
wcmsort = WCOMBINE( REVERSE(WMSORT(ABS(wpcoef))) )
thr = wthresh

threshslider = ALOG(thr)
;----------------------------------------
			

IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0


MAINWPC = WIDGET_BASE(GROUP_LEADER=Group, $
	ROW=3, $
	MAP=1, $
	TITLE='Wavelet Packet Compression', $
	UVALUE='MAINWPC')

BASE2 = WIDGET_BASE(MAINWPC, $
	ROW=1, $
	MAP=1, $
	UVALUE='BASE2')

FSLID1 = CW_FSLIDER( BASE2, $
	DRAG=0, $
	TITLE='Threshold Value', $
	UVALUE='FSLID1', $
	EDIT=1, $
	VALUE= threshslider, $
	XSIZE=200, $
	MINIMUM=ALOG(wcmsort(0))-10, $
	MAXIMUM=ALOG(wcmsort(0)))

BUTTON1 = WIDGET_BUTTON( BASE2, $
	UVALUE='BUTTON1', $
	VALUE='Exp(Thresh)/2', $
	XSIZE=110, $
	YSIZE=40)

BUTTON2 = WIDGET_BUTTON( BASE2, $
	UVALUE='BUTTON2', $
	VALUE='Exp(Thresh)*2', $
	XSIZE=110, $
	YSIZE=40)
	
BUTTON6 = WIDGET_BUTTON( BASE2, $
      UVALUE='Done', $
      VALUE='Done')

BASE17 = WIDGET_BASE(MAINWPC, $
	ROW=1, $
	MAP=1, $
	TITLE='Compute', $
	UVALUE='BASE17')

BUTTON18 = WIDGET_BUTTON( BASE17, $
	UVALUE='Compute', $
	VALUE='Compute', $
	XOFFSET=50, $
	XSIZE=400, $
	YSIZE=20)


WIDGET_CONTROL, MAINWPC, /REALIZE

XMANAGER, 'MAINWPC', MAINWPC

END	;of Procedure WPC