% Procedure: ComputeAndSaveCollapseRiskForAPeriodAndAllGMSets.m
% -------------------
% This function computes and saves the P[collapse | 2% in 50] and the MAF of collapse
% for a certain period and for all of the GM sets.
%
% Author: Curt Haselton 
% Date Written: 9-27-05
%
% Sources of Code: Some information was taken from Paul Cordovas processing file called "ProcessData.m".
%
% Functions and Procedures called: none
%
% Variable definitions: 
%
% Units: Whatever OpenSees is using - just be consistent!
%
% -------------------
function[probOfCollapseAtTwoPercentInFifty_AllGMSetsForCurrentPeriod, MAFofCollapse_AllGMSetsForCurrentPeriod] = ComputeAndSaveCollapseRiskForAPeriodAndAllGMSets(currentPeriodForColRiskCalcs, twoPercentInFiftySa, k, outputFolderName, currentFundPeriodOfSDOF, originalTOneUsedWhenAnalysesWereRun, originalDampRatForSaTOneUsedWhenAnalysesWereRun, currentDampRatToUseForSaCol, periodLISTForCalcs, recordSetsLIST, periodThatRecordSetsWereSelectedForLIST, markerTypeForPlotsLIST_indivPoints, markerTypeForPlotsLIST_lognormalFit, legendEntriesLIST);

disp('Computing collapse probability and MAF...');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open the file that has the fitten lognormal collapse CDF for the IM of this period
% (for all EQ sets)
    % Go to the output folder
    currentDir = [pwd];
    cd ..;
    cd ..;
    cd Output;
    cd(outputFolderName);
    
    % Make the file name and open the file
    fileName = sprintf('CollapseCDFResults_LogNormalFits_T=%.2f.mat', currentPeriodForColRiskCalcs);
    load(fileName);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loop for all GM sets and do calcs...
    for gmSetIndex = 1:length(currentMeanLnSaColLIST)

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Compute the MAF of collapse (with only RTR variability) for each GM
        % set
                % Compute kZero, which is the coeff. on the hazard curve (see hand notes on 9-27-05 for EERI paper with Baker)
                    kZero = (1.0 / 2500.0) / ((twoPercentInFiftySa) ^ -k);

                % Compute adaC, which is just the the median collapse capacity (see hand notes on 9-27-05 for EERI paper with Baker)
                    meanLnSaCol_current = currentMeanLnSaColLIST(gmSetIndex);
                    adaC_current = exp(meanLnSaCol_current);

                % Compute the MAF of collapse
                    stDevLnSaCol_current = currentStDevLnSaColLIST(gmSetIndex);
                    MAFCollapse_current = kZero * adaC_current ^ -k * exp(0.5 * (k^2) * (stDevLnSaCol_current^2));
    
                % Save results
                    MAFofCollapse_AllGMSetsForCurrentPeriod(gmSetIndex) = MAFCollapse_current;
                    
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Compute the P[collapse | 2% in 50 years]
            % Compute
                probOfCollapseAtTwoPercentInFifty_current = logncdf(twoPercentInFiftySa, meanLnSaCol_current, stDevLnSaCol_current);

            % Save
                probOfCollapseAtTwoPercentInFifty_AllGMSetsForCurrentPeriod(gmSetIndex) = probOfCollapseAtTwoPercentInFifty_current;

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    end
    
    % Now save the results to a file
    fileNameToSave = sprintf('CollapseProbAndMAF_IMUsedAtT=%.2f.mat', currentPeriodForColRiskCalcs);
    save(fileNameToSave, 'probOfCollapseAtTwoPercentInFifty_AllGMSetsForCurrentPeriod', 'MAFofCollapse_AllGMSetsForCurrentPeriod', 'currentPeriodForColRiskCalcs', 'twoPercentInFiftySa', 'k', 'outputFolderName');
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Back to the original folder
cd(currentDir);

