def test_pcr_as_numpy(self): array = numpy.array([ [-2.0, -1.0 ], [ 0.0, numpy.nan], [ 1.0, 2.0 ] ]) nrRows, nrCols, cellSize = 3, 2, 1.0 west, north = 0.0, 0.0 pcraster.setclone(nrRows, nrCols, cellSize, west, north) # Create a raster. raster = pcraster.numpy2pcr(pcraster.Scalar, array, 999.0) # Test type checking. with self.assertRaises(Exception) as context_manager: pcraster.pcr_as_numpy(5) self.assertEqual(str(context_manager.exception), "Expecting a PCRaster field") # Create an array referencing the raster. array2 = pcraster.pcr_as_numpy(raster) self.assertEqual(array2[0][0], -2) self.assertEqual(array2[0][1], -1) self.assertEqual(array2[1][0], 0) self.assertTrue(numpy.isnan(array2[1][1])) self.assertEqual(array2[2][0], 1) self.assertEqual(array2[2][1], 2) # Change the array and verify the raster changed too. array2[0][0] = 5.0 self.assertEqual(pcraster.pcr2numpy(raster, 999.0)[0][0], 5.0) # Replace exising raster and verify the array still behaves. raster += 1.0 self.assertEqual(array2[0][0], 5.0) # Delete the raster and verify the array still behaves. del raster self.assertEqual(array2[0][0], 5.0) self.assertEqual(array2[2][1], 2.0)
def test_002(self): """ nonspatial and pcr_as_numpy """ nrRows, nrCols, cellSize = 3, 2, 1.0 west, north = 0.0, 0.0 pcraster.setclone(nrRows, nrCols, cellSize, west, north) nonspatial = pcraster.nominal(5) with self.assertRaises(Exception) as context_manager: array = pcraster.pcr_as_numpy(nonspatial) self.assertEqual(str(context_manager.exception), "Argument is non-spatial, only spatial PCRaster data types are supported")