%% Find best correlation of Ekl and array of H_proj for every Fi  
%  5-min interval in the year
%  direction 0 - 355 deg with 5 deg step 
%%

function fi_corr(year,Hproj_arr);


% load  file 

load(['ekls_' num2str(year) '.mat']);
eval (['ekl = ekls_' num2str(year)]);

%load time file
  load('yeartime.mat');

%
H_proj = Hproj_arr';
time_a = yeartime';  
% Correlation

for month=1:12

    clear R;  
    for hr=0:23
        for min =0:5:55;


        day = 16+30*(month-1);
        day_min = day-15;
        day_max = day+15;

        min5 = day*288;
        min5_min = day_min*288;
        min5_max = day_max*288;
        clear tab*;
        clear temp*;

        temp1 = H_proj(min5_min:min5_max,:);
        temp_date = time_a(min5_min:min5_max,:);
        temp_ace = ekl(1,min5_min:min5_max)';
        temp_pos = find(and(temp_date(:,1)==hr,temp_date(:,2)==min));
        tabcorr_Esw = temp_ace(temp_pos);
        tabcorr_Hproj = temp1(temp_pos,:);

        %clear temp*
        tabcorr_Hproj(find(isnan(tabcorr_Esw)),:)=[];
        tabcorr_Esw(find(isnan(tabcorr_Esw)))=[];
        tabcorr_Esw(find(isnan(tabcorr_Hproj(:,1))))=[];
        tabcorr_Hproj(find(isnan(tabcorr_Hproj(:,1))),:)=[];
        %temp_ace(temp_pos);
                for gr = 1:72 
                    rr = corrcoef(tabcorr_Esw(:),tabcorr_Hproj(:,gr)); 
                    if length(rr)>1
                     R((hr.*12)+(min+5)/5,gr) = rr(1,2);
                    else
                     R((hr.*12)+(min+5)/5,gr) = NaN;
                    end    
                end; %for gr=
        end; % for min=

    end; % for hr=
    F(month,1:288) = makerr(R).*5-180; 
    disp (month);
end; % for month


% Save Fi to file F_YYYY.mat

eval (['F_' num2str(year) '=F;']);
fn = ['F_' num2str(year)];
eval (['save(''' fn ''', ''' fn ''');']);



