!******************************************************************************!
! Test plot prescaling - precedence order - part of MAIN baseline test run by
! test_plot_prescaler.mm
!******************************************************************************!
!
! NOTE: The various other very controllable and specific prescaler tests are being
! KEPT, so they can be used when finer control / diagnostic testing is needed
!
! @bug      NXM-3078 PLOT: Does not display zero-padded Pre-D data
! @since    3.9.1
! @author:  Jeanette McCall
!******************************************************************************!
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 DEBUG       debug    GET DEF=FALSE SDEF=TRUE

  switch RANDOM      randomPercent GET DEF=100   SDEF=10    ! get percentage of random frames/blocks to use

  if DEBUG then
    res debugSwitch "/DEBUG=PRESCALE"
  else
    res debugSwitch ""
  endif

  res scaletbl_outside {} ! outside pipe
  res scaletbl_pipe     {} ! inside pipe

  res psd_file_no_kw   "nxm3078_test2000_psd_no_kw.tmp{aux=ram}"
  res psd_file_kw      "nxm3078_test2000_psd_kw.tmp{aux=ram}"
  res psd_file_gets_kw "nxm3078_test2000_psd_gets_kw.tmp{aux=ram}"
  info "Will see warning messages about not running prescaler=ext due to no scaletable and not being in a pipe section"
  if interAct then
    file copy/f nxm.sys.test.test2000_psd.prm psd_file_no_kw
    file unprotect psd_file_no_kw
    file copy/f nxm.sys.test.test2000_psd.prm psd_file_kw
    file unprotect psd_file_kw
    file copy/f nxm.sys.test.test2000_psd.prm psd_file_gets_kw
    file unprotect psd_file_gets_kw
    set instructions "A 2000 file with increasing intensity is plotted in various scenarios\n\n&
                      Top-Left   - defaulted to autoscale\n             &
                                   (bad)\n&
                      Bottom Row - true piped and wrapped\n             &
                                   (shades of yellow) except\n             &
                                   bottom right which will\n             &
                                   z1 and z2 on view change\n&
                      The rest   - Good, though random\n             &
                                   prescale may not yield\n             &
                                   perfect results"

   ! **********  PRIORITY TESTING - OUTSIDE A PIPE************************
   ! put keyword in file, for test with kw
   prescaler IN=psd_file_kw /SCALEKW

   ! Show that /PRESCALE will do internal prescaling no matter what ever else is specified
   plot/id=outinternal/all/bg psd_file_no_kw Z1=0 Z2=100 SETUP={SCALETABLE={Z1=0,Z2=250}} SCALETABLE={Z1=0,Z2=500} /PRESCALE

   ! /PRESCALE=EXT looks at SCALETABLE
   ! uses separate scaletable Changed to use /PRESCALE={type=EXT,scaletable={Z1=0,Z2=1024}} to test table defined in the prescale switch
   plot/id=outextscaletable/all/bg psd_file_no_kw scaletable={z1=0,z2=2} /PRESCALE={type=EXT,scaletable={Z1=0,Z2=1024}}
   ! uses setup scaletable
   plot/id=outextsetup/all/bg psd_file_no_kw Z1=0 Z2=100 SETUP={SCALETABLE={Z1=0,Z2=1024}} /PRESCALE=EXT
   ! uses Z1 Z2
   plot/id=outextz1z2/all/bg psd_file_no_kw Z1=0 Z2=1024.0 /PRESCALE=EXT
   ! autoscaling
   plot/id=outextnone/all/bg psd_file_no_kw /PRESCALE=EXT

   ! /PRESCALE=KEYWORD
   ! uses keyword
   plot/id=outkwexists/all/bg FILE=psd_file_kw Z1=0 Z2=100 SETUP={SCALETABLE={Z1=0,Z2=250}} SCALETABLE={Z1=0,Z2=500} /PRESCALE=kw
   ! internal prescale
   plot/id=outkwnexists/all/bg FILE=psd_file_gets_kw Z1=0 Z2=100 SETUP={SCALETABLE={Z1=0,Z2=250}} SCALETABLE={Z1=0,Z2=500} /PRESCALE=kw


   ! **********  PRIORITY TESTING - INSIDE A PIPE************************
   set this.timer(0) 1

   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 24 /title

     noop psd_file_no_kw   _psd_no_kw
     noop psd_file_kw      _psd_kw
     noop psd_file_gets_kw _psd_gets_kw

     ! Show that /PRESCALE will do internal prescaling no matter what ever else is specified'
     if DEBUG then
       say "\nacting on PRESCALE switch and ignoring other things "
     endif
     plot/id=pipeinternal/all^debugswitch _psd_no_kw Z1=0 Z2=100 OUTCOPY="pipeinternal.tmp{aux=ram}" SETUP={SCALETABLE={Z1=0,Z2=250}} SCALETABLE={Z1=0,Z2=500} /PRESCALE

     ! /PRESCALE=EXT looks at SCALETABLE
     ! uses separate scaletable
     if DEBUG then
       say "\nacting on SCALETABLE entry directly specified on command line - problem case"
     endif
     plot/id=pipeextscaletable^debugswitch _psd_no_kw Z1=0 Z2=100 SETUP={SCALETABLE={Z1=0,Z2=500}} SCALETABLE={Z1=0,Z2=1024} /PRESCALE=EXT
     ! uses setup scaletable
     if DEBUG then
       say "\nacting on SCALETABLE entry inside SETUP table - problem case"
     endif
     plot/id=pipeextsetup^debugswitch _psd_no_kw Z1=0 Z2=100 SETUP={SCALETABLE={Z1=0,Z2=1024}} /PRESCALE=EXT
     ! note that in a pipe the plot.process() will wait for a table to arrive - IT WILL WAIT/HANG until one arrives

     ! /PRESCALE=KEYWORD
     ! uses keyword
     if DEBUG then
       say "\nacting on KEYWORD"
     endif
     plot/id=pipeoutkwexists^debugswitch FILE=_psd_kw Z1=0 Z2=100 SETUP={SCALETABLE={Z1=0,Z2=250}} SCALETABLE={Z1=0,Z2=500} /PRESCALE=kw
     ! internal prescale - needs /all because is actually using a temporary file
     if DEBUG then
       say "\nINTERNAL prescale because keyword not found"
     endif
     plot/id=pipeoutkwnexists/all^debugswitch FILE=_psd_no_kw Z1=0 Z2=100  OUTCOPY="pipeoutkwnexists.tmp{aux=ram}" SETUP={SCALETABLE={Z1=0,Z2=250}} SCALETABLE={Z1=0,Z2=500} /PRESCALE=kw

   pipe off

  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"
    message FUNC=send ID=outinternal NAME=EXIT
    message FUNC=send ID=outextscaletable NAME=EXIT
    message FUNC=send ID=outextsetup NAME=EXIT
    message FUNC=send ID=outextz1z2 NAME=EXIT
    message FUNC=send ID=outextnone NAME=EXIT
    message FUNC=send ID=outkwexists NAME=EXIT
    message FUNC=send ID=outkwnexists NAME=EXIT
    pause 0.1
    pipe stop
  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0
    !say "message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
    res pane reg.win.outextnone
    invoke ,, pane.attach(reg.outextnone,"OUTEXTNONE")
    res pane reg.win.outinternal
    invoke ,, pane.attach(reg.outinternal,"OUTINTERNAL")
    res pane reg.win.outextscaletable
    invoke ,, pane.attach(reg.outextscaletable,"OUTEXTSCALETABLE")
    res pane reg.win.outextsetup
    invoke ,, pane.attach(reg.outextsetup,"OUTEXTSETUP")
    res pane reg.win.outextz1z2
    invoke ,, pane.attach(reg.outextz1z2,"OUTEXTZ1Z2")
    res pane reg.win.outkwexists
    invoke ,, pane.attach(reg.outkwexists,"OUTKWEXISTS")
    res pane reg.win.outkwnexists
    invoke ,, pane.attach(reg.outkwnexists,"OUTKWNEXISTS")
  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

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure generate1000data
    ! Generate 0-padded type 1000 data
    waveform ram:f1 FORM=CI ELEM=67500  SHAPE=CONST AMP=0  !DR value 67500
    waveform ram:f2 FORM=CI ELEM=30500  SHAPE=CONST AMP=0  !DR value 30500
    waveform ram:f3 FORM=CI ELEM=672000 SHAPE=CONST AMP=1  !DR value 672000
    waveform ram:f4 FORM=CI ELEM=30000  SHAPE=CONST AMP=0  !DR value 30000
    waveform ram:f5 FORM=CI ELEM=7000   SHAPE=CONST AMP=1  !DR value 7000
    mergefile ram:f1 zero_padded_data
    mergefile ram:f2 zero_padded_data
    mergefile ram:f3 zero_padded_data
    mergefile ram:f4 zero_padded_data
    mergefile ram:f5 zero_padded_data
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
  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

!******************************************************************************!
! 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 zero_padded_data _zero_padded 
    plot/all _zero_padded
  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 zero_padded_data _zero_padded_in
     if MAKECOPY then
       plot/all FILE=_zero_padded_in OUTCOPY=ram:prescale_out_pipeprescaletest /PRESCALE=kw /SCALEKW
     else
       plot/all FILE=_zero_padded_in /PRESCALE=kw /SCALEKW
     endif
   pipe off
return


!******************************************************************************!
! outside pipe prescale test
!******************************************************************************!
procedure prescaletest
  if MAKECOPY then
    plot/all FILE=zero_padded_data OUTCOPY=ram:prescale_out_prescaletest /PRESCALE=kw /SCALEKW
  else
    plot/all FILE=zero_padded_data /PRESCALE=kw /SCALEKW
  endif
return

!******************************************************************************!
! outside pipe no prescale test
!******************************************************************************!
procedure outsidepipe
  plot/all zero_padded_data
return
