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

  call setDatDir

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

  ! create the 1000 data
  res zero_padded_data ram:data
  call generate1000data
  ! make a copy
  file copy ram:data ram:data2

  if interAct then
    INFO "There should be 3 warnings - 1 WARN for '... not support multiple' and 2 for '... only works with'"
    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 "PRESCALE & PRESCALE=KW ONLY work with a single 1000 or 2000 file\n\n&
                      Top-Left   - defaulted to autoscale\n             &
                                   because internal PRESCALE\n             &
                                   only allowed on single file\n             &
                                   (bad)\n&
                      Top-Right  - PRESCALE=EXT with multiple\n             &
                                   files works! Looks like 1000\n             &
                                   from earlier test.\n&
                      Row 2 Left - world.shp PRESCALE ignored\n&
                      Row 2 Right- 5000 cones PRESCALE ignored &
                      "

   ! **********  PRIORITY TESTING - OUTSIDE A PIPE************************
   ! put scale settings in scaletable
    prescaler/random=^randomPercent IN=zero_padded_data SCALETABLE=scaletbl_outside

   ! /PRESCALE ignored with multiple files when not /PRESCALE=EXT
   plot/id=outmultiple/all/bg ram:data{layer={color=GREEN}}|ram:data2{layer={color=RED}} /PRESCALE

   ! /PRESCALE=EXT looks at SCALETABLE, so it is okay to have a pipe separated list of files
   plot/id=out2ext/all/bg ram:data{layer={color=GREEN}}|ram:data2{layer={color=BLUE}} SCALETABLE=scaletbl_outside /PRESCALE=EXT

   ! **********  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 20 /title
     
     ! /PRESCALE is ignored unless it is a 1000 or 2000 file
     plot/id=pipeshape/all/bg ^{datDir}.world.shp /PRESCALE

     ! /PRESCALE will dbe ignored if not on a 1000 or 2000 file
     plot/id=pipe5000 ^{datDir}.ellipse5000.prm /PRESCALE


   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=outmultiple NAME=EXIT
    message FUNC=send ID=out2ext    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.outmultiple
    invoke ,, pane1.attach(reg.outmultiple,"OUTMULTIPLE")
    res pane4 reg.win.out2ext
    invoke ,, pane4.attach(reg.out2ext,"OUT2EXT")

  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

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! Generate 1000 data for this test
!******************************************************************************!
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
