% define desired region
region_name='global';
nx=2160;

% get and save daily-averaged regional fields
pn='~dmenemen/llc_2160/MITgcm/';
pout=['~dmenemen/llc_2160/regions/' region_name '/'];
for fnm={'Eta','PhiBot','SIarea','SST','U','V'}
    eval(['mkdir ' pout fnm{1} '_daily'])
    eval(['mkdir ' pout fnm{1} '_mnsq_daily'])
    eval(['cd ' pout fnm{1} '_daily'])
    
    if strcmp(fnm,'U')
        fnm2 = 'oceTAUX';
        eval(['mkdir ' pout fnm2 '_daily'])
        eval(['mkdir ' pout fnm2 '_mnsq_daily'])
        eval(['mkdir ' pout fnm{1} '_' fnm2 '_daily'])
    elseif strcmp(fnm,'V')
        fnm2 = 'oceTAUY';
        eval(['mkdir ' pout fnm2 '_daily'])
        eval(['mkdir ' pout fnm2 '_mnsq_daily'])
        eval(['mkdir ' pout fnm{1} '_' fnm2 '_daily'])
    end
   
    for  ts=92160:(80*24):1586400;
         
         if (ts<1198080)
            pin=[pn 'run_day49_624/'];
         else
            pin=[pn 'run/'];
         end
        
        dy=ts2dte(ts,45,2011,1,17,30);
        
        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:23 %, mydisp(h)
            fin=[pin fnm{1} '.' myint2str(ts,10) '.data'];
            
            if exist(fin)
                tmp = readbin(fin,[nx nx*13]);
                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
            
            writebin([pout fnm{1} '_daily/' fout],fld./n);
            fout_msq = [pout fnm{1} '_mnsq_daily/' 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 '_daily/' fout2],fld2/n);
                fout2_msq = [pout fnm2 '_mnsq_daily/' fnm2 '_mnsq_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
                writebin(fout2_msq,fld_mn2/n);

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