%% 获取边界1d模型与剩余电导率模型
%
%


function  [boundModel,sigdiff] = get_Boundary1DModel(sig,epsr,mur,elemCen,belemID,elem2node,node2coord,topoZ,plotflag)
%4个方位边界3D模型参数插值得到1D层状模型,得到边界1D模型
%sigdiff = sig - backgroundModel ，backgroundModel = boundModel


% b = ['E' ;'W'; 'N'; 'S' ];
% zNode = [] ;
% for i = 1:4
%     bi    =  b(i) ;
%     Z_sig =[];
%     b_elem2node   = elem2node(belemID.(bi),:);
%     b_elem2node_z = node2coord(b_elem2node(:),3);
%     zNode    = uniquetol(b_elem2node_z,0.005);% 地层顶部面Z坐标,uniquetol减少间隔很小的层界面
%
%     fine_airZ =[];
%     fine_groundZ=[];
%     if  min(b_elem2node_z)< 0
%         fine_airZ    = cumsum( calculate_layers(100,1,abs(min(b_elem2node_z))) )'-topoZ; %空气层
%     elseif max(b_elem2node_z)> 0
%         fine_groundZ    = cumsum( calculate_layers(200,1,abs(max(b_elem2node_z))) )'-topoZ; %地层
%     else
%         error 'no finded boundry'
%     end
%     zNode    = unique([ zNode; topoZ; -fine_airZ; fine_groundZ]);%在地层和空气界面加密
%
%     nlay     = length(zNode);
%     lay_centerZ  = zNode(1:nlay-1)+0.5*diff(zNode) ; % 层中心Z坐标
%     Z_sig(:,1)   = belem_CenCoord.(bi)(:,3) ;    % 获取E东边界附近区域的四面体中心的Z坐标
%     Z_sig(:,2)   = sig(belemID.(bi),1) ;         % 获取相应的电导率参数
%     coordZ_sig_sort   = unique(Z_sig,"rows") ;   % 为使用griddedInterpolant插值函数，对coordZ-sig排序
%     F.sig    = griddedInterpolant(coordZ_sig_sort(:,1),coordZ_sig_sort(:,2),'nearest') ;%定义电导率插值函数
%     sig1D    = F.sig(lay_centerZ) ;           % 获取层中心电导率，代替层电导率
% end



Z_sig =[];
belemIDEWNS   = [belemID.E;belemID.W;belemID.N;belemID.S];

b_elem2node   = elem2node(belemIDEWNS,:);
b_elem2node_z = node2coord(b_elem2node(:),3);
zNode         = uniquetol(b_elem2node_z,0.005);% 地层顶部面Z坐标,uniquetol减少间隔很小的层界面

fine_airZ     =[];
fine_groundZ  =[];
if  min(b_elem2node_z)< 0
    minz      = abs(min(b_elem2node_z));
    fine_airZ = cumsum( calculate_layers(50,minz/500,minz) )'-topoZ; %空气层
elseif max(b_elem2node_z)> 0
    maxz            = abs(max(b_elem2node_z));
    fine_groundZ    = cumsum( calculate_layers(50, maxz/500, maxz) )'-topoZ; %地层
else
    error 'no finded boundry'
end
zNode    = unique([ zNode; topoZ; -fine_airZ; fine_groundZ]);%在地层和空气界面加密

nlay     = length(zNode);
lay_centerZ  = zNode(1:nlay-1)+0.5*diff(zNode) ; % 层中心Z坐标
Nsigcomp     = size(sig,2);
Z_sig(:,1)   = elemCen(belemIDEWNS,3);% 获取四个边界附近区域的四面体中心的Z坐标
[~,ia,~]     = unique(Z_sig(:,1),"rows");

Z_sig(:,2:1+Nsigcomp) = sig(belemIDEWNS,:) ;         % 获取相应的电导率参数
Z_sig_sort            = Z_sig(ia,:) ;   % 为使用griddedInterpolant插值函数，对coordZ-sig排序
F.sig  = griddedInterpolant(Z_sig_sort(:,1),Z_sig_sort(:,2:1+Nsigcomp),'nearest') ;%定义电导率插值函数
sig1D  = F.sig(lay_centerZ) ;           % 获取层中心电导率，代替层电导率

Nepsrcomp     = size(epsr,2);
Z_epsr(:,1)   = elemCen(belemIDEWNS,3);% 获取四个边界附近区域的四面体中心的Z坐标
Z_epsr(:,2:1+Nepsrcomp)   = epsr(belemIDEWNS,:) ;         % 获取相应的电导率参数
Z_epsr_sort   = Z_epsr(ia,:)  ;   % 为使用griddedInterpolant插值函数，对coordZ-sig排序
F.epsr  = griddedInterpolant(Z_epsr_sort(:,1),Z_epsr_sort(:,2:1+Nepsrcomp),'nearest') ;%定义电导率插值函数
epsr1D  = F.epsr(lay_centerZ) ;           % 获取层中心电导率，代替层电导率

Nmurcomp     = size(mur,2);
Z_mur(:,1)   = elemCen(belemIDEWNS,3);% 获取四个边界附近区域的四面体中心的Z坐标
Z_mur(:,2:1+Nmurcomp )   = mur(belemIDEWNS,:) ;         % 获取相应的电导率参数
Z_mur_sort   = Z_mur(ia,:) ;   % 为使用griddedInterpolant插值函数，对coordZ-sig排序
F.mur  = griddedInterpolant(Z_mur_sort(:,1),Z_mur_sort(:,2:1+Nmurcomp ),'nearest') ;%定义电导率插值函数
mur1D  = F.mur(lay_centerZ) ;           % 获取层中心电导率，代替层电导率

boundModel.zNode  = zNode ;
boundModel.sig1D  = sig1D ;
boundModel.epsr1D = epsr1D ;
boundModel.mur1D  = mur1D ;

backgroundModel = F.sig(elemCen(:,3)) ;
sigdiff         = sig - backgroundModel ;

if plotflag
    figure(100)
    subplot(1,3,1)
    col =jet(6);
    for i=1:6
        hold on
        stairs( [sig1D(1,i);sig1D(:,i)],zNode./1000,'Color',col(i,:),"LineWidth",2)
        title('Conductivity')
        ylabel('Z(km)') ;xlabel('sigma(S/m)');
        set(gca,'YDir','reverse','Xscale','log')
        axis tight
        box on
        l{i} = ['Sig ' num2str(i)] ;
    end
    legend(l,'FontSize',6, Box="off");

    subplot(1,3,2)
    for i=1:6
        hold on
        stairs( [epsr1D(1,i);epsr1D(:,i)],zNode./1000,'Color',col(i,:),"LineWidth",2)
        title('Relative permittivity')
        xlabel('Epsilonr');
        set(gca,'YDir','reverse')
        axis tight
        box on
        legend
        l{i} = ['Epsr ' num2str(i)] ;
    end
    legend(l,'FontSize',6,Box="off");

    subplot(1,3,3)
    stairs( [mur1D(1,1);mur1D(:,1)],zNode./1000,'Color',col(1,:),"LineWidth",2)
    title('Relative permeability')
    xlabel('Mur');
    set(gca,'YDir','reverse')
    axis tight
    box on

end
end
