function fldOut= merge_tiles_nordic_seas_tracer(varName,dirvar,iz,itime,flag_field)
%-----
%function fldOut= merge_tiles_nordic_seas_tracer(varName,dirvar,iz,itime,flag_field)
%--
% -- Function to create tracer field in the Nordic Seas region from ASTE itile [5 6 7 8 11 14]
% Input:
% varName (TRACER, e.g., hFacC, Depth, THETA, ETAN) , 
%         can enter as cell or char, will be converted to cell
% dirvar: directory where varName is stored 
%         /full_path/nctiles_grid for grid, 
%         /full_path/nctiles_monthly for monthly fields
%         /full_path/nctiles_monthly_climatology for climatology fields
% iz: depth level(s), [1] for 2D field, [1,5,7] or [1:nz] etc for 3D fields
% itime: time indices [1] for grid, between [1:192] for model fields, 
%        between [1:12] for climatology 
% flag_field: -1 for grid, 0 for time-dependent (monthly), 1 for climatology
%
% Output: 
%  fldOut: X Y Z t. depth levels is 3.-dim, time 4th
% 
% ATN Nov.2020, mod Feb.2021
% mod by MFJ Dec.2020
%----

%%tiles variable is now global
global tiles

%if missing argument , send error
if(nargin<5);
  fprintf('fldOut= merge_tiles_nordic_seas_tracer(varName,dirvar,iz,itime,flag_field)\n');
  error('inside function ''merge_tiles_nordic_seas_tracer'', need 5 total arguments');
end;

%atn: move path outside of function, now takes dirvar as input
if(~exist(dirvar));error('dirvar not exist');end;

%For grid, varName needs to be cell; however, for other fields, they need to be char
%here we simply convert to cell; 
aa=whos('varName');if(strmatch(aa.class,'char'));varName={varName};end;

%=============================================================================
%=============================================================================
%======================TILE ASTE  ====== =====================================
%=============================================================================
%
%tracer field:

%For the Nordic Seas, this itile list below is hardcoded
itile=[5 6 7 8 11 14];	%tile 5-7 on face1 tile 8,11,14 on face3
flag_aste=0;		%1 to put output into full ASTE domain compact size, 0 to leave as individual tiles (to save memory)
xytile=tiles.tsize(1);	%90	%make assumption xtile = ytile, which is true in the case of ASTE R1

%read data
if flag_field==0|flag_field==1	%monthly fields or climatology, need to take varName{1} <--> cell to char
  [field_tracer time_step]=nctiles2aste_v2(varName{1},[dirvar],itile,iz,itime,tiles,flag_aste);
else				%grid
  itime=1;%reduce itime = 1 number as it's only a dummy, takes varName = cell
  tmp = nctiles_grid(varName,[dirvar],itile,tiles,flag_aste);
  for j=1:length(itile);
    field_tracer{j}=tmp{j}.(varName{1})(:,:,iz);
  end;
end

%create an array of size 90*3 x 90*2 x iz x itime
fldOut=zeros(xytile*3,xytile*2,length(iz),length(itime));
fldOut(:,1:xytile,:,:)=cat(1,field_tracer{1},field_tracer{2},field_tracer{3});	%tile 5,6,7, size 270 x 90
tmp                   =cat(2,field_tracer{4},field_tracer{5},field_tracer{6});	%tile 8,11,14, size 90 x 270
%NOW rotate 90deg ccw (DO NOT USE TRANSPOSE, as it's a mirror flip instead of rotation)
%the function sym_g_mod rotates: ccw with combo (5,0), cw with combo (7,0), 180deg with combo (6,0)
tmp=sym_g_mod(tmp,5,0);	%5,0 for ccw
fldOut(:,xytile+1:2*xytile,:,:)=tmp;

figure(1);clf;imagesc(1:3*xytile,1:2*xytile,squeeze(fldOut(:,:,1,1))');colorbar;grid;
    set(gca,'ydir','normal');title([varName{1} ' iz=' num2str(iz(1)) ' itime=' num2str(itime(1))]);

