function [diel, SDNs] = parse_to_diel(SDN, Y, n)

% [diel] = parse_to_diel(SDN, Y, hours)

% Parses data from a time series into a "diel" cycle with frequency given
% by hours. This function returns a m x n matrix, where m is the number of
% days in the time series data Y, and n is 24/hours. 
%
% This function will split a day into n intervals. 

% Author: Yui Takeshita
% Scripps Institution of Oceanography
% Created: 10/23/2014

% presize matrix


ndays = ceil(nanmax(SDN)) - floor(nanmin(SDN));

% Make vector that will find closest points extract 
SDN_ = floor(nanmin(SDN)):1/n:ceil(nanmax(SDN));

% array for SDN to identify which day it is
SDNs = SDN_(1):1:SDN_(end-1);

trex = NaN(size(SDN_));

for i = 1:length(SDN_)
    iavg = SDN < SDN_(i) + 1/(2*n) & SDN >= SDN_(i) - 1/(2*n);
    trex(i) = nanmean(Y(iavg));
end

% diel = vec2mat(trex(1:end-1), n);

diel = reshape(trex(1:end-1), [n, ndays])';












