%% MT1D正演计算边界电场
%% MT 1D Forward modeling of Boundary Electric Field
%
% This module uses the 1D MT analytical solution for an anisotropic layered model to approximate the 3D MT forward modeling at the boundaries.
% In the case of layered anisotropy:
% E1 polarization mode calculates the electric field at the E, W, and B boundaries, with zero electric field at the N and S boundaries.
% E2 polarization mode calculates the electric field at the N, S, and B boundaries, with zero electric field at the E and W boundaries.
% beField: Electric field on the six boundary edges.
% {\mathbf{E}}_b =\mathit{\mathbf{e}}∙\mathbf{E}\left(\bar{x} ,\bar{y} ,\bar{z} \right)
% TE模式只计算E、W、B边界，在N、S边界上电场为零
% TM模式只计算N、S、B边界，在E、W边界上电场为零
% beField：6个边界边上的电场
%
%


function  [beField] = get_BoundaryFieldAniEpsMu(freq,boundModel,bedge_CenCoord,bedge_eunit,plotflag,col)
%% MT 1D 解析解插值到边界棱边中心
%% Interpolation of the electric field analytical solution to the boundary edge center
sig1D  = boundModel.sig1D ;
epsr1D = boundModel.epsr1D ;
mur1D  = boundModel.mur1D ;
zNode  = boundModel.zNode ;

if size(sig1D,2)==1
    sig1D(:,2:3) = [sig1D(:,1) sig1D(:,1)];
    sig1D(:,4:6) = 0;
elseif  size(sig1D,2)==3
    sig1D(:,4:6) = 0;
end

if size(epsr1D,2)==1
    epsr1D(:,2:3) = [epsr1D(:,1) epsr1D(:,1)];
    epsr1D(:,4:6) = 0;
elseif size(epsr1D,2)==3
    epsr1D(:,4:6) = 0;
end

