"""
Solar PV device reliability: evidence and access
================================================

Review 14 sources and load 371 PV-specific records offline. Counts, rates,
availability, survival and sensor drift remain separate. Existing Sandia PVROM
fits are available through the original failure-database connector.

See the `PV reliability research <../../_generated/guides/pv-device-reliability.html>`_.
"""

import matplotlib.pyplot as plt

from energy_data_retrieval.pv_reliability_database import (
    load_pv_reliability_evidence,
    pv_reliability_sources,
)

data = load_pv_reliability_evidence()
print(pv_reliability_sources()[["source_id", "access"]].to_string(index=False))
print(
    data[data.device.str.contains("combiner|monitoring|tracker", case=False)][
        ["source_id", "device", "metric", "unit"]
    ]
    .drop_duplicates()
    .to_string(index=False)
)

counts = data.groupby("metric").size().sort_values()
ax = counts.plot.barh(figsize=(10, 5), color="#117b88")
ax.set(title="371 PV evidence records: metric type matters", xlabel="Records", ylabel="")
for index, count in enumerate(counts):
    ax.text(count + 1, index, str(count), va="center")
plt.tight_layout()
plt.show()
