function plotdiagram(r1,r2,r3,r4,BA,BB,BC,BD,jav,divB,stepanz)
%**************************************************************************
% CLUSTER project: Plot divB & curlB to screen     Autor: Christian Maszl *
% Unit System = [SI]                  Mail: e0025754@student.tuwien.ac.at *
%**************************************************************************
l=size(BA,1);
BA  = BA (1:l-1,:);
BB  = BB (1:l-1,:);
BC  = BC (1:l-1,:);
BD  = BD (1:l-1,:);
r1  = r1 (1:l-1,:)*1000;
r2  = r2 (1:l-1,:)*1000;
r3  = r3 (1:l-1,:)*1000;
r4  = r4 (1:l-1,:)*1000;
jav = jav(1:l-1,:);
divB=divB(1:l-1,:);
B1=BA;
B2=BB;
B3=BC;
B4=BD;
%[] Load data from a mat-file
%[] Find plot ranges
timelabel=['{t / [s]}'];
T=jav(:,1);
jav=[jav(:,2) jav(:,3) jav(:,4)];
% Plot the x component of the current density versus the time.
figure(1)
subplot(4,1,1)
plot(T,jav(:,1))
    axis([0.0 stepanz -1000 1000])
    axis 'auto y'
    ylabel('{J}_{x}{ / [A/m}^{2}{]}','FontSize',10)
    xlabel(timelabel    ,'FontSize',10)
    grid on
% Plot the y component of the current density versus the time.
subplot(4,1,2)
plot(T,jav(:,2))
    axis([0.0 stepanz -1000 1000])
    axis 'auto y'
    ylabel('{J}_{y}{ / [A/m}^{2}{]}','FontSize',10)
    xlabel(timelabel    ,'FontSize',10)
    grid on
% Plot the z component of the current density versus the time.
subplot(4,1,3)
plot(T,jav(:,3))
    axis([0.0 stepanz -1000 1000])
    axis 'auto y'
    ylabel('{J}_{z}{ / [A/m}^{2}{]}','FontSize',10)
    xlabel(timelabel    ,'FontSize',10)
    grid on
% Plot <divB>/abs(curlB) versus the time.
divBocurlB=(divB./sqrt(dot(jav,jav,2)))*100;
subplot(4,1,4)
plot(T,divBocurlB)
     axis([0.0 stepanz -1000 1000])
     axis 'auto y'
     ylabel('{\nabla\cdotB/|\nablaxB| / [%]}' ,'FontSize',10)
     xlabel(timelabel            ,'FontSize',10)
     grid on
% Plot the x component of B versus the time
figure(2)
subplot(4,1,1)
plot(T,BA(:,1),T,BB(:,1),T,BC(:,1),T,BD(:,1))
    axis([0.0 stepanz -1000 1000])
    axis 'auto y'
    ylabel('{B}_{x}{ / [nT]}','FontSize',10)
    xlabel(timelabel  ,'FontSize',10)
    grid on
% Plot the y component of B versus the time.
subplot(4,1,2)
plot(T,BA(:,2),T,BB(:,2),T,BC(:,2),T,BD(:,2))
    axis([0.0 stepanz -1000 1000])
    axis 'auto y'
    ylabel('{B}_{y}{ / [nT]}','FontSize',10)
    xlabel(timelabel  ,'FontSize',10)
    grid on
% Plot the z component of B versus the time.
subplot(4,1,3)
plot(T,BA(:,3),T,BB(:,3),T,BC(:,3),T,BD(:,3))
    axis([0.0 stepanz -1000 1000])
    axis 'auto y'
    ylabel('{B}_{z}{ / [nT]}','FontSize',10)
    xlabel(timelabel  ,'FontSize',10)
    grid on
% Calculate |B|
B1n=sqrt(dot(BA,BA,2));
B2n=sqrt(dot(BB,BB,2));
B3n=sqrt(dot(BC,BC,2));
B4n=sqrt(dot(BD,BD,2));
% Plot |B| versus the time.
subplot(4,1,4)
plot(T,B1n,T,B2n,T,B3n,T,B4n)
    axis([0.0 stepanz -1000 1000])
    axis 'auto y'
    ylabel('{|B| / [nT]}' ,'FontSize',10)
    xlabel(timelabel    ,'FontSize',10)
    legend('{SC1}','{SC2}','{SC3}','{SC4}',4);
    grid on
Bm      =(B1+B2+B3+B4)/4;
norm_jav=sqrt(dot(jav,jav,2));
norm_B  =sqrt(dot(Bm ,Bm ,2));
cosphi  =(dot(Bm,jav,2)./(norm_B.*norm_jav));
jav_p   =dot(Bm,jav,2)./norm_B;
jav_n   =norm_jav.* sqrt(1-dot(cosphi,cosphi,2));
figure(3);
% Plot the Jav_para versus the time.
    subplot(3,1,1)
    plot(T,jav_p)
        axis([0.0 stepanz -1000 1000])
        axis 'auto y'
        ylabel('{J}_{||} { / [A/m}^{2}{]}','FontSize',10)
        xlabel(timelabel    ,'FontSize',10)
        grid on
% Plot Jav_perp versus the time.
     subplot(3,1,2)
     plot(T,jav_n) 
         axis([0.0 stepanz -1000 1000])
         axis 'auto y'
         ylabel('{J}_\perp{ / [A/m}^{2}{]}','FontSize',10)
         xlabel(timelabel            ,'FontSize',10)
         grid on
% Plot <divB>/abs(curlB) versus the time.
    divBocurlB=(divB./sqrt(dot(jav,jav,2)))*100;
    subplot(3,1,3)
    plot(T,divBocurlB)
        axis([0.0 stepanz -1000 1000])
        axis 'auto y'
        ylabel('{\nabla\cdotB/|\nablaxB| / [%]}' ,'FontSize',10)
        xlabel(timelabel            ,'FontSize',10)
        grid on
figure(4)
    subplot(4,1,1)
    plot(T,r1)
        axis([0.0 stepanz -1000 1000])
        axis 'auto y'
        ylabel('{SC1 - r_1 / [m]}','FontSize',10)
        xlabel(timelabel          ,'FontSize',10)
        grid on
        legend('{x_1}','{y_1}','{z_1}',3);
    subplot(4,1,2)
    plot(T,r2) 
        axis([0.0 stepanz -1000 1000])
        axis 'auto y'
        ylabel('{SC2 - r_2 / [m]}','FontSize',10)
        xlabel(timelabel          ,'FontSize',10)
        grid on
	legend('{x_2}','{y_2}','{z_2}',3);
    subplot(4,1,3)
    plot(T,r3) 
        axis([0.0 stepanz -1000 1000])
        axis 'auto y'
        ylabel('{SC3 - r_3 / [m]}','FontSize',10)
        xlabel(timelabel          ,'FontSize',10)
        grid on
        legend('{x_3}','{y_3}','{z_3}',3);
    subplot(4,1,4)
    plot(T,r4) 
         axis([0.0 stepanz -1000 1000])
         axis 'auto y'
    ylabel('{SC4 - r_4 / [m]}','FontSize',10)
        xlabel(timelabel          ,'FontSize',10)
        grid on
        legend('{x_4}','{y_4}','{z_4}',3);
        
