pro plot_subhtr,svmhk1,single=single,noconvert=noconvert,relays=relays,utime=utime,save=save
;+
; NAME:
;	PLOT_SUBHTR
;
; PURPOSE:
;	This procedure plots the LASCO and EIT substitution heater status
;
; CATEGORY:
;	LASCO PACKETS
;
; CALLING SEQUENCE:
;	PLOT_SUBHTR,Svmhk1
;
; INPUTS:
;	Svmhk1:	An array of the telemetry packets from SVMHK1.  It should
;		have been read in using READ_TM_PACKETS with the NO_HDR
;		switch set.
;
; KEYWORD PARAMETERS:
;	SINGLE:	A parameter from 1 to 3, indicating the plot number.  The default
;		is to plot all three plots too quickly to be of any use except
;		if the output is going to the printer.
;
;	NOCONVERT:	If set then don't convert the DN to engineering units
;		The default is to do the conversion.
;
;	SAVE:	A gif file will be written to the ftp directory on lasco6 for web access
;
;	UTIME:	If set, then the UTC time get put into it.
;	Relays:	If set, then the state of the relays get put into it.  It would be an
;		integer array (npts,nplot)  where nplot is either 3, 2 or 5 depending upon
;		how many relays are being plotted and npts is the number of samples.
;
; PROCEDURE:
;	The TM files can be from DACS or from ECS.  At this point, there is no
;	difference as long as the NO_HDR option was used in READ_TM_PACKET.
;
;	The plots are:
;		1	LASCO Nominal subsitition heater (along with GOLF and Virgo)
;		2	EIT Nominal substitution heater (along with SUMER and CELIAS)
;		3	LASCO and EIT redundant substitution heaters
;		4	LASCO and EIT substitution heater relays settings
;		5	LASCO and EIT substitution heater relays dutycycle
;
; EXAMPLE:
;	To plot the first plot type of the LASCO substitution heater on the nominal side.
;		PLOT_SUBHTR,svmhk1,single=1
;
; MODIFICATION HISTORY:
; 	Written by:	R.A. Howard, 1994.
;	Sep 17, 1998	RAH.  Modified for ECS and DACS usage.
;	Sep 20, 1998	RAH.  Corrected the relay setting plots.  Added relay dutycycle plot
;				Added relay plot.  Added save to gif option.  Added return of
;				relay and utc arrays.
;	Sep 22, 1998	RAH.  Added 11 point median filter to Plot type 5
;	Sep 24, 1998	RAH.  Check of packet times, changed position of labels.
;
;	@(#)plot_subhtr.pro	1.7 09/24/98 LASCO IDL LIBRARY
;-

; 
; offset the beginning of HK before HK information begins 
; the offset is set to 0, because the offset values used later are the ones as
; specified in the Project Data Base.  Those offset values are counted from the packet ID,
; etc.  The first byte of the packet ID has an offet of 0.
; This was verified from the LASCO HK sync words which have an offset of 12 and 13 in the
; PDB.  Thus they include the 12 bytes of packet ID, length, and LOBT.  In many of the routines
; we count from the LOBT as byte 0.
; 
offset=0		; be careful of this definition!!!
; 
 
tickname=['OFf','ON','OFF','ON','OFF','ON','OFF','ON','OFF','ON']
 
;
;  Check to see if there are problems with the packets, by looking at the first
;  byte of OBT.  If it is zero then there is a packet problem
;
svmhk1 = svmhk1(*,1:*)

w = WHERE(svmhk1(6,*) GT 0,nw)
IF (nw GT 0)  THEN svmhk1 = svmhk1(*,w)
;
;  Convert OBT to UTC
;
utc = TAI2UTC(OBT2TAI(svmhk1(offset+6:offset+11,*)))
IF KEYWORD_SET(UTIME)  THEN utime=utc
 
!x.title = 'Time (Hours) Starting Date:  '+ utc2str(utc(0)) 
; 
;   QI2P1B3, (Nominal),   LASCO , Virgo, Golf 
;
IF ((single EQ 0) or (single EQ 1) ) THEN BEGIN 
   !p.multi=[0,1,2,0,1] 
   v = FIX(svmhk1(offset+124,*))               ;Byte offset 124 
   IF ( NOT KEYWORD_SET(noconvert) ) THEN BEGIN 
        v = CNVRT_CURRENT (v,0) 
        !y.range=[0,5] 
        !y.title='Current (Amps)' 
   ENDIF ELSE BEGIN 
        !y.range=[0,255] 
        !y.title = 'Current (DN)'  
   ENDELSE 
   UTPLOT,utc,v,title='Nominal Non-OP Heater Current: QI2P1B3 (GOLF, LASCO, VIRGO)' 
 
; 
;   Heater Status, LASCO (QSL1), VIRGO (QSVA), GOLF (QSG1) Nominal  
; 
   Q1 = (FIX(svmhk1(offset+102,*)) AND 2)/2              ;QSL1 Byte offset 102 , mask 02
   Q2 = (FIX(svmhk1(offset+102,*)) AND 4)/4              ;QSG1 Byte offset 102 , mask 04
   Q3 = FIX(svmhk1(offset+102,*)) AND 1                  ;QSVA Byte offset 102 , mask 01
   UTPLOT,utc,Q1+1,title='Nominal Non-OP Heater Status',yrange=[0,13]   $ 
      ,yticks=5,ytickv=[1,2,5,6,9,10] , ytitle='Heater Relay Status' ,ytickname=tickname
   OUTPLOT,utc,Q2+5 
   OUTPLOT,utc,Q3+9 
   XYOUTS,0.1,0.15,'QSL1 (LASCO-COB)',/normal,charsize=0.7 
   XYOUTS,0.1,0.28,'QSG1 (Golf)',/normal,charsize=0.7 
   XYOUTS,0.1,0.4,'QSVA (Virgo)',/normal,charsize=0.7 
   IF KEYWORD_SET(RELAYS)  THEN BEGIN
      relays      = INTARR(N_ELEMENTS(utc),3)
      relays(0,0) = REFORM(Q1)
      relays(0,1) = REFORM(Q2)
      relays(0,2) = REFORM(Q3)
   ENDIF
ENDIF 
 
; 
;   QI3P1B3, (Nominal),   EIT , Sumer, Celias 
; 
IF ((single EQ 0) or (single EQ 2) ) THEN BEGIN 
   !p.multi=[0,1,2,0,1] 
   v = FIX(svmhk1(offset+123,*)) AND 255              ;Byte offset 123 

   IF ( NOT KEYWORD_SET(noconvert) ) THEN BEGIN 
        v = CNVRT_CURRENT (v,1) 
        !y.range=[0,5] 
        !y.title='Current (Amps)' 
   ENDIF ELSE BEGIN 
        !y.range=[0,255] 
        !y.title = 'Current (DN)'  
   ENDELSE 
   UTPLOT,utc,v,title='Nominal Non-OP Heater Current: QI3P1B3 (Celias, EIT, Sumer)' 
 
; 
;   Heater Status, EIT (QSL2), Sumer (QSSA), Celias (QSF2) Nominal  
; 
   Q1 = (FIX(svmhk1(offset+104,*)) AND 32)/32              ;QSL2 Byte offset 104 
   Q2 = (FIX(svmhk1(offset+104,*)) AND 8)/8                ;QSSA Byte offset 104 
   Q3 = (FIX(svmhk1(offset+104,*)) AND 16)/16              ;QSF2 Byte offset 104 
   UTPLOT,utc,Q1+1,title='Nominal Non-OP Heater Status',yrange=[0,13]   $ 
      ,yticks=5,ytickv=[1,2,5,6,9,10] , ytitle='Heater Relay Status' ,ytickname=tickname
   OUTPLOT,utc,Q2+5 
   OUTPLOT,utc,Q3+9 
   XYOUTS,0.1,0.15,'QSL2 (EIT)',/normal,charsize=0.7 
   XYOUTS,0.1,0.28,'QSSA (SUMER)',/normal,charsize=0.7 
   XYOUTS,0.1,0.4,'QSF2 (CELIAS)',/normal,charsize=0.7 
   IF KEYWORD_SET(RELAYS)  THEN BEGIN
      relays      = INTARR(N_ELEMENTS(utc),3)
      relays(0,0) = REFORM(Q1)
      relays(0,1) = REFORM(Q2)
      relays(0,2) = REFORM(Q3)
   ENDIF
ENDIF 
 
; 
;   QIL3 (LASCO), QIL4 (EIT)  (REDUNDANT) 
; 
IF ((single EQ 0) or (single EQ 3) ) THEN BEGIN 
   !p.multi=[0,1,3,0,1] 
   v = FIX(svmhk1(offset+176,*))               ;Byte offset 176 
   IF ( NOT KEYWORD_SET(noconvert) ) THEN BEGIN 
        v = CNVRT_CURRENT (v,2) 
        !y.range=[0,5] 
        !y.title='Current (Amps)' 
   ENDIF ELSE BEGIN 
        !y.range=[0,255] 
        !y.title = 'Current (DN)'  
   ENDELSE 
   UTPLOT,utc,v,title='Redundant Non-OP Heater Current: QIL3 (EIT)' 
   v = FIX(svmhk1(offset+174,*))               ;Byte offset 174 
   IF ( NOT KEYWORD_SET(noconvert) ) THEN BEGIN 
        v = CNVRT_CURRENT (v,3) 
        !y.range=[0,1] 
        !y.title='Current (Amps)' 
   ENDIF ELSE BEGIN 
        !y.range=[0,255] 
        !y.title = 'Current (DN)'  
   ENDELSE 
   UTPLOT,utc,v,title='Redundant Non-OP Heater Current: QIL4 (LASCO)' 
; 
;   Heater Status, EIT (QSL3), LASCO (QSL4) Redundant
; 
   Q1 = (FIX(svmhk1(offset+145,*)) AND 2)/2              ;QSL3 Byte offset 145 
   Q2 = (FIX(svmhk1(offset+145,*)) AND 4)/4              ;QSL4 Byte offset 145 
   UTPLOT,utc,Q1+1,title='Redundant Non-OP Heater Status',yrange=[0,9]   $ 
       ,yticks=3,ytickv=[1,2,5,6] ,ytickname=tickname, ytitle='Heater Relay Status'
   OUTPLOT,utc,Q2+5 
   XYOUTS,0.08,0.12,'QSL3 (EIT)',/normal,charsize=0.7 
   XYOUTS,0.08,0.24,'QSL4 (LASCO)',/normal,charsize=0.7 
   IF KEYWORD_SET(RELAYS)  THEN BEGIN
      relays      = INTARR(N_ELEMENTS(utc),2)
      relays(0,0) = REFORM(Q1)
      relays(0,1) = REFORM(Q2)
   ENDIF
ENDIF 


;
;   Heater Status, LASCO COB (QSL1) Nominal, 
;                  EIT (QSL2) Nominal, 
;		   EIT (QSL3) Redundant, 
;		   LASCO (QSL4) Redundant
;                  LASCO LEB (QSL5) Nominal, 
;
IF ((single EQ 0) or (single EQ 4) OR (single EQ 5) ) THEN BEGIN 
   !p.multi=''
   Q1 = (FIX(svmhk1(offset+102,*)) AND 2)/2              ;QSL1 Byte offset 102, mask 02
   Q2 = (FIX(svmhk1(offset+104,*)) AND 32)/32            ;QSL2 Byte offset 104, mask 20
   Q3 = (FIX(svmhk1(offset+145,*)) AND 2)/2              ;QSL3 Byte offset 145, mask 02
   Q4 = (FIX(svmhk1(offset+145,*)) AND 4)/4              ;QSL4 Byte offset 145, mask 04
   Q5 = (FIX(svmhk1(offset+104,*)) AND 64)/64            ;QSL5 Byte offset 104, mask 40
   IF ((single EQ 0) or (single EQ 4) ) THEN BEGIN 
      UTPLOT,utc,Q1+1,title='LASCO/EIT Non-OP Heater Status',yrange=[0,21]   $ 
         ,yticks=10,ytickv=[1,2,5,6,9,10,13,14,17,18] , ytitle='Heater Relay Status' $
         ,ytickname=tickname
      OUTPLOT,utc,Q2+5 
      OUTPLOT,utc,Q3+9 
      OUTPLOT,utc,Q4+13 
      OUTPLOT,utc,Q5+17 
      XYOUTS,0.15,0.178*1,'QSL1 (COB)',/normal,charsize=0.7 
      XYOUTS,0.15,0.178*2,'QSL2 (EIT)',/normal,charsize=0.7 
      XYOUTS,0.15,0.178*3,'QSL3 (EIT-R)',/normal,charsize=0.7 
      XYOUTS,0.15,0.178*4,'QSL4 (LASCO-R)',/normal,charsize=0.7 
      XYOUTS,0.15,0.178*5,'QSL5 (LEB)',/normal,charsize=0.7 
   ENDIF
   IF KEYWORD_SET(RELAYS)  THEN BEGIN
      relays      = INTARR(N_ELEMENTS(utc),5)
      relays(0,0) = REFORM(Q1)
      relays(0,1) = REFORM(Q2)
      relays(0,2) = REFORM(Q3)
      relays(0,3) = REFORM(Q4)
      relays(0,4) = REFORM(Q5)
   ENDIF
;
;  Plot dutycycle of heaters
;
   IF (single EQ 0) OR (single EQ 5)  THEN BEGIN
      z = Q1
      nz=N_ELEMENTS(z)
      nz=31*(nz/31)
      z=z(0:nz-1)
      nc=nz/31
      duty=FLTARR(nc)
      t = utc
      ind = 31*INDGEN(nc)
      t = t(ind)
      for i=0,nc-1 do duty(i)=TOTAL(z(31*i:31*(i+1)-1))/31.
      htr_labels = ['COB-N','EIT-N','LASCO-R','EIT-R','LEB-N']
      UTPLOT,t,MEDIAN(duty*100,11), yrange=[-10,110],psym=10 ,  $
          title='LASCO/EIT Substitution Heater Dutycycle',   $
          ytitle = 'Percent'
      fstart = duty(0)*100+10
      dt = UTC2TAI(t(N_ELEMENTS(t)-1))-UTC2TAI(t(0))
      dt = dt/20.
      startt = TAI2UTC(UTC2TAI(t(0))+dt)
      UTSTRING,startt,fstart,htr_labels(0),charsize=1.
      FOR j=1,4 DO BEGIN 
          CASE J OF
            1:  z=Q2
            2:  z=Q3
            3:  z=Q4
            4:  z=Q5
          ENDCASE
          for i=0,nc-1 do duty(i)=TOTAL(z(31*i:31*(i+1)-1))/31.
          IF (MAX(duty) GT 0)  THEN BEGIN
             OUTPLOT,t,MEDIAN(duty*100,11),psym=10
             fstart = duty(0)*100+10
             UTSTRING,startt,fstart,htr_labels(j),charsize=1.
          ENDIF
      ENDFOR
   ENDIF
ENDIF
IF KEYWORD_SET (save)  THEN BEGIN
   z = TVRD()
   z = NOT z
   ld = UTC2YYMMDD(utc(n_elements(utc)-1))
   fn = 'subhtr'+STRING(single,format='(i1)')+'_'+ld+'.gif'
   file = PICKFILE(PATH='/net/lasco6/data/ftp/pub/lasco/hk/',GET_PATH=DIR, $
          FILE=fn,/write)
   sep = '/'
   IF (!version.os_family EQ 'dos')  THEN sep='\'
   IF (!version.os_family EQ 'vms')  THEN sep='['
   IF (STRMID(file,0,1) NE sep)   THEN file=dir+file
   IF (file NE '')  THEN WRITE_GIF,file,z
ENDIF
 
!y.title = '' 
!y.range = '' 
!p.multi = 0 
!x.title = '' 
EMPTY 
RETURN 
END 
