Resizing a WolfArray
Note —
rebin()is also available onWolfArrayModel(no GUI dependency). See the Model/GUI architecture tutorial for details.
It is common to need to resize a matrix and/or adapt its resolution.
To do this, the WolfArray object provides a rebin method that resizes the matrix by adapting its resolution.
The method is primarily designed to change the resolution using an integer factor. It can also be used with non-integer values, but in that case the resulting matrix may be extended to accommodate the new resolution.
[1]:
import numpy as np
from pathlib import Path
import matplotlib.pyplot as plt
from wolfhece import is_enough
if not is_enough('2.2.42'):
raise ImportError("This code requires wolfhece version 2.2.42 or higher. -- try pip install wolfhece --upgrade")
from wolfhece.wolf_array import WolfArray
from wolfhece.pydownloader import toys_dataset
from wolfhece.PyVertexvectors import Zones, zone, vector, wolfvertex as wv
Loading a digital elevation model
Display its resolution and size.
You can verify that the resolution is 5.0 m × 5.0 m.
[2]:
mnt = WolfArray(toys_dataset("Array_Theux_Pepinster", 'mnt.bin'))
print(mnt)
INFO:root:File C:\Users\pierre\Documents\Gitlab\HECEPython\wolfhece\data\downloads\Array_Theux_Pepinster\mnt.bin already exists. Skipping download.
INFO:root:File C:\Users\pierre\Documents\Gitlab\HECEPython\wolfhece\data\downloads\Array_Theux_Pepinster\mnt.bin.txt already exists. Skipping download.
Shape : 480 x 1160
Resolution : 5.0 x 5.0
Spatial extent :
- Origin : (251000.0 ; 135500.0)
- End : (253400.0 ; 141300.0)
- Width x Height : 2400.0 x 5800.0
- Translation : (0.0 ; 0.0)
Null value : 0.0
Changing the resolution to 10 m × 10 m
During the resolution adaptation procedure, you can choose a mathematical operation to apply to the pixel values that will be merged.
'mean': average of the values'min': minimum value'max': maximum value'sum': sum of the values'median': median of the values
[3]:
mnt.rebin(factor =2, # factor can be non-integer (>= 1.0 == zoom out, < 1.0 == zoom in)
operation = 'mean',) # operation can be 'mean', 'min', 'max', 'sum', 'median'
[4]:
print(mnt)
Shape : 240 x 580
Resolution : 10.0 x 10.0
Spatial extent :
- Origin : (251000.0 ; 135500.0)
- End : (253400.0 ; 141300.0)
- Width x Height : 2400.0 x 5800.0
- Translation : (0.0 ; 0.0)
Null value : 0.0
Warning: the operation is performed in place — the original WolfArray is modified.
If you want to keep the original, make a copy before calling rebin.
[5]:
mnt_copy = WolfArray(mold=mnt)
mnt_copy.rebin(factor=2., operation='max')
print(mnt_copy)
Shape : 120 x 290
Resolution : 20.0 x 20.0
Spatial extent :
- Origin : (251000.0 ; 135500.0)
- End : (253400.0 ; 141300.0)
- Width x Height : 2400.0 x 5800.0
- Translation : (0.0 ; 0.0)
Null value : 0.0
Increasing the resolution (zoom in)
In this case there is no aggregation operation — pixel values are simply duplicated.
However, if the factor is a non-integer divisor of the current resolution, the minimum value of ambiguous pixels is kept. In this situation the code:
first finds the greatest common integer divisor of the two resolutions,
applies a zoom-in rebin with that divisor (producing a finer resolution than requested),
applies a second zoom-out rebin with the factor needed to reach the target resolution, using the
'min'operation.
This means the matrix may temporarily grow to accommodate the new resolution, which can increase memory usage significantly.
[6]:
mnt2 = WolfArray(toys_dataset("Array_Theux_Pepinster", 'mnt.bin'))
mnt2.rebin(factor=.5)
print(mnt2)
INFO:root:File C:\Users\pierre\Documents\Gitlab\HECEPython\wolfhece\data\downloads\Array_Theux_Pepinster\mnt.bin already exists. Skipping download.
INFO:root:File C:\Users\pierre\Documents\Gitlab\HECEPython\wolfhece\data\downloads\Array_Theux_Pepinster\mnt.bin.txt already exists. Skipping download.
Shape : 960 x 2320
Resolution : 2.5 x 2.5
Spatial extent :
- Origin : (251000.0 ; 135500.0)
- End : (253400.0 ; 141300.0)
- Width x Height : 2400.0 x 5800.0
- Translation : (0.0 ; 0.0)
Null value : 0.0
[7]:
mnt2 = WolfArray(toys_dataset("Array_Theux_Pepinster", 'mnt.bin'))
mnt2.rebin(1./2.5) # zoom in by a factor 2.5
print(mnt2)
INFO:root:File C:\Users\pierre\Documents\Gitlab\HECEPython\wolfhece\data\downloads\Array_Theux_Pepinster\mnt.bin already exists. Skipping download.
INFO:root:File C:\Users\pierre\Documents\Gitlab\HECEPython\wolfhece\data\downloads\Array_Theux_Pepinster\mnt.bin.txt already exists. Skipping download.
WARNING:root:The factor 0.4 doesn't lead to an integer dimension for the Kronecker product
WARNING:root:The array will be rebinned firstly using the most common factor and then using the operation
WARNING:root:The greatest common divisor is 1.0
Shape : 1200 x 2900
Resolution : 2.0 x 2.0
Spatial extent :
- Origin : (251000.0 ; 135500.0)
- End : (253400.0 ; 141300.0)
- Width x Height : 2400.0 x 5800.0
- Translation : (0.0 ; 0.0)
Null value : 0.0
Anticipating the result without performing the rebin
You can obtain the header of a WolfArray after rebin without actually performing it.
This is useful to estimate the memory footprint before launching the operation.
[8]:
print((mnt2.get_rebin_header(3))) # anticipate the header after rebin by a factor 3 without doing the rebin
Shape : 400 x 967
Resolution : 6.0 x 6.0
Spatial extent :
- Origin : (251000.0 ; 135500.0)
- End : (253400.0 ; 141302.0)
- Width x Height : 2400.0 x 5802.0
- Translation : (0.0 ; 0.0)
Null value : 0.0
When rebin is not possible
In that case an error is raised. You will need to adapt your code accordingly, or report your specific case for analysis and potential integration in a future version of the library.