% extract T/S in Gulf of Mexico
% 26N to 31N, 270E to 276E
% from March 2013 to end of simulation

% define desired region
nx=2160;
region_name='Gulf_south';
minlat=26;
maxlat=31;
minlon=270;
maxlon=276;

kx=1;
minlon=-69.677086;
maxlon=-59.677082;

minlat=26.641497;
maxlat=41.811443;


% extract indices for desired region
prec='real*4';
pin=['~dmenemen/llc_' int2str(nx) '/grid/'];
fnam=[pin 'Depth.data'];
gdir=pin;

[fld fc ix jx]=quikread_llc(fnam,nx,1,prec,gdir,minlat,maxlat,minlon,maxlon);
quikpcolor(fld')
thincolorbar

% get and save grid information
close all
pout=['/nobackup/jwang23/llc_' int2str(nx) '/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(length(ix)) 'x' int2str(length(jx))];
    switch fnm{1}
      case{'hFacC','hFacS','hFacW'}
        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 S/T

pin='~dmenemen/llc_2160/MITgcm/run/';
pout=['/nobackup/jwang23/llc_2160/regions/' region_name '/'];
for fnm={'Eta'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
    for ts=92160:80:1586400
%    for ts=1486080:80:1586400
        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(length(ix)) 'x' int2str(length(jx)) ...
              'x' int2str(length(kx)) '.' dy];
        disp(fout)
        for k=1:length(kx);
            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 S/T
kx=1:10;
pin='~dmenemen/llc_2160/MITgcm/run/';
pout=['/nobackup/jwang23/llc_2160/regions/' region_name '/'];
for fnm={'U','V'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
%    for ts=92160:80:1586400
    for ts=1486080:80:1586400
        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(length(ix)) 'x' int2str(length(jx)) ...
              'x' int2str(length(kx)) '.' dy];
        disp(fout)
        for k=1:length(kx);
            fld=read_llc_fkij(fin,nx,fc,kx(k),ix,jx);
            writebin(fout,fld,1,'real*4',k-1);
        end
    end
end
