def test01CurvRectRegridderInit(self): ''' Test of the CurvRectRegridder.__init__ method. ''' regridder = CurvRectRegridder() self.assertTrue(regridder != None, "CurvRectRegridder() returned None") regridder.finalize()
def test02CreateCurvGrid(self): ''' Tests the CurvRectRegridder.createCurvGrid method. Since nothing is returned from this method, just checks for unexpected/expected Errors being raised. ''' regridder = CurvRectRegridder() # Test with all corner and center data regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats, self.curv_center_ignr, self.curv_corner_lons, self.curv_corner_lats, self.curv_corner_ignr) # Test without flags regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats, None, self.curv_corner_lons, self.curv_corner_lats) # Test without corners regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats, self.curv_center_ignr) # Test without corners or flags regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats) # TODO: Test invalid cases # Done with this regridder regridder.finalize()
def test10RegridRectToCurvBilinear(self): ''' Tests the CurvRectRegridder.regridRectToCurv method using bilinear regridding ''' regridder = CurvRectRegridder() # Test with only center data and no flags, using bilinear regridding regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats) regridder.assignCurvField() regridder.createRectGrid(self.rect_center_lons, self.rect_center_lats) regridder.assignRectField(self.rect_data) regrid_data = regridder.regridRectToCurv(self.undef_val, ESMP.ESMP_REGRIDMETHOD_BILINEAR) expect_data = numpy.array(self.curv_data, dtype=numpy.float64) mismatch_found = False # Ignore outermost edges of curvilinear grid since # they aren't really well covered by the rectilinear grid # Also ignore the second east-most edge and second south-most edge; # also not covered for i in xrange(1, expect_data.shape[0] - 2): for j in xrange(2, expect_data.shape[1] - 1): if numpy.abs(expect_data[i, j] - regrid_data[i, j]) > 0.0003: mismatch_found = True print "expect = %#6.4f, found = %#6.4f for lon = %7.3f, " \ "lat = %7.3f" % (expect_data[i, j], regrid_data[i, j], self.curv_center_lons[i][j], self.curv_center_lats[i][j]) if mismatch_found: self.fail("data mismatch found")
def test11RegridRectToCurvPatch(self): ''' Tests the CurvRectRegridder.regridRectToCurv method using patch regridding ''' # Mark as the last test so ESMPControl().stopESMP will be called self.last_test = True regridder = CurvRectRegridder() # Test with only center data, and flags only on curvilinear centers, # using patch regridding regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats, self.curv_center_ignr) regridder.assignCurvField() regridder.createRectGrid(self.rect_center_lons, self.rect_center_lats) regridder.assignRectField(self.rect_data) regrid_data = regridder.regridRectToCurv(self.undef_val, ESMP.ESMP_REGRIDMETHOD_PATCH) expect_data = numpy.array(self.curv_data, dtype=numpy.float64) undef_flags = numpy.array(self.curv_center_ignr, dtype=numpy.bool) expect_data[undef_flags] = self.undef_val mismatch_found = False # Ignore outermost edges of curvilinear grid since # they aren't really well covered by the rectilinear grid # Also ignore the second east-most edge and second south-most edge; # also not covered for i in xrange(1, expect_data.shape[0] - 2): for j in xrange(2, expect_data.shape[1] - 1): if numpy.abs(expect_data[i, j] - regrid_data[i, j]) > 0.0011: mismatch_found = True print "expect = %#6.4f, found = %#6.4f for lon = %7.3f, " \ "lat = %7.3f" % (expect_data[i, j], regrid_data[i, j], self.curv_center_lons[i][j], self.curv_center_lats[i][j]) if mismatch_found: self.fail("data mismatch found")
def test09RegridRectToCurvConserve(self): ''' Tests the CurvRectRegridder.regridRectToCurv method using conservative regridding ''' regridder = CurvRectRegridder() # Test with all corner and center data, using conservative regridding regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats, self.curv_center_ignr, self.curv_corner_lons, self.curv_corner_lats, self.curv_corner_ignr) regridder.assignCurvField() regridder.createRectGrid(self.rect_center_lons, self.rect_center_lats, self.rect_center_ignr, self.rect_corner_lons, self.rect_corner_lats, self.rect_corner_ignr) regridder.assignRectField(self.rect_data) regrid_data = regridder.regridRectToCurv(self.undef_val, ESMP.ESMP_REGRIDMETHOD_CONSERVE) expect_data = numpy.array(self.curv_data, dtype=numpy.float64) undef_flags = numpy.array(self.curv_center_ignr, dtype=numpy.bool) expect_data[undef_flags] = self.undef_val # Couple "good" points next to ignored area are a bit wonky expect_data[1, 3] = self.undef_val regrid_data[1, 3] = self.undef_val expect_data[2, 3] = self.undef_val regrid_data[2, 3] = self.undef_val mismatch_found = False # Ignore outermost edges of curvilinear grid since # they aren't really well covered by the rectilinear grid # Also ignore the second east-most edge; # also not well covered and errors are larger for i in xrange(1, expect_data.shape[0] - 2): for j in xrange(1, expect_data.shape[1] - 1): if numpy.abs(expect_data[i, j] - regrid_data[i, j]) > 0.0004: mismatch_found = True print "expect = %#6.4f, found = %#6.4f for lon = %7.3f, " \ "lat = %7.3f" % (expect_data[i, j], regrid_data[i, j], self.curv_center_lons[i][j], self.curv_center_lats[i][j]) if mismatch_found: self.fail("data mismatch found")
def test08RegridCurvToRectPatch(self): ''' Tests the CurvRectRegridder.regridCurvToRect method using patch regridding ''' regridder = CurvRectRegridder() # Test with only center data, and flags only on rectilinear centers, # using patch regridding regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats) regridder.assignCurvField(self.curv_data) regridder.createRectGrid(self.rect_center_lons, self.rect_center_lats, self.rect_center_ignr) regridder.assignRectField() regrid_data = regridder.regridCurvToRect(self.undef_val, ESMP.ESMP_REGRIDMETHOD_PATCH) expect_data = numpy.array(self.rect_data, dtype=numpy.float64) undef_flags = numpy.array(self.rect_center_ignr, dtype=numpy.bool) expect_data[undef_flags] = self.undef_val # one point falls outside the curvilinear centerpoints grid? expect_data[5, 0] = self.undef_val mismatch_found = False for i in xrange(expect_data.shape[0]): for j in xrange(expect_data.shape[1]): if numpy.abs(expect_data[i, j] - regrid_data[i, j]) > 0.0011: mismatch_found = True print "expect = %#6.4f, found = %#6.4f for lon = %5.1f, " \ "lat = %5.1f" % (expect_data[i, j], regrid_data[i, j], self.rect_center_lons[i], self.rect_center_lats[j]) if mismatch_found: self.fail("data mismatch found")
def test07RegridCurvToRectBilinear(self): ''' Tests the CurvRectRegridder.regridCurvToRect method using bilinear regridding ''' regridder = CurvRectRegridder() # Test with only center data and no flags, using bilinear regridding regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats) regridder.assignCurvField(self.curv_data) regridder.createRectGrid(self.rect_center_lons, self.rect_center_lats) regridder.assignRectField() regrid_data = regridder.regridCurvToRect(self.undef_val, ESMP.ESMP_REGRIDMETHOD_BILINEAR) expect_data = numpy.array(self.rect_data, dtype=numpy.float64) mismatch_found = False # one point falls outside the curvilinear centerpoints grid? expect_data[5, 0] = self.undef_val for i in xrange(expect_data.shape[0]): for j in xrange(expect_data.shape[1]): if numpy.abs(expect_data[i, j] - regrid_data[i, j]) > 0.0003: mismatch_found = True print "expect = %#6.4f, found = %#6.4f for lon = %5.1f, " \ "lat = %5.1f" % (expect_data[i, j], regrid_data[i, j], self.rect_center_lons[i], self.rect_center_lats[j]) if mismatch_found: self.fail("data mismatch found")
def test06RegridCurvToRectConserve(self): ''' Tests the CurvRectRegridder.regridCurvToRect method using conservative regridding ''' regridder = CurvRectRegridder() # Test with all corner and center data, using conservative regridding regridder.createCurvGrid(self.curv_center_lons, self.curv_center_lats, self.curv_center_ignr, self.curv_corner_lons, self.curv_corner_lats, self.curv_corner_ignr) regridder.assignCurvField(self.curv_data) regridder.createRectGrid(self.rect_center_lons, self.rect_center_lats, self.rect_center_ignr, self.rect_corner_lons, self.rect_corner_lats, self.rect_corner_ignr) regridder.assignRectField() regrid_data = regridder.regridCurvToRect(self.undef_val, ESMP.ESMP_REGRIDMETHOD_CONSERVE) expect_data = numpy.array(self.rect_data, dtype=numpy.float64) undef_flags = numpy.array(self.rect_center_ignr, dtype=numpy.bool) expect_data[undef_flags] = self.undef_val mismatch_found = False # Couple "good" points next to ignored data area are a bit wonky expect_data[2, 0] = self.undef_val regrid_data[2, 0] = self.undef_val expect_data[2, 1] = self.undef_val regrid_data[2, 1] = self.undef_val for i in xrange(expect_data.shape[0]): for j in xrange(expect_data.shape[1]): if numpy.abs(expect_data[i, j] - regrid_data[i, j]) > 0.0007: mismatch_found = True print "expect = %#6.4f, found = %#6.4f for lon = %5.1f, " \ "lat = %5.1f" % (expect_data[i, j], regrid_data[i, j], self.rect_center_lons[i], self.rect_center_lats[j]) if mismatch_found: self.fail("data mismatch found")
# main routine - for testing "by hand" if __name__ == '__main__': max_negl = 1.0E-4 # Synthesize test data (lons, lats, data, ignore) = createExampleData() # Assign the value to be used for undefined values in the result undef_val = numpy.array([-1.0E32]) # Initialize ESMP if not ESMPControl().startCheckESMP(): raise RuntimeError("Unexpected failure to start ESMP") # Create the regridder regridder = CurvRectRegridder() # "curvilinear" corners every other starting on edges curv_corner_lons = numpy.array(lons[::2, ::2]) curv_corner_lats = numpy.array(lats[::2, ::2]) curv_corner_ignore = numpy.array(ignore[::2, ::2]) # "curvilinear" centers every other starting one in curv_center_lons = numpy.array(lons[1:-1:2, 1:-1:2]) curv_center_lats = numpy.array(lats[1:-1:2, 1:-1:2]) curv_center_ignore = numpy.array(ignore[1:-1:2, 1:-1:2]) # "curvilinear" data at the centers curv_data = numpy.array(data[1:-1:2, 1:-1:2]) # rectilinear corners every other starting one in rect_corner_lons = numpy.array(lons[1:-1:2, 0]) rect_corner_lats = numpy.array(lats[0, 1:-1:2])