!******************************************************************************!
! Test plot prescaling - 2000 portion 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 RANDOM      randomPercent GET DEF=100   SDEF=10    ! get percentage of random frames/blocks to use

  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}"

  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 with plot/all in various scenarios\n\n&
                      Left Column - outside a pipe\n\n&
                      Right Column - inside a pipe\n\n&
                      Top Row  - no prescaling\n           &
                                     (bad)\n\n&
                      The Rest - use prescaling\n           & 
                                     (good & all match)"

   ! **********  2000 FILE TESTING - OUTSIDE A PIPE************************
   ! Show what it is like without the fix
   plot/id=outnofix/all/bg psd_file_no_kw

   ! Use external pre-scale to save scale values in both a scale table and default KEYWORD
   prescaler/random IN=psd_file_no_kw SCALETABLE=scaletbl_outside
   prescaler/random IN=psd_file_kw /SCALEKW

   ! plot based on the default keyword
   plot/id=outkw/all/bg FILE=psd_file_kw /PRESCALE=kw /SCALEKW
   ! plot based on the scale table
   plot/id=outext/all/bg FILE=psd_file_no_kw SCALETABLE=scaletbl_outside /PRESCALE=ext

   ! Use plot's internal prescaler and make a copy (and check the copy)
   plot/id=outinternalcopy/all/bg FILE=psd_file_no_kw OUTCOPY=plot_makes_copy.tmp{AUX=RAM} /PRESCALE
   assert/text="^reg.main.name: plot internal prescaler makes a copy with correct header" psd_file_no_kw FEQ/H plot_makes_copy.tmp{AUX=RAM}
   assert/text="^reg.main.name: plot internal prescaler makes a copy with correct data" psd_file_no_kw FEQ/T/D plot_makes_copy.tmp{AUX=RAM}

   ! **********  2000 FILE 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 20 /title

     noop psd_file_no_kw _psd_no_kw

     ! external prescaler in pipe sets setuptable
     prescaler/random=^randomPercent IN=_psd_no_kw OUT=ram:prescale_out_ext SCALETABLE=scaletbl_pipe
     plot/id=pipeext/all ram:prescale_out_ext SCALETABLE=scaletbl_pipe /PRESCALE=EXT

     ! without prescaling when reading from file in pipe
     plot/id=pipebad _psd_no_kw

     ! internal prescaler
     plot/id=pipeinternal/all psd_file_no_kw /PRESCALE={TYPE=INTERNAL,RANDOM=^randomPercent}

   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=outnofix NAME=EXIT
    message FUNC=send ID=outkw NAME=EXIT
    message FUNC=send ID=outext NAME=EXIT
    message FUNC=send ID=outinternalcopy 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 pane1 reg.win.OUTNOFIX
    invoke ,, pane1.attach(reg.OUTNOFIX,"OUTNOFIX")
    res pane2 reg.win.OUTKW
    invoke ,, pane2.attach(reg.OUTKW,"OUTKW")
    res pane3 reg.win.outext
    invoke ,, pane3.attach(reg.outext,"OUTEXT")
    res pane4 reg.win.outinternalcopy
    invoke ,, pane4.attach(reg.outinternalcopy,"OUTINTERNALCOPY")
  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/random=^randomPercent FILE=_zero_padded_in OUTCOPY=ram:prescale_out_pipeprescaletest /PRESCALE=kw /SCALEKW
     else
       plot/all/random=^randomPercent FILE=_zero_padded_in /PRESCALE=kw /SCALEKW
     endif
   pipe off
return


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

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