# Path: cotede/qctests/valid_geolocation.py
# def valid_geolocation(lat, lon):
#     if np.shape(lat) != np.shape(lon):
#         module_logger.warning("Coordinates lat & lon have inconsistent shapes")
#         return False
# 
#     lat = np.atleast_1d(lat)
#     lon = np.atleast_1d(lon)
#     idx = (lat >= -90) & (lat <= 90) & (lon >= -180) & (lon <= 360)
#     return idx
# 
# class ValidGeolocation(QCCheck):
#     flag_bad = 3
# 
#     def test(self):
#         self.flags = {}
# 
#         try:
#             # Note that QCCheck fallback to self.data.attrs if attrs not given
#             if hasattr(self, "attrs"):
#                 lat, lon = extract_coordinates(self.data, self.attrs)
#             else:
#                 lat, lon = extract_coordinates(self.data)
#         except LookupError:
#             module_logger.warning("Missing geolocation (lat/lon)")
#             self.flags["valid_position"] = self.flag_bad
#             return
# 
#         idx = valid_geolocation(lat, lon)
#         flag = np.zeros(np.shape(idx), dtype="i1")
#         flag[~idx] = self.flag_bad
#         flag[idx] = self.flag_good
#         self.flags["valid_geolocation"] = flag
# 
# Path: tests/data.py
# class DummyData(object):
#     def __init__(self):
#         self.attrs = {
#                 'datetime': datetime(2016,6,4),
#                 'LATITUDE': 15, 'LONGITUDE': -38}
#         self.data = {
#                 'PRES': ma.fix_invalid(
#                     [2, 6, 10, 21, 44, 79, 100, 150,
#                         200, 400, 410, 650, 1000, 2000, 5000]),
#                 'TEMP': ma.fix_invalid(
#                     [25.32, 25.34, 25.34, 25.31, 24.99, 23.46, 21.85, 17.95,
#                         15.39, 11.08, 6.93, 7.93, 5.71, 3.58, np.nan]),
#                 'PSAL': ma.fix_invalid(
#                     [36.49, 36.51, 36.52, 36.53, 36.59, 36.76, 36.81, 36.39,
#                         35.98, 35.30, 35.28, 34.93, 34.86, np.nan, np.nan])}
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def keys(self):
#         return self.data.keys()
# 
#     @property
#     def attributes(self):
#         print('attributes will be removed. Use attrs instead!')
#         return self.attrs
# 
# Path: tests/qctests/compare.py
# def compare_feature_input_types(feature, *args, **kwargs):
#     compare_feature_tuple(feature, *args, **kwargs)
#     compare_feature_series(feature, *args, **kwargs)
#     compare_feature_dataarray(feature, *args, **kwargs)
# 
# def compare_input_types(Procedure, cfg):
#     compare_tuple(Procedure, cfg)
#     compare_pandas(Procedure, cfg)
#     compare_xarray(Procedure, cfg)


import numpy as np
    import pandas as pd
    import xarray as xr
from numpy import ma
from cotede.qctests.valid_geolocation import valid_geolocation, ValidGeolocation
from ..data import DummyData
from tests.qctests.compare import compare_feature_input_types, compare_input_types


def test_compare_pandas():
    """Validate the results using pandas.DataFrame
    """
    if not PANDAS_AVAILABLE:
        return

    profile = DummyData()
    profile.data["lat"] = profile["TEMP"] * 0
    profile.data["lon"] = profile["TEMP"] * 0
    df = pd.DataFrame(profile.data)

    y = ValidGeolocation(profile)
    y2 = ValidGeolocation(df)

    for f in y.features:
        assert np.allclose(y.features[f], y2.features[f], equal_nan=True)
    for f in y.flags:
        assert type(y.flags[f]) == type(y2.flags[f])
        assert y.flags[f].dtype == y2.flags[f].dtype
        assert np.allclose(y.flags[f], y2.flags[f], equal_nan=True)


def test_compare_xarray():
    """Validate the results using pandas.DataFrame
    """
    if not XARRAY_AVAILABLE:
        return


