;+
; NAME:
;     WTESTPLOT
; PURPOSE:
;     Widget routine to test plotting and reading a file that is
;     growing in real time.
; CATEGORY:
;     OVRO APC DATA ANALYSIS
; CALLING SEQUENCE:
;     wtestplot,filename,srec
; INPUTS:
;     filename   the name of the file containing data to be plotted.
;     srec       the record number to start plotting from.  Plotting
;                  will continue until an EOS segment is found.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 13-Jan-1998 by Dale Gary
;-

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

PRO WTestPlot_Event, Event

  base = Event.handler
  stash = WIDGET_INFO(base, /CHILD)
  WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY

  IF (Event.Id EQ state.bgroup) THEN $
       Ev=Event.Value ELSE WIDGET_CONTROL,Event.Id,GET_UVALUE=Ev

  IF (TAG_NAMES(Event, /STRUCTURE_NAME) EQ $
                            'WIDGET_KILL_REQUEST') THEN BEGIN
     free_lun,state.lun
     WIDGET_CONTROL, Event.top, /DESTROY
     RETURN
  ENDIF

  CASE Ev OF

  'Ant 1': BEGIN                    ;Event for Ant 1 button
        IF (Event.Select EQ 1) THEN state.ant=1
        state.rec = state.srec
        wset,state.wid
        plot,[0,state.nx],[0,state.maxamp],/nodata
        state.off = indgen(state.header.nsr)
        WIDGET_CONTROL, state.wDraw, TIMER=1
      END
  'Ant 2': BEGIN                    ;Event for Ant 2 button
        IF (Event.Select EQ 1) THEN state.ant=2
        state.rec = state.srec
        wset,state.wid
        plot,[0,state.nx],[0,state.maxamp],/nodata
        state.off = indgen(state.header.nsr)
        WIDGET_CONTROL, state.wDraw, TIMER=1
      END
  'Ant 4': BEGIN                    ;Event for Ant 4 button
        IF (Event.Select EQ 1) THEN state.ant=3
        state.rec = state.srec
        wset,state.wid
        plot,[0,state.nx],[0,state.maxamp],/nodata
        state.off = indgen(state.header.nsr)
        WIDGET_CONTROL, state.wDraw, TIMER=1
      END
  'Ant 5': BEGIN                    ;Event for Ant 5 button
        IF (Event.Select EQ 1) THEN state.ant=4
        state.rec = state.srec
        wset,state.wid
        plot,[0,state.nx],[0,state.maxamp],/nodata
        state.off = indgen(state.header.nsr)
        WIDGET_CONTROL, state.wDraw, TIMER=1
      END
  'Ant 6': BEGIN                    ;Event for Ant 6 button
        IF (Event.Select EQ 1) THEN state.ant=5
        state.rec = state.srec
        wset,state.wid
        plot,[0,state.nx],[0,state.maxamp],/nodata
        state.off = indgen(state.header.nsr)
        WIDGET_CONTROL, state.wDraw, TIMER=1
      END

  'TIMER': BEGIN
        a = assoc(state.lun,intarr(1024))
;        lun = openarc(state.filename,a,nrec)
        ON_IOERROR,set_timer
        rec = state.rec
        off = state.off
        nws = state.header.nws
        nsr = state.header.nsr         ; Number of samples/record
        rdoff = state.header.rdoff     ; Offset to start of raw data
        nndw = 1 + state.header.ndbw + state.header.nfgw
        nchan = nws - nndw             ; Number of data channels
        ant = state.ant
        ;sec = tls.msec
        wset,state.wid
        while (1) do begin
           data = a(rec-1)
           tls = tl_decode(data)
           if (tls.segmentcode EQ 8) then goto, done
           if (tls.segmentcode EQ 0) then goto, set_timer
           tp = data(nws*indgen(nsr)+rdoff+nndw+ant-1)
           oplot,(off mod state.nx),tp,psym=3
           rec = rec + 1
           off = off + nsr
        endwhile

   set_timer:
        state.rec = rec
        WIDGET_CONTROL, state.wDraw, TIMER=5
   done:
        state.rec = state.srec
      END

  ENDCASE

