!******************************************************************************!
! Test intersection of FDOA lines in LayerGeo solutions.
!
! Arguments: None
!
! Issue:   NXM-2415 Plot / LayerGeo: FDOA lines do not intersect both solutions as expected
! Since:   NeXtMidas 3.1.1
! Author:  J Bauer
!******************************************************************************!
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 MANUAL        manual        GET DEF=FALSE SDEF=TRUE
  switch ALTHZNDEBUG   althzndebug   GET DEF=FALSE SDEF=TRUE
  switch ALTHZNCLASSIC althznclassic GET DEF=FALSE SDEF=TRUE

  res z:mytrue  true
  res z:myfalse false
  call setDatDir

  if interAct then
    ! Horizon at Altitude prototype testing
    invoke ,, nxm.sys.lib.Transform.setHorizonAltDebug(althzndebug)
    invoke traditional nxm.sys.libg.LayerGeo.isHorizonAltTraditionalCode()
    if althznclassic then
      invoke ,, nxm.sys.libg.LayerGeo.useHorizonAltTraditionalCode(mytrue)
    else
      invoke ,, nxm.sys.libg.LayerGeo.useHorizonAltTraditionalCode(myfalse)
    endif

    set instructions "You should see &
                      a plot of the world with a LayerGeo. Changing the values &
                      should change the offsets."

    if manual ISFALSE then
      set instructions "This test case should finish automatically. ^{instructions}"
    else
      set instructions "^{instructions}\n\nClick the Run Tests button.\n\nAll terminal tests should PASS."
    endif

    invoke GEODETIC nxm.sys.lib.Position.GEODETIC

    timex now d:startTime sod=mySod

    calc startTime1 startTime 150 -     ! set to 2.5 minutes ago
    calc startTime2 startTime1 30 +     ! 30 seconds after the first one
    calc initSetDate startTime1 10 +    ! Default LayerGeo Time
    calc setToDate startTime2 30 +      ! Default tVal time
    calc timeMax startTime 300 +        ! max time (for tVal)

    noop ^{datDir}.sv1.prm testsv1{TC=^startTime1,XDelta=0.5}     ! the .5 forces interpolation in StateVector.process()
    noop ^{datDir}.sv2.prm testsv2{TC=^startTime2,XDelta=0.5}     ! the .5 forces interpolation in StateVector.process()

    timex startTime1 tmp1
    timex startTime2 tmp2

    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 8 /title

      plot/id=idplot world

      if manual then gcontrol button runTests "" "Run Tests"

      gcontrol label "Info"
        gcontrol prompt pSVAStartTime "SVA TimeCode:" ^tmp1
        gcontrol prompt pSVBStartTime "SVB TimeCode:" ^tmp2

      gcontrol label timeLabel "Set LayerGeo Time"
        gcontrol tval tSec "By Seconds of day:" DEF=mySod       MIN=0 MAX=86400   INC=1 /msg=y
        gcontrol tval tDate "By Full Date:"     DEF=setToDate   MIN=0 max=timeMax inc=1 /msg=y
        gcontrol prompt sva1Time "SVA 1 Time Offset:" ""
        gcontrol prompt sva2Time "SVA 2 Time Offset:" ""

      call setupLayerGeo

      if manual ISFALSE then set this.timer(0) 1.0   ! this is set to only run once

    pipe off

    invoke ,, nxm.sys.lib.Transform.setHorizonAltDebug(myfalse)
    invoke ,, nxm.sys.libg.LayerGeo.useHorizonAltTraditionalCode(traditional)
  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 "TSEC" then
    set geoLayer.TimeCode ^msg.data
    call setSVAPromptTimes
  elseif msg.name eqs "TDATE" then
    set geoLayer.TimeCode ^msg.data
    call setSVAPromptTimes

  elseif msg.name eqs "TIMER" OR msg.name eqs "RUNTESTS" then
    set this.timer(0) 0.0   ! kill the timer
    set geoLayer.Time ^setToDate
    call setSVAPromptTimes
    call checkGeoLayerTimes
  else
    !say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  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

!******************************************************************************!
! Set up all the values needed for LayerGeo
procedure setupLayerGeo

  ! set up a dummy SITE to use
  new nxm.sys.lib.Position(GEODETIC) mysite
  set mysite.lat 10
  set mysite.lon 10
  set mysite.alt 0

  ! create the layer with all the options it needs
  layer "Geo" geoLayer {NAME=GL,SVA=testsv1,SVB=testsv2,&
                        SVALABEL="SVA1",SVBLABEL="SVA2",&
                        SITE=mysite,SITELABEL="SITEA",&
                        TARGETLABEL="TGT",&
                        CONIC=1,TDOAMODE="Region",FDOAMODE="Region",&
                        FLINKT=1e8,FLINKA=-1e8,FLINKB=-1e8,HORIZONALT=10000,&
                        FLAGS="ALL",PREDICTREFERENCE="SITE",TDOA=0,FDOA=0,&
                        DTDOA=1e3,DFDOA=0.01} idplot

   ! set the initial time in the LayerGeo
   set geoLayer.timeCode initSetDate

   ! get reference to each of the StateVectors in the Layer
   invoke mySVA geoLayer.getSVA()
   invoke mySVB geoLayer.getSVB()

   ! display the Time in the gcontrol prompts
   call setSVAPromptTimes

return

!******************************************************************************!
! Set the time displayed in the prompts
procedure setSVAPromptTimes

  ! get the time from each of the StateVectors
  invoke svaT mySVA.getTime()
  invoke svbT mySVB.getTime()

  ! set the prompt values
  set gc.sva1Time.value svaT
  set gc.sva2Time.value svbT

return

!******************************************************************************!
! Check the times in the GeoLayer against what they should be
procedure checkGeoLayerTimes
  ! The comparison row values ('120' for SVA and '60' for SVB) are based off
  !  of time offset / XDelta (60/.5=120 for SVA and 30/.5=60 for SVB)

  invoke d:vxa mySVA.getVX()
  res d:expected_vxa file(testsv1).data(120,"VEL").0

  invoke d:vxb mySVB.getVX()
  res d:expected_vxb file(testsv2).data(60,"VEL").0

  !info "SVAT = ^svat , SVBT = ^svbt , VXB = ^vxb , eVXB = ^expected_vxb "

  assert/text="^reg.main.name SVA time offset" vxa eq expected_vxa
  assert/text="^reg.main.name SVB time offset" vxb eq expected_vxb

  if manual then
    !!! do nothing
  else
    pipe stop
  endif

return
