# Path: D3HRE/core/navigation_utility.py
# def calculate_initial_compass_bearing(pointA, pointB):
#     """
#     Calculate the compass bearing of the mvoing platform on point A and B.
# 
#     :param pointA: tuple or list in the format of [lat, lon]
#     :param pointB: tuple or list in the format of [lat, lon]
#     :return: float degrees compass bearing between A and B
#     """
#     if (type(pointA) != tuple) or (type(pointB) != tuple):
#         raise TypeError("Only tuples are supported as arguments")
#     lat1 = math.radians(pointA[0])
#     lat2 = math.radians(pointB[0])
#     diffLong = math.radians(pointB[1] - pointA[1])
#     x = math.sin(diffLong) * math.cos(lat2)
#     y = math.cos(lat1) * math.sin(lat2) - (
#         math.sin(lat1) * math.cos(lat2) * math.cos(diffLong)
#     )
#     initial_bearing = math.atan2(x, y)
#     # Now we have the initial bearing but math.atan2 return values
#     # from -180° to + 180° which is not what we want for a compass bearing
#     # The solution is to normalize the initial bearing as shown below
#     initial_bearing = math.degrees(initial_bearing)
#     compass_bearing = (initial_bearing + 360) % 360
# 
#     return compass_bearing


import ephem
import math
import numpy as np
from D3HRE.core.navigation_utility import calculate_initial_compass_bearing
    # Clearness index processing
    zenith_cosine_list = []

    for index, row in dataframe.iterrows():
        zenith_cosine = calculate_zenith_cosine(index, row.lat, row.lon)
        zenith_cosine_list.append(zenith_cosine)

    dataframe['zenith_cos'] = zenith_cosine_list
    dataframe['kt'] = dataframe.SWGDN / (dataframe.SWTDN * zenith_cosine_list)
    # Maximum clearness index for hourly data pvlib
    dataframe.loc[dataframe.kt > 0.82, 'kt'] = 0.82


    # Temperature processing
    dataframe['temperature'] = dataframe.T2M - 273.15


    # Wind speed at 2 metres height
    dataframe['V2'] = np.sqrt(dataframe.V2M ** 2 + dataframe.U2M ** 2)

    # Get true wind direction from north ward and east ward wind speed
    dataframe['true_wind_direction'] = (
        np.degrees(np.arctan2(dataframe['U2M'], dataframe['V2M'])) + 360
    ) % 360

    location_list = [tuple(x) for x in dataframe[['lat', 'lon']].values]

    # Calculate heading with initial compass bearing
    heading = []
    for a, b in zip(location_list[:-1], location_list[1:]):

# Path: D3HRE/core/navigation_utility.py
# def calculate_initial_compass_bearing(pointA, pointB):
#     """
#     Calculate the compass bearing of the mvoing platform on point A and B.
# 
#     :param pointA: tuple or list in the format of [lat, lon]
#     :param pointB: tuple or list in the format of [lat, lon]
#     :return: float degrees compass bearing between A and B
#     """
#     if (type(pointA) != tuple) or (type(pointB) != tuple):
#         raise TypeError("Only tuples are supported as arguments")
#     lat1 = math.radians(pointA[0])
#     lat2 = math.radians(pointB[0])
#     diffLong = math.radians(pointB[1] - pointA[1])
#     x = math.sin(diffLong) * math.cos(lat2)
#     y = math.cos(lat1) * math.sin(lat2) - (
#         math.sin(lat1) * math.cos(lat2) * math.cos(diffLong)
#     )
#     initial_bearing = math.atan2(x, y)
#     # Now we have the initial bearing but math.atan2 return values
#     # from -180° to + 180° which is not what we want for a compass bearing
#     # The solution is to normalize the initial bearing as shown below
#     initial_bearing = math.degrees(initial_bearing)
#     compass_bearing = (initial_bearing + 360) % 360
# 
#     return compass_bearing


# Path: D3HRE/core/weather_data_processing.py
import ephem
import math
import numpy as np
from D3HRE.core.navigation_utility import calculate_initial_compass_bearing

    # Clearness index processing
    zenith_cosine_list = []

    for index, row in dataframe.iterrows():
        zenith_cosine = calculate_zenith_cosine(index, row.lat, row.lon)
        zenith_cosine_list.append(zenith_cosine)

    dataframe['zenith_cos'] = zenith_cosine_list
    dataframe['kt'] = dataframe.SWGDN / (dataframe.SWTDN * zenith_cosine_list)
    # Maximum clearness index for hourly data pvlib
    dataframe.loc[dataframe.kt > 0.82, 'kt'] = 0.82


    # Temperature processing
    dataframe['temperature'] = dataframe.T2M - 273.15


    # Wind speed at 2 metres height
    dataframe['V2'] = np.sqrt(dataframe.V2M ** 2 + dataframe.U2M ** 2)

    # Get true wind direction from north ward and east ward wind speed
    dataframe['true_wind_direction'] = (
        np.degrees(np.arctan2(dataframe['U2M'], dataframe['V2M'])) + 360
    ) % 360

    location_list = [tuple(x) for x in dataframe[['lat', 'lon']].values]

    # Calculate heading with initial compass bearing
    heading = []
    for a, b in zip(location_list[:-1], location_list[1:]):

        heading.append(calculate_initial_compass_bearing(a, b))