;+
; NAME:
;	WPLOT_NONOP
;
; PURPOSE:
;	This procedure generates plots of the non-operational temperature sensors.
;
; CATEGORY:
;	LASCO PACKETS
;
; CALLING SEQUENCE:
;	WPLOT_NONOP
;
; INPUTS:
;	None
;
; KEYWORD PARAMETERS:
;	DACS:	If set then use the .recs files generated by DACS.  The default
;		is to use the .REL files generated by ECS.  If the environment
;		variable, TMPCKTS, is set then tHe files should be located in
;		that directory.  If it isn't set then the files are located in
;		the current directory.
;
;	AUTO:	If set then generate an automatic plot for the latest data.
;		The value of the AUTO parameter determines the number of times
;		the plot cycle is repeated.  The default is to use the program
;		interactively.
;
;	FDAY:	If set then specifies the start time of the plot in fractional
;		days from the current time in the automatic mode.  The default 
;		is to start 1 day, 86400 seconds, earlier than the current time.
;
;	NWAIT:	If set then specifies the number of seconds to wait between 
;		automatic cycles.  The default is to wait 3600 seconds.
;
; COMMON BLOCKS:
;	WPLOT_NONOP_COMMON
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; EXAMPLE:
;	
;	WPLOT_NONOP
;		Allows interactive plotting using .REL files from ECS
;
;	WPLOT_NONOP,/dacs
;		Allows interactive plotting using .recs files from DACS
;
;	WPLOT_NONOP,/dacs,/auto
;		Generates a single automatic plot for the latest 24 hours.
;
;	WPLOT_NONOP,/dacs,auto=24,fday=2,wait=7200.
;		Generates automatic plots every 2 hours covering the latest 2 days.
;
;
; MODIFICATION HISTORY:
; 	Written by:	Scott Paswaters, Dec 1995
;	Modifications:
; 	971216  aee 	 Fixed year/month change in GET_PCKT_NAMES.
; 	980811  rah	Fixed offsets (added 6 bytes in line 96 to nonop.offs)
;	980916  rah	A number of changes to make it work for DACS/REL files
;			There were many files per hour of REL files.
;			TM_READ_PACKET returns the same 12 byte header, for both .recs and .REL
;			files.  The 12 bytes are 6 for packet ID, length, etc and 6 for OBT.
;			We want to start the packet offsets with the first byte of time at
;			0.  Therefore the dacs_offset value should be 6, to skip over the 
;			packet ID, etc.
;	980917  rah	Added the automatic plot capability.
;	980923  rah	Added plot of -10 or -50
;       990126  aee     Fixed for Y2K problem.
;
; @(#)wplot_nonop.pro	1.11 12/23/98 :LASCO IDL LIBRARY
;
;-

;
FUNCTION GET_PCKT_NAMES, startt, stopt

   IF (stopt EQ '') THEN BEGIN
      GET_UTC, utc
      utc = ANYTIM2CAL(utc, FORM=8)
      stopt = STRMID(utc,2,2)+STRMID(utc,4,2)+STRMID(utc,6,2)+'_'+STRMID(utc,8,2)
   ENDIF
   y0 = FIX(STRMID(startt, 0, 2)) & y1 = FIX(STRMID(stopt, 0, 2))
   m0 = FIX(STRMID(startt, 2, 2)) & m1 = FIX(STRMID(stopt, 2, 2))
   d0 = FIX(STRMID(startt, 4, 2)) & d1 = FIX(STRMID(stopt, 4, 2))
   h0 = FIX(STRMID(startt, 7, 2)) & h1 = FIX(STRMID(stopt, 7, 2))
   m0 = m0 > 1 & m1 = m1 > 1
   d0 = d0 > 1 & d1 = d1 > 1

   arr = '000000_00'

   YEAR0 = ((y0 + 50) MOD 100) + 1950
   YEAR1 = ((y1 + 50) MOD 100) + 1950

   MONTHS = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
;   DAY = (JULDAY(1, 1, y0+1900) + 1) MOD 7
;   DAY1_NEXT_YEAR = (JULDAY(1, 1, y0+1900+1) + 1) MOD 7

   DAY = (JULDAY(1, 1, YEAR0) + 1) MOD 7
   DAY1_NEXT_YEAR = (JULDAY(1, 1, YEAR0+1) + 1) MOD 7

   case ((DAY1_NEXT_YEAR + 7 - DAY) mod 7) of
        2 :
        1 : MONTHS(1) = 28      ; Not a leap year
        else: MONTHS(8) = 19    ; 1752
        endcase

;   FOR y=y0, y1 DO BEGIN
    FOR y= YEAR0, YEAR1 DO BEGIN
     nm0 = 1
     nm1 = 12
;     IF (y EQ y0) THEN nm0 = m0
     IF (y EQ YEAR0) THEN nm0 = m0
;    IF (y EQ d1) THEN nm1 = m1
;     IF (y EQ y1) THEN nm1 = m1
     IF (y EQ YEAR1) THEN nm1 = m1
;     FOR m=m0, m1 DO BEGIN
     FOR m=nm0, nm1 DO BEGIN
       nd0 = 1
       nd1 = months(m-1)
       IF (m EQ m0) THEN nd0 = d0
       IF (m EQ m1) THEN nd1 = d1
       FOR d=nd0, nd1 DO BEGIN
         nh0 = 0
         nh1 = 23
         IF (d EQ d0) THEN nh0 = h0
         IF (d EQ d1) THEN nh1 = h1
         FOR h=nh0, nh1 DO BEGIN
;            add = STRMID(STRING(100+y,FORM='(i3)'),1,2) + $
            add = STRMID(STRING(y,FORM='(i4.4)'),2,2) + $
                  STRMID(STRING(100+m,FORM='(i3)'),1,2) + $
                  STRMID(STRING(100+d,FORM='(i3)'),1,2) + '_' + $
                  STRMID(STRING(100+h,FORM='(i3)'),1,2)
            arr = [arr, add]
         ENDFOR
       ENDFOR
     ENDFOR
   ENDFOR

   len = N_ELEMENTS(arr)
   IF (len LE 1) THEN RETURN, arr
   RETURN, arr(1:N_ELEMENTS(arr)-1)
END

PRO DO_PLOT_NONOP, nonopv

   COMMON WPLOT_NONOP_COMMON, sv2, sv4, time2, time4, gap_ind2, gap_ind4,dacsflag

   dacs_offset = +6	;** 12 bytes put on by dacs, 6 for RT Hdr

   ind = WHERE(nonopv.selected GE 1, num)
   IF (nonopv.single) THEN nnum = 1 ELSE nnum = num
   CASE (1) OF
      (nnum LE 3) : p = [0,1,nnum,0,1]
      (nnum EQ 4) : p = [0,2,2,0,1]
      ( (nnum GT 4) AND (nnum LE 6) ): p = [0,2,3,0,1]
      (nnum GT 6) : p = [0,3,3,0,1]
   ENDCASE
   origmulti=!p.multi
   origfont=!p.font
   origtitle=!y.title
   origrange=!y.range
   !P.MULTI = p
   !P.FONT = 5

   IF (nonopv.cnvrt) THEN BEGIN
      !Y.TITLE = 'Temp (Deg)'
      IF (nonopv.negat EQ 1)  THEN !Y.RANGE = [-50,40] ELSE !Y.RANGE = [-10,40]
   ENDIF ELSE BEGIN
      !Y.TITLE = 'Temp (DN)'
      !Y.RANGE = [0,255]
   ENDELSE
   IF (num EQ 1) THEN ttitle = nonopv.fields(ind(0)) ELSE ttitle="SOHO Non-Op Temperatures"

   FOR i=0, num-1 DO BEGIN
      j = ind(i)
      IF (nonopv.pckts(j) EQ 4) THEN sv = sv4 ELSE sv = sv2
      ;** offs is in bytes 
      offset = nonopv.offs(j)+dacs_offset		; 12 bytes for offset
      v = sv(offset, *)
      IF (nonopv.cnvrt) THEN v = CNVRT_TEMPS(v,nonopv.cnvrs(j))
      IF (nonopv.pckts(j) EQ 4) THEN BEGIN
         time = time4
         gap_ind = gap_ind4
      ENDIF ELSE BEGIN
         time = time2
         gap_ind = gap_ind2
      ENDELSE

sub_zero = 0
;      mv = max(v) & sub_zero = (mv lt 0)
;      if sub_zero then begin
;         min_v = min(v) & mv0 = 10*(fix(min_v/10.) - 1)
;         mv1 = 10*(fix(mv/10.) + 1)
;         sz_range = [mv0, mv1]
;      end

      IF (nonopv.single) THEN BEGIN
         IF (i EQ 0) THEN $
            UTPLOT, time, v, TITLE=ttitle, /NODATA, XSTYLE=1, $
               TIMERANGE=[time(0),time(N_ELEMENTS(time)-1)]
         FOR k=0, N_ELEMENTS(gap_ind)-2 DO BEGIN
            ind0 = gap_ind(k)+1 & ind1 = gap_ind(k+1)
            IF (ind1 GT ind0) THEN $
               OUTPLOT, time(ind0:ind1), v(ind0:ind1), COLOR=((j MOD 9)+1)
         ENDFOR
         XYOUTS, .7, 0.95-((i+1)*.02), COLOR=((j MOD 9)+1), /NORMAL, '____'
         XYOUTS, .75, 0.95-((i+1)*.02), COLOR=((j MOD 9)+1), /NORMAL, nonopv.fields(j) 
      ENDIF ELSE BEGIN
         oldyrange = !y.range
         if sub_zero then !y.range = sz_range
         UTPLOT, time, v, TITLE=nonopv.fields(j), /NODATA, XSTYLE=1, $
            TIMERANGE=[time(0),time(N_ELEMENTS(time)-1)]
         FOR k=0, N_ELEMENTS(gap_ind)-2 DO BEGIN
            ind0 = gap_ind(k)+1 & ind1 = gap_ind(k+1)
            IF (ind1 GT ind0) THEN $
               OUTPLOT, time(ind0:ind1), v(ind0:ind1)
         ENDFOR
         !y.range = oldyrange
      ENDELSE
   ENDFOR
   !y.range=origrange
   !p.multi=origmulti
   !p.font=origfont
   !p.title=origtitle
END

PRO WPLOT_READ_PACKETS, STARTT, STOPT

   COMMON WPLOT_NONOP_COMMON, sv2, sv4, time2, time4, gap_ind2, gap_ind4,dacsflag

   startt = STRTRIM(startt, 2)
   stopt  = STRTRIM(stopt , 2)
   pckt_names = GET_PCKT_NAMES(startt, stopt)
   dir = GETENV('TMPCKTS')
   IF (dir EQ '') THEN dir = '.'
   IF (KEYWORD_SET(dacsflag)) THEN BEGIN
      read_arr2 = dir+'/SVHK2'+pckt_names+'.recs'
      read_arr4 = dir+'/SVHK4'+pckt_names+'.recs'
   ENDIF ELSE BEGIN
      read_arr2 = dir+'/SVMHK2_'+pckt_names+'*.*'
      nf = N_ELEMENTS(read_arr2)
      IF (nf GT 0)  THEN BEGIN
         nfirst=1
         FOR i=0,nf-1 DO BEGIN
             ff = FINDFILE(read_arr2(i),COUNT=nff)
             IF (nff GT 0)  THEN BEGIN
                IF (nfirst EQ 1)  THEN tempff=ff ELSE tempff=[tempff,ff]
             ENDIF
         ENDFOR
         read_arr2 = tempff
      ENDIF
      read_arr4 = dir+'/SVMHK4_'+pckt_names+'*.*'
      nf = N_ELEMENTS(read_arr4)
      IF (nf GT 0)  THEN BEGIN
         nfirst=1
         FOR i=0,nf-1 DO BEGIN
             ff = FINDFILE(read_arr4(i),COUNT=nff)
             IF (nff GT 0)  THEN BEGIN
                IF (nfirst EQ 1)  THEN tempff=ff ELSE tempff=[tempff,ff]
             ENDIF
         ENDFOR
         read_arr4 = tempff
      ENDIF
   ENDELSE
   first = 1
   FOR i=0, N_ELEMENTS(read_arr2)-1 DO BEGIN
       IF ((FINDFILE(read_arr2(i)))(0) EQ '') THEN BEGIN
          PRINT, '%%WPLOT_NONOP: Packet file not found: ', read_arr2(i)
       ENDIF ELSE BEGIN
          s2 = READ_TM_PACKET(read_arr2(i), /NO_HDR)
          sv2_size = SIZE(s2)
          IF (first) THEN BEGIN
             sv2_len = sv2_size(1)
             sv2 = REFORM(s2, sv2_size(1)*sv2_size(2))
             first = 0
          ENDIF ELSE $
             sv2 = [sv2, REFORM(s2, sv2_size(1)*sv2_size(2))]
          PRINT,'Processing file '+read_arr2(i)+':',sv2_size(2),'  records'
       ENDELSE
   ENDFOR
   first = 1
   FOR i=0, N_ELEMENTS(read_arr4)-1 DO BEGIN
       IF ((FINDFILE(read_arr4(i)))(0) EQ '') THEN BEGIN
          PRINT, '%%WPLOT_NONOP: Packet file not found: ', read_arr4(i)
       ENDIF ELSE BEGIN
          s4 = READ_TM_PACKET(read_arr4(i), /NO_HDR)
          sv4_size = SIZE(s4)
          IF (first) THEN BEGIN
             sv4_len = sv4_size(1)
             sv4 = REFORM(s4, sv4_size(1)*sv4_size(2))
             first = 0
          ENDIF ELSE $
             sv4 = [sv4, REFORM(s4, sv4_size(1)*sv4_size(2))]
          PRINT,'Processing file '+read_arr4(i)+':',sv4_size(2),'  records'
       ENDELSE
   ENDFOR
   IF (DATATYPE(sv2_len) NE 'UND') THEN BEGIN
      sv2 = REFORM(sv2, sv2_len, N_ELEMENTS(sv2)/sv2_len)
      sv4 = REFORM(sv4, sv4_len, N_ELEMENTS(sv4)/sv4_len)
      dacs_offset = 6	;** 12 bytes put on by dacs, 6 for RT Hdr
      offset = dacs_offset 	;** TAI timestamp put on by S/C is first word of packet
				;** after 6 byte pckt hdr
      time2 = TAI2UTC(OBT2TAI(sv2(offset:offset+5, *)))
      time4 = TAI2UTC(OBT2TAI(sv4(offset:offset+5, *)))

      ;** throw out invalid data and sort data by S/C time
      utc1990 = STR2UTC('1990/1/1')
      utc2050 = STR2UTC('2050/1/1')
      good = WHERE( (time2.mjd GT utc1990.mjd) AND (time2.mjd LT utc2050.mjd) )
      ind = SORT(time2(good).mjd)
      sv2 = sv2(*,good(ind))
      time2 = time2(good(ind))
      good = WHERE( (time4.mjd GT utc1990.mjd) AND (time4.mjd LT utc2050.mjd) )
      ind = SORT(time4(good).mjd)
      sv4 = sv4(*,good(ind))
      time4 = time4(good(ind))

      ;** don't plot in data gaps 
      tai = UTC2TAI(time2)
      time_shift = SHIFT(tai, -1)
      time_diff  = tai - time_shift
      gap_ind = WHERE(time_diff LT -60)	;** define gaps as 60 sec apart
      IF (gap_ind(0) GE 0) THEN $
         gap_ind2 = [0, gap_ind, N_ELEMENTS(time2)-1] $
      ELSE $
         gap_ind2 = [0, N_ELEMENTS(time2)-1]
      tai = UTC2TAI(time4)
      time_shift = SHIFT(tai, -1)
      time_diff  = tai - time_shift
      gap_ind = WHERE(time_diff LT -60)
      IF (gap_ind(0) GE 0) THEN $
         gap_ind4 = [0, gap_ind, N_ELEMENTS(time4)-1] $
      ELSE $
         gap_ind4 = [0, N_ELEMENTS(time4)-1]
   ENDIF
RETURN
END

PRO WPLOT_AUTO, nonopv, nauto, nsecs, lengthplot

   COMMON WPLOT_NONOP_COMMON, sv2, sv4, time2, time4, gap_ind2, gap_ind4,dacsflag

   FOR i=0,nauto-1 DO BEGIN
      GET_UTC, utc
      tai = UTC2TAI(utc)
      utc = ANYTIM2CAL(utc, FORM=8)
      stopt = STRMID(utc,2,2)+STRMID(utc,4,2)+STRMID(utc,6,2)+'_'+STRMID(utc,8,2)
      tai = tai-lengthplot
      utc = ANYTIM2CAL(TAI2UTC(tai),FORM=8)
      startt = STRMID(utc,2,2)+STRMID(utc,4,2)+STRMID(utc,6,2)+'_'+STRMID(utc,8,2)
      WPLOT_READ_PACKETS,startt,stopt
      nonopv.startt = startt
      nonopv.stopt = stopt
      nonopv.single = 1
      IF (DATATYPE(sv2) EQ 'UND') THEN $
         PRINT, "%%WPLOT_NONOP: Must read in packet files first." $
      ELSE $
         DO_PLOT_NONOP, nonopv
      img = TVRD()
      TVLCT, r, g, b, /GET
      file = '/net/lasco6/data/ftp/pub/lasco/hk/nonop_temps_'+STRMID(stopt,0,6)+'.gif'
      WRITE_GIF, file, img, r, g, b
      PRINT,'WPLOT_NONOP:  written gif file:  '+file
      IF (i LT (nauto-1))  THEN WAIT,nsecs
   ENDFOR
   WIDGET_CONTROL, /DESTROY, nonopv.base
RETURN
END

PRO WPLOT_NONOP_EVENT, event

   COMMON WPLOT_NONOP_COMMON, sv2, sv4, time2, time4, gap_ind2, gap_ind4,dacsflag

   WIDGET_CONTROL, event.id, GET_UVALUE=uval
   WIDGET_CONTROL, event.top, GET_UVALUE=nonopv   ; get structure from UVALUE


    CASE (uval) OF

	'QUIT' : BEGIN	;** exit program
		WIDGET_CONTROL, /DESTROY, nonopv.base
		GOTO, done
	    END

	'READ' : BEGIN
                WIDGET_CONTROL, /HOUR
                WIDGET_CONTROL, nonopv.startt, GET_VALUE=startt & startt = startt(0)
                WIDGET_CONTROL, nonopv.stopt, GET_VALUE=stopt & stopt = stopt(0)
                WPLOT_READ_PACKETS,startt,stopt
	    END

	'PLOT' : BEGIN
	        WIDGET_CONTROL, /HOUR
	        IF (DATATYPE(sv2) EQ 'UND') THEN $
                   PRINT, "%%WPLOT_NONOP: Must read in packet files first." $
                ELSE $
                   DO_PLOT_NONOP, nonopv
	    END

	'NEGS' : BEGIN
                   IF (event.select) THEN BEGIN
                      nonopv.negat = 1
                   ENDIF ELSE BEGIN
                      nonopv.negat = 0
                   ENDELSE
	    END

	'PRINT' : BEGIN
	        WIDGET_CONTROL, /HOUR
	        IF (DATATYPE(sv2) EQ 'UND') THEN $
                   PRINT, "%%WPLOT_NONOP: Must read in packet files first." $
                ELSE BEGIN
                   SET_PLOT, 'PS'
                   DEVICE, /INCHES, XSIZE = 9.5, YSIZE = 7.5, $
                       /LANDSCAPE,  BITS_PER_PIXEL=8, FILENAME = '/tmp/test.ps'
                   DO_PLOT_NONOP, nonopv
                   DEVICE, /CLOSE
                   SPAWN, 'lpr /tmp/test.ps'
                   SET_PLOT, 'X'
                   SPAWN, '/bin/rm /tmp/test.ps'
                ENDELSE
	    END

	'SAVE' : BEGIN
	        WIDGET_CONTROL, /HOUR
                img = TVRD()
                TVLCT, r, g, b, /GET
                file = CDSPICKFILE(PATH='/net/lasco6/data/ftp/pub/lasco/hk/', GET_PATH=dir, $
                          FILE='nonop_temps.gif', /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, img, r, g, b
	    END

	'CNVRT' : BEGIN
                IF (event.select) THEN BEGIN
                   nonopv.cnvrt = 1
                ENDIF ELSE BEGIN
                   nonopv.cnvrt = 0
                ENDELSE
	    END

	'SINGLE' : BEGIN
                IF (event.select) THEN BEGIN
                   nonopv.single = 1
                ENDIF ELSE BEGIN
                   nonopv.single = 0
                ENDELSE
	    END

	'SELECT' : BEGIN
                ind = WHERE(nonopv.button_id EQ event.id)
                IF (event.select) THEN BEGIN
                   PRINT, 'Selected ', nonopv.fields(ind)
                   nonopv.selected(ind) = 1
                ENDIF ELSE BEGIN
                   PRINT, 'De-selected ', nonopv.fields(ind)
                   nonopv.selected(ind) = 0
                ENDELSE
	    END

	ELSE : BEGIN
	    END

   ENDCASE

  WIDGET_CONTROL, event.top, SET_UVALUE=nonopv

done:
END

;-----------------------------------------------------------------------

PRO WPLOT_NONOP,  DACS=DACS, AUTO=AUTO, NWAIT=NWAIT, FDAY=FDAY

;
; KEYWORDS:
;	DACS	If set then use data files created by DACS (recs).  The default is to 
;		use data files from ECS (REL).
;	AUTO	If set then use the standard LASCO/EIT settings and don't accept input
;		from the mouse.  A cycle of plots will be generated every NWAIT seconds.
;		and will be repeated AUTO times.  The time coverage is to plot 1 day
;		at a time, but that can be changed using a keyword.  The default is to 
;		perform the plots using the mouse.
;	NWAIT	If set then the number of seconds to wait between AUTO cycles.  The 
;		default is to wait 3600 seconds.
;	FDAY	If set the size of the plot in fractions of a day.  The default is to
;		plot the temperatures for 1 day.
;

   COMMON WPLOT_NONOP_COMMON, sv2, sv4, time2, time4, gap_ind2, gap_ind4,dacsflag


    IF (KEYWORD_SET(DACS))  THEN dacsflag=1  ELSE dacsflag=0

    num = 47
    fields = STRARR(num) & pckts = INTARR(num) & offs = INTARR(num) & cnvrs = INTARR(num)
    i=0   & fields(i)='COB Temp Ref Point'      & pckts(i)=2 & offs(i)= 69 & cnvrs(i)=4
    i=i+1 & fields(i)='COB Non-OP Thermistor A' & pckts(i)=2 & offs(i)= 76 & cnvrs(i)=5
    i=i+1 & fields(i)='COB Non-OP Thermistor B' & pckts(i)=2 & offs(i)= 90 & cnvrs(i)=5
    i=i+1 & fields(i)='LEB Temp Ref Point'      & pckts(i)=4 & offs(i)=147 & cnvrs(i)=4
    i=i+1 & fields(i)='LEB Non-OP Thermistor A' & pckts(i)=4 & offs(i)=150 & cnvrs(i)=5
    i=i+1 & fields(i)='LEB Non-OP Thermistor B' & pckts(i)=4 & offs(i)=111 & cnvrs(i)=5
    i=i+1 & fields(i)='EIT Temp Ref Point'      & pckts(i)=4 & offs(i)=108 & cnvrs(i)=4
    i=i+1 & fields(i)='EIT Non-OP Thermistor A' & pckts(i)=2 & offs(i)=109 & cnvrs(i)=5
    i=i+1 & fields(i)='EIT Non-OP Thermistor B' & pckts(i)=2 & offs(i)= 58 & cnvrs(i)=5
    i=i+1 & fields(i)='VIRGO SU TRP1'           & pckts(i)=2 & offs(i)= 55 & cnvrs(i)=4
    i=i+1 & fields(i)='VIRGO ENICS TRP2'        & pckts(i)=2 & offs(i)= 91 & cnvrs(i)=4
    i=i+1 & fields(i)='OSR QT-14       '        & pckts(i)=2 & offs(i)= 48 & cnvrs(i)=4
    i=i+1 & fields(i)='OSR QT-15       '        & pckts(i)=2 & offs(i)= 65 & cnvrs(i)=4
    i=i+1 & fields(i)='OSR QT-16       '        & pckts(i)=2 & offs(i)= 80 & cnvrs(i)=4
    i=i+1 & fields(i)='OSR QT-17       '        & pckts(i)=2 & offs(i)= 95 & cnvrs(i)=4
    i=i+1 & fields(i)='SUMER 1 SU TRP1'         & pckts(i)=2 & offs(i)= 53 & cnvrs(i)=4
    i=i+1 & fields(i)='SUMER 2 DPU TRP2'        & pckts(i)=2 & offs(i)= 86 & cnvrs(i)=4
    i=i+1 & fields(i)='SUMER 1 SU TH1'          & pckts(i)=2 & offs(i)=111 & cnvrs(i)=4
    i=i+1 & fields(i)='SUMER 4 PSU TRP3'        & pckts(i)=2 & offs(i)=104 & cnvrs(i)=4
    i=i+1 & fields(i)='SUMER 1 SU TH2'          & pckts(i)=4 & offs(i)=113 & cnvrs(i)=4
    i=i+1 & fields(i)='SUMER TDC HTR'           & pckts(i)=2 & offs(i)=113 & cnvrs(i)=4
    i=i+1 & fields(i)='GOLF SU TRP1'            & pckts(i)=2 & offs(i)= 52 & cnvrs(i)=4
    i=i+1 & fields(i)='GOLF SU TH1'             & pckts(i)=2 & offs(i)= 74 & cnvrs(i)=4
    i=i+1 & fields(i)='GOLF SU TH2'             & pckts(i)=2 & offs(i)= 89 & cnvrs(i)=4
    i=i+1 & fields(i)='GOLF ENICS1 TRP2'        & pckts(i)=2 & offs(i)=103 & cnvrs(i)=4
    i=i+1 & fields(i)='GOLF ENICS2 TRP3'        & pckts(i)=2 & offs(i)= 85 & cnvrs(i)=4
    i=i+1 & fields(i)='CDS TH2'                 & pckts(i)=2 & offs(i)= 56 & cnvrs(i)=4
    i=i+1 & fields(i)='CDS SPECTROMETER TRP1'   & pckts(i)=2 & offs(i)= 70 & cnvrs(i)=4
    i=i+1 & fields(i)='CDS CDHS TRP2'           & pckts(i)=4 & offs(i)=145 & cnvrs(i)=4
    i=i+1 & fields(i)='CDS TH1'                 & pckts(i)=2 & offs(i)=106 & cnvrs(i)=4
    i=i+1 & fields(i)='CDS EPS TRP3'            & pckts(i)=4 & offs(i)=106 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC LION TRP1'         & pckts(i)=2 & offs(i)= 54 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC ESU TH1'           & pckts(i)=2 & offs(i)= 57 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC EPHIN TRP2'        & pckts(i)=2 & offs(i)= 68 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC EPHIN SU TH3'      & pckts(i)=2 & offs(i)=107 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC ESU TH2'           & pckts(i)=2 & offs(i)= 88 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC CDPU TRP4'         & pckts(i)=2 & offs(i)=102 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC LVPC TRP3'         & pckts(i)=2 & offs(i)= 83 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC LION'              & pckts(i)=2 & offs(i)= 71 & cnvrs(i)=4
    i=i+1 & fields(i)='CEPAC ERNE TRP5'         & pckts(i)=4 & offs(i)=107 & cnvrs(i)=4
    i=i+1 & fields(i)='SWAN SU 1 TH1'           & pckts(i)=2 & offs(i)= 60 & cnvrs(i)=4
    i=i+1 & fields(i)='SWAN SU 1 TRP1'          & pckts(i)=2 & offs(i)= 51 & cnvrs(i)=4
    i=i+1 & fields(i)='SWAN SU 1 TRP2'          & pckts(i)=2 & offs(i)=105 & cnvrs(i)=4
    i=i+1 & fields(i)='SWAN SU 2 TH2'           & pckts(i)=2 & offs(i)=112 & cnvrs(i)=4
    i=i+1 & fields(i)='SWAN ENICS TRP3'         & pckts(i)=2 & offs(i)= 84 & cnvrs(i)=4
    i=i+1 & fields(i)='SWAN SU TH3'             & pckts(i)=4 & offs(i)=100 & cnvrs(i)=4
    i=i+1 & fields(i)='SWAN SU 2 TH4'           & pckts(i)=2 & offs(i)= 92 & cnvrs(i)=4
    button_id = LONARR(N_ELEMENTS(fields))
    selected = BYTARR(N_ELEMENTS(fields))
    selected(0:8) = 1
    cnvrt = 1
    single = 1

    LOADCT, 0
    TVLCT, 255,0,0, 1      ;** load red into entry 1
    TVLCT, 0,255,0, 2      ;** load green into entry 2
    TVLCT, 142,229,238, 3  ;** load blue into entry 3
    TVLCT, 255,255,0,4     ;** load yellow into entry 4
    TVLCT, 200,0,0, 5      ;** load dull red into entry 5
    TVLCT, 0,200,0, 6      ;** load dull green into entry 6
    TVLCT, 0,206,237, 7    ;** load dull blue into entry 7
    TVLCT, 200,200,0,8     ;** load dull yellow into entry 8
    TVLCT, 255,255,255, 9  ;** load white into entry 9

    ;********************************************************************
    ;** SET UP WIDGETS **************************************************

    base = WIDGET_BASE(/ROW, TITLE='SOHO NON-OP TEMPERATURES', /FRAME)

    col1 = WIDGET_BASE(base, /COLUMN)

    col2 = WIDGET_BASE(col1, /COLUMN, /FRAME)
    row = WIDGET_BASE(col2, /ROW)
    lab = WIDGET_LABEL(row, VALUE='START :')
    GET_UTC, utc
    utc = ANYTIM2CAL(utc, FORM=8)
    str = STRMID(utc,2,2)+STRMID(utc,4,2)+STRMID(utc,6,2)+'_00'
    startt = WIDGET_TEXT(row, XSIZE=10, VALUE=str, /EDITABLE, UVALUE='START')
    row = WIDGET_BASE(col2, /ROW)
    lab = WIDGET_LABEL(row, VALUE='STOP  :')
    stopt = WIDGET_TEXT(row, XSIZE=10, /EDITABLE, UVALUE='STOP')
    junk = WIDGET_BUTTON(col2, VALUE='READ PACKETS', UVALUE='READ')

    col2 = WIDGET_BASE(col1, /COLUMN, /NONEXCLUSIVE, /FRAME, $
           /SCROLL, X_SCROLL_SIZE=200, Y_SCROLL_SIZE=400)
    FOR i=0, N_ELEMENTS(fields)-1 DO BEGIN
       button_id(i) = WIDGET_BUTTON(col2, VALUE=fields(i), UVALUE='SELECT')
    ENDFOR

    col2 = WIDGET_BASE(col1, /COLUMN, /NONEXCLUSIVE, /FRAME)
    conv = WIDGET_BUTTON(col2, VALUE='Convert to Deg C', UVALUE='CNVRT')

    col2 = WIDGET_BASE(col1, /COLUMN, /NONEXCLUSIVE, /FRAME)
    sing = WIDGET_BUTTON(col2, VALUE='Plot all on single plot', UVALUE='SINGLE')
    col2 = WIDGET_BASE(col1, /COLUMN, /NONEXCLUSIVE, /FRAME)
    negs = WIDGET_BUTTON(col2, VALUE='Plot negative temps to -50', UVALUE='NEGS')

    junk = WIDGET_BUTTON(col1, VALUE='PLOT', UVALUE='PLOT')
    junk = WIDGET_BUTTON(col1, VALUE='PRINT', UVALUE='PRINT')
    junk = WIDGET_BUTTON(col1, VALUE='SAVE AS GIF', UVALUE='SAVE')
    junk = WIDGET_BUTTON(col1, VALUE='QUIT', UVALUE='QUIT')

    draw = WIDGET_DRAW(base, XSIZE=700, YSIZE=600, /FRAME)
    WIDGET_CONTROL, draw, GET_VALUE=win      ; find out window's index no.
    negat = 0

    ;********************************************************************
    ;** REALIZE THE WIDGETS *********************************************

    WIDGET_CONTROL, /REAL, base

    FOR i=0, N_ELEMENTS(button_id)-1 DO $
       WIDGET_CONTROL, button_id(i), SET_BUTTON=selected(i)
    WIDGET_CONTROL, conv, SET_BUTTON=cnvrt
    WIDGET_CONTROL, sing, SET_BUTTON=single
    WIDGET_CONTROL, negs, SET_BUTTON=negat

    ;********************************************************************
    ;** Create a structure that will be carried around as the UVALUE of the main base (BASE)
    nonopv = { $
	base:base, 			$
	win:win, 			$
	fields:fields, 			$
	button_id:button_id, 		$
	startt:startt, 			$
	stopt:stopt, 			$
	pckts:pckts, 			$
	offs:offs, 			$
	cnvrs:cnvrs, 			$
	cnvrt:cnvrt, 			$
	single:single, 			$
	selected:selected, 		$
	negat:negat			$
    }

    ; Load the structure into the main base's UVALUE for one way trip to handler
    WIDGET_CONTROL, base, SET_UVALUE=nonopv

    IF (KEYWORD_SET(AUTO))  THEN BEGIN
       nauto=auto
       IF KEYWORD_SET(NWAIT)  THEN  nsecs=nwait ELSE nsecs=3600
       IF KEYWORD_SET(FDAY)   THEN lengthplot=fday*86400. ELSE lengthplot=86400.
       WPLOT_AUTO,nonopv,nauto,nsecs,lengthplot
    ENDIF ELSE XMANAGER, 'WPLOT_NONOP', base, EVENT_HANDLER='WPLOT_NONOP_EVENT'

RETURN
END
