Skip to content
TheChatArchiveThe Chat Archive
o3-mini· OpenAIHugging Face LMArena Human Preference 140K

Write a function to convert ICRF coordinates to ITRF coordinates given a date and time.…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    Write a function to convert ICRF coordinates to ITRF coordinates given a date and time. It is important that your implementation is complete and that you do everything from scratch

  2. Assistant

    Below is one “from‐scratch” Python implementation that takes an input 3‑vector expressed in ICRF (or, equivalently, ICRS) coordinates and a Python datetime (assumed to be in UTC, with the simplifying assumption that UT1 ≅ UTC and with no polar‐motion corrections), and returns the corresponding ITRF coordinates. (In a “real‐world” application you would want to use much more complete models and precise Earth orientation parameters; here we “roll our own” simplified precession–nutation–earth‐rotation model.)

    The basic idea is to form a chain of rotations:

    1. Celestial transformation: Transform the ICRF vector to the “celestial intermediate reference system” (CIRS) by applying (a) a precession matrix computed from Lieske’s approximations and (b) a simple nutation matrix (using the dominant term only).
    2. Earth rotation: Rotate by the Earth Rotation Angle (ERA) to “lock” the celestial vector into Earth–fixed (ITRF) coordinates.
    3. (Optionally, one would then apply a small polar–motion rotation, but here we assume zero polar motion.)

    Below is the complete code with extensive inline comments. (Note that many of the formulas used below are approximate; this “from‐scratch” implementation is meant as an educational starting point rather than a substitute for a full library such as the IERS Conventions code.)


    import math
    import numpy as np
    from datetime import datetime
    
    # -----------------------
    # Helper functions
    # -----------------------
    
    def julian_date(dt):
        """
        Compute the Julian Date from a datetime instance.
        Assumes datetime is in UTC.
        Algorithm from e.g. Jean Meeus.
        """
        year = dt.year
        month = dt.month
        day = dt.day + (dt.hour + dt.minute/60.0 + dt.second/3600.0) / 24.0
    
        if month <= 2:
            year -= 1
            month += 12
    
        A = math.floor(year/100)
        B = 2 - A + math.floor(A/4)
        JD = math.floor(365.25*(year+4716)) + math.floor(30.6001*(month+1)) + day + B - 1524.5
        return JD
    
    def R1(angle):
        """Rotation matrix about the x-axis by given angle (radians)."""
        c = math.cos(angle)
        s = math.sin(angle)
        return np.array([[1, 0, 0],
                         [0, c, -s],
                         [0, s,  c]])
    
    def R2(angle):
        """Rotation matrix about the y-axis by given angle (radians)."""
        c = math.cos(angle)
        s = math.sin(angle)
        return np.array([[ c, 0, s],
                         [ 0, 1, 0],
                         [-s, 0, c]])
    
    def R3(angle):
        """Rotation matrix about the z-axis by given angle (radians)."""
        c = math.cos(angle)
        s = math.sin(angle)
        return np.array([[c, -s, 0],
                         [s,  c, 0],
                         [0,  0, 1]])
    
    # -----------------------
    # Precession matrix (Lieske et al.'s approximations)
    # -----------------------
    
    def precession_matrix(T):
        """
        Compute the precession matrix from ICRF (J2000) to the mean equator and equinox
        at date using approximate formulas.
        
        T is Julian centuries (TT) from J2000.
        
        Uses Lieske et al. (1977) formula in arcseconds:
          zeta_A   = 2306.2181″ * T + 0.30188″ * T² + 0.017998″ * T³
          theta_A  = 2004.3109″ * T - 0.42665″ * T² - 0.041833″ * T³
          z_A      = 2306.2181″ * T + 1.09468″ * T² + 0.018203″ * T³
        Then convert these angles into radians.
        The precession matrix is then given (in one common convention) by:
            P = R3(-z_A) * R2(theta_A) * R3(-zeta_A)
        """
        # Conversion factor from arcseconds to radians.
        arcsec_to_rad = math.pi / (180.0 * 3600.0)
        
        zeta_A  = (2306.2181 * T + 0.30188 * T**2 + 0.017998 * T**3) * arcsec_to_rad
        theta_A = (2004.3109 * T - 0.42665 * T**2 - 0.041833 * T**3) * arcsec_to_rad
        z_A     = (2306.2181 * T + 1.09468 * T**2 + 0.018203 * T**3) * arcsec_to_rad
        
        P = R3(-z_A) @ R2(theta_A) @ R3(-zeta_A)
        return P
    
    # -----------------------
    # Nutation matrix (using dominant term only)
    # -----------------------
    
    def nutation_matrix(T):
        """
        Compute a simple nutation matrix using a single dominant term.
        
        For a more complete implementation one would sum many series terms.
        Here we use:
           Δψ = -17.20" * sin(Ω)
           Δε =  +9.20" * cos(Ω)
        where Ω (the longitude of the ascending node of the Moon’s mean orbit on the ecliptic)
        is given approximately by:
           Ω = 125.04452° - 1934.136261° * T   (with T in Julian centuries)
           
        Also compute the mean obliquity of the ecliptic:
           ε₀ = 84381.448" - 46.8150" * T - 0.00059" * T² + 0.001813" * T³
           
        Then we define:
            ε = ε₀ + Δε
        and form the nutation matrix as (one common approximate formulation):
            N = R1(-ε) * R3(-Δψ) * R1(ε₀)
        (See e.g. Kaplan’s notes.)
        """
        # Conversion factors.
        arcsec_to_rad = math.pi / (180.0 * 3600.0)
        deg_to_rad    = math.pi / 180.0
        
        # Compute Ω in radians.
        Omega_deg = 125.04452 - 1934.136261 * T
        Omega = Omega_deg * deg_to_rad
        
        # Nutation in longitude and obliquity in arcseconds.
        dpsi_arcsec = -17.20 * math.sin(Omega)
        deps_arcsec =  +9.20 * math.cos(Omega)
        
        # Convert to radians.
        dpsi = dpsi_arcsec * arcsec_to_rad
        deps = deps_arcsec * arcsec_to_rad
        
        # Mean obliquity of the ecliptic in arcseconds.
        eps0_arcsec = 84381.448 - 46.8150 * T - 0.00059 * T**2 + 0.001813 * T**3
        eps0 = eps0_arcsec * arcsec_to_rad
        
        # True obliquity.
        eps = eps0 + deps
        
        # Form the nutation rotation matrix.
        # This simple formulation rotates from the mean to the true equator.
        N = R1(-eps) @ R3(-dpsi) @ R1(eps0)
        return N, eps0  # also returning eps0 might be useful
    
    # -----------------------
    # Earth rotation (ERA)
    # -----------------------
    
    def earth_rotation_angle(JD):
        """
        Compute the Earth Rotation Angle (ERA) in radians.
        
        ERA = 2π * (0.7790572732640 + 1.00273781191135448*(JD_UT1 - 2451545.0)) mod 2π.
        
        Here we assume UT1 = UTC.
        """
        ERA = 2 * math.pi * (0.7790572732640 + 1.00273781191135448 * (JD - 2451545.0))
        # Reduce to [0, 2π)
        ERA = ERA % (2 * math.pi)
        return ERA
    
    # -----------------------
    # Complete Transformation Function
    # -----------------------
    
    def icrf_to_itrf(icrf_vector, dt):
        """
        Convert a position vector from ICRF (ICRS) coordinates to ITRF coordinates.
        
        Parameters:
          icrf_vector: A 3-element numpy array representing the vector in ICRF.
          dt: a datetime.datetime instance (assumed UTC; UT1 ~ UTC).
        
        Returns:
          A 3-element numpy array representing the vector in ITRF.
        
        The transformation performed (in order) is:
          - Celestial transformation: ICRF -> mean equator/equinox of date (via precession)
            then adjust for nutation: mean -> true equator/equinox.
          - Earth rotation: apply the Earth Rotation Angle about the z-axis.
          - (Polar motion rotation would be applied here if available.)
        """
        # 1. Compute Julian Date (we assume UT1 ≅ UTC)
        JD = julian_date(dt)
        
        # 2. Compute Julian centuries T from J2000.0
        # Here T is taken as (JD_TT - 2451545.0)/36525.
        # For simplicity, we assume TT ≅ UTC + 64 seconds (error of order 10^-4 days),
        # so for our approximate purpose we set:
        T = (JD - 2451545.0) / 36525.0
        
        # 3. Compute the precession matrix (from J2000 to the mean equator/equinox at date)
        P = precession_matrix(T)
        
        # 4. Compute the nutation matrix (which rotates from the mean to the true equator/equinox)
        N, eps0 = nutation_matrix(T)
        
        # The combined celestial transformation matrix:
        # Q = N * P transforms from ICRF (approximated as the J2000 system) into the Celestial Intermediate Reference System.
        Q = N @ P
        
        # 5. Compute the Earth Rotation Angle and corresponding rotation matrix.
        ERA = earth_rotation_angle(JD)
        R = R3(ERA)
        
        # 6. Optionally, one would apply the polar motion matrix W here.
        # For simplicity we assume zero polar motion (W = identity).
        W = np.identity(3)
        
        # 7. The complete transformation from ICRF to ITRF is then:
        #      v_ITRF = W * R * Q * v_ICRF
        transformation_matrix = W @ R @ Q
        itrf_vector = transformation_matrix @ np.array(icrf_vector)
        return itrf_vector
    
    # -----------------------
    # Example usage
    # -----------------------
    
    if __name__ == '__main__':
        # Define an example ICRF vector (for instance, a unit vector in some arbitrary direction)
        icrf_vec = np.array([1.0, 0.0, 0.0])
        
        # Define the date and time (UTC) at which to perform the transformation.
        # For example: January 1, 2023 at 00:00:00 UTC.
        dt = datetime(2023, 1, 1, 0, 0, 0)
        
        # Convert to ITRF coordinates.
        itrf_vec = icrf_to_itrf(icrf_vec, dt)
        
        print("ICRF vector: ", icrf_vec)
        print("ITRF vector: ", itrf_vec)
    

    Notes on the Implementation

    1. Julian Date:
      The julian_date function implements the standard algorithm (e.g. from Meeus) to compute the Julian Date from a Gregorian calendar date and time.

    2. Precession:
      The precession is computed using the conventional Lieske et al. formulas (in arcseconds) and converted into a rotation matrix according to
      [ P = R_{3}(-z) ; R_{2}(\theta) ; R_{3}(-\zeta) ] This approximates the change in the orientation of Earth’s mean equator and equinox from J2000 to the given date.

    3. Nutation:
      The nutation is approximated by only the dominant term (using the lunar node angle Ω) to compute corrections in longitude (Δψ) and obliquity (Δε). A full treatment would include many periodic terms.

    4. Earth Rotation:
      The Earth Rotation Angle (ERA) is computed from the Julian Date; it represents the rotation of Earth with respect to the Celestial Intermediate Origin (CIO).

    5. Polar Motion:
      For a fully rigorous transformation you would include a polar motion matrix W (using xp, yp values from e.g. IERS bulletins). Here we simply set W to the identity.

    6. Assumptions:
      This implementation makes several simplifying assumptions (e.g. UT1 ≅ UTC, only one nutation term, ignoring polar motion) so its precision is limited. In practice one would use a dedicated library (or consult the IERS Conventions) for high‑precision transformations.

    The code above is “complete” in that it implements all steps (from scratch, without using external astronomy libraries) necessary for a basic ICRF→ITRF conversion.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.