!******************************************************************************!
! Test Histogram with complex and scalar input
! CXMODE: REAL, IMAG, MAG
!
! @issue    NXM-4051 Histogram fails with complex input data
! @since    NeXtMidas 4.1.4
! @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)

  res prefix "test_histogram_"

  res data_repeats  10
  res bins          20
  res start        -50
  res end           49  ! forces 0 to bin 11

  res sfile1     "^{prefix}input_s1.tmp"
  res sfile2     "^{prefix}input_s2.tmp"
  res cfile1     "^{prefix}input_c1.tmp"
  res magfile1   "^{prefix}input_m1.tmp"
  res shist1     "^{prefix}output_s1.tmp"
  res shist2     "^{prefix}output_s2.tmp"
  res chist1mag  "^{prefix}output_s1mag.tmp"
  res chist1real "^{prefix}output_s1real.tmp"
  res chist1imag "^{prefix}output_s1imag.tmp"
  res maghist1   "^{prefix}output_mag1.tmp"

  call create1000s

  histogram in=sfile1   out=shist1 BINS=^bins start=^start end=^end
  histogram in=cfile1   out=chist1real BINS=^bins start=^start end=^end CXMODE="REAL"
  histogram in=sfile2   out=shist2 BINS=^bins start=^start end=^end
  histogram in=cfile1   out=chist1imag BINS=^bins start=^start end=^end CXMODE="IMAG"
  histogram in=cfile1   out=chist1mag BINS=^bins start=^start end=^end CXMODE="MAG"
  histogram in=magfile1 out=maghist1 BINS=^bins start=^start end=^end

  ASSERT/text="^reg.main.name: CXMODE=REAL matches" chist1real FEQ/D shist1
  ASSERT/text="^reg.main.name: CXMODE=IMAG matches" chist1imag FEQ/D shist2
  ASSERT/text="^reg.main.name: CXMODE=MAG matches"  chist1mag  FEQ/D maghist1

  if interAct then
    set instructions "Check the terminal for automated tests passing\n\n&
                      ******************************\n\n&
                      Histogram Bins:\n&
                      ##   range   real  imag mag\n&
                      01  -50/-46   0     0    0 \n&
                      02  -45/-41   0     0    0 \n&
                      03  -40/-36   0     0    0 \n&
                      04  -35/-31   0     0    0 \n&
                      05  -30/-26   0     0    0 \n&
                      06  -25/-21   1     0    0 \n&
                      07  -20/-16   1     0    0 \n&
                      08  -15/-11   1     0    0 \n&
                      09  -10/-06   1     0    0 \n&
                      10  -05/-01   0     4    0 \n&
                      11   00/04    1     5    0 \n&
                      12   05/09    0     1    2 \n&
                      13   10/14    2     0    3 \n&
                      14   15/19    0     0    0 \n&
                      15   20/24    1     0    3 \n&
                      16   25/29    0     0    0 \n&
                      17   30/34    2     0    2 \n&
                      18   35/39    0     0    0 \n&
                      19   40/44    0     0    0 \n&
                      20   45/49    0     0    0\n\n&
                      "

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

      plot/id=cfile1     cfile1
      plot/id=shist1     shist1{layer={line={TYPE=+BAR,THICKNESS=4}}} x1=-64 x2=64 type=dot
      plot/id=chist1real chist1real{layer={line={TYPE=+BAR,THICKNESS=4}}} x1=-64 x2=64 type=dot
      plot/id=shist2     shist2{layer={line={TYPE=+BAR,THICKNESS=4}}} x1=-64 x2=64 type=dot
      plot/id=chist1imag chist1imag{layer={line={TYPE=+BAR,THICKNESS=4}}} x1=-64 x2=64 type=dot
      plot/id=chist1mag  chist1mag{layer={line={TYPE=+BAR,THICKNESS=4}}} x1=-64 x2=64 type=dot
      plot/id=maghist1   maghist1{layer={line={TYPE=+BAR,THICKNESS=4}}} x1=-64 x2=64 type=dot

      list2/id=lcfile1     cfile1
      list2/id=lshist1     shist1
      list2/id=lchist1real chist1real
      list2/id=lshist2     shist2
      list2/id=lchist1imag chist1imag
      list2/id=lchist1mag  chist1mag
      list2/id=lmaghist1   maghist1

    pipe off

  endif

  ! Cleanup test files
  files/e ^{prefix}*
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

!******************************************************************************!
procedure create1000s
  result sfile1data {f1=0,f2=-6,f3=10,f4=11,f5=-11,f6=-21,f7=-25,f8=21,f9=31,f10=31}
  result sfile2data {f1=5,f2=-5,f3=3,f4=-2,f5=1,f6=4,f7=-2,f8=3,f9=2,f10=-2}

  result cfile1data {f1=(0,5),f2=(-6,-5),f3=(10,3),f4=(11,-2),f5=(-11,1),f6=(-21,4),f7=(-25,-2),f8=(21,3),f9=(31,2),f10=(31,-2)} 

  call create_file filename=sfile1   type=1000 format=SF data=sfile1data
  call create_file filename=sfile2   type=1000 format=SF data=sfile2data
  call create_file filename=cfile1   type=1000 format=CF data=cfile1data

  fcalc magfile1 cfile1 MAG

return

!******************************************************************************!
procedure create_file fn:filename i:type s:format t:data
  file open/n out ^filename{TYPE=^type,FORM=^format}

  foreach item intable/v data
    invoke ,, out.insertData(-1,{F1=item})
  endfor

  file close out
return

