# Path: gridded/pysgrid/utils.py
# def calculate_bearing(lon_lat_1, lon_lat_2):
#     """
#     return bearing from true north in degrees
# 
#     """
#     lon_lat_1_radians = lon_lat_1 * np.pi / 180
#     lon_lat_2_radians = lon_lat_2 * np.pi / 180
#     lon_1 = lon_lat_1_radians[..., 0]
#     lat_1 = lon_lat_1_radians[..., 1]
#     lon_2 = lon_lat_2_radians[..., 0]
#     lat_2 = lon_lat_2_radians[..., 1]
#     x1 = np.sin(lon_2 - lon_1) * np.cos(lat_2)
#     x2 = np.cos(lat_1) * np.sin(lat_2) - np.sin(lat_1) * np.cos(lat_2) * np.cos(lon_2 - lon_1)  # noqa
#     bearing_radians = np.arctan2(x1, x2)
#     bearing_degrees = bearing_radians * 180 / np.pi
#     return (bearing_degrees + 360) % 360
# 
# def calculate_angle_from_true_east(lon_lat_1, lon_lat_2):
#     """
#     Return the angle from true east in radians.
# 
#     """
#     bearing = calculate_bearing(lon_lat_1, lon_lat_2)
#     bearing_from_true_east = 90 - bearing
#     bearing_from_true_east_radians = bearing_from_true_east * np.pi / 180
#     # not sure if this is the most appropriate thing to do for the last grid
#     # cell
#     angles = np.append(bearing_from_true_east_radians,
#                        bearing_from_true_east_radians[..., -1:],
#                        axis=-1)
#     return angles
# 
# def check_element_equal(lst):
#     """
#     Check that all elements in an
#     iterable are the same.
# 
#     :params lst: iterable object to be checked
#     :type lst: np.array, list, tuple
#     :return: result of element equality check
#     :rtype: bool
# 
#     """
#     return lst[1:] == lst[:-1]
# 
# def does_intersection_exist(a, b):
#     set_a = set(a)
#     try:
#         set_b = set(b)
#     except TypeError:
#         intersect_exists = False
#     else:
#         intersect = set_a.intersection(set_b)
#         if len(intersect) > 0:
#             intersect_exists = True
#         else:
#             intersect_exists = False
#     return intersect_exists
# 
# def pair_arrays(x_array, y_array):
#     """
#     Given two arrays to equal dimensions,
#     pair their values element-wise.
# 
#     For example given arrays [[1, 2], [3, 4]]
#     and [[-1, -2], [-3, -4]], this function will
#     return [[[1, -1], [2, -2]], [[3, -3], [4, -4]]].
# 
#     :param np.array x_array: a numpy array containing "x" coordinates
#     :param np.array y_array: a numpy array containing "y" coordinates
#     :return: array containing (x, y) arrays
#     :rtype: np.array
# 
#     """
#     x_shape = x_array.shape
#     paired_array_shape = x_shape + (2,)
#     paired_array = np.empty(paired_array_shape, dtype=np.float64)
#     paired_array[..., 0] = x_array[:]
#     paired_array[..., 1] = y_array[:]
#     return paired_array


import pytest
import numpy as np
from gridded.pysgrid.utils import (calculate_bearing,
                                   calculate_angle_from_true_east,
                                   check_element_equal,
                                   does_intersection_exist,
                                   pair_arrays,
                                   )
"""
Created on Mar 23, 2015

@author: ayan

"""

from __future__ import (absolute_import, division, print_function)





@pytest.fixture
def intersection_data():
    a = (718, 903, 1029, 1701)
    b = (718, 828)
    c = (15, 20)
    return a, b, c


def test_intersect_exists(intersection_data):
    a, b, c = intersection_data
    result = does_intersection_exist(a, b)
    assert result


def test_intersect_does_not_exist(intersection_data):
    a, b, c = intersection_data

