;+
; Project     : SOHO - CDS     
;                   
; Name        : SEL_PLOT_EVENT
;               
; Purpose     : Processes events from the SEL_PLOT_MENU.
;               
; Explanation : Any events originating within the widgets realized by 
;               SEL_PLOT_MENU are handled by this routine.
;               
; Use         : Only by XMANAGER
;    
; Inputs      : ev  -  the event structure
;               
; Opt. Inputs : None
;               
; Outputs     : None
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : None
;               
; Restrictions: For use with SEL_PLOT_MENU.
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 29-Sep-94
;               
; Modified    : Add sorting of time values before plotting. CDP, 7-Dec-94
;               Handle uncalibrated data.  CDP, 6-Jan-95
;               Handle new CANCEL button.  CDP, 18-Jan-95
;               Tile layout of plot windows.  CDP, 1-Jun-95
;               Cater for type 7 calibration (log plot). CDP, 18-Sep-95
;               Allow non-time plotting.    CDP, 22-Nov-95
;               Add plot zooming feature.  CDP, 05-Dec-95
;               Fix bug of duplicate widget IDs.  CDP, 13-Dec-95
;               Include CCD voltage to temperature calibration.  CDP, 2-Feb-96
;               Change utplot name.  CDP, 2-Oct-96
;               Fix IDLv5 change to WDELETE use. CDP< 30-9-99
;
; Version     : Version 12, 30-Sep-99
;-            

PRO sel_plot_event, ev

;
;  COMMON block for variables
;
@emon.com
common cds_utplot_com, cds_xoffset

      
;
;  interpret type of event
;
type = TAG_NAMES(ev, /STRUCTURE)

;
;  could have been a BUTTON
;                    ======
;
IF (type EQ 'WIDGET_BUTTON') THEN BEGIN
   WIDGET_CONTROL, ev.id, GET_VALUE=value, get_uvalue=uvalue
 
;
;  Done (ie exit) request received which triggers plotting of chosen 
;  parameters
; 
   if (uvalue EQ 'Done') then begin
      plot_parms(*) = ''
      WIDGET_CONTROL, sel_plot_base, /destroy
      widget_control, main_base, sens=1
   endif

;
;  (R)ZOOM or PLOT
;
;  zoom in on the plot in time (or datum) space
;
   if (uvalue eq 'Zoom') or (uvalue eq 'Plot') or (uvalue eq 'RZOOM') then begin
     if (uvalue eq 'Zoom') then begin
        widget_control, rzoom_id, sens=1
        nw = where(plot_parms ne '')
        if nw(0) ge 0 then nw = n_elements(nw) else nw = 0
        wset,pwind_id((nw-1) > 0)
        wshow
        if emon_plottd then begin
           xyouts,0.4,0.8,'Use cursor to redefine time limits',/normal,chars=1.5
        endif else begin
           xyouts,0.4,0.8,'Use cursor to redefine data limits',/normal,chars=1.5
        endelse
        cursor,t1,y,3,/data
        if emon_plottd then begin
           eplot_tmin = sec2utc(t1 + cds_xoffset)
           cds_outplot,[eplot_tmin,eplot_tmin],[-1000,10000],line=2
        endif else begin
           oplot,[t1,t1],[0,10000],line=2
           eplot_dmin = t1
        endelse
        cursor,t2,y,3,/data
        if emon_plottd then begin
           eplot_tmax = sec2utc(t2 + cds_xoffset)
           cds_outplot,[eplot_tmax,eplot_tmax],[-1000,10000],line=2

           t1 = utc2tai(eplot_tmin)
           t2 = utc2tai(eplot_tmax)
           if t1 eq t2 then begin
              eplot_tmin = str2utc(ut_data(0))
              eplot_tmax = str2utc(ut_data(n_elements(ut_data)-1))
           endif
           if t2 lt t1 then begin
              temp = t1
              t1=t2
              t2 = temp
              eplot_tmin = tai2utc(t1)
              eplot_tmax = tai2utc(t2)
           endif
        endif else begin
           oplot,[t2,t2],[0,10000],line=2
           eplot_dmax = t2
           if eplot_dmax eq eplot_dmin then begin
              eplot_dmin = 0
              eplot_dmax = n_elements(ut_data)
           endif
           if eplot_dmax lt eplot_dmin then begin
              temp = eplot_dmin
              eplot_dmin = eplot_dmax
              eplot_dmax = temp
           endif
        endelse
        wait,1.5
     endif

