!******************************************************************************!
! Test Platinum-to-Blue automatic conversion
!
! @bug      NXM-3787 Need better support for Blue 2.0 (platinum)
! @since    NeXtMidas 4.1.0
! @author:  Jeanette McCall
!******************************************************************************!
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 KEEP        keep     GET DEF=FALSE SDEF=TRUE

  invoke autoConvertSetting nxm.sys.lib.DataFile.getAutoConvertPlatinumDefault()
  invoke ,, nxm.sys.lib.DataFile.setAutoConvertPlatinumDefault(true)

  ! Define Expected Messages
  res platConvInfo "You should see 2 INFOs about PLATINUM 1) Does not have full support 2) auto-conversion."
  res platForceInfo "You should see 2 INFOs about unsupported BLUE version VER=2.0."
  res platForceWarn "You should see 2 WARNings about unsupported BLUE version VER=2.0."
  res warnFixTC "Currently there is a WARNing about fixing BLUE TimeCode."
  res warnMissIntro "Expect WARNings about missing these source keys: "
  res warnMiss "^{warnMissIntro} AOI.LAT, AOI.LON, AOI.ALT, SITE_DELAY, COL_RFF, COL_IF_CENTER, ACQETF, MISSION , and StateVector data"

  ! Assign file names
  set origTimelineFilename       nxm.sys.test.test_fftout_timeline.prm
  set platMissingKwds            nxm.sys.test.testplat1001.cdif
  set origPlatFullKeywords      "nxm.sys.test.testplat4plat2blue_cov.cdif{BLUEVER=2.0}"
  set fixedPlatFullKeywords      "fixedplat4plat2blue_cov.cdif"
  set bsPlatFullKeywords         "bs_plat4plat2blue_cov.cdif"
  set fftMissingOut              "fftofplatinummisskwds.tmp"
  set fftFullOut                 "fftofplatinumfullkwds.tmp"
  set fftFullBSOut               "fftofplatinumbskwds.tmp"
  set fftFullOutNoConvert        "fftofplatinumfullkwdsnoconvert.tmp"

  ! Transform testplat4plat2blue_cov.cdif to 2.0 valid PLATINUM files
  file copy/f origPlatFullKeywords fixedPlatFullKeywords
  keyword fixedPlatFullKeywords set TOA_SIGMA 0.0 FOA_SIGMA 0.0
  keyword fixedPlatFullKeywords set ver 2.0 /SCOPE=main
  !status/key fixedPlatFullKeywords

  ! Transform testplat4plat2blue_cov.cdif to valid PLATINUM 2.0 file with BS.LAT, BS.LON, BS.ALT (NXM-3898)
  file copy/f origPlatFullKeywords bsPlatFullKeywords
  keyword bsPlatFullKeywords get AOI.LAT BS_LAT AOI.LON BS_LON AOI.ALT BS_ALT
  keyword bsPlatFullKeywords DEL NAME=AOI.LAT
  keyword bsPlatFullKeywords DEL AOI.LON
  keyword bsPlatFullKeywords DEL AOI.ALT
  keyword bsPlatFullKeywords DEL AOI.RAD
  keyword bsPlatFullKeywords set BS.LAT BS_LAT BS.LON BS_LON BS.ALT BS_ALT
  keyword bsPlatFullKeywords set TOA_SIGMA 0.0 FOA_SIGMA 0.0
  keyword bsPlatFullKeywords set ver 2.0 /SCOPE=main
  !status/key bsPlatFullKeywords

  INFO "****TESTING WITH AUTOCONVERSION OF PLATINUM OFF AND FORCE=TRUE****"
  call testAutoConversionOff
  SAY ""
  INFO "****TESTING THAT FORCE=TRUE TURNS OFF DEFAULT AUTOCONVERSION****"
  call testDefaultWithForce
  SAY ""
  INFO "****TESTING WITH AUTOCONVERSION OF PLATINUM ON WITHOUT FORCE****"
  call testDefaultAutoConvertion ! coverts fixedPlatFullKeywords to 1.1, so needs to be last


  ! Clean up remaining test data (unless /KEEP switch is set to true)
  if keep isFalse then
    IF fixedPlatFullKeywords FEXISTS THEN ERASE fixedPlatFullKeywords
    IF bsPlatFullKeywords    FEXISTS THEN ERASE bsPlatFullKeywords
    IF fftFullOut            FEXISTS THEN ERASE fftFullOut
    IF fftFullBSOut          FEXISTS THEN ERASE fftFullBSOut
    IF fftMissingOut         FEXISTS THEN ERASE fftMissingOut
    IF fftFullOutNoConvert   FEXISTS THEN ERASE fftFullOutNoConvert
  else
    info "Not erasing these files: ^{fixedPlatFullKeywords}, ^{fftFullOut}, &
                                   ^{fftMissingOut}, ^{fftFullOutNoConvert}, &
                                   ^{bsPlatFullKeywords}, ^{fftFullBSOut}
  endif

  invoke ,, nxm.sys.lib.DataFile.setAutoConvertPlatinumDefault(autoConvertSetting)
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
  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