[EFx1,EFy1,EFz1,HFx1,HFy1]=mt1DAniAnalyticFieldEspMu(freq,sig1D,epsr1D,mur1D,zNode',[1; 0]);
[EFx2,EFy2,EFz2,HFx2,HFy2]=mt1DAniAnalyticFieldEspMu(freq,sig1D,epsr1D,mur1D,zNode',[0; 1]);

% [eField,hField] = mt1DAnalyticFields(freq,sig1D,zNode,1,'E', 1);%MT 1D 正演
EFx1(isnan(EFx1))=0;EFy1(isnan(EFy1))=0;EFz1(isnan(EFz1))=0;HFx1(isnan(HFx1))=0;HFy1(isnan(HFy1))=0;
EFx2(isnan(EFx2))=0;EFy2(isnan(EFy2))=0;EFz2(isnan(EFz2))=0;HFx2(isnan(HFx2))=0;HFy2(isnan(HFy2))=0;
EFx1(1:2,1)=1;
EFy2(1:2,1)=1;

Ffield1  = griddedInterpolant(zNode,[EFx1,EFy1,EFz1]);    %定义插值函数 interpolation function
Ffield2  = griddedInterpolant(zNode,[EFx2,EFy2,EFz2]);
beField.Ffield1=Ffield1;
beField.Ffield2=Ffield2;

b = ['E' ;'W'; 'N'; 'S' ];
for i = 1:4
    bi    =  b(i) ;
    z = bedge_CenCoord.(bi)(:,3) ;
    % Interpolation of the electric field analytical solution to the boundary edge center
    eF_edCentE1.(bi)   = Ffield1(z);   % E1(1,0,0)
    eF_edCentE2.(bi)   = Ffield2(z);   % E2(0,1,0)
end
eF_edCentE1.all =  Ffield1(bedge_CenCoord.all(:,3));
eF_edCentE2.all =  Ffield2(bedge_CenCoord.all(:,3));

maxzid   = find(z == max(z));         % Bottom boundary
eF_edCentE1.B = eF_edCentE1.(bi)(maxzid(1),:);
eF_edCentE2.B = eF_edCentE2.(bi)(maxzid(1),:);


beField.EFx1 = EFx1 ;%save
beField.EFy1 = EFy1 ;
beField.HFx1 = HFx1 ;
beField.HFy1 = HFy1 ;
beField.EFx2 = EFx2 ;
beField.EFy2 = EFy2 ;
beField.HFx2 = HFx2 ;
beField.HFy2 = HFy2 ;

beField.E1all = sum(eF_edCentE1.all.*bedge_eunit.all,2);
beField.E2all = sum(eF_edCentE2.all.*bedge_eunit.all,2);

% （1,0,0） 模式 Ey=0;Ez=0
% T: Top boundary
beField.TE.T      = sum([1 0 0].*bedge_eunit.T,2);
% B: Bottom boundary
beField.TE.B      = sum(eF_edCentE1.B.*bedge_eunit.B,2);
% E: East boundary
etemp             = zeros(size(eF_edCentE1.E,1),3) ;
etemp(:,1:3)      = eF_edCentE1.E;% Ex
beField.TE.E      = sum(etemp.*bedge_eunit.E,2);
% W: West boundary.
etemp             = zeros(size(eF_edCentE1.W,1),3) ;
etemp(:,1:3)        = eF_edCentE1.W;% Ex
beField.TE.W      = sum(etemp.*bedge_eunit.W,2);
% N S

% E1(0,1,0) 模式Ex=0;Ez=0
%T: Top boundary
beField.TM.T      = sum([0 1 0].*bedge_eunit.T,2);
% B: Bottom boundary
beField.TM.B      = sum(eF_edCentE2.B.*bedge_eunit.B,2);
% N: North boundar
etemp             = zeros(size(eF_edCentE2.N,1),3) ;
etemp(:,1:3)        = eF_edCentE2.N;%Ey
beField.TM.N      = sum(etemp.*bedge_eunit.N,2);
% S: South boundary
etemp             = zeros(size(eF_edCentE2.S,1),3) ;
etemp(:,1:3)        = eF_edCentE2.S;%Ey
beField.TM.S      = sum(etemp.*bedge_eunit.S,2);

% timeMT1D = toc(timeMT1DStart);


if plotflag
    comp=EFx1;
    figure(3)
    subplot(1,3,1);hold on;
    % scatter(real(eF_edCentExEy2.(bi)(:,2)),bedge_CenCoord.(bi)(:,3)/1000,'.','MarkerFaceColor',col)
    plot(real(comp),zNode./1000,'-','Color',col,'LineWidth',1.5);
    ylabel('Z(km)') ;xlabel('real(E)');
    % title('Real-part of boundary electric field')
    set(gca,'YDir','reverse','XDir','reverse')
    % legend('imag(E)','real(E)')
    axis tight
    box on
    % legend

    figure(3)
    subplot(1,3,2);hold on;
    % scatter(imag(eF_edCentExEy2.(bi)(:,2)),bedge_CenCoord.(bi)(:,3)/1000,'.','MarkerFaceColor',col) ;  hold on
    plot(imag(comp),zNode./1000,'-','Color',col,'LineWidth',1.5);
    ylabel('Z(km)') ;xlabel('imag(E)');
    % title('Imaginary part of boundary electric field')
    set(gca,'YDir','reverse','XDir','reverse')

    % legend('imag(E)','real(E)')
    axis tight
    box on

    figure(3)
    subplot(1,3,3);hold on;
    % scatter(abs(eF_edCentExEy2.(bi)(:,2)),bedge_CenCoord.(bi)(:,3)/1000,'.','MarkerFaceColor',col) ;  hold on
    plot(abs(comp),zNode./1000,'-','Color',col,'LineWidth',1.5);
    ylabel('Z(km)') ;xlabel('$|\mathbf{E}|$','Interpreter','latex');
    % title('Imaginary part of boundary electric field')
    set(gca,'YDir','reverse','XDir','reverse')
    axis tight
    box on
end
end
