function [grid_diag]=set_grid_diag_v2(avail_diag,tileName,gridDir);
%----
%function [grid_diag]=set_grid_diag_v2(avail_diag,tileName,gridDir);
%This routine is copied/adapted from gcmfaces_IO/process2nctiles.m
%
%Extract GRID information to be written in diag output nc files
%
%Input:
%       avail_diag      a matlab structure with some information on diag
%                       avail_diag is the output of read_avail_diag
%       tileName        The tile being processed
%       gridDir         Directory with domain grid in binary tile form
%
%Output:
%       grid_diag       structure with the relevant grid variables needed for nc output
%                       will be different for 2D and 3D diags
%e.g.:
%>> grid_diag
%grid_diag = 
%  struct with fields:
%
%    lon: [90×90 double]
%    lat: [90×90 double]
%    msk: [90×90×nz double]
%    RAC: [90×90 double]
%    dep: [1×1×nz double]
%     dz: [1×1×nz double]
%
% Created by Victor Ocana 2019
% modified by An Nguyen Jul 2022
%--

names = {'lon','lat','msk','RAC'};
%switch for non-tracer point values
if strcmp(avail_diag.loc_h,'C');
    namesg ={'XC','YC','hFacC','RAC'};%maskCtrlC: need 3d
elseif strcmp(avail_diag.loc_h,'W');
    namesg ={'XG','YC','hFacW','RAC'};%maskCtrlW
elseif strcmp(avail_diag.loc_h,'S');
    namesg ={'XC','YG','hFacS','RAC'};%maskCtrlS
end;

for ii=1:length(names);
   fileN = [gridDir namesg{ii} '.' tileName '.data'];
   temp = dir([gridDir namesg{ii} '*.meta']);   
   temp1 = read_meta(temp.name,gridDir);
   nprec=str2num(temp1.dataprec(end-1:end))/8; prec=['real*' num2str(nprec)];
   for idim=1:temp1.nDims; 
      dims(idim)=temp1.dimList(1+3*(idim-1));
   end;
   if (temp1.nDims==3 && avail_diag.nr==1); 
      dims(temp1.nDims) = avail_diag.nr;
   end;
   eval(['grid_diag.' names{ii} '=readbin(fileN,dims,1,prec);']);
   clear temp temp1 nprec prec dims;
end;

%atn: set mask[C,W,S] to be binary
grid_diag.msk(find(grid_diag.msk>0))=1;

%vertical grid (for 3D diags)
clear names namesg;
if avail_diag.nr~=1;
   names = {'dep','dz'};
   if strcmp(avail_diag.loc_z,'M');
      namesg= {'RC','DRF'};
   elseif strcmp(avail_diag.loc_z,'L');
      namesg= {'RF','DRC'};
   elseif strcmp(avail_diag.loc_z,'U');
      namesg= {'RF','DRC'};
   end;
   for ii=1:length(names);
      fileN = [gridDir namesg{ii}  '.data'];
      temp = dir([gridDir namesg{ii} '*.meta']);   
      temp1 = read_meta(temp.name,gridDir);
      nprec=str2num(temp1.dataprec(end-1:end))/8; prec=['real*' num2str(nprec)];
      dims = temp1.dimList(7);
      eval(['grid_diag.' names{ii} '=readbin(fileN,dims,1,prec);']);
      clear temp temp1 nprec prec;
   end;
   if strcmp(avail_diag.loc_z,'M');
       %grid_diag.dep=-grid_diag.dep;
       grid_diag.dep=abs(grid_diag.dep);
       grid_diag.dz=grid_diag.dz;
   elseif strcmp(avail_diag.loc_z,'L');
       %grid_diag.dep=-grid_diag.dep(2:end);
       grid_diag.dep=abs(grid_diag.dep(2:end));
       grid_diag.dz=grid_diag.dz(2:end);
   elseif strcmp(avail_diag.loc_z,'U');
       %grid_diag.dep=-grid_diag.dep(1:end-1);
       grid_diag.dep=abs(grid_diag.dep(1:end-1));
       grid_diag.dz=grid_diag.dz(1:end-1);
   else;
       error('unknown vertical grid');
   end;
%
   grid_diag.dep = reshape(grid_diag.dep,[1 1 avail_diag.nr]);
   grid_diag.dz  = reshape(grid_diag.dz,[1 1 avail_diag.nr]);
end;