;
;  reset zoom parameters
;
     if uvalue eq 'RZOOM' then begin
        widget_control, rzoom_id, sens=0
        if emon_plottd then begin
           eplot_tmin = str2utc(ut_data(0))
           eplot_tmax = str2utc(ut_data(n_elements(ut_data)-1))
        endif else begin
           eplot_dmin = 0
           eplot_dmax = n_elements(ut_data)
        endelse
     endif
 
;
;  Plot chosen parameter data
;
     np = n_elements(pwind_id)

     DEVICE, WINDOW_STATE=STATE
     IF N_ELEMENTS(STATE) GT 32 THEN FOR I_WIN=32,N_ELEMENTS(STATE)-1 DO $
        IF STATE(I_WIN) THEN WDELETE, I_WIN

     nw = where(plot_parms ne '')
     if nw(0) ge 0 then begin
        widget_control, zoom_id, sens=1
        nw = n_elements(nw)
        device,get_screen_size=sz
        pwind_id = intarr(nw)
        for i=0,nw-1 do begin
           window,i,xs=1000,ys=250,/free, $
                title='CDS Engineering telemetry monitor',$
                xpos = i*25, ypos=sz(1)-i*60-590
           pwind_id(i) = !d.window
           name = plot_parms(i)
           it = where(wanted_parms eq name) & it = it(0)
           circle_sym, /fill
           title = 'Engineering '+wanted_packets+': parameter - '+name
;
;  better sort in case of funnies seen at Matra
;
           if emon_plottd then begin
              utt = utc2sec(str2utc(ut_data,/dmy))
              nutt = sort(utt)
              uts = ut_data(nutt)
              pds = parm_data(it,nutt)             
              if eplot_tmin.mjd lt 0 then begin
                 eplot_tmin = str2utc(ut_data(0))
                 eplot_tmax = str2utc(ut_data(n_elements(ut_data)-1))
              endif
           endif else begin
              pds = parm_data(it,*)
              if eplot_dmin lt 0 then begin
                 eplot_dmin = 0
                 eplot_dmax = n_elements(pds)
              endif
           endelse

;
;  attempt to calibrate the data
;
           pds = tm_calib(name,pds,words)

;
;  if it is the CCD temperature, apply secondary calibration of 
;  voltage --> degrees
;
           if name eq 'ATVDIODE' then begin
              pds = (pds + 0.410) / 0.00278
           endif

