# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>

# <codecell>

"""
Created on Thu May 29 11:29:32 2014

@author: chiaracasotto
"""
# Clear existing variables
def clearall():
    all = [var for var in globals() if var[0] != "_"]
    for var in all:
        del globals()[var]
clearall()
import pandas as pd
import numpy as np
import os
import csv
from rmtk.vulnerability.NSP.spo2ida_based.get_spo2ida_parameters import get_spo2ida_parameters
from rmtk.vulnerability.NSP.spo2ida_based.spo2ida_allTfunction import spo2ida_allT
from rmtk.vulnerability.NSP.spo2ida_method import spo2ida
cd = os.getcwd()
pi = 3.141592653589793

# <codecell>

Gamma = [1.292]
T = [1.612]
Tav = [1.612]
Sa_ratios = 1
SPO=[[0.104435862,0.233993594,0.897709787,0.940278828, 2093.9,2093.9,91.43446494]];
dcroof = [[0.046144456, 0.107312842, 0.212491246, 0.473466774, 0.724976013, 0.940278828, 0.940278828]]
EDPlim = [[0.002, 0.005, 0.01, 0.02, 0.04, 0.06, 0.08],dcroof[0]]

with open(cd+'/inputs/EDPvec-RDvec.csv', 'rb') as f:
    reader = csv.reader(f)
    newlist = [row for row in reader]
EDPvec = [np.array([float(ele[0]) for ele in newlist]),[0, 1000]]
RDvec = [np.array([float(ele[1]) for ele in newlist]),[0, 1000]]
noBlg = 1
w = 1
dispersion = [np.repeat(0.,len(dcroof[0])), np.repeat(0.25,len(dcroof[0]))]
g=9.81
Tc=0.5
Td=1.8
mc = 2.24
r = 0.04
MC = 25
data = pd.DataFrame(columns=['Sa50-EdpRd0-beta0','bTSa50-EdpRd0-beta0'],index=np.arange(len(dcroof[0])))

# <codecell>

for k in range(0,len(EDPvec)):
    for j in range(0,len(dispersion)):
        bUthd = [dispersion[j]]*noBlg
        [mc,a,ac,r,mf] = get_spo2ida_parameters(SPO[0], T[0], Gamma[0]) # Convert MDoF into SDoF
        [idacm, idacr] = spo2ida_allT(mc,a,ac,r,mf,T[0],1,0,3,50,2,14) # apply SPO2IDA procedure
        [SaT50,bTSa] = spo2ida(idacm, idacr, mf, T[0], Gamma[0], g, EDPlim[k], dcroof[0], EDPvec[k], RDvec[k],  SPO[0], bUthd[0], MC)
        data['Sa50-EdpRd'+str(k)+'-beta'+str(j)] = pd.DataFrame(SaT50)
        data['bTSa50-EdpRd'+str(k)+'-beta'+str(j)] = pd.DataFrame(bTSa)
        
# <codecell>

data.to_csv('results/SPO2IDA_fragility.csv',header=True,index=True)

# <codecell>

