% Hurricane Michael for Mar
% extract oceTAUX, oceTAUY, U, V, KPPhbl
% for 3-Sep-2012 to 17-Sep-2012
% lats 20N to 50N, lons 310E to 330E
% (example extraction on face 5, i.e., rotated UV fields)

% define desired region
nx=2160;
prec='real*4';
region_name='Michael';
minlat=25;
maxlat=45;
minlon=312;
maxlon=322;
mints=dte2ts('03-Sep-2012',45,2011,1,17);
maxts=dte2ts('17-Nov-2012',45,2011,1,17);

% extract indices for desired region
gdir='/nobackupp2/dmenemen/llc_2160/grid_withlakes/';
fnam=[gdir 'Depth.data'];
[fld fc ix jx] = ...
    quikread_llc(fnam,nx,1,prec,gdir,minlat,maxlat,minlon,maxlon);
quikpcolor(fld'); caxis([0 1])
kx=1;

% Get and save grid information
close all
pin='~dmenemen/llc_2160/grid_withlakes/';
pout=['~dmenemen/llc_2160/regions/' region_name '/grid/'];
eval(['mkdir ' pout])
eval(['cd ' pout])
suf1=['_' int2str(length(ix)) 'x' int2str(length(jx))];
suf2=[suf1 'x' int2str(length(kx))];

% Grid cell center
for fnm={'Depth','RAC','XC','YC','hFacC'}
    fin=[pin fnm{1} '.data'];
    switch fnm{1}
      case{'hFacC'}
        fld=read_llc_fkij(fin,nx,fc,kx,ix,jx);
        fout=[fnm{1} suf2];
      otherwise
        fld=read_llc_fkij(fin,nx,fc,1,ix,jx);
        fout=[fnm{1} suf1];
    end
    writebin(fout,fld);
end

% Southwest corner (vorticity) points, no direction
for fnm={'XG','YG','RAZ'}
    fin=[pin fnm{1} '.data'];
    fout=[fnm{1} suf1];
    for f=1:length(fc)
        switch fc(f)
          case {1,2}
            fld=read_llc_fkij(fin,nx,fc,1,ix,jx);
          case {4,5}
            fld=read_llc_fkij(fin,nx,fc,1,ix,jx-1); % <<<<<<<<
        end
    end
    writebin(fout,fld);
end

% West edge points, no direction
fnx='DXC';
fny='DYC';
finx=[pin fnx '.data'];
finy=[pin fny '.data'];
foutx=[fnx suf1];
fouty=[fny suf1];
for f=1:length(fc)
    switch fc(f)
      case {1,2}
        fldx=read_llc_fkij(finx,nx,fc,1,ix,jx);
        fldy=read_llc_fkij(finy,nx,fc,1,ix,jx);
      case {4,5}
        fldx=read_llc_fkij(finy,nx,fc,1,ix,jx);
        fldy=read_llc_fkij(finx,nx,fc,1,ix,jx-1); % <<<<<<<<
    end
end
writebin(foutx,fldx);
writebin(fouty,fldy);

% Southwest corner (vorticity) points, no direction
fnx='DXV';
fny='DYU';
finx=[pin fnx '.data'];
finy=[pin fny '.data'];
foutx=[fnx suf1];
fouty=[fny suf1];
for f=1:length(fc)
    switch fc(f)
      case {1,2}
        fldx=read_llc_fkij(finx,nx,fc,1,ix,jx);
        fldy=read_llc_fkij(finy,nx,fc,1,ix,jx);
      case {4,5}
        fldx=read_llc_fkij(finy,nx,fc,1,ix,jx-1); % <<<<<<<<
        fldy=read_llc_fkij(finx,nx,fc,1,ix,jx-1); % <<<<<<<<
    end
end
writebin(foutx,fldx);
writebin(fouty,fldy);

% Southwest edge points, no direction
fnx='DXG';
fny='DYG';
finx=[pin fnx '.data'];
finy=[pin fny '.data'];
foutx=[fnx suf1];
fouty=[fny suf1];
for f=1:length(fc)
    switch fc(f)
      case {1,2}
        fldx=read_llc_fkij(finx,nx,fc,1,ix,jx);
        fldy=read_llc_fkij(finy,nx,fc,1,ix,jx);
      case {4,5}
        fldx=read_llc_fkij(finy,nx,fc,1,ix,jx-1); % <<<<<<<<
        fldy=read_llc_fkij(finx,nx,fc,1,ix,jx);
    end
end
writebin(foutx,fldx);
writebin(fouty,fldy);

% create commands for extracting model output fields
pout=['~dmenemen/llc_2160/regions/' region_name '/'];
extract='/home4/bcnelson/MITgcm/extract/v1.10/extract2160 -g ';
timesteps=[int2str(mints) '-' int2str(maxts) ' '];
startPoint=[int2str(3*nx+min(ix)) ',' int2str(min(jx)) ',1 '];

% get and save regional 2D tracer fields
extent=[int2str(length(ix)) ',' int2str(length(jx)) ',1'];
for fnm={'KPPhbl'}
    eval(['mkdir ' pout fnm{1}])
    eval(['cd ' pout fnm{1}])
    disp(['cd ' pout fnm{1}])
    fieldNames=[fnm{1} ' '];
    system([extract timesteps  fieldNames  startPoint  extent '  > joblist']);
    disp('parallel --slf $PBS_NODEFILE -j2 -a joblist')
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
% there is a -1 index offset -U meridional velocity in faces 4/5
% example below is for face 4

% U
startPoint=[int2str(3*nx+min(ix)) ',' int2str(min(jx)) ',1 '];
extent=[int2str(length(ix)) ',' int2str(length(jx)) ',' int2str(kx(end))];
eval(['mkdir ' pout 'U'])
eval(['cd ' pout 'U'])
fieldNames=['V' ' '];
system([extract timesteps  fieldNames  startPoint  extent ' > joblist']);
system('sed -i ''s/_V_/_U_/'' joblist')
disp(['cd ' pout 'U'])
disp('parallel --slf $PBS_NODEFILE -j2 -a joblist')

% V
startPoint=[int2str(3*nx+min(ix)) ',' int2str(min(jx)-1) ',1 '];
extent=[int2str(length(ix)) ',' int2str(length(jx)) ',' int2str(kx(end))];
eval(['mkdir ' pout 'V'])
eval(['cd ' pout 'V'])
fieldNames=['U' ' '];
system([extract '-n ' timesteps  fieldNames  startPoint  extent ' > joblist']);
system('sed -i ''s/_U_/_V_/'' joblist');
system(['sed -i ''s/' int2str(length(kx)) '_Neg/' int2str(length(kx)) '/'' joblist']);
disp(['cd ' pout 'V'])
disp('parallel --slf $PBS_NODEFILE -j2 -a joblist')

% oceTAUX
startPoint=[int2str(3*nx+min(ix)) ',' int2str(min(jx)) ',1 '];
extent=[int2str(length(ix)) ',' int2str(length(jx)) ',1'];
eval(['mkdir ' pout 'oceTAUX'])
eval(['cd ' pout 'oceTAUX'])
fieldNames=['oceTAUY' ' '];
system([extract timesteps  fieldNames  startPoint  extent ' > joblist']);
system('sed -i ''s/_oceTAUY_/_oceTAUX_/'' joblist')
disp(['cd ' pout 'oceTAUX'])
disp('parallel --slf $PBS_NODEFILE -j2 -a joblist')

% oceTAUY
startPoint=[int2str(3*nx+min(ix)) ',' int2str(min(jx)-1) ',1 '];
extent=[int2str(length(ix)) ',' int2str(length(jx)) ',1'];
eval(['mkdir ' pout 'oceTAUY'])
eval(['cd ' pout 'oceTAUY'])
fieldNames=['oceTAUX' ' '];
system([extract '-n ' timesteps  fieldNames  startPoint  extent ' > joblist']);
system('sed -i ''s/_oceTAUX_/_oceTAUY_/'' joblist');
system(['sed -i ''s/1_Neg/1/'' joblist']);
disp(['cd ' pout 'oceTAUY'])
disp('parallel --slf $PBS_NODEFILE -j2 -a joblist')
