close all;d=figure('Position',[300 50 1600 950])

% check whether the selected time windows are overlapping or not
TT=[];Tcat=Catalog(1).val;Ncat=Tcat(Tcat>=time_windows(1).Tstart & Tcat<=time_windows(length(ExPr)).Tend);
for i=1:length(MRPer);
TW1(i)=time_windows(i).Tstart;Tw2(i)=time_windows(i).Tend;
tplo(i)=mean([time_windows(i).Tstart time_windows(i).Tend]);meanM(i)=mean(time_windows(i).M);hold on
TT=[TT;time_windows(i).Time];
lambda(i)=HP(i).lamb;
if strcmp(HP(1).method,'GRU') || strcmp(HP(1).method,'GRT');yyaxis right;bval(i)=HP(i).b;end
end

%if numel(TT)==numel(Ncat)
DTW=TW1(2:length(TW1))-Tw2(1:length(Tw2)-1);    %%% THIS SEEMS TO WORK!!!!
if isempty(find(DTW<0))
overlap='NO';
    
for i=1:length(MRPer);
    

subplot(3,1,1)    % plot Mean return period
hold on;plot([time_windows(i).Tstart time_windows(i).Tend],[MRPer(i) MRPer(i)],'k-','LineWidth',2)
if i<length(MRPer);plot([time_windows(i).Tend time_windows(i+1).Tstart],[MRPer(i) MRPer(i+1)],'k--');end
datetick('x',20);title(['Mean Return Period for M\geq',answer3{1}],'FontSize',16);ylabel([HP(1).unit,'s'],'FontSize',18)

subplot(3,1,2)   % plot Exceedance Probability
hold on;plot([time_windows(i).Tstart time_windows(i).Tend],[ExPr(i) ExPr(i)],'k-','LineWidth',2)
if i<length(ExPr);plot([time_windows(i).Tend time_windows(i+1).Tstart],[ExPr(i) ExPr(i+1)],'k--');end
datetick('x',20);title(['Exceedance Probability for M\geq',answer3{1},' within ',answer3{2}, ' ',HP(1).unit, '(s) period'],'FontSize',16);ylabel('probability','FontSize',14)

subplot(3,1,3)   % plot Activity rate 
hold on;yyaxis left;plot([time_windows(i).Tstart time_windows(i).Tend],[HP(i).lamb HP(i).lamb],'k-','LineWidth',2)
if i<length(ExPr);plot([time_windows(i).Tend time_windows(i+1).Tstart],[HP(i).lamb HP(i+1).lamb],'k--');end
datetick('x',20);title(['Activity Rate'],'FontSize',16);ylabel(['Events/',HP(1).unit],'FontSize',14,'Color','k')
set(gca,'YColor','k');
% plot b-value (GR) or mean M (NP)
 if strcmp(HP(1).method,'GRU') || strcmp(HP(1).method,'GRT');yyaxis right;
 plot([time_windows(i).Tstart time_windows(i).Tend],[HP(i).b HP(i).b],'-','LineWidth',2)
 ylabel('b-value','FontSize',14);
 else
 yyaxis right;plot([time_windows(i).Tstart time_windows(i).Tend],[mean(time_windows(i).M) mean(time_windows(i).M)],'-','LineWidth',2)
 ylabel('mean Magnitude','FontSize',14);
 end

end

else
    
overlap='YES';
    
subplot(3,1,1)      % plot Mean return period
plot(tplo,MRPer,'o','LineWidth',2,'MarkerSize',12);
datetick('x',20);title(['Mean Return Period for M\geq',answer3{1}],'FontSize',16);ylabel([HP(1).unit,'s'],'FontSize',18)
subplot(3,1,2)     % plot Exceedance Probability
plot(tplo,ExPr,'o','LineWidth',2,'MarkerSize',12);
datetick('x',20);title(['Exceedance Probability for M\geq',answer3{1},' within ',answer3{2}, ' ',HP(1).unit, '(s) period'],'FontSize',16);ylabel('probability','FontSize',14)
subplot(3,1,3)    % plot Activity rate 
plot(tplo,lambda,'o','LineWidth',2,'MarkerSize',12);ylabel(['Events/',HP(1).unit],'FontSize',14)
if strcmp(HP(1).method,'GRU') || strcmp(HP(1).method,'GRT');
yyaxis right;plot(tplo,bval,'o','LineWidth',2,'MarkerSize',12); ylabel('b-value','FontSize',14);
ylabel('b-value','FontSize',14);
else; yyaxis right;plot(tplo,meanM,'o','LineWidth',2,'MarkerSize',12)
 ylabel('mean Magnitude','FontSize',14);
end
datetick('x',20);title(['Activity Rate'],'FontSize',16);
end

if isempty(PROD_Data)==0
subplot(3,1,1);yyaxis right;plot(PROD_Data(1).val,PROD_Data(s2).val,'-','Linewidth',1);ylabel(PROD_Data(s2).field,'interpreter','none','FontSize',14);    
subplot(3,1,2);yyaxis right;plot(PROD_Data(1).val,PROD_Data(s2).val,'-','Linewidth',1);ylabel(PROD_Data(s2).field,'interpreter','none','FontSize',14);    
end
subplot(3,1,3);xlabel('Date','FontSize',18)

% option to switch linear-log Y axis Scale

    txt = uicontrol('Parent',d,...
           'Style','text',...
           'Position',[200 621 150 30],...
           'String','Select Y Axis Scale:');
       
    popup = uicontrol('Parent',d,...
           'Style','popup',...
           'Position',[350 630 120 25],...
           'String',{'Linear';'Log'},...
           'Callback',@popup_callback);
       
     btn = uicontrol('Parent',d,...
            'Position',[210 880 210 50],...
            'String','SAVE and CLOSE',...
            'FontSize',18,...
            'ForeGroundColor','r',...
            'FontWeight','Bold',...
            'Callback',@savefig_callback);
       
    choice = 'Linear';
       
    % Wait for d to close before running to completion
    uiwait(d);
   
       function popup_callback(popup,event)
          idx = popup.Value;
          popup_items = popup.String;
          % This code uses dot notation to get properties.
          % Dot notation runs in R2014b and later.
          % For R2014a and earlier:
          % idx = get(popup,'Value');
          % popup_items = get(popup,'String');
          choice = char(popup_items(idx,:));
          subplot(3,1,1);yyaxis left;
          set(gca,'YScale',choice)
       end

 function savefig_callback(popup,event)
          cd Outputs_SHA
          print(gcf,'SHA.jpeg','-djpeg','-r300')
          savefig(gcf,'SHA.fig')
          % This code uses dot notation to get properties.
          % Dot notation runs in R2014b and later.
          % For R2014a and earlier:
          % idx = get(popup,'Value');
          % popup_items = get(popup,'String');
          cd ../
          delete(gcf)
       end






