!******************************************************************************!
! Test case PLOT autoscaling for line (Layer1D) and raster (Layer2D) plots.
!
! @bug      2734 - PLOT: incorrect AutoMin/AutoMax autoscaling when first frame of data has all zeros
! @author:  Neon Ngo
!******************************************************************************!
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)

  if interAct then
    set instructions "1) The line plots should have y-axis starting from 0 to ~4e36 (static) or &
      3e36 (realtime) with a spike at 0.5 kHz.\n\n&
      2) The realtime raster plot should be mostly dark blue with energy (highlight color) at 0.5 kHz.\n\n&
      Note: In the realtime raster plot, the true signal is at 0.5 kHz and the bursts of &
      near infinity noise are at slightly below 0.5 kHz. The plot briefly recovers &
      displaying the true data (before the next noise spike) by using NOAVERAGE with a short AUTOL.\n\n&
      3) The 'Frames with Large Values Raster Plot' should alternate between a reddish background (not purple) &
      and white lines"

    ! Since NeXtMidas 3.5.3
    ! test_plot_autoscale_cd was created because of bug 2857
    res testfile "nxm.sys.test.test_plot_autoscale_cd.tmp"

    ! Create Type 2000 with large values where Math.ulp(float) is greater than 1
    ! Tests fixes in MPlot autoscaling for NXM-2892 - since 3.7.1
    res amp    0.000001e14
    res mean   9.000000e14
    waveform ram:frame_large_const FORM=SF ELEM=80k SHAPE=SQUARE AMP=amp MEAN=mean FREQ=1/4k PHASE=.5
    headermod ram:frame_large_const TYPE=2000 FS=512

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO" AlwaysOpen
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft
      gcontrol text  idtext "Instructions" instructions 28 32 /title
      ! below button can be used to pause/resume pipe for debugging
      gc button pipestate "" "Run,Pause" 1 /flags=+OneRow|+TOff

      plot/id=staticlineplot/XMULT=K testfile type=line scale=AutoMin|AutoMax

      ! using /TL=1 to show PLOT's auto scaling problem on realtime pipe and /RT to throttle it
      noop/RT/WRAP/TL=1 testfile _pipe
      plot/id=lineplot/XMULT=K   _pipe type=line   scale=AutoMin|AutoMax|           AUTOL=32
      plot/id=rasterplot/XMULT=K _pipe type=raster scale=AutoMin|AutoMax|NoAverage| AUTOL=9   ! Using NOAVERAGE - since 3.7.0 for bug 2901

      ! Tests fixes in MPlot autoscaling for NXM-2892 - since 3.7.1
      noop/RT/WRAP/TL=1 frame_large_const _bignum
      plot/id=large_rasterplot/XMULT=K  _bignum type=raster scale=AutoMin|AutoMax|NoAverage AUTOL=2 VIEW=iYX
    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"
    pipe stop
  elseif msg.name eqs "PIPESTATE"
    if msg.data neqs "" then
      pipe ^{msg.data}
    endif
  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
