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




nazev_souboru_katalog_pn10 = ['volna_metoda_katal_pn10.csv']

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


with open(nazev_souboru_katalog_pn10[-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 = 'B'
nazev_osy_y = 'sigmaTHETA_provoz'
nazev_osy_y1 = 'sigmaTHETA_montaz'
nazev_osy_H = 'H'
nazev_osy_HT = 'HT'
nazev_osy_HG = 'HG'
nazev_osy_W = 'W'
nazev_osy_HD = 'HD'
nazev_osy_f = 'f_priruba'

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_H = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_H:
        break
    else:
        sloupec_promenna_H += 1

sloupec_promenna_HT = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_HT:
        break
    else:
        sloupec_promenna_HT += 1

sloupec_promenna_HG = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_HG:
        break
    else:
        sloupec_promenna_HG += 1

sloupec_promenna_W = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_W:
        break
    else:
        sloupec_promenna_W += 1

sloupec_promenna_HD = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_HD:
        break
    else:
        sloupec_promenna_HD += 1
                
sloupec_promenna_f = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_f:
        break
    else:
        sloupec_promenna_f += 1

material_priruba = 0
for promenne in nazvy_promennych:
    if promenne == 'oznaceni_priruby':
        break
    else:
        material_priruba += 1

hodnoty_x_katal_pn10 = []
hodnoty_y_katal_pn10 = []
hodnoty_y1_katal_pn10 = []
hodnoty_H_katal_pn10 = []
hodnoty_HT_katal_pn10 = []
hodnoty_HG_katal_pn10 = []
hodnoty_W_katal_pn10 = []
hodnoty_HD_katal_pn10 = []
hodnoty_f_katal_pn10 = []
text_materialu = []
with open(nazev_souboru_katalog_pn10[-1],'rb') as csvsoubor:
    cteni_radku = csv.reader(csvsoubor, delimiter=';')
    for radky in cteni_radku:
        if radky[0] == 'Temp':
            pass
        elif radky[1]== '\xb0C':
            pass
        else:
            hodnoty_x_katal_pn10.append(float(radky[sloupec_promenna_x]))
            hodnoty_y_katal_pn10.append(float(radky[sloupec_promenna_y]))
            hodnoty_y1_katal_pn10.append(float(radky[sloupec_promenna_y1]))
            hodnoty_H_katal_pn10.append(float(radky[sloupec_promenna_H])/1000)
            hodnoty_HT_katal_pn10.append(float(radky[sloupec_promenna_HT])/1000)
            hodnoty_HG_katal_pn10.append(float(radky[sloupec_promenna_HG])/1000)
            hodnoty_W_katal_pn10.append(float(radky[sloupec_promenna_W])/1000)
            hodnoty_HD_katal_pn10.append(float(radky[sloupec_promenna_HD])/1000)
            hodnoty_f_katal_pn10.append(float(radky[sloupec_promenna_f]))
            text_materialu.append(radky[material_priruba])
        
##hodnoty_x_katal_pn10 = []
##hodnoty_y_katal_pn10 = []
##hodnoty_y1_katal_pn10 = []
##hodnoty_f_katal_pn10 = []
##with open(nazev_souboru_katalog_pn10[-1],'rb') as csvsoubor:
##    cteni_radku = csv.reader(csvsoubor, delimiter=';')
##    for radky in cteni_radku:
##        if radky[0] == 'Temp':
##            pass
##        elif radky[0]== '\xb0C':
##            pass
##        else:
##            hodnoty_x_katal_pn10.append(float(radky[sloupec_promenna_x]))
##            hodnoty_y_katal_pn10.append(float(radky[sloupec_promenna_y]))
##            hodnoty_y1_katal_pn10.append(float(radky[sloupec_promenna_y1]))
##            hodnoty_f_katal_pn10.append(float(radky[sloupec_promenna_f]))
##            text_materialu.append(radky[material_priruba])
##
            
# stress limits
sideL_eq1_volna = []
sideP_eq1_volna = []
sideL_eq2_volna = []
sideP_eq2_volna = []
sideL_eq3_volna = []
sideP_eq3_volna = []
sideL_eq4_volna = []
sideP_eq4_volna = []

k_list = []
for B in hodnoty_x_katal_pn10[2:-1]:
    if B <= 1000:
        k = 1.
    elif B >= 2000:
        k = 1.333
    else:
        k = 2*(1+B/2000)/3
    k_list.append(k)

for i in xrange(0,len(k_list)):
    sideL_eq1_volna.append(k_list[i]*hodnoty_y_katal_pn10[i])
    sideP_eq1_volna.append(hodnoty_f_katal_pn10[i])
    sideL_eq2_volna.append(0.5*k_list[i]*(hodnoty_y_katal_pn10[i]))
    sideP_eq2_volna.append(hodnoty_f_katal_pn10[i])
    sideL_eq3_volna.append(k_list[i]*hodnoty_y1_katal_pn10[i])
    sideP_eq3_volna.append(hodnoty_f_katal_pn10[i])
    sideL_eq4_volna.append(0.5*k_list[i]*(hodnoty_y1_katal_pn10[i]))
    sideP_eq4_volna.append(hodnoty_f_katal_pn10[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_pn10[2:-1], sideL_eq1_volna, 'o:', label='$k*\sigma_\Theta \leq f$ $-$ $provoz$')
ax.plot(hodnoty_x_katal_pn10[2:-1], sideL_eq2_volna, 'o:', label='$0.5*k*(\sigma_H + \sigma_\Theta) \leq f$ $-$ $provoz$')
ax.plot(hodnoty_x_katal_pn10[2:-1], sideL_eq3_volna, 'o:', label='$k*\sigma_\Theta \leq f$ $-$ $montaz$')
ax.plot(hodnoty_x_katal_pn10[2:-1], sideL_eq4_volna, 'o:', label='$0.5*k*(\sigma_H + \sigma_\Theta) \leq f$ $-$ $montaz$')
ax1.plot(hodnoty_x_katal_pn10[2:-1], hodnoty_H_katal_pn10[2:-1], '-', label='$H$')
ax1.plot(hodnoty_x_katal_pn10[2:-1], hodnoty_HT_katal_pn10[2:-1], '-', label='$H_T$')
ax1.plot(hodnoty_x_katal_pn10[2:-1], hodnoty_HG_katal_pn10[2:-1], '-', label='$H_G$')
ax1.plot(hodnoty_x_katal_pn10[2:-1], hodnoty_W_katal_pn10[2:-1], '-', label='$W$')
ax1.plot(hodnoty_x_katal_pn10[2:-1], hodnoty_HD_katal_pn10[2:-1], '-', label='$H_D$')
ax.plot(hodnoty_x_katal_pn10[2:-1], sideP_eq1_volna, '--', label='$f_d$', color = 'purple')
ax.grid()

##ax.fill_between(hodnoty_x_katal_pn10[2:-1], sideP_eq1_volna,-100, label='f', color='blue', alpha=0.5)
ax.text(480, sideP_eq1_volna[0]+10, '$f_d = f(material$ $priruby - '+str(text_materialu[0])+')$', color = 'purple', fontsize=18)
##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]$', fontsize=20)
ax.set_ylabel('$\sigma_\Theta , f_d $ $[MPa]$', fontsize=20)
ax1.set_ylabel('$H,$ $H_T,$ $H_G,$ $W,$ $H_D$ $[kN]$', fontsize=20)
##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.title('CSN EN 13445 - Volna metoda (PN10)', fontsize=20)
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()
