!******************************************************************************!
! 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 COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE ! for a quick check of graphics - more extreme tests - since 3.8.1

  switch CREATEFILE  createfile    GET DEF=FALSE SDEF=TRUE  ! re-create the test file
  switch NOPRESCALE  noprescale    GET DEF=FALSE SDEF=TRUE  ! do not do prescaling
  switch OUTSIDEPIPE outsidepipe   GET DEF=FALSE SDEF=TRUE  ! do test outside pipe
  switch RANDOM      randomPercent GET DEF=100   SDEF=10    ! get percentage of random frames/blocks to use
  switch MAKECOPY    makeCopy      GET DEF=FALSE SDEF=TRUE  ! have prescaler make a copy of the input file

  res psd_file_name nxm.sys.test.test2000_psd.prm
  res myscaletbl {}

  if createfile then
    call createPSDFile psd_file_name
  endif

  if interAct or coreGraphics then
    set instructions "The raster plot should render propertly.\n&
                      None of the raster should not be largely white or off-white.\n\n&
                      This test can be run with the switch NOPRESCALE to see the behavior without prescaling\n\n&
                      It can also be run with the switch OUTSIDE pipe to see performance when run outside of a pipe"

   if noprescale then
     if outsidepipe then
       call outsidepipe
     else
       call pipetest
     endif
   else
     if outsidepipe then
       call prescaletest
     else
       call pipeprescaletest
     endif
   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 20 /title

    noop psd_file_name _psd 
    plot/all _psd
  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 20 /title

     noop psd_file_name _psdin
     if MAKECOPY then
       prescaler/random=^randomPercent IN=_psdin OUT=ram:prescale_out SCALETABLE=myscaletbl
       noop ram:prescale_out _psd
     else
       prescaler/random=^randomPercent IN=_psdin SCALETABLE=myscaletbl
       noop psd_file_name _psd
     endif
     plot _psd scaletable=myscaletbl /PRESCALE=EXT
   pipe off
return


!******************************************************************************!
! outside pipe prescale test
!******************************************************************************!
procedure prescaletest
  if MAKECOPY then
    prescaler/random=^randomPercent IN=psd_file_name OUT=ram:prescale_out SCALETABLE=myscaletbl
    plot/all ram:prescale_out scaletable=myscaletbl /PRESCALE=EXT
  else
    prescaler/random=^randomPercent IN=psd_file_name SCALETABLE=myscaletbl
    plot/all psd_file_name scaletable=myscaletbl /PRESCALE=EXT
  endif
return

!******************************************************************************!
! outside pipe no prescale test
!******************************************************************************!
procedure outsidepipe
  plot/all psd_file_name
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

    invoke filetag.writeDataTable(data)
  enddo

  file close filetag

return