
# -*- coding: cp1250 -*-
import csv
import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy



merkblatt_katal_pn16 = ['merkblatt_katal_pn16.csv']


with open(merkblatt_katal_pn16[-1],'rb') as csvsoubor:
    cteni_radku = csv.reader(csvsoubor, delimiter=';')
    pocet_radku_katal_pn6 = 0
    for radky in cteni_radku:
        pocet_radku_katal_pn6 += 1

with open(merkblatt_katal_pn16[-1],'rb') as csvsoubor:
    cteni_radku = csv.reader(csvsoubor, delimiter=';')
    _cteni_radku = list(cteni_radku)
    nazvy_promennych = _cteni_radku[0]
    nazvy_jednotek = _cteni_radku[1]

nazev_osy_x = 'di'
nazev_osy_y = 'hF_provozAA'
nazev_osy_y1 = 'hF_montazAA'
nazev_osy_y2 = 'hF_provozBB'
nazev_osy_y3 = 'hF_montazBB'
nazev_osy_y4 = 'sF'


sloupec_promenna_x = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_x:
        break
    else:
        sloupec_promenna_x += 1

sloupec_promenna_y = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_y:
        break
    else:
        sloupec_promenna_y += 1

sloupec_promenna_y1 = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_y1:
        break
    else:
        sloupec_promenna_y1 += 1
        
sloupec_promenna_y2 = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_y2:
        break
    else:
        sloupec_promenna_y2 += 1

sloupec_promenna_y3 = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_y3:
        break
    else:
        sloupec_promenna_y3 += 1

sloupec_promenna_y3= 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_y3:
        break
    else:
        sloupec_promenna_y3 += 1

sloupec_promenna_y4 = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_y4:
        break
    else:
        sloupec_promenna_y4 += 1


hodnoty_x_katal_pn6 = []
hodnoty_y_katal_pn6 = []
hodnoty_y1_katal_pn6 = []
hodnoty_y2_katal_pn6 = []
hodnoty_y3_katal_pn6 = []
hodnoty_y4_katal_pn6 = []

with open(merkblatt_katal_pn16[-1],'rb') as csvsoubor:
    cteni_radku = csv.reader(csvsoubor, delimiter=';')
    for radky in cteni_radku:
            hodnoty_x_katal_pn6.append(float(radky[sloupec_promenna_x]))
            hodnoty_y_katal_pn6.append(float(radky[sloupec_promenna_y]))
            hodnoty_y1_katal_pn6.append(float(radky[sloupec_promenna_y1]))
            hodnoty_y2_katal_pn6.append(float(radky[sloupec_promenna_y2]))
            hodnoty_y3_katal_pn6.append(float(radky[sloupec_promenna_y3]))
            hodnoty_y4_katal_pn6.append(float(radky[sloupec_promenna_y4]))
        

sideL_eq1_volna = []
sideP_eq1_volna = []
sideL_eq2_volna = []
sideP_eq2_volna = []
sideL_eq3_volna = []
sideP_eq3_volna = []
sideL_eq4_volna = []
sideP_eq4_volna = []


for i in xrange(0,len(hodnoty_x_katal_pn6)):
    sideL_eq1_volna.append(hodnoty_y_katal_pn6[i]/3)
    sideP_eq1_volna.append(hodnoty_y4_katal_pn6[i])
    sideL_eq2_volna.append(hodnoty_y1_katal_pn6[i]/3)
    sideP_eq2_volna.append(hodnoty_y4_katal_pn6[i])
    sideL_eq3_volna.append(hodnoty_y2_katal_pn6[i]/3)
    sideP_eq3_volna.append(hodnoty_y4_katal_pn6[i])
    sideL_eq4_volna.append(hodnoty_y3_katal_pn6[i]/3)
    sideP_eq4_volna.append(hodnoty_y4_katal_pn6[i])
    
##x = map(float, hodnoty_x_katal_pn6[2:-1]) # vytvoreni rady pro osu x + prevod seznamu s typem string na float
##y = map(float, hodnoty_y_katal_pn6[2:-1]) # vytvoreni rady pro osu y + prevod seznamu s typem string na float
##fit_pn6 = np.polyfit(x,y,1) # vytvoreni fitu
##fit_fn_pn6 = np.poly1d(fit_pn6) # vytvoreni regrese

