%simple daily mean example
%author: Oleksandr (Sasha) Huziy


of1 = importdata('ORLANDO.OF1');


YEAR_INDEX = 2;
DAY_INDEX = 1;
QH_INDEX = 5;
QE_INDEX = 6;


qh = of1.data(:,QH_INDEX);
qe = of1.data(:,QE_INDEX);
day = of1.data(:, DAY_INDEX);



disp(['processing ' of1.colheaders(QH_INDEX)]);

daily_means_qh = zeros(365,1);
daily_means_qe = zeros(365,1);

for d = 1:365
    selection = day == d;
    daily_means_qh(d) = mean(qh(selection));
    daily_means_qe(d) = mean(qe(selection));
end
    

%plot results
figure
    hold on
    grid on
    
    
    plot(1:365, daily_means_qh, 'b')
    plot(1:365, daily_means_qe, 'r')
    
    legend('QH', 'QE')
    %put months on x-axis
    %datetick('x','dd-mmm','keepticks')
    hold off
    
    
%% diurnal cycle example

    
of6 = importdata('ORLANDO.OF6');

%1-winter, 2-spring, 3-summer, 4-autumn
season_names = { 'winter', 'spring', 'summer', 'fall' };
interest_season = 1;


HOUR_INDEX = 1;
YEAR_INDEX = 4;
DAY_INDEX = 3;
PCP_INDEX = 7;
TEMP_AIR_INDEX = 8;
MINUTE_INDEX = 2;



pcp = of6.data(:, PCP_INDEX);
ta = of6.data(:, TEMP_AIR_INDEX);
day = of6.data(:, DAY_INDEX);
year = of6.data(:, YEAR_INDEX);
hour = of6.data(:, HOUR_INDEX);
minute = of6.data(:, MINUTE_INDEX);


%determine date of each record
dates = arrayfun(@(y,d) datevec(addtodate( datenum(y,1,1), d, 'day')), year, day - 1, 'UniformOutput', false);
months = arrayfun(@(d) d{1}(2), dates); %find month for each record

seasons = months(:);
seasons(seasons == 12) = 0;
seasons = floor(seasons / 3) + 1;





pcp_diurn = zeros(2*24, 1);
ta_diurn = zeros(2*24, 1);



index = 1;
for h = 1:24
    for mins = [0 30]
        selection = (seasons == interest_season) & (hour == h) & (minute == mins);
        pcp_diurn(index) = mean(pcp(selection));
        ta_diurn(index) = mean(ta(selection));
        index = index + 1;
    end
end

%plot results
figure
    hold on
    grid on
    
    
    plot(0:0.5:23.5, ta_diurn, 'r')
    
    legend('Air temp')
    title(['Diurnal cycle ' season_names(interest_season)])
    %put months on x-axis
    %datetick('x','dd-mmm','keepticks')
    hold off
    

figure
    hold on
    grid on
    
    
    
    plot(0:0.5:23.5, pcp_diurn, 'b')
    
    legend('precip')
    title(['Diurnal cycle ' season_names(interest_season)])
    %put months on x-axis
    %datetick('x','dd-mmm','keepticks')
    hold off
    


    
    
 


