% extract sea ice fields in Arctic Ocean
nx=2160;
prec='real*4';
region_name='Arctic';
ix{1}=1:nx;
ix{2}=1:nx;
jx{1}=6001:(3*nx);
jx{2}=1:nx;

% get and save grid information
pin='~dmenemen/llc_2160/grid/';
pout=['~dmenemen/llc_2160/regions/' region_name '/grid/'];
eval(['mkdir ' pout])
eval(['cd ' pout])
for fnm={'AngleCS','AngleSN','DXC','DXG','DYC','DYG','Depth', ...
         'RAC','RAS','RAW','RAZ','U2zonDir','V2zonDir', ...
         'XC','XG','YC','YG','hFacC','hFacS','hFacW'}
    fin=[pin fnm{1} '.data'];
    fout=[fnm{1} '_' int2str(nx) 'x' int2str(length([jx{1} jx{2}]))];
    fld=[rot90(read_llc_fkij(fin,nx,3,1,ix{2},jx{2}),1) ...
         rot90(read_llc_fkij(fin,nx,4,1,ix{1},jx{1}),2)];
    writebin(fout,fld);
end

% get and save regional 2D fields
pn='~dmenemen/llc_2160/MITgcm/run';
pout=['~dmenemen/llc_2160/regions/' region_name '/'];
for fnm={'SIarea','SIheff','SIuice','SIvice'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
    for ts=92160:80:1586400; disp(ts)
        if (ts<1198080)
            pin=[pn '_day49_624/'];
        else
            pin=[pn '/'];
        end
        fin=[pin fnm{1} '.' myint2str(ts,10) '.data'];
        dy=ts2dte(ts,45,2011,1,17,30);
        fout=[fnm{1} '_' int2str(nx) 'x' int2str(length([jx{1} jx{2}])) '.' dy];
        fld=[rot90(read_llc_fkij(fin,nx,3,1,ix{2},jx{2}),1) ...
             rot90(read_llc_fkij(fin,nx,4,1,ix{1},jx{1}),2)];
        writebin(fout,fld);
    end
end
