;+
; NAME:
;	XCME_SUM
;
; PURPOSE:
;	This procedure is used to display the CME summary page.
;
; CATEGORY:
;	CME
;
; CALLING SEQUENCE:
;	XCME_SUM
;
; INPUTS:
;
; OPTIONAL INPUT PARAMETERS:
;	Filename:	If filename is present then it is used immediately
;
; OUTPUTS:
;
; OPTIONAL OUTPUT PARAMETERS:
;
; COMMON BLOCKS:
;	com_xcme_sum
;
; SIDE EFFECTS:
;	Initiates the XMANAGER if it is not already running.
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
; 	Written by:	RA Howard, NRL, 7 May 1997
;
; @(#)xcme_sum.pro	1.1 05/14/97 :NRL Solar Physics
;
;-

PRO DISPLAY_SUMMARY
;
;  Displays the CME summary information
;
common com_height_time,currfeat,currtime,filename,nht,tel,tai,feature,radius,pa, $
       nfeat,feat_description,ntime

common com_xcme_sum,wcurrhtlabl,winnum,fac_rsec,winsave,wmeas_list,wdate_label, $
	wtime_label,cme_summ,wtelibase,wclassbase,wdisconbase,wprombase,wcolorbase, $
	wpolarbase,weitbase,wyohkbase,wpaspanbase,wmaxaccbase,wcoeffsbase, curr_summ, $
	weruptbase,wxraybase

bl1 = '                            '
bl  = '          '
teli=[bl1+'C1'+bl+bl+'C2'+bl+bl+'C3', $
      ' Initial Date Time'+'  '+cme_summ.initial_date_1+'     '+ $
              cme_summ.initial_date_2+'     '+cme_summ.initial_date_3, $
      ' Final Date Time'+'  '+cme_summ.final_date_1+'     '+ $
              cme_summ.final_date_2+'     '+cme_summ.final_date_3  ]
WIDGET_CONTROL,wtelibase,set_value=teli
WIDGET_CONTROL,wclassbase,set_value=cme_summ.class
WIDGET_CONTROL,wcommbase,set_value=cme_summ.comment
WIDGET_CONTROL,wprombase,set_value=cme_summ.prominence
WIDGET_CONTROL,wyohkbase,set_value=cme_summ.yohkoh
WIDGET_CONTROL,weitbase,set_value=cme_summ.eit
WIDGET_CONTROL,wdisconbase,set_value=cme_summ.disconnect
WIDGET_CONTROL,wcolorbase,set_value=cme_summ.color_seq
WIDGET_CONTROL,wpolarbase,set_value=cme_summ.polariz
WIDGET_CONTROL,warbase,set_value=cme_summ.ar_region
WIDGET_CONTROL,wxraybase,set_value=cme_summ.goes_xray
WIDGET_CONTROL,weruptbase,set_value=cme_summ.epl_db

paspan = ['		INITIAL		FINAL', $
          'Pos Angle	'+cme_summ.initali_pa+'     '+cme_summ.final_pa, $
          'SPAN		'+cme_summ.initial_span+'     '+cme_summ.max_span, $
          'RADIUS	'+cme_summ.initial_height+'     '+cme_summ.max_height, $
          'SPEED	'+cme_summ.initial_speed+'     '+cme_summ.final_speed]
WIDGET_CONTROL,wpaspanbase,set_value=paspan

maxacc = ['MAX SPEED       '+cme_summ.max_speec, $
          'ACCELERATION    '+cme_summ.acceleration, $
          'ERROR ACCEL     '+cme_summ.acc_err, $
          'NPTS SPEED	   '+cme_summ.npts_speed, $
          'LIMB TIME (<3R) '+cme_summ.limb_time_low, $
          'LIMB TIME (>3R) '+cme_summ.limb_time_high]
WIDGET_CONTROL,wmaxaccbase,set_value=maxacc

coeffs = ['			COEFF		ERROR', $
          'LINEAR FIT - 0    '+cme_summ.linfit_0+'     '+cme_summ.linfit_err_0, $
          'LINEAR FIT - 1    '+cme_summ.linfit_1+'     '+cme_summ.linfit_err_1, $
          'QUADRATIC FIT - 0 '+cme_summ.quadfit_0+'     '+cme_summ.quadfit_err_0, $
          'QUADRATIC FIT - 1 '+cme_summ.quadfit_1+'     '+cme_summ.quadfit_err_1, $
          'QUADRATIC FIT - 2 '+cme_summ.quadfit_2+'     '+cme_summ.quadfit_err_2]
wcoeffsbase = widget_text(wrr42base,/frame,value=coeffs,xsize=60,ysize=6)

RETURN
END
;------------------------------------------------------------------------------
;	procedure plot_ht_ev
;------------------------------------------------------------------------------
; This procedure processes the events being sent by the XManager.
; This is the event handling routine for the plot_ht widget.  It is
; responsible for dealing with the widget events such as mouse clicks on
; buttons in the plot_ht widget.  The tool menu choice routines are
; already installed.  This routine is required for the plot_ht widget to
; work properly with the XManager.
;------------------------------------------------------------------------

PRO xcme_sum_ev, event

common com_height_time,currfeat,currtime,filename,nht,tel,tai,feature,radius,pa, $
       nfeat,feat_description,ntime

common com_xcme_sum,wcurrhtlabl,winnum,fac_rsec,winsave,wmeas_list,wdate_label, $
	wtime_label,cme_summ,wtelibase,wclassbase,wdisconbase,wprombase,wcolorbase, $
	wpolarbase,weitbase,wyohkbase,wpaspanbase,wmaxaccbase,wcoeffsbase, curr_summ, $
	weruptbase,wxraybase

WIDGET_CONTROL, event.id, GET_UVALUE = eventval		;find the user value
						                        	;of the widget where
													;the event occured
;print,eventval

CASE eventval OF

; here is where you would add the actions for your events.  Each widget
; you add should have a unique string for its user value.  Here you add
; a case for each of your widgets that return events and take the
; appropriate action.

  'update':   IF (curr_ht NE '') THEN BEGIN
                 READ_HT,htname,tai,rs,pa,comment,tel,feat,col,row,date_obs,time_obs
                 UPDATE_CME_SUMMARY,cme_summ
                 cme_summary=cme_summ
                 SAVE,curr_summ,cme_summary
                 display_summary
              ENDIF
  'readht':	BEGIN
                  curr_summ = pickfile (title='Select CME Summary File')
                  if (curr_summ ne '') THEN BEGIN
                     ff = FINDFILE(curr_summ)
                     IF (ff NE '')  THEN BEGIN
                        parts = str_sep(curr_summ,!delimiter)
                        filename = parts(n_elements(parts)-1)
                     ;
                     ;  Update the current file name
                     ;
                        widget_control,wcurrhtlabl,set_value=filename
                     ;
                     ; read in the file and plot the h-t values
                     ;
                        RESTORE,curr_summ
                        cme_summ = cme_summary
                     ENDIF ELSE cme_summ=GET_CME_SUMMARY()
                     htname = cme_summ.htname
                     IF (htname EQ '')  THEN BEGIN
                        htname = pickfile (title='Select Height-Time File')
                        cme_summ.htname = htname
                     ENDIF
                     ff=FINDFILE(htname)
                     IF (ff NE '') THEN BEGIN
                        READ_HT,htname,tai,rs,pa,comment,tel,feat,col,row,date_obs,time_obs
                        UPDATE_CME_SUMMARY,cme_summ
                     ENDIF
                  END
	 	END
   'COMMENT':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wcommbase,GET_VALUE=tmp
                   cme_summ.comment=tmp
		ENDIF
   'CLASS':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wclassbase,GET_VALUE=tmp
                   cme_summ.class=tmp
		ENDIF
   'DISCONN':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wdisconbase,GET_VALUE=tmp
                   cme_summ.disconn=tmp
		ENDIF
   'PROM':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wprombase,GET_VALUE=tmp
                   cme_summ.prom=tmp
		ENDIF
   'COLOR':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wcolorbase,GET_VALUE=tmp
                   cme_summ.color=tmp
		ENDIF
   'POLAR':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wpolarbase,GET_VALUE=tmp
                   cme_summ.polar=tmp
		ENDIF
   'EIT':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,weitbase,GET_VALUE=tmp
                   cme_summ.eit=tmp
		ENDIF
   'YOHKOH':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wyohkbase,GET_VALUE=tmp
                   cme_summ.yohkoh=tmp
		ENDIF
   'XRAY':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,wxraybase,GET_VALUE=tmp
                   cme_summ.goes_xray=tmp
		ENDIF
   'ERUPT':	IF (curr_summ NE '')  THEN BEGIN
                   WIDGET_CONTROL,weruptbase,GET_VALUE=tmp
                   cme_summ.epl_db=tmp
		ENDIF
  'done': 	BEGIN   ;save summary information
                   IF (curr_summ NE '') THEN BEGIN
                      cme_summary = cme_summ
                      SAVE,curr_sum,cme_summary
                   ENDIF
          	   WIDGET_CONTROL, event.top, /DESTROY	   
		END

  ELSE: MESSAGE, "Event User Value Not Found"	   ;When an event occurs
;in a widget that has no user value in this case statement, an error message 
;is shown
ENDCASE
;There is no need to "unregister" a widget application.  The
;XManager will clean the dead widget from its list.
;print,eventval
END ;=========== end of xcme_sum event handling routine task ============



;-------------------------------------------------------------------------
;	procedure xcme_sum
;-------------------------------------------------------------------------
; This routine creates the widget and registers it with the XManager.
; This is the main routine for the xcme_sum widget.  It creates the
; widget and then registers it with the XManager which keeps track of the
; currently active widgets.
;-------------------------------------------------------------------------
PRO xcme_sum, fname, GROUP = GROUP

common com_height_time,currfeat,currtime,filename,nht,tel,tai,feature,radius,pa, $
       nfeat,feat_description,ntime

common com_xcme_sum,wcurrhtlabl,winnum,fac_rsec,winsave,wmeas_list,wdate_label, $
	wtime_label,cme_summ,wtelibase,wclassbase,wdisconbase,wprombase,wcolorbase, $
	wpolarbase,weitbase,wyohkbase,wpaspanbase,wmaxaccbase,wcoeffsbase, curr_summ, $
	weruptbase,wxraybase

usefile = 0
filename=''
IF (N_PARAMS() EQ 1) THEN IF (fname NE '')  THEN usefile=1
IF (!D.WINDOW eq -1) THEN winsave=-1 else winsave=!D.WINDOW

IF (usefile EQ 1) THEN BEGIN
   filename = fname
   ff = FINDFILE(filename)
   sz = SIZE(ff)
   IF (sz(0) NE 0)  THEN BEGIN
      htname=filename
      READ_HT,htname,tai,rs,pa,comment,tel,feat,col,row,date_obs,time_obs
   ENDIF
ENDIF

fac_rsec = 6.96E5/3600.    ; factor to convert R/hour to km/sec
currtime=0

;    xcme_sum cannot have multiple copies running.

IF(XRegistered("xcme_sum") NE 0) THEN RETURN		;only one instance of
							;the plot_ht widget
							;is allowed.  If it is
							;already managed, do
							;nothing and return

;  Create the main base.  The ROW keyword arranges the widget visually.

;xcme_sumbase = WIDGET_BASE(TITLE = "CME SUMMARY",/row)

; To add other routines or remove any of these, remove them both below
; and in the xcme_sum_ev routine.


xcme_sumbase = widget_base(/column,  $
                      TITLE='          CME SUMMARY')
wtopbase = widget_base(xcme_sumbase,/col)
wcontrolbase = widget_base(wtopbase,/row,/frame, $
                          xpad=10,ypad=10,space=35)
wreadhtbase = widget_base(wcontrolbase,/row)
wreadhtbutton = widget_button(wreadhtbase,uvalue='readht', $
                      value='Read CME Summary File')
IF (filename EQ '') $
   THEN curr_summ='            unknown            ' $
   ELSE curr_summ=filename

wreadhtlabl = widget_label(wreadhtbase,/align_center, $
                     value='Current Summary File:')
wcurrhtlabl = widget_label(wreadhtbase,/align_center,value=curr_summ)
wupdhtbase = widget_base(wcontrolbase,/row)
wupdhtbutton = widget_button(wupdhtbase,uvalue='update', $
                      value='Reread Height-Time File')

wrow2base = widget_base(xcme_sumbase,/col)
teli=['			C1		C2		C3', $
      ' Initial Date Time	97/05/01 12:23:45	95/05/01 12:23:45	95/05/01 12:23:45', $
      ' Final Date Time']
wtelibase = widget_text(wrow2base,/frame,value=teli,xsize=80,ysize=3)

wrow3base = widget_base(xcme_sumbase,/col)
wrc31base = widget_base(wrow3base,/row)

wrrc31base = widget_base(wrc31base,/col)

wrrcc31base = widget_base(wrrc31base,/row)
wclasslabel = widget_label(wrrcc31base,value='CLASSIFICATION',xsize=144)
wclassbase = widget_text(wrrcc31base,/editable,uvalue='CLASS',xsize=10,ysize=1)

wrrcc32base = widget_base(wrrc31base,/row)
wdisconlabel = widget_label(wrrcc32base,value='DISCONNECTION',xsize=144)
wdisconbase = widget_text(wrrcc32base,uvalue='DISCONN',/editable,xsize=10,ysize=1)

wrrcc33base = widget_base(wrrc31base,/row)
wpromlabel = widget_label(wrrcc33base,value='PROMINENCE',xsize=144)
wprombase = widget_text(wrrcc33base,uvalue='PROM',/editable,xsize=10,ysize=1)

wrrcc34base = widget_base(wrrc31base,/row)
wcolorlabel = widget_label(wrrcc34base,value='COLOR SEQ',xsize=144)
wcolorbase = widget_text(wrrcc34base,uvalue='COLOR',/editable,xsize=10,ysize=1)

wrrcc35base = widget_base(wrrc31base,/row)
wpolarlabel = widget_label(wrrcc35base,value='POLARIZ SEQ',xsize=144)
wpolarbase = widget_text(wrrcc35base,uvalue='POLAR',/editable,xsize=10,ysize=1)

wrrc32base = widget_base(wrc31base,/col)


wrrcc36base = widget_base(wrrc32base,/row)
weitlabel = widget_label(wrrcc36base,value='     EIT',xsize=9*12)
weitbase = widget_text(wrrcc36base,uvalue='EIT',/editable,xsize=10,ysize=1)
wxraylabel = widget_label(wrrcc36base,value='     GOES XRAY',xsize=12*12)
wxraybase = widget_text(wrrcc36base,uvalue='XRAY',/editable,xsize=10,ysize=1)

wrrcc37base = widget_base(wrrc32base,/row)
wyohklabel = widget_label(wrrcc37base,value='     YOHKOH',xsize=9*12)
wyohkbase = widget_text(wrrcc37base,uvalue='YOHKOH',/editable,xsize=10,ysize=1)
weruptlabel = widget_label(wrrcc37base,value='     Ha EPL/DB',xsize=12*12)
weruptbase = widget_text(wrrcc37base,uvalue='ERUPT',/editable,xsize=10,ysize=1)

wrrcc38base = widget_base(wrrc32base,/row)
paspan = ['		INITIAL		FINAL', $
          'Pos Angle	', $
          'SPAN		', $
          'RADIUS	', $
          'SPEED	']
wpaspanbase = widget_text(wrrcc38base,/frame,value=paspan,xsize=50,ysize=4)

wrow4base = widget_base(xcme_sumbase,/col)
wrc41base  = widget_base(wrow4base,/row)
wrr41base = widget_base(wrc41base,/col)
maxacc = ['MAX SPEED    ', $
          'ACCELERATION ', $
          'ERROR ACCEL  ', $
          'NPTS SPEED	   ', $
          'LIMB TIME (<3R) ', $
          'LIMB TIME (>3R) ']
wmaxaccbase = widget_text(wrr41base,/frame,value=maxacc,xsize=30,ysize=6)

wrr42base = widget_base(wrc41base,/col)
coeffs = ['			COEFF		ERROR', $
          'LINEAR FIT - 0 ', $
          'LINEAR FIT - 1 ', $
          'QUADRATIC FIT - 0 ', $
          'QUADRATIC FIT - 1 ', $
          'QUADRATIC FIT - 2 ']
wcoeffsbase = widget_text(wrr42base,/frame,value=coeffs,xsize=60,ysize=6)

wrow5base = widget_base(xcme_sumbase,/col)
wcommbase  = widget_text(wrow4base,/editable,uvalue='COMMENT',xsize=60,ysize=5,/wrap)

wdonerowbase = widget_base(xcme_sumbase,/col)
wdonecontbase = widget_base(wdonerowbase,/row,/frame, $
                          xpad=10,ypad=10,space=35)
wdonebase = widget_base(wdonecontbase,/row)
wdonebutton = widget_button(wdonebase,uvalue='done', $
                      value='EXIT')


; Typically, any widgets you need for your application are created here.
; Create them and use plot_htbase as their base.  They will be realized
; (brought into existence) when the following line is executed.

WIDGET_CONTROL, xcme_sumbase, /REALIZE			;create the widgets
							;that are defined
winnum = !d.window
widget_control,wcurrhtlabl,set_value=filename

XManager, "xcme_sum", xcme_sumbase, $			;register the widgets
		EVENT_HANDLER = "xcme_sum_ev", $	;with the XManager
		GROUP_LEADER = GROUP			;and pass through the
							;group leader if this
							;routine is to be
							;called from some group
							;leader.
  IF (winsave GT -1)   THEN WSET,winsave

END ;================== end of xcme_sum main routine ====================
