!******************************************************************************!
! Test to ensure raster is not corrupted when drawing a dragbox / DATAX refresh
! (This creates / needs a raster file where the data is varying (increasing) in
! intensity, so any corruption of the plotted data is detected)
!
! @bug      NXM-3088 PLOT/Layer2D: The NXM 3.7.2 release has a raster drawing
!           issue when boxes are added
! @since    3.7.3
! @author:  Jeanette McCall (test file creation contributed by user)
!******************************************************************************!
startmacro
  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 CREATEFILE  createfile GET DEF=FALSE SDEF=TRUE  ! re-create the test file
  switch PRESEED     preseed    GET DEF=TRUE SDEF=TRUE  ! re-create the test file
  switch PRESCALE    prescale   GET DEF=TRUE SDEF=TRUE  ! re-create the test file



  res psd_file_name nxm.sys.test.test2000_psd.prm
  res myscaletbl {}
  res pipesize 50e50

  if createfile then
    call createPSDFile psd_file_name
  endif

  if interAct then
    set instructions "Draw a DRAGBOX on the raster.\n&
                      The dragbox should render as it is being drawn.\n&
                      The raster should not change.\n\n&
                      ***************************\n&
                      Note: This tests that the DATAX refresh &
                      used when drawing the box does not corrupt &
                      the raster plot."

   if prescale then
     !call prescaletest
     call pipeprescaletest
   else
     call pipetest
   endif

  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the 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
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Regular pipe test
!******************************************************************************!
procedure pipetest
   pipe on
      panel/msgid=MAIN/setup/controls=gc
     gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

     gcontrol text  idtext "Instructions" instructions 24 10 /title

     plot/all psd_file_name
   pipe off
return

!******************************************************************************!
! Special pipe prescale test
!******************************************************************************!
procedure pipeprescaletest
   pipe on
     panel/msgid=MAIN/setup/controls=gc
     gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

     gcontrol text  idtext "Instructions" instructions 24 10 /title

     prescaler IN=psd_file_name OUT=ram:scalerout{ps=^pipesize} SCALETABLE=myscaletbl
     plot/all ram:scalerout{ps=^pipesize} SCALETABLE=myscaletbl /PRESCALE
   pipe off
return


!******************************************************************************!
! Special no pipe prescale test - /PRESCALE switch is not needed if we leave code as is,
! but may be needed if switch to true backwards compatibility
!******************************************************************************!
procedure prescaletest
  prescaler IN=psd_file_name OUT=ram:scalerout SCALETABLE=myscaletbl
  plot/all ram:scalerout SCALETABLE=myscaletbl
return

!******************************************************************************!
! Generate imitation Power Spectral Density file for testing
! (This is included for reference and should not generally be regenerated)
! 
!******************************************************************************!
procedure createPSDFile S:psd_file_name

  ! Note: added meaningful YStart and YDelta in 3.8.1
  headermod/create psd_file_name type=2000 form=sf FS=1024 &
    XUnits=Freq XStart=0 XDelta=1 &
    YUnits=Time YStart=2.161187381679256E9 YDelta=0.001

  file OPEN/D/A filetag psd_file_name
  
  ! Create File with 1025 frames of data
  do i 0 1024 1
    res T:data {}

    ! Bins 50-250 contain the signal data
    do j 50 250
      ! signal strength gradually increases each frame
      calc data.F^{j} i
    enddo

    ! Adding a bin with max signal strength to foce autoscaling
    ! to work correctly for this situation
    ! (Note: This will result in a very light yellow horizontal line)
    if preseed then
      res data.F1000 1024 ! <-- Trick to pre-seed autoscaling
    endif

    invoke filetag.writeDataTable(data)
  enddo

  file close filetag

return