clear, close all
cd ~dmenemen/llc_4320/grid
nx=4320;
pn='~/llc_4320/run_template/';

% define the 16 grid-dimension fields contained in the tile*.mitgrid files
fld={'XC','YC','DXF','DYF','RAC','XG','YG','DXV','DYU','RAZ','DXC','DYC','RAW','RAS','DXG','DYG'};

for f=1:length(fld)
    fnm=[fld{f} '.data'];
    disp(fnm)

    % extract field from tile*.mitgrid files
    tmp=zeros(nx*nx*13,1);
    tmp1=readbin([pn 'tile001.mitgrid'],[(nx+1) (nx*3+1)],1,'real*8',f-1);
    tmp2=tmp1(1:nx,1:nx*3);
    tmp(1:3*nx^2)=tmp2(:);
    tmp1=readbin([pn 'tile002.mitgrid'],[(nx+1) (nx*3+1)],1,'real*8',f-1);
    tmp2=tmp1(1:nx,1:nx*3);
    tmp(3*nx*nx+1:6*nx*nx)=tmp2(:);
    tmp1=readbin([pn 'tile003.mitgrid'],[(nx+1) (nx+1)],1,'real*8',f-1);
    tmp2=tmp1(1:nx,1:nx);
    tmp(6*nx*nx+1:7*nx*nx)=tmp2(:);
    tmp1=readbin([pn 'tile004.mitgrid'],[(nx*3+1) (nx+1)],1,'real*8',f-1);
    tmp2=tmp1(1:nx*3,1:nx);
    tmp(7*nx*nx+1:10*nx*nx)=tmp2(:);
    tmp1=readbin([pn 'tile005.mitgrid'],[(nx*3+1) (nx+1)],1,'real*8',f-1);
    tmp2=tmp1(1:nx*3,1:nx);
    tmp(10*nx*nx+1:13*nx*nx)=tmp2(:);
    
    %  write extracted field
    writebin(fnm,tmp)
end
