% ---- Save *.txt file with Parameters Report ----
cd Outputs_SHA
fid=fopen('REPORT_Hazard_Analysis.txt','w');
fprintf(fid,['Parameters Report & Results for HAZARD ANALYSIS (created on ', datestr(now),')\n']);
fprintf(fid,['Parameters Estimated: Mean Return Period (MRP) and Exceedance Probability (EP) \n']);
fprintf(fid,'------------------------------------------------------------------------------------\n');
fprintf(fid,['<Magnitude Scale Selected      >: ', MScale,'\n']);
fprintf(fid,['<Time Unit                     >: ', Tunit,'\n']);
fprintf(fid,['<Magnitude Range               >: ', num2str(Mc), ' to ', num2str(max(Cmag)),'\n']);
if strcmp(method,'GRT')==1 || strcmp(method,'NPT')==1
fprintf(fid,['<Maximum Magnitude             >: ', num2str(HP(1).Mmax),'\n']);
else
fprintf(fid,['<Maximum Magnitude             >: Unbounded','\n']);
end
fprintf(fid,['<Magnitude Distribution Model  >: ', method,'\n']);
fprintf(fid,['<Magnitude (for EPP and MRP)   >: ', num2str(MaG),'\n']);
fprintf(fid,['<Time Period (for EPR)         >: ', num2str(Plength),' ',Tunit,'s','\n']);
fprintf(fid,['<Time Window Creation Mode     >: ', winmode,'\n']);
if strcmp(winmode,'Time')==1
    fprintf(fid,['<              Window Size     >: ', num2str(window_size),'(days) \n']);
     fprintf(fid,['<              Window Step     >: ', num2str(dt),'(days) \n']);
elseif strcmp(winmode,'Events')==1
    fprintf(fid,['<              Window Size     >: ', num2str(window_size),'(events) \n']);
    fprintf(fid,['<              Window Step     >: ', num2str(dt),'(days) \n']);
elseif strcmp(winmode,'Graphical')==1
    fprintf(fid,['<              Window Size     >: variable \n']);
    fprintf(fid,['<              Window Step     >: variable \n']);
end
fprintf(fid,['<Overlapping Time Windows      >: ', overlap,'\n']);
fprintf(fid,['<Bootstrap Iterations          >: ', num2str(Nbst),'\n']);
fprintf(fid,'------------------------------------------------------------------------------------\n');

for j=1:numel(HP)
SN(j)=j;Nevents(j)=numel(time_windows(j).M);TS(j)=time_windows(j).Tstart;TE(j)=time_windows(j).Tend;
end


fprintf(fid,[' Set   N    Starting Date/Time     Ending Date/Time       events            MRP                  MRP0.95CI                     EP                    E)0.95CI               b-value    b-value0.95CI \n']);
fprintf(fid,['                                                          per ',Tunit, '         ',Tunit,'s' '\n']);
for i=1:numel(HP)
       if strcmp(method,'GRU')==1 || strcmp(method,'GRT')==1;
       fprintf(fid,['%4d %5d  %s   %s %9.3f  %13.3f   %13.3f - %13.3f         %13.11f     %13.11f - %13.11f     %5.3f    %5.3f     %5.3f \n'],SN(i),Nevents(i),datestr(TS(i),0),datestr(TE(i),0),lambda(i),MRPer(i),...
           MRPer_low(i),MRPer_high(i),ExPr(i),ExPr_low(i),ExPr_high(i),bval(i),HP(i).bCI(1),HP(i).bCI(2));
       else
       fprintf(fid,['%4d %5d  %s   %s %9.3f  %13.3f   %13.3f - %13.3f         %13.11f     %13.3f - %13.11f     %s      %s \n'],SN(i),Nevents(i),datestr(TS(i),0),datestr(TE(i),0),lambda(i),MRPer(i),...
           MRPer_low(i),MRPer_high(i),ExPr(i),ExPr_low(i),ExPr_high(i),'NaN','NaN');
       end
end

fclose(fid);

% Save output structure time_window merged with HP
for i=1:length(HP)
    SHA(i).Time=time_windows(i).Time;
    SHA(i).M=time_windows(i).M;
    SHA(i).Mmin=HP(i).mmin;
    SHA(i).eps=HP(i).eps;
    SHA(i).lambd=HP(i).lamb;
    SHA(i).lambd_err=HP(i).lamb_err;
    SHA(i).unit=HP(i).unit;
    SHA(i).method=HP(i).method;
           if strcmp(method,'GRU')==1 || strcmp(method,'GRT')==1
    SHA(i).b=HP(i).b;
    SHA(i).b_0025=HP(i).bCI(1);
    SHA(i).b_0975=HP(i).bCI(2);
          else
    SHA(i).h=HP(i).h;
    SHA(i).xx=HP(i).xx;
    SHA(i).ambd=HP(i).ambd;
    SHA(i).ierr=HP(i).ierr;
           end
           if strcmp(method,'GRT')==1 || strcmp(method,'NPT')==1
    SHA(i).Mmax=HP(i).Mmax;
    SHA(i).err=HP(i).err;
           else
           end
    SHA(i).PDF=HP(i).PDF;                                                  %K29JAN2020
    SHA(i).CDF=HP(i).CDF;                                                  %K29JAN2020
    SHA(i).MRP=MRPer(i);                                                     %K03JUN2020
    SHA(i).MRP_0025=MRPer_low(i);                                                     %K03JUN2020
    SHA(i).MRP_0975=MRPer_high(i);                                                     %K03JUN2020
    SHA(i).EP_=ExPr(i);                                                     %K03JUN2020
    SHA(i).EP_0025=ExPr_low(i);                                                     %K03JUN2020
    SHA(i).EP_0975=ExPr_high(i);                                                     %K03JUN2020
                        
end




save('SHA.mat','SHA')

cd ../
