function [ eps_bar, tSpec, PerTgt ] = targetSpec( agC, Rls, T1, T1dej, nX, M_mean, R_mean, ecSoilCode )
%EPS_BAR Summary of this function goes here
%   Detailed explanation goes here

% if ecSoilCode==1
%     S1=0; % is there shallow alluvium
%     S2=0; % is there deep alluvium
%     coef=4; % 1-fault dist,raw coef; 2-fault dist,smooth coef 3-epicentral dist,raw coef; 4-epicentral dist,smooth coef
% elseif ecSoilCode==2
%     S1=0; % is there shallow alluvium
%     S2=1; % is there deep alluvium
%     coef=4; % 1-fault dist,raw coef; 2-fault dist,smooth coef 3-epicentral dist,raw coef; 4-epicentral dist,smooth coef  
% elseif ecSoilCode==3
%     S1=1; % is there shallow alluvium
%     S2=0; % is there deep alluvium
%     coef=4; % 1-fault dist,raw coef; 2-fault dist,smooth coef 3-epicentral dist,raw coef; 4-epicentral dist,smooth coef  
% else
%     S1=0;% is there shallow alluvium
%     S2=1; % is there deep alluvium
%     coef=4; % 1-fault dist,raw coef; 2-fault dist,smooth coef 3-epicentral dist,raw coef; 4-epicentral dist,smooth coef  
% end

PerTgt_tmp    = linspace(0,4,nX);
% Modify perTgt to include T1
if ~any(PerTgt_tmp == T1)
    PerTgt_tmp2 = zeros(1,size(PerTgt_tmp,2)+1);
    PerTgt_tmp2 = [PerTgt_tmp(PerTgt_tmp<T1) T1 PerTgt_tmp(PerTgt_tmp>T1)];
else
    PerTgt_tmp2 = PerTgt_tmp;
end
% Modify perTgt to include T1dej
if ~any(PerTgt_tmp2 == T1dej)
    PerTgt = zeros(1,size(PerTgt_tmp2,2)+1);
    PerTgt = [PerTgt_tmp2(PerTgt_tmp2<T1dej) T1dej PerTgt_tmp2(PerTgt_tmp2>T1dej)];
else
    PerTgt = PerTgt_tmp2;
end


len = max(abs(size(PerTgt)));
sa    = zeros(1, len);
sigma = zeros(1, len);
rho = zeros(1,len);

for i = 1:len
    %[sa_, sigma_] = SP_1996(PerTgt(i), M_mean, R_mean, S1, S2, coef);
    [koefAtonacijsko]=SP1996hor(2,PerTgt(i),ecSoilCode);
    [temp1,temp2]=AtonacijskoTip1(M_mean,R_mean,koefAtonacijsko);
    sa(1,i)=temp1(1);
    sigma(1,i)=temp2(1);    
end
sa=exp(sa);

indT1=find(PerTgt == T1);
eps_bar_=(log(agC/Rls)-log(sa(1,indT1)))/sigma(1,indT1);
eps_bar = eps_bar_(1,1);

% (Log) Response Spectrum Mean: meanReq
for i = 1:len
    rho(i) = baker_jayaram_correlation(PerTgt(i), T1);
end
tSpec = log(sa) + sigma.*eps_bar.*rho;