util.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:wrf-python 作者: NCAR 项目源码 文件源码
def to_np(array):
    """Return the :class:`numpy.ndarray` contained in an 
    :class:`xarray.DataArray` instance.

    If the :class:`xarray.DataArray` instance does not contain a *_FillValue* 
    or *missing_value* attribute, then this routine simply returns the 
    :attr:`xarray.DataArray.values` attribute.  If the 
    :class:`xarray.DataArray` object contains a *_FillValue* or *missing_value* 
    attribute, then this routine returns a :class:`numpy.ma.MaskedArray` 
    instance, where the NaN values (used by xarray to represent missing data) 
    are replaced with the fill value.

    If the object passed in to this routine is not an 
    :class:`xarray.DataArray` instance, then this routine simply returns the
    passed in object.  This is useful in situations where you do not know 
    if you have an :class:`xarray.DataArray` or a :class:`numpy.ndarray` and 
    simply want a :class:`numpy.ndarray` returned.  

    Args:

        array (:class:`xarray.DataArray`, :class:`numpy.ndarray`, or any \
        object): Can be any object type, but is generally 
            used with :class:`xarray.DataArray` or :class:`numpy.ndarray`.

    Returns:

        :class:`numpy.ndarray` or :class:`numpy.ma.MaskedArray`: The 
        extracted array or the *array* object if *array* is not a 
        :class:`xarray.DataArray` object..

    """

    try:
        fill_value = array.attrs["_FillValue"]
    except AttributeError:
        result = array # Not a DataArray
    except KeyError:
        result = array.values # Does not have missing values
    else:
        result = ma.masked_invalid(array.values, copy=False)
        result.set_fill_value(fill_value)

    return result


# Helper utilities for metadata
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号