%create table 1
clear
load grzeronobatsnoland.mat % no size threshold, remove bats, no land mask




%imagesc((((HR)./(N))))./(log10(((HR+HRmega)./(N+Nummega)))))
Dmegaz =(((Dismov+Dismovmega)./(N+Nummega)));
Dz =(((Dismov)./(N)));
Dz(find(Dz>0.1064 & Dz<0.1065))=NaN;
Dmegaz(find(Dmegaz>0.1064 & Dmegaz<0.1065))=NaN;
% africa, 30km2
    v=zz2;
    v(find(v==.18))=1;
    v(find(v<1))=NaN;
    QZmAF = (v.*Dmegaz);
    QZmAFz = (v.*Dz);
    
     % austraulia, 7.7km2
    v=zz2;
    v(find(v==.88))=1;
    v(find(v<1))=NaN;
    QZmAU = (v.*Dmegaz);
    QZmAUz = (v.*Dz);
    
    
    % eurasia, 53km2
    v=zz2;
    v(find(v==.36))=1;
    v(find(v<1))=NaN;
    QZmEU = (v.*Dmegaz);
    QZmEUz = (v.*Dz); 
    
    % north america, 24.7km2
    v=zz2;
    v(find(v==.72))=1;
    v(find(v<1))=NaN;
    QZmNA = (v.*Dmegaz);
    QZmNAz = (v.*Dz);
    
     % south america, 17.8km2
    v=zz2;
    v(find(v==.84))=1;
    v(find(v<1))=NaN;
    QZmSA = (v.*Dmegaz);
    QZmSAz = (v.*Dz);

    wdmega = [nanmean(QZmNA(:)) nanmean(QZmSA(:)) nanmean(QZmAU(:)) nanmean(QZmEU(:)) nanmean(QZmAF(:))   nanmean(Dmegaz(:)) ];
    wd = [nanmean(QZmNAz(:)) nanmean(QZmSAz(:)) nanmean(QZmAUz(:)) nanmean(QZmEUz(:)) nanmean(QZmAFz(:))   nanmean(Dz(:)) ];
    wd2 = wdmega./wd;
    zzz=[wdmega; wd; wd2];
    
Dmega =(((HR+HRmega)./(N+Nummega)));
D =(((HR)./(N)));
D(find(D>0.2058 & D<0.2059))=NaN;
Dmega(find(Dmega>0.2058 & Dmega<0.2059))=NaN;
% africa, 30km2
    v=zz2;
    v(find(v==.18))=1;
    v(find(v<1))=NaN;
    QZmAF1 = (v.*Dmega);
    QZmAFz1 = (v.*D);
    
     % austraulia, 7.7km2
    v=zz2;
    v(find(v==.88))=1;
    v(find(v<1))=NaN;
    QZmAU1 = (v.*Dmega);
    QZmAUz1 = (v.*D);
    
    
    % eurasia, 53km2
    v=zz2;
    v(find(v==.36))=1;
    v(find(v<1))=NaN;
    QZmEU1 = (v.*Dmega);
    QZmEUz1 = (v.*D); 
    
    % north america, 24.7km2
    v=zz2;
    v(find(v==.72))=1;
    v(find(v<1))=NaN;
    QZmNA1 = (v.*Dmega);
    QZmNAz1 = (v.*D);
    
     % south america, 17.8km2
    v=zz2;
    v(find(v==.84))=1;
    v(find(v<1))=NaN;
    QZmSA1 = (v.*Dmega);
    QZmSAz1 = (v.*D);
    
    wdmega1 = [nanmean(QZmNA1(:)) nanmean(QZmSA1(:)) nanmean(QZmAU1(:)) nanmean(QZmEU1(:)) nanmean(QZmAF1(:))   nanmean(Dmega(:)) ];
    wd1 = [nanmean(QZmNAz1(:)) nanmean(QZmSAz1(:)) nanmean(QZmAUz1(:)) nanmean(QZmEUz1(:)) nanmean(QZmAFz1(:))   nanmean(D(:)) ];
    wd3 = wdmega1./wd1;
    zzz1=[wdmega1; wd1; wd3];
    
    
    %calculate percentage of dispersal lost in less than 11,000 ybp
    globarea = 30+7.7+53+24.7+17.8;
    pernear = (53/3+24.7+17.8)/globarea;
    perNA = 24/globarea;
    %45% of global area went extinct 11,000ybp
    %
    dis11 = nanmean(QZmEU(:))*(53/3) + nanmean(QZmNA(:))*24.7 + nanmean(QZmSA(:))*17.8;
    dis11z = nanmean(QZmEUz(:))*(53/3) + nanmean(QZmNAz(:))*24.7 + nanmean(QZmSAz(:))*17.8;
    dis11z./(dis11+dis11z);
    %global value
    disallz = Dz./Dmegaz;
    
    %table that goes in the paper
    table1 = round([zzz1; zzz],1);
%xxx = (((Dismov+Dismovmega)./(N+Nummega)))./(((Dismov)./(N)));
%xxx = (((Dismov)./(N)));
%xxx(find(xxx>0.1064 & xxx<0.1065))=NaN;   
 xxx = ((HR+HRmega)./(N+Nummega))./(HR./N);
 xxx = ((HR+HRmega)./(N+Nummega));
% xxx = (HR./N);
% xxx(find(xxx>0.2058 & xxx<0.2059))=NaN;
%xxx = Psummega;
%xxx = log10(QDboth);
xxx2 = [xxx(:,181:end) xxx(:,1:180)];
geoidR = makerefmat('RasterSize', size(xxx2), 'Latlim', [-90 90], 'Lonlim', [0 360]);
h1 = worldmap('world');
% manually delete long lines
land = shaperead('landareas.shp', 'UseGeoCoords', true);

%geoshow('landareas.shp', 'FaceColor', [0.5 1.0 0.5]);
axis off
geoshow(flipud(xxx2), geoidR, 'DisplayType', 'texturemap');
geoshow([land.Lat],[land.Lon], 'Color', 'red')
colorbar('location', 'southoutside');
%title('change in vertical nutrient movement ', 'FontSize', 10);
colormapeditor