% 

clear;clc;close all   
% This is a script for a simple example of how to use the function
% Select_GM_AvgSA.m
% Mohsen Kohrangi 
% Last Updated 10/31/2018
%% Record Selection

HZ_SA = [0.25];
R_new=[30, 50];
M=[6.5, 6.0]; 

scenarios(:,1)=M';
scenarios(:,2)=R_new';
scenarios(:,3)=1.0; % this is not important here, because we backcalculate
                    % the conditining epsilon_bar in the function anyway
                    
scenarios(:,4)=1/length(M); % we assume that all bin have the same contibution in the hazard
T1_r=0.4:0.2:2.2;

for gg=1  % number of IM level considered for selection          
    id_calc=scenarios(:,4)>0;
    scenarios=scenarios(id_calc,:);
    disp( strcat(' Number of scenarios to evaluate: ', num2str(length(scenarios(:,1))) ))
%   
     OutFolderName = 'Rec_Output_CS(AvgSA)';
     mkdir(OutFolderName);
     nGM=20; 
    
outputFile = strcat(OutFolderName, '/AvgSA_IML', num2str(gg), '_mean.dat');
[SF, Fname, PerTgt, meanReq] =  Select_GM_AvgSA(nGM,T1_r, scenarios, outputFile, HZ_SA(gg));
save(strcat(OutFolderName, '/AvgSA_IML', num2str(gg), '_mean.mat'), 'SF', 'Fname', 'PerTgt', 'meanReq');
end