#!/opt/conda/conda-bld/pyciao_1701809863847/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeh/bin/python3
# 
#  Copyright (C) 2010-2012,2015-2017,2022  Smithsonian Astrophysical Observatory
#
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#


# ----------------------------------------------------------------------------
# pyregion.t.py
# this file

# pyregion.t
# unix sh script, to activate pyregion.t.py for running regression testing

# OUTFILE
# global variables exported in pyregion.t@
# ----------------------------------------------------------------------------


#------------  Beginning of Template ----------------------
# Support python 2 and 3
from __future__ import print_function

# import the packge
from region import * # this works for all old region.py functions
import region as pyr # Need this to access all the newer pyregion functions
import numpy
import os  # used for environment vars
from numpy import isclose  # for double comparison
from subprocess import call

#rename the function to get the environment variable for readability
getenv = os.environ.get

# get outfile by invoking environment variables
# exported from "pygrpglue.t"
TESTID     = getenv('TESTID')
if( TESTID == None ):
  print("No TESTID specified\n")

OUTFILE = getenv('OUTFILE')
if( OUTFILE != None):
  OutFilePtr = open (OUTFILE, 'w')
  if( OutFilePtr == None):
    print("Unable to open %s\n" % OUTFILE)

INDIR = getenv('INDIR')
if INDIR[-1] != '/':
    INDIR += '/'

# Get pyregion dir for test_region_ui.py
#  NOTE: installed to same location as this script.
pyr_dir = os.path.dirname( os.path.realpath(__file__))

#------------  End of Template ----------------------


# Difference tolerance for floating point number comparison
tolerance = 0.00000001
cleanUp = False

# Compare two float values and print the result to a file
def compare_floats(expected, actual, file):
  if (isclose(actual, expected, tolerance)):
      print("Result: ", expected, file=OutFilePtr)
  else:
      print("Error: expected: ", expected, "  actual: ", actual, file=OutFilePtr)

def compare_float_arrays(expected, actual, file):
  """
  Check that all elements of both arrays are the same
  """
  if (numpy.all(isclose(actual, expected, tolerance))):
      print("Result: ", expected, file=OutFilePtr)
  else:
      print("Error: expected: ", expected, "  actual: ", actual, file=OutFilePtr)
# !! 2
# Below are pecific subroutines for regression testing
#=============================================================================
#
#  --- Subroutines ---
#
#=============================================================================
#
# First version of test
#
def runtest_1(OutFilePtr, test, shape, expected ):
  print("running ", str(test), file=OutFilePtr)

  reg = regParse( shape )
  area = regArea( reg )

  # write the test string to the screen and log file
  print("Test: regArea(regParse(\"" + shape + "\"))", file=OutFilePtr)
  compare_floats(expected, area, OutFilePtr)

#
# Second version of test
#
def runtest_2(OutFilePtr, test, shape, p1, p2, p3, p4, bin, expected ):
  print("running ", str(test), file=OutFilePtr)

  reg = regParse( shape )
  area = regArea( reg, p1, p2, p3, p4, bin )

  # write the test string to the screen and log file
  print("Test: regArea(regParse(" + "\"" + shape + "\"," + str(p1) + "," + str(p2) + "," + str(p3) + "," + str(p4) + "," + str(bin) + "))", file=OutFilePtr)
  compare_floats(expected, area, OutFilePtr)

# !!4
#=============================================================================
#
#   --- Main Routine ---
#
#=============================================================================

#Python does not have a native switch statement, so the code below
#tries to duplicate the C switch format/behavior

class switch(object):
    def __init__(self, value):
        self.value = value
        self.fall = False

    def __iter__(self):
        """Return the match method once, then stop"""
        yield self.match
        raise StopIteration

    def match(self, *args):
        """Indicate whether or not to enter a case suite"""
        if self.fall or not args:
            return True
        elif self.value in args:
            self.fall = True
            return True
        else:
            return False

#shape variables
aa = "RotBox(3860, 3468, 600, 150, 335)";
bb = "RotBox(4167, 4168, 600, 150, 335)";
cc = "RotBox(4331, 3687, 450, 200, 245)";
dd = "RotBox(3648, 3959, 450, 200, 245)";

#additional shapes
circ1 = "circle(10,10,4)"
rect = "rect(10,10,20,20)"
poly = "poly(10,10,20,20,10,20,20,10,15,5)"
circ2 = "circle(11,10,4)"
rbox = "RotBox(10,10,20,20,40)"
ann = "annulus(10,10,2,4)"
ellip = "ellipse(10,10,3,4,45)"
circ_rect = circ1+'+'+rect
rbox_ann = rbox+'-'+ann
circ_ellip_rect =circ1+'+'+ellip+'+'+rect

