# Path: skyfield/constants.py
# ASEC2RAD = 4.848136811095359935899141e-6
# 
# Path: skyfield/functions.py
# def rot_x(theta):
#     c = cos(theta)
#     s = sin(theta)
#     zero = theta * 0.0
#     one = zero + 1.0
#     return array(((one, zero, zero), (zero, c, -s), (zero, s, c)))
# 
# def rot_y(theta):
#     c = cos(theta)
#     s = sin(theta)
#     zero = theta * 0.0
#     one = zero + 1.0
#     return array(((c, zero, s), (zero, one, zero), (-s, zero, c)))
# 
# def rot_z(theta):
#     c = cos(theta)
#     s = sin(theta)
#     zero = theta * 0.0
#     one = zero + 1.0
#     return array(((c, -s, zero), (s, c, zero), (zero, zero, one)))


import re
import os
import sys
import numpy as np
from pprint import pformat
from textwrap import dedent
from skyfield.constants import ASEC2RAD
from skyfield.functions import rot_x, rot_y, rot_z
inertial_frames = [
    %s,
    ]

inertial_frames = dict((key, array(value)) for key, value in inertial_frames)
"""

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("usage: build_spice_rotations.py /path/to/spice/toolkit/",
              file=sys.stderr)
        sys.exit(2)
    path = os.path.join(sys.argv[1], 'src/spicelib/chgirf.f')
    with open(path) as f:
        fortran = f.read()
    fortran = re.sub(r'\n +\.', '', fortran, flags=re.S)
    names = re.findall(r"DATA FRAMES[^']*'([^']+)", fortran)
    bases = re.findall(r"DATA BASES[^']*'([^']+)", fortran)
    rotations = re.findall(r"DATA DEFS[^']*'([^']+)", fortran)
    assert len(names) == len(bases) == len(rotations) == 21

    bases = {name: base for name, base in zip(names, bases)}
    frames = {}
    framelist = []

    for name, rotation in zip(names, rotations):
        fields = rotation.replace(',', ' ').split()
        matrices = []
        for i in range(0, len(fields), 2):
            theta, axis = fields[i:i+2]

# Path: skyfield/constants.py
# ASEC2RAD = 4.848136811095359935899141e-6
# 
# Path: skyfield/functions.py
# def rot_x(theta):
#     c = cos(theta)
#     s = sin(theta)
#     zero = theta * 0.0
#     one = zero + 1.0
#     return array(((one, zero, zero), (zero, c, -s), (zero, s, c)))
# 
# def rot_y(theta):
#     c = cos(theta)
#     s = sin(theta)
#     zero = theta * 0.0
#     one = zero + 1.0
#     return array(((c, zero, s), (zero, one, zero), (-s, zero, c)))
# 
# def rot_z(theta):
#     c = cos(theta)
#     s = sin(theta)
#     zero = theta * 0.0
#     one = zero + 1.0
#     return array(((c, -s, zero), (s, c, zero), (zero, zero, one)))


# Path: builders/build_spice.py
import re
import os
import sys
import numpy as np
from pprint import pformat
from textwrap import dedent
from skyfield.constants import ASEC2RAD
from skyfield.functions import rot_x, rot_y, rot_z

inertial_frames = [
    %s,
    ]

inertial_frames = dict((key, array(value)) for key, value in inertial_frames)
"""

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("usage: build_spice_rotations.py /path/to/spice/toolkit/",
              file=sys.stderr)
        sys.exit(2)
    path = os.path.join(sys.argv[1], 'src/spicelib/chgirf.f')
    with open(path) as f:
        fortran = f.read()
    fortran = re.sub(r'\n +\.', '', fortran, flags=re.S)
    names = re.findall(r"DATA FRAMES[^']*'([^']+)", fortran)
    bases = re.findall(r"DATA BASES[^']*'([^']+)", fortran)
    rotations = re.findall(r"DATA DEFS[^']*'([^']+)", fortran)
    assert len(names) == len(bases) == len(rotations) == 21

    bases = {name: base for name, base in zip(names, bases)}
    frames = {}
    framelist = []

    for name, rotation in zip(names, rotations):
        fields = rotation.replace(',', ' ').split()
        matrices = []
        for i in range(0, len(fields), 2):
            theta, axis = fields[i:i+2]

            theta = float(theta.replace('D', 'E')) * ASEC2RAD