!******************************************************************************!
! Tests Jeff's snapper "MODE" change by adding button and logic to perform
! a pipe init, specify new pipe processing, pipe run
!
! Arguments: None
!
! @bug      NXM-3360 Jeff is getting timeline WARN: ERROR messages on his "MODE"
!                    changes which trigger pipe inits
! @since    NeXtMidas 3.9.1
! @author:  Jeanette McCall
!******************************************************************************!
startmacro S:throttle["REAL"]
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  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 LIBNXM mode
  invoke panelExists reg.shell.M.dictionary.isDictionaryEntry(PANEL)

  if interAct and panelExists then
    set instructions "\nYou are CHECKING TIME ACROSS RESTART BOUNDARIES by causing pipe inits and restarts\n&
                      ===============================================================\n&
                      TIMES can be cheked 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 'PIPE_INIT' button.\n\n&
                      3) There should not be any warnings about back-in-time entries and the&
                         'Time at last init' should match the new beginning of the raster displays\n\n&
                      **************************************************************\n&
                      OPTIONAL ADDITIONAL tests that currently duplicate test_timeline_restart.mm\n&
                      4) Click the 'WARM_START' button for the bottom plot to do a WARM START &
                         (will be delayed behind the middle plot).\n&
                      5) Cause the first RESTART by changing the SFREQ multiplier and check times &
                         across the boundary on the top and bottom plots.\n&
                      6) Cause the second RESTART by changing the FFT FRAME Size and check times &
                         across the boundary on the top and bottom plots.\n&
                      7) In one case draw a DRAGBOX across the boundaries and examine the DRAGBOX times"


    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

      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 button "PIPE_INIT" "" "PIPE_INIT"
      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   startTime "startTimeAt" tcur 0 -1 /msg=no /edit=no
      gcontrol tval   timeAtInit "Time at last Pipe init" 0 0 -1 /flags="NoMsg|NoEdit"
      gcontrol tval   tmin "box tMin" 0 0 -1 /msg=no /edit=no
      gcontrol tval   tmax "box tMax" 0 0 -1 /msg=no /edit=no

      call pipeaction ! all the waveform / fft / plot action

    pipe off
  endif

  env set IOOptions origIOOptions ! restore original IOOptions
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
  elseif msg.name eqs "PIPE_INIT" then
    invoke tmptime nxm.sys.lib.Time.current()
    calc tcur ^tmptime 7 - ! go back this many seconds
    res gc.timeAtInit.value ^tcur
    rem reg.fft
    rem reg.fft2
    rem reg.idplotfft
    rem reg.idplotfft2
    rem reg.warmstart
    rem reg.wave
    rem reg.wavec
    pipe init
      call pipeaction
    pipe run
  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 frequency 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

!***********************************************************************
! put the real action in the pipe in here
! so we can simulate Jeff's mode change
!***********************************************************************
procedure pipeaction

      ! 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


return