# -*- coding: utf-8 -*-
"""
Created on Mon Sep 22 16:58:30 2014

@author: chiaracasotto
"""

import numpy as np
from rmtk.vulnerability.NSP.spo2ida_method import spo2ida
from rmtk.vulnerability.NSP.simplified_method import simplified_bilinear
from rmtk.vulnerability.NSP.DF_method import DFfragility
from rmtk.vulnerability.NSP.spo2ida_based.spo2ida_allTfunction import spo2ida_allT
from rmtk.vulnerability.NSP.spo2ida_based.get_spo2ida_parameters import get_spo2ida_parameters
from rmtk.vulnerability.NSP.export_fragility import plot_fragility
pw = 1
filletstyle = 3

def fragility_process(an_type, T, Gamma, w, EDPlim, dcroof, EDPvec, RDvec, SPO, bUthd, noBlg, g, MC, Sa_ratios, plot_feature, N, Tc, Td):
    plotflag, linew, fontsize, units, iml = plot_feature[0:5]
    allSa, allbTSa, allLR50, allbLR = [],[],[],[]     
    for blg in range(0,noBlg):
        # Derive median Sa value (median of Sa) of capacity for each Limit State and corresponding overall dispersion std(log(Sa))
        if an_type==0: # Ruiz-Garcia Miranda's method
            [SaT50, bTSa] = simplified_bilinear(T[blg], Gamma[blg], EDPlim[blg], dcroof[blg], EDPvec[blg], RDvec[blg], SPO[blg], bUthd[blg], g, MC)
        elif an_type==2: # Dolsek and Fajfar's method
            [mc,a,ac,r,mf] = get_spo2ida_parameters(SPO[blg], T[blg], Gamma[blg]) # Convert MDoF into SDoF
            [SaT50,bTSa] = DFfragility(T[blg], Gamma[blg], EDPlim[blg], dcroof[blg], EDPvec[blg], RDvec[blg], SPO[blg], bUthd[blg], mc, r, g, Tc, Td, MC)
        else: # Vamvatsikos and Cornell's method
            [mc,a,ac,r,mf] = get_spo2ida_parameters(SPO[blg], T[blg], Gamma[blg]) # Convert MDoF into SDoF
            [idacm, idacr] = spo2ida_allT(mc,a,ac,r,mf,T[blg],pw,plotflag[1],filletstyle,N,linew,fontsize) # apply SPO2IDA procedure
            [SaT50,bTSa] = spo2ida(idacm, idacr, mf, T[blg], Gamma[blg], g, EDPlim[blg], dcroof[blg], EDPvec[blg], RDvec[blg], SPO[blg], bUthd[blg], MC)
        
        # Converting the Sa(T1) to Sa(Tav), the common IM
        SaTlogmean_av, bTSa_av = np.log(SaT50)*Sa_ratios[blg], np.array(bTSa)*Sa_ratios[blg]
        if blg<20:
            plot_fragility(iml,np.exp(SaTlogmean_av),bTSa_av,0.5,fontsize,units,'off')
        allSa.append(SaTlogmean_av)
        allbTSa.append(bTSa_av)

    # Combine the fragility of each building in a single lognormal curve with
    # mean = weighted_mean(means) and std = SRSS(weighted_std(means),weighted_mean(stds))
    log_meanSa, log_stSa = [],[]
    for i in range(0,len(dcroof[0])):
        SaLS = [ele[i] for ele in allSa]
        StdSaLS = [ele[i] for ele in allbTSa]
        log_meanSa.append(np.average(SaLS,weights = w)) # weighted log-mean mean(log(Sa))
        log_stSa.append(np.sqrt(np.sum(w*(np.power((SaLS-log_meanSa[i]),2)+np.power(StdSaLS,2))))) # weighted log-std (dispersion)
    
    return [log_meanSa, log_stSa]