%Be sure to import MB_CTDData2.txt before running this script

close all

depthIndex = 3;
tempIndex = 4;
salinityIndex = 5;
sigmaThetaIndex = 6;
sigmaIndex = 7;

depthInc = 10.0;
j = 1;
for i = 0:depthInc:4000
   % depth(j) = -i - (depthInc / 2);
    ind=find(data(:,depthIndex)>=i & data(:,depthIndex) < i+depthInc); 
    depth(j) = -1 * mean(data(ind,depthIndex));
    temperature{j} = data(ind,tempIndex);
     if length(temperature{j} > 0)
        tempMean(j) = mean(temperature{j});
        tempStdDev(j) = std(temperature{j});
        tempMin(j) = min(temperature{j});
        tempMax(j) = max(temperature{j});
        sortTemp = sort(temperature{j});
        if length(sortTemp > 11);
            len = length(sortTemp);
            temp10(j) = sortTemp(ceil(0.05*len));
            temp90(j) = sortTemp(floor(.95*len));
        else
            temp10(j) = NaN;
            temp90(j) = NaN;
        end
     else
        tempMean(j) = NaN;
        tempStdDev(j) = NaN;
        tempMin(j) = NaN;
        tempMax(j) = NaN;
        temp10(j) = NaN;
        temp90(j) = NaN;
    end   

    salinity{j} = data(ind,salinityIndex);
    if length(salinity{j} > 0)
        salMean(j) = mean(salinity{j});
        salStdDev(j) = std(salinity{j});
        salMin(j) = min(salinity{j});
        salMax(j) = max(salinity{j});
        sortSal = sort(salinity{j});
        if length(sortSal > 11);
            len = length(sortSal);
            sal10(j) = sortSal(ceil(0.05*len));
            sal90(j) = sortSal(floor(.95*len));
        else
            sal10(j) = NaN;
            sal90(j) = NaN;
       end
    else
        salMean(j) = NaN;
        salStdDev(j) = NaN;
        salMin(j) = NaN;
        salMax(j) = NaN;
        sal10(j) = NaN;
        sal90(j) = NaN;
    end   
  
    sigmaTheta{j} = data(ind,sigmaThetaIndex);
    if length(sigmaTheta{j} > 0)
        STMean(j) = mean(sigmaTheta{j});
        STStdDev(j) = std(sigmaTheta{j});
        STMin(j) = min(sigmaTheta{j});
        STMax(j) = max(sigmaTheta{j});
        sortST = sort(sigmaTheta{j});
        if length(sortST > 11);
            len = length(sortST);
            ST10(j) = sortST(ceil(0.05*len));
            ST90(j) = sortST(floor(.95*len));
        else
            ST10(j) = NaN;
            ST90(j) = NaN;
        end
    else
        STMean(j) = NaN;
        STStdDev(j) = NaN;
        STMin(j) = NaN;
        STMax(j) = NaN;
        ST10(j) = NaN;
        ST90(j) = NaN;
    end   
    
    sigma{j} = data(ind,sigmaIndex);
    if length(sigma{j} > 0)
        sigmaMean(j) = mean(sigma{j});
        sigmaStdDev(j) = std(sigma{j});
        sigmaMin(j) = min(sigma{j});
        sigmaMax(j) = max(sigma{j});
        sortsigma = sort(sigma{j});
        if length(sortsigma > 11);
            len = length(sortsigma);
            sigma10(j) = sortsigma(ceil(0.05*len));
            sigma90(j) = sortsigma(floor(.95*len));
        else
            sigma10(j) = NaN;
            sigma90(j) = NaN;
       end
    else
        sigmaMean(j) = NaN;
        sigmaStdDev(j) = NaN;
        sigmaMin(j) = NaN;
        sigmaMax(j) = NaN;
        sigma10(j) = NaN;
        sigma90(j) = NaN;
    end   

    lengths(j) = length(salinity{j});
    
    j = j+1;
end
plot(tempMean,depth,temp10,depth,temp90,depth);
title(strcat('Temperature vs Depth (mean, 0.05 & 0.95 percentiles, MBARI CTD Data Base 2009-2012 col=',textdata(tempIndex),')') );
ax = axis;
ax(3) = -1200;
axis(ax);
figure
plot(salMean,depth,sal10,depth,sal90,depth);
title(strcat('Salinity vs Depth (mean, 0.05 & 0.95 percentiles, CTD Data Base 2009-2012 col=',textdata(salinityIndex),')') )
ax = axis;
ax(3) = -1200;
axis(ax);
figure
plot(STMean,depth,ST10,depth,ST90,depth);
title(strcat('Sigma Theta vs Depth (mean, 0.05 & 0.95 percentiles, MBARI CTD Data Base 2009-2012 col=',textdata(sigmaThetaIndex),')') )
ax = axis;
ax(3) = -1200;
axis(ax);
figure
plot(sigmaMean,depth,sigma10,depth,sigma90,depth);
title(strcat('Sigma vs Depth (mean, 0.05 & 0.95 percentiles, MBARI CTD Data Base 2009-2012 col=',textdata(sigmaIndex),')') )
ax = axis;
ax(3) = -1200;
axis(ax);
    