%**************************************************************************
% CLUSTER project: Plot divB & curlB to screen     Autor: Christian Maszl 
% Unit System = [SI]                  Mail: e0025754@student.tuwien.ac.at 
%**************************************************************************
%
% Version 1.0 - 20.8.2004
%
% This subroutine plot jav and B on the screen. The program looks
% up the datafiles in the "outputdata" directrory defined in
% config.m . Please make sure that you selected the right mode!

clear variables;
% Load configfile and check the path's ...
syscheck;
% Plot some infos from configfile.m for the manual mode
dataname=upper(dataname);
datasrc =upper(datasrc);
message =['Enter a date to plot ' dataname ' - ' timeres 'sec '...
	  datasrc '-data.'];
          disp(message);
date=input('Please enter the desired day [yymmdd]: ','s');
% Extract extension
l=size(datatype,2);
if (l==4)
    exten=datatype(2:4);
else
    exten=datatype(2:4);
end
javname =[outputspath date '0000' mode '.jav.gse.hr.igm' exten];
D1name  =[outputspath date '0000' mode '.D1.gse.hr.igm' exten];
D2name  =[outputspath date '0000' mode '.D2.gse.hr.igm' exten];
D3name  =[outputspath date '0000' mode '.D3.gse.hr.igm' exten];
D4name  =[outputspath date '0000' mode '.D4.gse.hr.igm' exten];
divBname=[outputspath date '0000' mode '.divB.gse.hr.igm' exten];
reportna=[outputspath date '0000' mode '.report'];
% Load the variabels to the memory
try
load(javname,datatype);
    disp('jav loaded to memory!');
load(D1name,datatype);
load(D2name,datatype);
load(D3name,datatype);
load(D4name,datatype);
    disp('B1-B4 loaded to memory!');
    clear r1 r2 r3 r4;
load(divBname,datatype);
    disp('<divB> loaded to memory!');
catch
    disp(['Error: Filename not found! Please check your input'...
          ' or the available files!']);
    disp('Please restart the program!');
    try
       load(reportna,'-mat');
       disp(error_report);
    catch
       reportfile='n';
    end
    break;
end
try
   load(reportna,'-mat');
   reportfile='y';
catch
   reportfile='n';
end
clear javname B14name divBname l exten year month day;
try
year(1,1) =str2num(date(1,1:2));
month(1,1)=str2num(date(1,3:4));
day(1,1)  =str2num(date(1,5:6));
datum=datestr(datenum([2000+year month day]));
timelabel=['{t / [h]}'];
text1=[datum ' , Coord. system: {\itGSE} , Unit System: {\itSI} , '...
             'Time System: {\itUT}'];    
% Find plot range for jav and check for data
if (isempty(jav)~=1)
    if (plot_mode_normal=='y')
    range =(sum(abs(jav) ,1)/size(jav,1))*10;
% Define styles, labels for axis, ...
    jav_axisr=[0 24.0 -range(1,2) range(1,2)];
figure(1);
textfig1=['Jav & divB - ' datum];
set(1,'Name',textfig1);
% Plot the x component of the current density versus the time.
    subplot(4,1,1)
    plot(T,jav(:,1))
        axis(jav_axisr)
        ylabel('{J}_{x}{ / [A/m}^{2}{]}','FontSize',10)
        xlabel(timelabel    ,'FontSize',10)
        title(text1,'FontSize',12)
        grid on
% Plot the y component of the current density versus the time.
    subplot(4,1,2)
    plot(T,jav(:,2)) 
        axis(jav_axisr)
        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(jav_axisr)
        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;
    range     =(sum(abs(divBocurlB) ,1)/size(T,1))*10;
    subplot(4,1,4)
    plot(T,divBocurlB) 
        axis([0.0 24.0 0 range]) 
        ylabel('{\nabla\cdotB/|\nablaxB| / [%]}' ,'FontSize',10)
        xlabel(timelabel            ,'FontSize',10)
        formula1=['{\it\mu}_{\it0}{\itJ}_{\itav}{ = }'...
                  '{\it\nablaxB}'];
        formula2=['{\it\int_V}{\itdV \nabla\cdotB} = '...
                  '{\it\int_A}{\itdA B}'];
        text=['Used formula:   ' formula1 '   and   ' formula2...
              '     Data source: {\it' upper(datasrc) '} - '  ...
              timeres   'sec data'];
        title(text,'Fontsize',12)
        grid on
    else
    end
    if (plot_mode_parnor=='y')
% Calculate the normal and parallel component of Jav
    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));
    % Find plot ranges
    range =(sum(abs(jav_p) ,1)/size(jav_p,1))*10;
    % Define styles, labels for axis, ...
    jav_axisr=[0 24.0 -range(1,1) range(1,1)];
    timelabel=['{t / [h]}'];
