!******************************************************************************!
! Test uprightspectrum command
!
! Arguments: none
!
! Note: Test case created in 3.5.4 with major update/correction in 3.7.0
!
! @bug      2899 DSP: Need support for Platinum keyword DATA_INVERSION_FLAG (e.g. UPRIGHT or INVERT_SPECTRUM command)
! @author:  J Bauer / Jeanette McCall
!******************************************************************************!
startmacro/msgid=main/theme=gear4
  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)

  ! If inverted the inversion happens around center freq of25000
  res orig_freq 20000
  res freq_if_uprighted 30000
  res force_invert true
  res normal_operation false
  res origFile ram:testfile2889

  ! create test file (originally no keywords)
  waveform origFile format=cf elem=10e3+1 shape=sin freq=orig_freq amp=1e4 delta=1e-5

  ! check behavior when no keyword is present
  call uprightFile origFile orig_freq normal_operation "no keyword related to spectral sense"
  call uprightPipe origFile orig_freq normal_operation "no keyword related to spectral sense"

  ! force uprighting when no keyword is present
  call uprightFile origFile freq_if_uprighted force_invert "/FORCEINVERSION"
  call uprightPipe origFile freq_if_uprighted force_invert "/FORCEINVERSION"

  ! set keyword DATA_INVERSION_FLAG=0    ! 0=UPRIGHT 
  key origFile  PUT NAME=L:DATA_INVERSION_FLAG VALUE=0

  ! check behavior when DATA_INVERSION_FLAG=0 (UPRIGHT)
  call uprightFile origFile orig_freq normal_operation "L:DATA_INVERSION_FLAG=0 (UPRIGHT)"
  call uprightPipe origFile orig_freq normal_operation "L:DATA_INVERSION_FLAG=0 (UPRIGHT)"

  ! check behavior when DATA_INVERSION_FLAG=0 (UPRIGHT)
  call uprightFile origFile orig_freq normal_operation "DATA_INVERSION_FLAG=0 (UPRIGHT)"
  call uprightPipe origFile orig_freq normal_operation "DATA_INVERSION_FLAG=0 (UPRIGHT)"

  ! set keyword DATA_INVERSION_FLAG=INVERTED=1 (INVERTED)
  key origFile  PUT NAME=L:DATA_INVERSION_FLAG VALUE=1

  ! check behavior when DATA_INVERSION_FLAG=1 (INVERTED)
  call uprightFile origFile freq_if_uprighted force_abort "DATA_INVERSION_FLAG=1 (INVERTED)"
  call uprightPipe origFile freq_if_uprighted force_abort "DATA_INVERSION_FLAG=1 (INVERTED)"

  ! set keyword SNAP_SPECTRAL_SENSE=NORMAL
  ! NOTE: THIS KEYWORD TAKES PRECEDENCE OVER DATA_INVERSION_FLAG
  key origFile  PUT NAME=SNAP_SPECTRAL_SENSE VALUE=NORMAL

  ! check behavior when SPECTRAL_SENSE=NORMAL
  call uprightFile origFile orig_freq normal_operation "SNAP_SPECTRAL_SENSE=NORMAL"
 
  ! set keyword SNAP_SPECTRAL_SENSE=INVALID
  ! NOTE: THIS KEYWORD TAKES PRECEDENCE OVER DATA_INVERSION_FLAG
  key origFile  PUT NAME=SNAP_SPECTRAL_SENSE VALUE=INVALID

  ! check behavior when SPECTRAL_SENSE=INVALID - treated as NORMAL
  call uprightFile origFile orig_freq normal_operation "SNAP_SPECTRAL_SENSE=INVALID"

  ! set keyword SNAP_SPECTRAL_SENSE=NONE
  ! NOTE: THIS KEYWORD TAKES PRECEDENCE OVER DATA_INVERSION_FLAG
  key origFile  PUT NAME=SNAP_SPECTRAL_SENSE VALUE=NONE

  ! check behavior when SPECTRAL_SENSE=NONE - treated as NORMAL
  call uprightFile origFile orig_freq normal_operation "SNAP_SPECTRAL_SENSE=NONEID"

  !**** Note: NEED to end on INVERTED for INTERACTIVE test case ***************!
  ! set keyword SNAP_SPECTRAL_SENSE=INVERTED
  ! NOTE: THIS KEYWORD TAKES PRECEDENCE OVER DATA_INVERSION_FLAG
  key origFile  PUT NAME=SNAP_SPECTRAL_SENSE VALUE=INVERTED

  ! check behavior when SPECTRAL_SENSE=INVERTED
  call uprightFile origFile freq_if_uprighted normal_operation "SNAP_SPECTRAL_SENSE=INVERTED"


  if interAct then
    set instructions "You should see two plots. The plot on the top should &
                      have a freq = 20 MHz and the bottom plot should have a &
                      freq = 30 MHz (inverted around the center freq of 25 MHz)."

    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

      noop origFile _waveorig /WRAP

      ! invert the file
      uprightspectrum _waveorig _waveupright

      ! fft both files for viewing
      fft _waveorig _fft1
      fft _waveupright _uprightfft

      ! plot both files for comparison
      plot/id=plot1  _fft1        READOUT=+onAxis
      plot/id=plot2  _uprightfft  READOUT=+onAxis

    pipe off

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

