! Tests the plotting of cones using both new code and legacy code
! The new code and legacy code are compared side-by-side
!
! Bug:    NXM-3753 GeodeticUtil.computeLLA giving bad values
! Since:  NeXtMidas 4.0.1
! Author: Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE SDEF=TRUE ! for normal interactive test(s)

  if interAct then
    res numOrbitPos 20           ! breaking "orbit" into 20 positions
    res minPos 0
    res curOrbitPos 0
    res fileName "orbitcurrent"
    res z:usingOrbitPos true
    res z:plotsDisplayed false
    res defaultTextChoice "Please select a test case"

    ! Get original state of compare cone mode and cone list
    invoke z:wasComparingConeCode nxm.sys.lib.Transform.getCompareConeCode()
    invoke origConeList           nxm.sys.lib.Transform.getCompareConeList()

    ! save off AUX.DAT and switch to DAT with all the test files
    call setDatDir
    res origDatAux "^{aux.dat}"
    invoke nm_root nxm.sys.lib.Shell.getNmRoot()
    res aux.dat "^{nm_root}^{datDirSlash}"

    ! Enable comparing of default code and legacy code
    invoke ,, nxm.sys.lib.Transform.setCompareConeCode(true)
    invoke ,, nxm.sys.lib.Transform.setCompareConeList(0x03) ! 0x03 new default and legacy being compared

    ! Set test instructions based on compare cone list mask
    call setInstructions

    ! Setup Test Names and Data
    call populateTestData

    pipe init
      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplotLatLon       /READOUT="+OnAxis|+DateString" view=LATLON
      plot/id=idplotGeoCutMinus1 /READOUT="+OnAxis|+DateString" view=GEO
      plot/id=idplotGeoCut1      /READOUT="+OnAxis|+DateString" view=GEO

      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" mainInstructions 24 12 /title

      gcontrol/noedit label clearPlotsLabel "CLEAR ALL PLOTS"
      gcontrol button clearPlots "" "Clear" Clear /ttop

      gcontrol/noedit label testCases "TEST CASES"
      table test_names getvaluess/s test_names_list
      gcontrol/nc=1 choice testCaseChoices "Test Cases" ^test_names_list defaultTextChoice

      gcontrol/noedit label testCases "SELECTED TEST CASE"
      gcontrol text selectedText "" "" 24 5

      gcontrol button toggleUseOrbitPos "Toggle Use Orbit Positions" "Use Orbit Positions" "|Use Orbit Positions|" /chkbox /toggle
      gcontrol lval orbitPos "Orbit Position" minPos minPos numOrbitPos 1
    pipe off

    ! restore original AUX.DAT
    res aux.dat "^origDatAux"

    ! Restore cone mode and cone list to their original state
    invoke ,, nxm.sys.lib.Transform.setCompareConeCode(^wasComparingConeCode)
    invoke ,, nxm.sys.lib.Transform.setCompareConeList(^origConeList)

    ! cleanup tmp file
    erase fileName
  endif

endmacro

