% define desired region
clear
region_name='global';
nx=4320;
%'PhiBot','SIarea','SST',
% get and save daily-averaged regional fields
pin='~dmenemen/llc_4320/MITgcm/run/';
pout=['~dmenemen/llc_4320/regions/' region_name '/'];
for fnm={'Eta'}
    eval(['mkdir ' pout fnm{1} '_9day'])
    eval(['mkdir ' pout fnm{1} '_mnsq_9day'])
    eval(['cd ' pout fnm{1} '_9day'])
    
    if strcmp(fnm,'U')
        fnm2 = 'oceTAUX';
        eval(['mkdir ' pout fnm2 '_9day'])
        eval(['mkdir ' pout fnm2 '_mnsq_9day'])
        eval(['mkdir ' pout fnm{1} '_' fnm2 '_9day'])
    elseif strcmp(fnm,'V')
        fnm2 = 'oceTAUY';
        eval(['mkdir ' pout fnm2 '_9day'])
        eval(['mkdir ' pout fnm2 '_mnsq_9day'])
        eval(['mkdir ' pout fnm{1} '_' fnm2 '_9day'])
    end
    %10368 %
    for ts=152064% :(144*72): 485568 % note this starting point is due to a readbin error on phibot. Change back later.
        dy=ts2dte(ts,25,2011,9,10,29);
        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];

        if mod(ts,144*24*10) 
            disp(fout);
        end
        
        fld=0; n=0; fld2 = 0; pwr = 0; fld_mn = 0; fld_mn2 = 0;
        for h=0:24*9 -1 %, mydisp(h)
            fin=[pin fnm{1} '.' myint2str(ts,10) '.data'];
              
            if exist(fin)
                try
                tmp = readbin(fin,[nx nx*13]);
                catch err
                    disp('Warning: readbin failed, skipping one day');
                    disp(err);
                    break;
                end
                fld=fld+tmp;
                fld_mn = fld_mn + tmp.^2;
                n=n+1;
            else
                break
            end
            
            if strcmp(fnm{1},'V') || strcmp(fnm{1},'U')
                fin2=[pin fnm2 '.' myint2str(ts,10) '.data'];
                if exist(fin2)
                    tmp2 = readbin(fin2,[nx nx*13]);
                    fld2=fld2+tmp2;
                    fld_mn2 = fld_mn2 + tmp2.^2;
                    pwr = pwr + tmp.*tmp2;
                else
                    break;
                end
            end

        end
        if n==24*9
            
            writebin([pout fnm{1} '_9day/' fout],fld./n);
            fout_msq = [pout fnm{1} '_mnsq_9day/' fnm{1} '_mnsq_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
            writebin(fout_msq,fld_mn./n);
            
            if strcmp(fnm,'V') || strcmp(fnm,'U')

                writebin([pout fnm2 '_9day/' fout2],fld2/n);
                fout2_msq = [pout fnm2 '_mnsq_9day/' fnm2 '_mnsq_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
                writebin(fout2_msq,fld_mn2/n);

                fout_pwr = [pout fnm{1} '_' fnm2 '_9day/' fnm{1} '_' fnm2 '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
                writebin(fout_pwr,pwr/n);
            end
            
        else
            continue
        end
    end
    
end
