import numpy as np
import matplotlib.pyplot as plt

def read_file(filename):
    """
    Reads a file with Julian Dates, periods (seconds), and errors.
    Expected format: Julian Date, Period (seconds), Error (per line).
    Returns: lists of julian_dates, periods, and errors.
    """
    julian_dates = []
    periods = []
    errors = []
    
    with open(filename, 'r') as file:
        for line in file:
            jd, period, error = map(float, line.split())
            julian_dates.append(jd)
            periods.append(period)
            errors.append(error)
    
    return julian_dates, periods, errors

def compute_period_differences(periods):
    """
    Computes the difference in periods between consecutive values.
    """
    return np.diff(periods)  # Returns an array of period differences

def compute_JD_differences(jd):
    """
    Computes the difference in periods between consecutive values.
    """
    return np.diff(jd)  # Returns an array of period differences   

def period_to_angular_velocity(period):
    """
    Converts a period (in seconds) into angular velocity (degrees per second).
    """
    return 360 / period

def compute_angular_velocity_differences(period_diffs):
    """
    Converts the period differences into angular velocity (degree/second).
    """
    return 360 / period_diffs

# Main function to read data, compute differences, and convert to angular velocity
def main(filename):

    # Step 1: Read the file
    julian_dates, periods, errors = read_file(filename)
    
    # Step 2: Compute period differences
    period_diffs = compute_period_differences(periods)
    jd_diffs = compute_JD_differences(julian_dates)
   # print (period_diffs)
   # print(jd_diffs * 86400)
    
    # Step 3: Compute angular velocity differences
    #angular_velocity_diffs = compute_angular_velocity_differences(period_diffs)
    
    # Print results
    for i in range(len(period_diffs)):
        print(julian_dates[i], periods[i],-period_diffs[i]/(jd_diffs[i]*86400))
        #print(f"From JD {julian_dates[i]} to {julian_dates[i+1]}: "
              #f"Period Difference = {period_diffs[i]:.6f} s, "
             # f"Angular Velocity Difference = {angular_velocity_diffs[i]:.6f} deg/s")

# Example usage:
# Replace 'data.txt' with the actual file path containing your data
filename = '94008A.txt'
main(filename)