##x = map(float, hodnoty_x_katal_pn10[2:-1]) # vytvoreni rady pro osu x + prevod seznamu s typem string na float
##y = map(float, hodnoty_y_katal_pn10[2:-1]) # vytvoreni rady pro osu y + prevod seznamu s typem string na float
##fit_pn10 = np.polyfit(x,y,1) # vytvoreni fitu
##fit_fn_pn10 = np.poly1d(fit_pn10) # vytvoreni regrese

##fig, ax = plt.subplots()
##ax.plot(hodnoty_x_katal_pn6[2:-1], hodnoty_y_katal_pn6[2:-1], '-b')
####ax.plot(x,y, 'yo', x, fit_fn_pn6(x), '-k')
##ax.plot(hodnoty_x_katal_pn10[2:-1], hodnoty_y_katal_pn10[2:-1], '-r')
####ax.plot(x,y, 'yo', x, fit_fn_pn10(x), '-k')
##plt.xlabel(hodnoty_x_katal_pn10[0]+' ['+(hodnoty_x_katal_pn10[1])+']')
##plt.ylabel(hodnoty_y_katal_pn10[0]+' ['+(hodnoty_y_katal_pn10[1])+']')
##plt.xlabel(hodnoty_x_katal_pn6[0]+' [mm]')
##plt.ylabel(hodnoty_y_katal_pn6[0]+' [MPa]')
##plt.title('Zavislost '+nazev_osy_x+' na '+nazev_osy_y)
##plt.legend(('PN6', 'PN10', ),loc='upper left', shadow=True)
##plt.savefig('zavislost_'+nazev_osy_x+'_na_'+nazev_osy_y+'.png',dpi=300)
##plt.show()


fig, ax = plt.subplots(figsize=(15,8))
ax1 = ax.twinx()
ax.plot(hodnoty_x_katal_pn6[2:-1], sideL_eq1_volna, 'o:', label='$k*\sigma_\Theta \leq f$ $-$ $provoz$')
ax.plot(hodnoty_x_katal_pn6[2:-1], sideL_eq2_volna, 'o:', label='$0.5*k*(\sigma_H + \sigma_\Theta) \leq f$ $-$ $provoz$')
ax.plot(hodnoty_x_katal_pn6[2:-1], sideL_eq3_volna, 'o:', label='$k*\sigma_\Theta \leq f$ $-$ $montaz$')
ax.plot(hodnoty_x_katal_pn6[2:-1], sideL_eq4_volna, 'o:', label='$0.5*k*(\sigma_H + \sigma_\Theta) \leq f$ $-$ $montaz$')
ax.plot(hodnoty_x_katal_pn6[2:-1], hodnoty_y4_katal_pn6, '--', label='$f_d$', color = 'purple')
ax.grid()

##ax.fill_between(hodnoty_x_katal_pn6[2:-1], sideP_eq1_volna,-100, label='f', color='blue', alpha=0.5)
##ax.set_xlim([])
minimum = min(min(sideL_eq1_volna),min(sideL_eq2_volna),min(sideL_eq3_volna),min(sideL_eq4_volna))
maximum = max(max(sideL_eq1_volna),max(sideL_eq2_volna),max(sideL_eq3_volna),max(sideL_eq4_volna))
##ax.set_ylim([minimum-50,maximum+50])

ax.set_xlabel('$B [mm]$')
ax.set_ylabel('$\sigma_\Theta , f_d $ $[MPa]$')
ax1.set_ylabel('$H,$ $H_T,$ $H_G,$ $W,$ $H_D$ $[kN]$')
##ax.set_title('Zavislost '+nazev_osy_x+' na '+nazev_osy_y)
ax.legend(loc='upper left', shadow=False)
ax1.legend(loc='lower right', shadow=False)
plt.savefig('zavislost_'+nazev_osy_x+'_na_'+nazev_osy_y+'.svg')
plt.savefig('zavislost_'+nazev_osy_x+'_na_'+nazev_osy_y+'.png',dpi=300)
plt.show()
