/**
 * Converts decimal degrees into a given format
 *
 * @param   float       $degrees The decimal degrees
 * @param   string      $format  The format: DMS, HMS, MOD360, NONE, RADIANS, default = NONE
 * @param   bool        $mod360  True to convert degrees to a value between 0 and 360 degrees before formating,
 *                               false otherwise, default = false
 * @return  float|array          The degrees expressed in the given format
 * @returns int         0        The degrees or the hours
 * @returns int         1        The minutes
 * @returns float       2        The seconds
 * @returns bool        3        True if positive, false if negative
 */
function aa_degrees_to_format($degrees, $format = null, $mod360 = false)
{
    if ($mod360) {
        $degrees = aa_degrees_to_mod360($degrees);
    }
    switch ($format) {
        case 'DMS':
        case 'dms':
            $degrees = aa_degrees_to_dms($degrees);
            break;
        case 'HMS':
        case 'hms':
            $degrees = aa_degrees_to_hms($degrees);
            break;
        case 'MOD360':
        case 'mod360':
            $degrees = aa_degrees_to_mod360($degrees);
            break;
        case 'NONE':
        case null:
            // passthru, no formatting
            break;
        case 'RADIANS':
        case 'radians':
            $degrees = deg2rad($degrees);
            break;
        default:
            aa_set_error('Invalid format.');
            return false;
    }
    return $degrees;
}
/**
 * Converts decimal degrees into hours, minutes and seconds
 *
 * @param   float $degrees The decimal degrees
 * @return  array          The degrees expressed in hours, minutes and seconds
 * @returns int   0        The hours
 * @returns int   1        The minutes
 * @returns float 2        The seconds
 */
function aa_degrees_to_hms($degrees)
{
    $degrees = aa_degrees_to_mod360($degrees);
    $time = $degrees * 24 / 360;
    $hours = (int) $time;
    $remainder = ($time - $hours) * 60;
    $minutes = (int) $remainder;
    $seconds = ($remainder - $minutes) * 60;
    return array($hours, $minutes, $seconds);
}
/**
 * Calculates the nutation arguments
 *
 * This function is used internally to calculate the nutation.
 *
 * @param   int   $time      The time measured in Julian centuries from the Epoch J2000.0 (JDE 2451 545.0)
 * @param   bool  $use_cache Caches the arguments of the nutation during the calculation, default = true
 * @return  float            The nutation arguments
*/
function aa_nutation_arguments($time, $use_cache = true)
{
    global $_aa_nutation_periodic_terms;
    static $cache = array();
    if ($use_cache === false or !isset($cache[$time])) {
        $T2 = $time * $time;
        $T3 = $T2 * $time;
        $D = aa_degrees_to_mod360(297.85036 + 445267.11148 * $time - 0.0019142 * $T2 + $T3 / 189474);
        $M = aa_degrees_to_mod360(357.52772 + 35999.05034 * $time - 0.0001603 * $T2 - $T3 / 300000);
        $Mprime = aa_degrees_to_mod360(134.96298 + 477198.867398 * $time + 0.0086972 * $T2 + $T3 / 56250);
        $F = aa_degrees_to_mod360(93.27191000000001 + 483202.017538 * $time - 0.0036825 * $T2 + $T3 / 327270);
        $omega = aa_degrees_to_mod360(125.04452 - 1934.136261 * $time + 0.0020708 * $T2 + $T3 / 450000);
        foreach ($_aa_nutation_periodic_terms as $terms) {
            list($coef_D, $coef_M, $coef_Mprime, $coef_F, $coef_omega, $coef0_sin, $coef1_sin) = $terms;
            $nutation_arguments[] = deg2rad($coef_D * $D + $coef_M * $M + $coef_Mprime * $Mprime + $coef_F * $F + $coef_omega * $omega);
        }
        if ($use_cache === false) {
            return $nutation_arguments;
        }
        $cache[$time] = $nutation_arguments;
    }
    return $cache[$time];
}
function calculate_first_argument($time)
{
    $T2 = $time * $time;
    $T3 = $T2 * $time;
    return deg2rad(aa_degrees_to_mod360(125.04452 - 1934.136261 * $time + 0.0020708 * $T2 + $T3 / 450000));
}