! Test for starting code for Jay's DRAGBOX high resolution FFT
! @author Jeanette McCall
!
startmacro
  res oldLookAndFeel env.theme
  lookandfeel Ocean

  ! Commands used to generate test file
  ! set sr 1e4
  ! set over 0
  ! set sfreq sr/10
  ! set chirpamt sfreq/4
  ! set amp 1e4
  ! waveform/id=wavec chirped_1000.tmp format=SF elem=40k cos freq=sfreq amp=amp delta=1/sr chirp=^chirpamt start=^tcur
  ! fft IN1=chirped_1000.tmp OUT1=fft1kchirp 1k
  ! plot/id=fft1k/READOUT=Show|OnAxis|T|dT fft1kchirp.tmp view=iYX type=raster

  ! defaults
  res trimmers ""
  res qualifiers ""
  res inFileName "nxm.sys.test.chirped_1000.tmp"

  pipe on
    panel/jsetup="nxm.sys.test.test_dragbox_highres_fft"

    ! Low Res fft
    fft/id=fftl IN1=^{inFileName} OUT1=_fft_low 1k
    plot/theme=WoB/id=plotLowResRaster/MSGID=MAIN/READOUT=Show|OnAxis|T|dT|X _fft_low view=iYX type=raster
    FEATURE/id=box1 label=box1 table={NAME=BOX1,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=plotLowResRaster

    ! Starts off with same low res plot, but then is changed by dragbox
    fft/id=ffth IN1=^{inFileName}^{trimmers} OUT1=_fft_high 4k
    plot/theme=WoB/ID=plotHighResRaster/MSGID=MAIN/READOUT=Show|OnAxis|T|dT|X _fft_high^{qualifiers} view=iYX type=raster

  pipe off
  lookandfeel oldLookAndFeel
  
endmacro

!******************************************************************************!
procedure processMessage m:msg
  !say "msg.name ^msg.name, data ^msg.data fid ^msg.fid"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name " msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "DRAGBOX"  then
    if msg.fid eqss "PLOTLOWRESRASTER" then

      ! draw dragbox
      set box1.setBoundsFromTable(msg.data)

      ! update high res plot
      result tmin msg.data.tmin
      result tmax msg.data.tmax
      invoke trimmers nxm.sys.lib.DataFile.trimmersFromTimes(^{inFileName},tmin,tmax)
      invoke qualifiers nxm.sys.test.EvaluationUtil.qualifiersFromData(^{msg.data},"XMIN,XMAX")
      fft IN1=^{inFileName}^{trimmers} OUT1=ram:ffth 4k
      res file "ram:ffth^{qualifiers}"
      invoke ,, reg.plotHighResRaster.closeFile("*")
      invoke ,, reg.plotHighResRaster.openFile(file)
    endif
  else
    ! do nothing
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return
