# Path: data_cube_utilities/dc_utilities.py
# def create_default_clean_mask(dataset_in):
#     """
#     Creates a data mask that masks nothing.
#     
#     Parameters
#     ----------
#     dataset_in: xarray.Dataset
#         dataset retrieved from the Data Cube.
#     
#     Raises
#     ------
#     ValueError
#         if dataset_in is an empty xarray.Dataset.
#     """
#     data = None
#     if isinstance(dataset_in, xr.Dataset):
#         data_vars = list(dataset_in.data_vars)
#         if len(data_vars) != 0:
#             data = dataset_in[data_vars[0]].data
#     elif isinstance(dataset_in, xr.DataArray):
#         data = dataset_in.data
#     clean_mask = None
#     if isinstance(data, dask.array.core.Array):
#         clean_mask = dask.array.ones_like(data, dtype='uint8')
#     else:
#         if data is None:
#             clean_mask = np.ones(dataset_in.shape, dtype=np.bool)
#         else:
#             clean_mask = np.ones_like(data, dtype=np.bool)
#     return clean_mask.astype(np.bool)


import gc
import numpy as np
import xarray as xr
import warnings
    import scipy.ndimage.filters as conv
from xarray.ufuncs import sign as xr_sign
from . import dc_utilities as utilities
from .dc_utilities import create_default_clean_mask
from datetime import datetime
    tsm = 3983 * _tsmi(dataset_in)**1.6246
    tsm = tsm.where(clean_mask, no_data)

    # Create xarray of data
    _coords = { key:dataset_in[key] for key in dataset_in.dims.keys()}
    dataset_out = xr.Dataset({'tsm': tsm}, coords=_coords)
    return dataset_out


def mask_water_quality(dataset_in, wofs):

    wofs_criteria = wofs.where(wofs > 0.8)
    wofs_criteria.values[wofs_criteria.values > 0] = 0
    kernel = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])

    mask = conv.convolve(wofs_criteria.values, kernel, mode='constant')
    mask = mask.astype(np.float32)

    dataset_out = dataset_in.copy(deep=True)
    for var in dataset_out.data_vars:
        dataset_out[var].values += mask
    dataset_out.where(dataset_out != np.nan, 0)

    return dataset_out


def watanabe_chlorophyll(dataset_in, clean_mask=None, no_data=0):
    assert 'red' in dataset_in and 'nir' in dataset_in, "Red and NIR bands are required for the Watanabe Chlorophyll analysis."
    # Default to masking nothing.
    if clean_mask is None:

# Path: data_cube_utilities/dc_utilities.py
# def create_default_clean_mask(dataset_in):
#     """
#     Creates a data mask that masks nothing.
#     
#     Parameters
#     ----------
#     dataset_in: xarray.Dataset
#         dataset retrieved from the Data Cube.
#     
#     Raises
#     ------
#     ValueError
#         if dataset_in is an empty xarray.Dataset.
#     """
#     data = None
#     if isinstance(dataset_in, xr.Dataset):
#         data_vars = list(dataset_in.data_vars)
#         if len(data_vars) != 0:
#             data = dataset_in[data_vars[0]].data
#     elif isinstance(dataset_in, xr.DataArray):
#         data = dataset_in.data
#     clean_mask = None
#     if isinstance(data, dask.array.core.Array):
#         clean_mask = dask.array.ones_like(data, dtype='uint8')
#     else:
#         if data is None:
#             clean_mask = np.ones(dataset_in.shape, dtype=np.bool)
#         else:
#             clean_mask = np.ones_like(data, dtype=np.bool)
#     return clean_mask.astype(np.bool)


# Path: data_cube_utilities/dc_water_quality.py
import gc
import numpy as np
import xarray as xr
import warnings
    import scipy.ndimage.filters as conv
from xarray.ufuncs import sign as xr_sign
from . import dc_utilities as utilities
from .dc_utilities import create_default_clean_mask
from datetime import datetime

    tsm = 3983 * _tsmi(dataset_in)**1.6246
    tsm = tsm.where(clean_mask, no_data)

    # Create xarray of data
    _coords = { key:dataset_in[key] for key in dataset_in.dims.keys()}
    dataset_out = xr.Dataset({'tsm': tsm}, coords=_coords)
    return dataset_out


def mask_water_quality(dataset_in, wofs):

    wofs_criteria = wofs.where(wofs > 0.8)
    wofs_criteria.values[wofs_criteria.values > 0] = 0
    kernel = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])

    mask = conv.convolve(wofs_criteria.values, kernel, mode='constant')
    mask = mask.astype(np.float32)

    dataset_out = dataset_in.copy(deep=True)
    for var in dataset_out.data_vars:
        dataset_out[var].values += mask
    dataset_out.where(dataset_out != np.nan, 0)

    return dataset_out


def watanabe_chlorophyll(dataset_in, clean_mask=None, no_data=0):
    assert 'red' in dataset_in and 'nir' in dataset_in, "Red and NIR bands are required for the Watanabe Chlorophyll analysis."
    # Default to masking nothing.
    if clean_mask is None:

        clean_mask = create_default_clean_mask(dataset_in)