! Test the rapid OPENFILE or swapping of plotted files. BUGs 2151, 1975.
!
! Author:  J. Hallam / J. Bauer / Neon Ngo
! Version: $Id: test_plot_file_swap.mm,v 1.6 2013/02/25 19:33:45 jwb Exp $
!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE
  switch KEEP        keep     GET DEF=FALSE SDEF=TRUE

  res s:currFile "NONE"
  set file1    "test_plot_file_swap_1d_1.tmp"
  set file2    "test_plot_file_swap_1d_2.tmp"
  set psdfile1 "test_plot_file_swap_psd_1.tmp"
  set psdfile2 "test_plot_file_swap_psd_2.tmp"
  set interval   2.0
  set amp  10
  set xd   4.096e-5
  set size 1m
  set format "CI"
  ! to duplicate Bug 2151, set file2 scale to be out of range on file1 (x seconds beyond last element)
  calc start2 size xd * 1.0 +

  if interAct then
    wave file1 form=format elem=size shape=white amp=amp delta=xd
    wave file2 form=format elem=size shape=white amp=amp delta=xd start=start2
    fft/psd/log/mag file1 psdfile1
    fft/psd/log/mag file2 psdfile2

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplot/READOUT="+OnAxis|+DateString"
      plot/id=psdplot/all/fs=1m/READOUT="+T|+iX" view=iYX cm=mag
      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" "Check that the PLOTS change files:\n&
                                          1) if'SWAP FILE' is pressed\n&
                                          2) if 'SWAP EVERY ^interval SEC' is pressed\n\n&
                                          Note: If you don't see line or PSD PLOT on file2/psdfile2, &
                            the X scale is wrong (zoom/pan 1 sec beyond the end of file1/psdfile2 to see it)." 24 10 /title

      gcontrol button swapButt "" "SWAP FILE" none

      gcontrol button auto "" "SWAP EVERY ^interval SEC"

      message send id=main name="SWAPBUTT"
    pipe off

    if keep isFALSE then
      erase/quiet file1 file2 psdfile1 psdfile2
    endif
  endif

endmacro

!************************************************************
procedure processMessage m:msg

  if msg.name eqs "MAIN" then
    assert/text="Plot Bug 2151 files tests" msg.data eqss "PASS"
    set this.timer(0) 0.0
    pipe stop
  elseif msg.name eqs "SWAPBUTT" then
    set this.timer(0) 0.0
    call processFiles
  elseif msg.name eqs "TIMER" then
    call processFiles 2.0
  elseif msg.name eqs "AUTO" then
    call processFiles
    set this.timer(0) interval
  endif

return

!************************************************************
procedure processFiles d:pauseTime[0.0]
  if currFile eqs "NONE" then
    set currFile file1
    set currPsd  psdfile1
  elseif currFile eqs file1
    set currFile file2
    set currPsd  psdfile2
  elseif currFile eqs file2
    set currFile file1
    set currPsd  psdfile1
  endif

  !info "Opening ^currFile"
  message send id=idplot  name="OPENFILE" info=-1 data="^currFile"
  message send id=psdplot name="OPENFILE" info=-1 data="^currPsd"

  if pauseTime gt 0.0 then
    pause ^pauseTime
  endif
return
