! Tests the turning on and off of LTC in StateVector.computeMeasurements
! (using the DUAL5000 command in UCL)
!
! @author  Jeanette McCall
! @since   NeXtMidas 3.5.3 Bug 2866 & Bug 2871
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=^interAct SDEF=TRUE

  call setDatDir

  ! If we are running with LIBNXM NeXtMidas UCL will not be present
  path get UCL uclpath
  if baseline AND uclpath DEXISTS then
    ! requires the UCL option tree for DUAL5000 command
    res origPath opt.path
    path add UCL
    fname dual5000class "intr" "Dual5000.class" OPT="UCL"
    if dual5000class nfexists then
      build dual5000;ucl
    endif

    !************************************************************************
    ! CREATE A T5000 FILE WITH ONE FIXED GROUND LOCATION
    !************************************************************************
    HEADERMOD/CREATE ram:t5_fixed_loc.tmp type=5001 &
           COMPONENTS=(POS/VD/CARTESIAN/DISTANCE_M,VEL/VD/CARTESIAN/SPEED_M_S)
    ! append data to file
    file open/d/a df ram:t5_fixed_loc.tmp
    !set one point
    set dataTable {POS=(1115049.94740992,4843965.59272063,3983260.12854638),VEL=(0,0,0)}
    invoke result df.writeDataTable(dataTable)  
    !close file
    file close df

    !************************************************************************
    ! RUN DUAL 5000 WITH LTC and WITHOUT LTC 
    ! (with sv2.prm (satellite - state-vector) and this fixed location) 
    !************************************************************************

    ! Test run with LTC using default LTCTOL (1e-6)
    res expected_range_ltc                4.6846012578082055E7
    res expected_range_rate_ltc         -40.6112309262374
    res expected_elevation_angle_ltc     -0.7603144565565461
    dual5000 ^{datDir}.sv2.prm ram:t5_fixed_loc.tmp ram:dual5000_ltc_out.tmp /RF=1e6 /LTC
    call compareWithExpected ram:dual5000_ltc_out.tmp "(with LTC)" &
         expected_range_ltc ^expected_range_rate_ltc ^expected_elevation_angle_ltc 1.0

    ! Test run with LTC using LTCTOL=1e-8
    res expected_range_ltctol             4.684601257808271E7
    res expected_range_rate_ltctol      -40.61123092626651
    res expected_elevation_angle_ltctol  -0.7603144565484758
    dual5000 ^{datDir}.sv2.prm ram:t5_fixed_loc.tmp ram:dual5000_ltctol_out.tmp /RF=1e6 /LTC /LTCTOL=1e-8
    call compareWithExpected ram:dual5000_ltctol_out.tmp "(with LTC & LTCTOL=1e-8)" &
         expected_range_ltctol ^expected_range_rate_ltctol ^expected_elevation_angle_ltctol 0.01

    ! Test run with no LTC
    res expected_range_no_ltc             4.684600623213263E7
    res expected_range_rate_no_ltc      -40.61094429972203
    res expected_elevation_angle_no_ltc  -0.7603938899439119
    dual5000 ^{datDir}.sv2.prm ram:t5_fixed_loc.tmp ram:dual5000_out.tmp /RF=1e6
    call compareWithExpected ram:dual5000_out.tmp "(no LTC)" &
         expected_range_no_ltc ^expected_range_rate_no_ltc ^expected_elevation_angle_no_ltc 0.001

    path set ^origPath
  endif

endmacro

!******************************************************************************!
! 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

!**********************************************************************************
! Check the most sensitive DUAL5000 values (range, range rate, and elevation angle)
! with high enough precision to tell the effect of LTC and LTCTOL
!
! Note: Though the non-light time correction values will be exact to the precision
! held by a double, the other values will vary some due to the fact that the method
! to come up with timeB (transmitter time) within tolerance may vary in the code.
! This is reflected in the base tolerance for each value, which can be increased
! for a finer LTCTOL.
!**********************************************************************************
procedure compareWithExpected FN:dual5000_out_file S:assertmsg D:expected_range D:expected_range_rate &
                              D:expected_elevation_angle D:tol_mult

  file open/d act dual5000_out_file

  ! Grab the third entry, as LTC makes a significant difference by that point
  invoke actTbl=act.getDataTable(3)

  res range actTbl.R
  assert/text="^{reg.main.name} range ^assertmsg" range EQT expected_range /TOL=1e-12*^tol_mult
  res range_rate actTbl.RR
  assert/text="^{reg.main.name} range rate ^assertmsg" range_rate EQT expected_range_rate /TOL=1e-10*^tol_mult
  res elevation_angle actTbl.EL
  assert/text="^{reg.main.name} elevation angle ^assertmsg" elevation_angle EQT expected_elevation_angle /TOL=1e-9*^tol_mult

  file close act

return