!
! Test the plotting of LayerPoly (1999 files).
!
! @author  J. Czechowski / Neon Ngo
!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  call setDatDir

  if interAct then
    res instruct "Try All File buttons.  Adjust special X-Midas color numbers to see that colors &
                  are as expected."

    pipe init
      panel/msgid=MAIN/setup/controls=gc
      plot/msgid=MAIN/id=idplot

      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" "^instruct" 24 10 /title

        gcontrol button nextplot "File" "World,Semi-bad 1999 files,Block1999,SpecialColors" none /nc=2
        gcontrol lval specialColorNum "Special ColorNum" -1 -7 1 1
    pipe off
  endif
endmacro

!**************************************************************************************************!
procedure processMessage m:msg

  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.fid eqs "IDPLOT" then
    if msg.name eqs "ZOOM" then
      invoke ,, reg.idplot.layers.world.refresh() ! Used to duplicate Bug 1162
    endif
  elseif msg.name eqs "NEXTPLOT" then
    if     msg.data EQS "NONE" then
      call test_plot_none
    elseif msg.data EQS "WORLD" then
      call test_plot_world
    elseif msg.data EQSS "Semi-bad" then
      call test_plot_bad_1999
    elseif msg.data EQS "Block1999" then
      call test_plot_block_1999
    elseif msg.data EQS "SpecialColors" then
      call test_plot_1999_special_colors
    else
      warning "Do not know ^msg.data layer"
    endif
  elseif msg.name eqs "SPECIALCOLORNUM" then
    call test_plot_1999_special_colors
  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

!******************************************************************************!
procedure open
  call setInstructions "Click 'World' button to display plot."
return

!**************************************************************************************************!
procedure newplot s:plotstring s:instructions
  message FUNC=send ID=IDPLOT NAME=CLOSEFILE DATA="*"
  foreach item intable testfiles
    if testfiles.^item nFEXISTS then
      warning "Could not generate/find ^testfiles.^item"
      return
    endif
  endfor
  call setInstructions instructions
  message FUNC=send ID=IDPLOT NAME=OPENFILE DATA=^{plotstring}
return

!**************************************************************************************************!
procedure setInstructions s:msgString
  set reg.panel.controls.idtext.value msgString
return

!**************************************************************************************************!
procedure test_plot_none
  result testfiles {}
  call newplot "" "Should see a blank screen"
return

!**************************************************************************************************!
procedure test_plot_world
  result testfiles {}
  result testfiles.world    "^{datDir}.world.prm"

  call newplot "^{testfiles.world}" &
               "Should see plot outlining the continents.\n\n&
                1) Zoom ~14 times until you can zoom in no more\n&
                2) make sure that the plot does not flicker\n\n&
                ********************************************\n&
                Note: after each zoom an extra refresh is forced by the macro as part of &
                the test for Bug 1162"
return

!**************************************************************************************************!
procedure test_plot_block_1999
!
!  This tests a "block-oriented" 1999 file, one with a -999 indicator in place
!  of the color.
!

  set block1999 "nxm.sys.test.test_1999_with_minus999.prm"
  result testfiles {}
  result testfiles.file1 block1999

  call newplot ^{datDir}.world.prm|block1999{Layer={COLORS=RED}} &
               "Should see a RED segment covering part of the ALASKA coast."
return

!**************************************************************************************************!
procedure test_plot_bad_1999
!
! create Midas type 1999 files with invalid color numbers and plot them
! to make sure they are still visible, LayerPoly should print out warning
! messages for these invalid color numbers.
!
! @bug 1225
!
!  A simple form of 1999 file is:
!
!  # Real     Imag
!  - ----     ----
!  1 #pts     color #
!  2 min_lon  min_lat
!  3 max_lon  max_lat
!  4 lon1     lat1
!  5 lon2     lat2
!        ...
!  n lonN     latN

  set file1 "ram:test1_file1999.prm"
  set file2 "ram:test2_file1999.prm"

  ! test file 1 - rectangle in atlantic ocean
  file open/d/n tag file1{TYPE=1999,FORMAT=CF}
  file write tag (5.0, 0.5)  ! create invalid color index that will be truncated to 0 (BG color)
  file write tag (-61.0,19.0)
  file write tag (-39.0,41.0)
  file write tag (-60.0,20.0)
  file write tag (-60.0,40.0)
  file write tag (-40.0,40.0)
  file write tag (-40.0,20.0)
  file write tag (-60.0,20.0)
  file close tag

  result testfiles {}
  result testfiles.file1 file1

  info "Warning about invalid color number is expected"
  call newplot ^{datDir}.world.prm|file1 &
               "Should see a rectangular box &
                in the Atlantic ocean on the world plot. Should also see a &
                warning in the console from LayerPoly on the invalid color number."
return

!**************************************************************************************************!
procedure test_plot_1999_special_colors

  set file1 "ram:test1_file1999.prm"
  set file2 "ram:test2_file1999.prm"

  ! test file 1 - rectangle in atlantic ocean
  set colorNum ^gc.specialColorNum.value

  file open/d/n tag file1{TYPE=1999,FORMAT=CF}
  file write tag (5.0, colorNum)
  file write tag (-61.0,19.0)
  file write tag (-39.0,41.0)
  file write tag (-60.0,20.0)
  file write tag (-60.0,40.0)
  file write tag (-40.0,40.0)
  file write tag (-40.0,20.0)
  file write tag (-60.0,20.0)
  file close tag

  ! test file 2 - triangle to left of the rectangle
  file open/d/n tag2 file2{TYPE=1999,FORMAT=CF}
  file write tag2 (4.0, colorNum)
  file write tag2 (-41.0,20.0)
  file write tag2 (-19.0,40.0)
  file write tag2 (-40.0,21.0)
  file write tag2 (-30.0,39.0)
  file write tag2 (-20.0,21.0)
  file write tag2 (-40.0,21.0)
  file close tag2

  result testfiles {}
  result testfiles.file1 file1
  result testfiles.file2 file2
  call getSpecialXMidasColor gc.specialColorNum.value
  call newplot ^{datDir}.world.prm|file1|file2 &
               "Should see a ^colorName rectangular box and a ^colorName triangle to the right of it &
                in the Atlantic ocean on the world plot."
return
!**************************************************************************************************!
procedure getSpecialXMidasColor L:sc_num
  set colorName "UNKNOWN"
  if sc_num < -6 then
    set colorName WHITE
  elseif sc_num == -6 then
    set colorName WHITE
  elseif sc_num == -5 then
    set colorName BLACK
  elseif sc_num == -4 then
    set colorName GRAY
  elseif sc_num == -3 then
    set colorName GREEN
  elseif sc_num == -2 then
    set colorName RED
  elseif sc_num == -1 then
    set colorName YELLOW
  elseif sc_num == 0 then
    set colorName PURPLE
  elseif sc_num == 1 then
    set colorName FOREGROUND
  else
    set colorName WHITE
  endif
return
