% define desired region
region_name='global_1080_test_7d';
nx=1080;
%ham =     [0.0021    0.0033    0.0063    0.0116    0.0195    0.0299    0.0423    0.0556    0.0686    0.0799    0.0883    0.0927 ...
    %0.0927    0.0883    0.0799    0.0686    0.0556    0.0423    0.0299    0.0195    0.0116    0.0063    0.0033    0.0021];
d = 7;
    
    ham = ones(1,24*d)./(24*d);

% get and save daily-averaged regional fields
pin='~dmenemen/llc_1080/MITgcm/run_day732_896/';
pout=['/nobackupp8/awinetee/' region_name '/'];
for fnm={'U','V'}
    eval(['mkdir ' pout fnm{1} '_30hLPF_hamming_daily'])
    
    eval(['cd ' pout fnm{1} '_30hLPF_hamming_daily'])
    
    if strcmp(fnm,'U')
        fnm2 = 'oceTAUX';
        eval(['mkdir ' pout fnm2 '_30hLPF_hamming_daily'])
        
        eval(['mkdir ' pout fnm{1} '_' fnm2 '_30hLPF_hamming_daily'])
    elseif strcmp(fnm,'V')
        fnm2 = 'oceTAUY';
        eval(['mkdir ' pout fnm2 '_30hLPF_hamming_daily'])
        
        eval(['mkdir ' pout fnm{1} '_' fnm2 '_30hLPF_hamming_daily'])
    end
   
     for ts=700800:(40*24*d):700800+(40*24*5)*30 
        
            
        dy=ts2dte(ts,90,2010,1,1)      
        
        if strcmp(fnm,'U')
            fout2 = [fnm2 '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
        elseif strcmp(fnm,'V')
            fout2 = [fnm2 '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
        end
                  
        fout=[fnm{1} '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];

       
        
        fld=0; n=0; fld2 = 0; pwr = 0; fld_mn = 0; fld_mn2 = 0; n2 = 0;
        
        for h=0:24*d-1 %, mydisp(h)
            fin=[pin fnm{1} '.' myint2str(ts+h*40,10) '.data'];
            
            if exist(fin)
                tmp = readbin(fin,[nx nx*13]);
                if sum(sum(tmp)) == 0
                    warning(['ZEROs FILE ' fin])
                    break
                else
                    fld=fld+ham(h+1).*tmp;
                    
                    n=n+1;
                end
            else
                warning(['MISSING FILE ' fin])
                break
            end
            
            if strcmp(fnm{1},'V') || strcmp(fnm{1},'U')
                fin2=[pin fnm2 '.' myint2str(ts+h*40,10) '.data'];
                if exist(fin2)
                    tmp2 = readbin(fin2,[nx nx*13]);
                    if sum(sum(tmp2)) == 0
                        warning(['ZEROs FILE ' fin2])
                        break
                    else
                        fld2=fld2+ham(h+1).*tmp2;
                        
                        pwr = pwr + ham(h+1).*tmp.*tmp2;
                        n2 = n2 +1;
                    end
                else
                    disp(['MISSING FILE ' fin2])
                    break;
                end
            end

        end
        if n==24*d
            
            writebin([pout fnm{1} '_30hLPF_hamming_daily/' fout],fld);
           
        end
        if n2 == 24*d && (strcmp(fnm,'V') || strcmp(fnm,'U'))
            
            writebin([pout fnm2 '_30hLPF_hamming_daily/' fout2],fld2);
            
            fout_pwr = [pout fnm{1} '_' fnm2 '_30hLPF_hamming_daily/' fnm{1} '_' fnm2 '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
            writebin(fout_pwr,pwr);
        end
        
    end
end
