%% Average a and b coefficients 
% 
%  
%%

function ab_step2;

% load all fi files from 1997 to 2009
i=0;
    for year = 1997:2009
      i=i+1;
      name = ['ab_' num2str(year)];
      eval (['load(''' name ''');']);
      eval (['a_years(:,:,' num2str(i) ') = ' name '.a;']);
      eval (['b_years(:,:,' num2str(i) ') = ' name '.b;']);
    end;  
% avarage    
   a_avr = nanmean(a_years,3);
   b_avr = nanmean(b_years,3);

   c_avr = a_avr;
     c_e = [c_avr c_avr c_avr ; c_avr c_avr c_avr ; c_avr c_avr c_avr];
     c_ex(:,:,1)= c_e;
     c_ex(:,:,2)= c_e;
     c_ex(:,:,3)= c_e;
     c_ex(:,:,4)= c_e;
     c_ex(:,:,5)= c_e;
     c_s = smooth3(c_ex,'gaussian',7,0.8);%7
     c_n = smooth3(c_s,'box',5);%5
     c_2d = c_n(13:12*2,289:288*2,3);
     ab_2d.a = c_2d;

   c_avr = b_avr;
     c_e = [c_avr c_avr c_avr ; c_avr c_avr c_avr ; c_avr c_avr c_avr];
     c_ex(:,:,1)= c_e;
     c_ex(:,:,2)= c_e;
     c_ex(:,:,3)= c_e;
     c_ex(:,:,4)= c_e;
     c_ex(:,:,5)= c_e;
     c_s = smooth3(c_ex,'gaussian',9,0.1);%7
     c_n = smooth3(c_s,'box',7);%5
     c_2d = c_n(13:12*2,289:288*2,3);
     ab_2d.b = c_2d;
     
 subplot(2,1,1);    
 contourf(ab_2d.b,12); 
 subplot(2,1,2);    
 contourf(ab_2d.a,12);  
 

 ab_year.a = coeff_for_year(ab_2d.a)';
 ab_year.b = coeff_for_year(ab_2d.b)';
 ab_year.a=smooth(ab_year.a,24,'lowess');
 ab_year.b=smooth(ab_year.b,24,'lowess');
 
 save('ab_2d.mat','ab_2d');
 save('ab_year.mat','ab_year');
 
   