!******************************************************************************!
! Primarily tests that pipe restarts do not corrupt the timeline
! Secondarily tests the special features of Layer2D for selecting time boxes in
! a raster plot.
!
! Arguments: throttle - Waveform data throttle mode
!
! Since 3.7.2 for NXM-3040 add frame size restarts and timeline value checking
! Adjusted in 3.7.3 & 3.9.0 with NXM-3073 frame size pipe restarts are now working
! Note: this test can also be run with throttle=full to isolate RESTART issues
!
! Author:  Neon Ngo, Jeanette McCall
!******************************************************************************!
startmacro S:throttle["REAL"]
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE     baseline     GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1

  env get IOOptions origIOOptions
  !env set IOOptions +FixPipeRestartTimeLines  ! No longer needed in 3.9.4

  ! Only allow interAct if PANEL command Exists - may be running in thin mode
  invoke panelExists reg.shell.M.dictionary.isDictionaryEntry(PANEL)

  if (interAct OR coreGraphics) AND panelExists then
    set instructions "\nYou are CHECKING TIME ACROSS RESTART BOUNDARIES by causing pipe restarts\n&
                      ===============================================================\n&
                      CHECK TIMES by LMB clicking right before and after the restart and examining &
                      the 'Time at ... click' displays\n&
                      ----------------------------------------------------------------\n&
                      PASS if across restarts TIME STAYS the SAME or SLIGHTLY INCREASES.\n\n&
                      1) WAIT until the plots are a portion of the way across the screen\n\n&
                      2) Click the 'WARM_START' button for the bottom plot to do a WARM START &
                         (will be delayed behind the middle plot).\n\n&
                      3) Cause the first RESTART by changing the SFREQ multiplier and check times &
                         across the boundary on the top and bottom plots.\n\n&
                      4) Cause the second RESTART by changing the FFT FRAME Size and check times &
                         across the boundary on the top and bottom plots.\n\n&
                      5) In one case draw a DRAGBOX across the boundaries and examine the DRAGBOX times\n\n&
                     **************************************************************\n\n&
                      ADVANCED: The middle plot shows the fft of a signal that is chirped by 1/4fs every 4k samples. &
                      Restarts from frame size changes show that processing starts up with re-processing the current input &
                      data (i.e frequency progression from chirp should continue where it left off)"

    set format sf
    set sr 1e4
    set over 0
    set sfreq sr/10
    set chirpamt sfreq/4
    set amp 1e4
    set noiseamp 1e2
    set fsize 512
    set numelem 10e10
    set firstClick true

    invoke tcur nxm.sys.lib.Time.current()

    pipe init
      panel/msgid=MAIN/setup/controls=gc

      ! Create waveforms with adjustable frequency (2nd one has frequency chirped each TL)
      waveform/id=wave/throttle=^throttle _wave format elem=numelem cos freq=sfreq amp=amp delta=1/sr start=^tcur
      waveform/id=wavec/throttle=^throttle/tl=4k _wavechirp format elem=numelem cos freq=sfreq amp=amp delta=1/sr chirp=^chirpamt start=^tcur
      ! Create ffts of the 2 waveforms (frame size can be adjusted using gcontrol)
      fft/dbf/id=fft _wave _fft{form=sf} fsize over=over
      fft/dbf/id=fft2 _wavechirp _fft2 fsize over=over
      ! plot the ffts
      plot/id=idplotfft/msgid=MAIN/READOUT=Show|OnAxis|T|dT _fft view=iXY 
      plot/id=idplotfft2/msgid=MAIN/READOUT=Show|OnAxis|T|dT _fft2 view=iXY
      plot/id=warmstart/msgid=MAIN/READOUT=Show|OnAxis|T|dT _fft2 view=iXY

      ! add box feature to plot for creating a user selectable dragbox on the raster plot
      FEATURE/id=box1 label=box1 table={NAME=BOX1,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=idplotfft
      FEATURE/id=box2 label=box2 table={NAME=BOX2,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=idplotfft2

      gcontrol label  macro   "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "Instructions" "^instructions" 20 30 /title

      gcontrol label  plot  "PLOT/TIMELINE"
      gcontrol button "WARM_START" "" "WARM_START"
      gcontrol choice sfreq "Set SFREQ (multiplier):" ".25,.5,1,2,3,4" "1" /tleft
      gcontrol choice framesz "Set FFT FRAME Size:" "256,512,1k" "512" /tleft
      gcontrol tval   timeAtPos1 "Time at 1st click" 0 0 -1 /flags="NoMsg|NoEdit"
      gcontrol tval   timeAtPos2 "Time at 2nd click" 0 0 -1 /flags="NoMsg|NoEdit"
      gcontrol tval   tcurr "startTimeAt" tcur 0 -1 /msg=no /edit=no
      gcontrol tval   tmin "box tMin" 0 0 -1 /msg=no /edit=no
      gcontrol tval   tmax "box tMax" 0 0 -1 /msg=no /edit=no

    pipe off
  endif

  env set IOOptions origIOOptions
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "WARM_START" then
    message send id=warmstart name="OPENFILE" info=-1 data=_fft2
!    plot/id=warmstart/msgid=MAIN/READOUT=Show|OnAxis|T|dT _fft2 view=iXY
  elseif msg.name eqs "DRAGBOX"  then
    if msg.fid eqss "IDPLOTFFT" then
      result tmin msg.data.tmin
      result tmax msg.data.tmax
      timex tmin d:gc.tmin.value
      timex tmax d:gc.tmax.value
      if msg.fid eqs "IDPLOTFFT2" then
        ! second fft from signal with noise added in 3.7.3
        set box2.setBoundsFromTable(msg.data)
      else
        set box1.setBoundsFromTable(msg.data)
      endif
    endif
  elseif msg.name eqs "SFREQ" then
    ! set frequwency of both waveforms
    calc newfreq sfreq ^msg.data *
    set reg.wave.freq  ^newfreq
    set reg.wavec.freq  ^newfreq
    set reg.wave.state "RESTART"
    set reg.wavec.state "RESTART"
  elseif msg.name eqs "FRAMESZ" then ! since 3.7.2 for NXM-3040
    ! set framse size of both ffts
    res fsize msg.data
    set reg.fft.nfft fsize
    set reg.fft2.nfft fsize
  elseif msg.name eqs "MARK"
    ! set the "Time At # Click" gcontrols
    if firstClick then
      res gc.timeAtPos1.value msg.data.time
      res firstClick false
    else
      res gc.timeAtPos2.value msg.data.time
      res firstClick true
    endif
  elseif msg.name eqs "UNZOOM"
    if msg.fid eqs "IDPLOTFFT" then
      invoke reg.idplotfft.refresh()
    elseif msg.fid eqs "IDPLOTNOOP" then
      invoke reg.idplotnoop.refresh()
    endif
  elseif msg.name eqs "ZOOM"
  else
    say "Unhandled message name=^msg.name info=^msg.info fid=^msg.fid data=^msg.data"
  endif

return