!******************************************************************************!
! MAIN TEST for testing both external (directly using prescaler) and internal
! plot prescaling.  This tests checks a large zero padded 1000 files in various
! scenarios and then calls the baseline 2000 prescaling test.
!
! 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 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 2000 test file
  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

  if randomPercent lt 21 then
    res possibleWarn "WARN: View Y Min and Y Max both set to ..."
    info "When doing only a random sampling for scaling, it is 'okay' to see '^{possibleWarn}' as it does not always result in good scale values"
  endif

  res zero_padded_data ram:data
  res psd_file_name nxm.sys.test.test2000_psd.prm

  ! create the 1000 data
  call generate1000data
  ! create copies so that there are no race conditions for each test
  file copy/f zero_padded_data zero_padded_data2
  file copy/f zero_padded_data zero_padded_data3
  ! the 2000 data should be available, but can be recreated with the /CREATEFILE switch
  if createfile then
    call createPSDFile psd_file_name
  endif

  if interAct OR coreGraphics then
    set instructions "A large zero padded 1000 file 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 Right - no prescaling\n           &
                                     (bad)\n\n&
                      The Rest  - use prescaling\n           & 
                                     (good & all match)"

   ! **********  1000 FILE TESTING - OUTSIDE A PIPE************************
   ! Show what it is like without the fix
   plot/id=outnofix/all/bg zero_padded_data
   ! Use external pre-scale to save scale values in both a scale table and default KEYWORD
   prescaler/random=^randomPercent IN=zero_padded_data SCALETABLE=scaletbl_outside   ! making 2 separate commands until timing issues fixed
   prescaler/random=^randomPercent IN=zero_padded_data /SCALEKW
   ! plot based on the default keyword
   plot/id=outkw/all/bg FILE=zero_padded_data /PRESCALE=kw /SCALEKW
   ! plot based on the scale table
   plot/id=outext/all/bg FILE=zero_padded_data SCALETABLE=scaletbl_outside /PRESCALE=ext

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

   ! **********  1000 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 zero_padded_data3 _zero_padded_in

     ! external prescaler in pipe sets setuptable
     prescaler/random=^randomPercent IN=_zero_padded_in OUT=ram:prescale_out SCALETABLE=scaletbl_pipe
     plot/id=pipeext/all ram:prescale_out{LAYER={COLOR=BLUE}} SCALETABLE=scaletbl_pipe /PRESCALE=EXT

     ! without prescaling when reading from file in pipe
     plot/id=pipebad/all ram:prescale_out

     ! internal prescaler
     plot/id=pipeinternal/all/random=^randomPercent _zero_padded_in OUTCOPY=ram:prescale_out /PRESCALE

   pipe off
  endif

  ! If COREGRAPHICS run a short list of extreme tests that tend to be the ones to show graphical issues
  if coreGraphics then
    %test_plot_prescaler_2000/RANDOM=^randomPercent     ! NXM-3078
    %test_plot_prescaler_openfile/RANDOM=^randomPercent ! NXM-3078

  ! Additional interactive tests
  elseif interAct then
    %test_plot_prescaler_2000/RANDOM=^randomPercent       ! NXM-3078
    %test_plot_prescaler_precedence/RANDOM=^randomPercent ! NXM-3078
    %test_plot_prescaler_ignored/RANDOM=^randomPercent    ! NXM-3078
    %test_plot_prescaler_openfile/RANDOM=^randomPercent   ! NXM-3078
    %test_plot_prescaler_qualifier/RANDOM=^randomPercent  ! NXM-3078
  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

