import matplotlib.pyplot as plt
import numpy as np


with open('eclipse.txt', 'r') as f:
    lines = f.read().split('\n')

dates = []
# Pre jednoduchsie vyobrazenie nahradime slovne typy zatmeni cislami:
# P = polotienove (1), U = uplne (2), C = ciastocne (3)
eclipse_numbers = []
for line in lines:
    line_list = line.split()
    year = line_list[0]
    month = line_list[1]
    eclipe_type = line_list[2]
    dates.append(int(year) + (int(month) - 1)/12)
    if eclipe_type == 'P':
        eclipse_numbers.append(1)
    elif eclipe_type == 'Ú':
        eclipse_numbers.append(2)
    elif eclipe_type == 'Č':
        eclipse_numbers.append(3)

plt.figure(figsize=(9, 3))
plt.plot(dates, eclipse_numbers, 'o')
plt.xlabel('cas [rok]')
plt.ylabel('typ zatmenia')
plt.yticks([1, 2, 3], ['P', 'U', 'C'])
plt.show()

# Z grafu vidno, ze periodicita sa ukazuje u ciastocnych zatmeni.
# Vyberieme zaciatky prveho a posleneho cyklu, ktore maju hodnoty cca 1912
# a 1988 a vydelenim poctom cyklov dostaneme periodu sarosu.
time1 = [dates[i] for i in range(len(dates)) if (dates[i] > 1910 and
         (eclipse_numbers[i] == 3))][0]
time2 = [dates[i] for i in range(len(dates)) if (dates[i] > 1985 and
         (eclipse_numbers[i] == 3))][0]

saros = (time2 - time1)/4
print('Vypocitana perioda sarosu je {} rokov.'.format(np.round(saros, 2)))
print('Skutocna perioda sarosu je 18.03 roka.')