!******************************************************************************!
! Run uprightSpectrum on file
!******************************************************************************!
procedure uprightFile fn:inFile d:uprightPeak s:forceInvert s:test_type

      ! upright the file
      if forceInvert eqs true then
        uprightspectrum inFile ram:uprightfile /FORCEINVERSION=true
      else
        uprightspectrum inFile ram:uprightfile
      endif

      ! fft both files for viewing
      fft inFile ram:testfft1
      peakpick ram:testfft1 ram:fft1_cfreq
      file open/d tag_fft1_cfreq ram:fft1_cfreq
      set d:fft1_cfreq   tag_fft1_cfreq.getData(0)
      file close tag_fft1_cfreq
      ASSERT/TEXT="^reg.main.name: ^test_type cfreq orig file" fft1_cfreq eqt 20000 /DELTA=50

      fft ram:uprightfile ram:uprightfft
      peakpick ram:uprightfft ram:uprightfft_cfreq
      file open/d tag_uprightfft_cfreq ram:uprightfft_cfreq
      set d:uprightfft_cfreq   tag_uprightfft_cfreq.getData(0)
      file close tag_uprightfft_cfreq
      ASSERT/TEXT="^reg.main.name: ^test_type cfreq uprighted file" uprightfft_cfreq eqt uprightPeak /DELTA=50
      if test_type eqs "DATA_INVERSION_FLAG=1 (INVERTED)" then
        key ram:uprightfft get "DATA_INVERSION_FLAG" flag
        key ram:uprightfft get "ORIG_DATA_INVERSION_FLAG" origflag
        key ram:uprightfft get "UPRIGHTSPECTRUM_COMMENT" comment
        ASSERT/TEXT="^reg.main.name: ^test_type upright file:DATA_INVERSION_FLAG=0 (UPRIGHT)" 0 eq flag
        ASSERT/TEXT="^reg.main.name: ^test_type upright file:ORIG_DATA_INVERSION_FLAG=1 (INVERTED)" 1 eq origflag
        ASSERT/TEXT="^reg.main.name: ^test_type upright file contains comment " &
                    "This data has been uprighted by the UPRIGHTSPECTRUM command" eqs comment
      elseif test_type eqs "SNAP_SPECTRAL_SENSE=INVERTED" then
        key ram:uprightfft get "SNAP_SPECTRAL_SENSE" flag
        key ram:uprightfft get "ORIG_SNAP_SPECTRAL_SENSE" origflag
        key ram:uprightfft get "UPRIGHTSPECTRUM_COMMENT" comment
        ASSERT/TEXT="^reg.main.name: ^test_type upright file:SNAP_SPECTRAL_SENSE=NORMAL" "NORMAL" eqs flag
        ASSERT/TEXT="^reg.main.name: ^test_type upright file:ORIG_SNAP_SPECTRAL_SENSE=INVERTED " "INVERTED" eqs origflag
        ASSERT/TEXT="^reg.main.name: ^test_type upright file:ORIG_SNAP_SPECTRAL_SENSE=INVERTED " "INVERTED" eqs origflag
        ASSERT/TEXT="^reg.main.name: ^test_type upright file contains comment " &
                    "This data has been uprighted by the UPRIGHTSPECTRUM command" eqs comment
      endif

return

!******************************************************************************!
! Run uprightSpectrum on pipe
!******************************************************************************!
procedure uprightPipe fn:inFile d:uprightPeak s:forceInvert s:test_type
   pipe on
      noop origFile _waveorig 
      
      ! invert the file
      if forceInvert eqs true then
        uprightspectrum _waveorig _waveupright /FORCEINVERSION=true
      else
        uprightspectrum _waveorig _waveupright
      endif

      ! fft both files for viewing
      fft _waveorig _fft1
      fft _waveupright _uprightfft
      peakpick _uprightfft ram:upright_cfreq_pipe.tmp

    pipe off

    file open/d tag_uprightfft_cfreq ram:upright_cfreq_pipe.tmp
    set d:uprightfft_cfreq   tag_uprightfft_cfreq.getData(0)
    file close tag_uprightfft_cfreq
    ASSERT/TEXT="^reg.main.name:(pipe:^{test_type}) cfreq upright pipe" uprightfft_cfreq eqt uprightPeak /DELTA=50

return
