% 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={'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=10368:(144*24):485568 
        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; n2 = 0;
        for h=0:24 -1 %, mydisp(h)
            fin=[pin fnm{1} '.' myint2str(ts+h*144,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
                if sum(sum(tmp)) == 0
                    warning(['ZEROs file ' fin])
                    break
                else
                    fld=fld+tmp;
                    fld_mn = fld_mn + tmp.^2;
                    n=n+1;
                end
            else
                disp(['Missing file ' fin])
                break
            end
            
            if strcmp(fnm{1},'V') || strcmp(fnm{1},'U')
                fin2=[pin fnm2 '.' myint2str(ts+h*144,10) '.data'];
                if exist(fin2)
                    tmp2 = readbin(fin2,[nx nx*13]);
                    if sum(sum(tmp2)) == 0
                        warning(['ZEROs file ' fin2])
                        break
                    else
                        fld2=fld2+tmp2;
                        fld_mn2 = fld_mn2 + tmp2.^2;
                        pwr = pwr + tmp.*tmp2;
                        n2 = n2 +1;
                    end
                else
                    disp(['Missing file' fin2])
                    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);
%         end
        
        if n2 == 24 && (strcmp(fnm,'V') || strcmp(fnm,'U'))
            
            writebin([pout fnm2 '_daily/' fout2],fld2/n2);
            fout2_msq = [pout fnm2 '_mnsq_daily/' fnm2 '_mnsq_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
            writebin(fout2_msq,fld_mn2/n2);
            
            fout_pwr = [pout fnm{1} '_' fnm2 '_daily/' fnm{1} '_' fnm2 '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
            writebin(fout_pwr,pwr/n2);
        end
        
        
    end
    
end
