;+
; Project     : SOHO - CDS     
;                   
; Name        : TP_DRAW_RASDUR
;               
; Purpose     : Plot the calculated exposure interval for a raster.
;               
; Explanation : Plots the exposure interval and on board packet buffer use
;               as calculated by the software model of the CDHS behaviour.
;               
; Use         : IDL> tp_draw_rasdur, obs_str
;    
; Inputs      : obs_str  - a valid TPLAN data structure.
;                   
; Opt. Inputs : None
;               
; Outputs     : Just plots the results.
;               
; Opt. Outputs: None
;               
; Keywords    : HARD - for hardcopy plot
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Planning, technical
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 04-Mar-96
;               
; Modified    : 
;
; Version     : Version 1, 04-Mar-96
;-            

pro tp_draw_rasdur, hard=hard

;
;  common for variables associated with widget mode
;
@tplan_com


;
;  record plot setup, assumed on entry to be that for the main plot
;  area
;
savep = !p  & savex = !x & savey = !y

if not keyword_set(hard)  then begin
   if not xregistered('tp_rasdur') then begin
      window,xs=512,ys=512,xpos=0,ypos=512,$
                           title='CDS VDS Raster Duration'
   endif else begin
      widget_control, rasdurp_image, get_val=wind
      wset,wind
   endelse
endif

;
;  hard copy preparation
;
if keyword_set(hard) then ps
!p.multi= [0,1,2]


;
;  read the Engineering rate
;
widget_control,engrate_id, get_val=erate
if strtrim(erate(0),2) ne '' then erate = fix(erate(0)) else erate = 8

;
;  read the Telemetry rate and convert to packet interval in msecs
;
widget_control,tlmrate_id, get_val=tlmrate
if strtrim(tlmrate(0),2) ne '' then begin
   tlmrate = round(float(tlmrate(0))) 
   case tlmrate of
         22: tpi = 103.5
         11: tpi = 205
         14: tpi = 176
          2: tpi = 1249
          3: tpi = 717.1
       else: tpi = 205
   endcase
endif

;
;  calculate duration parameters
;
xyouts,0.2,0.3,/normal,'Calculating....',chars=3.0

rasdur = raster_dur(ras=tp_obs,pktb,exint,spare=spare,erate=erate,tpi=tpi)
widget_control,ras_info1,set_val=trim(round(rasdur))
widget_control,ras_info2,set_val=trim(round_off(spare,0.1))
tp_obs.raster_v.duration = float(rasdur)

;
;  plot exposure intervals
;
if n_elements(exint) gt 1 then begin
      circle_sym,/fill
      nex = n_elements(exint)
      x = indgen(nex)+1
      yrange = ext_range(exint,50)

      if yrange(1)-yrange(0) lt 5 then begin
         yrange(1) = max(exint) + 3
         yrange(0) = min(exint) - 3
      endif

      plot,x,exint,psym=8,xr=[0,nex*1.2],yr=yrange,chars=1.5,$
           tit='Expected exposure interval for current setup',$
            xtit='Exposure number',ytit='Interval (secs)',syms=0.7
endif else begin
      if exint(0) eq 0.0 then begin
         plot,[0,1],[0,1],/nodata,xst=4,yst=4
         xyouts,0.1,0.1,'Single exposure',chars=2
      endif else begin
         x = [1,1]
         yrange=[exint-5,exint+5]
         exint = [exint,exint]
         plot,x,exint,psym=8,xr=[0,5],yr=yrange,chars=1.5,$
           tit='Expected exposure interval for current setup',$
            xtit='Exposure number',ytit='Interval (secs)',syms=0.7
      endelse
endelse

;
;  plot packet buffer status
;
x = (findgen(n_elements(pktb))+1)*float(tpi)/1000.
plot,x,pktb,tit='Packet buffer status',ytit='Packets in buffer',$
            xtit='Seconds',chars=1.5,yr=[0,600],/yst
oplot,[0,100000],[500,500],line=2,syms=1.2
xyouts,(!x.crange(1)-!x.crange(0))/10.+!x.crange(0),510,'Buffer full'
           

;
; print hardcopy
;
if keyword_set(hard) then psplot
!p.multi = 0


;
;  return plot set up to standard
;
if not keyword_set(hard) then begin
   if xregistered('tp_rasdur') then begin
      widget_control, image_area, get_val=wind
      wset,wind
      !p = savep & !x = savex & !y = savey
   endif
endif 


end
