function[variances,means] = calcVar_MLH(mlh,time,range)
%This function computes the horizontal variance of the mixed layer heights for a given range.

% Inputs:
% - mlh: A vector of mixed layer heights to use as reference
% - time: A vector of times for the data 
% - range: A range of times by which to compute the horizontal variance
% (e.g., 5,10,15,20)

% Outputs:
% - variances: The vertical variance of the given altitude range. Same size
% as the original mlh cell array given. 


variances = cell(size(mlh));
means = cell(size(mlh));
for vector = 1:length(mlh)
    mlh_data = mlh{vector};
    time_data = time{vector};
    variances{vector} = NaN(1, length(time_data));
    means{vector} = NaN(1, length(time_data));
    
    for i = range+1:length(time_data)-range
        i_start = i - range;
        i_end = i + range;
        
        if i_start < 1 || i_end > length(mlh_data)
            continue;
        else
            mlh_range = mlh_data(i_start:i_end);
            mlh_variance = var(mlh_range, 'omitnan');
            mlh_mean = mean(mlh_range, 'omitnan');
        end
        
        variances{vector}(i) = mlh_variance;
        means{vector}(i) = mlh_mean;
    end
end




end 

