load oxygenplot_alldata



z = [0 , 10, 20, 30, 50, 75, 100, 125, 150, 200, 500, 1000];
level = {'_z0m' '_z10m' '_z20m' '_z30m' '_z50m' '_z75m' '_z100m' '_z125m' '_z150m' '_z200m' '_z500m' '_z1000m'};
for i = 1: 12
  levelt = level{i};
   oxx=ox;
   latt=lat;
   lonn=lon;
   monthss=months;
   
   
  bad1=find(depth1 <z(i)-2 | depth1 >z(i)+2 | depth1<0);
  
  oxx(bad1)=[];
  latt(bad1)=[];
  lonn(bad1)=[];
  monthss(bad1)=[];
  
  
  bad2 =find(depth2 ~= z(i));
  oxx(bad2)=[];
  latt(bad2)=[];
  lonn(bad2)=[];
  monthss(bad2)=[];
  



 
     delta = 1;
     latmin = 30;
     latmax = 50;
     lonmin = -80;
     lonmax = -60;
     imax = ceil((lonmax-lonmin)/delta);
     jmax = ceil((latmax-latmin)/delta);
     bimax = imax;
     bjmax = jmax;

     lattrans = (latt - latmin)/(latmax - latmin);
     latind = ceil(jmax*lattrans);
     lontrans = (lonn - lonmin)/(lonmax - lonmin);
     lonind = ceil(imax*lontrans);
  
     bin = cell(12, bimax, bjmax);

     oxmean = nan(12, bimax, bjmax);
     oxstd = nan(12, bimax, bjmax);
     oxcount = nan(12, bimax, bjmax);  
     oxcountnew = nan(12, bimax, bjmax);


    for m = 1:12
   


        for bi = 1:bimax;
            for bj = 1:bjmax;
                bin{m, bi, bj} = find(latind == bi & lonind == bj & monthss == m & oxx >=0 & oxx < 600);
                oxmean(m, bi, bj) = mean(ox(bin{m, bi, bj}));
                oxstd(m, bi, bj) = std(ox(bin{m, bi, bj}));
                oxcount(m, bi, bj) = length(bin{m, bi, bj});
                oxcountnew(m, bi,bj)= change(oxcount(m, bi, bj), '==', 0, nan );
            end
        end
    end



% 4. make the plot of mean concentration of oxygen for different month

longrid = linspace(lonmin+delta/2,lonmax-delta/2,20); %names variable to be used for plotting
latgrid = linspace(latmin+delta/2,latmax-delta/2,20); %names variable to be used for plotting
for m = 1:12
    figure
    %subplot(4,3,m), 
    pcolorjw(longrid, latgrid, squeeze(oxstd(m,:,:)))  %subplots surf. mean O2 values of each month
    pnc
    colorbar
    caxis([0,200])
    han = colorbar;
    set(han , 'pos',[0.3 0.55 0.02 0.35])
   
    month = {'January' 'February' 'March' 'April' 'May' 'June' 'July' 'August' 'September'...
    'October' 'November' 'December'};
    title(['Monthly standard deviation [O_2] mmol/m^3 in' ' ', month{m}])
    xlabel('Longitude (^oE)')
    ylabel('Latitude (^oN)')
    monthnum = {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12'};
    
    base = ['oxygenplot/monthly/standard deviation/1degree/oxyg_1deg_mon' '' monthnum{m} ];
    print(gcf, '-dpng', [base levelt]);
    
end

end