!******************************************************************************!
! This test case plots several points in a diagonal fashion across several
! region groups and checks the point count across specified geographical
! regions.
!
! Arguments: None
!
! @bug      NXM-3542 Count points in a specified geographic region
! @since    NeXtMidas 4.0.0
! @author:  Zachary Matthews
!******************************************************************************!
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)

  call setDatDir

  if interAct then
    set instructions "Check that all terminal tests passed."
  else
    set instructions "This test will run to completion automatically."
  endif

  set ellipselayerPIPE "LT=T5,TimeOrder=False,Group=t5Group2,NAME=DIAG,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=AQUA,SELECTMESSAGES=TRUE"

  invoke origT5Setting   nxm.sys.libg.LayerT5.getAutoAdjustLineColor()
  invoke origPolySetting nxm.sys.libg.LayerPoly.getAutoAdjustLineColor()
  invoke ,, nxm.sys.libg.LayerT5.setAutoAdjustLineColor(true)
  invoke ,, nxm.sys.libg.LayerPoly.setAutoAdjustLineColor(true)

  set bufferLimit 1800
  set this.timer(0) 5
  set l:count 3
  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

    nxm.sys.test.plotellipsetester/DIAGONAL/DIRECTCOLOR=YELLOW/NOFLAGS/NOTEMODETYPE=4Z/RT/RATE=500/STARTTIME="2007:01:02::00:00:00" OUT=_t5pipe{SIZE=^{bufferLimit}} ELEM=Inf

    plot/id=timepipe/msgid=1 ^{datDir}.world.prm|{LT="Drawable",name=timepipe,listlayers=true} readoutExt=+SHOW|+POINTS &
             readout=+OnAxis|+AlwaysDeltas view=latloncontinuous Y1=-90 Y2=90

    invoke ,, reg.timepipe.openFile(_t5pipe{LAYER={^ellipselayerPIPE,FormRegions=true},TAG=DIAGPIPE})

    FEATURE label=pipebox table={NAME=PIPEBOX,TYPE=BOX,COLOR=AQUA,ENABLE=1} plot=timepipe

  pipe off

  invoke ,, nxm.sys.libg.LayerT5.setAutoAdjustLineColor(^origT5Setting)
  invoke ,, nxm.sys.libg.LayerPoly.setAutoAdjustLineColor(^origPolySetting)
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
  elseif msg.fid eqs "TIMEPIPE" then
    if msg.name eqs "DRAGBOX" then
      set pipebox.setBoundsFromTable(msg.data)
    endif
  elseif msg.name eqs "TIMER" then
    invoke diagLayer reg.timepipe.layers.DIAG
    set this.timer(0) 0.5
    call calcPtsInGeoRegion 9 0.25 0.5 "first"
    call calcPtsInGeoRegion 18 -25.25 -50.5 "second"
    call calcPtsInGeoRegion 36 35.25 70.5 "third"
    
    calc count count 1 -
    if count eq 0 then
      set this.timer(0) 0
      if interAct isFalse then
        pipe stop
      endif
    endif
  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

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

!******************************************************************************!
! Calculate the number of points in a specific geographic region
!******************************************************************************!
procedure calcPtsInGeoRegion d:mapToGeoRegionRatio d:latCenter d:lonCenter s:order
  calc latRange 180 mapToGeoRegionRatio /
  calc lonRange 360 mapToGeoRegionRatio /
  calc latMin latRange 2 / -1 * latCenter +
  calc latMax latRange 2 / latCenter +
  calc lonMin lonRange 2 / -1 * lonCenter +
  calc lonMax lonRange 2 / lonCenter +
  invoke numPtsGeoRegion diagLayer.countPointsInRange(^{lonMin},^{lonMax},^{latMin},^{latMax})
  calc numPtsGeoRegionExpected bufferLimit mapToGeoRegionRatio /
  assert/text="total number of points in ^order geographic region = ^numPtsGeoRegionExpected" numPtsGeoRegion EQ numPtsGeoRegionExpected
return