!******************************************************************************!
! Main process message for this macro
!******************************************************************************!
procedure processMessage m:msg
  !say "^{msg.name} ^{msg.data}"
  if msg.name eqs "MAIN" then
    assert/text="Plot files tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "CLEARPLOTS" then
    call test_plot_none
    set gc.testCaseChoices.value defaultTextChoice
    set gc.selectedText.value ""
    set gc.orbitPos.value minPos
    set curOrbitPos minPos
    set plotsDisplayed false
  elseif msg.name eqs "TESTCASECHOICES" then
    foreach key intable t:test_names
      if msg.data eqs ^{test_names.^{key}} then
        set gc.orbitPos.value minPos
        set curOrbitPos minPos
        call test_plot_layerT5_cone ^{key}
        call format_selected_text_output ^{key}
        res lastKey ^{key}
        set plotsDisplayed true
        break
      endif
    endfor
  elseif msg.name eqs "ORBITPOS" then
    if plotsDisplayed then
      set curOrbitPos msg.data
      if curOrbitPos EQ numOrbitPos then
        set gc.orbitPos.value minPos
        res curOrbitPos minPos
      endif
      call test_plot_layerT5_cone ^{lastKey}
      call format_selected_text_output ^{lastKey}
    else
      set gc.orbitPos.value minPos
    endif
  elseif msg.name eqs "TOGGLEUSEORBITPOS" then
    if gc.toggleUseOrbitPos.value eqs "|USE ORBIT POSITIONS|" then
      set usingOrbitPos true
      set gc.orbitPos.show 1
      set gc.orbitPos.value minPos
      set curOrbitPos minPos
    else
      set usingOrbitPos false
      set gc.orbitPos.show 0
    endif

    ! Replot
    if plotsDisplayed then call test_plot_layerT5_cone ^{lastKey}
  else
    ! do nothing
    say "^reg.main.name: 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 instructions
!******************************************************************************!
procedure setInstructions
  ! Get available cone list masks
  invoke coneDefaultMask nxm.sys.lib.Transform.CONE_DEFAULT
  invoke coneLegacyMask  nxm.sys.lib.Transform.CONE_LEGACY
  invoke coneSphereMask  nxm.sys.lib.Transform.CONE_SPHERE

  ! Determine whether or not each mask is being used
  invoke coneDefaultEnabled nxm.sys.lib.Transform.isOnCompareList(^coneDefaultMask)
  invoke coneLegacyEnabled  nxm.sys.lib.Transform.isOnCompareList(^coneLegacyMask)
  invoke coneSphereEnabled  nxm.sys.lib.Transform.isOnCompareList(^coneSphereMask)

  res firstLineNewLine ""
  res lastCompareConeCodeEnabled ""

  ! Enable instructions for cone codes that are enabled
  res noteDefault ""
  if coneDefaultEnabled then
    set firstLineNewLine "\n"
    set noteDefault "- PURPLE Andrew's oblate earth"
    set lastCompareConeCodeEnabled "DEFAULT"
  endif
  res noteLegacy  ""
  if coneLegacyEnabled then
    set firstLineNewLine "\n"
    set noteLegacy "- RED pure Jeff's code"
    set lastCompareConeCodeEnabled "LEGACY"
  endif
  res noteSphere  ""
  if coneSphereEnabled then
    set firstLineNewLine "\n"
    set noteSphere "- GOLDENROD Jeff's + Andrew's spherical earth"
    set lastCompareConeCodeEnabled "SPHERE"
  endif

  ! Append a newline to any line that isn't the last line listed
  if coneDefaultEnabled and lastCompareConeCodeEnabled neqs "DEFAULT" then set noteDefault "^{noteDefault}\n"
  if coneLegacyEnabled  and lastCompareConeCodeEnabled neqs "LEGACY"  then set noteLegacy  "^{noteLegacy}\n"
  if coneSphereEnabled  and lastCompareConeCodeEnabled neqs "SPHERE"  then set noteSphere  "^{noteSphere}\n"

  res mainInstructions "Expected: world plots with CONIC=-1,1^{firstLineNewLine}&
                        ^{noteDefault}^{noteLegacy}^{noteSphere}"
return