;
;  set up fancy axis labels
;
      case words(0) of
         'title':  begin
                      yt = strtrim(words(1),2)
                      yr=ext_range(pds,100)
                      if emon_plottd then begin
                         cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                 symsize=0.3, ytitle=yt,yrange=yr,chars=1.2,$
                                 timerange=[eplot_tmin,eplot_tmax]
                         if hard then begin
                            ps
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3,ytitle=yt,yrange=yr,chars=1.2,$
                                    timerange=[eplot_tmin,eplot_tmax]
                            psplot
                         endif
                      endif else begin
                         plot, pds, psym=8, title=title, xtit='Datum',$
                                 symsize=0.3, ytitle=yt,yrange=yr,chars=1.2,$
                                 xran=[eplot_dmin,eplot_dmax]
                         if hard then begin
                            ps
                            plot, pds, psym=8, title=title, xtit='Datum',$
                                    symsize=0.3,ytitle=yt,yrange=yr,chars=1.2,$
                                    xran=[eplot_dmin,eplot_dmax]
                            psplot
                         endif
                      endelse
                   end

         'Ltitle':  begin
                      yt = strtrim(words(1),2)
                      yr=ext_range(pds,100)
                      if emon_plottd then begin
                         cds_utplot, uts, pds, /dmy, psym=8, title=title, /log,$
                                 symsize=0.3, ytitle=yt,yrange=yr,chars=1.2,$
                                 timerange=[eplot_tmin,eplot_tmax]
                         if hard then begin
                            ps
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3,ytitle=yt,yrange=yr,$
                                    chars=1.2, /log,$
                                    timerange=[eplot_tmin,eplot_tmax]
                            psplot
                         endif
                      endif else begin
                         plot_io, pds, psym=8, title=title, xtit='Datum',$
                                 symsize=0.3, ytitle=yt,yrange=yr,chars=1.2,$
                                 xran=[eplot_dmin,eplot_dmax]
                         if hard then begin
                            ps
                            plot_io, pds, psym=8, title=title, xtit='Datum',$
                                    symsize=0.3,ytitle=yt,yrange=yr,$
                                    chars=1.2,$
                                    xran=[eplot_dmin,eplot_dmax]
                            psplot
                         endif
                      endelse
                   end

   'Uncalibrated': begin            
                      yt = strtrim(words(1),2)+' (Error)'
                      yr=ext_range(pds,100)
                      if emon_plottd then begin
                         cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                 symsize=0.3, ytitle=yt,yrange=yr,chars=1.2,$
                                 timerange=[eplot_tmin,eplot_tmax]
                         if hard then begin
                            ps
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3,ytitle=yt,yrange=yr,chars=1.2,$
                                    timerange=[eplot_tmin,eplot_tmax]
                            psplot
                         endif
                      endif else begin
                         plot, pds, psym=8, title=title, xtit='Datum',$
                                 symsize=0.3, ytitle=yt,yrange=yr,chars=1.2,$
                                 xran=[eplot_dmin,eplot_dmax]
                         if hard then begin
                            ps
                            plot, pds, psym=8, title=title, xtit='Datum',$
                                    symsize=0.3,ytitle=yt,yrange=yr,chars=1.2,$
                                    xran=[eplot_dmin,eplot_dmax]
                            psplot
                         endif
                      endelse
                   end     

          'Label': begin
                      nw = n_elements(words)
                      yt = words(nw-1)
                      yr = [0,nw-1]
                      if emon_plottd then begin
                         cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                 symsize=0.3, ytitle=yt,yrange=yr,$
                                 ystyle=1,yticks=nw-1,chars=1.1,$
                                 ytickname=[' ',words(1:nw-2),' '],$
                                 timerange=[eplot_tmin,eplot_tmax]
                         if hard then begin
                            ps
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3,ytitle=yt,yrange=yr,$
                                    chars=1.1,yticks=nw-1,ystyle=1,$
                                    ytickname=[' ',words(1:nw-2),' '],$
                                    timerange=[eplot_tmin,eplot_tmax]
                            psplot
                         endif
                      endif else begin
                         plot, pds, psym=8, title=title, xtit='Datum',$
                                 symsize=0.3, ytitle=yt,yrange=yr,$
                                 ystyle=1,yticks=nw-1,chars=1.1,$
                                 ytickname=[' ',words(1:nw-2),' '],$
                                 xran=[eplot_dmin,eplot_dmax]
                         if hard then begin
                            ps
                            plot, pds, psym=8, title=title, xtit='Datum',$
                                    symsize=0.3,ytitle=yt,yrange=yr,$
                                    chars=1.1,yticks=nw-1,ystyle=1,$
                                    ytickname=[' ',words(1:nw-2),' '],$
                                    xran=[eplot_dmin,eplot_dmax]
                            psplot
                         endif
                      endelse
                   end
            else:  begin
                      yt = strtrim(words(2),2)
                      yr = [-1,2]
                      ytn=[' ',words(0),words(1),' ']
                      if emon_plottd then begin 
                         cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                 symsize=0.3, ytitle=yt,yrange=yr,$
                                 ytickname=ytn,chars=1.2,$
                                 timerange=[eplot_tmin,eplot_tmax]
                         if hard then begin
                            ps
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3,ytitle=yt,yrange=yr,$
                                    ytickname=ytn,chars=1.2,$
                                    timerange=[eplot_tmin,eplot_tmax]
                            psplot
                         endif
                      endif else begin
                         plot, pds, psym=8, title=title, xtit='Datum',$
                                 symsize=0.3, ytitle=yt,yrange=yr,$
                                 ytickname=ytn,chars=1.2,$
                                 xran=[eplot_dmin,eplot_dmax]
                         if hard then begin
                            ps
                            plot, pds, psym=8, title=title, xtit='Datum',$
                                    symsize=0.3,ytitle=yt,yrange=yr,$
                                    ytickname=ytn,chars=1.2,$
                                    xran=[eplot_dmin,eplot_dmax]
                            psplot
                         endif
                      endelse
                   end
      endcase
         
       
         endfor
     endif else begin
         widget_control, msg_widg, set_val='No parameters.', /append
     endelse
   ENDIF