figure(3);
textfig1=['Jav(para), Jav(perp) & divB - ' datum];
set(3,'Name',textfig1);
% Plot the Jav_para versus the time.
    subplot(3,1,1)
    plot(T,jav_p)
        axis(jav_axisr)
        ylabel('{J}_{||} { / [A/m}^{2}{]}','FontSize',10)
        xlabel(timelabel    ,'FontSize',10)
        text1=[datum ' , Coord. system: {\itGSE} , Unit System:'...
               ' {\itSI} , Time System: {\itUT}'];    
        title(text1,'FontSize',12)
        grid on
% Plot Jav_perp versus the time.
    range     =(sum(abs(jav_n) ,1)/size(T,1))*10;
    subplot(3,1,2)
    plot(T,jav_n) 
        axis([0.0 24.0 0 range]) 
        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;
    range     =(sum(abs(divBocurlB) ,1)/size(T,1))*10;
    subplot(3,1,3)
    plot(T,divBocurlB) 
        axis([0.0 24.0 0 range]) 
        ylabel('{\nabla\cdotB/|\nablaxB| / [%]}' ,'FontSize',10)
        xlabel(timelabel            ,'FontSize',10)
        formula1=['{\it\mu}_{\it0}{\itJ}_{\itav}{ = }'...
                  '{\it\nablaxB}'];
        formula2=['{\it\int_V}{\itdV \nabla\cdotB} = '...
                  '{\it\int_A}{\itdA B}'];
        text=['Used formula:   ' formula1 '   and   ' formula2...
              '     Data source: {\it' upper(datasrc) '} - '  ...
              timeres   'sec data'];
        title(text,'Fontsize',12)
        grid on

    else
    end
else
end
% Open a new plot window
figure(2);
textfig2=['B-field - ' datum];
set(2,'Name',textfig2)
% Plot the x component of B versus the time
subplot(4,1,1)
plot(T,B1(:,1),T,B2(:,1),T,B3(:,1),T,B4(:,1)) 
    axis([0.0 24.0 -1000 1000])
    axis 'auto y'
    ylabel('{B}_{x}{ / [nT]}','FontSize',10)
    xlabel(timelabel  ,'FontSize',10)
    title(text1,'FontSize',12)
    grid on
% Plot the y component of B versus the time.
subplot(4,1,2)
plot(T,B1(:,2),T,B2(:,2),T,B3(:,2),T,B4(:,2)) 
    axis([0.0 24.0 -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,B1(:,3),T,B2(:,3),T,B3(:,3),T,B4(:,3)) 
    axis([0.0 24.0 -1000 1000])
    axis 'auto y'
    ylabel('{B}_{z}{ / [nT]}','FontSize',10)
    xlabel(timelabel  ,'FontSize',10)
    grid on
% Calculate |B|
B1m=sqrt(dot(B1,B1,2));
B2m=sqrt(dot(B2,B2,2));
B3m=sqrt(dot(B3,B3,2));
B4m=sqrt(dot(B4,B4,2));
% Plot |B| versus the time.
subplot(4,1,4)
plot(T,B1m,T,B2m,T,B3m,T,B4m) 
    axis([0.0 24.0 -1000 1000])
    axis 'auto y'
    ylabel('{|B| / [nT]}' ,'FontSize',10)
    xlabel(timelabel    ,'FontSize',10)
    text=['Used formula:   ' '{\itB = (B}_{\it1}{+\itB}_{\it2}+'...
	  '{\itB}_{\it3}{+\itB}_{\it4}{\it)/4}' '    Data source:  '...
	  '{\it' upper(datasrc) '} - ' timeres 'sec data'];
    title(text,'FontSize',12)
    grid on
    if (legend_on=='y')
        legend('SC1','SC2','SC3','SC4',4);
    else
    end
catch
   disp('Error: Files contain no data!')
end
% Check if there is a report file
if (reportfile=='n')
else
% Display error messages on the screen
disp(' ');
disp('REPORT FROM THE GETDATA ROUTINE! READ THIS MESSAGE CAREFULLY!');
disp(error_report);
end
clear advanced datapath datasrc datatype date datum day extension;
clear formula1 formula2 instrument jav_axisr logfilepath mode;
clear month nolog opmode outputspath range reportfile reportna;
clear tempdirpath text* timeacc timelabel timeres y0 year compmode;
clear B*m D*name varname option op_mode message isdat_db dataname;
clear plot_* legend_* cos* norm_*
% Unlock plotdiagram
munlock plotdiagram;
