import sys
import pandas as pd
import matplotlib.pyplot as plt

def plot_data(csv_file):
    # Read CSV data
    df = pd.read_csv(csv_file)
    
    # Sort by A values to ensure proper plotting
    df = df.sort_values('A')
    
    # Calculate average of d12_lb and d12_ub
    df['d12_avg'] = (df['d12_lb'] + df['d12_ub']) / 2
    
    # Create plot
    plt.figure(figsize=(10, 6))
    
    # Plot average line
    plt.plot(df['A'], df['d12_avg'], 
             color='darkorange', 
             linewidth=2,
             label='Average (d12_lb/d12_ub)')
    
    # Plot confidence interval shading
    plt.fill_between(df['A'],
                     df['d12_lb'],
                     df['d12_ub'],
                     color='orange',
                     alpha=0.2,
                     label='Confidence Interval')
    
    # Format plot
    plt.xlabel('A Value', fontsize=12)
    plt.ylabel('d12 Metric', fontsize=12)
    plt.title('d12 Metric vs A Value with Confidence Interval', fontsize=14)
    plt.grid(True, alpha=0.3)
    plt.legend()
    plt.tight_layout()
    
    # Save and show
    plt.savefig('plot.pdf', dpi=600)
    #plt.show()

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python plot.py <input_csv>")
        sys.exit(1)
    
    csv_path = sys.argv[1]
    plot_data(csv_path)
