!******************************************************************************!
! Test the time readout on Layer1D (type 1000) files
!
! Bug    : 2063 PLOT / LAYER1D: Time readout not correct
! Bug    : 2155 PLOT: view=xy not working properly after nxm242 for type 1000 file
! Bug    : 2426 PLOT: DateString readout ignores ExtPrec
! Author : J. Hallam / J Bauer / Neon Ngo
! Version: $Id: test_plot_layer1d_readout.mm,v 1.16 2012/06/05 13:32:17 jwb Exp $
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch KEEP        keep     GET DEF=FALSE SDEF=TRUE

if interAct then
  ! create test files
  set sr 1e5
  set sfreq sr/10
  set testfile_bigxs    "test1000_tc_tcprec_big_xstart.tmp"
  set testfile_temp     "test1000_temp.tmp"
  set testfile_timeline "test1000_timeline_xstart.tmp"
  waveform OUT=testfile_bigxs FORM=SI ELEM=172800 AMP=10 FREQ=0.0001 DELTA=0.5
  waveform OUT=testfile_temp  FORM=SI ELEM=127    AMP=10  ! file to add a timeline to

  ! get some times
  timex 1970:01:01::00:00:00 unixEpoch  ! 01/01/1970 (Unix epoch)
  timex now tcnow                       ! current time
  timex tcnow sod=sod                   ! seconds so far this day
  set xs tcnow-unixEpoch-sod            ! seconds up to today (relative to Unix epoch time)
  set s:epochTL "2010:03:13::00:00:00.000000" ! used to set TimeLine

  ! use J. Czechowski's timelinetester to add a timeline to the file
  nxm.sys.test.timelinetester &
         IN=testfile_temp{YU=FREQ,XU=TIME} &
         OUT=testfile_timeline{USETIMELINEFILE=true} &
         TSTART=^epochTL &
         TDELTA=0.1 &
         JITTER=0.05

  ! update the headers
  headermod ^testfile_timeline xstart=^tcnow                ! XStart should not matter since file has a TimeLine
  headermod ^testfile_bigxs timecode=^unixEpoch xstart=^xs  ! these two should combine to form midnight this morning / last night
  keyword/scope=main ^testfile_bigxs set tc_prec .001       ! adds the TC precision, but usually too small to notice

  set instr "Move the mouse over each plot and observe the time (t=) readout in the title bar.\n\n&
             (1) The time for the top-left plot (TimeLine) should start at ^{epochTL}; it is in &
                 view=iXY, so time starts at the top and increases as the mouse moves down. &
                 +ExtPrec is enabled so, time should be displayed to 6 decimal places for t= and x= readout.\n\n&
             (2) The time for the bottom-left plot (Epoch+XStart) should start at 00:00:00 of the &
                 current day and end at 00:00:00 tonight; it is in view=XY, so the time starts at &
                 the bottom of the plot and increases as the mouse moves up.\n\n&
             (3) The top-right plot (Zoomed), which is the bottom-left plot ZOOMed across the &
                 left border, should show a time of 0 when the mouse is not over the data area &
                 and the time should start at 00:00:00 of the current day when the mouse is &
                 at the beginning of the data; time in the readout should increase as you move &
                 the mouse to the right.\n\n&
             (4) The bottom-right plot (Realtime) should change every 8.2 seconds; time in the &
                 readout should increase as you move the mouse to the right"

  pipe on
    panel/setup/controls=gc

    waveform _wave SF inf white freq=(sfreq*.5) amp=1 delta=1/(sr*.005) /throttle=real

    plot/all/id=timeline  ^testfile_timeline view=ixy readout=+t|+onaxis|+ExtPrec|-x|-y|-view
    plot/all/id=bigxstart ^testfile_bigxs    view=xy readout=+t|+onaxis|-x|-y|-view
    plot/all/id=zoomplot/msgid=main ^testfile_bigxs readout=+t|+onaxis|-x|-y|-view ! same file as above but this one gets zoomed
    plot/id=rtplot2/nice _wave /fs=4096 cnt=motion type=line readout=+onaxis|+t|-x|-y|-view

    gcontrol label macro "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemcolors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "INSTRUCTIONS" "^instr" 28 36 /title

   call zoomPlot   ! ZOOM the plot across the border

  pipe off

  ! cleanup test files (if user did not added switch to keep test files)
  if keep isFalse then
    erase testfile_bigxs testfile_temp testfile_timeline
  else
    erase testfile_temp
    info "Not erasing testfiles: ^testfile_bigxs ^testfile_timeline"
  endif

endif ! interAct
endmacro

!******************************************************************************!
procedure  processMessage m:msg
  !say "Got msg ^msg.name data=^msg.data"
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: user indicated tests" msg.data eqss "PASS"
    pipe stop
  endif
return

!******************************************************************************!
procedure zoomPlot
  calc x1 xs 1e5 -
  calc x2 x1 1.5e5 +
  MESSAGE FUNC=SEND ID=ZOOMPLOT NAME=ZOOM &
             DATA={&
                  XMIN=^x1,&
                  XMAX=^x2,&
                  YMIN=-3.559,&
                  YMAX=3.830,&
                  TMIN=0.0,&
                  TMAX=1.9019764770287366E9&
             }

return
