import struct


def get_slicer_info(mod_file):
    """
    Open an IMOD .mod file and retrieve the Slicer information

    Args:
        mod_file: The .mod file path

    Returns: A list of Slicer point objects with keys {"angles", "coords"}

    """
    results = []
    with open(mod_file, "rb") as file:
        token = file.read(4)
        if token != b"IMOD":
            print(
                "ID of .mod file is not 'IMOD'. This does not seem to be an IMOD MOD file!"
            )
            exit(1)

        # Read past rest of ID and file header
        file.read(236)

        while token != b"IEOF":
            file.seek(-3, 1)
            token = file.read(4)
            if token == b"SLAN":
                # Read past SLAN object size and time
                file.read(8)

                angles = struct.unpack(">" + ("f" * 3), file.read(4 * 3))
                xyz = struct.unpack(">" + ("f" * 3), file.read(4 * 3))

                results.append({"angles": angles, "coords": xyz})
                file.read(32)

                # Read forward a little so next iteration of while loop starts at end of SLAN object
                file.read(3)
            elif token == b"OBJT":
                # Objects are 176 bytes; skip path to make reading faster
                file.read(176)
                # Read forward a little so next iteration of while loop starts at end of object
                file.read(3)

    if len(results) == 0:
        print("Reached end of MOD file without finding slicer angles!")
        exit(1)

    return results