function [] = Stats()
%
%###########################################################################
% Failure Trace Archive (FAT) scripts
% Program to extract basic statisctics from event_trace.tab. 
%
% Author: Bahman Javadi
%        Yogesh Sharma
%        School of Computing, Engineering and Mathematics
%        Western Sydney University, Australia
%        b.javadi@westernsydeny.edu.au
%        http://fta.scem.uws.edu.au/
%
%###########################################################################
% Example:
% Enter the path of the file: ~/FTA/lanl05_tab_union/event_trace.tab
%###########################################################################

prompt='Enter the path of the file: ';
fid=input(prompt,'s');
[event_id, component_id, node_id, platform_id, node_name, event_type, event_start_time, event_stop_time, end_reason] = textread(fid, '%f %f %f %f %s %f %f %f %s', ...
                                  'headerlines', 1);

%{
###########################################################################

Counting number of rows in the file

###########################################################################
%}                              
number_of_rows=numel(event_start_time);


%{
###########################################################################

This part of the program extract information about the start and stop 
intervals in the traces.   

###########################################################################
%}
unavailability_event_start = [];
unavailability_event_stop = [];
event_unavailability_interval = [];
availability_event_start = [];
availability_event_stop = [];
event_availability_interval = [];
disp ('All numbers are in hour.....');
j=1;
k=1;
for i=1:number_of_rows
    if(event_type(i)==0)
        unavailability_event_start(j)=event_start_time(i)/3600;
        unavailability_event_stop(j)=event_stop_time(i)/3600;
        event_unavailability_interval(j)=unavailability_event_stop(j)-unavailability_event_start(j);
        j=j+1;
    else
        availability_event_start(k)=event_start_time(i)/3600;
        availability_event_stop(k)=event_stop_time(i)/3600;
        event_availability_interval(k)=availability_event_stop(k)-availability_event_start(k);
        k=k+1;
    end
end
event_unavailability_interval1=event_unavailability_interval';
event_availability_interval1=event_availability_interval';

% Save the data into fta_data.mat for next usage
save('fta_data.mat','event_availability_interval', 'event_unavailability_interval');

disp('Total number of unavailability intervals');
disp(j-1);

disp('Total number of availability intervals');
disp(k-1);


%{
###########################################################################

This part of the program calculate the sum of availability and 
unavailability intervals

###########################################################################
%}
sum_event_unavailability_interval=sum(event_unavailability_interval);
disp('Sum of the unavailability intervals of all the nodes');
disp(sum_event_unavailability_interval);
sum_event_availability_interval=sum(event_availability_interval);
disp('Sum of the availability intervals of all the nodes');
disp(sum_event_availability_interval);

%{
###########################################################################

This part of the program calculate the mean of availability and 
unavailability intervals

###########################################################################
%}
mean_event_unavailability_interval=mean(event_unavailability_interval);
disp('Mean of the unavailability intervals of all the nodes (MTTR)');
disp(mean_event_unavailability_interval);
mean_event_availability_interval=mean(event_availability_interval);
disp('Mean of the availability intervals of all the nodes (MTBF)');
disp(mean_event_availability_interval);

%{
###########################################################################

This part of the program calculate the median of availability and 
unavailability intervals

###########################################################################
%}
median_event_unavailability_interval=median(event_unavailability_interval);
disp('Median of the unavailability intervals of all the nodes');
disp(median_event_unavailability_interval);
median_event_availability_interval=median(event_availability_interval);
disp('Median of the availability intervals of all the nodes');
disp(median_event_availability_interval);

%{
###########################################################################

This part of the program calculate the trimmed mean of availability and 
unavailability intervals

###########################################################################
%}
trimmean_event_unavailability_interval=trimmean(event_unavailability_interval,10);
disp('Trimmed mean of the unavailability intervals of all the nodes');
disp(trimmean_event_unavailability_interval);
trimmean_event_availability_interval=trimmean(event_availability_interval,10);
disp('Trimmed mean of the availability intervals of all the nodes');
disp(trimmean_event_availability_interval);

%{
###########################################################################

This part of the program calculate the Standard Deviation of availability
and unavailability intervals

###########################################################################
%}
std_event_unavailability_interval=std(event_unavailability_interval);
disp('Standard Deviation of the unavailability intervals of all the nodes');
disp(std_event_unavailability_interval);
std_event_availability_interval=std(event_availability_interval);
disp('Standard Deviation of the availability intervals of all the nodes');
disp(std_event_availability_interval);

%{
###########################################################################

This part of the program calculate the Coefficient of Variance of 
availability and unavailability intervals

###########################################################################
%}
CV_event_unavailability_interval=std_event_unavailability_interval/mean_event_unavailability_interval;
disp('Coefficient of Variance of the unavailability intervals of all the nodes');
disp(CV_event_unavailability_interval);
CV_event_availability_interval=std_event_availability_interval/mean_event_availability_interval;
disp('Coefficient of Variance of the availability intervals of all the nodes');
disp(CV_event_availability_interval);

%{
###########################################################################

This part of the program calculate the Interquartile range (IQR) of 
availability and unavailability intervals

###########################################################################
%}
IQR_event_unavailability_interval=iqr(event_unavailability_interval);
disp('Interquartile range (IQR) of the unavailability intervals of all the nodes');
disp(IQR_event_unavailability_interval);
IQR_event_availability_interval=iqr(event_availability_interval);
disp('Interquartile range (IQR) of the availability intervals of all the nodes');
disp(IQR_event_availability_interval);

%{
###########################################################################

This part of the program calculate the Maximum Duration of availability and 
unavailability intervals

###########################################################################
%}
Max_event_unavailability_interval=max(event_unavailability_interval);
disp('Maximum Duration of the unavailability intervals of all the nodes');
disp(Max_event_unavailability_interval);
Max_event_availability_interval=max(event_availability_interval);
disp('Maximum Duration of the availability intervals of all the nodes');
disp(Max_event_availability_interval);

%{
###########################################################################

This part of the program calculate the Minimum Duration of availability and 
unavailability intervals

###########################################################################
%}
Min_event_unavailability_interval=min(event_unavailability_interval);
disp('Minimum Duration of the unavailability intervals of all the nodes');
disp(Min_event_unavailability_interval);
Min_event_availability_interval=min(event_availability_interval);
disp('Minimum Duration of the availability intervals of all the nodes');
disp(Min_event_availability_interval);

%{
###########################################################################

This part of the program calculate the sample skewness of availability and 
unavailability intervals

###########################################################################
%}
Skew_event_unavailability_interval=skewness(event_unavailability_interval);
disp('Skewness of the unavailability intervals of all the nodes');
disp(Skew_event_unavailability_interval);
Kurt_event_availability_interval=skewness(event_availability_interval);
disp('Skewness of the availability intervals of all the nodes');
disp(Kurt_event_availability_interval);

%{
###########################################################################

This part of the program calculate the sample kurtosis of availability and 
unavailability intervals

###########################################################################
%}
Kurt_event_unavailability_interval=kurtosis(event_unavailability_interval);
disp('Kurtosis of the unavailability intervals of all the nodes');
disp(Kurt_event_unavailability_interval);
Kurt_event_availability_interval=kurtosis(event_availability_interval);
disp('Kurtosis of the availability intervals of all the nodes');
disp(Kurt_event_availability_interval);

