function[]=get_grid_noblank(nx,dirRoot,list_fields)
%------------------
%function[]=get_grid_noblank(nx,dirRoot)
% nx: llc family, [90,270,540,1080,2160,4320]
% dirRoot: locations where global tile* or llc* files 
%          are found inside either 
%          [dirRoot 'run_template/'] or [dirRoot 'GRID/']
% list_fields: cell, {'XC','YC','DXG','DYG}, etc
%
% output: global compact format files from the list_fields 
% atn 17.feb.2018
%----
      
%dirRoot='/scratch/atnguyen/llc540/global/';

%check if grid directory exists
dirGridglobal=[dirRoot '/run_template/'];
if(exist(dirGridglobal))
  flist=dir([dirGridglobal 'tile*.mitgrid']);
  if(length(flist)==0);
    flist=dir([dirGridglobal 'llc_*.bin']);%
    if(length(flist)==0);
      dirGridglobal=[dirRoot 'GRID/'];
      if(~exist(dirGridglobal));fprintf('%s ',dirGridglobal);error(': dirGridglobal missing');end;
      flist=dir([dirGridglobal 'tile*.mitgrid']);
      if(length(flist)==0);
        flist=dir([dirGridglobal 'llc_*.bin']);%
        if(length(flist)==0); error('missing llc*.bin or tile*.mitgrid in global grid dir'); end;
      end
    end
  end;
else;
      dirGridglobal=[dirRoot 'GRID/'];
      if(~exist(dirGridglobal));fprintf('%s ',dirGridglobal);error(': dirGridglobal missing');end;
      flist=dir([dirGridglobal 'tile*.mitgrid']);
      if(length(flist)==0);
        flist=dir([dirGridglobal 'llc_*.bin']);%
        if(length(flist)==0); error('missing llc*.bin or tile*.mitgrid in global grid dir'); end;
      end
end;

dirGridnb=[dirRoot 'GRID_noblank/'];if(~exist(dirGridnb));mkdir(dirGridnb);end;

%nx=540;

list_fields_ref={'XC','YC','DXF','DYF','RAC','XG','YG','DXV','DYU','RAZ',...
    'DXC','DYC','RAW','RAS','DXG','DYG'};

nfy1=[3*nx+1 3*nx+1 nx+1 nx+1 nx+1];nfx1=[nx+1 nx+1 nx+1 3*nx+1 3*nx+1];

%loop through once to find index
ind=[];
for i=1:size(list_fields,2)
  clear imatch;
  imatch=strmatch(list_fields{i},list_fields_ref);
  if(length(imatch)>0);
    ind=[ind imatch];
  end;
end;

%for id=[1,2,5,11,12,15,16];
for id=ind;
  for iface=1:5;
    tmp=read_slice([dirGridglobal flist(iface).name],nfx1(iface),nfy1(iface),id,'real*8');
    ff{iface}=tmp(1:nfx1(iface)-1,1:nfy1(iface)-1);
  end;
  ffc=cat(2,ff{1},ff{2},ff{3},reshape(ff{4},nx,3*nx),reshape(ff{5},nx,3*nx));
  fOut=[dirGridnb list_fields_ref{id} '.data'];writebin(fOut,ffc,1,'real*8');
  fprintf('%s\n',fOut);
end;

return