# Path: gridded/pysgrid/utils.py
# def calculate_bearing(lon_lat_1, lon_lat_2):
#     """
#     return bearing from true north in degrees
# 
#     """
#     lon_lat_1_radians = lon_lat_1 * np.pi / 180
#     lon_lat_2_radians = lon_lat_2 * np.pi / 180
#     lon_1 = lon_lat_1_radians[..., 0]
#     lat_1 = lon_lat_1_radians[..., 1]
#     lon_2 = lon_lat_2_radians[..., 0]
#     lat_2 = lon_lat_2_radians[..., 1]
#     x1 = np.sin(lon_2 - lon_1) * np.cos(lat_2)
#     x2 = np.cos(lat_1) * np.sin(lat_2) - np.sin(lat_1) * np.cos(lat_2) * np.cos(lon_2 - lon_1)  # noqa
#     bearing_radians = np.arctan2(x1, x2)
#     bearing_degrees = bearing_radians * 180 / np.pi
#     return (bearing_degrees + 360) % 360
# 
# def calculate_angle_from_true_east(lon_lat_1, lon_lat_2):
#     """
#     Return the angle from true east in radians.
# 
#     """
#     bearing = calculate_bearing(lon_lat_1, lon_lat_2)
#     bearing_from_true_east = 90 - bearing
#     bearing_from_true_east_radians = bearing_from_true_east * np.pi / 180
#     # not sure if this is the most appropriate thing to do for the last grid
#     # cell
#     angles = np.append(bearing_from_true_east_radians,
#                        bearing_from_true_east_radians[..., -1:],
#                        axis=-1)
#     return angles
# 
# def check_element_equal(lst):
#     """
#     Check that all elements in an
#     iterable are the same.
# 
#     :params lst: iterable object to be checked
#     :type lst: np.array, list, tuple
#     :return: result of element equality check
#     :rtype: bool
# 
#     """
#     return lst[1:] == lst[:-1]
# 
# def does_intersection_exist(a, b):
#     set_a = set(a)
#     try:
#         set_b = set(b)
#     except TypeError:
#         intersect_exists = False
#     else:
#         intersect = set_a.intersection(set_b)
#         if len(intersect) > 0:
#             intersect_exists = True
#         else:
#             intersect_exists = False
#     return intersect_exists
# 
# def pair_arrays(x_array, y_array):
#     """
#     Given two arrays to equal dimensions,
#     pair their values element-wise.
# 
#     For example given arrays [[1, 2], [3, 4]]
#     and [[-1, -2], [-3, -4]], this function will
#     return [[[1, -1], [2, -2]], [[3, -3], [4, -4]]].
# 
#     :param np.array x_array: a numpy array containing "x" coordinates
#     :param np.array y_array: a numpy array containing "y" coordinates
#     :return: array containing (x, y) arrays
#     :rtype: np.array
# 
#     """
#     x_shape = x_array.shape
#     paired_array_shape = x_shape + (2,)
#     paired_array = np.empty(paired_array_shape, dtype=np.float64)
#     paired_array[..., 0] = x_array[:]
#     paired_array[..., 1] = y_array[:]
#     return paired_array


# Path: gridded/tests/test_pysgrid/test_utils.py
import pytest
import numpy as np
from gridded.pysgrid.utils import (calculate_bearing,
                                   calculate_angle_from_true_east,
                                   check_element_equal,
                                   does_intersection_exist,
                                   pair_arrays,
                                   )

"""
Created on Mar 23, 2015

@author: ayan

"""

from __future__ import (absolute_import, division, print_function)





@pytest.fixture
def intersection_data():
    a = (718, 903, 1029, 1701)
    b = (718, 828)
    c = (15, 20)
    return a, b, c


def test_intersect_exists(intersection_data):
    a, b, c = intersection_data
    result = does_intersection_exist(a, b)
    assert result


def test_intersect_does_not_exist(intersection_data):
    a, b, c = intersection_data

    result = does_intersection_exist(a, c)