!******************************************************************************!
! Populate Test Names and Data
!******************************************************************************!
procedure populateTestData

  ! Test names for cone data
  set t:test_names {}
  set test_names.1  " 1. NO-INTERSECT-LARGE-CONE-ORIGINAL"
  set test_names.2  " 2. FULL-INTERSECT-NORTHPOLE-INLINE-SMALL-BEAMWIDTH"
  set test_names.3  " 3. PARTIAL-INTERSECT-NORTHPOLE-STRAIGHT-ANGLE-SMALL-BEAMWIDTH"
  set test_names.4  " 4. FULL-INTERSECT-NORTHPOLE-STEEP-ANGLE-SMALL-BEAMWIDTH"
  set test_names.5  " 5. NO-INTERSECT-NORTHPOLE-INLINE-LARGE-BEAMWIDTH"
  set test_names.6  " 6. PARTIAL-INTERSECT-NORTHPOLE-STRAIGHT-ANGLE-LARGE-BEAMWIDTH"
  set test_names.7  " 7. PARTIAL-INTERSECT-NORTHPOLE-STEEP-ANGLE-LARGE-BEAMWIDTH"
  set test_names.8  " 8. FULL-INTERSECT-EQUATOR-INLINE-SMALL-BEAMWIDTH"
  set test_names.9  " 9. FULL-INTERSECT-EQUATOR-INLINE-LARGE-BEAMWIDTH"
  set test_names.10 "10. NO-INTERSECT-EQUATOR-INLINE-HUGE-BEAMWIDTH"
  set test_names.11 "11. FULL-INTERSECT-EQUATOR-ANGLE-SMALL-BEAMWIDTH"
  set test_names.12 "12. PARTIAL-INTERSECT-EQUATOR-ANGLE-LARGE-BEAMWIDTH"
  set test_names.13 "13. NO-INTERSECT-EQUATOR-ANGLE-HUGE-BEAMWIDTH"
  set test_names.14 "14. FULL-INTERSECT-MEDITERRANEAN-ANGLE-SMALL-BEAMWIDTH"
  set test_names.15 "15. PARTIAL-INTERSECT-MEDITERRANEAN-ANGLE-LARGE-BEAMWIDTH"
  set test_names.16 "16. NO-INTERSECT-MEDITERRANEAN-HUGE-BEAMWIDTH"
  set test_names.17 "17. FULL-INTERSECT-PRIME-MERIDIAN-STEEP-ANGLE-SMALL-BEAMWIDTH"
  set test_names.18 "18. PARTIAL-INTERSECT-PRIME-MERIDIAN-STEEP-ANGLE-LARGE-BEAMWIDTH"
  set test_names.19 "19. NO-INTERSECT-PRIME-MERIDIAN-STEEP-ANGLE-HUGE-BEAMWIDTH"
  set test_names.20 "20. PARTIAL-INTERSECT-LARGE-CONE-ORIGINAL"
  set test_names.21 "21. PARTIAL-INTERSECT-CONEFLAGS5000-PROBLEM-CASE-ORIGINAL"
  set test_names.22 "22. FULL-INTERSECT-SMALL-CONE-ORIGINAL"

  ! Populate cone data with test names
  ! note: ROTATE vector should have either lat or lon 90 degrees off from POS vector
  set t:cone5000_data    {}
  set t:cone5000_data.1  {POS=(3.7E7,15,20),POSP=(0,50,20),ROTATE=(3.7E7,-75,20),ANGLE=14.75,NAME=^{test_names.1},LABEL=^{test_names.1}}
  set t:cone5000_data.2  {POS=(3E7,90,0),POSP=(0,90,0),ROTATE=(3E7,0,0),ANGLE=4,NAME=^{test_names.2},LABEL=^{test_names.2}}
  set t:cone5000_data.3  {POS=(3E6,45,0),POSP=(0,90,0),ROTATE=(3E6,0,90),ANGLE=4,NAME=^{test_names.3},LABEL=^{test_names.3}}
  set t:cone5000_data.4  {POS=(3E7,60,0),POSP=(0,90,0),ROTATE=(3E7,-30,0),ANGLE=4,NAME=^{test_names.4},LABEL=^{test_names.4}}
  set t:cone5000_data.5  {POS=(3E7,90,0),POSP=(0,90,0),ROTATE=(3E7,0,0),ANGLE=14,NAME=^{test_names.5},LABEL=^{test_names.5}}
  set t:cone5000_data.6  {POS=(3E6,45,0),POSP=(0,90,0),ROTATE=(3E6,-45,0),ANGLE=14,NAME=^{test_names.6},LABEL=^{test_names.6}}
  set t:cone5000_data.7  {POS=(3E7,60,0),POSP=(0,90,0),ROTATE=(3E7,-30,0),ANGLE=14,NAME=^{test_names.7},LABEL=^{test_names.7}}
  set t:cone5000_data.8  {POS=(3E7,0,0),POSP=(0,0,0),ROTATE=(3E7,-90,0),ANGLE=3,NAME=^{test_names.8},LABEL=^{test_names.8}}
  set t:cone5000_data.9  {POS=(3E7,0,0),POSP=(0,0,0),ROTATE=(3E7,-90,0),ANGLE=10,NAME=^{test_names.9},LABEL=^{test_names.9}}
  set t:cone5000_data.10 {POS=(3E7,0,0),POSP=(0,0,0),ROTATE=(3E7,-90,0),ANGLE=14,NAME=^{test_names.10},LABEL=^{test_names.10}}
  set t:cone5000_data.11 {POS=(3E7,0,0),POSP=(0,0,60),ROTATE=(3E7,-90,0),ANGLE=0.5,NAME=^{test_names.11},LABEL=^{test_names.11}}
  set t:cone5000_data.12 {POS=(3E7,0,0),POSP=(0,0,60),ROTATE=(3E7,-90,0),ANGLE=10,NAME=^{test_names.12},LABEL=^{test_names.12}}
  set t:cone5000_data.13 {POS=(3E7,0,0),POSP=(0,0,60),ROTATE=(3E7,-90,0),ANGLE=22,NAME=^{test_names.13},LABEL=^{test_names.13}}
  set t:cone5000_data.14 {POS=(3E7,0,0),POSP=(0,45,45),ROTATE=(3E7,-90,0),ANGLE=0.5,NAME=^{test_names.14},LABEL=^{test_names.14}}
  set t:cone5000_data.15 {POS=(3E7,0,0),POSP=(0,45,45),ROTATE=(3E7,-90,0),ANGLE=10,NAME=^{test_names.15},LABEL=^{test_names.15}}
  set t:cone5000_data.16 {POS=(3E7,0,0),POSP=(0,45,45),ROTATE=(3E7,-90,0),ANGLE=22,NAME=^{test_names.16},LABEL=^{test_names.16}}
  set t:cone5000_data.17 {POS=(3E7,-20,20),POSP=(0,30,0),ROTATE=(3E7,70,20),ANGLE=1,NAME=^{test_names.17},LABEL=^{test_names.17}}
  set t:cone5000_data.18 {POS=(3E7,-20,20),POSP=(0,30,0),ROTATE=(3E7,70,20),ANGLE=10,NAME=^{test_names.18},LABEL=^{test_names.18}}
  set t:cone5000_data.19 {POS=(3E7,-20,20),POSP=(0,30,0),ROTATE=(3E7,70,20),ANGLE=22,NAME=^{test_names.19},LABEL=^{test_names.19}}
  set t:cone5000_data.20 {POS=(3.7E7,-30,0),POSP=(0,0,0),ROTATE=(3.7E7,60,0),ANGLE=13,NAME=^{test_names.20},LABEL=^{test_names.20}}
  set t:cone5000_data.21 {POS=(34815020,-3.9,16),POSP=(0.1997,34.3,67),ROTATE=(34815020,86.1,16),ANGLE=0.6332,NAME=^{test_names.21},LABEL=^{test_names.21}}
  set t:cone5000_data.22 {POS=(3.7E6,0,0),POSP=(0,0,0),ROTATE=(3.7E6,-90,0),ANGLE=8,NAME=^{test_names.22},LABEL=^{test_names.22}}
