import numpy as np

x = np.array([1, 2, 3, 4, 5])
x

"""
Explanation: A masked array includes:
- mask of bad values travels with the array.
Those elements deemed bad are treated as if they did not exist. Operations using the array automatically use the mask of bad values.
Typically bad values may represent something like a land mask (i.e. sea surface temperature only exists where there is ocean).

All operations related to masked arrays live in numpy.ma submodule.
End of explanation
"""


mx = np.ma.masked_array(data=x,
                        mask=[True, False, False, True, False],
#                        fill_value=-999
                      )
mx

"""
Explanation: The simplest example of manual creation of a masked array:
End of explanation
"""


np.ma.is_masked(mx)

"""
Explanation: We can check if an array contains any masked values
End of explanation
"""


mx[1] is np.ma.masked

"""
Explanation: or we can check if a particular element is masked
End of explanation
"""


mx.data

"""
Explanation: The original data are not erased, they are still stored in the data attribute:
End of explanation
"""


mx.mask

"""
Explanation: The Mask
Can be accessed directly
End of explanation
"""


mx.filled()

"""
Explanation: The masked entries can be filled with a given value to get an usual array back:
End of explanation
"""


mx.mask = np.ma.nomask

mx.mask

"""
Explanation: The mask can also be cleared:
End of explanation
"""


np.log(mx)

np.ma.log(mx)

"""
Explanation: Domain-aware functions
Some functions handle masked values automatically, e.g. the log function.
End of explanation
"""


np.dot(mx, mx)

np.ma.dot(mx, mx)

"""
Explanation: Note that result is the same.
Others don't see the mask, and so a relevant function from the ma submodule should be used instead (if it exists):
End of explanation
"""


a = np.linspace(1, 15, 15)

a

masked_a = np.ma.masked_greater_equal(a, 11)

masked_a

"""
Explanation: Array creation
Often, a task is to mask array depending on a criterion.
End of explanation
"""


HTML(html)

"""
Explanation: Examples
Other simple examples can be found in the NumPy Docs:
https://docs.scipy.org/doc/numpy-1.13.0/reference/maskedarray.generic.html#examples
End of explanation
"""


