%% Find best linear fit of Ekl and H_proj
%
%
%%
function ab_regress (year);

    % load Esw file (5-min and 15 min shifted forward
    load(['ekls_' num2str(year) '.mat']);
    eval (['ekl = ekls_' num2str(year) ';']);
    %ekl = smooth(ekl,5,'rloess');
    
    % load H_proj file
    load(['Hproj_' num2str(year) '.mat']);
    eval (['H_proj = Hproj_' num2str(year) ';']);
    %H_proj = smooth(H_proj,5,'rloess');
    
    %load time file
    load('yeartime.mat');
    time_a = yeartime';
    
    
% Correlation

    for mn=1:12
        disp(mn);
        for hr=0:23
            for min =0:5:55;

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

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

                temp1 = H_proj(min5_min:min5_max)';
                temp_dt = time_a(min5_min:min5_max,:);
                temp_ace = ekl(1,min5_min:min5_max)';


                temp_pos = find(and(temp_dt(:,1)==hr,temp_dt(:,2)==min));
                tcorr_Esw = temp_ace(temp_pos);
                tcorr_Hproj = temp1(temp_pos);

                tcorr_Hproj(find(isnan(tcorr_Esw)))=[];
                tcorr_Esw(find(isnan(tcorr_Esw)))=[];
                tcorr_Esw(find(isnan(tcorr_Hproj)))=[];
                tcorr_Hproj(find(isnan(tcorr_Hproj)))=[];

                R = polyfit(tcorr_Esw,tcorr_Hproj,1);
                ab.a(mn,(hr.*12)+(min+5)/5) = R(1);
                ab.b(mn,(hr.*12)+(min+5)/5) = R(2);
                
                
            end; % for min=
        end; % for hr=
    end; % mn
    
    
% Save regression coeff to file ab_YYYY.mat

eval (['ab_' num2str(year) '=ab;']);
fn = ['ab_' num2str(year)];
eval (['save(''' fn ''', ''' fn ''');']);


