function CHL_WOD01 = extract_bin(datetime_wod01, dep_wod01, bindepths, chl_wod01)
%
% finds all chl data from WOD01 data for a given month and depth and calculates median
% depths (bindepths) provided in plot_osd.m - can adjust
%
[yy,month,dd] = datevec(datetime_wod01); %converts date string to date vector
uyear = unique(yy); %finds each year (once) in dataset and stores in array uyear
CHL_WOD01 = []; %initializes array CHL_WOD01
for ky = 1:length(uyear) %for each year in dataset
    umonth = unique(month(yy==uyear(ky))); %finds each month (once) in each year
    for km = 1:length(umonth) %for each month in each year in each dataset
        midx = yy==uyear(ky) & month==umonth(km); %where year AND month exist (create logical array)
        chl_wod01_midx = chl_wod01(midx); %create array chl_midx of chl data at midx = true
        dep_midx = dep_wod01(midx); %create array dep_midx of depth data at midx = true
        %disp([uyear(ky) umonth(km)]);
        thisrow = [uyear(ky) umonth(km) nan(1,2*(length(bindepths)-1))]; %make array thisrow of yr, mo, NaNs for each bindepth to fill w/chl and st dev
        for kb = 1:(length(bindepths)-1) %for all bindepths
            found = dep_midx>=bindepths(kb) & dep_midx<bindepths(kb+1); %where dep falls within bindepth range (logical array created)
            thisrow(2+kb) = nanmedian(chl_wod01_midx(found)); %skip yr, mo cols of array thisrow and add median chl for bindepth range to next cols of row kb
            thisrow(2+length(bindepths)-1+kb) = nanstd(chl_wod01_midx(found)); %skip yr, mo, chl cols and add std to next cols of row kb
        end
        CHL_WOD01 = [CHL_WOD01; thisrow]; %append new row to big array CHL_WOD01 (see plot.osd.m for CHL_WOD01 contents)
    end
end
return
