% Batch Processor
% This script batch processes the Kalman Filter Inference effort

%The purpose of this simulation run is to ascertain the difference  between
%initialization the next iteration of the Kalman filter with a the
%constrained estimate and covariance or the unconstrained convariance and
%estimate of either of the two. Only one author[Sircoulomb2008] seems to have noticed this
%change.
function Batch_Process(nGene,nMeas,nTimePts,nRealizations,tag)

% Paramter declarations

dirName = sprintf('New2DataGene%dMeas%dTimePts%d%s',nGene,nMeas,nTimePts,tag);
mkdir('.',dirName);
% nGene = 10;
% nMeas = 7;
% nTimePts = 3;
n_Measurements_Vector = nMeas*ones(1,nTimePts);% for each time point
Beta = 0.2; %If an estimated edge coefficient is in Beta% vicinity of the
%original value, the estimate is a good estimate
% epsilon = 0.2; %sparsity constraint for the l1 norm minimization problem
% n_ErrorsVec_Smooth_Constr_IC = zeros(sum(n_Measurements_Vector), n_Realizations);
Alpha = 0.05; %Alpha% of the edges will die and Alpha% new edges will be
%created during each time slot
% A_Array = zeros(nGene,nGene,numel(n_Measurements_Vector));
% A_Array_save = zeros(nGene,nGene,nRealizations,numel(n_Measurements_Vector));
% EKalmanSmthConst_save = zeros(n_Gene,n_Gene,n_Realizations,sum(n_Measurements_Vector));
% EKalmanSmthConstDim_save = zeros(n_Gene,n_Gene,n_Realizations,sum(n_Measurements_Vector));
% EKalmanSmthConstVec_save = zeros(nGene,nGene,nRealizations,numel(n_Measurements_Vector));

% EEnKF_save = zeros(n_Gene,n_Gene,n_Realizations,sum(n_Measurements_Vector));

if(matlabpool('size') == 0)
    matlabpool local 7
end

parfor i=1:nRealizations
    tic;
    fid = 1;%fopen(sprintf('Output_%dNgene_%dNtau_%dsigma%dnObs.txt',n_Gene_j,Ntau,sqrt(sigmasq_j),n_Measurements_Vector_j(1)),'a');

    fprintf(fid,'\n Processing realization no --> %u \n',i);
    [A_Array, X_Array, Y_Array,~] = func_TVNW_Generator2_vec(nGene, n_Measurements_Vector,Alpha);
%     A_Array_save(:,:,i,:) = A_Array;

    %The vectorized Kalman filter
    [EKalmanSmthConstVec,aHatApos,aHatApr,Pkp1k,Pkk,PkN,Lambda] = func_Kalman_Smooth_Const_Vec(A_Array,X_Array,Y_Array,n_Measurements_Vector,nGene);
%     EKalmanSmthConstVec_save(:,:,i,:) = EKalmanSmthConstVec;

    %Calculate the Error
    [pKalmanSmthConstVecErr, dKalmanSmthConstVec, tp, tn, fp, fn] = CalcErrorAlpha(A_Array, EKalmanSmthConstVec,n_Measurements_Vector,i,nGene,Beta,fid,'CVX Kalman Vec');
    savePath = sprintf('.\\%s\\Gene%dMeas%dTimePts%dRealization%d.mat',dirName,nGene,nMeas,nTimePts,i);
    fprintf('Saving %s\n',savePath);
    timetaken = toc;
    iSave(savePath,A_Array,X_Array,Y_Array,EKalmanSmthConstVec,pKalmanSmthConstVecErr,dKalmanSmthConstVec,aHatApos,aHatApr,Pkp1k,Pkk,PkN,Lambda,timetaken,i,tp, tn, fp, fn);
    if (fid ~= 1)
        fclose(fid);
    end
end

% figure;plot(mean(pKalmanSmthConstVecErr,2),'r');title(sprintf('CVX Kalman Dim IC Mean: %0.1f , Var: %0.1f',mean(mean(pKalmanSmthConstVecErr)),var(mean(pKalmanSmthConstVecErr,2))));