WRITE_STATE:
  WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY

END

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


PRO WTestplot, filename, srec, GROUP=Group

  ; Open the file, and exit on failure
  lun = openarc(filename,a,nrec)
  IF (lun EQ 0) THEN return

  IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0

  wBase = WIDGET_BASE(GROUP_LEADER=Group, $
      COLUMN=1, $
      MAP=1, $
      /TLB_KILL_REQUEST_EVENTS,$
      TITLE='WTestPlot', $
      UVALUE='MainBase')

  ExtraBase = WIDGET_BASE(wBase, $
      COLUMN=1, $
      MAP=1, $
      TITLE='ExtraBase', $
      UVALUE='ExtraBase')

  wDraw = WIDGET_DRAW(ExtraBase, $
      XSIZE=600, YSIZE=480,$
      UVALUE='TIMER')

  BGroup = CW_BGROUP(ExtraBase, $
      ['Ant 1','Ant 2','Ant 4','Ant 5','Ant 6'], $
      /EXCLUSIVE, SET_VALUE=0, /ROW, /RETURN_NAME)

  WIDGET_CONTROL, wBase, /REALIZE

  WIDGET_CONTROL, wDraw, GET_VALUE=wid

  ant = 1
  nx = 500   ; Default number to use in case there is no OBSEQ segment
  maxamp = 2200 ; Default maximum amplitude for plot
  ON_IOERROR,bail

  ; Find the start of the data segment
  tls = tl_decode(a(srec-1))
  IF (tls.segmentcode EQ 1) THEN BEGIN     ; This is a header
     out = dumprec(a(srec-1),header)
     nws = header.nws
     nsr = header.nsr                      ; Number of samples/record
     nndw = 1 + header.ndbw + header.nfgw
     nchan = nws - nndw                    ; Number of data channels
  ENDIF

  while (tls.segmentcode NE 7) do begin    ; We would normally gather
     srec = srec + 1                       ; header and config info
     tls = tl_decode(a(srec-1))            ; here
     IF (tls.segmentcode EQ 3) THEN BEGIN  ; This is an OBSEQ
        nx = (a(srec-1))(26)  ; Number of entries
        IF (nx EQ 1) THEN BEGIN
           ans = widget_message('Is this FCAL data?',/Question)
           IF (ans EQ 'Yes') THEN BEGIN
              nx = 500
              maxamp = 20
           ENDIF ELSE BEGIN
              nx = (a(srec-1))(25)  ; If only 1 entry, assume
              maxamp = 2200         ; TC mode and use number
           ENDELSE                     ; of times repeated
        ENDIF
     ENDIF
  endwhile

  ; Found the data (srec now points to first data record)
  ON_IOERROR,set_timer
  rec = srec
;  msec = tls.msec
;   datstr = '98/01/13 00:00'
;   utplot,[0.,7200.]+ msec/1000.,[0,2000],/nodata,datstr
  wset,wid
  plot,[0,nx],[0,maxamp],/nodata
  off = indgen(nsr)
;   tls = tl_decode(a(rec))
;   if (tls.segmentcode EQ 8) then goto,done
;   msec = tls.msec
   while (1) do begin
      data = a(rec-1)
      tls = tl_decode(data)
      if (tls.segmentcode EQ 8) then goto, continue
      if (tls.segmentcode EQ 0) then goto, set_timer
      tp = data((nws)*indgen(nsr)+header.rdoff+nndw+ant-1)
      oplot,(off mod nx),tp,psym=3
      rec = rec + 1
      off = off + nsr
   endwhile

set_timer:
  WIDGET_CONTROL, wDraw, TIMER=5

continue:
  state = {ant:1, wDraw:wDraw, BGroup:BGroup, wid:wid,$
           rec:rec, off:off, srec:srec, $
           header:header, lun:lun, nx:nx, maxamp:maxamp}

  WIDGET_CONTROL, ExtraBase, SET_UVALUE=state, /NO_COPY

  XMANAGER, 'WTestPlot', wBase

bail:
;  free_lun,lun

END