# Path: cotede/qctests/valid_geolocation.py
# def valid_geolocation(lat, lon):
#     if np.shape(lat) != np.shape(lon):
#         module_logger.warning("Coordinates lat & lon have inconsistent shapes")
#         return False
# 
#     lat = np.atleast_1d(lat)
#     lon = np.atleast_1d(lon)
#     idx = (lat >= -90) & (lat <= 90) & (lon >= -180) & (lon <= 360)
#     return idx
# 
# class ValidGeolocation(QCCheck):
#     flag_bad = 3
# 
#     def test(self):
#         self.flags = {}
# 
#         try:
#             # Note that QCCheck fallback to self.data.attrs if attrs not given
#             if hasattr(self, "attrs"):
#                 lat, lon = extract_coordinates(self.data, self.attrs)
#             else:
#                 lat, lon = extract_coordinates(self.data)
#         except LookupError:
#             module_logger.warning("Missing geolocation (lat/lon)")
#             self.flags["valid_position"] = self.flag_bad
#             return
# 
#         idx = valid_geolocation(lat, lon)
#         flag = np.zeros(np.shape(idx), dtype="i1")
#         flag[~idx] = self.flag_bad
#         flag[idx] = self.flag_good
#         self.flags["valid_geolocation"] = flag
# 
# Path: tests/data.py
# class DummyData(object):
#     def __init__(self):
#         self.attrs = {
#                 'datetime': datetime(2016,6,4),
#                 'LATITUDE': 15, 'LONGITUDE': -38}
#         self.data = {
#                 'PRES': ma.fix_invalid(
#                     [2, 6, 10, 21, 44, 79, 100, 150,
#                         200, 400, 410, 650, 1000, 2000, 5000]),
#                 'TEMP': ma.fix_invalid(
#                     [25.32, 25.34, 25.34, 25.31, 24.99, 23.46, 21.85, 17.95,
#                         15.39, 11.08, 6.93, 7.93, 5.71, 3.58, np.nan]),
#                 'PSAL': ma.fix_invalid(
#                     [36.49, 36.51, 36.52, 36.53, 36.59, 36.76, 36.81, 36.39,
#                         35.98, 35.30, 35.28, 34.93, 34.86, np.nan, np.nan])}
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def keys(self):
#         return self.data.keys()
# 
#     @property
#     def attributes(self):
#         print('attributes will be removed. Use attrs instead!')
#         return self.attrs
# 
# Path: tests/qctests/compare.py
# def compare_feature_input_types(feature, *args, **kwargs):
#     compare_feature_tuple(feature, *args, **kwargs)
#     compare_feature_series(feature, *args, **kwargs)
#     compare_feature_dataarray(feature, *args, **kwargs)
# 
# def compare_input_types(Procedure, cfg):
#     compare_tuple(Procedure, cfg)
#     compare_pandas(Procedure, cfg)
#     compare_xarray(Procedure, cfg)


# Path: tests/qctests/test_qc_valid_geolocation.py
import numpy as np
    import pandas as pd
    import xarray as xr
from numpy import ma
from cotede.qctests.valid_geolocation import valid_geolocation, ValidGeolocation
from ..data import DummyData
from tests.qctests.compare import compare_feature_input_types, compare_input_types



def test_compare_pandas():
    """Validate the results using pandas.DataFrame
    """
    if not PANDAS_AVAILABLE:
        return

    profile = DummyData()
    profile.data["lat"] = profile["TEMP"] * 0
    profile.data["lon"] = profile["TEMP"] * 0
    df = pd.DataFrame(profile.data)

    y = ValidGeolocation(profile)
    y2 = ValidGeolocation(df)

    for f in y.features:
        assert np.allclose(y.features[f], y2.features[f], equal_nan=True)
    for f in y.flags:
        assert type(y.flags[f]) == type(y2.flags[f])
        assert y.flags[f].dtype == y2.flags[f].dtype
        assert np.allclose(y.flags[f], y2.flags[f], equal_nan=True)


def test_compare_xarray():
    """Validate the results using pandas.DataFrame
    """
    if not XARRAY_AVAILABLE:
        return


    profile = DummyData()