clear all; clc
% model written by Allison Shaw (contact for assistance: ashaw@umn.edu)
% started April 2021
%   last updated: 10 January 2023

load fig4sims.mat *

avec2 = fliplr(avec);

mymap = parula;
mymap = [1 1 1; mymap]; % add white for non-spreading simulations

fs1 = 10;  % axes labels
fs3 = 09;  % axis numbering
lw2 = 1; % fig edges

width = 11;
height = 9;
xpos = 3;
ypos = 2;
sx = 0.13;
sy = 0.11;
w1 = 0.35;
w2 = w1-0.115;
he = 0.3;
dy = 0.22;
dx = 0.17;

% set NaNs to zero to be able to calculate difference
mat_speed_without(isnan(mat_speed_without))=0;

eps1 = 0.00000001; % set threshold below which consider speed zero

mat_speed_without2 = mat_speed_without;
mat_speed_without2(mat_speed_without2<eps1)=0;

mat_speed_with2 = mat_speed_with;
mat_speed_with2(mat_speed_with2<eps1)=0;

% set zeros to NaN for plotting
diff2 = mat_speed_with2 - mat_speed_without2;
diff2(abs(diff2)<eps1)=NaN;

xx = 9;
yy = 10;

figure(1); clf
hh = gcf;
set(hh,'PaperUnits','centimeters');
set(hh,'Units','centimeters');
set(gcf,'Position',[xpos ypos width height])



colormap(mymap)

axes('position',[sx sy+dy+he w1 he])
    imagesc(flipud(mat_speed_with2),[0 0.45])
    %axis square
    colorbar
    set(gca,'XTick',1:xx:length(muvec))
    set(gca,'XTickLabel',muvec(1:xx:end))
    set(gca,'YTick',1:yy:length(avec2))
    set(gca,'YTickLabel',avec2(1:yy:end))
    title('speed (with)')
    ylabel('Allee threshold','fontsize',fs1)
    xlabel('dispersal mortality','fontsize',fs1)
    set(gca,'FontSize',fs3,'LineWidth',lw2,'Fontname', 'Arial');
    
axes('position',[sx sy w1 he])
    imagesc(flipud(mat_speed_without2),[0 0.45])
    %axis square
    colorbar
    set(gca,'XTick',1:xx:length(muvec))
    set(gca,'XTickLabel',muvec(1:xx:end))
    set(gca,'YTick',1:yy:length(avec2))
    set(gca,'YTickLabel',avec2(1:yy:end))
    title('speed (without)')
    ylabel('Allee threshold','fontsize',fs1)
    xlabel('dispersal mortality','fontsize',fs1)
    set(gca,'FontSize',fs3,'LineWidth',lw2,'Fontname', 'Arial');
    
axes('position',[sx+dx+w1 sy+dy+he w1 he])
    imagesc(flipud(diff2),[-0.15 0.15])
    %axis square
    colorbar
    set(gca,'XTick',1:xx:length(muvec))
    set(gca,'XTickLabel',muvec(1:xx:end))
    set(gca,'YTick',1:yy:length(avec2))
    set(gca,'YTickLabel',avec2(1:yy:end))
    title('difference')
    ylabel('Allee threshold','fontsize',fs1)
    xlabel('dispersal mortality','fontsize',fs1)
    set(gca,'FontSize',fs3,'LineWidth',lw2,'Fontname', 'Arial');

axes('position',[sx+dx+w1 sy w2 he])
    imagesc(flipud(1*(diff2<0) + 2*(diff2>=0)),[0 2])
    %axis square
    set(gca,'XTick',1:xx:length(muvec))
    set(gca,'XTickLabel',muvec(1:xx:end))
    set(gca,'YTick',1:yy:length(avec2))
    set(gca,'YTickLabel',avec2(1:yy:end))
    %title('effect of spatial sorting')
    ylabel('Allee threshold','fontsize',fs1)
    xlabel('dispersal mortality','fontsize',fs1)
    set(gca,'FontSize',fs3,'LineWidth',lw2,'Fontname', 'Arial');

% label subpanels
axes('position',[0 0 1 1],'visible','off')
hold on
     text(0.02,0.97,'a)')
     text(0.02,0.44,'b)')
     text(0.52,0.97,'c)')
     text(0.52,0.44,'d)')
     %
     text(0.70,0.25,'slower','rotation',-45)
     text(0.655,0.20,'faster','rotation',-45)
     set(gca,'FontSize',fs3,'Fontname', 'Arial');
axis([0 1 0 1])

% Backup previous settings
prePaperType = get(hh,'PaperType');
prePaperPosition = get(hh,'PaperPosition');
prePaperSize = get(hh,'PaperSize');

% Make changing paper type possible
set(hh,'PaperType','<custom>');
% Set the page size and position to match the figure's dimensions
position = get(hh,'Position');
set(hh,'PaperPosition',[0,0,position(3:4)]);
set(hh,'PaperSize',position(3:4));

%print -dpdf -r600 Figure1.pdf
print -djpeg -r600 fig4.jpg
print -depsc fig4.eps

% Restore the previous settings
set(hh,'PaperType',prePaperType);
set(hh,'PaperPosition',prePaperPosition);
set(hh,'PaperSize',prePaperSize);
