# pyshic/juliandate/juliandate.py
"""
Brewersza tool to 

Provides utilities for converting dates between Julian and Gregorian calendars, 
including Julian Day Number calculations. Features include type hints, 
improved docstrings, and refined function names for enhanced usability.

Copyright (C) 2024  Cédric Renda

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""

import math
import numpy as np


def brewer_sza(julian_date:float, lat, long, mode='sun', ho3=22, hray=5):
    """
    Calculate solar zenith angle, path lengths for ozone and Rayleigh scattering,
    azimuth, and corrected zenith angle for a given time, location, and mode.

    Parameters:
    - julian_date: Julian date with precision to seconds
    - lat: Latitude
    - long: Longitude (positive east)
    - mode: 'sun', 'azimuth', or 'moon'
    - ho3: Ozone height
    - hray: Rayleigh scattering height

    Returns:
    - ZA: Zenith Angle
    - M2: Ozone path length
    - M3: Rayleigh path length
    - AZ: Azimuth
    - ZC: Corrected Zenith Angle
    """
    R = 6370
    EP = 1 - np.finfo(float).eps


    # Mode-specific calculations
    if mode.lower() == 'moon':
        # Placeholder for LunarAzEl calculation
        # AZ, el = LunarAzEl(T0, lat, -long, 0)
        AZ, el = 0, 0  # This needs to be replaced with actual calculation
        ZA = 90 - el
    else:
        # Sun or azimuth mode calculations here
        pass  # Extend this section based on actual requirements and available algorithms

    # Common calculations for all modes
    E = ZA * math.pi / 180
    M3 = R / (R + hray) * np.sin(E)
    M3 = 1 / np.cos(np.arctan(M3 / np.sqrt(1 - M3**2)))

    M2 = R / (R + ho3) * np.sin(E)
    M2 = 1 / np.cos(np.arctan(M2 / np.sqrt(1 - M2**2)))

    # Placeholder for ZC calculation
    ZC = ZA  # Adjust this based on actual corrected zenith angle calculation

    return ZA, M2, M3, AZ, ZC

# Note: This translation assumes placeholders for certain calculations and simplifies others.
# You'll need to implement or adapt additional functions like julianday, LunarAzEl, and any
# other MATLAB-specific functions to complete Python equivalents.

# This example serves as a starting point for translating the MATLAB script's logic into Python.
