I am writing code to add data along the length of a numpy array (for combining satellite data records). In order to do this my code reads two arrays and then uses the function
def swath_stack(array1, array2):
"""Takes two arrays of swath data and compares their dimensions.
The arrays should be equally sized in dimension 1. If they aren't the
function appends the smallest along dimension 0 until they are.
The arrays are then stacked on top of each other."""
if array1.shape[1] > array2.shape[1]:
no_lines = array1.shape[1] - array2.shape[1]
a = np.zeros((array2.shape[0], no_lines))
a.fill(-999.)
new_array = np.hstack((array2, a))
mask = np.zeros(new_array.shape, dtype=int)
mask[np.where(new_array==-999.)] = 1
array2 = ma.masked_array(new_array, mask=mask)
elif array1.shape[1] < array2.shape[1]:
no_lines = array2.shape[1] - array1.shape[1]
a = np.zeros((array1.shape[0], no_lines))
a.fill(-999.)
new_array = np.hstack((array1, a))
mask = np.zeros(new_array.shape, dtype=int)
mask[np.where(new_array==-999.)] = 1
array1 = ma.masked_array(new_array, mask=mask)
return np.vstack((array1, array2))
to make one array of the two in the line
window_data = swath_stack(window_data, stack_data)
In the event that the arrays under consideration are equal in width the swath_stack() function reduces to np.vstack(). My problem is that I keep encountering MemoryError
during this stage. I know that in the case of arithmetic operators it is more memory efficient to do the arithmetic in place (i.e. array1 += array2
as opposed to array1 = array1 + array2
) but I don't know how to avoid this kind of memory issue whilst using my swath_stack() function.
Can anyone please help?