return

!******************************************************************************!
! Format selected text output
!******************************************************************************!
procedure format_selected_text_output l:tblIndex[0]
  res pos   ^{cone5000_data.^{key}.POS}
  res posp  ^{cone5000_data.^{key}.POSP}
  res ang   ^{cone5000_data.^{key}.ANGLE}
  res name  ^{cone5000_data.^{key}.NAME}
  res label ^{cone5000_data.^{key}.LABEL}

  res posStr  "POS   = ^{pos}"
  res pospStr "POSP  = ^{posp}"
  res posStrLenOrig posStr.length()
  res pospStrLenOrig pospStr.length()
  res l:index 0
  if posStrLenOrig > pospStrLenOrig then
    calc l:numSpaces posStrLenOrig pospStrLenOrig -
    while index lt numSpaces
      set pospStr "^{pospStr} "
      calc index index 1 +
    endwhile
    set pospStr "^{pospStr} (alt,lat,lon)"
    set posStr "^{posStr} (alt,lat,lon)"
  else
    calc l:numSpaces pospStrLenOrig posStrLenOrig -
    while index lt numSpaces
      set posStr "^{posStr} "
      calc index index 1 +
    endwhile
    set posStr "^{posStr} (alt,lat,lon)"
    set pospStr "^{pospStr} (alt,lat,lon)"
  endif
  set gc.selectedText.value "^{posStr}\n&
                             ^{pospStr}\n&
                             ANGLE = ^{ang}\n&
                             NAME  = \"^{name}\"\n&
                             LABEL = \"^{label}\""
