function [fldu,fldv]= merge_tiles_nordic_seas_vector(varName,dirvar,iz,itime,flag_sign,flag_field)
%-----
%function [fldu,fldv]= merge_tiles_nordic_seas_vector(fldu,fldv,dirvar,iz,itime,flag_sign,flag_field)
%--
% -- Function to create vector field pair in the Nordic Seas region from ASTE itile [5 6 7 8 11 14]
% Input:
% varName: VECTOR PAIR: e.g.,{'hFacW','hFacS'}, {'DXG','DYG'}, {'UVELMASS','VVELMASS'}, {'SIuice','SIvice'}
% 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
% flag_sign: 0 = NOT apply sign flip, appropriate for grid, 
%            1 =     apply sign flip, appropriate for velocity fields
%
% Output: 
%  fld[u,v]: X Y Z t. depth levels is 3.-dim, time 4th, 
%  The new pair of velocities are now appropriate for Nordic Seas region
% 
% 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<6);
  fprintf('[fldu,fldv]= merge_tiles_nordic_seas_vector(varName,dirvar,iz,itime,flag_sign,flag_field)\n');
  error('inside function ''merge_tiles_nordic_seas_vector'', need 6 total arguments');
end;

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

%For vector, because we need pair, varName will need to be cell

%=============================================================================
%=============================================================================
%======================TILE ASTE  ====== =====================================
%=============================================================================
%
%vector field:
% For the Nordic Seas, the itile list below is hardcoded.
% Because the c-grid stores at the edges, there is 1 more edge per face than center,
% such that during rotation, some edges are at the +1 position.  In addition, there 
% are also sign flips when converting v-> -u. So the safest approach is always to pad one 
% set of grid column from tile 27 onto tile 14. So read 1 more tile in compared to tracer.
%
itile=[5 6 7 8 11 14 27];%tile 5-7 on face1 tile 8,11,14 on face3 + tile 27 for padding
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
  [field_u time_step]=nctiles2aste_v2(varName{1},[dirvar],itile,iz,itime,tiles,flag_aste);
  [field_v time_step]=nctiles2aste_v2(varName{2},[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_u{j}=tmp{j}.(varName{1})(:,:,iz);
    field_v{j}=tmp{j}.(varName{2})(:,:,iz);
  end;
end

%padding for tile 14:g
fldu=zeros(xytile*3,xytile*2,length(iz),length(itime));fldv=zeros(xytile*3,xytile*2,length(iz),length(itime));
fldu(:,1:xytile,:,:)=cat(1,field_u{1},field_u{2},field_u{3});	%size 270 x 90 x iz x itime
fldv(:,1:xytile,:,:)=cat(1,field_v{1},field_v{2},field_v{3});	%size 270 x 90 x iz x itime

tmpu=cat(2,field_u{4},field_u{5},field_u{6});	%size 90 x 270 x iz x itime
tmpv=cat(2,field_v{4},field_v{5},field_v{6});	%size 90 x 270 x iz x itime

%getting the padding from tile 27:
tmp27u=sym_g_mod(field_u{7},5,0);		%making u --> v
tmpv=cat(2,tmpv,tmp27u(:,1,:,:));		%now size 90 x 271

%now rotating:
tmpu_into_v      = sym_g_mod(tmpu,5,0);		%u --> v , size 270x90
tmpv_into_minusu = sym_g_mod(tmpv,5,0);		%v --> -u, size 271x90

%now catting, also need to flip the temporary u sign:
fldu(:,xytile+1:2*xytile,:,:)=-tmpv_into_minusu(1:3*xytile,:,:,:);%note the minus sign, also takes only from 1:270
fldv(:,xytile+1:2*xytile,:,:)= tmpu_into_v;		

%finally treat signs, if flag_sign=0, we take abs.  This is appropriate for hFac[W,S], D[X,Y]*
if(flag_sign==0);
  fldu=abs(fldu);
  fldv=abs(fldv);
end;

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

