I have two 3D masked arrays (netCDF4 files output from climate model) that I want to add together. I followed this thread and got the following (simplified) code out of it:
import numpy as np
from netCDF4 import Dataset
from operator import and_
from numpy.ma.core import MaskedArray
with Dataset(dir + 'V10.nc') as file_V10:
with Dataset(dir + 'U10.nc') as file_U10:
raw_V10 = file_V10.variables['V10'][744 : 9503, :, :] ** 2
raw_U10 = file_U10.variables['U10'][744 : 9503, :, :] ** 2
10m_raw_squared = MaskedArray(raw_V10[:].data + raw_U10[:].data, mask=list(map(and_,raw_V10.mask, raw_U10.mask)))
However, I get the error message:
Traceback (most recent call last):
File "code.py", line 92, in <module>
10m_raw_squared = MaskedArray(raw_V10[:].data + raw_U10[:].data, mask=list(map(and_,raw_V10.mask, raw_U10.mask)))
TypeError: 'numpy.bool_' object is not iterable
If I try changing the mask from boolean to string (in order to make it iterable) by adding mask.astype('str'), I get this error message:
Traceback (most recent call last):
File "code.py", line 92, in <module>
10m_raw_squared = MaskedArray(raw_V10[:].data + raw_U10[:].data, mask=list(map(and_,raw_V10.mask.astype('str'),raw_U10.mask.astype('str'))))
TypeError: unsupported operand type(s) for &: 'str' and 'str'
I have also tried to add the arrays together using a for-loop, but somehow couldn't get that to work without losing a dimension and the majority of the array elements of the data.
How can I add my two datasets together?
Edit: I called for the class of the dataset and got the following output:
<class 'numpy.ma.core.MaskedArray'>
You can use np.logical_and
to create the mask.
with Dataset(dir + 'V10.nc') as file_V10:
with Dataset(dir + 'U10.nc') as file_U10:
raw_V10 = file_V10.variables['V10'][744 : 9503, :, :] ** 2
raw_U10 = file_U10.variables['U10'][744 : 9503, :, :] ** 2
mask = np.logical_and(raw_V10.mask, raw_U10.mask)
10m_raw_squared = MaskedArray(raw_V10[:].data + raw_U10[:].data, mask=mask)