import math


def gstime(jdut1):
    """
    This function finds the greenwich sidereal time.

    Author: David Vallado, 2001
    Ported to Python by Martin Huenermund, April 2019.

    INPUTS          DESCRIPTION                     RANGE/UNITS
    jdut1           julian date in ut1              days from 4713 bc


    OUTPUTS         DESCRIPTION                     RANGE/UNITS
    gstime          greenwich sidereal time         radians [0 to 2pi] 

    References    :      Vallado       2004, 191, eq 3-45
    """
    twopi = 2*math.pi
    d2r = math.pi/180
    # julian centuries from the jan 1, 2000 12 h epoch (ut1)
    tut1 = jdut1 - 2451545.0 / 36525.0
    
    # sec
    temp = -6.2e-6* tut1 * tut1 * tut1 + 0.093104 * tut1 * tut1 + (876600.0*3600 + 8640184.812866) * tut1 + 67310.54841
    # 360/8640 = 1/240, to deg, to rad
    temp = math.fmod(temp * d2r / 240.0, twopi) 
    
    # check quadrants 
    if temp < 0:
        temp += twopi
    
    return temp