% Average ekman data

clear
%define desired region
nx=2160;
region_name='DopplerScat/subpolar';
% subtropic size = [72 81 27]
% subpolar size = [72 109 27]
% southern size = [72 119 27];
% read in the data

pin=['/nobackupp9/dmenemen/llc_2160/regions/' region_name '/'];

for fnm={'U','V','Eta','oceTAUX','oceTAUY'}
    fnm{1}
    n = 0;
        fsum = 0;
    for ts=92160+((85+60)*80*24):(80*24):92160+((85+90)*80*24);
     %winter %ts=92160+((90+6*30)*80*24):(80*24):92160+((90+6*30+90)*80*24);
      
        fin=[pin fnm{1} '.' myint2str(ts,10) '.data'];
        dy=ts2dte(ts,45,2011,1,17,30);

        if strcmp(fnm{1},'Eta') || strcmp(fnm{1},'oceTAUX') || strcmp(fnm{1},'oceTAUY')
            siz = [72 109 1];
            if strcmp(fnm{1},'Eta')
                fnam = [pin fnm{1} '/' fnm{1} '_' num2str(siz(1)) 'x' num2str(siz(2)) 'x' num2str(27) '.' dy];
            else
                fnam = [pin fnm{1} '/' fnm{1} '_' num2str(siz(1)) 'x' num2str(siz(2)) '.' dy];
            end
            
            fld = readbin(fnam,siz);
            if sum(sum(fld)) ~= 0;
                fsum = fsum + fld;
                n = n + 1;
            end
        else
            
            siz = [72 109 27];
             fnam = [pin fnm{1} '/' fnm{1} '_' num2str(siz(1)) 'x' num2str(siz(2)) 'x' num2str(siz(3)) '.' dy];
      
            fld = readbin(fnam,siz);
            if sum(sum(sum(fld))) ~= 0
                fsum = fsum + fld;
                n = n + 1;
            end
        end
        
    end
    
    favg = fsum./n;
    writebin(['/nobackupp9/dmenemen/llc_2160/regions/' region_name '/averages_summer/' fnm{1} '_augAVG'],favg);
end