!******************************************************************
! TEST: AUTOCONVERSION turned OFF & FORCE BEHAVIOR
!******************************************************************
procedure testAutoConversionOff

  invoke ,, nxm.sys.lib.DataFile.setAutoConvertPlatinumDefault(false)

  info "^{platForceWarn} \n      ^{warnFixTC}"
  fft fixedPlatFullKeywords{FORCE=true} fftFullOutNoConvert{FORCE=true}
  keyword fftFullOutNoConvert GET TERMINAL.LAT vLAT VER vVER
  assert/text="^reg.main.name: FFT of PLATINUM with autoconversion OFF and FORCE=true is BLUE 2.0" vVER EQ 2.0
  assert/text="^reg.main.name: FFT of PLATINUM with autoconversion OFF and FORCE=true has key TERMINAL.LAT" vLAT EQ 75.0

  say ""
  info "^{platForceWarn}"
  keyword fixedPlatFullKeywords{FORCE=true} put kwdbeforeconvert "added with FORCE=true"
  keyword fixedPlatFullKeywords GET KWDBEFORECONVERT vKEY VER vVER
  assert/text="^reg.main.name: Key added to PLATINUM with autoconversion OFF and FORCE=true has VER=2.0" vVER EQ 2.0
  assert/text="^reg.main.name: Key added to PLATINUM with autoconversion OFF and FORCE=true has key" vKEY EQS "added with FORCE=true"

  ! NOTE: If the FORCE=true was not used for the FFT or KEY PUT, these would throw
  !       (nxm.sys.lib.MidasException: DataFile: Can not set VER=2.0, version number is invalid.)

  invoke ,, nxm.sys.lib.DataFile.setAutoConvertPlatinumDefault(true)
return

!******************************************************************
! TEST: DEFAULT FORCE BEHAVIOR (AUTOCONVERSION will not happen)
!******************************************************************
procedure testDefaultWithForce

  info "^{platForceInfo} \n      ^{warnFixTC}"
  fft fixedPlatFullKeywords{FORCE=true} fftFullOut{FORCE=true}
  keyword fftFullOut GET TERMINAL.LAT vLAT VER vVER
  assert/text="^reg.main.name: FFT of unconverted PLATINUM with FORCE=true is BLUE 2.0" vVER EQ 2.0
  assert/text="^reg.main.name: FFT of unconverted PLATINUM has key TERMINAL.LAT" vLAT EQ 75.0

  say ""
  info "^{platForceInfo}"
  keyword fixedPlatFullKeywords{FORCE=true} put nextkwdbeforeconvert "also added with FORCE=true"
  keyword fixedPlatFullKeywords GET NEXTKWDBEFORECONVERT vKEY VER vVER
  assert/text="^reg.main.name: Key added to unconverted PLATINUM with FORCE=true has VER=2.0" vVER EQ 2.0
  assert/text="^reg.main.name: Key added to unconverted PLATINUM with FORCE=true has key" vKEY EQS "also added with FORCE=true"

  ! NOTE: If the FORCE=true was not used for the FFT or KEY PUT, these would throw
  !       (nxm.sys.lib.MidasException: DataFile: Can not set VER=2.0, version number is invalid.)
return

!******************************************************************
! TEST: DEFAULT BEHAVIOR AUTOCONVERSION (not FORCE or AUTOCONVERSION off)
!******************************************************************
procedure testDefaultAutoConvertion

  ! FFT of full PLATINUM file
  info "^{platConvInfo} \n      ^{warnFixTC}"
  fft fixedPlatFullKeywords fftFullOut
  keyword fftFullOut GET VRF vVRF POS_COVAR_MTX11 vCOVAR11
  assert/text="^reg.main.name: Converted Full Platinum has VRF key and value" vVRF EQ 1252.5
  assert/text="^reg.main.name: Converted Full Platinum has POS_COVAR_MTX11 key and value" vCOVAR11 EQ 1.1
  keyword fftFullOut GET FEED_LAT aoifeedlat FEED_LON aoifeedlon FEED_ALT aoifeedalt 
  !status/key fftFullOut

  ! BS.LAT and AOI.LAT both work for FEED_LAT (alternate source support NXM-3898)
  info "^{platConvInfo} \n      ^{warnFixTC}"
  fft bsPlatFullKeywords fftFullBSOut
  keyword fftFullBSOut GET FEED_LAT bsfeedlat FEED_LON bsfeedlon FEED_ALT bsfeedalt 
  assert/text="^reg.main.name: BS.LAT and AOI.LAT both work for FEED_LAT" bsfeedlat EQ aoifeedlat
  assert/text="^reg.main.name: BS.LON and AOI.LON both work for FEED_LON" bsfeedlon EQ aoifeedlon
  assert/text="^reg.main.name: BS.ALT and AOI.ALT both work for FEED_ALT" bsfeedalt EQ aoifeedalt
  !status/key fftFullBSOut

  ! Do an FFT of a VER=2.0 that is not really a full PLATINUM
  say ""
  info "^{platConvInfo} \n      ^{warnFixTC}      \n      ^{warnMiss}"
  fft platMissingKwds  fftMissingOut
  keyword fftMissingOut GET PLAT2BLUE_COMMENT vComment
  assert/text="^reg.main.name: Converted BLUE 2.0 has PLAT2BLUE_COMMENT" vComment EQS "INFO: Conversion Successful"

  ! Add a keyword to a PLATINUM FILE with autoconversion on
  ! NOTE: fixedPlatFullKeywords will be a VER=1.1 after this test
  ! DO THIS TEST AFTER other fixedPlatFullKeywords TESTS
  say ""
  info "^{platConvInfo} \n      There should not be any warnings"
  ! need add case where modify 2.0 input file - add keyword
  keyword fixedPlatFullKeywords put addedkwd "should add, but convert"
  keyword fixedPlatFullKeywords GET ADDEDKWD vKWD ANTENNA vANTENNA VER vVER
  assert/text="^reg.main.name: Platinum gets converted to BLUE 1.1 when key added" vVER EQ 1.1
  assert/text="^reg.main.name: Full Platinum key added" vKWD EQS "should add, but convert"
  assert/text="^reg.main.name: Full Platinum with key added has ANTENNA" vANTENNA EQS DOWNLINK_ANTENNA
  !status/key fixedPlatFullKeywords
return