% extract SST, SSS, SIarea, and SIheff for East Greenland (May-July)
% 72N to 80N, -20E to 20E
% define desired region
region_name='EastGreenland';
minlat=74;
maxlat=82;
minlon=-30;
maxlon=20;
nx=4320;
prec='real*4';
kx=1;

% extract indices for desired region
gdir='~dmenemen/llc_4320/grid/';
fnam=[gdir 'Depth.data'];
[fld fc ix jx]=quikread_llc(fnam,nx,1,prec,gdir,minlat,maxlat,minlon,maxlon);
fld=fld{3};
fc=3;
ix=ix{3};
jx=jx{3};
quikpcolor(rot90(fld,1)')
thincolorbar

% get and save grid information
close all
pin='~dmenemen/llc_4320/grid/';
pout=['~dmenemen/llc_4320/regions/' region_name '/grid/'];
eval(['mkdir ' pout])
eval(['cd ' pout])
for fnm={'Depth','RAC','XC','YC','hFacC'}
    fin=[pin fnm{1} '.data'];
    fout=[fnm{1} '_' int2str(length(ix)) 'x' int2str(length(jx))];
    switch fnm{1}
      case{'hFacC'}
        fld=read_llc_fkij(fin,nx,fc,kx,ix,jx);
        fout=[fout 'x' int2str(length(kx))];
      otherwise
        fld=read_llc_fkij(fin,nx,fc,1,ix,jx);
    end
    writebin(fout,fld);
end

% get and save regional SST, SSS, SIarea, SIheff 
pin='~dmenemen/llc_4320/MITgcm/run';
pout=['~dmenemen/llc_4320/regions/' region_name '/'];
for fnm={'Theta','Salt','SIarea','SIheff'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
    %    for ts=10368:144:1366128, mydisp(ts)
    for ts=622080, mydisp(ts)
        if ts<485568
            fin=[pin '/' fnm{1} '.' myint2str(ts,10) '.data'];
        else
            fin=[pin '_485568/' fnm{1} '.' myint2str(ts,10) '.data'];
        end
        dy=ts2dte(ts,25,2011,9,10,30);
        fout=[fnm{1} '_' int2str(length(ix)) 'x' int2str(length(jx)) '.' dy];
        fld=read_llc_fkij(fin,nx,fc,1,ix,jx);
        writebin(fout,fld);
    end
end

% get and save regional S/T/W
for fnm={'Salt','Theta','W'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
    for ts=10368:144:1366128, mydisp(ts)
        if ts<485568
            fin=[pin '/' fnm{1} '.' myint2str(ts,10) '.data'];
        else
            fin=[pin '_485568/' fnm{1} '.' myint2str(ts,10) '.data'];
        end
        dy=ts2dte(ts,25,2011,9,10,30);
        fout=[fnm{1} '_' int2str(length(ix)) 'x' int2str(length(jx)) 'x' int2str(length(kx)) '.' dy];
        for k=1:length(kx); mydisp(k)
            fld=read_llc_fkij(fin,nx,fc,kx(k),ix,jx);
            writebin(fout,fld,1,'real*4',k-1);
        end
    end
end

% get and save regional fields of U and V
% note that zonal velocity is U in faces 1/2 and V in faces 4/5
% and meridional velocity is V in faces 1/2 and -U in faces 4/5
eval(['mkdir ' pout 'U'])
eval(['mkdir ' pout 'V'])
eval(['cd ' pout])
for ts=10368:144:1366128, mydisp(ts)
    if ts<485568
        finu=[pin '/U.' myint2str(ts,10) '.data'];
        finv=[pin '/V.' myint2str(ts,10) '.data'];
    else
        finu=[pin '_485568/U.' myint2str(ts,10) '.data'];
        finv=[pin '_485568/V.' myint2str(ts,10) '.data'];
    end
    dy=ts2dte(ts,25,2011,9,10,30);
    foutu=['U/U_' int2str(length(ix)) 'x' int2str(length(jx)) 'x' int2str(length(kx)) '.' dy];
    foutv=['V/V_' int2str(length(ix)) 'x' int2str(length(jx)) 'x' int2str(length(kx)) '.' dy];
    for k=1:length(kx); mydisp(k)
        fld=read_llc_fkij(finv,nx,fc,kx(k),ix,jx);
        writebin(foutu,fld,1,'real*4',k-1);
        fld=-read_llc_fkij(finu,nx,fc,kx(k),ix,jx-1);
        writebin(foutv,fld,1,'real*4',k-1);
    end
end
