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

% get and save daily-averaged regional fields
pn='~dmenemen/llc_4320/MITgcm/';
pout=['~dmenemen/llc_4320/regions/' region_name '/'];
for fnm={'KPPhbl'}
    eval(['mkdir ' pout fnm{1} '_daily_MAX'])

    eval(['cd ' pout fnm{1} '_daily_MAX'])
    pin=[pn 'run/'];
    for  ts=10368:(144*24):485568 
        
            
        dy=ts2dte(ts,25,2011,9,10,29);       
        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:23 %, mydisp(h)
            fin=[pin fnm{1} '.' myint2str(ts+h*144,10) '.data'];
            
            if exist(fin)
                tmp = readbin(fin,[nx nx*13]);
                if sum(sum(tmp)) == 0
                    warning(['ZEROs FILE ' fin])
                    break
                else
                    fld=max(fld,tmp);
                    
                    n=n+1;
                end
            else
                warning(['MISSING FILE ' fin])
                break
            end
            
        end
        if n==24
            
            writebin([pout fnm{1} '_daily_MAX/' fout],fld);
          
        end
      
        
    end
end
