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

% get and save daily-averaged regional fields
pn='~dmenemen/llc_2160/MITgcm/run';
pout=['~dmenemen/llc_2160/regions/' region_name '/'];
fn1={'Salt','KPPhbl'};
fnm={'SSS_dailymean','KPPhbl_dailymax'};
for i=1:length(fnm)
    eval(['mkdir ' pout fnm{i}])
    eval(['cd ' pout fnm{i}])
    for ts=92160:80:1586400; disp(ts)
        if (ts<1198080)
            pin=[pn '_day49_624/'];
        else
            pin=[pn '/'];
        end
   for ts=10368:(144*24):485568
        dy=ts2dte(ts,25,2011,9,10,29);
        fout=[fnm{1} '_' int2str(length(nx)) 'x' int2str(length(nx*13)) '.' dy];
        disp(fout); fld=0; n=0;
        for h=0:23, mydisp(h)
            fin=[pin fnm{1} '.' myint2str(ts,10) '.data'];
            if exist(fin)
                fld=fld+readbin(fin,[nx nx*13]);
                n=n+1;
            else
                break
            end
        end
        if n==24
            writebin(fout,fld/n);
        else
            break
        end
    end
end
