import os
import sys
import json
from pathlib import Path



def dict2json(data_dict, file_path):  
    """  
    将字典转换为JSON格式并保存到文件中。  
  
    参数:  
    data_dict (dict): 要转换的字典。  
    file_path (str): 保存JSON文件的路径。  
    """  
    try:  
        with open(file_path, 'w', encoding='utf-8') as json_file:  
            json.dump(data_dict, json_file, ensure_ascii=False, indent=4)  
        print(f"JSON文件已保存到 {file_path}")  
    except Exception as e:  
        print(f"保存JSON文件时出错: {e}")

def get_interpolation(x, point1, point2):
    """
    According to the two extreme value points, the equation of one variable is determined,
    and the solution of the equation is obtained in the domain of definition.

    Arguments:
        x: A float number of the independent variable.
        point1: A set of the coordinate extreme point.
        point2: A set of the other coordinate extreme point.

    Returns:
        y: A float number of the dependent variable.
    """
    try:
        k = (point1[1] - point2[1]) / (point1[0] - point2[0])
        b = (point1[0] * point2[1] - point1[1] * point2[0]) / (point1[0] - point2[0])
        y = x * k + b
        return y
    except Exception as e:
        return f"Error: {str(e)}"
    
def get_frame_with_time(df1, df2):
    # 将dataframe1按照start_time与simTime进行合并
    df1_start = df1.merge(df2[['simTime', 'simFrame']], left_on='start_time', right_on='simTime')
    df1_start = df1_start[['start_time', 'simFrame']].copy()
    df1_start.rename(columns={'simFrame': 'start_frame'}, inplace=True)

    # 将dataframe1按照end_time与simTime进行合并
    df1_end = df1.merge(df2[['simTime', 'simFrame']], left_on='end_time', right_on='simTime')
    df1_end = df1_end[['end_time', 'simFrame']].copy()
    df1_end.rename(columns={'simFrame': 'end_frame'}, inplace=True)

    # 将两个合并后的数据框按照行索引进行拼接
    result = pd.concat([df1_start, df1_end], axis=1)
    return result
    

if __name__ == "__main__":
    pass
