"""Utility functions for file operations."""
import os
import json

import numpy as np

__all__ = [
    "read_calibration_mat",
]

def read_calibration_mat(mat_path):
    """Read calibration matrix from a json file."""
    sign = True
    info = "Success"
    if not os.path.exists(mat_path):
        info = f"{mat_path} not found, use E(4, 4)"
        sign = False
        mat = np.eye(4)
    else:
        with open(mat_path, "r", encoding="utf-8") as f:
            mat = np.array(json.load(f)["T"])
            if mat.shape != (4, 4):
                info = "The shape is wrong, use E(4, 4)"
                sign = False
                mat = np.eye(4)
    return mat, info, sign
