;+
; Project     : SOHO - CDS     
;                   
; Name        : CDS_TPLOT
;               
; Purpose     : Performs CDS temperature plotting within CDS_TEMP.
;               
; Explanation : Called by CDS_TEMP_EVENT and SEL_TSENS_EVENT to perform
;               CDS temperature plotting.
;               
; 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_TSENS_EVENT and CDS_TEMP_EVENT
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 27-Oct-95
;               
; Modified    : Add option to have single/multiple hardcopy plots.
;                                                  CDP, 18-Dec-95
;               Change UTPLOT name.  CDP, 2-Oct-96
;
; Version     : Version 3, 2-Oct-96
;-            

PRO cds_tplot

;
;  COMMON block for variables
;
@cds_temperature.com

;
;  pull out data for requested parameter and plot calibrated data
;      
   nw = where(tplot_parms ne '')
   if nw(0) ge 0 then begin
      nw = n_elements(nw)
      device,get_screen_size=sz
      for i=0,nw-1 do begin
         name = tplot_parms(i)
;
;  better sort in case of funnies
;
         k = where(cds_temp_log.name eq name, count)
         if count gt 0 then begin
            k = k(0)
            nutt = sort(cds_temp_log(k).time )
            uts  = cds_temp_log(k).time(nutt)
            pds  = cds_temp_log(k).value(nutt)

            nt = where((uts ge tplot_min) and (uts le tplot_max),count)
            if count gt 2 then begin
               uts = tai2utc(uts(nt))
               pds = pds(nt)
            endif else begin
               widget_control, msg_widg,set_val='Insufficient data in time range.',/appe
               tplot_min = min(cds_temp_log(0).time)
               tplot_max = max(cds_temp_log(0).time)
               bell
               return
            endelse
;
;  set up plot window
;
            window,i,xs=1000,ys=250,/free, $
                   title='CDS Temperature monitor',$
                   xpos = i*25, ypos=sz(1)-i*60-290
            circle_sym, /fill
            title = 'Temperature sensor - '+name
;
;  attempt to calibrate the data
;
            pds = tm_calib(name,pds,words)

;
;  time range
;
            utc1 = tai2utc(tplot_min)
            utc2 = tai2utc(tplot_max)


;
;  set up fancy axis labels
;
            case words(0) of
               'title':  begin
                            yt = strtrim(words(1),2)
                            yr=ext_range(pds,50)
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3,ytitle=yt,yrange=yr,$
                                    chars=1.2,timerange=[utc1,utc2] 
                            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=[utc1,utc2] 
                               if single_plot then psplot else xwin
                            endif
                         end

               'Ltitle':  begin
                            yt = strtrim(words(1),2)
                            yr=ext_range(pds,50)
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, /log,$
                                    symsize=0.3, ytitle=yt,yrange=yr,$
                                    chars=1.2,timerange=[utc1,utc2] 
                            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=[utc1,utc2] 
                               if single_plot then psplot else xwin
                            endif
                         end

         'Uncalibrated': begin            
                            yt = strtrim(words(1),2)+' (Error)'
                            yr=ext_range(pds,50)
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3, ytitle=yt,yrange=yr,$
                                    chars=1.2,timerange=[utc1,utc2] 
                            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=[utc1,utc2] 
                               if single_plot then psplot else xwin
                            endif
                         end

                'Label': begin
                            nw = n_elements(words)
                            yt = words(nw-1)
                            yr = [0,nw-1]

                            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=[utc1,utc2] 
                            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=[utc1,utc2] 
                               if single_plot then psplot else xwin
                            endif
                         end
                  else:  begin
                            yt = strtrim(words(2),2)
                            yr = [-1,2]
                            ytn=[' ',words(0),words(1),' '] 
                            cds_utplot, uts, pds, /dmy, psym=8, title=title, $
                                    symsize=0.3, ytitle=yt,yrange=yr,$
                                    ytickname=ytn,chars=1.2,timerange=[utc1,utc2] 
                            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=[utc1,utc2] 
                               if single_plot then psplot else xwin
                            endif
                         end
            endcase
         
       
         endif else begin
            widget_control,msg_widg,set_val='Parameter not found. <'+name+'>'
         endelse
      endfor
      if hard and (not single_plot) then psplot
   endif
END