#Switch statement for TESTID
for case in switch(TESTID):
  if case('test1'):
      runtest_1(OutFilePtr, "test1","rotbox(100,100,10,10,0)*rotbox(200,200,10,10,0)", 0.0 )
      break
  if case('test2'):
      runtest_2(OutFilePtr, "test2", "rotbox(100,100,10,10,0)*rotbox(200,200,10,10,0)",80,80,230,230,0.1, 0.0)
      break
  if case('test3'):
      runtest_2(OutFilePtr, "test3","rotbox(100,100,10,10,0)*rotbox(200,200,10,10,0)",100,100,200,200,0.1, 0.0)
      break
  if case('test4'):
      runtest_1(OutFilePtr, "test4","rotbox(3693,3884,10,10,0)*rotbox(3924.5,4096,20,20,0)",0.0 )
      break
  if case('test5'):
      runtest_1(OutFilePtr, "test5","box(3914,4086,10,10,0)*box(3924.5,4096,20,20,0)", 30.0 )
      break
  if case('test6'):
      runtest_1(OutFilePtr, "test6","circle(1000,1000,10)-circle(2000,2000,20)", 314.159265359 )
      break
  if case('test7'):
      runtest_1(OutFilePtr, "test7",aa, 90000.0 )
      break
  if case('test8'):
      runtest_1(OutFilePtr, "test8",bb, 90000.0 )
      break
  if case('test9'):
      runtest_1(OutFilePtr, "test9",cc, 90000.0 )
      break
  if case('test10'):
      runtest_1(OutFilePtr, "test10",dd, 90000.0 )
      break
  if case('test11'):
      runtest_1(OutFilePtr, "test11",aa+"+"+bb, 180000.0 )
      break
  if case('test12'):
      runtest_1(OutFilePtr, "test12",aa+"+"+cc, 180000.0 )
      break
  if case('test13'):
      runtest_1(OutFilePtr, "test13",aa+"+"+dd, 180000.0 )
      break
  if case('test14'):
      runtest_1(OutFilePtr, "test14",bb+"+"+cc, 180000.0 )
      break
  if case('test15'):
      runtest_1(OutFilePtr, "test15",bb+"+"+dd, 180000.0 )
      break
  if case('test16'):
      runtest_1(OutFilePtr, "test16",cc+"+"+dd, 180000.0 )
      break
  if case('test17'):
      runtest_1(OutFilePtr, "test17",aa+"+"+bb+"+"+cc, 270000.0 )
      break
  if case('test18'):
      runtest_1(OutFilePtr, "test18",aa+"+"+bb+"+"+dd, 270000.0 )
      break
  if case('test19'):
      runtest_1(OutFilePtr, "test19",aa+"+"+cc+"+"+dd, 270000.0 )
      break
  if case('test20'):
      runtest_1(OutFilePtr, "test20",bb+"+"+cc+"+"+dd, 270000.0 )
      break
  if case('test21'):
      runtest_1(OutFilePtr, "test21",aa+"+"+bb+"+"+cc+"+"+dd, 360002.0 )
      break
  if case('test22'):
      runtest_1(OutFilePtr, "test22",dd+"+"+bb+"+"+cc+"+"+aa, 360002.0 )
      break
  if case('test23'):
      runtest_1(OutFilePtr, "test23","box(3860,3468,600,150,335)+box(4167,4168,600,150,335)+box(4331,3687,450,200,245)+box(3920,3867,450,200,245)", 360000.0 )
      break
  if case('test24'):
      runtest_1(OutFilePtr, "test24","circle(4070,4277,100)&pie(4070,4277,0,100,0,90)&!circle(4070,4277,50)", 5943.0 )
      break
  if case('test25'):
      runtest_1(OutFilePtr, "test25","Box(4120.5,4033.5,148,148)&Box(3727.5,4810.5,1050,1050)", 0.0 )
      break
  if case('test26'):
      runtest_1(OutFilePtr, "test26","Box(4120.5,4033.5,148,148)|Box(3727.5,4810.5,1050,1050)", 1124404.0 )
      break
  if case('test27'):
      runtest_2(OutFilePtr, "test27", "ellipse(4138.5,3949.25,5,4.5,0)-ellipse(4138.5,3949.25,2.5,2.25,0)-ellipse(4134.5872,3944.4593,3.4536592,2.3207029,171.78377)-ellipse(4141.5872,3943.9593,3.2738025,3.0163679,171.78377)", 0,0,6000,6000,0.25, 44.875)
      break
  if case('test28'):
      runtest_2(OutFilePtr, "test28", "ellipse(4138.5,3949.25,5,4.5,0)-ellipse(4138.5,3949.25,2.5,2.25,0)-ellipse(4134.5872,3944.4593,3.4536592,2.3207029,171.78377)-ellipse(4141.5872,3943.9593,3.2738025,3.0163679,171.78377)", 4135,3947,4141,3951,0.25, 9.75)
      break
  if case('test29'):
      runtest_2(OutFilePtr, "test29", "rotbox(4096.5,4096.5,100,150,45)", 4000,4000,4200,4200,0.1, 15000.0)
      break
  if case('test30'):
      runtest_2(OutFilePtr, "test30", "rotbox(4096.5,4096.5,100,150,45)", 4050,4050,4150,4150,0.1, 9109.59 )
      break
  if case('test31'):
      runtest_2(OutFilePtr, "test31", "rotbox(4096.5,4096.5,100,150,45)", 4070,4050,4100,4150,0.1, 3010.25)
      break
  if case('test32'):
      runtest_2(OutFilePtr, "test32", "box(4096.5,4096.5,100,150,45)*box(4017.75,4020.25,113.5,113.5,0)", 4050,4050,4150,4150,0.1, 416.9)
      break
  if case('test33'):
      runtest_2(OutFilePtr, "test33", "box(4096.5,4096.5,100,150,45)*circle(4022,4115,1)", 4050,4050,4150,4150,0.1, 0.0)
      break
  if case('test34'):
      runtest_2(OutFilePtr, "test34", "cir(0,0,10)+cir(50,50,10)+cir(100,100,10)", 0,0,100,100,0.1, 473.239265359)
      break
  if case('test35'):
      flag = regInsideRegion( regParse("circle(10,10,4)"), 12, 11 )
      print("running  test35", file=OutFilePtr)
      print("Test: regInsideRegion( regParse(\"circle(10,10,4)\"), 12, 11 )", file=OutFilePtr)
      print("Result: ", flag, file=OutFilePtr)
      break
  if case('test36'):
      print("running  test36", file=OutFilePtr)
      print("Test: regInsideRegion( regParse(\"circle(10,10,4)\"), numpy.array([ 10, 16, 12 ]), numpy.array([ 11, -4, 8 ]) )", file=OutFilePtr)
      print("Result: ", regInsideRegion(regParse("circle(10,10,4)"), numpy.array([ 10, 16, 12 ]), numpy.array([ 11, -4, 8 ])), file=OutFilePtr)
      break
  if case('test37'):
      area1 = regArea(regParse("rotbox(4096.5,4096.5,100,150,45)"))
      expected1 = 15000.0

      area2 = regArea(regParse("annulus(4096.5,4096.5,40,50)"))
      expected2 = 2827.43338823

      area3 = regArea(regParse("rotbox(4096.5,4096.5,100,150,45)" + "-" + "annulus(4096.5,4096.5,40,50)"))
      expected3 = 12190.0

      print("running  test38", file=OutFilePtr)
      print("Test: regArea(regParse(\"rotbox(4096.5,4096.5,100,150,45)\"))", file=OutFilePtr)
      print("Test: regArea(regParse(\"annulus(4096.5,4096.5,40,50)\"))", file=OutFilePtr)
      print("Test: regArea(regParse(\"rotbox(4096.5,4096.5,100,150,45)\" + \"-\" + \"annulus(4096.5,4096.5,40,50)\"))", file=OutFilePtr)

      compare_floats(expected1, area1, OutFilePtr)
      compare_floats(expected2, area2, OutFilePtr)
      compare_floats(expected3, area3, OutFilePtr)
      break
  if case('test38'):
      (x0,y0,x1,y1) = regExtent( regParse("circle(10,10,4)-rect(4,2,5,4)") )
      print("running  test39", file=OutFilePtr)
      print("Test: regExtent( regParse(\"circle(10,10,4)-rect(4,2,5,4)) )", file=OutFilePtr)
      print("Result: ", x0, file=OutFilePtr)
      print("Result: ", y0, file=OutFilePtr)
      print("Result: ", x1, file=OutFilePtr)
      print("Result: ", y1, file=OutFilePtr)
      break
  if case('test39'):
      regRegionString(regParse("circle(10,10,4)"))
      regRegionString(regParse("circle(10,10,4)-rect(4,2,5,4)"))
      print("running  test40", file=OutFilePtr)
      print("Test: regRegionString(regParse(\"circle(10,10,4)\"))", file=OutFilePtr)
      print("Test: regRegionString(regParse(\"circle(10,10,4)-rect(4,2,5,4)\"))", file=OutFilePtr)
      print("Result: ", regRegionString(regParse("circle(10,10,4)")), file=OutFilePtr)
      print("Result: ", regRegionString(regParse("circle(10,10,4)-rect(4,2,5,4)")), file=OutFilePtr)
      break
  if case('test40'):
      region1 = regParse("region(" + INDIR + "test.fits.gz)")
      region2 = regParse("region(" + INDIR + "test.fits)")
      region3 = regParse("region(" + INDIR + "in.reg)")
      region4 = regParse("ellipse(2,2,1,3,0)")
      print("running test41", file=OutFilePtr)
      print("region1: ", regRegionString(region1), file=OutFilePtr)
      print("region2: ", regRegionString(region2), file=OutFilePtr)
      print("region3: ", regRegionString(region3), file=OutFilePtr)
      print("region4: ", regRegionString(region4), file=OutFilePtr)
      break
  if case('test41'):
      runtest_1(OutFilePtr, "test42","polygon(10,10,20,20,10,20,20,10,15,5)", 96.0 )
      runtest_1(OutFilePtr, "test42","polygon(10,10,20,20,10,20,20,10,15,25,10,10)+rect(0,0,5,5)", 100.0)
      break
  if case('test42'):
      region1=regParse( "circle(10,10,4)" )
      region2=regParse(b"circle(11,10,4)" )
      region3=regParse(u"circle(12,10,4)" )
      area   =regArea(region2)
      print("running test42", file=OutFilePtr)
      print("region1 fs-str: ", regRegionString(region1), file=OutFilePtr)
      print("region2 byte-str: ", regRegionString(region2), file=OutFilePtr)
      print("region3 unicode-str: ", regRegionString(region3), file=OutFilePtr)
      print("region2-area =", "%6.2f" % area,      file=OutFilePtr)
      break
  if case('test43'):
      stat = call('python -W ignore {}/test_region_ui.py'.format(pyr_dir),
                  shell=True, stdout=OutFilePtr, stderr=OutFilePtr)
      cleanUp = True
      break
  if case('test44'):
    # Test regGetShapes. Show that pyShape object is retured for circle, rect
    s_circ, s_rect = pyr.regGetShapes(pyr.regParse(circ_rect))
    tp_circ = str(type(s_circ))
    tp_rect = str(type(s_rect))
    print("running test44", file=OutFilePtr)
    print("Test: type(regGetShapes(regParse(\"" + circ1+ "+" + rect + "\"))[0])", file=OutFilePtr)
    if tp_circ.find("pyShape"):  # P2/P3 compatibility
      print("Result: <type 'pyShape'>", file=OutFilePtr)
    else:
      print("Result: " + tp_circ, file=OutFilePtr)
    print("Test: type(regGetShapes(regParse(\"" + circ1+ "+" + rect + "\"))[1])", file=OutFilePtr)
    if tp_rect.find("pyShape"):  # P2/P3 compatibility
      print("Result: <type 'pyShape'>", file=OutFilePtr)
    else:
      print("Result: " + tp_rect, file=OutFilePtr)
    break
  if case('test45'):
    # Test regShapeGetName. Input circ1+rect output correct name for each
    s_circ, s_rect = pyr.regGetShapes(pyr.regParse(circ_rect))
    n_circ = pyr.regShapeGetName(s_circ)
    n_rect = pyr.regShapeGetName(s_rect)
    print("running test45", file=OutFilePtr)
    print("Test: regShapeGetName(regGetShapes(regParse(\"" + circ1+ "+" + rect + "\"))[0])", file=OutFilePtr)
    print("Result: " + str(n_circ), file=OutFilePtr)
    print("Test: regShapeGetName(regGetShapes(regParse(\"" + circ1+ "+" + rect + "\"))[1])", file=OutFilePtr)
    print("Result: " + str(n_rect), file=OutFilePtr)
    break
  if case('test46'):
    # Test regShapeGetNoPoints
    s_poly = pyr.regGetShapes(pyr.regParse(poly))
    s_ellip = pyr.regGetShapes(pyr.regParse(ellip))
    no_poly = pyr.regShapeGetNoPoints(s_poly[0])
    no_ellip = pyr.regShapeGetNoPoints(s_ellip[0])
    print("running test46", file=OutFilePtr)
    print("Test: regShapeGetNoPoints(regGetShapes(regParse(\"" + poly + "\"))[0])", file=OutFilePtr)
    print("Result: " + str(no_poly), file=OutFilePtr)
    print("Test: regShapeGetNoPoints(regGetShapes(regParse(\"" + ellip+  "\"))[0])", file=OutFilePtr)
    print("Result: " + str(no_ellip), file=OutFilePtr)
    break
  if case('test47'):
    # Test regShapeGetAngles: Try circle (no angles) and rotbox (1 angle)
    s_circ = pyr.regGetShapes(pyr.regParse(circ1))
    s_rbox = pyr.regGetShapes(pyr.regParse(rbox))
    a_circ = pyr.regShapeGetAngles(s_circ[0])
    a_rbox = pyr.regShapeGetAngles(s_rbox[0])
    exp_a_rbox = 40.0
    print("running test47", file=OutFilePtr)
    print("Test: regShapeGetAngles(regGetShapes(regParse(\"" + circ1 + "\"))[0])", file=OutFilePtr)
    print("Result: " + str(a_circ), file=OutFilePtr)
    print("Test: regShapeGetAngles(regGetShapes(regParse(\"" + rbox+  "\"))[0])", file=OutFilePtr)
    compare_floats(exp_a_rbox, a_rbox, file=OutFilePtr)
    break
  if case('test48'):
    # Test regShapeGetRadii: Circle (1 radius), ellipse (2 radii), rect(no radii)
    s_circ = pyr.regGetShapes(pyr.regParse(circ1))
    s_rect = pyr.regGetShapes(pyr.regParse(rect))
    s_ellip = pyr.regGetShapes(pyr.regParse(ellip))
    r_circ = pyr.regShapeGetRadii(s_circ[0])
    r_rect = pyr.regShapeGetRadii(s_rect[0])
    r_ellip = pyr.regShapeGetRadii(s_ellip[0])
    exp_r_circ = 4.0
    exp_min_ellip = 3.0
    exp_max_ellip = 4.0
    print("running test48", file=OutFilePtr)
    print("Test: regShapeGetRadii(regGetShapes(regParse(\"" + circ1 + "\"))[0])", file=OutFilePtr)
    compare_floats(exp_r_circ, r_circ, file=OutFilePtr)
    print("Test: regShapeGetRadii(regGetShapes(regParse(\"" + rect+  "\"))[0])", file=OutFilePtr)
    print("Result: " + str(r_rect), file=OutFilePtr)
    print("Test: regShapeGetRadii(regGetShapes(regParse(\"" + ellip + "\"))[0])", file=OutFilePtr)
    compare_floats(exp_min_ellip, r_ellip[0], file=OutFilePtr)
    compare_floats(exp_max_ellip, r_ellip[1], file=OutFilePtr)
    break
  if case('test49'):
    # Test regShapeGetPoints
    s_poly = pyr.regGetShapes(pyr.regParse(poly))
    s_ellip = pyr.regGetShapes(pyr.regParse(ellip))
    p_poly = pyr.regShapeGetPoints(s_poly[0])
    p_ellip = pyr.regShapeGetPoints(s_ellip[0])
    exp_p_poly = (numpy.array([ 10.,  20.,  10.,  20.,  15.,  10.]),
      numpy.array([ 10.,  20.,  20.,  10.,   5.,  10.]))
    exp_p_ellip = (numpy.array([ 10.]), numpy.array([ 10.]))
    print("running test49", file=OutFilePtr)
    print("Test: regShapeGetPoints(regGetShapes(regParse(\"" + poly + "\"))[0])", file=OutFilePtr)
    compare_float_arrays(p_poly, exp_p_poly, file=OutFilePtr)
    print("Test: regShapeGetPoints(regGetShapes(regParse(\"" + ellip+  "\"))[0])", file=OutFilePtr)
    compare_float_arrays(p_ellip, exp_p_ellip,file=OutFilePtr)
    break
  if case('test50'):
    # Test regShapeGetComponents. Add multiple shapes to a region, get right component back
    reg = pyr.regParse(circ_ellip_rect)
    s_reg = pyr.regGetShapes(reg)
    c_reg_circ = pyr.regShapeGetComponent(s_reg[0])
    c_reg_ellip = pyr.regShapeGetComponent(s_reg[1])
    c_reg_rect = pyr.regShapeGetComponent(s_reg[2])

    print("running test50", file=OutFilePtr)
    print("Test: regShapeGetComponent(regGetShapes(regParse(\"" + circ1+ "+" + ellip+ "+" + rect + "\"))[0])", file=OutFilePtr)
    print("Result:  " + str(c_reg_circ), file=OutFilePtr)
    print("Test: regShapeGetComponent(regGetShapes(regParse(\"" + circ1+ "+" + ellip+ "+" + rect + "\"))[1])", file=OutFilePtr)
    print("Result:  " + str(c_reg_ellip), file=OutFilePtr)
    print("Test: regShapeGetComponent(regGetShapes(regParse(\"" + circ1+ "+" + ellip+ "+" + rect + "\"))[2])", file=OutFilePtr)
    print("Result:  " + str(c_reg_rect), file=OutFilePtr)
    break
  if case('test51'):
    # Test regCreateEmptyRegion: show it has no name
    empt = pyr.regCreateEmptyRegion()
    s_empt = pyr.regGetShapes(empt)
    print("running test51", file=OutFilePtr)
    print("Test: regGetShapes(regCreateEmptyRegion())", file=OutFilePtr)
    print("Result:", s_empt, file=OutFilePtr)
    break
  if case('test52'):
    # Test regCompareRegion: 1) simple that are slightly different, 2) complex
    # that are the same,
    r_circ1 = pyr.regParse(circ1)
    r_circ2 = pyr.regParse(circ2)
    circ_comp = pyr.regCompareRegion(r_circ1, r_circ2)
    exp_circ_comp = 0
    r_rbox_ann = pyr.regParse(rbox_ann)
    rbox_ann_comp = pyr.regCompareRegion(r_rbox_ann,r_rbox_ann)
    exp_rbox_ann_comp = 1
    print("running test52", file=OutFilePtr)
    print("Test: regCompareRegion(regParse(\"" + circ1 + "\"),regParse(\"" + circ2 + "\"))", file=OutFilePtr)
    compare_floats(circ_comp,exp_circ_comp, file=OutFilePtr)
    print("Test: regCompareRegion(regParse(\"" + rbox_ann + "\"),regParse(\"" + rbox_ann + "\"))", file=OutFilePtr)
    compare_floats(rbox_ann_comp,exp_rbox_ann_comp, file=OutFilePtr)
    break
  if case('test53'):
    # Test regCopyRegion: 1) compare original w/ copy, 2) compare copy of different regions so it fails
    r_circ = pyr.regParse(circ1)
    r_rbox_ann = pyr.regParse(rbox_ann)
    r_rbox_ann_cop = pyr.regCopyRegion(r_rbox_ann)
    r_circ_cop = pyr.regCopyRegion(r_circ)
    rbox_ann_comp = pyr.regCompareRegion(r_rbox_ann,r_rbox_ann_cop)
    circ_rbox_ann_comp = pyr.regCompareRegion(r_rbox_ann,r_circ_cop)
    exp_rbox_ann_comp = 1
    exp_circ_rbox_ann_comp = 0
    print("running test53", file=OutFilePtr)
    print("Test: regCompareRegion(regParse(\"" + rbox_ann + "\"),regParse(\"" + rbox_ann + "\"))", file=OutFilePtr)
    compare_floats(rbox_ann_comp,exp_rbox_ann_comp, file=OutFilePtr)
    print("Test: regCompareRegion(regParse(\"" + rbox_ann + "\"),regParse(\"" + circ1 + "\"))", file=OutFilePtr)
    compare_floats(circ_rbox_ann_comp,exp_circ_rbox_ann_comp, file=OutFilePtr)
    break
  if case('test54'):
    # Test regGetNoShapes: get 0, 1, few shapes
    r_empt = pyr.regCreateEmptyRegion()
    r_ann = pyr.regParse(ann)
    r_circ_rect = pyr.regParse(circ_rect)
    no_shapes_empt = pyr.regGetNoShapes(r_empt)
    no_shapes_ann = pyr.regGetNoShapes(r_ann)
    no_shapes_circ_rect = pyr.regGetNoShapes(r_circ_rect)
    exp_empt = 0
    exp_ann = 1
    exp_circ_rect = 2
    print("running test54", file=OutFilePtr)
    print("Test: regGetNoShapes(regCreateEmptyRegion())", file=OutFilePtr)
    compare_floats(no_shapes_empt,exp_empt,file=OutFilePtr)
    print("Test: regGetNoShapes(regParse(\"" + ann + "\"))", file=OutFilePtr)
    compare_floats(no_shapes_ann,exp_ann,file=OutFilePtr)
    print("Test: regGetNoShapes(regParse(\"" + circ_rect + "\"))", file=OutFilePtr)
    compare_floats(no_shapes_circ_rect,exp_circ_rect,file=OutFilePtr)
    break
  if case('test55'):
    # Test regGetShape
    s_circ = pyr.regGetShape(pyr.regParse(circ_rect),0)
    s_rect = pyr.regGetShape(pyr.regParse(circ_rect),1)
    tp_circ = str(type(s_circ))
    tp_rect = str(type(s_rect))
    print("running test55", file=OutFilePtr)
    print("Test: type(regGetShape(regParse(\"" + circ1+ "\"),0))", file=OutFilePtr)
    if tp_circ.find("pyShape"):  # P2/P3 compatibility
      print("Result: <type 'pyShape'>", file=OutFilePtr)
    else:
      print("Result: " + tp_circ, file=OutFilePtr)
    print("Test: type(regGetShape(regParse(\"" + rect + "\"),1))", file=OutFilePtr)
    if tp_rect.find("pyShape"):  # P2/P3 compatibility
      print("Result: <type 'pyShape'>", file=OutFilePtr)
    else:
      print("Result: " + tp_rect, file=OutFilePtr)
    break
  if case('test56'):
    # Test regIntersectRegion. Test intersect of region with 2 shapes and 3 shapes
    regInt_circ_rect = pyr.regIntersectRegion(regParse(circ1),regParse(rect))
    regInt_circ_rect_ann = pyr.regIntersectRegion(regParse(circ_rect),regParse(ann))
    print("running test56", file=OutFilePtr)
    print("Test: regRegionString(regIntersectRegion(regParse(\"" + circ1+ "\"),"+  "regParse(\""+ rect + "\")))", file=OutFilePtr)
    print("Result: ", regRegionString(regInt_circ_rect), file=OutFilePtr)
    print("Test: regRegionString(regIntersectRegion(regParse(\"" + circ1+ "+" + rect + "\"),"+  "regParse(\""+ ann + "\")))", file=OutFilePtr)
    print("Result: ", regRegionString(regInt_circ_rect_ann), file=OutFilePtr)
    break
  if case('test57'):
    # Test regInvert. Invert simple and complex
    regInv_circ = pyr.regInvert(regParse(circ1))
    regInv_circ_rect = pyr.regInvert(regParse(circ_rect))
    print("running test57", file=OutFilePtr)
    print("Test: regRegionString(regInvert(regParse(\"" + circ1+ "\")))", file=OutFilePtr)
    print("Result: ", regRegionString(regInv_circ), file=OutFilePtr)
    print("Test: regRegionString(regInvert(regParse(\"" + circ1+ "+" + rect + "\")))", file=OutFilePtr)
    print("Result: ", regRegionString(regInv_circ_rect), file=OutFilePtr)
    break
  if case('test58'):
    # Test regUnionRegion. Test union of region with 2 shapes and 3 shapes
    regUn_circ_rect = pyr.regUnionRegion(regParse(circ1),regParse(rect))
    regUn_circ_rect_ann = pyr.regUnionRegion(regParse(circ_rect),regParse(ann))
    print("running test58", file=OutFilePtr)
    print("Test: regRegionString(regUnionRegion(regParse(\"" + circ1+ "\"),"+  "regParse(\""+ rect + "\")))", file=OutFilePtr)
    print("Result: ", regRegionString(regUn_circ_rect), file=OutFilePtr)
    print("Test: regRegionString(regUnionRegion(regParse(\"" + circ1+ "+" + rect + "\"),"+  "regParse(\""+ ann + "\")))", file=OutFilePtr)
    print("Result: ", regRegionString(regUn_circ_rect_ann), file=OutFilePtr)
    break
  if case('test59'):
    # Test regAppendShape: 1) add shape to empty region, 2) add shape to non-empty region
    reg = pyr.regCreateEmptyRegion()
    xpoints = numpy.array([10])
    ypoints = numpy.array([10])
    radii   = numpy.array([6])
    angles  = None #np.array([30],  dtype=np.long)
    pyr.regAppendShape(reg, "circle", 1, 0, xpoints, ypoints, 2, radii, angles, 0, 0)
    print("running test59", file=OutFilePtr)
    print("Test: regAppendShape(regGetShapes(regParse(pyr.regAppendShape(regCreateEmptyRegion(), \"circle\", 1, 0,"+ str(xpoints)+","+str(ypoints)+","+ "2, "+str(radii)+", "+str(angles)+", 0, 0))))",file=OutFilePtr)
    print("Result: ", pyr.regRegionString(reg), file=OutFilePtr)
    xpoints = numpy.array([7])
    ypoints = numpy.array([8])
    radii   = numpy.array([5,6])
    angles  = numpy.array([200])
    pyr.regAppendShape(reg, "rotbox", 1, 0, xpoints, ypoints, 2, radii, angles, 0, 0)
    print("Test: regAppendShape(regGetShapes(regParse(pyr.regAppendShape(reg, \"rotbox\", 1, 0,"+ str(xpoints)+","+str(ypoints)+","+ "2, "+str(radii)+", "+str(angles)+", 0, 0))))",file=OutFilePtr)
    print("Result: ", pyr.regRegionString(reg), file=OutFilePtr)
    break
  if case('test60'):
    # Test regFreeShape 1) 2 shapes in region -->1 , 2) 1 shape --> empty
    r_circ_rect = regParse(circ_rect)
    pyr.regFreeShape(r_circ_rect,0)
    print("running test60", file=OutFilePtr)
    print("Test: regFreeShape(regParse(\"" + circ1+ "+" + rect + "\"),0)", file=OutFilePtr)
    print("Result: ", regRegionString(r_circ_rect), file=OutFilePtr)
    pyr.regFreeShape(r_circ_rect,0)
    print("Test: regFreeShape(regParse(\"" + rect+ "\"),0)", file=OutFilePtr)
    print("Result: ", regRegionString(r_circ_rect), file=OutFilePtr)
    break
  if case('test61'):
    # Test regCompareShape: 1) simple that are slightly different, 2) complex
    # that are the same,
    s_circ1 = pyr.regGetShapes(pyr.regParse(circ1))
    s_circ2 = pyr.regGetShapes(pyr.regParse(circ2))
    circ_comp = pyr.regCompareShape(s_circ1[0], s_circ2[0])
    exp_circ_comp = 0
    s_poly = pyr.regGetShapes(pyr.regParse(poly))
    poly_comp = pyr.regCompareShape(s_poly[0],s_poly[0])
    exp_poly_comp = 1
    print("running test61", file=OutFilePtr)
    print("Test: regCompareShape(regGetShapes(regParse(\"" + circ1 + "\"))[0],regGetShapes(regParse(\"" + circ2 + "\"))[0])", file=OutFilePtr)
    compare_floats(circ_comp,exp_circ_comp, file=OutFilePtr)
    print("Test: regCompareShape(regGetShapes(regParse(\"" + poly + "\"))[0],regGetShapes(regParse(\"" + poly + "\"))[0])", file=OutFilePtr)
    compare_floats(poly_comp,exp_poly_comp, file=OutFilePtr)
    break
  if case('test62'):
    # -----------------------------------------------------------------
    # Test for SL-164: issue when numpy array is not contiguous C-style
    # -----------------------------------------------------------------
    def calc_areas(sky): 
      x = sky[0] 
      y = sky[1] 
      poly = pyr.polygon(x, y) 

      # calculate the polygon area manually 
      xy = x[:-1] * y[1:] 
      yx = x[1:] * y[:-1] 
      area_check = numpy.abs(xy.sum() - yx.sum()) / 2.0 

      # calculate the polygon area using region method
      area  = poly.area()
      sum_x = x.sum()
      sum_y = y.sum()
      
      return (area, area_check, sum_x, sum_y, poly)

    # test data - 3D as if coming from crates (row,X,Y)
    data = [[[4270.54880171,4270.54907688,4281.78670231,4326.73720403,4326.73720403,4326.73720403,4326.74689452,4326.7484941,4326.74863952,3938.97355515,3416.4242389,3388.67456905,3367.86306423,3367.86306423,3367.86306423,3367.7515466,3311.64626257,3311.61342702,3311.56417369,3311.56663326,4222.00756131,4270.50995917, 4270.54880171],
             [4266.51036013,4266.51007486,4244.22843913,4155.10189622,4155.10189622,4155.10189622,4155.04650078,4115.22687212,4111.60690588,3915.99523121,3652.39720502,3652.43559288,3652.46438274,3652.46438274,3652.46438274,3652.64935988,3763.91888607,3763.99881243,3764.11870197,3807.5583246 ,4266.53632021,4266.55062878,4266.51036013]]]
    pos  = numpy.array(data)
    pos0 = pos[0] # first (only) row

    # Expected output (both methods)
    expected_area   = 199213.0
    expected_manual = 199233.52115118504
    expected_sumx   = 89008.21014224998
    expected_sumy   = 91354.49741295999
    
    print("running test62", file=OutFilePtr)
    print("Test: Polygon from contiguous data array", file=OutFilePtr)
    sky = pos0
    (area, manual, sumx, sumy, p1) = calc_areas(sky)
    compare_floats( expected_area, area, OutFilePtr )
    compare_floats( expected_manual, manual, OutFilePtr )
    compare_floats( expected_sumx, sumx, OutFilePtr )
    compare_floats( expected_sumy, sumy, OutFilePtr )
    
    print("Test: Polygon from non-contiguous data array", file=OutFilePtr)
    indx = numpy.isfinite(pos0[0])
    sky = pos0[:,indx]
    (area, manual, sumx, sumy, p3) = calc_areas(sky)
    compare_floats( expected_area, area, OutFilePtr )
    compare_floats( expected_manual, manual, OutFilePtr )
    compare_floats( expected_sumx, sumx, OutFilePtr )
    compare_floats( expected_sumy, sumy, OutFilePtr )

    break
    
  if case(): # default
      print("Invalid TESTID")

OutFilePtr.close()

# for test43 - need to remove line with total runtime or test will fail
if cleanUp:
  tfile_orig = OUTFILE
  tfile_tmp  = "tmp.txt"
  comstr = "cat %s | egrep -v \"Ran \" > %s ; mv %s %s" % ( tfile_orig, tfile_tmp, tfile_tmp, tfile_orig )
  os.system( comstr )

