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



ASME_katal_300_Lbs = ['ASME_katal_300_Lbs.csv']


with open(ASME_katal_300_Lbs[-1],'rb') as csvsoubor:
    cteni_radku = csv.reader(csvsoubor, delimiter=';')
    pocet_radku_ASME_300 = 0
    for radky in cteni_radku:
        pocet_radku_ASME_300 += 1
 
with open(ASME_katal_300_Lbs[-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_B = 'B'
nazev_osy_ST = 'ST'
nazev_osy_SR = 'SR'
nazev_osy_SH = 'SH'
nazev_osy_Sf = 'Sf'
nazev_osy_Sn = 'Sn'

nazev_osy_H = 'H'
nazev_osy_HT = 'HT'
nazev_osy_HG = 'HG'
nazev_osy_W = 'W_montaz'
nazev_osy_HD = 'HD'

nazev_osy_material = 'material_priruby'

sloupec_promenna_B = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_B:
        break
    else:
        sloupec_promenna_B += 1

sloupec_promenna_ST = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_ST:
        break
    else:
        sloupec_promenna_ST += 1

sloupec_promenna_SR = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_SR:
        break
    else:
        sloupec_promenna_SR += 1

sloupec_promenna_SH = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_SH:
        break
    else:
        sloupec_promenna_SH += 1

sloupec_promenna_Sf = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_Sf:
        break
    else:
        sloupec_promenna_Sf += 1

sloupec_promenna_Sn = 0
for promenne in nazvy_promennych:
    if promenne == nazev_osy_Sn:
        break
    else:
        sloupec_promenna_Sn += 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

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

hodnoty_B_ASME_300 = []
hodnoty_ST_ASME_300 = []
hodnoty_SR_ASME_300 = []
hodnoty_SH_ASME_300 = []
hodnoty_Sf_ASME_300 = []
hodnoty_Sn_ASME_300 = []
hodnoty_H_ASME_300 = []
hodnoty_HT_ASME_300 = []
hodnoty_HG_ASME_300 = []
hodnoty_W_ASME_300 = []
hodnoty_HD_ASME_300 = []
text_materialu = []

with open(ASME_katal_300_Lbs[-1],'rb') as csvsoubor:
    cteni_radku = csv.reader(csvsoubor, delimiter=';')
    for radky in cteni_radku:
        if radky[0] == 'Temp':
            pass
        elif radky[1]== 'stC':
            pass
        else:
            hodnoty_B_ASME_300.append(float(radky[sloupec_promenna_B]))
            try:
                hodnoty_ST_ASME_300.append(float(radky[sloupec_promenna_ST]))
            except:
                hodnoty_ST_ASME_300.append(float(0.0))
            hodnoty_SR_ASME_300.append(float(radky[sloupec_promenna_SR]))
            hodnoty_SH_ASME_300.append(float(radky[sloupec_promenna_SH]))
            hodnoty_Sf_ASME_300.append(float(radky[sloupec_promenna_Sf]))
            hodnoty_Sn_ASME_300.append(float(radky[sloupec_promenna_Sn]))
            hodnoty_H_ASME_300.append(float(radky[sloupec_promenna_H])/1000)
            hodnoty_HT_ASME_300.append(float(radky[sloupec_promenna_HT])/1000)
            hodnoty_HG_ASME_300.append(float(radky[sloupec_promenna_HG])/1000)
            hodnoty_W_ASME_300.append(float(radky[sloupec_promenna_W])/1000)
            hodnoty_HD_ASME_300.append(float(radky[sloupec_promenna_HD])/1000)
            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_ASME_300 = []
sideP_eq1_ASME_300 = []
sideL_eq2_ASME_300 = []
sideP_eq2_ASME_300 = []
sideL_eq3_ASME_300 = []
sideP_eq3_ASME_300 = []
sideL_eq4_ASME_300 = []
sideP_eq4_ASME_300 = []
sideL_eq5_ASME_300 = []
sideP_eq5_ASME_300 = []
sideL_eq6_ASME_300 = []
sideP_eq6_ASME_300 = []

for i in xrange(0,len(hodnoty_SR_ASME_300[2:-1])):
    sideL_eq1_ASME_300.append(hodnoty_SH_ASME_300[i])
    sideP_eq1_ASME_300.append(hodnoty_Sf_ASME_300[i])
    sideL_eq2_ASME_300.append(hodnoty_SH_ASME_300[i])
    sideP_eq2_ASME_300.append(min(1.5*hodnoty_Sf_ASME_300[i],2.5*hodnoty_Sn_ASME_300[i]))
    sideL_eq3_ASME_300.append(hodnoty_SR_ASME_300[i])
    sideP_eq3_ASME_300.append(hodnoty_Sf_ASME_300[i])
    sideL_eq4_ASME_300.append(hodnoty_ST_ASME_300[i])
    sideP_eq4_ASME_300.append(hodnoty_Sf_ASME_300[i])
    sideL_eq5_ASME_300.append((hodnoty_SH_ASME_300[i]+hodnoty_SR_ASME_300[i])/2)
    sideP_eq5_ASME_300.append(hodnoty_Sf_ASME_300[i])
    sideL_eq6_ASME_300.append((hodnoty_SH_ASME_300[i]+hodnoty_ST_ASME_300[i])/2)
    sideP_eq6_ASME_300.append(hodnoty_Sf_ASME_300[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_B_ASME_300[2:-1], sideL_eq1_ASME_300, 'o:', label='$S_H\leq S_f$')
ax.plot(hodnoty_B_ASME_300[2:-1], sideL_eq2_ASME_300, 'o:', label='$S_H\leq min(1.5*S_f,2.5*S_n)$')
ax.plot(hodnoty_B_ASME_300[2:-1], sideL_eq3_ASME_300, 'o:', label='$S_R\leq S_f$')
ax.plot(hodnoty_B_ASME_300[2:-1], sideL_eq4_ASME_300, 'o:', label='$S_T\leq S_f$')
ax.plot(hodnoty_B_ASME_300[2:-1], sideL_eq5_ASME_300, 'o:', label='$0.5*(S_H + S_R) \leq S_f$')
ax.plot(hodnoty_B_ASME_300[2:-1], sideL_eq6_ASME_300, 'o:', label='$0.5*(S_H + S_T) \leq S_f$')

ax1.plot(hodnoty_B_ASME_300[2:-1], hodnoty_H_ASME_300[2:-1], '-', label='$H$')
ax1.plot(hodnoty_B_ASME_300[2:-1], hodnoty_HT_ASME_300[2:-1], '-', label='$H_T$')
ax1.plot(hodnoty_B_ASME_300[2:-1], hodnoty_HG_ASME_300[2:-1], '-', label='$H_G$')
ax1.plot(hodnoty_B_ASME_300[2:-1], hodnoty_W_ASME_300[2:-1], '-', label='$W$')
ax1.plot(hodnoty_B_ASME_300[2:-1], hodnoty_HD_ASME_300[2:-1], '-', label='$H_D$')

ax.plot(hodnoty_B_ASME_300[2:-1], sideP_eq1_ASME_300, '--', label='$S_f$', 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.text(25, sideP_eq1_ASME_300[0]+10, '$Sf = f(material$ $priruby - '+str(text_materialu[0])+')$', color = 'purple')
##ax.set_xlim([])
minimum = min(min(sideL_eq1_ASME_300),min(sideL_eq2_ASME_300),min(sideL_eq3_ASME_300),min(sideL_eq4_ASME_300))
maximum = max(max(sideL_eq1_ASME_300),max(sideL_eq2_ASME_300),max(sideL_eq3_ASME_300),max(sideL_eq4_ASME_300))
##ax.set_ylim([minimum-50,maximum+50])

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

