% Kerguelen Region for Lia Siegelman
% extract 01-Oct-2011 to 15-Nov-2012
% lats 60S to 40S, lons 30E to 100E
% (example extraction on face 1+2)

% define desired region
region_name='Kerguelen';
minlon=30;
maxlon=100;
minlat=-60;
maxlat=-40;
mints=dte2ts('01-Oct-2011',25,2011,9,10);
maxts=dte2ts('15-Nov-2012',25,2011,9,10);
nx=4320;
prec='real*4';

% extract indices for desired region
gdir='~dmenemen/llc_4320/grid/';
fnam=[gdir 'Depth.data'];
[tmp fc ix jx]=quikread_llc(fnam,nx,1,prec,gdir,minlat,maxlat,minlon,maxlon);
m(1)=0;
for f=1:length(fc)
    m(f+1)=length(ix{fc(f)});
end
n=length(jx{fc(1)});
fld=zeros(sum(m),n);
for f=1:length(fc)
    fld((sum(m(1:f))+1):sum(m(1:(f+1))),:)=tmp{fc(f)};
end
quikpcolor(fld'); colorbar
load ~dmenemen/llc_4320/grid/thk90.mat
bot90=dpt90+thk90/2; bot90(end)=bot90(end)+1;
kx=1:min(find(bot90>mmax(fld)));

% 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','XG','YG', ...
         'RAZ','DXC','DYC','DXV','DYU','DXG','DYG', ...
         'AngleCS','AngleSN','DXF','DYF','IDX','LandMask', ...
         'RAS','RAW','U2zonDir','V2zonDir','hFacS','hFacW'}    
    fin=[pin fnm{1} '.data'];
    fout=[fnm{1} '_' int2str(sum(m)) 'x' int2str(n)];
    switch fnm{1}
      case{'hFacC','hFacS','hFacW',}
        fout=[fout 'x' int2str(length(kx))];
        for k=1:length(kx); mydisp(k)
            for f=1:length(fc)
                fld((sum(m(1:f))+1):sum(m(1:(f+1))),:) = ...
                    read_llc_fkij(fin,nx,fc(f),kx(k),ix{fc(f)},jx{fc(f)});
            end
            writebin(fout,fld,1,prec,k-1);
        end
      otherwise
        for f=1:length(fc)
            fld((sum(m(1:f))+1):sum(m(1:(f+1))),:) = ...
                read_llc_fkij(fin,nx,fc(f),1,ix{fc(f)},jx{fc(f)});
        end
        writebin(fout,fld);
    end
end

% get and save 2D regional fields
pin='~dmenemen/llc_4320/MITgcm/run';
pout=['~dmenemen/llc_4320/regions/' region_name '/'];
for fnm={'Eta','KPPhbl','oceFWflx','oceQnet','oceQsw','oceSflux','oceTAUX','oceTAUY'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
    for ts=mints:144:maxts, 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(sum(m)) 'x' int2str(n) '.' dy];
        for f=1:length(fc)
            fld((sum(m(1:f))+1):sum(m(1:(f+1))),:) = ...
                read_llc_fkij(fin,nx,fc(f),1,ix{fc(f)},jx{fc(f)});
        end
        writebin(fout,fld);
    end
end

% get and save 3D regional fields
for fnm={'Salt','Theta','W','U','V'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
    for ts=mints:144:maxts, 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(sum(m)) 'x' int2str(n) 'x' int2str(length(kx)) '.' dy];
        for k=1:length(kx); mydisp(k)
            for f=1:length(fc)
                fld((sum(m(1:f))+1):sum(m(1:(f+1))),:) = ...
                    read_llc_fkij(fin,nx,fc(f),kx(k),ix{fc(f)},jx{fc(f)});
            end
            writebin(fout,fld,1,prec,k-1);
        end
    end
end