return

!******************************************************************************!
! Clear all plots
!******************************************************************************!
procedure test_plot_none
  result testfiles {}
  message FUNC=sendw ID=IDPLOTLATLON       NAME=OPENFILE INFO=-1 DATA="" /wait=2
  message FUNC=sendw ID=IDPLOTGEOCUTMINUS1 NAME=OPENFILE INFO=-1 DATA="" /wait=2
  message FUNC=sendw ID=IDPLOTGEOCUT1      NAME=OPENFILE INFO=-1 DATA="" /wait=2
return

!******************************************************************************!
! Plot cone data for entry in cone5000_data table
!******************************************************************************!
procedure test_plot_layerT5_cone l:tblIndex[0]
  result testfiles {}
  result testfiles.world "world.prm"

  call file_from_table tblIndex

  res trimmer ""
  if usingOrbitPos then
    calc l:plus1 curOrbitPos 1 +
    set trimmer "(^{curOrbitPos}:^{plus1})"
  endif

  ! LATLON conic=-1
  result testfiles.cone "^{fileName}^{trimmer}{LAYER={LT="T5",CONIC=-1,FIELD=+Labels,FLAGS=+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTLATLON NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2

  ! GEODETIC conic=-1
  result testfiles.cone "^{fileName}^{trimmer}{LAYER={LT="T5",CONIC=-1,FIELD=+Labels,FLAGS=+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTGEOCUTMINUS1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2

  ! GEODETIC conic=1
  result testfiles.cone "^{fileName}^{trimmer}{LAYER={LT="T5",CONIC=1,FIELD=+Labels,FLAGS=+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTGEOCUT1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2
return

!******************************************************************************!
! Generate temporary Blue file for cone data entry
!******************************************************************************!
procedure file_from_table l:tblIndex[0]
  table cone5000_data.^tblIndex GETKEY POS posval
  new nxm.sys.lib.Position() posPos
  invoke ,, posPos.setGeo(posVal.0,posVal.1,posVal.2)
  table cone5000_data.^tblIndex GETKEY POSP pospval
  new nxm.sys.lib.Position() pospPos
  invoke ,, pospPos.setGeo(pospVal.0,pospVal.1,pospVal.2)
  table cone5000_data.^tblIndex GETKEY ROTATE rotateval
  new nxm.sys.lib.Position() rotatePos
  invoke ,, rotatePos.setGeo(rotateVal.0,rotateVal.1,rotateVal.2)
  table cone5000_data.^tblIndex GETKEY ANGLE angleval
  invoke ,, nxm.sys.test.Orbit.create5000Orbit(^fileName,posPos,pospPos,rotatePos,^angleval,^numOrbitPos)

return
