#!/usr/bin/python3.12
import matplotlib.pyplot as plt
import numpy as np

# Dating methods and their properties
dating_methods = [
    "Dendrochronology", "Rehydroxylation", "Varve Counting", "Radiocarbon (C-14)", "OSL/TL/IRSL", 
    "Electron Spin Resonance", "Fossil Assemblages", "Speleothem Dating", "Cosmogenic Nuclide Dating", 
    "Magnetostratigraphy", "Pottery & Cultural Artifacts", "Uranium-Thorium", "Fission Track Dating", 
    "Potassium-Argon", "Argon-Argon", "Uranium-Lead", "Ice-Core Stratigraphy"
]

# Temporal range (min, max in years)
temporal_range = [
    (0, 14000), (100, 2000), (0, 50000), (0, 50000), (100, 200000), 
    (1000, 2000000), (1000, 500000000), (1000, 500000), (1000, 5000000), 
    (20000, 1000000000), (1000, 10000), (1000, 500000), (100000, 1000000000), 
    (100000, 1000000000), (1000, 1000000000), (1000000, 1000000000), (0, 800000)
]

# Estimated accuracy (as a percentage of range)
estimated_accuracy = [
    0.01, 10, 5, 1, 15, 
    20, 20, 10, 20, 
    10, 10, 5, 10, 
    5, 1, 1, 5
]

# Adjusted ECDO impact (orders of magnitude for 4-5 previous events)
ecdo_impact = [
    (2,4), (2,4), (3,5), (3,6), (4,6), 
    (5,7), (4,7), (4,6), (4,7), 
    (5,9), (2,3), (5,7), (6,10), 
    (7,10), (7,10), (7,10), (3,6)
]

# Convert temporal range to log scale
log_min = [np.log10(t[0] + 1) for t in temporal_range]  # Avoid log(0) error
log_max = [np.log10(t[1]) for t in temporal_range]

# Convert ECDO impact to relative shift
ecdo_shift = [(min_ / 10, max_ / 10) for min_, max_ in ecdo_impact]

y_pos = np.arange(len(dating_methods))

# Create figure
fig, ax = plt.subplots(figsize=(12, 8))
ax.barh(y_pos, [max_ - min_ for min_, max_ in zip(log_min, log_max)], left=log_min, color='lightblue', edgecolor='black', label='Traditional Range')

# Overlay ECDO impact range
for i, (min_, max_, shift) in enumerate(zip(log_min, log_max, ecdo_shift)):
    ax.plot([min_ + shift[0], max_ + shift[1]], [y_pos[i], y_pos[i]], color='red', linewidth=2, label='ECDO Impact' if i == 0 else "")

# Add accuracy markers with traditional:ECDO ratio
for i, acc in enumerate(estimated_accuracy):
    ratio = (10**log_max[i]) / (10**(log_max[i] - ecdo_impact[i][1]))
    ax.text(log_max[i] + shift[1] + 0.5, y_pos[i], f"±{acc}% ({int(ratio)}:1)", va='center', fontsize=10)

# Labels and formatting
ax.set_yticks(y_pos)
ax.set_yticklabels(dating_methods, ha='right')
ax.set_xlabel("Time Period (Years, Log Scale)")
ax.set_xlim(left=min(log_min)-0.5, right=max(log_max)+1.5)  # Adjust margins
ax.set_xticks([3, 4, 5, 6, 7, 8, 9])
ax.set_xticklabels(["1k", "10k", "100k", "1M", "10M", "100M", "1B"])
ax.set_title("Dating Methods: Temporal Range, Accuracy, and ECDO Impact")
ax.legend()
ax.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.show()
