Raster array¶
Get array¶
The default band number is one.
from girs.rast.raster import RasterReader
r = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif')
print r.get_array() #
Output:
[[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
-9.99900000e+03]
...
[ -9.99900000e+03 -9.99900000e+03 -9.99900000e+03 -9.99900000e+03
-9.99900000e+03 -9.99900000e+03 -9.99900000e+03 -9.99900000e+03
-9.99900000e+03 -9.99900000e+03 -9.99900000e+03 -9.99900000e+03
-9.99900000e+03 -9.99900000e+03 -9.99900000e+03 -9.99900000e+03
-9.99900000e+03]]
The returned array is 2D or 3D, depending on whether band_number is a number or list of numbers:
from girs.rast.raster import RasterReader
r = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif')
print r.get_array(1).shape
print r.get_array([1]).shape
Output:
(14L, 17L)
(1L, 14L, 17L)
import numpy as np
from girs.rast.raster import RasterReader
from girs.rast.proc import composite
rasters = [
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif',
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.11.tif']
r0 = RasterReader(rasters[0])
r1 = composite(*rasters)
print
print r0.get_parameters()
print r1.get_parameters()
print
print r0.get_array(1).dtype
print r0.get_array(1, dtype=np.float64).dtype
print r0.get_array(1).sum()
print r0.get_array([1]).sum()
print
print r0.get_array(1, dtype=np.float64).sum()
print r0.get_array([1], dtype=np.float64).sum()
print
print r1.get_array(2).shape
print r1.get_array([2]).shape
print r1.get_array(2).sum()
print
print r1.get_array([1, 2]).sum(axis=(1, 2))
print r1.get_array([1, 2], dtype=np.float64).sum(axis=(1, 2))
Output:
DIM[1, 17, 14] ND[-9999.0] DT[Float32] DRV[GTiff] SRS[WGS 84WGS 84 / UTM zone 23S] TRANS(252029.43217803087, 27452.34389348292, 0.0, 8017235.206348279, 0.0, -27452.34389348292)
DIM[2, 17, 14] ND[-9999.0, -9999.0] DT[Float32,Float32] DRV[MEM] SRS[WGS 84WGS 84 / UTM zone 23S] TRANS(252029.43217803087, 27452.34389348292, 0.0, 8017235.206348279, 0.0, -27452.34389348292)
float32
float64
-509436.0
-509436.0
-509435.839268
-509435.839268
(14L, 17L)
(1L, 14L, 17L)
-508838.0
[-509435.875 -508837.5625]
[-509435.83926835 -508837.57883388]
Get masked array¶
Masked pixels are pixels with nodata. if a pixel mask is False, the pixel is valid.
from girs.rast.raster import RasterReader
r = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif')
a0 = r.get_array(mask=True)
a1 = r.get_array(mask=False)
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0.min(), a0.max(), a0.mean(), a0.std())
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a1.min(), a1.max(), a1.mean(), a1.std())
Output:
min: 0.00, max: 20.21, mean: 2.74, standard deviation: 4.11
min: -9999.00, max: 20.21, mean: -2140.49, standard deviation: 4103.98
from girs.rast.proc import composite
rasters = [
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif',
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.11.tif']
r = composite(*rasters)
print
a0 = r.get_array(band_number=1, mask=False)
a1 = r.get_array(band_number=2, mask=False)
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0.min(), a0.max(), a0.mean(), a0.std())
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a1.min(), a1.max(), a1.mean(), a1.std())
print
a0 = r.get_array(band_number=1, mask=True)
a1 = r.get_array(band_number=2, mask=True)
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0.min(), a0.max(), a0.mean(), a0.std())
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a1.min(), a1.max(), a1.mean(), a1.std())
print
a0 = r.get_array(band_number='all', mask=True)
print a0.shape
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0[0].min(), a0[0].max(), a0[0].mean(), a0[0].std())
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0[1].min(), a0[1].max(), a0[1].mean(), a0[1].std())
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0.min(), a0.max(), a0.mean(), a0.std())
Output:
min: -9999.00, max: 20.21, mean: -2140.49, standard deviation: 4103.98
min: -9999.00, max: 23.00, mean: -2137.97, standard deviation: 4105.29
min: 0.00, max: 20.21, mean: 2.74, standard deviation: 4.11
min: 0.00, max: 23.00, mean: 5.94, standard deviation: 5.73
(2L, 14L, 17L)
min: -9999.00, max: 20.21, mean: -2140.49, standard deviation: 4103.98
min: -9999.00, max: 23.00, mean: -2137.97, standard deviation: 4105.29
min: -9999.00, max: 23.00, mean: -2139.23, standard deviation: 4104.63
Get scaled array¶
Scale using the equation:
\[a_{new} = \frac{a - a_{min}}{a_{max} - a_{min}}\]
where a is the raster array.
from girs.rast.raster import RasterReader
r = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif')
a0 = r.get_array(mask=True, scale=True)
a1 = r.get_array(mask=True, scale=False)
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0.min(), a0.max(), a0.mean(), a0.std())
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a1.min(), a1.max(), a1.mean(), a1.std())
a0 = r.get_array(mask=False, scale=True) # not useful
a1 = r.get_array(mask=False, scale=False) # the original array
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a0.min(), a0.max(), a0.mean(), a0.std())
print 'min: {:8.2f}, max: {:.2f}, mean: {:8.2f}, standard deviation: {:8.2f}'.format(
a1.min(), a1.max(), a1.mean(), a1.std())
Output:
min: 0.00, max: 1.00, mean: 0.14, standard deviation: 0.20
min: 0.00, max: 20.21, mean: 2.74, standard deviation: 4.11
min: 0.00, max: 1.00, mean: 0.78, standard deviation: 0.41
min: -9999.00, max: 20.21, mean: -2140.49, standard deviation: 4103.98
Get full array¶
from girs.rast.raster import RasterReader
r = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif')
print 'A:', r.get_array_full(np.nan).shape
print 'A:', r.get_array_full(np.nan)[0]
print 'B:', r.get_array_full(0).shape
print 'B:', r.get_array_full(0)[0]
print 'C:', r.get_array_full(None).shape
print 'C:', r.get_array_full(None)[0]
print 'D:', r.get_array_full([np.nan]).shape
print 'D:', r.get_array_full([np.nan])[0][0]
print 'E:', r.get_array_full([0]).shape
print 'E:', r.get_array_full([0])[0][0]
print 'F:', r.get_array_full([None]).shape
print 'F:', r.get_array_full([None])[0][0]
Output:
A: (14L, 17L)
A: [ nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan]
B: (14L, 17L)
B: [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
C: (14L, 17L)
C: [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
D: (1L, 14L, 17L)
D: [ nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan]
E: (1L, 14L, 17L)
E: [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
F: (1L, 14L, 17L)
F: [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Behaviour by setting NAN with integer as dtype:
import numpy as np
from girs.rast.raster import RasterReader
r = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif')
print 'int8 min: {:>20}, max: {:>20}'.format(np.iinfo(np.int8).min, np.iinfo(np.int8).max)
print 'uint8 min: {:>19}, max: {:>19}'.format(np.iinfo(np.uint8).min, np.iinfo(np.uint8).max)
print 'int16 min: {:>20}, max: {:>20}'.format(np.iinfo(np.int16).min, np.iinfo(np.int16).max)
print 'uint16 min: {:>19}, max: {:>19}'.format(np.iinfo(np.uint16).min, np.iinfo(np.uint16).max)
print 'int32 min: {:>20}, max: {:>20}'.format(np.iinfo(np.int32).min, np.iinfo(np.int32).max)
print 'uint32 min: {:>19}, max: {:>19}'.format(np.iinfo(np.uint32).min, np.iinfo(np.uint32).max)
print 'int64 min: {:>20}, max: {:>20}'.format(np.iinfo(np.int64).min, np.iinfo(np.int64).max)
print 'uint64 min: {:>19}, max: {:>19}'.format(np.iinfo(np.uint64).min, np.iinfo(np.uint64).max)
print r.get_array_full([np.nan], dtype=np.int8)[0][0]
print r.get_array_full([np.nan], dtype=np.int16)[0][0]
print r.get_array_full([np.nan], dtype=np.int32)[0][0]
print r.get_array_full([np.nan], dtype=np.int64)[0][0]
print r.get_array_full([np.nan], dtype=np.uint8)[0][0]
print r.get_array_full([np.nan], dtype=np.uint16)[0][0]
print r.get_array_full([np.nan], dtype=np.uint32)[0][0]
print r.get_array_full([np.nan], dtype=np.uint64)[0][0]
Output:
int8 min: -128, max: 127
uint8 min: 0, max: 255
int16 min: -32768, max: 32767
uint16 min: 0, max: 65535
int32 min: -2147483648, max: 2147483647
uint32 min: 0, max: 4294967295
int64 min: -9223372036854775808, max: 9223372036854775807
uint64 min: 0, max: 18446744073709551615
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 -2147483648
-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 -2147483648
-2147483648 -2147483648 -2147483648 -2147483648 -2147483648]
[-9223372036854775808 -9223372036854775808 -9223372036854775808
-9223372036854775808 -9223372036854775808 -9223372036854775808
-9223372036854775808 -9223372036854775808 -9223372036854775808
-9223372036854775808 -9223372036854775808 -9223372036854775808
-9223372036854775808 -9223372036854775808 -9223372036854775808
-9223372036854775808 -9223372036854775808]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[9223372036854775808 9223372036854775808 9223372036854775808
9223372036854775808 9223372036854775808 9223372036854775808
9223372036854775808 9223372036854775808 9223372036854775808
9223372036854775808 9223372036854775808 9223372036854775808
9223372036854775808 9223372036854775808 9223372036854775808
9223372036854775808 9223372036854775808]