;
;  Reset request received
; 
   if (uvalue EQ 'Reset') then begin
      plot_parms(*) = ''

      DEVICE, WINDOW_STATE=STATE
      IF N_ELEMENTS(STATE) GT 32 THEN FOR I_WIN=32,N_ELEMENTS(STATE)-1 DO $
         IF STATE(I_WIN) THEN WDELETE, I_WIN

      if emon_plottd then begin
         eplot_tmin = {CDS_INT_TIME,mjd:-1L,time:0L}
         eplot_tmax = {CDS_INT_TIME,mjd:-1L,time:0L}
      endif else begin
         eplot_dmin = -1
         eplot_dmax = -1
      endelse
      bell
      widget_control,msg_widg,set_val='Plot selection cleared.'
   endif

;
;  Cancel request received
; 
;   if (uvalue EQ 'Cancel') then begin
;      plot_parms(*) = ''
;      WIDGET_CONTROL, sel_plot_base, /destroy
;      widget_control, main_base, sens=1
;   endif

;
;  change of plot mode requested
;
   if uvalue eq 'EPLOT_T' then begin
      emon_plottd = 1
      widget_control, emon_pids(0), set_b=1
      widget_control, emon_pids(1), set_b=0
   endif

   if uvalue eq 'EPLOT_D' then begin
      emon_plottd = 0
      widget_control, emon_pids(0), set_b=0
      widget_control, emon_pids(1), set_b=1
   endif
     
;
;  want HARDCOPY on/off
;       ========

   if (uvalue EQ 'HARD') THEN BEGIN
      if hard then begin
         hard = 0
         widget_control,hardonoff,set_val='Hardcopy OFF'
      endif else begin
         hard = 1
         widget_control,hardonoff,set_val='Hardcopy ON'
      endelse
   endif
 


ENDIF


;
;  or it was an INDEX input from the list itself
;               =====
;
if type eq 'WIDGET_LIST' then begin

   line = wanted_parms(ev.index)

;
;  already there?
;
   n = where(plot_parms eq line)

;
;  if so, ignore
;
   if n(0) eq -1 then begin
      n = where(plot_parms eq '')
;
;  if room, add to list, if not then warn
;
      if n(0) eq -1 then begin
         flash_msg, msg_widg,'Plot list full, reset if needed.'
      endif else begin
         n = min(n)
         plot_parms(n) = line
         widget_control, msg_widg, set_val=line, /append
      endelse
   endif
endif

END
